<!-- index: /slotted/llms.txt -->
# text-field

`data-ui="text-field"` · Single-line text input.

## Import

```tsx
import { TextField, TextFieldAction, TextFieldDefaultsProvider } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Field, FieldDescription, FieldLabel } from '@slotted/react/field';
import { TextField } from '@slotted/react/text-field';

export default function TextFieldBasic() {
  return (
    <Field required>
      <FieldLabel>Company name</FieldLabel>
      <TextField autoComplete="organization" clearable defaultValue="Northwind Traders" />
      <FieldDescription>Printed on every invoice and packing slip.</FieldDescription>
    </Field>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |
| `input` | input | yes |
| `start` | span | no |
| `end` | span | no |
| `action` | button (TextFieldAction), inside start or end | no |
| `busy` | span[data-ui="spinner"] | no |
| `caps-lock` | span | no |
| `clear` | button | no |
| `copy` | button | no |
| `reveal` | button | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `value` | `string` | — | TextField | Makes the value controlled for the life of the component. Without it the input keeps its own value, and a native form reset restores defaultValue. |
| `defaultValue` | `string` | — | TextField | The value an uncontrolled text field starts from. It is read once and written to the input as the native attribute. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | TextField, TextFieldDefaultsProvider | The step of the control scale. Without it the text field takes the size of the field around it, then the provider’s. Height, padding, type and icon size come from the control scales, which the theme restates for each density. |
| `variant` | `'outline' \| 'subtle' \| 'plain'` | outline | TextField, TextFieldDefaultsProvider | How much frame the control draws. plain draws no border at rest and keeps the same box, for a dense table cell. |
| `mono` | `boolean` | false | TextField, TextFieldDefaultsProvider | Sets the value in the monospaced font with tabular figures, for identifiers, hashes and keys. |
| `clearable` | `boolean` | false | TextField | Draws a button that empties the value with reason "clear" and returns focus to the input. The button is withdrawn while the value is empty, disabled or read-only. |
| `revealable` | `boolean` | false | TextField | Draws a toggle that shows a password as characters. It appears only on type="password" and is withdrawn while the input is disabled. |
| `copyable` | `boolean` | false | TextField | Draws a button that writes the value to the clipboard, shows a check for two seconds and announces the outcome. It is hidden while the value is empty or the input is disabled. |
| `loading` | `boolean` | false | TextField | Draws the shared spinner before the buttons and sets aria-busy on the input; the value stays editable. Once loading is set, false included, the spinner keeps its place so it can leave with a transition. |
| `revealed` | `boolean` | — | TextField | Makes the password toggle controlled. defaultRevealed sets the uncontrolled starting state. |
| `showCount` | `boolean` | false | TextField | Publishes the length of the value to the field around it, which draws the count at the end of its label row. |
| `countMax` | `number` | maxLength | TextField | The limit the count is measured against. Without maxLength it is a soft limit: the value can pass it, and the count turns to the danger tone before validation runs. |
| `start` | `ReactNode` | — | TextField | Content before the value in the reading direction. It is its own bidirectional run, and an icon inside it takes the control’s icon size. |
| `end` | `ReactNode` | — | TextField | Content after the value in the reading direction. |
| `disabled` | `boolean` | from the field, or false | TextField | Unset by default, so the text field takes the state of the field around it. When set, it wins, and it is the native attribute: the input leaves the tab order and form submission. |
| `readOnly` | `boolean` | from the field, or false | TextField | Unset by default, so the text field takes the state of the field around it. It is the native readonly: the input stays focusable and selectable, and its value is submitted. |
| `invalid` | `boolean` | from the field, or false | TextField | Unset by default, so the text field takes the state of the field around it. It sets aria-invalid on the input and data-invalid on the frame. |
| `required` | `boolean` | — | TextField | The input’s own attribute, passed through unchanged; TextField never writes it. A field marks a required value with aria-required instead, because the native attribute turns on browser constraint validation. |
| `type` | `'text' \| 'search' \| 'email' \| 'url' \| 'tel' \| 'password'` | text | TextField | The input’s own type, one of the six listed. A date, a time, a month or a number is not a TextField: use date-picker (forms.md FM-16), time-picker or number-field. Other strings still reach the element for compatibility and are not supported. |
| `messages` | `Partial<TextFieldMessages>` | — | TextField | Replaces words of the active language, key by key. clear, reveal and copy name the buttons; reveal does not change with the state, because aria-pressed carries it. copied and copyFailed announce the outcome of a copy. capsLock is announced once when Caps Lock turns on in a focused password field. |
| `render` | `(props: TextFieldRootProps) => ReactNode` | — | TextField | Renders the frame into another element and passes it every attribute. The props are typed against HTMLAttributes<HTMLElement>, so they can be spread onto a label or a fieldset. The input cannot be replaced, because it holds the value, the identifier and the wiring. |
| `TextFieldAction` | `ComponentPropsWithRef<"button"> & { render? }` | type="button" | start, end | A button you place inside the frame. Every native prop reaches the button, and render replaces it. MenuTrigger, PopoverTrigger and TooltipTrigger pass their wiring to it through their own render. |
| `ref` | `Ref<HTMLInputElement>` | — | TextField | Reaches the input. focus(), select() and setSelectionRange() are the element’s own, so TextField adds no imperative handle. |
| `className / style` | `string / CSSProperties` | — | TextField | Reach the frame, because the frame is what a reader sees. Every other native prop reaches the input. |
| `TextFieldDefaultsProvider` | `{ size, variant, mono }` | — | a subtree | Sets size, variant and mono once for every text field under it. A field outranks it for size, so a text field inside a field takes the field’s size. |
| `useField()` | `() => FieldContextValue \| undefined` | — | inside a Field | What TextField reads from the field around it: the identifier, the description, the disabled, read-only, invalid and required states, and the channel it publishes its count on. A text field inside a field needs no wiring of its own. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onValueChange` | `(value: string, request: ChangeRequest<string>)` | Before the value changes. The request carries the new value, the value it replaces, the reason ("keyboard" for an edit in the input, "clear" for the clear button), the originating event, and cancel(), which refuses the change before it commits. |
| `onRevealedChange` | `(revealed: boolean, request: ChangeRequest<boolean>)` | Before the password toggle changes, with reason "pointer" or "keyboard" read from the click. A form that hides the password again on blur controls revealed and listens here. |
| `onValueCopy` | `({ copied, reason, source, value }: TextFieldCopyDetail)` | After the copy button has asked the clipboard. copied is false when the clipboard refused the value, and reason is "pointer" or "keyboard". onCopy remains the input’s native clipboard event. |
| `onChange, onInput, onFocus, onBlur, onKeyDown, onKeyUp` | `the element’s own events` | Whenever the input fires them. TextField composes your handler with its own instead of replacing it: yours runs first, and always runs. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `outline`, `subtle`, `plain` | `outline` |

## Rules

- There is no WAI-ARIA Authoring Practices pattern for a text field: the APG lists thirty patterns, and none of them is a single-line text input. The sources are the HTML specification for the input element, the APG practice Providing Accessible Names and Descriptions (w3.org/WAI/ARIA/apg/practices/names-and-descriptions/) for its name, and the ARIA textbox role for what may sit inside it.
- The frame has no role. WAI-ARIA requires the children of a textbox to be non-interactive and presentational, so the clear and reveal buttons are siblings of the input inside a box that announces nothing.
- The input takes its name from the field’s label, whose for attribute points at the identifier the field generates. The APG naming practice records that some combinations of browser and assistive technology compute no name from a label that wraps its control.
- Inside a required field the input carries aria-required, never the native required, because the native attribute turns on browser constraint validation and changes how the form submits. Write required on the input to opt into the platform’s validation; TextField leaves it unchanged. disabled and readonly are the native attributes.
- The clear and reveal buttons are native buttons with names you translate once through messages. Each is at least 24 CSS pixels wide at every size and density, which meets WCAG 2.2 success criterion 2.5.8. The reveal button keeps one name and carries its state in aria-pressed, which is the APG rule for a toggle button.
- The copy button announces its outcome through the shared live announcer, Copied or Copy failed, because the check that replaces the copy mark is only visible. The Caps Lock mark is hidden from assistive technology; its message is announced once when Caps Lock turns on while the password field has focus.
- loading sets aria-busy on the input and leaves it enabled, so the keyboard stays on the input while an asynchronous check runs. The spinner is hidden from assistive technology.
- A menu inside the frame follows the APG menu button pattern (w3.org/WAI/ARIA/apg/patterns/menu-button/). The menu trigger on the action supplies aria-haspopup, aria-expanded, and Enter, Space and ArrowDown to open.
- No key is bound to Escape. Some browsers clear a search input on Escape, and a text field that consumed the key inside a dialog would leave a keyboard user unable to close the dialog. WCAG success criterion 2.1.4 addresses the defects that single-key shortcuts cause.
- The invalid state does not rely on color alone: aria-invalid announces it, the field’s error states it in words, and the inline message writes a visually hidden “Error:” before the sentence. In the high-contrast scheme the border cannot carry the state, so nothing depends on it.

## Tokens

`--slotted-border-subtle`, `--slotted-control-border-color`, `--slotted-control-border-width`, `--slotted-control-focus-ring-offset`, `--slotted-control-font-family`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-lg`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-inner-size-lg`, `--slotted-control-inner-size-md`, `--slotted-control-inner-size-sm`, `--slotted-control-invalid-edge-width`, `--slotted-control-padding-inline-lg`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-radius`, `--slotted-control-shadow`, `--slotted-control-shadow-focus-geometry`, `--slotted-control-surface`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-background`, `--slotted-disabled-border`, `--slotted-disabled-foreground`, `--slotted-duration-short`, `--slotted-easing-enter`, `--slotted-easing-exit`, `--slotted-focus-ring-color`, `--slotted-focus-ring-color-invalid`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-family-mono`, `--slotted-font-weight-medium`, `--slotted-radius-none`, `--slotted-radius-xs`, `--slotted-space-2`, `--slotted-space-3`, `--slotted-surface-sunken`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-field-action-active-background`, `--slotted-text-field-action-color`, `--slotted-text-field-action-hover-background`, `--slotted-text-field-action-label-color`, `--slotted-text-field-action-size`, `--slotted-text-field-adornment-color`, `--slotted-text-field-autofill-background`, `--slotted-text-field-background`, `--slotted-text-field-border-color`, `--slotted-text-field-caps-lock-color`, `--slotted-text-field-color`, `--slotted-text-field-copy-complete-color`, `--slotted-text-field-disabled-background`, `--slotted-text-field-disabled-border-color`, `--slotted-text-field-disabled-color`, `--slotted-text-field-hover-border-color`, `--slotted-text-field-icon-size-min`, `--slotted-text-field-invalid-border-color`, `--slotted-text-field-invalid-edge-width`, `--slotted-text-field-invalid-focus-ring-color`, `--slotted-text-field-placeholder-color`, `--slotted-text-field-plain-hover-background`, `--slotted-text-field-radius`, `--slotted-text-field-readonly-background`, `--slotted-text-field-shadow`, `--slotted-text-field-subtle-background`, `--slotted-text-field-subtle-border-color`, `--slotted-text-field-subtle-hover-background`, `--slotted-text-muted`, `--slotted-tone-accent-border`, `--slotted-tone-accent-surface`, `--slotted-tone-danger-border`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`, `--slotted-tone-success-text`, `--slotted-tone-warning-text`

## Examples

See [text-field.examples.md](./text-field.examples.md).
