Toggle
A button that stays on or off. Use it to turn on a text style, a filter, or a view option.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/toggle.jsonCopies Toggle into your app, installs the packages, and adds the styles. Then import it from @/components/ui/toggle. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @base-ui/reactAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Copy and paste the following code into your project.
'use client'; import { Toggle as BaseToggle } from '@base-ui/react/toggle'; import { cn, stateClassName } from '@/lib/sprout/classes'; export const toggleVariantNames = ['default', 'outline'] as const; export const toggleSizeNames = ['md', 'sm', 'lg'] as const; export type ToggleVariant = (typeof toggleVariantNames)[number]; export type ToggleSize = (typeof toggleSizeNames)[number]; export interface ToggleVariantOptions { variant?: ToggleVariant | null; size?: ToggleSize | null; className?: string; } export function toggleVariants({ variant, size, className }: ToggleVariantOptions = {}) { return cn( 'sprout-toggle', `sprout-toggle-variant-${variant ?? 'default'}`, `sprout-toggle-size-${size ?? 'md'}`, className, ); } export interface ToggleProps<Value extends string = string> extends BaseToggle.Props<Value> { variant?: ToggleVariant | null; size?: ToggleSize | null; } export function Toggle<Value extends string = string>({ variant, size, className, ...props }: ToggleProps<Value>) { return ( <BaseToggle<Value> data-slot="toggle" data-variant={variant ?? 'default'} data-size={size ?? 'md'} {...props} className={stateClassName<BaseToggle.State>(toggleVariants({ variant, size }), className)} /> ); }Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-toggle { --sprout-toggle-icon: 1rem; box-sizing: border-box; height: var(--sprout-control-height); min-width: var(--sprout-control-height); color: var(--sprout-muted); font-family: inherit; font-size: var(--sprout-text-sm); white-space: nowrap; cursor: pointer; user-select: none; -webkit-tap-highlight-color: transparent; background: padding-box padding-box; border: 1px solid #0000; border-radius: 10px; outline: none; flex-shrink: 0; justify-content: center; align-items: center; gap: 4px; margin: 0; padding: 0 10px; font-weight: 500; line-height: 20px; text-decoration: none; display: inline-flex; position: relative; } .sprout-toggle svg { pointer-events: none; flex-shrink: 0; } .sprout-toggle svg:not([class*="size-"]) { width: var(--sprout-toggle-icon); height: var(--sprout-toggle-icon); } .sprout-toggle:has( > [data-icon="inline-start"]) { padding-inline-start: 8px; } .sprout-toggle:has( > [data-icon="inline-end"]) { padding-inline-end: 8px; } .sprout-toggle:focus-visible { z-index: 1; outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: 0; } .sprout-toggle:disabled, .sprout-toggle[data-disabled] { cursor: not-allowed; opacity: .5; } .sprout-toggle[aria-busy="true"] { cursor: progress; } @media (hover: hover) { .sprout-toggle:not(:disabled, [data-disabled]):hover { background-color: var(--sprout-hover); color: var(--sprout-text); } } .sprout-toggle:not(:disabled, [data-disabled]):active { background-color: var(--sprout-active); color: var(--sprout-text); } .sprout-toggle[data-pressed], .sprout-toggle[data-pressed]:not(:disabled, [data-disabled]):hover { background-color: var(--sprout-hover); color: var(--sprout-text); } .sprout-toggle[aria-invalid="true"] { border-color: var(--sprout-danger); } .sprout-toggle[aria-invalid="true"]:focus-visible { outline-color: var(--sprout-danger); } .sprout-toggle-variant-outline { border-color: var(--sprout-input); background-color: #0000; } .sprout-toggle-size-sm { --sprout-toggle-icon: .875rem; height: calc(var(--sprout-control-height) - 4px); min-width: calc(var(--sprout-control-height) - 4px); font-size: var(--sprout-text-xs); line-height: 16px; } .sprout-toggle-size-sm:has( > [data-icon="inline-start"]) { padding-inline-start: 6px; } .sprout-toggle-size-sm:has( > [data-icon="inline-end"]) { padding-inline-end: 6px; } .sprout-toggle-size-lg { height: calc(var(--sprout-control-height) + 4px); min-width: calc(var(--sprout-control-height) + 4px); } @media (prefers-reduced-motion: no-preference) { .sprout-toggle { transition: color var(--sprout-motion-fast, .12s) ease-out, background-color var(--sprout-motion-fast, .12s) ease-out, border-color var(--sprout-motion-fast, .12s) ease-out, box-shadow var(--sprout-motion-fast, .12s) ease-out; } } @media (forced-colors: active) { .sprout-toggle { border-color: buttontext; } .sprout-toggle[data-pressed] { color: highlighttext; background-color: highlight; } } }Import it from
@/components/ui/toggle. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/toggle.
Variants and sizes
default has no border. outline draws a control border. Each size pairs a labeled toggle with an icon-only one.
Small
28px · filter chipMedium
32px · icon and labelLarge
36px · view optionStates
Off, on, disabled, and disabled while on. A disabled toggle keeps its pressed look so the saved value stays visible.
Formatting toolbar
Each toggle is controlled with pressed and onPressedChange. Tab moves between them; Space or Enter presses one.
Applied: Bold
Icon placement
Mark a leading or trailing icon with data-icon="inline-start" or "inline-end" to tighten that side by 2px.
Saving and errors
Disable the toggle and set aria-busy while a change saves. Set aria-invalid and describe the error when it fails.
Press Pin to sidebar to see the error.
toggleVariants
Give a Base UI Toggle the same look inside your own group. This is how Toggle Group items get their style.
Usage
import { Toggle } from "@tulipfarm/sprout/toggle";
<Toggle aria-label="Bold" defaultPressed>
<Bold />
</Toggle>
const [starred, setStarred] = useState(false);
<Toggle variant="outline" size="sm" pressed={starred} onPressedChange={setStarred}>
<Star data-icon="inline-start" />
Starred
</Toggle>Pass pressed and onPressedChange to control the toggle, or defaultPressed to let it keep its own state. While on, it hasaria-pressed="true" anddata-pressed.
Props
variant is "default" (no border) or "outline" (a control border). size is "sm" (28px, 12px text),"md" (32px, the default), or "lg" (36px). Each size is at least as wide as it is tall, with 10px side padding.
Icons are 16px (14px in sm) unless they set their own size. Mark a leading or trailing icon with data-icon="inline-start"or "inline-end" to tighten padding on that side.
disabled blocks presses and dims the toggle. Setaria-invalid to draw a danger border, and aria-busy while a change saves. The toggle also takes Base UI props such asvalue, render, nativeButton, and state functions for className.
toggleVariants
import { Toggle } from "@base-ui/react/toggle";
import { ToggleGroup } from "@base-ui/react/toggle-group";
import { toggleVariants } from "@tulipfarm/sprout/toggle";
<ToggleGroup aria-label="Time range" defaultValue={["30d"]}>
<Toggle value="7d" className={toggleVariants({ variant: "outline", size: "sm" })}>
7d
</Toggle>
<Toggle value="30d" className={toggleVariants({ variant: "outline", size: "sm" })}>
30d
</Toggle>
</ToggleGroup>toggleVariants({ variant, size, className }) returns the class names of a toggle. Use it when you build your own group or render another element as a toggle. For a ready-made group, useToggle Group.
Accessibility
Toggle renders a button with aria-pressed. Tab moves focus to it, and Space or Enter presses it. A focused toggle shows a 2px focus ring. An icon-only toggle needs an aria-label. Keep the label the same when the state changes; screen readers already announce on or off.
Color changes take 120ms and stop when reduced motion is requested. In forced colors mode the toggle gets a border and the pressed toggle is highlighted.
Theme tokens
Off uses --sprout-muted, and hover uses --sprout-hover. On uses --sprout-primary on --sprout-primary-subtle, so the opt-in TulipFarm theme tints it green. Outline uses --sprout-input and --sprout-surface. Invalid uses --sprout-danger. Radius follows --sprout-radius-control.