shadcn.io is an independent project built on shadcn/ui
Components
Free
Number Input for Svelte UI SDK
A field for one number, with step buttons, limits it stays inside, and currency or percent formatting. For quantities and prices in Svelte on UI SDK.
On this page9 sections
A number input is a text field for one number, with buttons that step it up and down and limits it won't leave. Reach for it when the number has a minimum, a maximum or a natural step: a cart quantity, a price, a percentage.
A postal code or a card number is a string of digits, not a quantity, so it belongs in an Input. When people pick a value by feel rather than type an exact one, a Slider is quicker.
Installation
Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.
The input is a plain text field that keeps letters out, and the keys below step it. invalid marks it with aria-invalid. The step buttons are labeled, left out of the tab order, and disabled at min and max. Inside a Field, UI SDK ties the FieldLabel to the input, as in With field, or put an aria-label on NumberInputInput. The label of a NumberInputScrubber is tied for you.
KeyAction
Arrow Up
Raises the value by one step.
Arrow Down
Lowers the value by one step.
Shift+Arrow Up
Raises the value by ten steps.
Shift+Arrow Down
Lowers the value by ten steps.
Alt+Arrow Up
Raises the value by a tenth of a step.
Alt+Arrow Down
Lowers the value by a tenth of a step.
Page Up
Raises the value by ten steps, as Shift + ArrowUp does.
Page Down
Lowers the value by ten steps, as Shift + ArrowDown does.
Home
When min is set, sets the value to it.
End
When max is set, sets the value to it, or to the last step below it when the steps don't land on it.
Enter
Commits the typed number, clamped to the range and rounded to the step, and lets the form submit.
Demos
Controlled
The number lives in your own state, starting at 1, and the line under the field changes from "Not 3 yet." to "That's 3." once the value is 3. Control it when another part of the screen reads the number, such as a cart total or a price summary. Leave it uncontrolled when only the form needs it.
Disabled
With disabled on NumberInput, the field and both buttons fade and stop responding, and the value is left out when the form submits. Use it for a number this person can't change right now, such as seats fixed by their plan, with a line nearby that says why. When the value must still reach the server, set readOnly instead.
Field only
The group holds only NumberInputInput, starting at 0, with no step buttons. People type the number or step it with the arrow keys. Use it in a table cell or a dense toolbar, where two buttons per field would crowd the row. Letters still can't get in.
Form
A Quantity field from 1 to 10 sits in a form under the name quantity, and Add to cart reads the form data the way a server would and prints what it got, such as quantity=3. The name goes on NumberInput. Use a plain form like this when the number goes out with other fields, such as a cart line.
Formatted
formatOptions set to US dollars shows the starting value, 19, as $19.00, and every step keeps the currency format. Use it for prices and other amounts where the format is part of what people read. When you only want a symbol beside a plain number, the With input group demo puts one there instead.
Invalid
invalid on NumberInput turns the border red around a value of 32 and tells screen readers the input is invalid. It doesn't stop typing or stepping, and the field can't say what is wrong, so pair it with a message such as "Only 20 left in stock". Use it for rules that min and max can't express.
Mouse wheel
The field starts at 10 with allowMouseWheel set, so turning the wheel over it steps the value. That only happens while the input has focus and the pointer is over it, so scrolling the page past the field leaves it alone. Turn it on in an editor panel where the pointer already rests on the field.
Range
min is 0, max is 10 and the field starts at 5. The buttons stop at either end, and a typed number past them is pulled back into range. Set both for a rating, a percentage or a small count with a fixed band, and only min when the one rule is no negatives.
Scrub
The Quantity label above the field is a NumberInputScrubber: press on it and drag sideways, and the value follows, the way design tools let you nudge a size. The buttons and typing still work. Add it where people adjust numbers by eye, such as dimensions in an editor, and leave it off forms people fill in on a phone.
Size lg
size set to lg raises the input and its buttons from 32 to 36 pixels. Use it when the number is the main control on the screen, such as the quantity on a product page, or when it sits in a row of other large inputs and buttons.
Size md
md is the default size, written out here so you can compare it with the other two: the input and its buttons are 32 pixels tall, the height of the default Input. Leave size out in most forms and the field lines up with its neighbors.
Size sm
size set to sm brings the input and its buttons down to 28 pixels, for a table row, a toolbar or a crowded side panel. On a touch screen each button keeps a 44-pixel tap area at every size, so the small one costs no reach. Keep a visible label, since a short box is easy to overlook.
Step
Two fields: one moves by 5 from 0, the other by 0.1 from 0.1. The step sets how far one press of a button moves the value. Pick one people can predict from the label, whole units for stock counts and tenths for a measurement such as a weight in kilograms.
With field
The input sits in a Field under the label Quantity, with the hint "0 - 100 only" below it and min and max set to match. Use a Field when the number needs a visible label, a hint and later an error message. UI SDK ties the label to the input through the Field.
With input group
The number sits in an Input Group between a euro sign and the letters EUR, with no step buttons. Use it when the unit is decoration around a plain number and a currency format would change how the number reads. The aria-label on NumberInputInput names the input, since the symbols can't.
FAQ
How do I control the Number Input value in Svelte?
Bind the value with bind:value on NumberInput, which holds the number, or null while the field is empty. Or pass value and update it in onValueChange, which receives the same number first and details second. UI SDK reports a typed number as soon as it parses inside min and max, and one past either end when the field loses focus or you press Enter, clamped and rounded to the step.
The Controlled demo holds the value this way and compares it with 3.
How do I set a minimum, maximum and step on a Number Input?
Set min, max and step on NumberInput. The buttons move by step and stop at either end, as the Range and Step demos show.
A typed number past either end is clamped back in when the field loses focus or you press Enter, and the text is corrected with it. That's also when a typed number is rounded to the step, counted from min or from 0, so 7 becomes 5 with a step of 5.
How do I format a Number Input as currency or a percentage?
Pass formatOptions, which takes the options of Intl.NumberFormat. { style: "currency", currency: "USD" } shows 19 as $19.00, as in the Formatted demo. With { style: "percent" } the value is a fraction, so 0.25 shows as 25%.
With the percent style, UI SDK steps by 0.01, one percent, unless you set step yourself.
With either style, the value you get back is the number itself, such as 19, never the text the field shows.
How do I submit a Number Input value with a form?
Give NumberInput a name. UI SDK renders a hidden input under that name holding the bare number, even when the field shows currency, and an empty field sends an empty string. A disabled field sends nothing, so use readOnly for a value that must be submitted but not changed.
The Form demo sends a quantity and shows what the form received.
Can a Number Input change value with the mouse wheel?
Yes, once you set allowMouseWheel on NumberInput: the wheel then steps the value while the input has focus and the pointer is over it, and the page doesn't scroll underneath. It's off by default, so someone scrolling a long form past the field doesn't change a quantity by accident. The Mouse wheel demo has it on.
How do I let people drag to change a Number Input value?
Put a NumberInputScrubber above the group with the label text inside it, as the Scrub demo does with Quantity. It renders the label and makes it a handle: press on it and drag sideways to move the value.
While you drag, UI SDK locks the pointer where the browser allows it and the scrubber draws its own cursor, so the drag can run past the edge of the screen. The value moves with the distance, not the speed, and holding Shift moves it ten steps at a time.
Is the UI SDK Number Input accessible?
Yes, once it has a name: put it in a Field with a FieldLabel, which UI SDK ties to the input, or give NumberInputInput an aria-label. The arrow keys step the value, Shift or Page Up and Page Down make it ten steps, Alt a tenth of one, and Home and End jump to min and max when they're set. The input is a plain text field rather than a spin button, and invalid reaches it as aria-invalid.
When should I use a Number Input instead of an input with type number?
Use the Number Input when the number needs a currency or percent format, step buttons that look the same in every browser, or a min and max it is pulled back inside. A native number input draws the browser's own spinners, can't format the value, accepts the letter e for exponents, and only reports an out-of-range number through form validation.
For digits that aren't a quantity, such as a postal code or a one-time code, use an Input with inputMode set to numeric, or an Input OTP. Step buttons mean nothing on those.
API Reference
NumberInput
<div>data-slot="number-input"
PropType
value?number | string
The value. Pair with onValueChange to control it. Two-way with bind:value.
Type
number | string
defaultValue?number | string
The value it starts with, when you don't control value.
Type
number | string
onValueChange?(details: { value: string }) → void
Called when the value changes.
Type
(details: { value: string }) → void
Parameters
details – { value: string }
Returns
void
allowMouseWheel?boolean
Lets the mouse wheel step the value while the input has focus.
controller?NumberInputController
Steps it from anywhere. Create one with createNumberInputController().
debug?boolean
Logs every state change with its reason to the console, in development builds.
disabled?boolean
Disables it like a native control: it can't be focused or changed, and isn't submitted.
form?string
The id of a form to belong to, when it isn't inside that form.
formatOptions?Intl.NumberFormatOptions
How the value shows: Intl.NumberFormat options.
invalid?boolean
Whether it's invalid.
largeStep?number
How far Page Up, Page Down, and Shift with an arrow key, a button, the wheel or a scrub move.
Default
10 steps
locale?string
The locale for formatting and reading typed numbers. Defaults to the page's lang, or the browser's language without one, once the page is interactive. Server markup uses English until then, so it never disagrees with the client.
Default
"en-US"
max?number
The highest value. Typed values above it are lowered to it when committed.
Default
Number.MAX_SAFE_INTEGER
min?number
The lowest value. Typed values below it are raised to it when committed.
Default
Number.MIN_SAFE_INTEGER
name?string
The name it submits under. The number is submitted as is, like 1234.5, never formatted.
onValueCommit?(value: number | null, details: CommitDetails<NumberInputValueChangeReason>) → void
Runs once an interaction that changed the value ends: typing is committed, a key or a button is released, or a scrub ends. For saving.
Type
(value: number | null, details: CommitDetails<NumberInputValueChangeReason>) → void
Keeps it focusable and submitted, but stops changes.
required?boolean
Blocks form submission while it's empty, with the browser's own message.
size?"sm" | "md" | "lg"
The size.
Type
"sm" | "md" | "lg"
Default
"md"
smallStep?number
How far Alt (Option on a Mac) with an arrow key, the wheel or a scrub moves, for fine changes. Values it reaches snap to it rather than to step.
Default
a tenth of a step
step?number
How far one step moves. Committed values snap to steps from min, or from 0 without one.
Default
1, or the smallest percent a percent format shows, like 0.01
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.
NumberInputGroup
<div>data-slot="number-input-group"
PropType
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.
NumberInputDecrement
<button>data-slot="number-input-decrement"
PropType
disabled?boolean
Turns off this button alone. Pressing, clicking or holding it doesn't step.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-at-bound?string
data-disabled?string
Present when the button can't step: it or the field is disabled, the field is read-only, or the value is at min.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.
NumberInputIncrement
<button>data-slot="number-input-increment"
PropType
disabled?boolean
Turns off this button alone. Pressing, clicking or holding it doesn't step.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when the button can't step: it or the field is disabled, the field is read-only, or the value is at max.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.
NumberInputInput
<input>data-slot="number-input-control"
PropType
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.
NumberInputScrubber
<span>data-slot="number-input-scrub-area"
PropType
controller?NumberInputController
Connects a scrub area outside the number input, like an icon in a toolbar, to the number input with this controller. Inside it, it connects on its own. Web components use for="<number input id>" instead.
orientation?"horizontal" | "vertical"
Which way to drag: right and up increase.
Type
"horizontal" | "vertical"
Default
"horizontal"
pixelsPerStep?number
How far the pointer moves for one step, in CSS pixels.
Default
2
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
AttributeType
data-disabled?string
Present when disabled, by its own prop or a <fieldset>.
data-orientation"horizontal" | "vertical"
Which way to drag.
Type
"horizontal" | "vertical"
data-readonly?string
Present when read-only.
data-required?string
Present when required.
data-scrubbing?string
Present while a drag on the scrub area changes the value.