Input
A text input with three sizes, invalid and read-only states, native validation styling and a 16px touch font so phones never zoom in.
- Form components
- 2 parts
- 14 examples
- vs shadcn/ui
@base-ui/reactclass-variance-authority
See it in a real product: Image Studio, Integrations, Memory, and 23 more
View blockpnpm dlx shadcn@latest add https://hextaui.com/r/input.jsonAdds the component, the HextaUI theme tokens and any HextaUI components it depends on.
Add the theme tokens to your global CSS, if you haven’t yet.
Install the dependencies.
pnpm add @base-ui/react class-variance-authority cnCopy and paste the following code into your project.
components/ui/input.tsx components/ui/number-flow.tsx lib/motion.ts Update the import paths to match your project setup.
sm, default and lg match the button heights, so an input and a button of the same size line up in a row.
Point aria-describedby at the helper text so screen readers read it after the label.
aria-invalid turns the edge and the focus ring red. Link the message with aria-describedby so it is announced, not just colored.
Fields with required, type="email" or pattern only turn red after someone has typed in them or tried to submit, never on first render. A submit that finds a field invalid shakes it once, so the eye lands on what needs fixing. It never shakes while you type or tab around. Submit the empty form to see it.
A disabled input can’t be focused, edited or submitted with the form.
readOnly keeps the value focusable, selectable and submitted, with a muted surface so it doesn’t look editable. Prefer it over disabled for values people need to copy.
type="file" gets the same frame, with the browser’s button restyled as plain text.
Password, number, search, date and time share one height and frame. In dark mode, the browser’s pickers and spinners switch to dark too.
onValueChange hands you the string directly, so there’s no event.target.value to unwrap. onChange still works too.
Side by side with a gap, or joined into one control inside a <ButtonGroup />, where the input takes the remaining width.
Inputs fill their container, so place them in a grid. Give grid cells min-w-0 so long values can’t stretch a column.
Long values scroll inside the field and long placeholders are cut off, without widening the layout.
Text, caret and padding follow the direction. Use dir="auto" on fields that hold left-to-right values, like an email address in an Arabic form.
- Every input needs a name. Use a
<label>withhtmlFor, oraria-labelwhen there’s no visible label. A placeholder is not a label. - Connect helper and error text with
aria-describedby, and setaria-invalidonly once there is an error to show. - On touch screens the text is at least 16px, so iOS Safari doesn’t zoom the page when the input is focused.
- Inside a Base UI
Field, the label, description, error and validity are wired up for you.
Built on the Base UI input. It accepts every native input attribute.
| Prop | Type | Default |
|---|---|---|
sizeHeight and padding, matched to the buttons. | "sm" | "default" | "lg" | "default" |
htmlSizeThe native size attribute, renamed because size is the variant. | number | – |
value | string | number | string[] | – |
defaultValue | string | number | string[] | – |
onValueChangeCalled with the new value on every change. | (value: string, details) => void | – |
type | string | "text" |
disabled | boolean | false |
readOnly | boolean | false |
aria-invalidShows the invalid edge and focus ring. | boolean | – |
className | string | (state) => string | – |
shakeShake once when a form submit finds this input invalid. Works with native validation, Base UI Field and libraries that set aria-invalid. Skipped under reduced motion. | boolean | true |
render | ReactElement | (props, state) => ReactElement | <input> |
| Attribute | Description |
|---|---|
data-slot="input" | Target the input in CSS. |
data-size | The current size. |
data-shake | Present while the input shakes after a failed submit. |
data-disabled | Present when the input is disabled. |
data-invalid | Present when the surrounding Field is invalid. Styled like aria-invalid. |
data-valid | Present when the surrounding Field is valid. |
data-touched | Present after the input lost focus once, inside a Field. |
data-dirty | Present once the value changed, inside a Field. |
data-filled | Present when the input has a value, inside a Field. |
data-focused | Present while focused, inside a Field. |
The class names behind the input, for styling another element to match, such as a native <select> or <textarea>. Call it with { size }.
The character count behind <InputGroupCount /> and <FieldCounter />. Use those parts, which read the field for you. Reach for this one only when you track the length yourself.
| Prop | Type | Default |
|---|---|---|
lengthRequired. | number | – |
maxLength | number | null | – |
threshold | number | 10% of maxLength, at most 20 |
announcement | (remaining: number) => string | – |
| Attribute | Description |
|---|---|
data-slot="input-count" | Target the count in CSS. |
data-state="near" | "limit" | Present within the threshold, and at the limit. |
- MotionThe easing curves, durations and reduced-motion check every component animates with, plus hooks for size morphs and sliding highlights.
- Number flowAnimated numbers where only the changed digits spin, with any Intl format and locale.
- useAutosizeGrows a textarea with what you write between its minimum and maximum height, animating each change without ever touching the text.
- useComposedRefKeeps a ref to your own element while still forwarding it to whatever ref the parent passed in.
- useInvalidShakeShakes a form control when a submit attempt finds it invalid, and never while someone is still typing.
- useMergedRefCombines any number of callback and object refs into one, with React 19 ref cleanup for each of them.
Used in Pro blocks
Blocks that build on Input.
Every block comes with HextaUI Pro, and Prompt Input is free.