FormsField
Searchastronomic.dev# Field
Around every form control, for its label, hint and error message.
Static: no JavaScript.
## Usage
```astro
import Field from "@astronomic-dev/ui/components/Field.astro";
```
## Examples
### Label, hint and error
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Enter an email address, like name@example.com.";
---
<div class="grid max-w-md gap-5">
<Field label="Name" for="field-name" required>
<Input id="field-name" name="name" autocomplete="name" required />
</Field>
<Field label="Email" for="field-email" hint="We answer within two business days." error={error} required>
<Input
id="field-email"
name="email"
type="email"
value="marit@northwind"
required
invalid
aria-describedby={describedBy("field-email", { hint: true, error })}
/>
</Field>
</div>
```
### On a band
tone="band" on a surface-band area: the label, hint and marker take the band's text colors, the focus ring is the band's text color (the accent when no preset sets the band), and the error is in the band's text color with a warning icon. Each control keeps its own fill, so the invalid one still shows its red edge. In a frame, so a test can set a band on it.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
import Textarea from "@astronomic-dev/ui/components/Textarea.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Enter an email address, like name@example.com.";
const hint = "A few lines are enough.";
---
<div class="surface-band grid max-w-md gap-5 rounded-card border-ui border-solid p-6">
<Field tone="band" label="Email" for="field-band-email" error={error} required>
<Input
id="field-band-email"
name="email"
type="email"
value="marit@northwind"
required
invalid
aria-describedby={describedBy("field-band-email", { error })}
/>
</Field>
<Field tone="band" label="Name" for="field-band-name" required>
<Input id="field-band-name" name="name" autocomplete="name" required />
</Field>
<Field tone="band" label="What do you need?" for="field-band-brief" hint={hint}>
<Textarea id="field-band-brief" name="brief" rows={3} aria-describedby={describedBy("field-band-brief", { hint })} />
</Field>
</div>
```
## Props
### Field
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` | `string` | None | Required. The visible label. |
| `for` | `string` | None | Required. The id of the control in the slot. Ties the label to it and gives the hint and error their ids. |
| `hint` | `string` | None | Help text under the control. |
| `error` | `string` | None | Error text under the control. Also set `invalid` on the control. |
| `required` | `boolean` | `false` | Shows a required marker. Also set `required` on the control. |
| `tone` | `"page" \| "band" \| "cta"` | `"page"` | The surface the Field sits on. `page`: the page's colors, the error in red. `band`: a `surface-band` area (the band when a preset sets `--ui-band`, else the page), with the band's text colors and focus ring. `cta`: the CTA band, with its text color and focus ring. On `band` and `cta` the error is in the band's text color with a warning icon, since no red reads on every band. The control keeps its own fill. |
| `errorSlot` | `boolean` | `false` | Renders the error node up front, empty and hidden, so Form's script (enhance="fetch") can write an error into it. With error set too, the error shows as usual. Off by default, so a plain Field does not change. |
| `labelHidden` | `boolean` | `false` | Keeps the label for screen readers only. Use it when a heading beside the field already says what it is for. |
| `class` | `string` | None | More classes, merged with cn(), so yours win on conflicts. |
| Slot | Description |
| --- | --- |
| `default` | The control, such as an Input or a Textarea, with the id given in for. |
Any other attribute goes on the outer <div>.
## Accessibility
- The label is a real <label> tied to the control by for, which must match the control's id.
- The hint and the error get ids from for; link them on the control with describedBy() from @astronomic-dev/ui/lib/field.ts.
- An Astro slot cannot pass props to the control, so repeat them on it: id (the same as for), aria-describedby={describedBy(…)}, invalid and required.
- labelHidden keeps the <label> tied to the control and hides it only visually; use it only when a visible heading beside the field says what it is for, and never use a placeholder as the label.
- The required marker (*) is hidden from screen readers, so also set required on the control.
- error only shows the text, so also set invalid on the control.
- On band and cta the error's warning icon is hidden from screen readers; the error text says what is wrong.
- On band and cta the focus ring is the band's text color (on band, the accent when no preset sets the band), so it shows on an accent band, an invalid control's too; its red edge on its own fill and the error's icon and text mark it.
Field
FreeAround every form control, for its label, hint and error message.
- File
- Field.astro
- Group
- Forms
- Kind
- Static: no JavaScript
- Examples
- 2
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
Label, hint and error
We answer within two business days.
Enter an email address, like name@example.com.
Force statemain.astro
examples/field/main.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Enter an email address, like name@example.com.";
---
<div class="grid max-w-md gap-5">
<Field label="Name" for="field-name" required>
<Input id="field-name" name="name" autocomplete="name" required />
</Field>
<Field label="Email" for="field-email" hint="We answer within two business days." error={error} required>
<Input
id="field-email"
name="email"
type="email"
value="marit@northwind"
required
invalid
aria-describedby={describedBy("field-email", { hint: true, error })}
/>
</Field>
</div>On a band
tone="band" on a surface-band area: the label, hint and marker take the band's text colors, the focus ring is the band's text color (the accent when no preset sets the band), and the error is in the band's text color with a warning icon. Each control keeps its own fill, so the invalid one still shows its red edge. In a frame, so a test can set a band on it.
This preview did not load.
band.astro
examples/field/band.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Input from "@astronomic-dev/ui/components/Input.astro";
import Textarea from "@astronomic-dev/ui/components/Textarea.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Enter an email address, like name@example.com.";
const hint = "A few lines are enough.";
---
<div class="surface-band grid max-w-md gap-5 rounded-card border-ui border-solid p-6">
<Field tone="band" label="Email" for="field-band-email" error={error} required>
<Input
id="field-band-email"
name="email"
type="email"
value="marit@northwind"
required
invalid
aria-describedby={describedBy("field-band-email", { error })}
/>
</Field>
<Field tone="band" label="Name" for="field-band-name" required>
<Input id="field-band-name" name="name" autocomplete="name" required />
</Field>
<Field tone="band" label="What do you need?" for="field-band-brief" hint={hint}>
<Textarea id="field-band-brief" name="brief" rows={3} aria-describedby={describedBy("field-band-brief", { hint })} />
</Field>
</div>Minimal · Light