Toggle Group
An animated React toggle group built on shadcn/ui and Radix: the drop-in Toggle and ToggleGroup add smooth transitions and a clear active state — plus a Motion block where the active pill glides between options, for single and multiple selection.
- Drop-in shadcn API
- Radix Toggle Group
- Motion layoutId
- Single & multiple
- prefers-reduced-motion
View: grid · Marks: bold
"use client";
import * as React from "react";
import { Bold, Code, Italic, LayoutGrid, List, Rows3, Underline } from "lucide-react";
import {
AnimatedToggleGroup,
AnimatedToggleGroupItem,
} from "@/components/dusk/toggle-group/animated-toggle-group";
export default function AnimatedToggleGroupDemo() {
const [view, setView] = React.useState("grid");
const [marks, setMarks] = React.useState<string[]>(["bold"]);
return (
<div className="grid justify-items-center gap-6">
<AnimatedToggleGroup type="single" value={view} onValueChange={(v) => v && setView(v)} aria-label="View">
<AnimatedToggleGroupItem value="grid">
<LayoutGrid /> Grid
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="list">
<List /> List
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="compact">
<Rows3 /> Compact
</AnimatedToggleGroupItem>
</AnimatedToggleGroup>
<AnimatedToggleGroup type="multiple" value={marks} onValueChange={setMarks} aria-label="Text formatting">
<AnimatedToggleGroupItem value="bold" aria-label="Bold">
<Bold />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="italic" aria-label="Italic">
<Italic />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="underline" aria-label="Underline">
<Underline />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="code" aria-label="Inline code">
<Code />
</AnimatedToggleGroupItem>
</AnimatedToggleGroup>
<p className="text-xs text-muted-foreground" aria-live="polite">
View: <span className="text-foreground">{view}</span> · Marks:{" "}
<span className="text-foreground">{marks.length ? marks.join(", ") : "none"}</span>
</p>
</div>
);
}Installation
$ npx shadcn@latest add https://duskui.com/r/toggle-group.json1. Install the dependencies.
npm install @radix-ui/react-toggle-group @radix-ui/react-toggle class-variance-authority2. Copy the source into
components/ui/toggle-group.tsx.toggle-group.tsx "use client"; import * as React from "react"; import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"; import { type VariantProps } from "class-variance-authority"; import { cn } from "@/lib/utils"; import { toggleVariants } from "@/components/ui/toggle"; /** * DuskUI Toggle Group — a drop-in upgrade of the shadcn/ui Toggle Group. * * Same parts, props and data-slot attributes as shadcn (ToggleGroup, * ToggleGroupItem with `variant`, `size` and `spacing`) plus everything the * DuskUI Toggle adds: smooth transitions and a clear neutral "on" surface. * With `spacing={0}` (default) the items join into one segmented bar and the * focused item lifts above its neighbours so its ring is never clipped. * `type="single"` picks one value, `type="multiple"` an array. Radix handles * roving focus (arrow keys), aria-pressed / aria-checked and RTL. */ type ToggleGroupContextValue = VariantProps<typeof toggleVariants> & { spacing?: number }; const ToggleGroupContext = React.createContext<ToggleGroupContextValue>({ size: "default", variant: "default", spacing: 0, }); function ToggleGroup({ className, variant, size, spacing = 0, children, ...props }: React.ComponentProps<typeof ToggleGroupPrimitive.Root> & VariantProps<typeof toggleVariants> & { /** Gap between items in Tailwind spacing units. 0 joins them into one bar. */ spacing?: number; }) { return ( <ToggleGroupPrimitive.Root data-slot="toggle-group" data-variant={variant} data-size={size} data-spacing={spacing} style={{ "--gap": spacing } as React.CSSProperties} className={cn( "group/toggle-group flex w-fit items-center gap-[--spacing(var(--gap))] rounded-md data-[spacing=0]:data-[variant=outline]:shadow-xs", className )} {...props} > <ToggleGroupContext.Provider value={{ variant, size, spacing }}>{children}</ToggleGroupContext.Provider> </ToggleGroupPrimitive.Root> ); } /** One toggle in the group. Inherits `variant` and `size` from the group unless set. */ function ToggleGroupItem({ className, children, variant, size, ...props }: React.ComponentProps<typeof ToggleGroupPrimitive.Item> & VariantProps<typeof toggleVariants>) { const context = React.useContext(ToggleGroupContext); return ( <ToggleGroupPrimitive.Item data-slot="toggle-group-item" data-variant={context.variant || variant} data-size={context.size || size} data-spacing={context.spacing} className={cn( toggleVariants({ variant: context.variant || variant, size: context.size || size, }), "w-auto min-w-0 shrink-0 px-3 focus:z-10 focus-visible:z-10", "data-[spacing=0]:rounded-none data-[spacing=0]:shadow-none data-[spacing=0]:first:rounded-l-md data-[spacing=0]:last:rounded-r-md", "data-[spacing=0]:data-[variant=outline]:border-l-0 data-[spacing=0]:data-[variant=outline]:first:border-l", className )} {...props} > {children} </ToggleGroupPrimitive.Item> ); } export { ToggleGroup, ToggleGroupItem };
Also installs components/ui/toggle.tsx (the group reuses its toggleVariants). Both keep shadcn's parts, props and data-slot attributes and animate with CSS only.
Usage
import { Bold, Italic, Underline } from "lucide-react"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
export function Formatting() {
return (
<ToggleGroup type="multiple" aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold"><Bold /></ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic"><Italic /></ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline"><Underline /></ToggleGroupItem>
</ToggleGroup>
)
}Examples
Default
type="multiple" lets several items be on at once. Pressed items get a neutral surface. Arrow keys move between items; Space or Enter toggles.
import { Bold, Italic, Strikethrough, Underline } from "lucide-react";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
export default function ToggleGroupBasicDemo() {
return (
<ToggleGroup type="multiple" defaultValue={["bold"]} aria-label="Text formatting">
<ToggleGroupItem value="bold" aria-label="Bold">
<Bold />
</ToggleGroupItem>
<ToggleGroupItem value="italic" aria-label="Italic">
<Italic />
</ToggleGroupItem>
<ToggleGroupItem value="underline" aria-label="Underline">
<Underline />
</ToggleGroupItem>
<ToggleGroupItem value="strikethrough" aria-label="Strikethrough">
<Strikethrough />
</ToggleGroupItem>
</ToggleGroup>
);
}Outline, single & spacing
variant="outline" joins the items into one bordered bar; type="single" keeps one value. Set spacing to separate them, and size for sm / lg.
import { AlignCenter, AlignJustify, AlignLeft, AlignRight } from "lucide-react";
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group";
export default function ToggleGroupOutlineDemo() {
return (
<div className="grid justify-items-center gap-6">
<ToggleGroup type="single" variant="outline" defaultValue="left" aria-label="Text alignment">
<ToggleGroupItem value="left" aria-label="Align left">
<AlignLeft />
</ToggleGroupItem>
<ToggleGroupItem value="center" aria-label="Align center">
<AlignCenter />
</ToggleGroupItem>
<ToggleGroupItem value="right" aria-label="Align right">
<AlignRight />
</ToggleGroupItem>
<ToggleGroupItem value="justify" aria-label="Justify">
<AlignJustify />
</ToggleGroupItem>
</ToggleGroup>
<ToggleGroup type="single" variant="outline" size="sm" spacing={2} defaultValue="week" aria-label="Range">
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="month">Month</ToggleGroupItem>
<ToggleGroupItem value="year" disabled>
Year
</ToggleGroupItem>
</ToggleGroup>
</div>
);
}Toggle
A single two-state button (components/ui/toggle.tsx) with pressed / onPressedChange, the same variants and sizes.
import { Bookmark, Star } from "lucide-react";
import { Toggle } from "@/components/ui/toggle";
export default function ToggleDemo() {
return (
<div className="flex items-center gap-3">
<Toggle aria-label="Star repository" defaultPressed>
<Star />
</Toggle>
<Toggle variant="outline" aria-label="Bookmark">
<Bookmark /> Bookmark
</Toggle>
<Toggle size="sm" disabled aria-label="Disabled">
<Star />
</Toggle>
</div>
);
}Blocks
Ready-made variations composed from the base component with Motion. Each installs on its own and pulls in its dependencies automatically.
Animated Toggle Group
Options sit on a recessed track. With type="single" one pill glides to the pressed item (a shared Motion layoutId); with type="multiple" each pressed item gets its own pill that fades in and out. Typed like Radix: value is a string for single and a string array for multiple.
View: grid · Marks: bold
"use client";
import * as React from "react";
import { Bold, Code, Italic, LayoutGrid, List, Rows3, Underline } from "lucide-react";
import {
AnimatedToggleGroup,
AnimatedToggleGroupItem,
} from "@/components/dusk/toggle-group/animated-toggle-group";
export default function AnimatedToggleGroupDemo() {
const [view, setView] = React.useState("grid");
const [marks, setMarks] = React.useState<string[]>(["bold"]);
return (
<div className="grid justify-items-center gap-6">
<AnimatedToggleGroup type="single" value={view} onValueChange={(v) => v && setView(v)} aria-label="View">
<AnimatedToggleGroupItem value="grid">
<LayoutGrid /> Grid
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="list">
<List /> List
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="compact">
<Rows3 /> Compact
</AnimatedToggleGroupItem>
</AnimatedToggleGroup>
<AnimatedToggleGroup type="multiple" value={marks} onValueChange={setMarks} aria-label="Text formatting">
<AnimatedToggleGroupItem value="bold" aria-label="Bold">
<Bold />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="italic" aria-label="Italic">
<Italic />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="underline" aria-label="Underline">
<Underline />
</AnimatedToggleGroupItem>
<AnimatedToggleGroupItem value="code" aria-label="Inline code">
<Code />
</AnimatedToggleGroupItem>
</AnimatedToggleGroup>
<p className="text-xs text-muted-foreground" aria-live="polite">
View: <span className="text-foreground">{view}</span> · Marks:{" "}
<span className="text-foreground">{marks.length ? marks.join(", ") : "none"}</span>
</p>
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-toggle-group.json1. Install the dependencies.
npm install motion @radix-ui/react-toggle-group2. Copy the source into
components/dusk/toggle-group/animated-toggle-group.tsx.animated-toggle-group.tsx "use client"; import * as React from "react"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { cn } from "@/lib/utils"; const pillSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; type Size = "sm" | "default" | "lg"; type AnimatedToggleGroupContextValue = { type: "single" | "multiple"; selected: string[]; size: Size; layoutId: string; reduceMotion: boolean; }; const AnimatedToggleGroupContext = React.createContext<AnimatedToggleGroupContextValue | null>(null); function useAnimatedToggleGroup() { const context = React.useContext(AnimatedToggleGroupContext); if (!context) throw new Error("AnimatedToggleGroupItem must be used inside <AnimatedToggleGroup>."); return context; } type BaseProps = Omit< React.ComponentProps<typeof ToggleGroup>, "type" | "value" | "defaultValue" | "onValueChange" | "variant" | "spacing" > & { size?: Size; }; type SingleProps = BaseProps & { type: "single"; value?: string; defaultValue?: string; onValueChange?: (value: string) => void; }; type MultipleProps = BaseProps & { type: "multiple"; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; }; export type AnimatedToggleGroupProps = SingleProps | MultipleProps; const toArray = (value: string | string[] | undefined) => Array.isArray(value) ? value : value ? [value] : []; /** * A toggle group on a recessed track. With `type="single"` one pill * glides to the pressed item (a shared Motion `layoutId`); with * `type="multiple"` every pressed item gets its own pill that springs in and * shrinks away. Built on the DuskUI Toggle Group (Radix), so arrow keys move * focus, Space / Enter toggle and the state is exposed with aria-pressed (or * aria-checked for single). Under reduced motion the pills appear instantly. */ export function AnimatedToggleGroup(props: AnimatedToggleGroupProps) { const { type, value: valueProp, defaultValue, onValueChange, size = "default", className, children, ...rest } = props; const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState<string[]>(() => toArray(defaultValue)); const selected = valueProp !== undefined ? toArray(valueProp) : uncontrolled; const handleChange = (next: string | string[]) => { if (valueProp === undefined) setUncontrolled(toArray(next)); (onValueChange as ((value: string | string[]) => void) | undefined)?.(next); }; const context = { type, selected, size, layoutId: `${id}-pill`, reduceMotion }; const groupProps = type === "single" ? { type, value: selected[0] ?? "", onValueChange: handleChange } : { type, value: selected, onValueChange: handleChange }; return ( <AnimatedToggleGroupContext.Provider value={context}> <LayoutGroup id={id}> <ToggleGroup {...groupProps} size={size} spacing={1} className={cn( "relative isolate rounded-lg bg-muted p-1", className )} {...rest} > {children} </ToggleGroup> </LayoutGroup> </AnimatedToggleGroupContext.Provider> ); } const itemSizes: Record<Size, string> = { sm: "h-7 min-w-7 px-2 text-xs", default: "h-8 min-w-8 px-2.5", lg: "h-9 min-w-9 px-3", }; /** One toggle. Children are its label and/or icon; give icon-only items an aria-label. */ export function AnimatedToggleGroupItem({ value, className, children, ...props }: React.ComponentProps<typeof ToggleGroupItem>) { const { type, selected, size, layoutId, reduceMotion } = useAnimatedToggleGroup(); const on = selected.includes(value); const pillClass = "pointer-events-none absolute inset-0 -z-10 rounded-md border border-white/10 bg-background shadow-sm"; return ( <ToggleGroupItem value={value} className={cn( "group/item relative isolate rounded-md border-0 shadow-none", // The pill replaces the base "on" surface. "hover:bg-white/[0.04] data-[state=on]:bg-transparent data-[state=on]:text-foreground data-[state=on]:shadow-none data-[state=on]:hover:bg-transparent", itemSizes[size], className )} {...props} > {type === "single" ? ( on && ( <motion.span aria-hidden layoutId={layoutId} transition={reduceMotion ? { duration: 0 } : pillSpring} className={pillClass} /> ) ) : ( <AnimatePresence initial={false}> {on && ( <motion.span key="pill" aria-hidden initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.96 }} animate={{ opacity: 1, scale: 1 }} exit={reduceMotion ? { opacity: 0, transition: { duration: 0.1 } } : { opacity: 0, scale: 0.96, transition: { duration: 0.12 } }} transition={reduceMotion ? { duration: 0.1 } : { type: "spring", bounce: 0.1, duration: 0.2 }} className={pillClass} /> )} </AnimatePresence> )} {children} </ToggleGroupItem> ); }
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | — | One value or many. Required. |
| value / onValueChange | string | string[] / (value) => void | — | Controlled value; a string for single, an array for multiple. |
| defaultValue | string | string[] | — | Uncontrolled initial value. |
| size | "sm" | "default" | "lg" | "default" | Item height and padding. |
| disabled / orientation / rovingFocus / loop | Radix ToggleGroup props | — | Forwarded to the Radix root. |
| AnimatedToggleGroupItem | ToggleGroupItem props | — | An item. Icon-only items need an aria-label. |
Animated Toggle Group — sizes
size="sm", default and lg — handy as a chart range picker. Each group gets its own pill, so several can live on one page.
import {
AnimatedToggleGroup,
AnimatedToggleGroupItem,
} from "@/components/dusk/toggle-group/animated-toggle-group";
const periods = ["1D", "1W", "1M", "1Y", "All"];
export default function AnimatedToggleGroupSizesDemo() {
return (
<div className="grid justify-items-center gap-4">
{(["sm", "default", "lg"] as const).map((size) => (
<AnimatedToggleGroup key={size} type="single" size={size} defaultValue="1M" aria-label={`Chart range (${size})`}>
{periods.map((p) => (
<AnimatedToggleGroupItem key={p} value={p} className="tabular-nums">
{p}
</AnimatedToggleGroupItem>
))}
</AnimatedToggleGroup>
))}
</div>
);
}$ npx shadcn@latest add https://duskui.com/r/animated-toggle-group.json1. Install the dependencies.
npm install motion @radix-ui/react-toggle-group2. Copy the source into
components/dusk/toggle-group/animated-toggle-group.tsx.animated-toggle-group.tsx "use client"; import * as React from "react"; import { AnimatePresence, LayoutGroup, motion, useReducedMotion, type Transition } from "motion/react"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { cn } from "@/lib/utils"; const pillSpring: Transition = { type: "spring", bounce: 0.1, duration: 0.3 }; type Size = "sm" | "default" | "lg"; type AnimatedToggleGroupContextValue = { type: "single" | "multiple"; selected: string[]; size: Size; layoutId: string; reduceMotion: boolean; }; const AnimatedToggleGroupContext = React.createContext<AnimatedToggleGroupContextValue | null>(null); function useAnimatedToggleGroup() { const context = React.useContext(AnimatedToggleGroupContext); if (!context) throw new Error("AnimatedToggleGroupItem must be used inside <AnimatedToggleGroup>."); return context; } type BaseProps = Omit< React.ComponentProps<typeof ToggleGroup>, "type" | "value" | "defaultValue" | "onValueChange" | "variant" | "spacing" > & { size?: Size; }; type SingleProps = BaseProps & { type: "single"; value?: string; defaultValue?: string; onValueChange?: (value: string) => void; }; type MultipleProps = BaseProps & { type: "multiple"; value?: string[]; defaultValue?: string[]; onValueChange?: (value: string[]) => void; }; export type AnimatedToggleGroupProps = SingleProps | MultipleProps; const toArray = (value: string | string[] | undefined) => Array.isArray(value) ? value : value ? [value] : []; /** * A toggle group on a recessed track. With `type="single"` one pill * glides to the pressed item (a shared Motion `layoutId`); with * `type="multiple"` every pressed item gets its own pill that springs in and * shrinks away. Built on the DuskUI Toggle Group (Radix), so arrow keys move * focus, Space / Enter toggle and the state is exposed with aria-pressed (or * aria-checked for single). Under reduced motion the pills appear instantly. */ export function AnimatedToggleGroup(props: AnimatedToggleGroupProps) { const { type, value: valueProp, defaultValue, onValueChange, size = "default", className, children, ...rest } = props; const id = React.useId(); const reduceMotion = useReducedMotion() ?? false; const [uncontrolled, setUncontrolled] = React.useState<string[]>(() => toArray(defaultValue)); const selected = valueProp !== undefined ? toArray(valueProp) : uncontrolled; const handleChange = (next: string | string[]) => { if (valueProp === undefined) setUncontrolled(toArray(next)); (onValueChange as ((value: string | string[]) => void) | undefined)?.(next); }; const context = { type, selected, size, layoutId: `${id}-pill`, reduceMotion }; const groupProps = type === "single" ? { type, value: selected[0] ?? "", onValueChange: handleChange } : { type, value: selected, onValueChange: handleChange }; return ( <AnimatedToggleGroupContext.Provider value={context}> <LayoutGroup id={id}> <ToggleGroup {...groupProps} size={size} spacing={1} className={cn( "relative isolate rounded-lg bg-muted p-1", className )} {...rest} > {children} </ToggleGroup> </LayoutGroup> </AnimatedToggleGroupContext.Provider> ); } const itemSizes: Record<Size, string> = { sm: "h-7 min-w-7 px-2 text-xs", default: "h-8 min-w-8 px-2.5", lg: "h-9 min-w-9 px-3", }; /** One toggle. Children are its label and/or icon; give icon-only items an aria-label. */ export function AnimatedToggleGroupItem({ value, className, children, ...props }: React.ComponentProps<typeof ToggleGroupItem>) { const { type, selected, size, layoutId, reduceMotion } = useAnimatedToggleGroup(); const on = selected.includes(value); const pillClass = "pointer-events-none absolute inset-0 -z-10 rounded-md border border-white/10 bg-background shadow-sm"; return ( <ToggleGroupItem value={value} className={cn( "group/item relative isolate rounded-md border-0 shadow-none", // The pill replaces the base "on" surface. "hover:bg-white/[0.04] data-[state=on]:bg-transparent data-[state=on]:text-foreground data-[state=on]:shadow-none data-[state=on]:hover:bg-transparent", itemSizes[size], className )} {...props} > {type === "single" ? ( on && ( <motion.span aria-hidden layoutId={layoutId} transition={reduceMotion ? { duration: 0 } : pillSpring} className={pillClass} /> ) ) : ( <AnimatePresence initial={false}> {on && ( <motion.span key="pill" aria-hidden initial={reduceMotion ? { opacity: 0 } : { opacity: 0, scale: 0.96 }} animate={{ opacity: 1, scale: 1 }} exit={reduceMotion ? { opacity: 0, transition: { duration: 0.1 } } : { opacity: 0, scale: 0.96, transition: { duration: 0.12 } }} transition={reduceMotion ? { duration: 0.1 } : { type: "spring", bounce: 0.1, duration: 0.2 }} className={pillClass} /> )} </AnimatePresence> )} {children} </ToggleGroupItem> ); }
API Reference
Same parts as shadcn/ui: ToggleGroup and ToggleGroupItem (plus Toggle and toggleVariants), each forwarding its Radix props.
| Prop | Type | Default | Description |
|---|---|---|---|
| type | "single" | "multiple" | — | On ToggleGroup. Required. |
| value / onValueChange | string | string[] / (value) => void | — | Controlled value (string for single, array for multiple). |
| variant | "default" | "outline" | "default" | Ghost items or a bordered bar. Inherited by items. |
| size | "sm" | "default" | "lg" | "default" | Item size. Inherited by items. |
| spacing | number | 0 | Gap between items in spacing units; 0 joins them. |
| pressed / onPressedChange | boolean / (pressed: boolean) => void | — | On Toggle: controlled state. |
Accessibility
- Built on Radix Toggle Group: one Tab stop with roving focus, arrow keys between items, Space / Enter to toggle.
- Multiple groups expose aria-pressed on each button; single groups use radio semantics (role="radio" + aria-checked).
- Name the group with aria-label and give icon-only items an aria-label of their own.
- The gliding pills are decorative (aria-hidden, pointer-events-none).
- Under prefers-reduced-motion the pills appear and move instantly.