# Field

Form field wrapper that associates a label, helper description, and validation error with its child control via React context. Generates a stable `useId` identifier and wires `htmlFor`, `aria-describedby`, `aria-invalid`, and `aria-required` automatically.

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| label | ReactNode | required | Visible label text bound to the control via htmlFor. |
| description | ReactNode | undefined | Optional helper text linked via aria-describedby. |
| error | ReactNode | undefined | Error message rendered with role='alert', linked via aria-describedby, and setting aria-invalid. |
| required | boolean | false | Displays a required marker and sets required and aria-required on the child control. |
| id | string | useId() | Optional explicit control id overriding the generated useId value. |
| children | ReactNode | required | Form control such as Input or Textarea that reads Field context. |

## Sizes and tokens

| Tier / Role | Token | Value |
| --- | --- | --- |
| Stack spacing | gap-2 (--spacing-2) | 8px vertical gap between label, control, description, and error |
| Label typography | text-caption (--text-caption) + --foreground | 12px / 16px medium weight |
| Description text | text-caption + --muted-foreground | 12px / 16px neutral step 11 |
| Error text | text-caption + --destructive-text | 12px / 16px red step 11 with role='alert' |

## Code

```tsx
import { Field, Input } from "@freshgemui/monoplinth";

export function Example({ error }: { error?: string }) {
  return (
    <Field
      label="Workspace slug"
      description="Lowercase letters and hyphens only."
      error={error}
      required
    >
      <Input placeholder="monoplinth" />
    </Field>
  );
}
```

## Do and don't

- **Do: Always provide visible error text when validation fails** — Pass a concrete error string to Field so screen readers announce the failure and users never rely on border color alone.
- **Don't: Hardcode matching ids by hand or hide labels visually** — Do not wire htmlFor and aria-describedby manually for every input when Field handles id generation and state attributes.

## Accessibility

- Generates a deterministic `useId` and connects `<label htmlFor>` to the child control's `id`.
- Joins both description and error element ids into `aria-describedby` so assistive technology reads guidance and errors together.
- Sets `aria-invalid="true"` on the child control only when `error` is present, and renders error text in a `role="alert"` paragraph so state never relies on color alone (WCAG 1.4.1).
- Sets both `required` and `aria-required="true"` on the child control when `required` is true.

## Rationale

Centralizing id generation and ARIA attribute wiring in `Field` prevents mismatched `htmlFor` or `aria-describedby` references across forms while keeping `Input` and `Textarea` composable on their own.
