DocsBlog

Get Started

Components

Data Grid

Signals

Styling

Theming

↑↓Navigate
↵Select
EscClose
  • 1.8.0

  • Day

    Night

    Preview

    Switch mode
  • cerberus

    acheron

    elysium

    oceanus

Get Started
Components
Data Grid
Signals
Styling
Theming

Concepts

OverviewCompositionCerberus ContextTesting

Layout

Aspect RatioBleedBoxCenterContainerContainer QueryDividerFlexFloatGridGroupLink OverlayScrollableStackWrap

Components

AccordionAdmonitionAvatarButtonCarouselCheckboxClipboardCollapsibleComboboxConfirm ModalCTA ModalDate PickerDialogFieldFieldsetFile UploaderIconButtonInputLoading StatesMarqueeMenuNotificationsNumber InputPaginationPin InputPopoverProgress IndicatorsPrompt ModalRadioRatingSelectSliderSplit ButtonSwitchTableTabsTagTextTextareaToggleTooltip

Utilities

Client OnlyDownload TriggerEnvironmentFeature FlagsFocus TrapForFormat ByteFormat NumberFormat Relative TimeFormat Relative TimeFrameHighlightJSON Tree ViewLocalePortalPresenceShowSwapsplitPropsTheme

Switch

The Switch component is a toggle input that allows users to switch between two states.

  • npm
  • source
  • recipe
  • Ark
View as Markdown
Open this page in Markdown
Anthropic
Open in Claude
Ask questions about this page
OpenAI
Open in ChatGPT
Ask questions about this page

Import

import { Switch, SwitchParts } from '@cerberus/react'

Usage

Loading example...

With Field

To use the Switch within a form, wrap it with the Field component.

Loading example...

Sizes

To change the visual size of the Switch use the size prop.

Loading example...

Customizing

To customize the Switch use style props or the Primitives

Loading example...

Primitives

You can utilize the primitive components or the css prop to customize the checkbox.

ComponentDescription
SwitchRootThe context provider for the switch parts
SwitchLabelThe label of the switch
SwitchControlThe visual input of the switch
SwitchThumbThe thumb of the switch state
SwitchHiddenInputThe native input for the switch

Parts

The SwitchParts API is an Object containing the full family of components.

NameDescription
RootThe SwitchRoot component which is the Provider for the family.
LabelThe SwitchLabel component which displays the label.
ControlThe SwitchControl component which is the visual field.
ThumbThe SwitchThumb component which displays the thumb of the switch.
HiddenInputThe SwitchHiddenInput component which displays the native input.

API

Root

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.
checkedbooleanfalseundefinedThe controlled checked state of the switch
disabledbooleanfalseundefinedWhether the switch is disabled.
idsPartial<{ root: string; hiddenInput: string; control: string; label: string; thumb: string }>falseundefinedThe ids of the elements in the switch. Useful for composition.
invalidbooleanfalseundefinedIf true, the switch is marked as invalid.
labelstringfalseundefinedSpecifies the localized strings that identifies the accessibility elements and their states
namestringfalseundefinedThe name of the input field in a switch
(Useful for form submission).
onCheckedChange(details: CheckedChangeDetails) => voidfalseundefinedFunction to call when the switch is clicked.
readOnlybooleanfalseundefinedWhether the switch is read-only
requiredbooleanfalseundefinedIf true, the switch input is marked as required,
valuestring | numberfalse"on"The value of switch input. Useful for form submission.

Data Attributes

AttributeDescription / Value
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

Control

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.

Data Attributes

AttributeDescription / Value
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

HiddenInput

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.

Label

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.

Data Attributes

AttributeDescription / Value
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

RootProvider

Props

PropTypeRequiredDefaultDescription
valueUseSwitchReturntrueundefinedundefined
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.

Thumb

Props

PropTypeRequiredDefaultDescription
asChildbooleanfalseundefinedUse the provided child element as the default rendered element, combining their props and behavior.

Data Attributes

AttributeDescription / Value
data-activePresent when active or pressed
data-focusPresent when focused
data-focus-visiblePresent when focused with keyboard
data-readonlyPresent when read-only
data-hoverPresent when hovered
data-disabledPresent when disabled
data-state"checked" | "unchecked"
data-invalidPresent when invalid
data-requiredPresent when required

Context

API:

PropertyTypeDescription
checkedbooleanWhether the switch is checked
disabledboolean | undefinedWhether the switch is disabled
focusedboolean | undefinedWhether the switch is focused
setChecked(checked: boolean) => voidSets the checked state of the switch.
toggleCheckedVoidFunctionToggles the checked state of the switch.

Accessibility

Complies with the Switch WAI-ARIA design pattern.

Keyboard Support

KeyDescription
Space + EnterToggle the switch

On this page

  • Import
  • Usage
  • With Field
  • Sizes
  • Customizing
  • Primitives
    • Parts
  • API
    • Root
      • Props
      • Data Attributes
    • Control
      • Props
      • Data Attributes
    • HiddenInput
      • Props
    • Label
      • Props
      • Data Attributes
    • RootProvider
      • Props
    • Thumb
      • Props
      • Data Attributes
    • Context
  • Accessibility
    • Keyboard Support
Edit this page on Github