Text Field
Accessible React text fields built with the InputField and Input components, with labels, hints, error messages, password toggles, icon slots, and horizontal layouts.
Examples
Can you send the launch notes before noon?
Sure! I'm adding the last screenshots now.
Great, thanks. Ping me when they're ready.
Overview
These examples build text fields with the Ariakit UI Input components. A text field has a visible label, the field itself, and often a hint or an error message. Some fields also have content beside the text, such as an icon, a unit, or a button.
The gallery starts with a field with a hint and a field with an error. Then it shows fields in each state, a password field with a show button, fields on colored backgrounds, fields around other controls, shapes and sizes, and horizontal fields. It also includes fields with colored edges and fills, fields with slots, and a message composer. The composer has no edge around its text. The Accessibility section tells why it still meets the contrast requirement.
Components
InputField lays out a field: an InputLabel, the control, an InputError, and one or more InputDescription parts. These parts only add styles, so connect them yourself. Point the label’s htmlFor at the control’s id. List the error and hint ids in the control’s aria-describedby, with the error first and only while it shows. Set aria-invalid on the control while the error shows, and its edge changes to the danger color.
InputError puts an alert icon before the error text, so the error has a sign besides its color. The icon sits on the first line, and wrapped lines start under the text, not under the icon. Set icon to show another icon, or set icon={false} to remove it, for example when the app draws its own icon. An error without content, such as an empty string or null, shows no icon. A component child always counts as content, even one that renders nothing, so render it only while there is an error. ComboboxError does the same. If you use the style recipes without the components, put the error text in an inputErrorText part, which takes the danger color. The error has no icon until you add an inputErrorIcon part before the text, with aria-hidden="true".
Frame variants, such as $layer, $rounded, and $p, show the error on a panel, for example <InputError $layer="danger" $mix={15}>. A panel gets padding and rounded corners by default, and the text keeps the danger color with enough contrast on it. While the error has no child nodes, such as with null or an empty string, the panel is visually hidden but stays in the accessibility tree. So an error with role="alert" keeps its live region while it waits for its text, and no empty box shows. The Colors example shows an error on a panel.
With $orientation="horizontal", the label takes a start column, and InputFieldContent holds the control and its texts. Put horizontal fields in an InputFieldGrid. Their labels share one column that fits the longest label and align to its end, so each label stays close to its own field, and the space between rows is larger than the space between a label and its field. Set $labelAlign="start" on a field to align its label to the start instead. The fields stack when the grid is narrower than 20rem. The grid fills the width of its container, so in a popover or another container that fits its content, give the grid a width. Other children of the grid, such as a heading, take a full row. A click anywhere on a label, or in the space between the label and the field, focuses the field. ComboboxField and its parts do the same for a combobox or a select. A select with aria-invalid shows the danger edge too, and a disabled select dims the label and the hints of its field. Their labels, ComboboxLabel and ComboboxSelectLabel, are connected by the combobox store.
Input renders the control. A color that you give to $edge shows at full strength, like the danger edge, and its lightness moves only when the edge needs more contrast. $layer gives the field a colored fill. The Colors example uses these props to show a status, a warning, and a value that the app filled in.
InputGroup puts several parts on one field surface: an Input or a textarea, and InputSlot parts for icons, units, avatars, buttons, and selects. A click on the surface focuses the field, and a button or a select in a slot keeps its own focus. In the Slots example, a ComboboxSelect in a slot picks the currency, and the symbol before the amount follows it. The amount group also has $fitContent, so it fits the value as you type, up to the width of its column. Browsers without CSS field-sizing keep the native width of the input. An Input in a group has no edge or padding of its own, because the group draws them.
The Slots example also has a right-to-left version in Arabic. The styles use logical properties, so the slots and the select move to the other side. It sets lang, so screen readers read it in the right language.
Accessibility
WCAG requires a contrast of 3:1 between the boundary that identifies a field and the surface around it (Non-text Contrast). The edge of an enabled field keeps that contrast on light, dark, and colored surfaces, as in the Backgrounds example, and 4.5:1 when the user asks for more contrast. On a few colored surfaces, Firefox can land slightly under these ratios. A disabled field, which WCAG exempts, gets a lower floor. A neutral edge raises its opacity to reach the floor. On dark layers, the raised fill is lighter than the surface, so the edge can stay under 3:1 with the fill, which WCAG allows. $edgeContrast turns this off for a field that something else identifies.
A colored edge gets the same floor. A light color, such as the yellow warning edge in the Colors example, gets darker on light layers until it reaches 3:1, and it stays close to its hue. A field with a colored fill keeps the neutral edge, which follows the fill. Color alone does not tell the status (Use of Color), so each colored field also has a hint that tells it in words.
A field can have no edge when something else identifies it. In the Auto-Growing Textarea example, the textarea has no edge, but the group around it has one. The group is the area where you type.
In the Slots example, the icons, the currency symbol, and the avatar are hidden from screen readers, because the label, the hint, or the value already tells the same thing. The green icon of the Domain field has the hint “Verified.” The currency select is a separate control with its own name.
Placeholder text does not identify a field, because it goes away when you type. Experts do not agree that an icon or a keyboard shortcut in a field is sufficient, so these examples do not use only those.
The Error example lists the error before the hint in aria-describedby, so screen readers read it first. The error has no live region, because it does not appear while you work in the field. When an error appears without a focus change, such as when focus leaves the field, give it role="alert".
The error icon is a sign of the error besides the danger color (Use of Color). Screen readers do not read it, because the text tells the same thing. This is also true for an icon with a label, so the field reads only the error text. In forced colors mode, such as a Windows contrast theme, the icon uses the system text color, like the text. The icon also shows when you print the page.
The show button of the Password Visibility Toggle example keeps the same name and tells its state with aria-pressed.