- Components
Number field
A number you can type or step, with minus and plus buttons: the WAI-ARIA spin button.
npx shadcn add @klarhimmel/number-field @klarhimmel/inputWhen to use
Use for
A quantity people may know or want to nudge: guests, nights, quantity.
Not for
A rough value on a range. Use Slider. A choice among named options. Use Select.
Pairs with
Field, a unit, a status line.
Preview
Up and Down add one, Page Up and Page Down add ten.
2 guests
number-field.tsx
import { Theme } from '@/components/klarhimmel/theme/style';
import { Field } from '@/components/klarhimmel/ui/input';
import { NumberField } from '@/components/klarhimmel/ui/number-field';
export default function Example() {
return (
<Theme theme="brand">
<Field label="Guests">
{(p) => <NumberField {...p} defaultValue={2} min={1} max={20} />}
</Field>
</Theme>
);
}Install
Import
import { NumberField } from '@/components/klarhimmel/ui/number-field';- Lands in
- components/klarhimmel/ui/number-field.tsx, plus cn() and the theme item if they are missing. Import theme.css after Tailwind.
- Render in
- Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.
Examples
Decimal
kgSteps of a quarter kilo. Type 12,5 or 12.5.
nightsimport { Field } from '@/components/klarhimmel/ui/input'; import { NumberField } from '@/components/klarhimmel/ui/number-field'; export default function NumberFieldDecimal() { return ( <div className="flex w-[min(24rem,calc(100cqw-3rem))] flex-col gap-5"> <Field label="Luggage weight" hint="Steps of a quarter kilo. Type 12,5 or 12.5."> {(p) => <NumberField {...p} defaultValue={12.5} step={0.25} min={0} max={32} unit="kg" />} </Field> <Field label="Nights"> {(p) => <NumberField {...p} defaultValue={3} min={1} max={30} unit="nights" decreaseLabel="One night fewer" increaseLabel="One night more" />} </Field> </div> ); }States
The first Up goes to the minimum.
At most two extra beds per room.
import { Field } from '@/components/klarhimmel/ui/input'; import { NumberField } from '@/components/klarhimmel/ui/number-field'; export default function NumberFieldStates() { return ( <div className="flex w-[min(24rem,calc(100cqw-3rem))] flex-col gap-5"> <Field label="Rooms (empty)" hint="The first Up goes to the minimum."> {(p) => <NumberField {...p} min={1} max={10} />} </Field> <Field label="Extra beds" error="At most two extra beds per room."> {(p) => <NumberField {...p} defaultValue={3} max={3} invalid />} </Field> <Field label="Children (set by the room)"> {(p) => <NumberField {...p} defaultValue={2} disabled />} </Field> </div> ); }
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
value / defaultValue / onValueChange | number | null / (value: number | null) => void | - | Controlled or uncontrolled. null means empty. |
min / max | number | - | Limits for the keys, the buttons and typed text. |
step / largeStep | number | 1 / ten steps | What the arrows and the buttons add; what Page Up and Page Down add. The decimals of step set how many the field shows. |
unit | string | - | Shown after the number and said with it (aria-valuetext). |
name | string | - | Submits the number with a form (a hidden input). |
invalid / disabled | boolean | false | Error styling; switches it off. |
decreaseLabel / increaseLabel / locale | string | 'Decrease' / 'Increase' / 'en' | The two buttons' names, and the language numbers are written in. Pass them translated. |
aria-label / aria-labelledby | string | - | Needed when no Field or <label htmlFor> names it. |
className | string | - | Extra classes, merged after the component's own. |
Accessibility
| Key | Does |
|---|---|
Up / Down | Add or subtract the step; from empty, Up goes to the minimum |
Page Up / Page Down | Add or subtract ten steps (or largeStep) |
Home / End | Jump to the minimum or the maximum, when there is one |
Enter / leaving the field | Reads what was typed, inside the limits; a non-number is put back |
The WAI-ARIA spin button. The buttons are for the pointer and are not tab stops: the keyboard has the arrow keys. A decimal comma is accepted.
Accessibility