Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / Controls

Switch

An on/off setting that takes effect straight away, such as "Auto-expand". It is built on a native checkbox with role="switch". If the choice is confirmed later with a submit button, use Checkbox.

Toggles as aswitch

When to useLink to "When to use"

  • A preference or feature that is on or off and applies immediately.
  • Settings lists with a short description under each label.

Use something else when:

UsageLink to "Usage"

tsx
Copy
import { Switch } from "@arshad-shah/cynosure";

<Switch label="Auto-expand" checked={auto} onChange={setAuto} />
<Switch label="Notifications" description="Email me about new comments" defaultChecked />
<Switch label="Beta features" tone="warning" disabled />

onChange receives the new boolean. Use checked for controlled state or defaultChecked for uncontrolled.

OptionsLink to "Options"

  • description: supporting text under the label.
  • size: sm, md or lg.
  • tone: colours the track when on: accent (default), neutral, danger, success, warning or info.
  • name and value: native, so the switch submits with a form.

A Switch has no strikeWhenChecked: that checklist style belongs to Checkbox. A switch that is on keeps a plain label.

PropsLink to "Props"

Every prop of Switch
PropWhat it doesTypeDefault
labelRequired
The control's label. Pressing it toggles the control.ReactNode
description
Help text shown under the label and read with the control.ReactNode
checked
Controlled: whether it is on.boolean
defaultChecked
On at first, when uncontrolled.boolean
onChange
Called when the person turns it on or off, with the new state.((checked: boolean) => void)
name
Name it is submitted under in a form, when on.string
value
The value submitted with name when on. The browser sends "on" when this is not set.string
required
It must be on for the form to submit.boolean
disabled
It cannot be changed and is not submitted with a form.boolean
size
Size of the control and its text. Default "md".Size"md"
tone
Fill colour when on. Default "accent".Tone"accent"
id
Id of the native input; one is generated when not set.string
className
Added to the root element (the label that wraps the row), for layout.string
style
Layout only, applied to the root element.CSSProperties

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

Behaviour and accessibility, table 1
InputResult
Click, tap, SpaceToggles
TabFocus with a visible ring
  • Native checkbox: forms, reset, required and keyboard behaviour match Checkbox, without the mixed state.
  • Role: it is announced as a switch with its checked state.
  • Motion: the thumb slides, and stops animating under reduced motion.

ThemingLink to "Theming"

Tokens: --cy-switch-{width,height,thumb-size,off-bg}, and --cy-switch-height-sm and --cy-switch-height-lg for the other sizes. The row around the switch is a Choice: --cy-choice-gap is the space before the label.

ts
Copy
createTheme({ components: { switch: { width: "44px", offBg: "#cbd5e1" } } });

Checkbox · Choice · RadioGroup · SegmentedControl

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog