mirror of
https://github.com/huntabyte/shadcn-svelte.git
synced 2026-09-14 19:33:46 +08:00
5.2 KiB
5.2 KiB
title, description, component, links
| title | description | component | links | ||
|---|---|---|---|---|---|
| Field | Combine labels, controls, and help text to compose accessible form fields and grouped inputs. | true |
|
Installation
{#snippet cli()} {/snippet} {#snippet manual()}Copy and paste the following code into your project.
{#if viewerData} {/if} {/snippet}Usage
<script lang="ts">
import * as Field from "$lib/components/ui/field/index.js";
</script>
<Field.Set>
<Field.Legend>Profile</Field.Legend>
<Field.Description>This appears on invoices and emails.</Field.Description>
<Field.Group>
<Field.Field>
<Field.Label for="name">Full name</Field.Label>
<Input id="name" autocomplete="off" placeholder="Evil Rabbit" />
<Field.Description
>This appears on invoices and emails.</Field.Description
>
</Field.Field>
<Field.Field>
<Field.Label for="username">Username</Field.Label>
<Input id="username" autocomplete="off" aria-invalid />
<Field.Error>Choose another username.</Field.Error>
</Field.Field>
<Field.Field orientation="horizontal">
<Switch id="newsletter" />
<Field.Label for="newsletter">Subscribe to the newsletter</Field.Label>
</Field.Field>
</Field.Group>
</Field.Set>
Anatomy
The Field family is designed for composing accessible forms. A typical field is structured as follows:
<Field.Field>
<Field.Label for="input-id">Label</Field.Label>
<!-- Input, Select, Switch, etc. -->
<Field.Description>Optional helper text.</Field.Description>
<Field.Error>Validation message.</Field.Error>
</Field.Field>
Field.Fieldis the core wrapper for a single field.Field.Contentis a flex column that groups label and description. Not required if you have no description.- Wrap related fields with
Field.Group, and useField.SetwithField.Legendfor semantic grouping.
Examples
Input
Textarea
Select
Slider
Fieldset
Checkbox
Radio
Switch
Choice Card
Wrap Field components inside FieldLabel to create selectable field groups. This works with RadioItem, Checkbox and Switch components.
Field Group
Stack Field components with Field.Group. Add Field.Separator to divide them.
Responsive Layout
- Vertical fields: Default orientation stacks label, control, and helper text—ideal for mobile-first layouts.
- Horizontal fields: Set
orientation="horizontal"onFieldto align the label and control side-by-side. Pair withField.Contentto keep descriptions aligned. - Responsive fields: Set
orientation="responsive"for automatic column layouts inside container-aware parents. Apply@container/field-groupclasses onField.Groupto switch orientations at specific breakpoints.
Validation and Errors
- Add
data-invalidtoFieldto switch the entire block into an error state. - Add
aria-invalidon the input itself for assistive technologies. - Render
FieldErrorimmediately after the control or insideFieldContentto keep error messages aligned with the field. Copy
<Field.Field data-invalid>
<Field.Label for="email">Email</Field.Label>
<Input id="email" type="email" aria-invalid />
<Field.Error>Enter a valid email address.</Field.Error>
</Field.Field>
Accessibility
Field.SetandField.Legendkeep related controls grouped for keyboard and assistive tech users.Fieldoutputsrole="group"so nested controls inherit labeling fromField.LabelandField.Legendwhen combined.- Apply
Field.Separatorsparingly to ensure screen readers encounter clear section boundaries.