Skip to content

    Switch

    Turns a setting on or off at once, with no Save. A track with a thumb that slides to the end when on: a light edge on the soft gray fill when off, the accent when on. label puts its name after it, and description a second line under the name.

    React

    Preview

    Get an email when someone mentions you.

    Props

    • size
    • checked
    • defaultChecked
    • disabled
    • label
    • description
    import { Switch } from "@/components/ui/switch"
    
    <Switch label="Email notifications" description="Get an email when someone mentions you." />

    Usage

    When to use

    • Turn a setting on or off where the change applies at once, such as email notifications.
    • A row of settings on a settings page, each with its own switch.

    When not to use

    • A choice that waits for Save, or accepting terms. Use Checkbox.
    • Picking one of several options. Use RadioGroup.

    Platform support

    What each platform has, from the spec. A selected platform is highlighted.

    NameWebiOSAndroidNote
    sizeYesYesYes
    labelYesYesYes
    descriptionYesYesYes
    requiredTextYesYesYes
    checkedYesYesYes
    defaultCheckedYesYesYes
    onCheckedChangeYesYesYes
    disabledYesYesYes
    requiredYesYesYes
    nameYesDeferredDeferredNot generated for iOS and Android.
    valueYesDeferredDeferredNot generated for iOS and Android.

    Variants

    size

    Track width from size-element-*, so it follows touch; the label and description are 14px at every size. lg is the default, for settings pages and props panels; md in dense rows and toolbars, such as a table; xl next to xl Inputs and Buttons.

    • Get an email when someone mentions you.

      md

    • Get an email when someone mentions you.

      lgdefault

    • Get an email when someone mentions you.

      xl

    import { Switch } from "@/components/ui/switch"
    
    <Switch label="Email notifications" description="Get an email when someone mentions you." size="md" />
    <Switch label="Email notifications" description="Get an email when someone mentions you." />
    <Switch label="Email notifications" description="Get an email when someone mentions you." size="xl" />

    Examples

    Default

    import { Switch } from "@/components/ui/switch"
    
    <Switch label="Dark mode" />

    With a description

    Get an email when someone mentions you.

    import { Switch } from "@/components/ui/switch"
    
    <Switch label="Email notifications" description="Get an email when someone mentions you." defaultChecked />

    Disabled

    Required by your workspace.

    import { Switch } from "@/components/ui/switch"
    
    <Switch label="Two-factor authentication" description="Required by your workspace." disabled defaultChecked />

    In a table row

    md, the smallest size, for dense rows and toolbars. The name is in another column, so the switch takes aria-label.

    import { Switch } from "@/components/ui/switch"
    
    <Switch size="md" aria-label="Enable the deploy webhook" defaultChecked />

    Next to large controls

    lg matches lg Inputs and Buttons; the label and description step up with it (14px). The Variants section shows every size side by side.

    Anyone with the link can view it.

    import { Input } from "@/components/ui/input"
    import { Switch } from "@/components/ui/switch"
    
    <div className="flex w-full max-w-sm flex-col gap-lg">
      <Input size="lg" aria-label="Workspace name" defaultValue="Acme" />
      <Switch size="lg" label="Public workspace" description="Anyone with the link can view it." />
    </div>

    Do and don't

    Do

    • Use it for a setting that takes effect at once, and apply the change in onCheckedChange.
    • Name the setting in the label, and say what on does in the description.
    • Use md in dense rows and toolbars, such as a table, where the name sits in another column (aria-label). A props panel keeps the default lg, so the switch stays easy to hit on a phone.
    • Match the size of the controls around it: lg with lg Inputs on a settings page, md in a dense row, xl with xl Inputs and Buttons.

    Don't

    • Don't use it in a form that waits for Save; use Checkbox.
    • Don't use it to pick one of several options; use RadioGroup.
    • Don't change the label when it toggles (On, Off); the thumb shows the state.

    Props

    NameTypeValuesDefaultDescription
    sizeenummd | lg | xllgTrack width from size-element-*, so it follows touch; the label and description are 14px at every size. lg is the default, for settings pages and props panels; md in dense rows and toolbars, such as a table; xl next to xl Inputs and Buttons.
    labelnode--The visible name, after the switch. A press on it toggles the switch. Name the setting, not the action: Email notifications.
    descriptionnode--A second line under the label: what turning it on does. Needs label.
    requiredTextnode-RequiredThe Required note shown with required and a label, for a page in another language.
    checkedboolean--Controlled state. Pair it with onCheckedChange.
    defaultCheckedboolean-falseOn at first render, when you don't control it.
    onCheckedChangefunction--Called with true or false when the user toggles it. Apply the change here; there is no Save.
    disabledboolean-falseCan't be focused or toggled; the label dims too.
    requiredboolean-falseMust be on to submit the form.
    namestring--Form name. The form submits value under this name while the switch is on. Not generated for iOS and Android.
    valuestring-onValue submitted while the switch is on. Not generated for iOS and Android.

    Anatomy

    PartDescription
    rootThe track: a recessed rail (shadow-inset), radius-full, a border-strong edge (border-control on hover, like Checkbox) on the soft gray fill; on, the accent fills it to the edge. As wide as size-element-* and as tall as half of it plus the two borders, so it follows touch. Mouse: md 32 by 18px, lg 36 by 20px, xl 44 by 24px; touch: md 36 by 20px, lg 44 by 24px, xl 48 by 26px.
    thumbThe thumb, a raised knob (background-raised and shadow-low), offset from the inside start edge (offset-inline-start is that distance): a circle half of spacing-xs (2px) from the inner edge, the inside height minus spacing-xs: 12px (md), 14px (lg) or 18px (xl) on mouse. It takes the track's icon color: secondary when off, on-accent when on.
    hitAreaTransparent ::before centered on the track, never shorter than size-target-min (24px on mouse, 44px on touch).
    rowWith label: the row that holds the switch, then the Label and the description, gap-sm. Shared with Checkbox and RadioGroup.
    labelWith label: a Label after the switch, level with it on its first line. 14px at md, lg and xl.
    descriptionWith description: secondary text under the label, gap-xs, the same size as the label.

    Accessibility

    Role: switch

    KeysAction
    Space, EnterTurns the switch on or off.
    TabMoves focus to the next control; each switch is its own tab stop.
    • Name every switch with the label prop (a press on it toggles the switch), or aria-label when the name is shown elsewhere, such as a row in a table.
    • description is linked with aria-describedby and read after the name, outside the label.
    • The change applies at once. A choice that waits for Save is a Checkbox.
    • On and off differ by the thumb's position as well as the fill, so the state never relies on color alone.
    • The thumb slides only when the user allows motion; under prefers-reduced-motion it jumps.
    • On mouse the track is 18px (md), 20px (lg) or 24px (xl) tall, but its hit area is never smaller than size-target-min (24px on mouse, 44px on touch). On touch md, lg and xl grow with size-element-md, -lg and -xl.
    • In a right-to-left page the thumb starts at the right and slides left when on.

    Install

    npx werewolf-ui add switch

    Run npx werewolf-ui init once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See Installation for requirements and the alternative registry setup.