Skip to main content

What are you looking for?

Try searching for…

Components

Switch

Switch allows users to choose between two options by turning something on or off.

HTML

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

render(<Preview />)

Usage

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

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 from the closest parent where these are set.

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

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)
Data Attributes
NameValue
data-widthauto

Right-aligned

To right-align the switch, add data-position="end" to <ds-field>.

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 a switch, 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 switch is easier because we have a ready-made component that handles most of the logic and state management for us.

Switch is a composite component that uses field, label, and input to create a switch.

aria-label
Description

Optional aria-label

Type
string
label
Description

Switch label

Type
ReactNode
description
Description

Description for field

Type
ReactNode
value
Description

Value of the `input` element

Type
string | number | readonly string[]
position
Description

Position of switch

Type
"start" | "end"
Default
start
variant
Description

If outline, the switch will have a border.

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

Optional aria-label

labelReactNode-

Switch label

descriptionReactNode-

Description for field

valuestring | number | readonly string[]-

Value of the `input` element

position"start" | "end"start

Position of switch

variant"outline"-

If outline, the switch will have a border.

aria-labelledbystring-

-

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