Skip to content

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

By name or description, across the 66 props below

TextField 66 props

  • idtype string

    The <input>'s (or <textarea>'s) id, for a <label for> or an error summary's link.

  • varianttype "filled" | "outlined"default "filled"

    The container style: a filled surface or an outline.

  • densitytype 0 | -1 | -2 | -3default 0

    Web density, 0 to -3: each step takes 4px off the container (56px at 0, 44px at -3). Left out, the field takes the density of the nearest ThemeProvider or ThemeScope. M3 asks for density to be the reader's choice rather than a default, so prefer setting it there, from a setting.

  • valuetype string

    The text, when controlled.

  • defaultValuetype stringdefault ""

    The initial text, when uncontrolled.

  • supportingTexttype ReactNode

    Helper text below the field. Replaced visually by the error message when invalid, and shown in the error colour while the field is invalid.

  • supportingTextOnFocustype booleandefault false

    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.

  • invalidtype boolean

    Marks the field invalid (in addition to validate and native constraints).

  • requiredtype boolean

    A value is required: adds the * to the label and native required validation.

  • disabledtype booleandefault false

    Disables the field: it can't be focused or edited.

  • readOnlytype booleandefault false

    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.

  • leadingIcontype ReactNode

    Decorative icon at the start, in a 48px box.

  • trailingIcontype ReactNode

    Icon or icon button at the end (e.g. clear, visibility toggle), in a 48px box. It takes the place of the error icon.

  • errorIcontype ReactNode

    The icon shown at the end while the field is invalid and has no trailingIcon (M3 strongly recommends one). Defaults to Material Symbols error, named by labels.error; false turns it off.

  • prefixtype ReactNode

    Text before the input, e.g. "$". Shown once the label floats.

  • prefixLabeltype string

    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.

  • suffixtype ReactNode

    Text after the input, e.g. "kg". Shown once the label floats.

  • suffixLabeltype string

    What screen readers hear for the suffix, e.g. "kilograms" for "kg".

  • multilinetype booleandefault false

    Renders a <textarea>. By default it's an M3 text area: a fixed height of rows lines that scrolls vertically, which M3 recommends over growing fields on the web.

  • autoGrowtype booleandefault false

    With multiline: grow with the content from rows up to maxRows (M3's multi-line field) instead of keeping a fixed height.

  • rowstype numberdefault 4, or 1 with autoGrow

    Visible lines of a multiline field.

  • maxRowstype number

    With autoGrow: lines after which the field scrolls instead of growing.

  • countertype boolean

    Shows count/maxLength below the field. Defaults to true when maxLength is set.

  • reftype Ref<HTMLDivElement>

    The root element. Use inputRef for the input itself.

  • inputReftype Ref<HTMLInputElement | HTMLTextAreaElement>

    The <input> (or <textarea> when multiline), e.g. for form libraries.

  • labeltype ReactNode

    The visible label, which floats above the input once it has focus or a value.

    One of label, aria-label or aria-labelledby is required.

Events

  • onChangetype ((value: string) => void)

    Called with the new text on every change.

Text

  • labelstype Partial<TextFieldLabels>

    Words the field announces, for other languages. A LabelsProvider sets them for the whole app.

  • aria-labeltype string

    Defines a string value that labels the current element.

    One of label, aria-label or aria-labelledby is required.

  • aria-labelledbytype string

    Identifies the element (or elements) that labels the current element.

    One of label, aria-label or aria-labelledby is required.

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.

Text field tokens (md-comp-text-field)
VariableDefaultPaintsCompose
Filled (variant)
--md-comp-filled-text-field-container-colorvar(--md-sys-color-surface-container-highest)The container.FilledTextFieldTokens.ContainerColor
--md-comp-filled-text-field-container-shapevar(--md-sys-shape-corner-extra-small)The top corners.FilledTextFieldTokens.ContainerShape
--md-comp-filled-text-field-active-indicator-colorvar(--md-sys-color-on-surface-variant)The line along the bottom of the container.FilledTextFieldTokens.ActiveIndicatorColor
--md-comp-filled-text-field-focus-active-indicator-colorvar(--md-sys-color-primary)The bottom line while the field has focus.FilledTextFieldTokens.FocusActiveIndicatorColor
--md-comp-filled-text-field-label-text-colorvar(--md-sys-color-on-surface-variant)The label.FilledTextFieldTokens.LabelColor
--md-comp-filled-text-field-focus-label-text-colorvar(--md-sys-color-primary)The label while the field has focus.FilledTextFieldTokens.FocusLabelColor
--md-comp-filled-text-field-input-text-colorvar(--md-sys-color-on-surface)The text the user types.FilledTextFieldTokens.InputColor
--md-comp-filled-text-field-caret-colorvar(--md-sys-color-primary)The text cursor.FilledTextFieldTokens.CaretColor
--md-comp-filled-text-field-supporting-text-colorvar(--md-sys-color-on-surface-variant)The supporting text below the field.FilledTextFieldTokens.SupportingColor
--md-comp-filled-text-field-leading-icon-colorvar(--md-sys-color-on-surface-variant)The leading icon.FilledTextFieldTokens.LeadingIconColor
--md-comp-filled-text-field-trailing-icon-colorvar(--md-sys-color-on-surface-variant)The trailing icon.FilledTextFieldTokens.TrailingIconColor
Outlined (variant)
--md-comp-outlined-text-field-container-shapevar(--md-sys-shape-corner-extra-small)The corners.OutlinedTextFieldTokens.ContainerShape
--md-comp-outlined-text-field-outline-colorvar(--md-sys-color-outline)The 1px outline.OutlinedTextFieldTokens.OutlineColor
--md-comp-outlined-text-field-hover-outline-colorvar(--md-sys-color-on-surface)The outline under the pointer.OutlinedTextFieldTokens.HoverOutlineColor
--md-comp-outlined-text-field-focus-outline-colorvar(--md-sys-color-primary)The 2px outline while the field has focus.OutlinedTextFieldTokens.FocusOutlineColor
--md-comp-outlined-text-field-label-text-colorvar(--md-sys-color-on-surface-variant)The label.OutlinedTextFieldTokens.LabelColor
--md-comp-outlined-text-field-focus-label-text-colorvar(--md-sys-color-primary)The label while the field has focus.OutlinedTextFieldTokens.FocusLabelColor
--md-comp-outlined-text-field-input-text-colorvar(--md-sys-color-on-surface)The text the user types.OutlinedTextFieldTokens.InputColor
--md-comp-outlined-text-field-caret-colorvar(--md-sys-color-primary)The text cursor.OutlinedTextFieldTokens.CaretColor
--md-comp-outlined-text-field-supporting-text-colorvar(--md-sys-color-on-surface-variant)The supporting text below the field.OutlinedTextFieldTokens.SupportingColor
--md-comp-outlined-text-field-leading-icon-colorvar(--md-sys-color-on-surface-variant)The leading icon.OutlinedTextFieldTokens.LeadingIconColor
--md-comp-outlined-text-field-trailing-icon-colorvar(--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.

  • 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.

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.kt and 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-floated and friends are exposed too.
  • The floating label is an absolutely positioned <label> that transitions top, inset-inline-start and 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 / legend carrying 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.
  • multiline is a fixed-height text area by default, which M3 recommends over growing fields on the web; autoGrow gives Compose's growing field. supportingTextOnFocus shows 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 separate filled-select and filled-autocomplete token 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.