All components
Toggle
A two-state button that stays pressed, in ghost and outline styles matched to Button.
12345678910111213import { Toggle } from "#components/ui/ui-sdk/toggle.tsx";
export function Example() { return ( // variant/size resolve on Root; aria-pressed and data-pressed come from // UI SDK's Toggle <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/ui-sdk/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/ui-sdk/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/ui-sdk/toggle.tsx
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202import * as stylex from "@stylexjs/stylex";import { createContext, useContext } from "react";import { Toggle as UiSdkToggle } from "@ui-sdk/react/toggle";import { createSlotRecipeContext } from "#lib/ui-sdk/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);
const PressedContext = createContext(false);
/** * Shares UI SDK's pressed state (controlled, uncontrolled or from a group) * with `Toggle.Indicator` through the Toggle's children-as-function — UI * SDK's Toggle has no indicator part of its own. */function RootNode({ children, ...props }: React.ComponentProps<typeof UiSdkToggle>) { return ( <UiSdkToggle {...props}> {(state) => ( <PressedContext.Provider value={state.pressed}> {typeof children === "function" ? children(state) : children} </PressedContext.Provider> )} </UiSdkToggle> );}
export interface ToggleIndicatorProps extends React.ComponentProps<"span"> { /** Rendered while the toggle is not pressed. */ fallback?: React.ReactNode;}
/** Renders `children` while pressed and `fallback` otherwise. */function Indicator({ children, fallback, ...props }: ToggleIndicatorProps) { const pressed = useContext(PressedContext); return ( <span data-pressed={pressed ? "" : undefined} {...props}> {pressed ? children : fallback} </span> );}
/** * Toggle — UI SDK's Toggle wearing the toggle recipe: a native button with * `aria-pressed`. `<Toggle.Root variant size>` resolves the recipe; the * pressed fill keys off UI SDK's `data-pressed`. `Toggle.Indicator` swaps * content on the pressed state (`fallback` renders while off). Composition * via `render`. */export const Toggle = { Root: withProvider(RootNode, "root", "ToggleRoot"), Indicator,};
export type ToggleRootProps = React.ComponentProps<typeof Toggle.Root>;
Examples
Installable blocks built with Toggle — real use cases, delivered as source you own.
Card Grid
Searchable, categorized pattern and starter-kit library with category toggles, saved bookmarks, a sort menu, grouped responsive card sections, and an interactive detail preview dialog.