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

`data-ui="field"` · Label, control, help and error for one input.

## Import

```tsx
import {
  Field,
  FieldAction,
  FieldControl,
  FieldCounter,
  FieldDefaultsProvider,
  FieldDescription,
  FieldError,
  FieldHeader,
  FieldHelp,
  FieldIndicator,
  FieldLabel,
} 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 FieldBasic() {
  return (
    <Field required>
      <FieldLabel>Email</FieldLabel>
      <TextField type="email" autoComplete="email" />
      <FieldDescription>Invoices are sent to this address.</FieldDescription>
    </Field>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |
| `header` | div, or the element render returns | no |
| `label` | label | no |
| `mark` | span, inside the label | no |
| `help` | button, fixed | no |
| `indicator` | span, or the element render returns | no |
| `action` | button type="button", or the element render returns | no |
| `control` | input, or the element render returns | no |
| `counter` | span, or the element render returns | no |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `size` | `'sm' \| 'md' \| 'lg'` | md | Field | The type step, matching the control the field wraps. The label, the messages and the counter read the control type scale, which the theme restates per density. |
| `labelPlacement` | `'top' \| 'side'` | top | Field | Where the label sits. side reflows to top when the field’s own inline size is below 320 pixels; the query is a container query, not a viewport query. |
| `necessity` | `'mark' \| 'optional' \| 'words' \| 'none'` | mark | Field | How necessity is written: an asterisk on a required field, (optional) on a field a reader may skip, both words, or nothing, for a form that says once that every field is required. |
| `width` | `'short' \| 'medium' \| 'full'` | full | Field | The longest the control stands, as a cue to the length of its answer: short (--slotted-field-width-short, 6rem) for an integer of up to four digits, medium (--slotted-field-width-medium, 15rem) for an amount, a code or a short word, full for free text, a name or a picker of records. The control still shrinks below the bound in a narrower field. |
| `messages` | `Partial<FieldMessages>` | — | Field | Words that replace the active language’s, key by key: the necessity words (optional, required) and the help button’s name (help). |
| `required` | `boolean` | — | Field | Unset rather than false. Writes aria-required on the control, never the native required, which would turn on browser constraint validation and change how the form submits. |
| `invalid` | `boolean` | — | Field, FieldCounter | On Field: unset rather than false; writes aria-invalid on the control and data-invalid on the field, and leaves the label’s color unchanged. On FieldCounter: the count has passed its limit, which can happen before validation runs, so it is the counter’s own state and defaults to false. |
| `disabled` | `boolean` | from the group, then the provider | Field, FieldDefaultsProvider | Unset rather than false, so a field follows the FieldGroup around it. A stated value wins, and it reaches the control as the native attribute. |
| `readOnly` | `boolean` | from the group, then the provider | Field, FieldDefaultsProvider | Unset rather than false. Reaches the control as the native readonly, which keeps the value focusable and copyable. |
| `id` | `string` | generated | Field | The base of every identifier the field derives: <id>-control, <id>-label, <id>-indicator, <id>-help, <id>-description, <id>-counter and <id>-error. Generated with useId when unset. |
| `children` | `ReactNode` | — | FieldHelp | The help text. Written once, it reaches the tooltip and the hidden node that describes the button and the control. |
| `open` | `boolean` | — | FieldHelp | Whether the hint is showing, controlled. Pair it with onOpenChange; leave both unset and FieldHelp owns the state. |
| `defaultOpen` | `boolean` | false | FieldHelp | Whether the hint starts open, read once when FieldHelp owns the state. |
| `presentation` | `'toggletip' \| 'auto'` | toggletip | FieldHelp | auto draws the hint as a bottom sheet under (max-width: 480px) and (pointer: coarse): modal, headed by the field’s label beside a close control, dismissed by its grip, its scrim, Escape and the platform’s way back, with focus given back to the help button. Anywhere else it is the toggletip. |
| `helpPresentation` | `'toggletip' \| 'auto'` | toggletip | FieldDefaultsProvider | The presentation of every FieldHelp inside, unless a FieldHelp states its own. |
| `side` | `'top' \| 'bottom' \| 'inline-start' \| 'inline-end' \| 'left' \| 'right'` | top | FieldHelp | Where the hint opens. It flips to the opposite side when there is no room. |
| `aria-label` | `string` | messages.help | FieldHelp | The accessible name of the help button. Unset, it is the field’s help message; set, it applies to this button alone. |
| `type` | `'button' \| 'submit' \| 'reset'` | button | FieldAction | button by default, so the action never submits the form it sits in. A link rendered through render receives the props without it. |
| `tone` | `'accent' \| 'secondary' \| 'success' \| 'warning' \| 'danger'` | secondary on FieldDescription, danger on FieldError | FieldDescription, FieldError | Both messages are InlineMessage roots and take its five tones, so a warning that does not block submission is FieldError with tone="warning". |
| `mark` | `'glyph' \| 'dot' \| 'none'` | none | FieldDescription, FieldError | The mark the message draws. A field’s messages draw none by default: an icon repeated under every control of a form reads as texture, and the ink and the sentence already state the tone. glyph draws the tone’s icon at the text size, and dot draws one neutral bullet. |
| `live` | `'off' \| 'polite' \| 'assertive'` | off | FieldDescription, FieldError | Whether the message announces itself. off by default, because a message present when the page loads is not announced whatever role it carries. |
| `reserve` | `boolean` | false | FieldDescription, FieldError | Keeps one line open whether or not there is a message, so a form that renders its error unconditionally does not move when the error appears. |
| `render` | `(props) => ReactNode` | — | Field, FieldHeader, FieldLabel, FieldIndicator, FieldAction, FieldControl, FieldCounter | Replaces the rendered element and hands it every attribute: a textarea as the control, an output as the counter, a link as the action, a fieldset as the field. The props are typed as HTMLAttributes<HTMLElement>, so they spread onto any element. FieldHelp has no render: its button carries the name, the description and the tooltip control, and all three belong to Field. |
| `onClick and the pointer and focus handlers` | `native` | — | FieldHelp, FieldAction | The consumer’s handler runs first and the field’s after it, unless the consumer called preventDefault. |
| `useField()` | `() => FieldContextValue \| undefined` | — | any component inside a Field | Returns the identifiers, the description and the four states, so a control Slotted does not ship wires itself without a wrapper. Returns undefined outside a field, so the same control also works alone. |
| `FieldDefaultsProvider` | `{ size, labelPlacement, necessity, disabled, readOnly }` | — | a subtree | The five decisions an application takes once for every field under it. FieldGroup renders one for the fields inside it, so a compact or disabled section is one prop instead of one per field. |
| `ref` | `Ref<HTMLElement>` | — | every part | Reaches the default element; on FieldHelp, the button. A part rendered through render is the consumer’s element, so the consumer attaches its ref there. There is no imperative handle: the one state Field holds, the hint, is controlled through open and onOpenChange. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onOpenChange` | `(open: boolean, request: TooltipChangeRequest)` | On FieldHelp, before the hint opens or closes, once per transition. request.reason is pointer for a hover or a press; keyboard for focus, Enter or Space; or escape, blur or programmatic. The sheet of presentation="auto" also reports outsidePress for a press on its scrim, and dismiss for its close control and the platform’s way back. request.cancel() refuses the change. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `labelPlacement` | `top`, `side` | `top` |
| `necessity` | `mark`, `optional`, `words`, `none` | `mark` |
| `width` | `full`, `medium`, `short` | `full` |

## Rules

- No WAI-ARIA Authoring Practices pattern covers a field. Four sources cover it together: the Authoring Practices’ Providing Accessible Names and Descriptions practice (w3.org/WAI/ARIA/apg/practices/names-and-descriptions/) for the label and the description, the HTML specification for label[for] and fieldset[disabled], and WCAG 2.2 Understanding 3.3.1 Error Identification and 3.3.2 Labels or Instructions for the error and the help.
- The label is associated explicitly. The naming practice records that some combinations of assistive technology and browser do not treat the implicit form, a label wrapping its control, as an accessible name, so the field derives an identifier and points for at it instead of nesting.
- Field writes aria-required, never the native required. The native attribute turns on browser constraint validation and changes how the form submits, which describing a field must not do as a side effect. To get it, write required on the control: an explicit value on the control always wins. disabled and readonly are native, because taking a control out of the tab order and out of submission is what those attributes mean.
- The accessible description is the indicator, the help, the description, the count and the error, in that order, whatever order the page writes them in. Each slot has one correct reading order, and deriving the order from registration would make what a reader hears depend on when an effect ran. The count is included so a counter is related to its control by identifier.
- The help, the indicator and the action stand beside the label, never inside it. HTML forbids labelable content such as a button inside a label other than the control it labels, and a press on it would reach the label and move focus to the control instead of opening the hint.
- The help is a toggletip. A tooltip alone opens on a touch screen only on a long press, because a tap triggers the element’s own action; the help button has no other action, so a press opens the hint and a second press closes it. The help text also sits in a hidden node inside the button, which describes the button and the control while the tooltip is closed, because a hidden element referenced directly still enters the description. The tooltip describes nothing, so the hint is never read twice.
- A disabled field leaves its help and its action operable, because the help often explains why the field is disabled.
- The necessity asterisk is hidden from assistive technology and the necessity words are not. A reader working through a form of twenty required fields does not need to hear “asterisk” twenty times, and aria-required already states the fact; (optional) is information no other attribute supplies.
- The error is text in the page, as WCAG 2.2 SC 3.3.1 requires, never a tooltip. As an InlineMessage it also carries the severity as visually hidden text, so a reader who cannot see the red ink hears “Error:” before the sentence.
- live defaults to off, because a message present when the page finishes loading is not announced whatever role it carries. A form that shows the error after submit sets live="polite", and the reader hears it once rather than on every render.

## Tokens

`--slotted-control-font-family`, `--slotted-control-font-size-lg`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-md`, `--slotted-control-icon-size-sm`, `--slotted-control-inner-size-md`, `--slotted-control-inner-size-sm`, `--slotted-control-radius`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-disabled-foreground`, `--slotted-field-action-color`, `--slotted-field-action-color-active`, `--slotted-field-action-color-hover`, `--slotted-field-action-radius`, `--slotted-field-column-gap`, `--slotted-field-counter-color`, `--slotted-field-counter-invalid-color`, `--slotted-field-font-family`, `--slotted-field-header-gap`, `--slotted-field-help-color`, `--slotted-field-help-radius`, `--slotted-field-help-surface-active`, `--slotted-field-help-surface-hover`, `--slotted-field-icon-size-min`, `--slotted-field-label-color`, `--slotted-field-label-font-weight`, `--slotted-field-label-inline-size`, `--slotted-field-line-height`, `--slotted-field-mark-color`, `--slotted-field-mark-gap`, `--slotted-field-mark-text-color`, `--slotted-field-row-gap`, `--slotted-field-width-medium`, `--slotted-field-width-short`, `--slotted-focus-ring-color`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-size-xs`, `--slotted-font-weight-medium`, `--slotted-font-weight-regular`, `--slotted-line-height-sm`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-space-3`, `--slotted-space-5`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-text`, `--slotted-tone-danger-text`, `--slotted-tone-secondary-subtle-active`, `--slotted-tone-secondary-subtle-hover`

## Examples

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