React Number Input
A glass React number input with steppers you can hold, a scrub label you can drag, and Intl formatting for currency, percentages and units.
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/number-field.jsonConcert tickets
$24 each, up to 10 per order.
Total: $24
NumberFielddefaultNumberFieldLabels
Using it in a form? See the react-hook-form and zod recipe.
Files to copy
5 folders · 7 files · 4 packages
- From
theme/src/ components/number-field/hooks/{use-latest.ts, use-media-query.ts, use-preferences.ts}motion/tokens.tslib/{cn.ts, field-size.ts, form.ts}styles/
- shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/number-field.jsonRegistry item: number-field. The styles need the Tailwind v4 glass UI setup.- The examples also use
components/button/components/spinner/hooks/use-magnetic.tsmotion/gate.tslib/state-light.tsx
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json- Packages
pnpm add @base-ui/react clsx motion tailwind-merge
Steppers
Hold a button to repeat. Arrow keys step by 1, Shift by 10; Home and End jump to the limits. The buttons switch off at min and max. Reset sets the value from outside, and the digits still roll the right way.
Up to 20 per order.
Value: 2
import { useState } from "react";
import { Button } from "@/components/sheenui/button/button";
import { NumberField } from "@/components/sheenui/number-field/number-field";
// Package build (after the first npm release, or a packed tarball): import { Button, NumberField } from "@sheenui/theme";
// This example also uses button: npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/button.json
export function Example() {
const [quantity, setQuantity] = useState<number | null>(2);
return (
<>
<NumberField
label="Quantity"
value={quantity}
onValueChange={setQuantity}
min={1}
max={20}
description="Up to 20 per order."
/>
{/* A change from outside rolls the digits the right way too. */}
<Button variant="ghost" size="sm" onClick={() => setQuantity(1)}>
Reset
</Button>
</>
);
}Formatting
Pass any Intl.NumberFormat options. Typing is parsed back in the same format and locale.
import { NumberField } from "@/components/sheenui/number-field/number-field";
// Package build (after the first npm release, or a packed tarball): import { NumberField } from "@sheenui/theme";
export function Example() {
return (
<>
<NumberField
label="Price"
defaultValue={49}
step={0.5}
min={0}
format={{ style: "currency", currency: "USD", currencyDisplay: "narrowSymbol" }}
/>
<NumberField
label="Discount"
defaultValue={0.15}
step={0.05}
min={0}
max={1}
steppers="stacked"
format={{ style: "percent" }}
/>
<NumberField
label="Weight"
defaultValue={2.5}
step={0.1}
min={0}
steppers="stacked"
format={{ style: "unit", unit: "kilogram", maximumFractionDigits: 1 }}
/>
</>
);
}Scrub and validation
With scrub, drag the label sideways to change the value (the pointer is captured, so you can keep dragging). Errors are linked to the input and announced politely when they appear.
import { useState } from "react";
import { NumberField } from "@/components/sheenui/number-field/number-field";
// Package build (after the first npm release, or a packed tarball): import { NumberField } from "@sheenui/theme";
export function Example() {
const [guests, setGuests] = useState<number | null>(2);
return (
<>
<NumberField
label="Opacity"
scrub
defaultValue={0.8}
step={0.01}
min={0}
max={1}
format={{ style: "percent" }}
/>
<NumberField
label="Guests"
value={guests}
onValueChange={setGuests}
min={1}
error={guests !== null && guests > 10 ? "The room seats 10 people at most." : undefined}
/>
</>
);
}Props
NumberFieldLabels keys and English defaults (2)
Keyboard
Accessibility
- A text input with inputmode and aria-roledescription="Number field"; the - and + buttons are named by labels.decrement and labels.increment.
- An error is announced politely. Buttons are 44 px targets on touch screens.
- The odometer motion is decorative; under reduced motion digits change in place.
All exports on this page
NumberField— A glass number field on Base UI's NumberField, for quantities, prices and settings where people nudge a number or type an exact one.defaultNumberFieldLabels— The English NumberFieldLabels used when labels is omitted; a partial labels prop is merged over it.
When to use the number field
- Use it as a number stepper for a quantity picker in a cart or a guest count on a booking form, where the minus and plus buttons repeat while held and min and max clamp the value.
- Use it for a price or percentage input that must show currency formatting: the format prop takes Intl.NumberFormat options, and typed text is parsed back in the same locale format.
- Use it in a settings panel or design tool where scrub lets people drag the label sideways to change the value, like the numeric fields of an editor.
- Skip it when a rough value on a scale is enough, such as volume or opacity; a Slider fits that better, and plain Input fits free text like a postal code or a phone number.
- Pair it with its description and error props for helper text and the message, SegmentedControl for a unit switch beside the number, and Button for an apply action after editing several values.
Frequently asked questions
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .