How it works
The fill, hairline and states are drawn on the outer element, around a borderless native input, so ref, name and autoComplete land on the input and work with form libraries. The clear button keeps its place and only fades, so text never shifts; the count reserves width for the limit's digits. Clearing uses the native setter and an input event, so controlled and uncontrolled share one onChange. Toggling a password restores the caret.
Use it for
- Single-line text in forms: names, titles, usernames, phone numbers, search terms.
- Inputs that need clearing, a password toggle, a length limit, or a unit or area-code prefix or suffix.
- Mobile forms: use size="lg" (52px high; 16px text does not zoom the page on iOS).
- Use inputMode="numeric" for phone numbers and codes and "decimal" for amounts to get the right keypad.
Look elsewhere when
- Multi-line content such as notes or messages: use a textarea.
- Numbers that need plus and minus buttons: use a number input.
- One-time codes: use a code input.
- Choosing from fixed options: use a select instead of making people type.
Usage
import { useState } from 'react'
import { TextInput } from './TextInput.jsx'
export function ProjectTitleField() {
const [title, setTitle] = useState('')
const tooShort = title !== '' && title.length < 4
return (
<>
<label htmlFor="project-title">Project title</label>
<TextInput
id="project-title"
value={title}
onChange={setTitle}
clearable
showCount
maxLength={20}
invalid={tooShort}
aria-describedby="project-title-msg"
placeholder="e.g. Spring launch"
/>
<p id="project-title-msg">{tooShort ? 'At least 4 characters' : 'Shown in the project list'}</p>
</>
)
}
// Password field with a built-in show / hide toggle
// <TextInput type="password" autoComplete="current-password" />
// Prefix and suffix: <TextInput prefix="+1" inputMode="numeric" /> <TextInput suffix="@example.com" />Props
| Prop | Type | Default | What it does |
|---|---|---|---|
value | string | — | Controlled value. Omit for uncontrolled. |
defaultValue | string | — | Initial value when uncontrolled. |
onChange | (value: string, event: ChangeEvent) => void | — | Receives the string value and the native event. The clear button also triggers it, with an empty string. |
size | 'sm' | 'md' | 'lg' | 'md' | Height 36 / 44 / 52px, radius 10 / 12 / 14px, text 14 / 15 / 16px. |
prefix | ReactNode | — | Icon or text inside the field on the left, such as a search icon or “+1”. |
suffix | ReactNode | — | Icon or text inside the field on the right, such as “kg” or “@example.com”. |
clearable | boolean | false | Shows a clear button when there is content and the field is focused or hovered. Its place is always reserved; hidden when read-only or disabled. |
onClear | () => void | — | Called after the clear button is pressed; onChange still receives the empty string. |
type | string | 'text' | Native input type. With password, a show / hide toggle is added. |
revealable | boolean | true | Whether a password field has the show toggle. Pass false to never reveal the text. |
maxLength | number | — | Maximum number of characters (native maxLength; extra input is rejected). |
showCount | boolean | false | Shows the count: “typed / limit” with maxLength, otherwise just the typed count. |
invalid | boolean | false | Error state: danger hairline and a faint danger tint, plus aria-invalid. Put the message below the field and link it with aria-describedby. |
disabled | boolean | false | Disabled: the fill stays, the hairline goes, text is muted and it cannot be focused. |
labels | object | — | Replaces built-in text (clear, showPassword, count, countMax). Shallow-merged over the English defaults. |
ref | Ref<HTMLInputElement> | — | Forwarded to the native <input>; works with react-hook-form's register. |
...rest | InputHTMLAttributes | — | className and style go on the outer element; other native attributes (id, name, placeholder, readOnly, autoComplete, inputMode, onBlur) go to the <input>. |
Accessibility
- It is a native <input>: link a <label htmlFor>; a prefix or suffix does not replace the label.
- invalid sets aria-invalid; link the message with aria-describedby so it is read on focus.
- The count is linked through aria-describedby and read as “12 of 20 characters entered”, not character by character.
- The show-password button is tabbable, named “Show password” and uses aria-pressed; the clear button is outside the Tab order (select all and delete works) and named “Clear”.
- Clicking buttons in the frame keeps focus in the input, so there is no blur validation and the mobile keyboard stays open.
Motion
On focus the fill turns white in 160ms and the accent line and 4px halo grow in over 220ms (ease-out). The clear button fades in over 160ms and out over 100ms. The slash on the eye draws and retracts in 240ms; the eye presses to 0.9.
Add it to your project
Copy the prompt into Codex or Claude Code. The agent reads the published source, shows you what it will write and adds the component next to your own.
TextInput.jsxtext-input.csstokens.cssREADME.md
Both files install together. The stylesheet reads the design tokens in tokens.css and carries fallbacks, so it also works alone.





