Skip to content

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

Components

Free

Label for Svelte UI SDK

A form label for Svelte on UI SDK that names an input, a checkbox or a switch, and focuses or toggles it when its text is clicked.

On this page9 sections

Label is a <label> for one form control. Its text becomes the name a screen reader reads for the control, and clicking it focuses an input or toggles a checkbox. Link it with for set to the control's id, or put the control inside it.

Use it on its own for a checkbox row or a quick one-off field. When the field also needs help text or an error message, build it with Field, whose FieldLabel sits in the same layout as both.

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

Label anatomy: label, control, text

Usage

<script lang="ts">
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
</script>

The label's for must match the input's id, and each id must be unique on the page.

<div class="grid max-w-xs gap-2">
  <Label for="email">Email</Label>
  <Input id="email" type="email" />
</div>

Accessibility

Label renders a native <label>, and its text names the control it points at for a screen reader. The browser links a native form element itself. For anything else with a control's role, such as a radio group or a slider, UI SDK sets the control's aria-labelledby to the label, and clicking the label focuses it, or clicks it when it is a checkbox, a switch or a radio. UI SDK's Checkbox and Switch take the id you pass onto the element a label can point at, so for works with them. A custom control of your own needs one of those roles to be named this way. For one without, set its aria-labelledby to the label's id.

Demos

RTL

The terms checkbox row, in Arabic, with dir set to rtl on the row. The row is a flex container that runs in the reading direction, so the checkbox moves to the right and the label follows it. Clicking the Arabic text toggles the checkbox, through the label's for.

Preview width

FAQ

How do I associate a Label with an input in Svelte?

Give the input an id and set the Label's for to the same value, as the Usage example does. Clicking the label then focuses the input, and a screen reader reads the label's text as the input's name. The id has to be unique on the page, or the label points at the first element that has it.

You can also put the input inside the Label, which links the two without any ids.

How do I label a Checkbox or a Switch with Label?

Give the Checkbox an id and point the Label's for at it, as the preview above does. UI SDK's Checkbox and Switch are <button> elements that take the id themselves, with a hidden checkbox input beside them for forms, so the label names the button and clicking it toggles the control.

Can a Label wrap its control instead of using an id?

Yes. A label that contains its control is linked to it without any ids, for an Input as much as for a Checkbox. Keep one control per label, because the browser links a wrapping label to the first form element inside it.

How do I use a Label for a custom control that isn't an input?

Give the control a role, such as slider or combobox, and point the Label's for at its id. UI SDK's Label names any element with a control's role through aria-labelledby, as it does its own radio groups and sliders, and clicking the label focuses the control, or clicks it when it is a checkbox, a switch or a radio. A control with an aria-label or an aria-labelledby of its own keeps that name.

An element without such a role, such as a color swatch drawn as a div, gets nothing from the label, so give the Label an id and set aria-labelledby on the element yourself.

Why doesn't my Label dim when its control is disabled?

It dims only when the control comes before it in the same parent, has the peer class and carries data-disabled, as UI SDK's Checkbox and Switch do when placed first. A label above an Input comes before the input, so the peer styles can't reach it from there.

What works with every control is a wrapper: give the row's container the group class and data-disabled set to true, and the Label fades and stops taking clicks.

How do I mark a Label as required or optional?

Put required on the control, which is what makes the browser enforce it and a screen reader announce it. Then show it in the label, such as an asterisk in a <span> with aria-hidden, so the star isn't read as part of the name. On a form where most fields are required, mark the few optional ones with the word Optional instead.

Say what the asterisk means once, near the top of the form, for people who don't know the convention.

When should I use Field instead of a Label?

Use Label for a caption and a control with nothing else around them, such as a checkbox row or a search box beside its label. Use Field once the field also has a description or an error message, or sits among other fields in a form.

FieldLabel is a label in that layout, and Field adds the spacing, the disabled and invalid styles, and a place for a FieldError. Mixing the two works, but a form reads more evenly built from one.

API Reference

Label

<label>

PropType
debug?boolean

Logs every state change with its reason to the console, in development builds.

render?Snippet<[{ props, state, children }]>

Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.

Type
Snippet<[{ props, state, children }]>
class?string

Merged with the part's classes.