Skip to main content

What are you looking for?

Try searching for…

Components

Checkbox

Checkbox allows users to select one or more options. It can also be used in situations where the user needs to confirm something.

HTML

Press Enter to start editing
Unable to parse html
const Preview = () => {
  return (
    <Checkbox label='Checkbox label' description='Description' value='value' />
  );
};

render(<Preview />)

Usage

Use the classname ds-input on <input> along with type="checkbox".

For a valid form element, we recommend using <ds-field> together with a <label>.

CSS variables and data attributes

Sizes are controlled with data-size and colors with data-color. The component will inherit these from the nearest parent.

Checkbox is ds-input with type="checkbox", so variables and data attributes that apply to input also apply to checkbox.

Data Attributes
NameValue
data-widthauto
CSS Variables
NameValue
--dsc-input-padding-blockvar(--ds-size-2)
--dsc-input-padding-inlinevar(--ds-size-3)
--dsc-input-paddingvar(--dsc-input-padding-block) var(--dsc-input-padding-inline)
--dsc-input-size--togglevar(--ds-size-6)
--dsc-input-sizevar(--ds-size-12)
--dsc-input-background--readonlyvar(--ds-color-neutral-surface-tinted)
--dsc-input-backgroundvar(--ds-color-neutral-surface-default)
--dsc-input-border-color--readonlyvar(--ds-color-neutral-border-subtle)
--dsc-input-border-colorvar(--ds-color-neutral-border-default)
--dsc-input-border-stylesolid
--dsc-input-border-width--togglemax(var(--ds-border-width-default),calc(var(--ds-size-1)/2))
--dsc-input-border-widthvar(--ds-border-width-default)
--dsc-input-outline-color--hovervar(--ds-color-neutral-border-default)
--dsc-input-outline-color--toggle--hovervar(--dsc-input-accent-color)
--dsc-input-outline-width--hovervar(--ds-border-width-default)
--dsc-input-outline-style--hoversolid
--dsc-input-color--readonlyvar(--ds-color-neutral-text-default)
--dsc-input-colorvar(--ds-color-neutral-text-default)
--dsc-input-stroke-colorvar(--ds-color-base-contrast-default)
--dsc-input-stroke-color--invalidvar(--ds-color-danger-base-contrast-default)
--dsc-input-stroke-width0.05em
--dsc-input-accent-colorvar(--ds-color-base-default)
--dsc-input-accent-color--invalidvar(--ds-color-danger-text-subtle)
--dsc-input-line-heightvar(--ds-line-height-md)

Examples

Grouping

Use fieldset for grouping multiple choices.

Outline

data-variant="outline" adds extra padding and a border around the selection to create a larger clickable area.

ReadOnly

type="checkbox" is always :read-only and does not support this in the same way as input types you can type into.

If you set readonly on checkbox we ensure that it behaves as if it has read-only access by stopping onClick and onChange.

The user will still be able to focus on the element.

React

In React, building a checkbox is simpler, because we have a ready-made component that handles most of the logic and state management for us.

Checkbox is a composite component that uses field, label, validation message, and input to create a checkbox.

We also have a dedicated useCheckboxGroup React hook to make it easier to manage a group of Checkbox components.

disabled
Description

Disables element @note Avoid using if possible for accessibility purposes

Type
boolean
readOnly
Description

Toggle `readOnly`

Type
boolean
data-indeterminate
Description

Indeterminate state for checkbox inputs Only works when used inside `Field` component

Type
boolean
Default
false
aria-label
Description

Optional aria-label

Type
string
label
Description

Checkbox label

Type
ReactNode
description
Description

Description for field

Type
ReactNode
value
Description

Value of the `input` element

Type
string | number | readonly string[]
error
Description

Error message for field

Type
ReactNode
variant
Description

If outline, the checkbox will have a border.

Type
"outline"
aria-labelledby
Type
string
NameTypeDefaultDescription
disabledboolean-

Disables element @note Avoid using if possible for accessibility purposes

readOnlyboolean-

Toggle `readOnly`

data-indeterminatebooleanfalse

Indeterminate state for checkbox inputs Only works when used inside `Field` component

aria-labelstring-

Optional aria-label

labelReactNode-

Checkbox label

descriptionReactNode-

Description for field

valuestring | number | readonly string[]-

Value of the `input` element

errorReactNode-

Error message for field

variant"outline"-

If outline, the checkbox will have a border.

aria-labelledbystring-

-

Indeterminate checkbox

Use useCheckboxGroup to create a parent checkbox that can select or clear all options in the group.

This activates an additional state, indeterminate, alongside checked and unchecked. It is shown as a horizontal line when one or more checkboxes are selected in the group. It is shown as a regular checkbox when all checkboxes in the group share the same state.

This is enabled by setting allowIndeterminate: true in getCheckboxProps.

Edit this page on github.com (opens in a new tab)