# Field

A label, a control and its help or error text, wired together.

A label, one control and its help or error, wired together. The label names the control, the helper and the error describe it, invalid turns the control's ring red and shows the error in place of the helper, required adds an asterisk to the label and marks the control required, disabled reaches the control. Ark UI's field: Input, Textarea, InputGroup and PasswordInput read it through its context. It replaces Input's Field part (same column, gap, type and colours).

Kind: component · Category: Inputs · Status: beta · Targets: React, Vue, Svelte · Behaviour: Zag `field` machine, through Ark UI

## Add

```bash
npx fndn add field
```

It writes the folder `field/` into the project's ui folder (`index.ts`, `field.tsx`, `Field.vue`, `Field.svelte`, `field.recipe.ts`), and what it is built from: [Label](/components/label.md), [Input](/components/input.md). First time: [Installation](/installation.md).

## Usage

```tsx
import { Field } from "@/components/ui/field";
```

```vue
import { Field } from "@/components/ui/field";
```

```svelte
import { Field } from "$lib/components/ui/field";
```

## Parts

In the DOM the root carries `data-slot="field"` and each part `data-slot="field-<part>"`; in the recipe the root is the `base` slot and each part a slot named after it in camelCase.

| Part | Element | data-slot | Flags | Notes |
| --- | --- | --- | --- | --- |
| `root` | `div` | `field` |  | Ark's field root (role="group", data-invalid / data-disabled / data-readonly): a column with a 6px gap; class and style land here. |
| `label` | `label` | `field-label` | optional | The Label component (data-field="label"), wired to the control (for / id). Only as wide as its text. |
| `required-indicator` | `span` | `field-required-indicator` | optional | An asterisk after the label text when required, in red, aria-hidden (the control itself carries required). |
| `control` | `slot` | `field-control` | optional | The control (children): Input, Textarea, InputGroup, PasswordInput or any control built on Ark's field context. An Input when omitted. |
| `helper` | `span` | `field-helper` | optional | Help under the control: text.xs, ink-2 (read for meaning, so at least 4.5:1); wraps, keeps its own direction. Linked by aria-describedby. Hidden while the error shows. |
| `error` | `span` | `field-error` | optional | Shown only while invalid and an error is given: text.xs, red, aria-live polite, linked by aria-describedby. It takes the helper's place. |

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `node (children / slot)` |  | The control's name, above it. |
| `helper` | `node (children / slot)` |  | Help under the control. |
| `error` | `node (children / slot)` |  | What to fix; shown while invalid, in place of the helper. |
| `invalid` | `boolean` | `false` | aria-invalid on the control (its red ring) and the error shown. |
| `required` | `boolean` | `false` | required on the control; an asterisk after the label. |
| `disabled` | `boolean` | `false` | Disables the control; the label fades with it. |
| `readOnly` | `boolean` | `false` |  |
| `id` | `string` |  | id of the control; generated when omitted. |

Other attributes and `class` go to the root; `class` is merged through the recipe.

## Slots

| Slot | Content |
| --- | --- |
| `default` | The control. An Input when omitted. |

## States

`invalid`, `required`, `disabled`, `read-only`

## Keyboard and accessibility

Role: group.

Notes:

- The label names the control (for / id), so a click on it focuses the control.
- The control's aria-describedby lists the helper, or the error while invalid; the error is aria-live polite.
- invalid sets aria-invalid on the control; required sets required (the asterisk is aria-hidden); disabled and readOnly reach the control.
- Text sizes and the 16px text on touch come from the control.

## Motion

- none of its own at rest: the control's ring changes as the control's do
- error: one that appears after the field's first paint takes its line at once and fades its ink in (opacity over duration.base); on first paint and from the keyboard it is instant

## Tokens

`color.ink-2`, `color.red`, `text.xs`

## Dependencies

- [Label](/components/label.md)
- [Input](/components/input.md)

## Examples

### Billing email

React · `billing-email.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* the field wires the label (for / id) and the helper (aria-describedby) to the control */
export default function BillingEmail() {
  return (
    <Field label="Billing email" helper="Invoices and receipts are sent here." className="w-full max-w-80">
      <Input type="email" autoComplete="email" defaultValue="billing@northwind.example" />
    </Field>
  );
}
```

Vue · `billing-email.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

const email = ref("billing@northwind.example");
</script>

<!-- the field wires the label (for / id) and the helper (aria-describedby) to the control -->
<template>
  <Field label="Billing email" helper="Invoices and receipts are sent here." class="w-full max-w-80">
    <Input v-model:value="email" type="email" autocomplete="email" />
  </Field>
</template>
```

Svelte · `billing-email.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";

  let email = $state("billing@northwind.example");
</script>

<!-- the field wires the label (for / id) and the helper (aria-describedby) to the control -->
<Field label="Billing email" helper="Invoices and receipts are sent here." class="w-full max-w-80">
  <Input bind:value={email} type="email" autocomplete="email" />
</Field>
```

### Email without a domain

React · `email-without-domain.tsx`

```tsx
import { useState } from "react";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* invalid: the control's ring turns red and the error takes the helper's place */
export default function EmailWithoutDomain() {
  const [email, setEmail] = useState("daniel@northwind");
  const invalid = email !== "" && !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email);
  return (
    <Field
      label="Invite by email"
      helper="They get a link to join Northwind."
      error="Add the domain, like daniel@northwind.example."
      invalid={invalid}
      className="w-full max-w-80"
    >
      <Input type="email" value={email} onChange={(event) => setEmail(event.target.value)} />
    </Field>
  );
}
```

Vue · `email-without-domain.vue`

```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

const email = ref("daniel@northwind");
const invalid = computed(() => email.value !== "" && !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email.value));
</script>

<!-- invalid: the control's ring turns red and the error takes the helper's place -->
<template>
  <Field
    label="Invite by email"
    helper="They get a link to join Northwind."
    error="Add the domain, like daniel@northwind.example."
    :invalid="invalid"
    class="w-full max-w-80"
  >
    <Input v-model:value="email" type="email" />
  </Field>
</template>
```

Svelte · `email-without-domain.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";

  let email = $state("daniel@northwind");
  const invalid = $derived(email !== "" && !/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email));
</script>

<!-- invalid: the control's ring turns red and the error takes the helper's place -->
<Field
  label="Invite by email"
  helper="They get a link to join Northwind."
  error="Add the domain, like daniel@northwind.example."
  {invalid}
  class="w-full max-w-80"
>
  <Input bind:value={email} type="email" />
</Field>
```

### Workspace ID, read-only

React · `workspace-id.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";

/* read-only: the value can be selected and copied, not changed (unlike disabled, it stays in the tab order) */
export default function WorkspaceId() {
  return (
    <Field label="Workspace ID" helper="Support may ask for it." readOnly className="w-full max-w-80">
      <Input mono defaultValue="ws_8f2k1m9q4t" />
    </Field>
  );
}
```

Vue · `workspace-id.vue`

```vue
<script setup lang="ts">
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
</script>

<!-- read-only: the value can be selected and copied, not changed (unlike disabled, it stays in the tab order) -->
<template>
  <Field label="Workspace ID" helper="Support may ask for it." read-only class="w-full max-w-80">
    <Input mono value="ws_8f2k1m9q4t" />
  </Field>
</template>
```

Svelte · `workspace-id.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";
</script>

<!-- read-only: the value can be selected and copied, not changed (unlike disabled, it stays in the tab order) -->
<Field label="Workspace ID" helper="Support may ask for it." readOnly class="w-full max-w-80">
  <Input mono value="ws_8f2k1m9q4t" />
</Field>
```

### Project description

React · `project-description.tsx`

```tsx
import { Field } from "@/components/ui/field";
import { Textarea } from "@/components/ui/textarea";

export default function ProjectDescription() {
  return (
    <Field label="Description" helper="Shown on the project's overview page." className="w-full max-w-80">
      <Textarea placeholder="What this project does and who owns it" />
    </Field>
  );
}
```

Vue · `project-description.vue`

```vue
<script setup lang="ts">
import { ref } from "vue";
import { Field } from "@/components/ui/field";
import { Textarea } from "@/components/ui/textarea";

const description = ref("");
</script>

<template>
  <Field label="Description" helper="Shown on the project's overview page." class="w-full max-w-80">
    <Textarea v-model:value="description" placeholder="What this project does and who owns it" />
  </Field>
</template>
```

Svelte · `project-description.svelte`

```svelte
<script lang="ts">
  import { Field } from "$lib/components/ui/field";
  import { Textarea } from "$lib/components/ui/textarea";

  let description = $state("");
</script>

<Field label="Description" helper="Shown on the project's overview page." class="w-full max-w-80">
  <Textarea bind:value={description} placeholder="What this project does and who owns it" />
</Field>
```

### Create a workspace

React · `create-workspace.tsx`

```tsx
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { InputGroup } from "@/components/ui/input-group";

/* fields stack with a 16px gap; required ones show an asterisk */
export default function CreateWorkspace() {
  return (
    <form className="flex w-full max-w-80 flex-col gap-4" onSubmit={(event) => event.preventDefault()}>
      <Field label="Workspace name" required>
        <Input placeholder="Northwind" autoComplete="organization" />
      </Field>
      <Field label="Workspace URL" helper="You can change it later in settings.">
        <InputGroup start="app.example.com/" placeholder="northwind" />
      </Field>
      <Field label="Your work email" required>
        <Input type="email" placeholder="maya@northwind.example" autoComplete="email" />
      </Field>
      <Button type="submit" variant="primary">Create workspace</Button>
    </form>
  );
}
```

Vue · `create-workspace.vue`

```vue
<script setup lang="ts">
import { Button } from "@/components/ui/button";
import { Field } from "@/components/ui/field";
import { Input } from "@/components/ui/input";
import { InputGroup } from "@/components/ui/input-group";
</script>

<!-- fields stack with a 16px gap; required ones show an asterisk -->
<template>
  <form class="flex w-full max-w-80 flex-col gap-4" @submit.prevent>
    <Field label="Workspace name" required>
      <Input placeholder="Northwind" autocomplete="organization" />
    </Field>
    <Field label="Workspace URL" helper="You can change it later in settings.">
      <InputGroup start="app.example.com/" placeholder="northwind" />
    </Field>
    <Field label="Your work email" required>
      <Input type="email" placeholder="maya@northwind.example" autocomplete="email" />
    </Field>
    <Button type="submit" variant="primary">Create workspace</Button>
  </form>
</template>
```

Svelte · `create-workspace.svelte`

```svelte
<script lang="ts">
  import { Button } from "$lib/components/ui/button";
  import { Field } from "$lib/components/ui/field";
  import { Input } from "$lib/components/ui/input";
  import { InputGroup } from "$lib/components/ui/input-group";
</script>

<!-- fields stack with a 16px gap; required ones show an asterisk -->
<form class="flex w-full max-w-80 flex-col gap-4" onsubmit={(event) => event.preventDefault()}>
  <Field label="Workspace name" required>
    <Input placeholder="Northwind" autocomplete="organization" />
  </Field>
  <Field label="Workspace URL" helper="You can change it later in settings.">
    <InputGroup start="app.example.com/" placeholder="northwind" />
  </Field>
  <Field label="Your work email" required>
    <Input type="email" placeholder="maya@northwind.example" autocomplete="email" />
  </Field>
  <Button type="submit" variant="primary">Create workspace</Button>
</form>
```

## Recipe

`field.recipe.ts`:

```ts
import { tv, type VariantProps } from "../lib/tv";

/* field — label, control, helper text and error text around one control (Ark UI's Field: it wires
 * the label's for, the control's id, aria-describedby, aria-invalid, required and disabled). It was
 * Input's field wrapper (label, hint) made a component: the same column, the
 * same 6px gap, the same 12px ink-2 helper (text read for meaning: 4.5:1 or more) that turns into the red error when invalid.
 *
 * The label part is the Label component (marked data-field="label"); the control is whatever
 * the caller puts in (Input, Textarea, InputGroup, PasswordInput read the field
 * through Ark's context). Helper and error wrap, keep their own direction, never cut. */
export const field = tv({
  slots: {
    base: "flex flex-col gap-1.5 min-w-0",
    /* only as wide as its text: the empty end of the row is not a click target */
    label: "self-start max-w-full",
    requiredIndicator: "ms-0.5 text-red",
    helper: "m-0 text-xs text-ink-2 wrap-anywhere [unicode-bidi:plaintext] [text-align:match-parent]",
    /* an error that arrives after the field's first paint (data-enter on the root, lib/paint.ts) takes
     * its line at once and fades its ink in over duration.base; on first paint and from the
     * keyboard (kbd-instant) it is simply there */
    error: [
      "m-0 text-xs text-red wrap-anywhere [unicode-bidi:plaintext] [text-align:match-parent]",
      "opacity-100 kbd-instant [[data-slot=field][data-enter]>&]:motion-opacity-base starting:[[data-slot=field][data-enter]>&]:opacity-0",
    ],
  },
});

export type FieldRecipeProps = VariantProps<typeof field>;
```
