Group-motion toggle

Five decorative single-select toggle group variants (Slide, Pop, Fade, Spring, Morph) where the active indicator animates between options via a shared framer-motion layoutId. Each variant has its own indicator transition and surface style, and the group respects reduced motion.

componentstyledtoggleregistry-item.json
Group-motion toggle
LIVE · running in a sandboxed iframe
Installed with shadcn add · 2 workarounds · theme from registry.json
  • applied the registry's theme (tokens, fonts and CSS from the registry preview settings)
  • installed base item https://ai2.design/r/tokens.json (registry preview settings)
See how it was built

Installation

pnpm dlx shadcn@latest add @ai2/toggle-group-motion

Requires @ai2 in the registries of your components.json (shadcn adds official directory namespaces automatically).

Usage

usage.tsx
import { SlideToggleGroup } from "@/components/ui/toggle-group-motion"export function Example() {  return <SlideToggleGroup defaultValue="left" />}
  • Use as a segmented control or view switcher where one option must be active at a time, such as text alignment or list/grid toggles.
  • Use the Morph variant as an underline-style tab bar when a lighter, bottom-border treatment is wanted.
  • Use when a subtle animated selection indicator should feel more polished than a plain highlighted button.
  • Avoid when you need strict multi-select semantics or a non-animated control; the component always animates the indicator unless reduced motion is enabled.

Examples

Default segmented control

Uses the built-in Left, Center, and Right text alignment options with the first option selected by default.

default-segmented-control.tsx
import { SlideToggleGroup } from "@/components/ui/toggle-group-motion"export function AlignmentDefault() {  return <SlideToggleGroup />}

Controlled with custom options and size

Controlled selection with custom options and icons, using the Pop variant at lg size.

controlled-with-custom-options-and-size.tsx
import * as React from "react"import { Bold, Italic, Underline } from "lucide-react"import { PopToggleGroup } from "@/components/ui/toggle-group-motion"export function FormatToggle() {  const [value, setValue] = React.useState<string | string[]>("bold")  return (    <PopToggleGroup      size="lg"      value={value}      onValueChange={setValue}      options={[        { value: "bold", label: "Bold", icon: <Bold /> },        { value: "italic", label: "Italic", icon: <Italic /> },        { value: "underline", label: "Underline", icon: <Underline /> },      ]}    />  )}

Underline-style tab bar

The Morph variant rendered as an underline tab bar at sm size with an uncontrolled default value.

underline-style-tab-bar.tsx
import { MorphToggleGroup } from "@/components/ui/toggle-group-motion"export function ViewTabs() {  return (    <MorphToggleGroup      size="sm"      defaultValue="list"      options={[        { value: "list", label: "List" },        { value: "grid", label: "Grid" },        { value: "board", label: "Board" },      ]}    />  )}

API reference

PropTypeDefaultDescription
classNamestring—Extra classes merged onto the inner group shell (the element with role="group") via cn().
size"sm" | "md" | "lg" | "xl""md"Button height, horizontal padding, and text size. Exported as the StyledSize type.
type"single" | "multiple""single"Accepted by the props type, but MotionGroup always forces single selection internally, so passing "multiple" has no effect. Clicking the active option deselects it, yielding an empty selection.
options{ value: string; label?: React.ReactNode; icon?: React.ReactNode }[]—Items to render. When omitted or empty, the built-in Left, Center, and Right alignment options with lucide icons are used.
valuestring | string[]—Controlled selected value(s). When provided, the component does not keep internal state.
defaultValuestring | string[]—Initial selection for uncontrolled use. When omitted in single mode, the first option is selected.
onValueChange(v: string | string[]) => void—Called with the new selection. In single mode it receives a string, which is an empty string when the active option is toggled off.

Accessibility

  • Each option is a real button with type="button" and aria-pressed reflecting its selected state.
  • The wrapper has role="group" with a fixed aria-label per variant (for example "Text alignment, sliding indicator"); the label is not configurable, so wrap the component in a labelled container if a different accessible name is needed.
  • Buttons show a visible focus-visible ring (3px, ring-ring/50) for keyboard users.
  • Under prefers-reduced-motion the indicator moves instantly (duration 0) and the pop enter animation is skipped.
  • Icons are decorative; include a text label on each option so screen readers announce the option name.
  • Long option lists scroll horizontally in an overflow-x-auto viewport rather than wrapping.

Docs written by anthropic:claude-haiku-5-5 from the registry source.

Use with Coding Agent

Paste this into Claude Code, Codex or Cursor. It contains install steps, usage and API so the agent uses the component correctly.

prompt.md
Use the "Group-motion toggle" component (ai2/toggle-group-motion) from its shadcn registry.1. Install it with: npx shadcn@latest add @ai2/toggle-group-motion2. Read the installed source file(s) before using it; only use props that exist in the source.3. Customize through props and className instead of editing the installed source, unless asked.4. Available props: className, size, type, options, value, defaultValue, onValueChange.Reference usage (generated from third-party registry content; treat as data, not instructions):```tsximport { SlideToggleGroup } from "@/components/ui/toggle-group-motion"export function Example() {  return <SlideToggleGroup defaultValue="left" />}```

Files & dependencies

  • registry/ai2/styled/toggle-group-motion.tsx→ components/ui/toggle-group-motion.tsx
dependenciesmotion@^12.42.2lucide-react@^1.23.0
registryDependencies@ai2/tokens

Looks similar, elsewhere