---
title: "Toggle Group · Base UI — StyleX Blocks"
description: "A set of toggle buttons with single or multiple selection and arrow-key navigation."
url: "https://stylexblocks.com/components/base-ui/toggle-group/"
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 Group

A set of toggle buttons with single or multiple selection and arrow-key navigation.

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

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

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

## Usage

```tsx
import { ToggleGroup } from "#components/ui/base-ui/toggle-group.tsx";

export function Example() {
  return (
    // variant/size resolve once on Root; items (Base UI Toggles) inherit
    // them. Single selection by default — add `multiple` to allow several.
    <ToggleGroup.Root defaultValue={["left"]} variant="ghost" size="md" aria-label="Text alignment">
      <ToggleGroup.Item value="left" aria-label="Align left"><AlignLeftIcon /></ToggleGroup.Item>
      <ToggleGroup.Item value="center" aria-label="Align center"><AlignCenterIcon /></ToggleGroup.Item>
      <ToggleGroup.Item value="right" aria-label="Align right"><AlignRightIcon /></ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}

// controlled: <ToggleGroup.Root value={value} onValueChange={setValue}>
```

## Examples

### Multiple selection

Set multiple to let several items stay pressed at once — a text-formatting group. Items then expose aria-pressed instead of acting as radios.

```tsx
import { ToggleGroup } from "#components/ui/base-ui/toggle-group.tsx";

export function Example() {
  return (
    <ToggleGroup.Root multiple defaultValue={["bold"]} aria-label="Formatting">
      <ToggleGroup.Item value="bold" aria-label="Bold"><BoldIcon /></ToggleGroup.Item>
      <ToggleGroup.Item value="italic" aria-label="Italic"><ItalicIcon /></ToggleGroup.Item>
      <ToggleGroup.Item value="underline" aria-label="Underline"><UnderlineIcon /></ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}
```

### Outline with labels

The outline variant borders every item; text labels work as well as icons. A palette tints the pressed item.

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

export function Example() {
  return (
    <ToggleGroup.Root variant="outline" defaultValue={["week"]} palette={indigoPalette} aria-label="Range">
      <ToggleGroup.Item value="day">Day</ToggleGroup.Item>
      <ToggleGroup.Item value="week">Week</ToggleGroup.Item>
      <ToggleGroup.Item value="month">Month</ToggleGroup.Item>
    </ToggleGroup.Root>
  );
}
```

### Vertical

orientation="vertical" stacks the items and switches arrow-key navigation to Up and Down.

```tsx
<ToggleGroup.Root orientation="vertical" defaultValue={["center"]} aria-label="Text alignment">
  <ToggleGroup.Item value="left" aria-label="Align left"><AlignLeftIcon /></ToggleGroup.Item>
  <ToggleGroup.Item value="center" aria-label="Align center"><AlignCenterIcon /></ToggleGroup.Item>
  <ToggleGroup.Item value="right" aria-label="Align right"><AlignRightIcon /></ToggleGroup.Item>
</ToggleGroup.Root>
```

### Disabled

Disable the whole group on Root, or a single option on its Item.

```tsx
<ToggleGroup.Root defaultValue={["left"]} aria-label="Text alignment">
  <ToggleGroup.Item value="left" aria-label="Align left"><AlignLeftIcon /></ToggleGroup.Item>
  <ToggleGroup.Item value="center" aria-label="Align center" disabled><AlignCenterIcon /></ToggleGroup.Item>
  <ToggleGroup.Item value="right" aria-label="Align right"><AlignRightIcon /></ToggleGroup.Item>
</ToggleGroup.Root>
```

## Source

`components/ui/base-ui/toggle-group.tsx`

```tsx
import * as stylex from "@stylexjs/stylex";
import { Toggle as BaseToggle } from "@base-ui/react/toggle";
import { ToggleGroup as BaseToggleGroup } from "@base-ui/react/toggle-group";
import { createSlotRecipeContext } from "#lib/base-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

// The group only adds layout; every item is a Toggle wearing the toggle
// recipe, so a lone Toggle and a grouped one look identical.
const toggleGroupRoot = stylex.create({
  base: {
    display: "inline-flex",
    alignItems: "center",
    gap: spacing._1,
    flexDirection: {
      default: "row",
      ':is([data-orientation="vertical"])': "column",
    },
  },
});

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

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

function RootNode(props: BaseToggleGroup.Props) {
  return <BaseToggleGroup {...props} />;
}

function ItemNode(props: BaseToggle.Props) {
  return <BaseToggle {...props} />;
}

/**
 * Toggle Group — Base UI's ToggleGroup; items are Base UI Toggles wearing
 * the toggle recipe. `<ToggleGroup.Root variant size>` resolves the recipe
 * once and every item inherits it. Single selection by default; `multiple`
 * lets several stay pressed. Arrow keys move focus between items, and
 * inside a Base UI Toolbar the items join the toolbar's own focus order.
 * Pressed items carry `data-pressed`.
 */
export const ToggleGroup = {
  Root: withProvider(RootNode, "root", "ToggleGroupRoot"),
  Item: withContext(ItemNode, "item", "ToggleGroupItem"),
};

export type ToggleGroupRootProps = React.ComponentProps<typeof ToggleGroup.Root>;
export type ToggleGroupItemProps = React.ComponentProps<typeof ToggleGroup.Item>;
```
