---
title: "Switch · Base UI — StyleX Blocks"
description: "On/off toggles with a sliding thumb in solid and raised track variants."
url: "https://stylexblocks.com/components/base-ui/switch/"
agent:
  instructions: "Use this search API to find relevant components and blocks across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://stylexblocks.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Switch

On/off toggles with a sliding thumb in solid and raised track variants.

- Behavior engine: Base UI
- Also available for: [Ark UI](https://stylexblocks.com/components/ark-ui/switch.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/switch.md)
- Category: Form
- Variants: solid, raised
- Sizes: xs, sm, md, lg
- Used in blocks: [Model Picker](https://stylexblocks.com/block/base-ui/model-picker.md)
- HTML page: https://stylexblocks.com/components/base-ui/switch/

## Install

Configure the project once (sets the behavior engine and import aliases):

```sh
npx stylexblocks init --style base-ui
```

Then add the item as source you own:

```sh
npx stylexblocks add switch
```

Equivalent: `npx shadcn add @stylexblocks/switch`. Registry item JSON: https://stylexblocks.com/r/base-ui/switch.json

## Usage

```tsx
import { Switch } from "#components/ui/base-ui/switch.tsx";
import { indigoPalette } from "@stylexblocks/ui/themes/palettes.ts";

export function Example() {
  return (
    // Root is the label wrapper; Control is Base UI's Switch.Root and takes
    // the checked props — checked styling resolves via Base UI's state-aware
    // className callback
    <Switch.Root variant="solid" size="md" palette={indigoPalette}>
      <Switch.Control defaultChecked />
      <Switch.Label>Auto-save</Switch.Label>
    </Switch.Root>
  );
}

// controlled: <Switch.Control checked={checked} onCheckedChange={setChecked} />
```

## Source

`components/ui/base-ui/switch.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Switch as BaseSwitch } from "@base-ui/react/switch";
import { createSlotRecipeContext } from "#lib/base-ui/create-slot-recipe-context.tsx";
import { defineSlotRecipe, resolveSlotStyles } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.stylex.ts'
import { colors } from '@stylexblocks/ui/tokens/colors.stylex.ts'
import { cursor } from '@stylexblocks/ui/tokens/cursor.stylex.ts'
import { durations } from '@stylexblocks/ui/tokens/durations.stylex.ts'
import { fontSizes } from '@stylexblocks/ui/tokens/font-sizes.stylex.ts'
import { fontWeights } from '@stylexblocks/ui/tokens/font-weights.stylex.ts'
import { radii } from '@stylexblocks/ui/tokens/radii.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticShadows } from '@stylexblocks/ui/semantic-tokens/shadows.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

const switchRoot = stylex.create({
  base: {
    display: 'inline-flex',
    gap: spacing._2_5,
    alignItems: 'center',
    position: 'relative',
    verticalAlign: 'middle',
    '--switch-diff': 'calc(var(--switch-width) - var(--switch-height))',
    '--switch-x': 'calc(var(--switch-width) - var(--switch-height))',
  },
})

const switchRootSizes = stylex.create({
  xs: {
    '--switch-width': sizes._6,
    '--switch-height': sizes._3,
    '--switch-indicator-font-size': fontSizes.xs,
  },
  sm: {
    '--switch-width': sizes._8,
    '--switch-height': sizes._4,
    '--switch-indicator-font-size': fontSizes.xs,
  },
  md: {
    '--switch-width': sizes._10,
    '--switch-height': sizes._5,
    '--switch-indicator-font-size': fontSizes.sm,
  },
  lg: {
    '--switch-width': sizes._12,
    '--switch-height': sizes._6,
    '--switch-indicator-font-size': fontSizes.md,
  },
})

const switchLabel = stylex.create({
  base: {
    lineHeight: 1,
    userSelect: 'none',
    fontSize: fontSizes.sm,
    fontWeight: fontWeights.medium,
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const switchIndicator = stylex.create({
  base: {
    position: 'absolute',
    height: 'var(--switch-height)',
    width: 'var(--switch-height)',
    fontSize: 'var(--switch-indicator-font-size)',
    fontWeight: fontWeights.medium,
    flexShrink: 0,
    userSelect: 'none',
    display: 'grid',
    placeContent: 'center',
    transition: 'inset-inline-start 0.12s ease',
    insetInlineStart: {
      default: 'calc(var(--switch-x) - 2px)',
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])':
        '2px',
    },
  },
})

const switchControl = stylex.create({
  base: {
    display: 'inline-flex',
    gap: '0.5rem',
    flexShrink: 0,
    justifyContent: 'flex-start',
    cursor: {
      default: cursor.switch,
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
    borderRadius: radii.full,
    position: 'relative',
    width: 'var(--switch-width)',
    height: 'var(--switch-height)',
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
    '--focus-ring-color': {
      default: null,
      ':is([data-invalid], [aria-invalid="true"])': semanticColors.borderError,
    },
  },
})

const switchControlVariants = stylex.create({
  solid: {
    borderRadius: radii.full,
    backgroundColor: {
      default: semanticColors.bgEmphasized,
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])':
        colorPalette.solid,
    },
    outlineWidth: {
      default: null,
      ':focus-visible': '1px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
  raised: {
    borderRadius: radii.full,
    height: 'calc(var(--switch-height) / 2)',
    backgroundColor: {
      default: semanticColors.bgMuted,
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])': `color-mix(in oklch, ${colorPalette.solid} 60%, transparent)`,
    },
    boxShadow: semanticShadows.inset,
  },
})

const switchThumb = stylex.create({
  base: {
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
    flexShrink: 0,
    transitionProperty: 'translate',
    transitionDuration: durations.fast,
    borderRadius: 'inherit',
    translate: {
      default: null,
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])':
        'var(--switch-x) 0',
    },
  },
})

const switchThumbVariants = stylex.create({
  solid: {
    backgroundColor: {
      default: colors.white,
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])':
        colorPalette.contrast,
    },
    width: 'var(--switch-height)',
    height: 'var(--switch-height)',
    scale: 0.8,
    boxShadow: semanticShadows.sm,
  },
  raised: {
    width: 'var(--switch-height)',
    height: 'var(--switch-height)',
    position: 'relative',
    top: 'calc(var(--switch-height) * -0.25)',
    backgroundColor: {
      default: colors.white,
      ':is(:checked, [data-checked], [aria-checked="true"], [data-state="checked"])':
        colorPalette.solid,
    },
    boxShadow: semanticShadows.xs,
    outlineWidth: {
      default: null,
      ':focus-visible': '1px',
    },
    outlineOffset: {
      default: null,
      ':focus-visible': '2px',
    },
    outlineStyle: {
      default: null,
      ':focus-visible': 'solid',
    },
    outlineColor: {
      default: null,
      ':focus-visible': colorPalette.focusRing,
    },
  },
})

export type SwitchVariant = keyof typeof switchControlVariants

export type SwitchSize = keyof typeof switchRootSizes

const switchRecipe = defineSlotRecipe({
  slots: {
    root: { styles: switchRoot, sizes: switchRootSizes },
    label: { styles: switchLabel },
    control: { styles: switchControl, variants: switchControlVariants },
    thumb: { styles: switchThumb, variants: switchThumbVariants },
  },
  defaultVariants: { variant: "solid", size: "md" },
});

const chrome = stylex.create({
  cursorPointer: { cursor: "pointer" },
});

function ControlNode({
  children,
  ...props
}: React.ComponentProps<typeof BaseSwitch.Root>) {
  return <BaseSwitch.Root {...props}>{children ?? <Thumb />}</BaseSwitch.Root>;
}

const { withProvider, withContext } = createSlotRecipeContext(switchRecipe);

const Thumb = withContext(BaseSwitch.Thumb, "thumb", "SwitchThumb");

/**
 * Switch — Base UI's switch wearing the switch slot recipe. The Root label
 * resolves variant and size once; Control (Base UI's Switch.Root) and Thumb
 * inherit them through context. Checked chrome targets the engine's
 * `data-checked`.
 */
export const Switch = {
  Root: withProvider(
    "label",
    (v) => [...resolveSlotStyles(switchRecipe.slots.root, v), chrome.cursorPointer],
    "SwitchRoot",
  ),
  Control: withContext(ControlNode, "control", "SwitchControl"),
  Thumb,
  Label: withContext("span", "label", "SwitchLabel"),
};

export type SwitchRootProps = React.ComponentProps<typeof Switch.Root>;
export type SwitchControlProps = React.ComponentProps<typeof Switch.Control>;
export type SwitchThumbProps = React.ComponentProps<typeof Switch.Thumb>;
export type SwitchLabelProps = React.ComponentProps<typeof Switch.Label>;
```
