---
title: "Toggle · Ark UI — StyleX Blocks"
description: "A two-state button that stays pressed, in ghost and outline styles matched to Button."
url: "https://stylexblocks.com/components/ark-ui/toggle/"
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/"
---

# Toggle

A two-state button that stays pressed, in ghost and outline styles matched to Button.

- Behavior engine: Ark UI
- Also available for: [Base UI](https://stylexblocks.com/components/base-ui/toggle.md), [UI SDK](https://stylexblocks.com/components/ui-sdk/toggle.md)
- Category: Form
- Variants: ghost, outline
- Sizes: xs, sm, md, lg
- HTML page: https://stylexblocks.com/components/ark-ui/toggle/

## 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 toggle
```

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

## Usage

```tsx
import { Toggle } from "#components/ui/ark-ui/toggle.tsx";

export function Example() {
  return (
    // variant/size resolve on Root; aria-pressed and data-pressed come from
    // the toggle machine
    <Toggle.Root defaultPressed variant="ghost" size="md" aria-label="Bold">
      <BoldIcon />
    </Toggle.Root>
  );
}

// controlled: <Toggle.Root pressed={pressed} onPressedChange={setPressed}>
```

## Examples

### Indicator

Toggle.Indicator swaps its content with the pressed state: children while pressed, fallback while off.

```tsx
import { Toggle } from "#components/ui/ark-ui/toggle.tsx";

export function Example() {
  return (
    <Toggle.Root variant="outline">
      <Toggle.Indicator fallback={<StarIcon />}>
        <StarIcon filled />
      </Toggle.Indicator>
      Favorite
    </Toggle.Root>
  );
}
```

### Palette

A palette tints the pressed fill and text.

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

export function Example() {
  return (
    <Toggle.Root defaultPressed palette={indigoPalette} aria-label="Bold">
      <BoldIcon />
    </Toggle.Root>
  );
}
```

### Disabled

A disabled toggle keeps its pressed state but ignores clicks.

```tsx
<Toggle.Root defaultPressed disabled aria-label="Bold">
  <BoldIcon />
</Toggle.Root>
```

## Source

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

```tsx
import * as stylex from "@stylexjs/stylex";
import { Toggle as ArkToggle } from "@ark-ui/react/toggle";
import { createSlotRecipeContext } from "#lib/ark-ui/create-slot-recipe-context.tsx";
import { defineSlotRecipe } from "#lib/recipe-props.ts";
import { colorPalette } from '@stylexblocks/ui/color-palette.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 { lineHeights } from '@stylexblocks/ui/tokens/line-heights.stylex.ts'
import { semanticColors } from '@stylexblocks/ui/semantic-tokens/colors.stylex.ts'
import { semanticRadii } from '@stylexblocks/ui/semantic-tokens/radii.stylex.ts'
import { sizes } from '@stylexblocks/ui/tokens/sizes.stylex.ts'
import { spacing } from '@stylexblocks/ui/tokens/spacing.stylex.ts'

// A two-state button. Shares the house Button's metrics (control heights,
// radii, focus ring, disabled treatment); the pressed state fills with the
// palette's subtle tint. Pressed is matched on every engine's attribute:
// Ark (`data-pressed`, toggle-group items `data-state="on"`) and Base UI
// (`data-pressed`).

const toggleStyles = stylex.create({
  base: {
    display: 'inline-flex',
    appearance: 'none',
    alignItems: 'center',
    justifyContent: 'center',
    userSelect: 'none',
    position: 'relative',
    whiteSpace: 'nowrap',
    verticalAlign: 'middle',
    flexShrink: 0,
    margin: 0,
    borderWidth: 0,
    borderStyle: 'solid',
    borderColor: 'transparent',
    backgroundColor: 'transparent',
    fontFamily: 'inherit',
    fontWeight: fontWeights.medium,
    lineHeight: 1.2,
    cursor: {
      default: 'pointer',
      ':is(:disabled, [disabled], [data-disabled])': 'not-allowed',
    },
    outline: 'none',
    transitionProperty: 'background-color, border-color, color, box-shadow',
    transitionDuration: durations.moderate,
    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,
    },
    opacity: {
      default: null,
      ':is(:disabled, [disabled], [data-disabled])': 0.5,
    },
  },
})

const toggleSizes = stylex.create({
  xs: {
    gap: spacing._1,
    height: sizes.controlXs,
    minWidth: sizes.controlXs,
    borderRadius: semanticRadii.control,
    paddingInline: spacing._1_5,
    fontSize: fontSizes.xs,
    lineHeight: lineHeights.xs,
  },
  sm: {
    gap: spacing._1_5,
    height: sizes.controlSm,
    minWidth: sizes.controlSm,
    borderRadius: semanticRadii.control,
    paddingInline: spacing._2,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  md: {
    gap: spacing._2,
    height: sizes.controlMd,
    minWidth: sizes.controlMd,
    borderRadius: semanticRadii.control,
    paddingInline: spacing._2_5,
    fontSize: fontSizes.sm,
    lineHeight: lineHeights.sm,
  },
  lg: {
    gap: spacing._2,
    height: sizes.controlLg,
    minWidth: sizes.controlLg,
    borderRadius: semanticRadii.control,
    paddingInline: spacing._3,
    fontSize: fontSizes.md,
    lineHeight: lineHeights.md,
  },
})

const toggleVariants = stylex.create({
  ghost: {
    color: {
      default: semanticColors.fgMuted,
      ':not([data-pressed], [data-state="on"]):hover': semanticColors.fg,
      ':is([data-pressed], [data-state="on"])': colorPalette.fg,
    },
    backgroundColor: {
      default: 'transparent',
      ':not([data-pressed], [data-state="on"]):hover': colorPalette.muted,
      ':is([data-pressed], [data-state="on"])': colorPalette.subtle,
    },
  },
  outline: {
    borderWidth: '1px',
    borderColor: {
      default: colorPalette.border,
      ':is([data-pressed], [data-state="on"])': colorPalette.emphasized,
    },
    color: {
      default: semanticColors.fgMuted,
      ':not([data-pressed], [data-state="on"]):hover': semanticColors.fg,
      ':is([data-pressed], [data-state="on"])': colorPalette.fg,
    },
    backgroundColor: {
      default: 'transparent',
      ':not([data-pressed], [data-state="on"]):hover': colorPalette.muted,
      ':is([data-pressed], [data-state="on"])': colorPalette.subtle,
    },
  },
})

export type ToggleSize = keyof typeof toggleSizes

export type ToggleVariant = keyof typeof toggleVariants

const toggleSlotRecipe = defineSlotRecipe({
  slots: {
    root: { styles: toggleStyles, sizes: toggleSizes, variants: toggleVariants },
  },
  defaultVariants: { variant: "ghost", size: "md" },
});

const { withProvider } = createSlotRecipeContext(toggleSlotRecipe);

/**
 * Toggle — Ark UI's toggle machine wearing the toggle recipe: a two-state
 * button with `aria-pressed`. `<Toggle.Root variant size>` resolves the
 * recipe; the pressed fill keys off the machine's `data-pressed`.
 * `Toggle.Indicator` swaps content on the pressed state (`fallback` renders
 * while off). Composition via `asChild`.
 */
export const Toggle = {
  Root: withProvider(ArkToggle.Root, "root", "ToggleRoot"),
  Indicator: ArkToggle.Indicator,
};

export type ToggleRootProps = React.ComponentProps<typeof Toggle.Root>;
export type ToggleIndicatorProps = React.ComponentProps<typeof Toggle.Indicator>;
```
