Skip to Content
CodeUI

NumberField

@codeui/solid v0.3.0

A numeric input with step buttons, bounds, and a fixed precision.

When to use

Use NumberField when the user types an exact number, such as a font size, a padding, or an export width. It masks the input to digits, keeps the value inside min and max, and steps it with the arrow keys or the buttons.

When to choose another component

  • Use Slider when the position in a range matters more than the exact number. Inkstone pairs a slider with a number field for a number in a useful range.
  • Use SegmentedControl for two to five fixed values, such as an export scale of 1×, 2×, or 4×.
  • Use TextField for digits that are not a quantity, such as a version or a postal code.

Live demo

Between 8 and 32 pixels.
number-field/basic.tsx
import { NumberField } from "@codeui/solid";
import { createSignal } from "solid-js";

export default function NumberFieldBasic() {
	const [fontSize, setFontSize] = createSignal<number | null | undefined>(14);
	return (
		<div style={{ width: "240px" }}>
			<NumberField
				label="Font size"
				description="Between 8 and 32 pixels."
				min={8}
				max={32}
				value={fontSize()}
				onChange={setFontSize}
			/>
		</div>
	);
}

Anatomy

Between 320 and 4096 pixels.

Tap a part to outline it in the demo.

  1. 01NumberFieldnumberfieldclasses.root
  2. 02Labeltextfield-labelclasses.label
  3. 03Inputtextfield-inputclasses.input
  4. 04Buttonbutton
  5. 05Descriptiontextfield-descriptionclasses.description
  6. 06Error messagetextfield-error-messageclasses.errorMessage
ExportRendersclasses keydata-cuiRole
NumberFieldRoot divrootnumberfieldgroup
label, when label is setlabeltextfield-labelnone
inputinputtextfield-inputspinbutton
Increment and decrement buttonsnonebuttonbutton
Hint div, when description is setdescriptiontextfield-descriptionnone
Error div, when invalid and errorMessage is seterrorMessagetextfield-error-messagenone

The input and the step buttons sit in an unnamed wrapper div. The buttons are kit Buttons with variant="secondary" and ghost. They show while the pointer is over the field or focus is inside it. They are not tab stops. The root always carries data-field-size, sm by default.

Variants

Bounds and step

From 0 to 128, in steps of 8.
number-field/bounds.tsx
import { NumberField } from "@codeui/solid";

export default function NumberFieldBounds() {
	return (
		<div style={{ width: "240px" }}>
			<NumberField
				label="Padding"
				description="From 0 to 128, in steps of 8."
				min={0}
				max={128}
				step={8}
				defaultValue={32}
			/>
		</div>
	);
}

min and max bound the value. The mask stops you typing past max. A value under min is allowed while you type and is clamped when the input loses focus. step is the amount the buttons and the arrow keys add or remove. Stepping clamps to the bounds.

Precision and formatting

Two decimal digits. The comma is the decimal separator.
number-field/precision.tsx
import { NumberField } from "@codeui/solid";

export default function NumberFieldPrecision() {
	return (
		<div style={{ width: "240px" }}>
			<NumberField
				label="Line height"
				description="Two decimal digits. The comma is the decimal separator."
				min={1}
				max={3}
				step={0.25}
				precision={2}
				defaultValue={1.5}
			/>
		</div>
	);
}

precision is the number of decimal digits the mask accepts. The default, 0, accepts whole numbers only. The format is fixed and does not follow the locale:

  • A comma separates the decimals, so one and a half is 1,5.
  • A non-breaking space separates thousands, so 1280 is 1 280.
  • A minus sign is accepted only when min is below 0.

Prefix and postfix

number-field/prefix-postfix.tsx
import { NumberField, Stack } from "@codeui/solid";

export default function NumberFieldPrefixPostfix() {
	return (
		<Stack gap="4" style={{ width: "240px" }}>
			<NumberField label="Export width" postfix=" px" min={320} max={4096} defaultValue={1280} />
			<NumberField label="Opacity" postfix=" %" min={0} max={100} defaultValue={80} />
			<NumberField label="Shadow offset" prefix="× " min={0} max={4} precision={1} defaultValue={1.5} />
		</Stack>
	);
}

prefix and postfix are text that the mask keeps around the number, such as a unit. The caret stays on the number. Include the space you want, as in postfix=" px". The value stays a plain number.

Size

number-field/sizes.tsx
import { NumberField, Stack } from "@codeui/solid";
import { For } from "solid-js";

const sizes = ["xs", "sm", "md", "lg"] as const;

export default function NumberFieldSizes() {
	return (
		<Stack gap="4" style={{ width: "240px" }}>
			<For each={sizes}>{size => <NumberField size={size} label={`Size ${size}`} defaultValue={14} />}</For>
		</Stack>
	);
}

size uses the same scale as TextField, with a 36px sm default at comfortable density. The step buttons range from 12px to 18px.

Filled and inline

number-field/themes.tsx
import { NumberField, Stack } from "@codeui/solid";

export default function NumberFieldThemes() {
	return (
		<Stack gap="4" style={{ width: "240px" }}>
			<NumberField variant="filled" label="Filled" defaultValue={16} />
			<NumberField variant="inline" label="Inline" defaultValue={16} />
		</Stack>
	);
}

filled draws the field fill. inline keeps a 2px bottom border only.

States

Required, read-only, and disabled

number-field/states.tsx
import { NumberField, Stack } from "@codeui/solid";

export default function NumberFieldStates() {
	return (
		<Stack gap="4" style={{ width: "240px" }}>
			<NumberField label="Tab size" required min={1} max={8} placeholder="Required" />
			<NumberField label="Export scale" readOnly defaultValue={2} />
			<NumberField label="Border radius" disabled defaultValue={12} />
		</Stack>
	);
}

required sets required and aria-required on the input. readOnly blocks typing, disables the step buttons, and makes the arrow keys do nothing. disabled removes the input from the tab order, dims it, and disables the step buttons.

Invalid

number-field/invalid.tsx
import { NumberField } from "@codeui/solid";
import { createSignal } from "solid-js";

export default function NumberFieldInvalid() {
	const [width, setWidth] = createSignal<number | null | undefined>(null);
	return (
		<div style={{ width: "240px" }}>
			<NumberField
				label="Export width"
				postfix=" px"
				min={320}
				max={4096}
				value={width()}
				onChange={setWidth}
				validationState={width() == null ? "invalid" : "valid"}
				errorMessage="Enter a width between 320 and 4096 pixels."
			/>
		</div>
	);
}

Set validationState="invalid" to show errorMessage and set aria-invalid. The demo is invalid while the input is empty.

Controlled and empty

value and onChange work as in the live demo. onChange receives a number, or null when you clear the input. Type your signal as number | null | undefined. Pass defaultValue to leave the value to the field.

Ready API

NumberField

Renders the root, the label, the masked input, the step buttons, the hint, and the error message.

Prop

Type

Inherited from Kobalte's text field root:

Prop

Type

NumberField sends the same native attributes as TextField to the input, except type, which is always text. Pass inputmode="decimal" or inputmode="numeric" to get a number keypad on phones. Use classes to style one part. The root accepts no class prop.

In a native form, the input submits its formatted text, such as 1 280 px. Read the number from onChange instead of FormData.

NumberFieldLabel

Renders Kobalte's TextField.Label with the kit label style. Use it only inside a Kobalte TextField root that you compose yourself.

Prop

Type

Styled parts API

Use @codeui/solid/number-field/parts to customize the layout around a formatted numeric input. Set the value, limits, step, and formatting on Root. Put Input and optional Controls inside Field to keep the input and stepper buttons aligned. Keep Label, Description, and ErrorMessage under the same root.

Accessibility

Keyboard

KeyAction
TabMove focus to the input.
Arrow UpAdd step to the value, up to max. Does nothing when read-only.
Arrow DownRemove step from the value, down to min. Does nothing when read-only.

ARIA

  • The root is a div with role="group". The input has role="spinbutton". It sets aria-valuenow to the value and aria-valuetext to the formatted value, with the prefix and the postfix. Both are absent while the field is empty. aria-valuemin and aria-valuemax are set when you pass min and max.
  • The label for points at the input id.
  • The step buttons are named "Increment by" and "Decrement by", followed by the step, such as "Increment by 8". A step button is disabled when the field is disabled or read-only, or when the value is already at max or min. A value one step from a bound stays enabled and steps to the bound.
  • The input gets aria-describedby, aria-invalid, aria-required, aria-disabled, and aria-readonly as TextField does.

Focus

The arrow keys keep focus in the input. The step buttons have tabindex="-1", as in the ARIA spinbutton pattern, so Tab moves from the input to the next control on the page. They are pointer targets, and the arrow keys are the keyboard path.

SSR notes

The root, the label, the input, and the hint render on the server. The label for and the input id are set during render, as in TextField. The mask attaches in onMount, so it runs only in the browser. Until hydration the input accepts any text.

Theming hooks

NumberField reads the same field variables as TextField. The step buttons add --cui-surface-input-accent for their background, --cui-text-input, --cui-radius-md, and --cui-space-2, plus the ghost secondary Button variables.

Composition examples

  • Build forms reads an export width from NumberField and validates it before submission.

On this page