All components
Toggle Group
A set of toggle buttons with single or multiple selection and arrow-key navigation.
123456789101112131415import { 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}>Variants
ghost
outline
Sizes
xs
sm
md
lg
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.
1234567891011import { 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.
123456789101112import { 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.
12345<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.
12345<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>Install
1npx stylexblocks add toggle-groupSource
components/ui/base-ui/toggle-group.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193import * 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>;