• Docs
  • Blog
    • 0.25.1

    • light

      dark

      system

      Switch mode
    • Cerberus

      Acheron

      Elysium

    Get Started
    Components
    Styling
    Theming

    Concepts

    OverviewCompositionTesting

    Layout

    Aspect RatioBleedBoxCenterContainerDividerFlexFloatGridLink OverlayScrollableStackWrap

    Components

    AccordionAdmonitionAvatarButtonCarouselCheckboxClipboardCollapsibleComboboxConfirm ModalCTAModalDate PickerDialogFieldFieldsetFile UploaderIcon ButtonInputLoading StatesMenuNotificationsNumber InputPin InputProgressPrompt ModalRadioRatingSelectSplit ButtonSwitchTableTabsTagTextTextareaToggleTooltip

    Utilities

    Client OnlyDownload TriggerEnvironmentFeature FlagsFocus TrapForFormat ByteFormat NumberFormat Relative TimeFrameHighlightJSON Tree ViewLocaleLocal StoragePortalPresenceShowsplitPropsTheme

    Switch

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

    • npm
    • source
    • recipe
    • Ark
    import { Switch, SwitchParts } from '@cerberus/react'

    Usage

    Copy
    valid-radio.tsx
    import { Switch } from '@cerberus/react'
    import { Box } from 'styled-system/jsx'
    function BasicDemo() {
    return (
    <Box w="1/2">
    <Switch>Receive in app notifications</Switch>
    </Box>
    )
    }

    With Field

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

    Copy
    valid-radio.tsx
    import { Switch, Field } from '@cerberus/react'
    import { Box } from 'styled-system/jsx'
    function WithFieldDemo() {
    return (
    <Box w="1/2">
    <Field label="This is the field label" required>
    <Switch>Receive in app notifications</Switch>
    </Field>
    </Box>
    )
    }

    Sizes

    Copy
    valid-radio.tsx
    import { Switch } from '@cerberus/react'
    import { Box, VStack } from 'styled-system/jsx'
    function SizesDemo() {
    return (
    <Box w="1/2">
    <VStack alignItems="flex-start" gap="lg">
    <Switch size="sm">Small size (default)</Switch>
    <Switch size="lg">Large size</Switch>
    </VStack>
    </Box>
    )
    }

    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
    Copy
    custom checkbox
    import { SwitchParts } from '@cerberus/react'
    import { Box } from 'styled-system/jsx'
    function CustomDemo() {
    return (
    <Box w="1/2">
    <SwitchParts.Root>
    <SwitchParts.Label
    css={{
    textTransform: 'uppercase',
    }}
    >
    Receive in app notifications
    </SwitchParts.Label>
    <SwitchParts.Control
    css={{
    rounded: 'sm',
    transform: 'skewX(-10deg)',
    _checked: {
    bgColor: 'danger.surface.initial',
    },
    }}
    >
    <SwitchParts.Thumb
    css={{
    rounded: 'sm',
    }}
    />
    </SwitchParts.Control>
    <SwitchParts.HiddenInput />
    </SwitchParts.Root>
    </Box>
    )
    }

    API

    Props

    The Switch component is an abstraction of the primitives and accepts the following props:

    NameDefaultDescription
    sizesmThis size of the Switch.

    The Switch component also accepts all the props of the SwitchRoot primitive props

    Parts

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

    Note

    It is best to only use the SwitchParts if you are building a custom solution. Importing Object based components will ship every property it includes into your bundle, regardless if you use it or not.

    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.

    On this page

    • Edit this page on Github
    Cerberus Design System | Docs