Skip to content

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

Components

Free

Checkbox for Svelte

A checkbox for Svelte on Ark UI: one yes or no per box, for terms, filters and table rows, with a mixed state for a partly selected group.

On this page9 sections
UI library: Ark UI

A checkbox is a small box beside a label that someone ticks to say yes to one thing: accepting the terms, picking a filter, selecting a row in a table. Boxes in a list are independent of each other, and each ticked one goes in with the form when it's submitted.

When the change should take effect the moment it's made, such as turning on notifications, use a Switch. When only one option of a few can be picked, use a Radio Group.

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

Checkbox anatomy: root, control, indicator, label

Usage

<script lang="ts">
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
</script>
<FieldGroup class="w-fit whitespace-nowrap">
  <Field orientation="horizontal">
    <Checkbox name="terms" />
    <FieldLabel>Accept terms</FieldLabel>
  </Field>
</FieldGroup>

Inside one Field, FieldLabel is wired to the checkbox for you: clicking the words ticks the box, and a screen reader reads them as its name.

Accessibility

The box is a native <input type="checkbox">, visually hidden inside the <label> that Checkbox renders, so it sits in the tab order and the browser reports it as checked, not checked or mixed. Inside a Field, it takes its name from FieldLabel. Anywhere else, such as a table cell, it has no words of its own: point the label entry of ids at the id of the text that names it.

Space
Toggle the checkbox.

Demos

Invalid

An Accept terms box in its error state, the way it should look after someone submits without ticking a box they had to tick. The demo sets invalid on Field, which marks the box's input invalid and turns the label red, and aria-invalid on Checkbox for the red border. Show it only after a submit, with a FieldError that says what's missing.

Preview width

Basic

An Accept terms box with a label and a name, the smallest checkbox worth shipping. In a form, a ticked box sends its name with the value on, and an unticked one sends nothing. Start here for a single agreement, and add a description only when the label can't carry the consequence on its own.

Preview width

Description

Accept terms starts ticked, with a second, quieter line under the label. FieldContent stacks the label over that description, and the field then aligns the box with the first line of text instead of centering it on the block. Use it when the label alone would hide a consequence, such as a charge or data being shared, and keep the second line to that consequence.

Preview width

Disabled

A Notifications box that can't be changed. Checkbox gets disabled and Field gets data-disabled, so the label fades along with the box and clicks do nothing. Use it for a preference the current plan or role doesn't allow, and say why next to it. Leave the row out when the person will never be able to change it.

Preview width

Group

Three kinds of item to show on the desktop, under a legend that reads Desktop: hard disks and external disks start ticked, servers doesn't. Each box has its own name, so the form sends one entry per ticked box. Put boxes that answer one question in a FieldSet with a FieldLegend, which renders a real fieldset and legend, so a screen reader names the group as focus enters it.

Preview width

Table

A table of three people with a box in each row and one in the header, with Sarah Chen selected to start. The header box is ticked when every row is and mixed while only some are, and a click on it selects every row or clears them all. The selected ids live in a Set in state, which the boxes and the row highlight both read.

Preview width

RTL

The Accept terms box in Arabic, with dir set to rtl on the FieldGroup around it. The box moves to the right edge and the label follows it to the left, with no change to the markup. Set the direction on the nearest wrapper like this when the block sits inside a page that otherwise reads left to right.

Preview width

FAQ

When should I use a checkbox instead of a switch or radio buttons?

Use a checkbox when the choice takes effect later, usually when a form is submitted, and when several options in a list can be picked at once: accepting terms, choosing filters, selecting rows for a bulk action.

A Switch reads as an on and off control that acts immediately, like a setting saved as soon as it changes. A Radio Group is for exactly one choice out of a few, all of them visible. A single box that turns a setting on or off right away is a switch in disguise.

How do I control a Checkbox's checked state in Svelte?

Pass checked and update it in onCheckedChange, which receives an object: the new state is details.checked, which is true, false or "indeterminate". Use defaultChecked instead when you only need a starting value and the box can keep its own state.

Hold the state yourself when something else reads it, such as a submit button that stays disabled until the terms box is ticked.

How do I make a select-all Checkbox with an indeterminate state?

Set the header box's checked to "indeterminate". It draws a dash, the browser reports it as mixed, and a click on it asks for true.

Work the state out from the rows on every render. Keep the selected ids in a Set: the header is checked when the set holds every row, mixed when it holds some, and on change it fills the set or empties it, as the Table demo does.

How do I add a label to a Checkbox in Ark UI?

Put Checkbox and a FieldLabel in one Field, as the Basic demo does. The field gives the box's input the label's id, so clicking the words ticks the box and a screen reader reads them as its name, with no ids to write. Outside a Field, an id on Checkbox is also its input's id, so a label that points at that id works too.

Where no visible label fits, point the label entry of ids at the id of the text that names the box, as the Table demo does with each row's name.

A visible label also makes the target bigger. The box has an invisible hit area a little wider than itself, but on a phone the words are what people aim for.

How do I show a Checkbox error in a form?

Set invalid on the Field around the box, or on Checkbox itself: the box's input then carries aria-invalid for screen readers, and a field marked this way turns its label red. The red border on the box comes from aria-invalid on Checkbox, so set that as well, as the Invalid demo does.

Put the message in a FieldError inside the same field, so it sits under the label, and show it after a submit rather than while someone is still filling the form in.

What value does a Checkbox send when the form is submitted?

A ticked box sends its name with the value on, or with its value when you set one, and an unticked box sends nothing, the way a native checkbox does. On the server, read a missing key as false.

With a form library, bind the library's field to the box's checked state and update the field from the change callback.

How do I get the checked values of a group of checkboxes?

Wrap the boxes in CheckboxGroup and give each Checkbox a value. The group holds an array of the checked values, which you read or control with value, defaultValue and onValueChange, and a name on the group goes to every box. Set maxSelectedValues to disable the unchecked boxes once that many are picked.

On submit, a FormData built from the form returns every ticked box that shares one name through getAll.

How do I change the Checkbox size or colors?

Edit components/ui/checkbox, which is your file once installed. The box's size, border and checked color are utility classes on the root, and the check icon's size is a class on the indicator, so change those to restyle every checkbox at once.

To change one box, add a class to Checkbox. It is merged over the defaults, so a size or a background color you add there wins over the one in the file.

API Reference

Checkbox

<label> data-slot="checkbox"

PropType
checked?boolean | "indeterminate"

The controlled checked state of the checkbox.

Type
boolean | "indeterminate"
defaultChecked?boolean | "indeterminate"

The initial checked state of the checkbox when rendered. Use when you don't need to control the checked state of the checkbox.

Type
boolean | "indeterminate"
onCheckedChange?(details: CheckedChangeDetails) → void

The callback invoked when the checked state changes.

Type
(details: CheckedChangeDetails) → void
Parameters

details – { checked: CheckedState }

Returns
void
dir?"ltr" | "rtl"

The document's text/writing direction.

Type
"ltr" | "rtl"
Default
"ltr"
disabled?boolean

Whether the checkbox is disabled.

form?string

The id of the form that the checkbox belongs to.

id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the checkbox. Useful for composition.

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

Whether the checkbox is invalid.

name?string

The name of the input field in a checkbox. Useful for form submission.

readOnly?boolean

Whether the checkbox is read-only.

required?boolean

Whether the checkbox is required.

value?string

The value of checkbox input. Useful for form submission.

Default
"on"
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-active?string

Present when active or pressed.

data-disabled?string

Present when disabled.

data-focus?string

Present when focused.

data-focus-visible?string

Present when focused with keyboard.

data-hover?string

Present when hovered.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

data-required?string

Present when required.

data-state"indeterminate" | "checked" | "unchecked"

"indeterminate", "checked" or "unchecked".

Type
"indeterminate" | "checked" | "unchecked"

CheckboxIndicator

<div> data-slot="checkbox-indicator"

PropType
indeterminate?boolean

Shows only while the checkbox is indeterminate, for the indeterminate icon.

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-active?string

Present when active or pressed.

data-disabled?string

Present when disabled.

data-focus?string

Present when focused.

data-focus-visible?string

Present when focused with keyboard.

data-hover?string

Present when hovered.

data-invalid?string

Present when invalid.

data-readonly?string

Present when read-only.

data-required?string

Present when required.

data-state"indeterminate" | "checked" | "unchecked"

"indeterminate", "checked" or "unchecked".

Type
"indeterminate" | "checked" | "unchecked"

CheckboxGroup

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

PropType
value?string[]

The controlled value of the checkbox group.

defaultValue?string[]

The initial value of value when uncontrolled.

onValueChange?(value: string[]) → void

The callback to call when the value changes.

Parameters

value – string[]

Returns
void
disabled?boolean

If true, the checkbox group is disabled.

invalid?boolean

If true, the checkbox group is invalid.

maxSelectedValues?number

The maximum number of selected values.

name?string

The name of the input fields in the checkbox group (Useful for form submission).

readOnly?boolean

If true, the checkbox group is read-only.

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.

CheckboxGroupProvider

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

PropType
valueUseCheckboxGroupContext

The group provider's value.

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.

CheckboxRootProvider

<label> data-slot="checkbox"

PropType
valueUseCheckboxReturn

What useCheckbox() returns: the state and API, made outside the component to control it.

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.

useCheckbox

PropType
checked?boolean | "indeterminate"

The controlled checked state of the checkbox.

Type
boolean | "indeterminate"
defaultChecked?boolean | "indeterminate"

The initial checked state of the checkbox when rendered. Use when you don't need to control the checked state of the checkbox.

Type
boolean | "indeterminate"
onCheckedChange?(details: CheckedChangeDetails) → void

The callback invoked when the checked state changes.

Type
(details: CheckedChangeDetails) → void
Parameters

details – { checked: CheckedState }

Returns
void
disabled?boolean

Whether the checkbox is disabled.

form?string

The id of the form that the checkbox belongs to.

id?string

The unique identifier of the machine.

ids?object

The ids of the elements in the checkbox. Useful for composition.

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

Whether the checkbox is invalid.

name?string

The name of the input field in a checkbox. Useful for form submission.

readOnly?boolean

Whether the checkbox is read-only.

required?boolean

Whether the checkbox is required.

value?string

The value of checkbox input. Useful for form submission.

Default
"on"

CheckboxContext

PropType
checkedboolean

Whether the checkbox is checked.

checkedStateboolean | "indeterminate"

The checked state of the checkbox.

Type
boolean | "indeterminate"
indeterminateboolean

Whether the checkbox is indeterminate.

setChecked(checked: CheckedState) → void

Function to set the checked state of the checkbox.

Parameters

checked – CheckedState

Returns
void
toggleCheckedVoidFunction

Function to toggle the checked state of the checkbox.

disabled?boolean

Whether the checkbox is disabled.

focused?boolean

Whether the checkbox is focused.