All components
Toggle
A two-state button that stays pressed, in ghost and outline styles matched to Button.
12345678910111213import { 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}>Variants
ghost
outline
Sizes
xs
sm
md
lg
Indicator
Toggle.Indicator swaps its content with the pressed state: children while pressed, fallback while off.
123456789101112import { 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.
12345678910import { 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.
123<Toggle.Root defaultPressed disabled aria-label="Bold"> <BoldIcon /></Toggle.Root>Install
1npx stylexblocks add toggleSource
components/ui/ark-ui/toggle.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168import * 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>;