---
title: "Switch · Ark UI — StyleX Blocks"
description: "On/off toggles with a sliding thumb in solid and raised track variants."
url: "https://stylexblocks.com/components/ark-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: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-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/ark-ui/model-picker.md)
- HTML page: https://stylexblocks.com/components/ark-ui/switch/

## Install

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

```sh
npx stylexblocks init
```

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/ark-ui/switch.json

## Usage

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

export function Example() {
  return (
    // variant/size resolve once on Root; Control renders the Thumb by default
    <Switch.Root defaultChecked variant="solid" size="md" palette={indigoPalette}>
      <Switch.Control />
      <Switch.Label>Auto-save</Switch.Label>
      <Switch.HiddenInput />
    </Switch.Root>
  );
}

// controlled: <Switch.Root checked={checked} onCheckedChange={(d) => setChecked(d.checked)}>
```

## Source

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

```tsx
import * as stylex from "@stylexjs/stylex";
import { Switch as ArkSwitch, useSwitchContext } from "@ark-ui/react/switch";
import { createSlotRecipeContext } from "#lib/ark-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 ArkSwitch.Control>) {
  const machine = useSwitchContext();
  return (
    <ArkSwitch.Control {...props} data-checked={machine.checked ? "" : undefined}>
      {children ?? <Thumb />}
    </ArkSwitch.Control>
  );
}

function ThumbNode(props: React.ComponentProps<typeof ArkSwitch.Thumb>) {
  const machine = useSwitchContext();
  return <ArkSwitch.Thumb {...props} data-checked={machine.checked ? "" : undefined} />;
}

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

const Thumb = withContext(ThumbNode, "thumb", "SwitchThumb");

/**
 * Switch — Ark UI's switch machine wearing the switch slot recipe.
 * `<Switch.Root variant size>` resolves the recipe once; every part
 * inherits it through context. Control and Thumb copy `data-checked` from
 * the machine so the recipe's checked selectors apply.
 */
export const Switch = {
  Root: withProvider(
    ArkSwitch.Root,
    (v) => [...resolveSlotStyles(switchRecipe.slots.root, v), chrome.cursorPointer],
    "SwitchRoot",
  ),
  Control: withContext(ControlNode, "control", "SwitchControl"),
  Thumb,
  Label: withContext(ArkSwitch.Label, "label", "SwitchLabel"),
  HiddenInput: ArkSwitch.HiddenInput,
};

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>;
```
