# Form

Put server-side errors back onto the fields they came from. (Custom element.)

Reference: https://timeless.build/docs/components/form/

> **Choosing between components.** Reach for Form only when errors arrive from somewhere the browser cannot see — a server response, an async uniqueness check. Everything a `required`, a `type="email"`, or a `pattern` can decide is already decided by native constraint validation, and adding Form does not change it.

> **Markup you author.** Author an empty `error` part inside each field wrapper. Form resolves it as the single `error` part in the nearest wrapper holding no other named control, which is the shape `.ui-field`, `.ui-choice-group`, and `.ui-fieldset` already produce, then writes the message and points the field’s `aria-describedby` at it. A field with no wrapper gets no error element rather than someone else’s.

## Markup

```html
<ui-form>
  <form class="ui-form-demo-stack" id="workspace-form" action="#workspace" method="post" data-ui-part="form">
    <div class="ui-field">
      <label class="ui-label" for="workspace-slug">Workspace address</label>
      <div data-ui-part="control">
        <input class="ui-input" id="workspace-slug" name="slug" type="text" value="acme" aria-describedby="workspace-slug-description">
        <p class="ui-description" id="workspace-slug-description">Lowercase letters, numbers, and hyphens.</p>
        <p class="ui-error" data-ui-part="error"></p>
      </div>
    </div>
    <div class="ui-field">
      <label class="ui-label" for="workspace-owner">Owner email</label>
      <div data-ui-part="control">
        <input class="ui-input" id="workspace-owner" name="owner" type="email" value="ops@acme.test">
        
        <p class="ui-error" data-ui-part="error"></p>
      </div>
    </div>
    <div class="ui-form-demo-actions">
      <button class="ui-button" data-ui-variant="primary" id="workspace-save" type="button">Save</button>
      <button class="ui-button" data-ui-variant="secondary" id="workspace-clear" type="button">Clear errors</button>
    </div>
  </form>
</ui-form>
```

## Install

```js
import '@timelessui/components/css/tokens.css'
import '@timelessui/components/css/core/form.css'
import '@timelessui/components/css/core/forms.css'
import '@timelessui/components/css/core/button.css'
import '@timelessui/components/css/themes/atmosphere/tokens.css'
import '@timelessui/components/css/themes/atmosphere/forms.css'
import '@timelessui/components/css/themes/atmosphere/button.css'
import '@timelessui/components/register/ui-form'
```

A `register/` import is a side effect: it defines the element as the module evaluates, so it
has to run in code the browser loads. To register explicitly instead — to control the timing,
or to define into another window — call the function from the matching `define/` entry point,
which registers nothing on its own:

```js
import { defineFormElement } from '@timelessui/components/define/ui-form'
defineFormElement()
```

## Consumer wiring

This example needs the following plain JavaScript beyond custom-element registration.

```js
const form = document.querySelector('ui-form')
if (form) {
  document.getElementById('workspace-save')?.addEventListener('click', () => {
    form.setErrors({
      slug: 'That workspace address is already taken.',
      owner: 'No account exists for this address.',
    })
  })
  document.getElementById('workspace-clear')?.addEventListener('click', () => form.clearErrors())
}
```

## Anatomy

Parts are authored in your own markup and identified by the selector below. Required parts
must be present for the component to work. Private `data-ui-internal-*` hooks are written by
the runtime and must never be authored.

**<ui-form>**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `form` | Yes | `form` | The native `<form>`. Submission, `method`, `action`, reset, and constraint validation are all still its job; `ui-form` only writes messages onto the fields inside it. |
| `error` | No | `[data-ui-part~='error']` | The message element for one field, resolved as the single `error` part inside the nearest wrapper that holds no other named control. `.ui-field`, `.ui-choice-group`, and `.ui-fieldset` all produce that shape, so no pairing attribute is needed. `ui-form` writes its text and points the field’s `aria-describedby` at it. |

**ui-field**

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `control` | No | `[data-ui-part~='control']` | Wrapper around the native control when one is needed. |

## Attributes

Every value below is implemented by the stylesheets this component ships. Boolean attributes
are presence-based: author the attribute with no value, or omit it.

**ui-field**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-layout` | `stacked` · `inline` | `stacked` | Whether the label sits above the control or beside it. |
| `data-ui-density` | `compact` · `normal` · `spacious` | `normal` | Gap between label, control, description, and error. |

**ui-input**

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `data-ui-size` | `sm` · `md` · `lg` | `md` | Control height, padding, and font size. |

## Element API

Attributes above are the authoring surface. These are the DOM properties and events the
registered element adds once it upgrades.

### `<ui-form>`

This element adds no public properties. Configure it with its attributes.

| Event | Detail | Description |
| --- | --- | --- |
| `ui-invalid` | `CustomEvent<FormInvalidDetail>` | Dispatched after `setErrors` has put at least one message on a control, naming the fields that matched. Clearing errors dispatches nothing. |

## State

Native attributes, ARIA, and platform pseudo-classes are authoritative. Style state through
the selectors below rather than adding your own state classes.

| State | Source | How to express it |
| --- | --- | --- |
| `invalid` | aria | Set `aria-invalid="true"` on the control and point `aria-describedby` at the error part. |
| `invalid` | native | Native `:invalid`, or `aria-invalid="true"`. |
| `disabled` | native | Native `disabled`. |

## Styling

Root identity: `ui-form`, `ui-field`, `ui-label`, `ui-input`, `ui-description`, `ui-error`.
Required stylesheets: `tokens.css`, `core/form.css`, `core/forms.css`, `core/button.css`, `themes/atmosphere/tokens.css`, `themes/atmosphere/forms.css`, `themes/atmosphere/button.css`.

This component adds no custom properties of its own. Restyle it through the design tokens at https://timeless.build/docs/styling/theming/ or your own CSS.

Design tokens this component reads (39), global and set at the theme level: `--ui-accent`, `--ui-bg-accent`, `--ui-bg-accent-active`, `--ui-bg-accent-hover`, `--ui-bg-control`, `--ui-bg-control-active`, `--ui-bg-control-hover`, `--ui-bg-control-muted`, `--ui-bg-danger`, `--ui-bg-danger-active`, `--ui-bg-danger-hover`, `--ui-bg-surface`, `--ui-bg-surface-subtle`, `--ui-danger`, `--ui-danger-soft`, `--ui-disabled-opacity`, `--ui-duration-fast`, `--ui-ease-standard`, `--ui-fg`, `--ui-fg-muted`, `--ui-fg-subtle`, `--ui-focus`, `--ui-line`, `--ui-line-strong`, `--ui-radius-control`, `--ui-radius-md`, `--ui-radius-pill`, `--ui-radius-sm`, `--ui-shadow-control`, `--ui-shadow-control-accent`, `--ui-shadow-control-active`, `--ui-shadow-control-danger`, `--ui-shadow-inset`, `--ui-shadow-outline-control`, `--ui-space-1`, `--ui-space-2`, `--ui-space-3`, `--ui-space-4`, `--ui-space-5`.

## Accessibility

Keep the native elements, roles, and relationships shown in the markup. Timeless adds only the
state and keyboard coordination the platform does not already provide, and it never supplies
your accessible names — those depend on your content.

The ARIA Authoring Practices Guide has no pattern for Form errors. The contract below is a composition of behavior the platform already defines, not ARIA invented to fill the gap.

There is no APG pattern for server-side error mapping. `ui-form` sets `aria-invalid` on each field it marks and points `aria-describedby` at the authored error text, then moves focus to the first field that took a message — which is what makes the error reachable rather than merely visible. Everything else, including the native validation bubble, stays with the platform.

## Before JavaScript runs

The markup above is complete and usable on its own: native controls submit, labels associate, and authored ARIA is already correct. Registration adds state synchronisation, focus management, and keyboard coordination on top of it.
