Text field
Filled or outlined text input with a floating label, icons, affixes, counter, error icon and a text area.
import { TextField } from '@windmorph/react';Props
TextField 66 props
The
<input>'s (or<textarea>'s) id, for a<label for>or an error summary's link.The container style: a filled surface or an outline.
Web density,
0to-3: each step takes 4px off the container (56px at 0, 44px at -3). Left out, the field takes the density of the nearestThemeProviderorThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.The text, when controlled.
The initial text, when uncontrolled.
Helper text below the field. Replaced visually by the error message when invalid, and shown in the error colour while the field is invalid.
Shows the supporting text only while the field has focus (M3 allows it "persistently visible or visible only on focus"). Its space is kept, and screen readers always get it.
Marks the field invalid (in addition to
validateand native constraints).A value is required: adds the
*to the label and nativerequiredvalidation.Disables the field: it can't be focused or edited.
The value can be read and copied but not changed. M3 styles it as a regular field and asks for it to be "clearly labeled as read-only", e.g. in the supporting text. The root gets
data-readonly.Decorative icon at the start, in a 48px box.
Icon or icon button at the end (e.g. clear, visibility toggle), in a 48px box. It takes the place of the error icon.
The icon shown at the end while the field is invalid and has no
trailingIcon(M3 strongly recommends one). Defaults to Material Symbolserror, named bylabels.error;falseturns it off.Text before the input, e.g. "$". Shown once the label floats.
What screen readers hear for the prefix, e.g. "US dollars" for "$". The prefix is in the input's description either way; without this, its visible text is read.
Text after the input, e.g. "kg". Shown once the label floats.
What screen readers hear for the suffix, e.g. "kilograms" for "kg".
Renders a
<textarea>. By default it's an M3 text area: a fixed height ofrowslines that scrolls vertically, which M3 recommends over growing fields on the web.With
multiline: grow with the content fromrowsup tomaxRows(M3's multi-line field) instead of keeping a fixed height.Visible lines of a multiline field.
With
autoGrow: lines after which the field scrolls instead of growing.Shows
count/maxLengthbelow the field. Defaults totruewhenmaxLengthis set.The root element. Use
inputReffor the input itself.The
<input>(or<textarea>when multiline), e.g. for form libraries.The visible label, which floats above the input once it has focus or a value.
One of
label,aria-labeloraria-labelledbyis required.
Text
Words the field announces, for other languages. A
LabelsProvidersets them for the whole app.Defines a string value that labels the current element.
One of
label,aria-labeloraria-labelledbyis required.Identifies the element (or elements) that labels the current element.
One of
label,aria-labeloraria-labelledbyis required.
Classes for the root element, merged with
classNames.root; consumer classes win.Classes for each part, by slot.
The root element, merged with
className.The filled or outlined container around the label, input and icons.
The floating label.
The
<input>or<textarea>.The leading icon's 48px box.
The trailing icon's 48px box.
The prefix text before the input.
The suffix text after the input.
The helper text below the field.
The error message below the field.
The character counter below the field.
The built-in error icon.
The outlined field's border (a
<fieldset>, notched for the floated label).The filled field's active indicator, the line along the container's bottom.
Inline styles for the root element.
An enumerated attribute that defines what action label or icon to preset for the enter key on virtual keyboards. See MDN.
Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
A function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if
validationBehavior="native". For realtime validation, use theinvalidprop instead.An error message for the field.
Whether the element should receive focus on render.
Handler that is called when the element receives focus.
Handler that is called when the element loses focus.
Handler that is called when the element's focus status changes.
Handler that is called when a key is pressed.
Handler that is called when a key is released.
Temporary text that occupies the text input when it is empty.
Identifies the element (or elements) that describes the object.
Identifies the element (or elements) that provide a detailed, extended description for the object.
Describes the type of autocomplete functionality the input should provide if any. See MDN.
The maximum number of characters supported by the input. See MDN.
The minimum number of characters required by the input. See MDN.
Regex pattern that the value of the input must match to be valid. See MDN.
The type of input to render. See MDN.
Hints at the type of data that might be entered by the user while editing the element or its contents. See MDN.
An attribute that takes as its value a space-separated string that describes what, if any, type of autocomplete functionality the input should provide. See MDN.
An enumerated attribute that defines whether the element may be checked for spelling errors. See MDN.
The name of the input element, used when submitting an HTML form. See MDN.
The
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.Handler that is called when the user copies text. See MDN.
Handler that is called when the user cuts text. See MDN.
Handler that is called when the user pastes text. See MDN.
Handler that is called when a text composition system starts a new text composition session. See MDN.
Handler that is called when a text composition system completes or cancels the current text composition session. See MDN.
Handler that is called when a new character is received in the current text composition session. See MDN.
Handler that is called when text in the input is selected. See MDN.
Handler that is called when the input value is about to be modified. See MDN.
Handler that is called when the input value is modified. See MDN.
Identifies the element that provides an error message for the object.
Also accepts the root element's HTML attributes (id, data-*, event handlers).
Tokens
M3's component tokens as CSS variables. Each is unset until you set it, on :root, a theme or any ancestor, and falls back to the default shown. The root carries data-variant (and data-size where it has sizes), which picks the variables that apply. See Component tokens in the Customisation guide.
| Variable | Default | Paints | Compose |
|---|---|---|---|
| Filled (variant) | |||
--md-comp-filled-text-field-container-color | var(--md-sys-color-surface-container-highest) | The container. | FilledTextFieldTokens.ContainerColor |
--md-comp-filled-text-field-container-shape | var(--md-sys-shape-corner-extra-small) | The top corners. | FilledTextFieldTokens.ContainerShape |
--md-comp-filled-text-field-active-indicator-color | var(--md-sys-color-on-surface-variant) | The line along the bottom of the container. | FilledTextFieldTokens.ActiveIndicatorColor |
--md-comp-filled-text-field-focus-active-indicator-color | var(--md-sys-color-primary) | The bottom line while the field has focus. | FilledTextFieldTokens.FocusActiveIndicatorColor |
--md-comp-filled-text-field-label-text-color | var(--md-sys-color-on-surface-variant) | The label. | FilledTextFieldTokens.LabelColor |
--md-comp-filled-text-field-focus-label-text-color | var(--md-sys-color-primary) | The label while the field has focus. | FilledTextFieldTokens.FocusLabelColor |
--md-comp-filled-text-field-input-text-color | var(--md-sys-color-on-surface) | The text the user types. | FilledTextFieldTokens.InputColor |
--md-comp-filled-text-field-caret-color | var(--md-sys-color-primary) | The text cursor. | FilledTextFieldTokens.CaretColor |
--md-comp-filled-text-field-supporting-text-color | var(--md-sys-color-on-surface-variant) | The supporting text below the field. | FilledTextFieldTokens.SupportingColor |
--md-comp-filled-text-field-leading-icon-color | var(--md-sys-color-on-surface-variant) | The leading icon. | FilledTextFieldTokens.LeadingIconColor |
--md-comp-filled-text-field-trailing-icon-color | var(--md-sys-color-on-surface-variant) | The trailing icon. | FilledTextFieldTokens.TrailingIconColor |
| Outlined (variant) | |||
--md-comp-outlined-text-field-container-shape | var(--md-sys-shape-corner-extra-small) | The corners. | OutlinedTextFieldTokens.ContainerShape |
--md-comp-outlined-text-field-outline-color | var(--md-sys-color-outline) | The 1px outline. | OutlinedTextFieldTokens.OutlineColor |
--md-comp-outlined-text-field-hover-outline-color | var(--md-sys-color-on-surface) | The outline under the pointer. | OutlinedTextFieldTokens.HoverOutlineColor |
--md-comp-outlined-text-field-focus-outline-color | var(--md-sys-color-primary) | The 2px outline while the field has focus. | OutlinedTextFieldTokens.FocusOutlineColor |
--md-comp-outlined-text-field-label-text-color | var(--md-sys-color-on-surface-variant) | The label. | OutlinedTextFieldTokens.LabelColor |
--md-comp-outlined-text-field-focus-label-text-color | var(--md-sys-color-primary) | The label while the field has focus. | OutlinedTextFieldTokens.FocusLabelColor |
--md-comp-outlined-text-field-input-text-color | var(--md-sys-color-on-surface) | The text the user types. | OutlinedTextFieldTokens.InputColor |
--md-comp-outlined-text-field-caret-color | var(--md-sys-color-primary) | The text cursor. | OutlinedTextFieldTokens.CaretColor |
--md-comp-outlined-text-field-supporting-text-color | var(--md-sys-color-on-surface-variant) | The supporting text below the field. | OutlinedTextFieldTokens.SupportingColor |
--md-comp-outlined-text-field-leading-icon-color | var(--md-sys-color-on-surface-variant) | The leading icon. | OutlinedTextFieldTokens.LeadingIconColor |
--md-comp-outlined-text-field-trailing-icon-color | var(--md-sys-color-on-surface-variant) | The trailing icon. | OutlinedTextFieldTokens.TrailingIconColor |
Spec status
Built to the M3 Expressive text field spec. Each item of its variants and configurations links to a live example; where the M3 Expressive column of the spec differs from the original M3 one, it wins.
| Item | Status | Details |
|---|---|---|
| Filled text field | Built | Variant · Variants |
| Outlined text field | Built | Variant · Variants |
| Leading and trailing icons | Built | Configuration · Adornments, Form |
| Prefix and suffix | Built | Configuration · Adornments |
| Supporting text and character counter | Built | Configuration · Variants, Multiline |
| Error, with an error icon | Built | Configuration · States, Form |
| Required, disabled and read-only | Built | Configuration · States |
| Text area and multi-line field | Built | Configuration · Multiline |
| Forms | Built | Use case · Form |
| Density (web, chosen by the reader) | Built | Use case · Density |
| Autofill | Extension | The label also floats for the browser's autofill (:has(:autofill)), since an autofilled value is hidden from scripts until the person interacts. |
prefixLabel and suffixLabel | Extension | What screen readers hear for a prefix or suffix ("US dollars" for "$"), which join the input's description. |
| Composable field parts | Not built | Only the complete TextField is exported as a field. Its label, supporting text, error text, counter and error icon are in @windmorph/react/primitives, but the container, outline and active indicator stay internal. |
Filled text field
BuiltVariant · Variants
Outlined text field
BuiltVariant · Variants
Leading and trailing icons
BuiltConfiguration · Adornments, Form
Prefix and suffix
BuiltConfiguration · Adornments
Required, disabled and read-only
BuiltConfiguration · States
Text area and multi-line field
BuiltConfiguration · Multiline
Forms
BuiltUse case · Form
Density (web, chosen by the reader)
BuiltUse case · Density
Autofill
ExtensionThe label also floats for the browser's autofill (
:has(:autofill)), since an autofilled value is hidden from scripts until the person interacts.
ExtensionprefixLabelandsuffixLabelWhat screen readers hear for a prefix or suffix ("US dollars" for "$"), which join the input's description.
Composable field parts
Not builtOnly the complete
TextFieldis exported as a field. Its label, supporting text, error text, counter and error icon are in@windmorph/react/primitives, but the container, outline and active indicator stay internal.
Bundle size
Gzipped, as published: the component and the library code it imports. React, React Aria, Motion and the other dependencies aren't counted, and your bundler's minifier makes it smaller still. CI fails a change that grows it past its budget.
TextField: 13.2 kB
M3 Expressive specs
- Shape
rounded-corner-extra-small- Variants
filled, outlined
Differences from Compose
- Values come from Compose's
TextFieldImpl.ktand the token files: a 56px minimum height, a 280px default width, 16px padding that drops to 4px next to an icon (icons sit in 48px boxes). - Colours follow a single computed
data-field-state(disabled > error-focus > error-hover > error > focus > hover > rest), so the parts never compete on CSS order;data-floatedand friends are exposed too. - The floating label is an absolutely positioned
<label>that transitionstop,inset-inline-startand the type scale, so it needs no transforms and mirrors in RTL (it also floats for browser autofill). - The outlined notch is a hidden
fieldset/legendcarrying the label text, so the gap matches the label exactly, with 4px on each side of it. The gap starts 12px from the field's edge at both of the outline's widths, so it is in the same place focused or not. With a label the placeholder, prefix and suffix appear only once it floats. - Below 52px (density −2 and −3) a filled field hides its label once it floats and centres the value, as Material's web text field does; the resting label still shows in an empty field. The outline grows to 2px on a spring that doesn't overshoot, since a browser draws borders in whole pixels.
multilineis a fixed-height text area by default, which M3 recommends over growing fields on the web;autoGrowgives Compose's growing field.supportingTextOnFocusshows the supporting text only while the field has focus.- Select and Autocomplete draw the same field, so the text field's component tokens (
--md-comp-filled-text-field-*,--md-comp-outlined-text-field-*) restyle them too. M3's separatefilled-selectandfilled-autocompletetoken sets aren't exposed. - Not yet supported: the composable TextField parts (the "two API levels"); only the batteries-included form is exported, built from the Field primitives.