Skip to content

Material 3

Components

Text field

Text fields let users enter text into a UI.

9:30

New contact

Text fields let people enter text into a UI, such as contact or payment information. They commonly appear in forms and dialogs.

To type a date, use a date field, and to search, a search bar.

import { TextField } from '@material-web/react'

Basic text field

Set the label text with label and the supporting text with description, and put icons around the input with leading and trailing. A text field takes no children.

import { Icon, IconButton, TextField } from '@material-web/react'
import { useState } from 'react'

export default function Basic() {
  const [amount, setAmount] = useState('12.50')
  return (
    <TextField
      variant="outlined"
      label="Amount"
      description="Before tax"
      name="amount"
      inputMode="decimal"
      isRequired
      maxLength={8}
      prefix="€"
      value={amount}
      onChange={setAmount}
      trailing={
        <IconButton aria-label="Clear" onPress={() => setAmount('')}>
          <Icon name="cancel" />
        </IconButton>
      }
      className="w-64"
    />
  )
}

The TextField comes in two variants: set variant to filled (the default) or outlined. Both work the same way, so choose between them by style. Use one variant within a region, and do not mix the two there.

import { TextField } from '@material-web/react'

export default function Variants() {
  return (
    <>
      <TextField variant="filled" label="Filled" description="Supporting text" className="w-64" />
      <TextField
        variant="outlined"
        label="Outlined"
        description="Supporting text"
        className="w-64"
      />
    </>
  )
}

Filled text field

A filled text field has more visual emphasis than an outlined one, which makes it stand out among other content and components.

import { TextField } from '@material-web/react'

export default function Filled() {
  return (
    <>
      <TextField variant="filled" label="Filled" description="Supporting text" className="w-64" />
      <TextField variant="filled" label="Disabled" isDisabled className="w-64" />
    </>
  )
}

Outlined text field

An outlined text field has less visual emphasis, which helps simplify a layout where many fields are placed together, such as a form.

import { TextField } from '@material-web/react'

export default function Outlined() {
  return (
    <>
      <TextField
        variant="outlined"
        label="Outlined"
        description="Supporting text"
        className="w-64"
      />
      <TextField variant="outlined" label="Disabled" isDisabled className="w-64" />
    </>
  )
}

Label and supporting text

Set label to the label text, which names the field. The label rests on the input's line until the field is focused or holds text, and then moves to the top of the field. Set description to the supporting text, which is shown under the field and describes it.

Set placeholder to show a hint while the field is empty. In a field with a label, the placeholder shows only once the field is focused.

Pressing the label focuses the input, and so does pressing anywhere in the field that is not another control, such as an icon button.

import { TextField } from '@material-web/react'

export default function LabelAndSupportingText() {
  return (
    <>
      <TextField
        label="Username"
        description="Letters and numbers only"
        placeholder="ada1815"
        className="w-64"
      />
      <TextField
        variant="outlined"
        label="Username"
        description="Letters and numbers only"
        defaultValue="ada1815"
        className="w-64"
      />
    </>
  )
}

Without a label

A field with no label can be named by a separate, adjacent label: set aria-labelledby to that label's id. With neither, set an aria-label. Without a label, the placeholder shows whenever the field is empty.

Full name
import { Icon, TextField } from '@material-web/react'

export default function WithoutLabel() {
  return (
    <div className="flex flex-wrap items-end justify-center gap-space-200">
      <div className="flex flex-col gap-space-100">
        <span id="full-name" className="text-body-lg text-on-surface">
          Full name
        </span>
        <TextField
          variant="outlined"
          aria-labelledby="full-name"
          placeholder="Ada Lovelace"
          className="w-64"
        />
      </div>
      <TextField
        type="search"
        aria-label="Search"
        placeholder="Search"
        leading={<Icon name="search" />}
        className="w-64"
      />
    </div>
  )
}

Icons

Set leading and trailing to the field's leading and trailing icons. Each takes one icon, icon button or 24dp image, and shows it at the start or the end of the field.

  • An icon button is a tab stop after the input. Give it an aria-label that says what it does. It is disabled with the field, unless it sets isDisabled={false} itself.
  • A value that renders nothing shows no icon, so an icon can be conditional: trailing={invalid && <Icon name="error" />}. In this example, the clear icon appears only while there is text to clear.
import { Icon, IconButton, TextField } from '@material-web/react'
import { useState } from 'react'

export default function Icons() {
  const [isShown, setShown] = useState(false)
  const [query, setQuery] = useState('Material')
  return (
    <>
      <TextField
        label="Password"
        type={isShown ? 'text' : 'password'}
        autoComplete="current-password"
        className="w-64"
        leading={<Icon name="lock" />}
        trailing={
          <IconButton
            aria-label={isShown ? 'Hide password' : 'Show password'}
            onPress={() => setShown(!isShown)}
          >
            <Icon name={isShown ? 'visibility_off' : 'visibility'} />
          </IconButton>
        }
      />
      <TextField
        variant="outlined"
        label="Search"
        type="search"
        value={query}
        onChange={setQuery}
        className="w-64"
        leading={<Icon name="search" />}
        trailing={
          query !== '' && (
            <IconButton aria-label="Clear" onPress={() => setQuery('')}>
              <Icon name="cancel" />
            </IconButton>
          )
        }
      />
    </>
  )
}

Prefix and suffix

Set prefix and suffix to show text on the input's line, before and after the input text: prefix="€", suffix="@gmail.com". In a field with a label, they show once the field is focused or holds text. They are shown as written and do not describe the input.

import { TextField } from '@material-web/react'

export default function PrefixAndSuffix() {
  return (
    <>
      <TextField
        label="Amount"
        inputMode="decimal"
        defaultValue="12.50"
        prefix="€"
        className="w-48"
      />
      <TextField label="Weight" inputMode="numeric" suffix="lbs" className="w-48" />
      <TextField variant="outlined" label="Email" suffix="@gmail.com" className="w-48" />
    </>
  )
}

State functions

Pass a function as leading, trailing, prefix or suffix to render it from the field's state: isHovered, isFocusWithin, isFocusVisible, isDisabled and isInvalid. isFocusWithin is true while the input has the focus, from the pointer too. The state's type is exported as TextFieldRenderProps.

import { Icon, TextField } from '@material-web/react'
import { useState } from 'react'

export default function RenderProps() {
  const [name, setName] = useState('ada lovelace')
  return (
    <TextField
      variant="outlined"
      label="Username"
      description="No spaces"
      value={name}
      onChange={setName}
      isInvalid={name.includes(' ')}
      className="w-64"
      leading={({ isFocusWithin }) => (
        <Icon name="person" className={isFocusWithin ? 'icon-filled' : undefined} />
      )}
      trailing={({ isInvalid }) =>
        isInvalid && <Icon name="error" className="icon-filled" aria-label="Error" />
      }
    />
  )
}

Multiline

Set lines to choose how the field displays its text:

  • single (the default) displays one line of text.
  • multi starts at the height of its rows and grows with its text.
  • area keeps the height of its rows and scrolls.
import { TextField } from '@material-web/react'

const note =
  'This is a long input that wraps overflow text onto a new line, past the height of the text area.'

export default function Lines() {
  return (
    <div className="flex flex-wrap items-start justify-center gap-space-200">
      <TextField label="Multi-line" lines="multi" defaultValue={note} className="w-64" />
      <TextField
        variant="outlined"
        label="Text area"
        lines="area"
        rows={3}
        defaultValue={note}
        className="w-64"
      />
    </div>
  )
}

Without rows, a multi field starts as one line and an area is two lines tall.

Controlled

Set the initial text with defaultValue, and the field keeps its text itself. To keep the text in your own state, control the field with value and onChange, which is called with the new text and not with an event.

Hello, Ada
import { TextField } from '@material-web/react'
import { useState } from 'react'

export default function Controlled() {
  const [name, setName] = useState('Ada')
  return (
    <>
      <TextField label="Uncontrolled" defaultValue="Ada" className="w-48" />
      <TextField label="Controlled" value={name} onChange={setName} className="w-48" />
      <span className="text-body-md text-on-surface-variant">
        {name === '' ? 'No name yet' : `Hello, ${name}`}
      </span>
    </>
  )
}

Color

A focused field draws its label, its caret and its active indicator or outline in the primary color role. To use another color, set that role on the field in your own CSS. The value applies to everything inside the field. See Theme.

.field-tertiary {
  --md-sys-color-primary: var(--md-sys-color-tertiary);
}
<TextField label="Name" className="field-tertiary" />

Widths

Set the field's width in className, such as w-64, or w-full to make the field as wide as the element it is in. Without a width, the field is as wide as the browser's default input.

import { TextField } from '@material-web/react'

export default function FullWidth() {
  return <TextField variant="outlined" label="Address" className="w-full" />
}

Form props

A text field takes the standard form props, such as name, type, placeholder, isRequired, isReadOnly, isDisabled, maxLength and autoFocus. For the error state, see Validation.

Required

Set isRequired to end the label with an asterisk, which becomes part of the field's name. Use the supporting text to say what the asterisk means.

import { TextField } from '@material-web/react'

export default function Required() {
  return <TextField label="Username" description="*required" isRequired className="w-64" />
}

Read-only and disabled

Set isReadOnly to make the field read-only. A read-only field looks the same as any other, so label it as read-only. Set isDisabled to disable the field and any icon button in it.

import { TextField } from '@material-web/react'

export default function ReadOnlyAndDisabled() {
  return (
    <>
      <TextField
        label="Order number"
        description="Read-only"
        defaultValue="CA-2026-10-0417"
        isReadOnly
        className="w-64"
      />
      <TextField
        variant="outlined"
        label="Coupon"
        description="Not available for this order"
        defaultValue="SPRING"
        isDisabled
        className="w-64"
      />
    </>
  )
}

Character counter

Set maxLength to also show a character counter after the supporting text: the characters used over the limit. It counts a controlled field's value, or an uncontrolled field's defaultValue and what is typed into it.

Set counterLabel to the text a screen reader reads before the counter's numbers. It is Character count by default.

import { TextField } from '@material-web/react'

export default function CharacterCounter() {
  return (
    <TextField
      label="Display name"
      description="Shown on your profile"
      defaultValue="Ada"
      maxLength={20}
      className="w-64"
    />
  )
}

Controlling the HTML input

The props of an <input> that a text field takes go to its <input>: id, name, type, inputMode, autoComplete, autoCapitalize, pattern, minLength, maxLength, enterKeyHint and spellCheck. Set inputRef for a ref to the <input>, or to the <textarea> when lines is multi or area.

<TextField label="Amount" inputMode="decimal" enterKeyHint="done" inputRef={inputRef} />

className, style, ref and a data-* attribute go to the field's outermost element instead. See Customization.

Input types

Set type to the type of the input. With text, search, url, tel, email, password and number, the label rests on the input's line while the field is empty. With any other type, such as date or time, the label stays at the top of the field.

A search input does not show the browser's own clear button or magnifier. Add these icons with leading and trailing.

import { TextField } from '@material-web/react'

export default function InputTypes() {
  return (
    <>
      <TextField label="Date" type="date" className="w-48" />
      <TextField label="Time" type="time" className="w-48" />
      <TextField label="Quantity" type="number" placeholder="0" className="w-48" />
    </>
  )
}

Validation

Set isInvalid to put the field in the error state. Pass the error text as description while the field is invalid: it replaces the supporting text and is announced as an alert. Add an error icon as a second cue. A trailing icon takes the error color, and one that is not a button needs an aria-label, such as Error.

In this example, the field is controlled, and is invalid while its text is not an email address.

import { Icon, TextField } from '@material-web/react'
import { useState } from 'react'

export default function ErrorState() {
  const [email, setEmail] = useState('name@')
  const isInvalid = email !== '' && !/^\S+@\S+\.\S+$/.test(email)
  return (
    <TextField
      variant="outlined"
      label="Email"
      type="email"
      value={email}
      onChange={setEmail}
      isInvalid={isInvalid}
      description={isInvalid ? 'Enter an address such as name@example.com' : 'Used to sign in'}
      trailing={isInvalid && <Icon name="error" className="icon-filled" aria-label="Error" />}
      className="w-80"
    />
  )
}

The field also shows the messages of React Aria's validation: those that validate returns, and the browser's own for native constraints such as isRequired. They show once the form is submitted, or as the text changes when validationBehavior is aria. While the field is invalid, they replace the supporting text as the error text and are announced as an alert. A field that is invalid with no message shows its description as the error text.

<TextField
  label="Username"
  description="At least 3 characters"
  validationBehavior="aria"
  validate={(text) => (text.length < 3 ? 'Enter at least 3 characters' : null)}
/>

A field in a form is also validated when the form is submitted. See Forms.

Forms

Set a name to include the field in a form. It is submitted and validated with the form, and its text and counter are reset with it.

Validation is native unless validationBehavior is aria. A required field left empty, or a field that is isInvalid, blocks its form from being submitted. A field its form finds invalid on submit shows the error state, with the browser's validation message as the error text, until its text is changed and the field loses the focus, or until the form is reset. See React Aria's Forms guide.

*indicates a required field
import { Button, TextField } from '@material-web/react'
import { useState } from 'react'

export default function Form() {
  const [sent, setSent] = useState<string>()
  return (
    <form
      className="flex w-80 flex-col gap-space-200"
      onSubmit={(event) => {
        event.preventDefault()
        setSent(JSON.stringify(Object.fromEntries(new FormData(event.currentTarget))))
      }}
    >
      <span className="text-body-md text-on-surface-variant">*indicates a required field</span>
      <TextField variant="outlined" label="Name" name="name" autoComplete="name" isRequired />
      <TextField variant="outlined" label="Email" name="email" type="email" autoComplete="email" />
      <TextField
        variant="outlined"
        label="Message"
        name="message"
        lines="area"
        rows={4}
        maxLength={200}
      />
      <div className="flex justify-end gap-space-100">
        <Button type="reset" variant="text" onPress={() => setSent(undefined)}>
          Reset
        </Button>
        <Button type="submit">Send</Button>
      </div>
      {sent && <output className="text-body-sm text-on-surface-variant">{sent}</output>}
    </form>
  )
}

Customization

Pass classes in className to size and place one field. See Styling for the classes, the states and the theme.

  • Element: A text field is an inline flex box, aligned to the top of its line. className, style and ref apply to its outermost element, so set its width and position there. inputRef applies to its <input>, or to its <textarea> when lines is multi or area.
  • States: On the field, style the disabled, invalid, read-only and required states with their variants, such as invalid:. Hover and focus are reported inside the field: use the has-* form for them, as in has-hover:bg-surface-container. See Variants.
  • Text inputs: The populated: variant matches a text input that holds text, that is, one not showing its placeholder.
  • Functions: className and style can be functions of isDisabled, isInvalid, isReadOnly and isRequired. For hover and focus, pass a state function as leading, trailing, prefix or suffix.
  • Every field: To restyle all the text fields at once, change the theme.

In your own CSS, target these attributes:

ElementAttributes
Outermost elementdata-disabled, data-invalid, data-readonly and data-required
Container around the input and its iconsdata-hovered, data-focus-within, data-focus-visible, data-disabled and data-invalid
Inputdata-focused whenever it has the focus, from the pointer too, and data-hovered, data-focus-visible, data-disabled and data-invalid

Density

A text field is 56dp tall, and has no size prop. Add the density-1, density-2 or density-3 class of the theme's spacing to the field, or to an element around it, to make the field 4dp shorter for each step.

import { TextField } from '@material-web/react'

export default function Density() {
  return (
    <>
      <TextField variant="outlined" label="Default" defaultValue="56dp" className="w-40" />
      <TextField
        variant="outlined"
        label="density-1"
        defaultValue="52dp"
        className="w-40 density-1"
      />
      <TextField
        variant="outlined"
        label="density-2"
        defaultValue="48dp"
        className="w-40 density-2"
      />
      <TextField
        variant="outlined"
        label="density-3"
        defaultValue="44dp"
        className="w-40 density-3"
      />
    </>
  )
}

Don't apply density to text fields by default: this lowers their targets below the recommended 48 by 48 CSS pixels. Instead, give people a way to choose a higher density, like selecting a denser layout or changing the theme.

Custom input

Pass a function to input to render an <input> of your own in place of the field's, such as one with an attribute that the field does not take, like list. It is called with the props to spread on your element, including ref, and with the input's state, such as isFocused and isInvalid. When lines is multi or area, it renders the <textarea> instead.

In this example, the input suggests values from a list.

import { TextField } from '@material-web/react'

export default function CustomInput() {
  return (
    <>
      <TextField
        label="City"
        input={(props) => <input {...props} list="cities" />}
        className="w-64"
      />
      <datalist id="cities">
        <option value="Calgary" />
        <option value="Edmonton" />
        <option value="Vancouver" />
      </datalist>
    </>
  )
}

Limitations

Floating label

The raised label of an outlined field extends above the field's top edge. If a scrolling box starts with an outlined field, give the box pt-space-100 so that the label is not cut off.

A label wider than the field is cut off with an ellipsis, so keep it short.

Supporting text

Supporting text and the character counter make the field taller. In a row where only some fields have them, align the fields to the top, as with items-start, so that their containers line up.

Disabled fieldset

A field in a disabled <fieldset> cannot be edited, but its label and text keep their enabled colors, and it does not report the disabled state. Set isDisabled on the field as well.