Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Field for Svelte

Form field layout for Svelte on Ark UI: a label, the control, a hint and an error message, stacked or side by side, plus fieldsets and groups.

On this page9 sections
UI library: Ark UI

Field lays out one form control with its label, a line of help and an error message, and its other parts arrange many fields into a form: FieldGroup spaces them, FieldSet gathers the ones that answer one question. It works around any control, from an input to a slider. It doesn't validate or hold values, so pair it with your form library.

For one caption beside one control with nothing else around them, a Label is enough.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Field anatomy: root, label, input, description, error

Usage

<script lang="ts">
import {
  Field, FieldDescription, FieldLabel,
} from "@/components/ui/field";
import { Input } from "@/components/ui/input";
</script>

Field links the label to the input by itself, so the label's for and the input's matching id below are optional on Ark UI.

<Field class="max-w-xs">
  <FieldLabel for="name">Name</FieldLabel>
  <Input id="name" placeholder="Evil Rabbit" type="text" />
  <FieldDescription>This is your public display name.</FieldDescription>
</Field>

Accessibility

Field is a div with role="group" and adds no keys of its own, so the control inside keeps its usual keyboard. FieldSet renders a <fieldset>, and a screen reader announces its FieldLegend as people move into the group. Field links FieldLabel to the control and names FieldDescription, FieldHelper and FieldError in its aria-describedby, as the description answer details. Its invalid, disabled and required reach the Ark UI control inside, and FieldError is a polite live region.

Demos

Input

A password field with its rule, at least 8 characters, between the label and the box. Put FieldDescription before the control when people need the constraint before they type, and after it, as the preview at the top of the page does, for a note they can skip. The type is password, so the characters stay masked.

Preview width

Invalid

An email field holding jordan@example, marked invalid with invalid on Field, which turns the label and the box's border to the destructive color and passes aria-invalid to the input. The FieldError under it says what to fix. Set invalid from your form library's error for the field, and keep the message to the one change people need to make.

Preview width

Textarea

Feedback, as a label over a Textarea with no description, so the placeholder is the only prompt. That's enough for a free-form box whose label says it all. Anything people still need once they start typing, such as a length limit, belongs in a FieldDescription, because the placeholder disappears.

Preview width

Select

Department, picked from Engineering, Design or Marketing in a Select that shows a placeholder until something is chosen. Field links the label to the select. Use a select for a short closed list, and a text input for an answer people make up.

Preview width

Slider

Volume at 40, with the field's label above a Slider. The Slider also carries an aria-label of Volume, which is the name its thumb announces when it takes focus. Keep that name the same as the visible label, so what a screen reader says matches what people see.

Preview width

Fieldset

Street and city under one Address legend. FieldSet renders a <fieldset> and FieldLegend its <legend>, so a screen reader says Address as people move into the group, and each input keeps its own label. Use a fieldset when several inputs answer one question, such as an address or a date split into parts.

Preview width

Checkbox

Same as shipping address, checked from the start, with the box and its label in one row through orientation set to horizontal. font-normal keeps the label at body weight, since it reads as the choice rather than a heading. Field links the label to the checkbox, so clicking the text toggles it.

Preview width

Radio

A Subscription plan question with Monthly and Yearly, one item to each horizontal Field, in a FieldSet whose FieldLegend has variant set to label for smaller text. Each item's text sits inside the item and is its own label, and the radio group takes its name from the legend. Use radios when exactly one answer is allowed.

Preview width

Switch

Multi-factor authentication, with the label first and the switch after it in one row, the usual shape for a settings list. Field links the label to the switch. When turning it on has a consequence, put a FieldDescription under the label, with both inside a FieldContent.

Preview width

Choice card

Monthly and Yearly as two bordered cards, where a click anywhere on a card selects its radio. Each FieldLabel wraps a horizontal Field, which the label's styles turn into a card with a border and a tint when checked. FieldTitle gives each card a title styled like a label, since the card is already the label. Keep it to a few options that need room for a price.

Preview width

Group

Email and Name in one FieldGroup, with a FieldSeparator line between them. FieldGroup gives every field in it the same gap, and the separator marks a break between parts of a form that don't share a legend. Pass the separator a short word as its child, such as or, and it prints it in the middle of the line.

Preview width

RTL

The public name field in Arabic, inside a wrapper with dir set to rtl. The label, the box and the description start from the right edge, since the field's alignment is logical, with text-start on the description. Check a translated form this way before it ships, because a physical margin you added yourself lands on the wrong side.

Preview width

Responsive

Name, with its label and a description grouped in FieldContent, then the input. orientation set to responsive stacks the two, then puts them side by side once the FieldGroup around the field is 28rem wide, through a container query on the group rather than the viewport. The demo's group is capped at max-w-lg, 32rem, so it shows a row on a wide screen and stacks on a phone.

Preview width

FAQ

How do I label a Field in Svelte?

Put a FieldLabel and the control inside Field, and Field links them: it gives the control an id, points the label at it, and does the same for an Ark UI Checkbox, Switch or Select. A component that doesn't read the field, such as the Slider, needs its own aria-label.

FieldTitle looks like a label without being one. Use it for a heading inside a choice card, where the card itself is the label.

How do I put a Field label beside the control?

Set orientation on Field. vertical, the default, stacks the label over the control, horizontal puts them in a row, and responsive stacks them until the FieldGroup around the field is 28rem wide, then uses a row. responsive only switches inside a FieldGroup, because the breakpoint is a container query on that group.

In a row, wrap the label and its description in FieldContent so they stay together beside the control. Set reverse to swap the label and the control on screen, which leaves the reading order of the markup as it is.

How do I show an error message on a Field?

Set invalid on Field and put the message in a FieldError, as the Invalid demo does. FieldError renders only while the field is invalid, so it can stay in your markup and invalid alone decides when it shows. Field also adds aria-invalid to the control and names the error in its aria-describedby, so a screen reader reads it with the control.

How do I use Field with a form library in Svelte?

Field holds no form state. Wire the library to the control as usual, set invalid on Field from the library's error for that field, and show the message in FieldError. Keep a FieldDescription for the hint and FieldError for the failure, since people need both while they fix it.

Pass the error text as the children of FieldError, which shows it only while invalid is set.

How do I disable a whole Field or a group of fields?

Set disabled on Field, and it reaches the control inside and fades the label. On FieldSet, disabled reaches every field in the set and disables the native controls in it as well, since the set is a real <fieldset>.

A disabled control's value is left out when the form submits. Make a value people can't change read-only instead when it still has to be sent.

When should I use FieldSet, FieldGroup or FieldSeparator?

Use FieldSet with a FieldLegend when several controls answer one question, such as an address or a choice between plans, because the legend names the whole group for a screen reader. Use FieldGroup to stack any fields with an even gap, with or without a legend.

FieldSeparator draws a line between two parts of a FieldGroup, as the Group demo does. Give it a short word as its child to print in the middle of the line. A FieldGroup inside another one takes a tighter gap.

How do I make a choice card with Field?

Wrap a horizontal Field in a FieldLabel, put a FieldTitle and the radio or checkbox inside, and repeat it for each option, as the Choice card demo does. The label's styles draw the border and tint the card when its control is checked, and a click anywhere on the card selects it. Inside a FieldLabel, Field drops its group role, so the card's text names the radio for a screen reader.

Use FieldTitle rather than another FieldLabel for the card's title, since the card is already the label and a second one would sit inside it. Put a FieldDescription under the title inside a FieldContent for a price or a line of detail.

Does a screen reader read a FieldDescription with its control?

Yes, for an Ark UI control that reads its field. Field gives FieldDescription the field's helper-text id, and an Input, Textarea, Checkbox, Switch, Number Input or Combobox inside names it in its aria-describedby, so the hint is read when the control takes focus. Keep one FieldDescription or FieldHelper per field, since both take that one id.

Ark UI's Slider and Radio Group don't read the field, so their thumbs and items aren't described. While the field is invalid, Field names FieldError in the control's aria-describedby too, so the error is read along with the hint.

API Reference

Field

<div>

PropType
disabled?boolean

Indicates whether the field is disabled.

id?string

The id of the field.

ids?object

The ids of the field parts.

Type
{ root?: string; control?: string; label?: string; errorText?: string; helperText?: string }
invalid?boolean

Indicates whether the field is invalid.

orientation?"horizontal" | "responsive" | "vertical"

Stacks the label over the control (vertical), puts them side by side (horizontal), or side by side on wide containers only (responsive).

Type
"horizontal" | "responsive" | "vertical"
Default
"vertical"
readOnly?boolean

Indicates whether the field is read-only.

required?boolean

Indicates whether the field is required.

reverse?boolean

Puts the control before its label, when the field is horizontal.

Default
false
target?string

The target field item value the label should point to.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldSet

<div> data-slot="field-set"

PropType
disabled?boolean

Indicates whether the field is disabled.

id?string

The id of the field.

ids?object

The ids of the field parts.

Type
{ root?: string; control?: string; label?: string; errorText?: string; helperText?: string }
invalid?boolean

Indicates whether the field is invalid.

readOnly?boolean

Indicates whether the field is read-only.

required?boolean

Indicates whether the field is required.

target?string

The target field item value the label should point to.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldLegend

data-slot="field-legend"

PropType
variant?"legend" | "label"

The variant of the legend.

Type
"legend" | "label"
Default
"legend"
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-variant?string

Mirrors variant.

FieldGroup

<div> data-slot="field-group"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldContent

<div> data-slot="field-content"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldLabel

<label> data-slot="field-label"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldRequiredIndicator

<span> data-slot="field-required-indicator"

PropType
fallback?Snippet

What it shows when the field isn't required.

asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldTitle

<div> data-slot="field-title"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldDescription

<p> data-slot="field-description"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldSeparator

<div> data-slot="field-separator"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

AttributeType
data-content?string

FieldHelper

<span> data-slot="field-helper"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

FieldError

<span> data-slot="field-error"

PropType
asChild?Snippet<[PropsFn]>

Render your own element: a snippet that gets the part's props to spread on it.

Type
Snippet<[PropsFn]>
class?string

Merged with the part's classes.

useField

PropType
disabled?boolean

Indicates whether the field is disabled.

id?string

The id of the field.

ids?object

The ids of the field parts.

Type
{ root?: string; control?: string; label?: string; errorText?: string; helperText?: string }
invalid?boolean

Indicates whether the field is invalid.

readOnly?boolean

Indicates whether the field is read-only.

required?boolean

Indicates whether the field is required.

target?string

The target field item value the label should point to.