Components · Web Components
Number Input Element
`<nx-number-input>`: the NumberInput rules as a form-associated web component, for server-rendered pages without React. A plain `<form>` posts its value.
<script type="module" src="https://cdn.jsdelivr.net/npm/@nexub/elements/dist/define.js"></script>Install: npm install @nexub/elements. No build step: the script tag above registers <nx-number-input>. With a bundler, import '@nexub/elements/define'; once does the same.
Usage
A number on a page with no React: server-rendered HTML, a plain `<form method="post">`, a CMS template.
Where `<input type="number">` would do, but it must look like the rest of Nexub, be empty without being 0, and clamp on blur as the React field does.
In a React app: use NumberInput from `@nexub/react` (steppers, `nullable`, NexubProvider strings).
Phone numbers, codes and ids: use a text input. They are not quantities.
Money: use MoneyInput.
Playground
Props
Committed value: 4
<nx-number-input ref={ref} id="seats" max="50" min="1" name="seats" placeholder="1–50" step="1" value="4" />Examples
Default
Labelled with `<label for>`, limits applied on blur. Rendered here in React 19, which passes the attributes and listens to the element’s events.
In a form
Form-associated: `FormData` has the value, `required` and `min` block the submit, and Reset goes back to the `value` attribute.
States
Committed value: 4
defaultCommitted value: 4
disabledCommitted value: 4
invalidCommitted value: 4
size="sm"Committed value: 4
size="md"Committed value: 4
size="lg"
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | — | The form field name: a <form> posts the value under it. |
| value | string | — | The starting value, and what form.reset() goes back to. Empty for none. The value property is the current value. In React, server rendering writes value as this attribute; in a client-only render pass defaultValue, which sets it. |
| min | string | — | Smallest value, applied on blur, Enter and steps (never while typing, so 30 can be typed with a minimum of 15). Below it, the field is invalid (rangeUnderflow). |
| max | string | — | Largest value, applied the same way (rangeOverflow). |
| step | string | — | Arrow-key increment: a positive number (default 1), or any for any decimal (keys then move by 1). |
| required | boolean | — | Empty is invalid (valueMissing); the form won’t submit and shows the browser’s message. |
| disabled | boolean | — | Not editable, not posted. A disabled <fieldset> around it does the same. |
| readonly | boolean | — | Posted but not editable; arrow keys do nothing. |
| placeholder | string | — | Hint shown while empty. |
| size | 'sm' | 'md' | 'lg' | — | sm, md (default) or lg: the control heights of @nexub/react. |
| aria-label | string | — | Names the field when it has no <label for>. |
| input event | Event | — | On every edit and step that changes the value. Read event.target.value (a string, '' when cleared). |
| change event | Event | — | When the value is committed: blur, Enter or a step. |
| invalid | boolean | — | Shows the field as invalid (aria-invalid, the error border) whatever its validity, as @nexub/react's invalid. |
Accessibility
ARIA roles
Keyboard
- ArrowUp / ArrowDown
One step up or down, inside `min` and `max`. Fires `input` and `change`.
- PageUp / PageDown
Ten steps.
- Home / End
To `min` or `max`, when set.
- Enter
Applies the limits and submits the form, as a native input does.
Focus
One tab stop: focus is delegated to the inner input, so `<label for>` clicks and `element.focus()` land in the field.
Focus ring from the tokens; forced colours draw it in Highlight.
Screen reader
The inner input is named from the host’s `<label for>` (or a wrapping `<label>`, or `aria-label`): a label can’t reach into a shadow root, so the element copies its text.
`aria-required` and `aria-invalid` follow the field; the invalid state shows only after an edit or a failed submit.
Validation messages come from a native input where the browser has one, so they are in the browser’s language.
Notes
Shadow DOM: page CSS can’t break the inside. Style it with the theme variables, or `::part(field)` and `::part(input)`.
Themed by the same CSS custom properties as `@nexub/react`: load `@nexub/themes` and `data-theme` / `data-product` apply.
On iOS & Android
@nexub/react-native, Stepper.