Motion toggle groups
Five single-select toggle groups that share one idea: the active indicator travels between options instead of jumping. Only the transition character changes, from a soft slide to a bouncy spring or a morphing underline. Each is self-contained, sized, token-driven, and every button is a real button with aria-pressed.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/toggle-group-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggle-group-motion.tsx"use client"
import * as React from "react"
import { AlignCenter, AlignLeft, AlignRight } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion toggle group family: 5 decorative single-select controls. The shared idea
is carrying the ACTIVE INDICATOR between options with animation: because the
indicator is shared through a framer `layoutId` it slides to the new option rather
than jumping. The variants only change the transition character of the indicator
(a soft slide, pop, dissolve, a springy hop, a morphing bar). Every button is a
real <button> + aria-pressed + a focus-visible ring. Color comes ONLY from
semantic tokens; alpha via color-mix. While useReducedMotion is on the indicator
moves instantly. The layoutId is unique per instance via React.useId(), so groups
on the same page do not leak animation into each other. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
type Option = { value: string; label?: React.ReactNode; icon?: React.ReactNode }
type ToggleGroupProps = {
className?: string
size?: StyledSize
type?: "single" | "multiple"
options?: Option[]
value?: string | string[]
defaultValue?: string | string[]
onValueChange?: (v: string | string[]) => void
}
const sizeBtn: Record<StyledSize, string> = {
sm: "h-8 px-3 text-xs",
md: "h-9 px-4 text-sm",
lg: "h-10 px-5 text-sm",
xl: "h-11 px-6 text-base",
}
const btnBase =
"relative z-10 inline-flex select-none items-center justify-center gap-2 whitespace-nowrap font-medium outline-none transition-colors focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const TEXT_OPTIONS: Option[] = [
{ value: "left", label: "Left", icon: <AlignLeft /> },
{ value: "center", label: "Center", icon: <AlignCenter /> },
{ value: "right", label: "Right", icon: <AlignRight /> },
]
function toArray(v: string | string[] | undefined): string[] {
if (v === undefined) return []
return Array.isArray(v) ? v : [v]
}
/* Kontrollu/kontrolsuz secim durumu. Tek-secimde varsayilan ilk secenektir. */
function useToggleGroup(props: ToggleGroupProps, fallback: Option[]) {
const { type = "single", options, value, defaultValue, onValueChange } = props
const list = React.useMemo(() => (options && options.length ? options : fallback), [options, fallback])
const controlled = value !== undefined
const [internal, setInternal] = React.useState<string[]>(() => {
const init = toArray(defaultValue)
if (init.length === 0 && type === "single" && defaultValue === undefined) {
return list[0] ? [list[0].value] : []
}
return init
})
const selected = controlled ? toArray(value) : internal
const emit = React.useCallback(
(next: string[]) => {
if (!controlled) setInternal(next)
onValueChange?.(type === "single" ? (next[0] ?? "") : next)
},
[controlled, onValueChange, type]
)
const toggle = React.useCallback(
(v: string) => {
const on = selected.includes(v)
if (type === "single") {
emit(on ? [] : [v])
} else {
emit(on ? selected.filter((s) => s !== v) : [...selected, v])
}
},
[selected, type, emit]
)
const isOn = React.useCallback((v: string) => selected.includes(v), [selected])
return { list, isOn, toggle }
}
type Spec = {
/** Root shell class. */
root: string
/** Button class. */
item: string
/** Aktif dugme metin rengi. */
onText: string
/** Kapali dugme metin rengi. */
offText: string
/** Indicator class. */
indicator: string
/** The indicator's enter/exit animation (skipped under reduced motion). */
pop?: boolean
/** Gosterge gecis ayari. */
transition: Record<string, unknown>
/** Erisilebilir ad. */
label: string
}
/* The shared shell: only the indicator transition and the surface change. */
function MotionGroup({ props, spec }: { props: ToggleGroupProps; spec: Spec }) {
const { className, size = "md" } = props
const { list, isOn, toggle } = useToggleGroup({ ...props, type: "single" }, TEXT_OPTIONS)
const id = React.useId()
const reduce = useReducedMotion()
const transition = reduce ? { duration: 0 } : spec.transition
return (
<div
data-slot="styled-toggle-group-viewport"
className="-m-1 flex max-w-full overflow-x-auto p-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
>
<div
data-slot="styled-toggle-group"
role="group"
aria-label={spec.label}
className={cn(spec.root, className)}
>
{list.map((o) => {
const on = isOn(o.value)
return (
<button
key={o.value}
type="button"
aria-pressed={on}
data-slot="styled-toggle-group-item"
onClick={() => toggle(o.value)}
className={cn(btnBase, sizeBtn[size], spec.item, on ? spec.onText : spec.offText)}
>
{on && (
<motion.span
layoutId={`${id}-motion-indicator`}
transition={transition}
initial={spec.pop && !reduce ? { opacity: 0, scale: 0.86 } : false}
animate={{ opacity: 1, scale: 1 }}
className={cn("absolute inset-0 -z-10", spec.indicator)}
/>
)}
{o.icon}
{o.label}
</button>
)
})}
</div>
</div>
)
}
/* Slide: gosterge yumusak bir yayla yeni segmente kayar. Klasik segment kontrol. */
export function SlideToggleGroup(props: ToggleGroupProps) {
return (
<MotionGroup
props={props}
spec={{
label: "Text alignment, sliding indicator",
root: "inline-flex items-center gap-0.5 rounded-lg border border-border bg-muted p-1",
item: "rounded-md",
onText: "text-foreground",
offText: "text-muted-foreground hover:text-foreground",
indicator: "rounded-md border border-border bg-background shadow-sm",
transition: { type: "spring" as const, stiffness: 420, damping: 34 },
}}
/>
)
}
/* Pop: the indicator grows slightly as it slides and settles into place. */
export function PopToggleGroup(props: ToggleGroupProps) {
return (
<MotionGroup
props={props}
spec={{
label: "Text alignment, popping indicator",
root: "inline-flex items-center gap-0.5 rounded-lg bg-surface-2 p-1",
item: "rounded-md",
onText: "text-primary-foreground",
offText: "text-muted-foreground hover:text-foreground",
indicator: "rounded-md bg-primary shadow-sm",
pop: true,
transition: { type: "spring" as const, stiffness: 520, damping: 26 },
}}
/>
)
}
/* Fade: gosterge kayarken opaklikla cozulur; en sakin gecis. */
export function FadeToggleGroup(props: ToggleGroupProps) {
return (
<MotionGroup
props={props}
spec={{
label: "Text alignment, fading indicator",
root: "inline-flex items-center gap-0.5 rounded-lg border border-border bg-background p-1",
item: "rounded-md",
onText: "text-primary",
offText: "text-muted-foreground hover:text-foreground",
indicator:
"rounded-md bg-[color-mix(in_oklab,var(--color-primary)_14%,transparent)] ring-1 ring-[color-mix(in_oklab,var(--color-primary)_30%,transparent)]",
pop: true,
transition: { duration: 0.28, ease: "easeOut" },
}}
/>
)
}
/* Spring: yuksek geri tepmeli yay; gosterge hedefin etrafinda salinir. */
export function SpringToggleGroup(props: ToggleGroupProps) {
return (
<MotionGroup
props={props}
spec={{
label: "Text alignment, springy indicator",
root: "inline-flex items-center gap-0.5 rounded-full border border-border bg-muted p-1",
item: "rounded-full",
onText: "text-foreground",
offText: "text-muted-foreground hover:text-foreground",
indicator: "rounded-full border border-border bg-background shadow-sm",
transition: { type: "spring" as const, stiffness: 700, damping: 18, mass: 0.7 },
}}
/>
)
}
/* Morph: the indicator is not a filled box but a thin bar sliding under the active
option. */
export function MorphToggleGroup(props: ToggleGroupProps) {
return (
<MotionGroup
props={props}
spec={{
label: "Text alignment, morphing underline",
root: "inline-flex items-center gap-1 border-b border-border",
item: "rounded-none rounded-t-md",
onText: "text-foreground",
offText: "text-muted-foreground hover:text-foreground",
indicator:
"rounded-t-md bg-[color-mix(in_oklab,var(--color-foreground)_6%,transparent)] after:absolute after:inset-x-0 after:bottom-0 after:h-0.5 after:rounded-full after:bg-primary",
transition: { type: "spring" as const, stiffness: 460, damping: 32 },
}}
/>
)
}Manual installs skip the @ai2/tokens theme, so add the token CSS from the theming guide or the tone colors will be missing.
Variations
5 takes on the same idea. Each is its own export, and every one accepts a size prop (sm, md, lg, xl) aligned to the base Button scale.
Slide
A soft spring slides the indicator to the new segment.
import { SlideToggleGroup } from "@/components/ui/toggle-group-motion"
<SlideToggleGroup />Pop
The indicator scales up as it lands on the option.
import { PopToggleGroup } from "@/components/ui/toggle-group-motion"
<PopToggleGroup />Fade
The tinted indicator dissolves across on an eased tween.
import { FadeToggleGroup } from "@/components/ui/toggle-group-motion"
<FadeToggleGroup />Spring
A bouncy spring lets the pill overshoot and settle.
import { SpringToggleGroup } from "@/components/ui/toggle-group-motion"
<SpringToggleGroup />Morph
A thin underline bar travels beneath the active option.
import { MorphToggleGroup } from "@/components/ui/toggle-group-motion"
<MorphToggleGroup />ai2 Motion toggle groups: 5 styled variations on the token system
The ai2 Motion toggle groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around toggle groups whose active indicator animates between options. They are free and MIT licensed, and every color comes from a semantic token, so they theme with the rest of ai2 in light and dark.
Motion runs on framer-motion: framer-motion shares a layoutId, so the indicator moves to the newly selected option. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the indicator changes position instantly with no animation.
What is in the ai2 Motion toggle groups?
5 exports in one file: Slide, Pop, Fade, Spring and Morph. Each renders a native button and takes a size prop (sm, md, lg, xl) aligned to the base Button. They are separate from the base Button on purpose: the base keeps its clean variant, tone and size axes, while the styled layer carries the effects.
You own the file. Copy the one category file and you have all 5 variations, with no runtime dependency on ai2 itself.
Why use it
- On-system by construction: Every color resolves to an ai2 semantic token, so the buttons follow your theme in light and dark with no extra work.
- Effect without the sprawl: The decorations live in a dedicated styled file, so the base Button keeps its clean, predictable API.
- Accessible and honest: Each renders a real button element, keeps a visible focus ring, and respects prefers-reduced-motion.
Features
- Token-driven color: No hardcoded hex or oklch; the look recolors with your theme tokens.
- framer-motion: framer-motion shares a layoutId, so the indicator moves to the newly selected option.
- Reduced-motion aware: Under prefers-reduced-motion, the indicator changes position instantly with no animation.
- Size aligned to the base: Every variation takes sm, md, lg and xl matching the base Button height scale, so styled and base buttons line up in a row.
Production tips
- Use it for emphasis, not everywhere: Styled buttons draw the eye. Reserve them for the one action you want people to take on a screen, and use the base Button for the rest.
- Keep labels as verbs: The decoration adds weight, so a clear action label keeps the button scannable.
- Pick one variation per surface: The variations share a family; using two different ones in the same view competes for attention.
Works with the rest of ai2
The Motion toggle groups sit alongside the base Button and the rest of the @ai2 registry. They share the same token file, so a styled action next to a base button or a badge stays visually consistent in both modes.