Components
Toggle
Pressed-state button and toggle group built on the Button surface.
Examples
Sliding selection
indicator="slide" draws the selection as one thumb that slides to the pressed item, and the pressed item drops its own fill. The thumb follows resizes and moves instantly under reduced motion. Style it with the --cui-track-highlight-* knobs. A group with multiple keeps painting each pressed item itself. Set indicators: { "toggle-group": "slide" } in a skin to make it the default.
Composition
Single toggle
- <Toggle />
Toggle group
- <ToggleGroup>
- <Toggle />
Installation
The Control UI source installs this component from src/registry/sources/control-ui/ui/toggle.tsx.
npx shadcn@latest add https://control-ui.dev/r/toggle.jsonDependencies
Raw code
Primary installed source
"use client";
import { Toggle as TogglePrimitive } from "@base-ui/react/toggle";import { ToggleGroup as ToggleGroupPrimitive } from "@base-ui/react/toggle-group";import { type ComponentProps, useContext } from "react";import type { ControlledMultiChoice, HoverIndicator, SelectionIndicator } from "@/components/control-ui/control-props";import { TrackHighlight } from "@/components/control-ui/extensions/track-highlight";import { cn } from "@/components/control-ui/lib/cn";import { type SkinIconProps, skinAdornment } from "@/components/control-ui/skin";import { useSkin, useSkinIcon } from "@/components/control-ui/skin-provider";
import type { ButtonProps } from "@/components/control-ui/ui/button";import { ButtonTrackContext, buttonContentClasses, buttonStructureClasses, buttonTrackStructureClasses,} from "@/components/control-ui/ui/button";
export type ToggleProps = Omit<ButtonProps, "render" | "nativeButton" | "value" | "focusableWhenDisabled"> & { pressed?: boolean; defaultPressed?: boolean; onPressedChange?: (pressed: boolean) => void; value?: string; showCheck?: boolean;};
export type ToggleGroupIndicator = SelectionIndicator | HoverIndicator;
export type ToggleGroupProps<TValue extends string = string> = Omit<ComponentProps<"div">, "defaultValue" | "onChange"> & ControlledMultiChoice<TValue> & { multiple?: boolean; disabled?: boolean; orientation?: "horizontal" | "vertical"; indicator?: ToggleGroupIndicator; };
const toggleItemSelector = '[data-control-ui="toggle"][data-slot="root"]';const pressedToggleSelector = '[data-control-ui="toggle"][data-slot="root"][data-active="true"]';
function ToggleCheckIcon(props: SkinIconProps) { return ( <svg viewBox="0 0 12 12" fill="none" aria-hidden="true" {...props}> <path d="M2.5 6.5 5 9l4.5-5" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" /> </svg> );}
export function Toggle({ variant = "surface", size = "sm", tone = "neutral", active, iconOnly = false, shape = "default", layout = "inline", showCheck = false, className, pressed, defaultPressed, onPressedChange, value, disabled, children, ...props}: ToggleProps) { const CheckGlyph = useSkinIcon("check", ToggleCheckIcon); const skin = useSkin(); const layer = skinAdornment(skin, "button", "layer", { variant, tone }); const tracksHover = useContext(ButtonTrackContext); return ( <TogglePrimitive data-track-item={tracksHover ? "" : undefined} pressed={pressed} defaultPressed={defaultPressed} onPressedChange={onPressedChange} value={value} disabled={disabled} render={(renderProps, state) => { const isActive = active ?? state.pressed; return ( <button type="button" {...renderProps} data-control-ui="toggle" data-control-family="button" data-slot="root" data-control="true" data-active={isActive ? "true" : undefined} data-variant={variant} data-tone={tone} data-size={size} data-icon-only={iconOnly ? "true" : undefined} data-shape={shape} data-layout={layout} className={cn(buttonStructureClasses, renderProps.className, className)} > {layer} <span data-control-ui="toggle" data-control-family="button" data-slot="content" className={buttonContentClasses}> {showCheck ? ( <span data-control-ui="toggle" data-slot="check" className="flex size-3.5 shrink-0 items-center justify-center"> {isActive ? <CheckGlyph className="size-3" aria-hidden="true" /> : null} </span> ) : null} {renderProps.children} </span> </button> ); }} {...props} > {children} </TogglePrimitive> );}
export function ToggleGroup<TValue extends string = string>({ className, orientation = "horizontal", multiple = false, indicator, children, ...props}: ToggleGroupProps<TValue>) { const skin = useSkin(); const requestedIndicator = indicator ?? skin.indicators?.["toggle-group"] ?? "none"; const resolvedIndicator = multiple && requestedIndicator === "slide" ? "none" : requestedIndicator; const tracksHover = resolvedIndicator === "hover"; const slides = resolvedIndicator === "slide"; return ( <ButtonTrackContext value={tracksHover}> <ToggleGroupPrimitive data-control-ui="toggle" data-control-family="button" data-slot="group" data-orientation={orientation} data-track={resolvedIndicator} orientation={orientation} multiple={multiple} className={cn( buttonTrackStructureClasses, "inline-flex items-center", orientation === "vertical" && "flex-col", slides && "relative isolate [&_[data-control-ui=toggle][data-slot=root]]:isolation-auto", className, )} {...props} > {children} {tracksHover ? <TrackHighlight className="z-0" /> : null} {slides ? ( <TrackHighlight className="z-0" itemSelector={toggleItemSelector} activeSelector={pressedToggleSelector} followHover={false} /> ) : null} </ToggleGroupPrimitive> </ButtonTrackContext> );}
Knobs
Typed custom properties the recipe paints with. Set one on the root — style, a utility class, or a skin — and every slot inherits it.
| Knob | Syntax | Default |
|---|---|---|
--cui-button-press-background | <color> | var(--cui-button-hover-background) |
--cui-button-press-scale | <number> | 0.96 |
--cui-button-hover-border-color | <color> | var(--cui-button-border-color) |
--cui-button-open-border-color | <color> | var(--cui-button-hover-border-color) |
--cui-button-open-background | <color> | var(--cui-button-hover-background) |
--cui-button-open-foreground | <color> | var(--cui-button-hover-foreground) |
--cui-button-open-shadow | * | var(--cui-button-shadow) |
--cui-button-hover-background-image | * | var(--cui-button-background-image) |
--cui-button-hover-background | <color> | var(--hover-fill) |
--cui-button-hover-foreground | <color> | var(--foreground) |
--cui-button-active-background | <color> | var(--active-fill) |
--cui-button-active-foreground | <color> | var(--foreground) |
--cui-button-active-hover-background | <color> | var(--cui-button-active-background) |
--cui-button-hover-shadow | * | var(--cui-button-shadow) |
--cui-button-press-shadow | * | var(--cui-button-hover-shadow) |
--cui-button-active-shadow | * | var(--cui-button-shadow) |
--cui-button-radius | <length-percentage> | var(--nest-radius, var(--radius-control)) |
--cui-button-gap | <length-percentage> | calc(var(--spacing) * 1.5) |
--cui-button-icon | <length> | 1rem |
--cui-button-height | <length-percentage> | var(--control-h-md) |
--cui-button-padding-inline | <length-percentage> | var(--padding-x) |
--cui-button-padding-block | <length-percentage> | calc(var(--spacing) * 1.5) |
--cui-button-icon-padding-inline | <length-percentage> | max(0px, calc(var(--cui-button-padding-inline) - var(--spacing) * 0.5)) |
--cui-button-font-size | <length-percentage> | var(--text-body) |
--cui-button-font-weight | <number> | var(--font-weight-medium) |
--cui-button-background | <color> | transparent |
--cui-button-background-image | * | none |
--cui-button-foreground | <color> | var(--muted-foreground) |
--cui-button-shadow | * | 0 0 transparent |
--cui-button-border-width | <length> | 0px |
--cui-button-border-style | * | solid |
--cui-button-border-color | <color> | var(--control-rim) |
--cui-button-backdrop-filter | * | none |