Skip to content
nexubDesign System
v11.9.0

Components · Web Components

Number Input Element

AlphaAdded v11.6.0

`<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.

View as Markdown

Usage

Use it for
  • 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.

Use something else for
  • 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

TSX
<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.

Committed value: 4

In a form

Form-associated: `FormData` has the value, `required` and `min` block the submit, and Reset goes back to the `value` attribute.

Posted: nothing yet

States

  • Committed value: 4

    default
  • Committed value: 4

    disabled
  • Committed value: 4

    invalid
  • Committed value: 4

    size="sm"
  • Committed value: 4

    size="md"
  • Committed value: 4

    size="lg"

Props

Props
PropTypeDefaultDescription
namestring—The form field name: a <form> posts the value under it.
valuestring—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.
minstring—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).
maxstring—Largest value, applied the same way (rangeOverflow).
stepstring—Arrow-key increment: a positive number (default 1), or any for any decimal (keys then move by 1).
requiredboolean—Empty is invalid (valueMissing); the form won’t submit and shows the browser’s message.
disabledboolean—Not editable, not posted. A disabled <fieldset> around it does the same.
readonlyboolean—Posted but not editable; arrow keys do nothing.
placeholderstring—Hint shown while empty.
size'sm' | 'md' | 'lg'—sm, md (default) or lg: the control heights of @nexub/react.
aria-labelstring—Names the field when it has no <label for>.
input eventEvent—On every edit and step that changes the value. Read event.target.value (a string, '' when cleared).
change eventEvent—When the value is committed: blur, Enter or a step.
invalidboolean—Shows the field as invalid (aria-invalid, the error border) whatever its validity, as @nexub/react's invalid.

Accessibility

ARIA roles

spinbutton (the inner `<input>`), with `aria-valuenow`, `aria-valuemin` and `aria-valuemax`

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