Motion button groups
Five action groups that share one structure and vary the interaction motion: a sliding indicator, a press pop, a fade, a spring press and a morphing highlight. Each is self-contained, sized, token-driven and respects reduced motion.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/button-group-motionDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motionCopy the source
components/ui/button-group-motion.tsx"use client"
import * as React from "react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Motion button-group family: five animated action groups. The structure stays
the same (a container with buttons inside); the difference is in the
INTERACTION motion: a sliding indicator, a pop on press, a soft fade, a springy
return, a morphing highlight. When a moving button is pressed the active index
is held in state; the state lives in the root component and no subtree ever
unmounts. All motion is disabled through useReducedMotion(). Color comes ONLY
from semantic tokens, via alpha color-mix. Deterministic: no randomness or
source of time. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
/* Base Button olcegiyle hizali: sm=h-8, md=h-9, lg=h-10, xl=h-12. */
const sizes: Record<StyledSize, string> = {
sm: "h-8 px-3 text-sm",
md: "h-9 px-4 text-sm",
lg: "h-10 px-5 text-sm",
xl: "h-12 px-6 text-base",
}
type Action = {
label?: React.ReactNode
icon?: React.ReactNode
onClick?: () => void
}
type GroupProps = {
className?: string
size?: StyledSize
actions?: Action[]
}
const defaultActions: Action[] = [
{ label: "Left" },
{ label: "Center" },
{ label: "Right" },
]
const item =
"relative inline-flex shrink-0 select-none items-center justify-center gap-2 rounded-lg font-medium whitespace-nowrap outline-none transition-colors focus-visible:z-10 focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
const shell =
"inline-flex items-center gap-1 rounded-xl border border-field-border bg-background p-1 isolate"
const spring = { type: "spring" as const, stiffness: 420, damping: 32 }
/* Slide: basilan butonun arkasina kayan bir gosterge tasinir. */
export function SlideButtonGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const [active, setActive] = React.useState(0)
const reduce = useReducedMotion()
const uid = React.useId()
return (
<div
data-slot="styled-button-group"
role="group"
aria-label="Slide actions"
className={cn(shell, className)}
>
{actions.map((action, i) => (
<button
key={i}
type="button"
onClick={() => {
setActive(i)
action.onClick?.()
}}
data-slot="styled-button-group-item"
className={cn(
item,
sizes[size],
active === i ? "text-foreground" : "text-muted-foreground hover:text-foreground"
)}
>
{active === i ? (
<motion.span
layoutId={reduce ? undefined : `${uid}-slide`}
aria-hidden="true"
className="absolute inset-0 -z-10 rounded-lg bg-secondary"
transition={reduce ? { duration: 0 } : spring}
/>
) : null}
<span className="relative inline-flex items-center gap-2">
{action.icon}
{action.label}
</span>
</button>
))}
</div>
)
}
/* Pop: basinca kisa bir olcek darbesi, birakinca geri doner. */
export function PopButtonGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const reduce = useReducedMotion()
return (
<div
data-slot="styled-button-group"
role="group"
aria-label="Pop actions"
className={cn(shell, className)}
>
{actions.map((action, i) => (
<motion.button
key={i}
type="button"
onClick={action.onClick}
data-slot="styled-button-group-item"
whileTap={reduce ? undefined : { scale: 0.92 }}
whileHover={reduce ? undefined : { scale: 1.04 }}
transition={spring}
className={cn(
item,
sizes[size],
"text-foreground hover:bg-secondary hover:text-secondary-foreground"
)}
>
{action.icon}
{action.label}
</motion.button>
))}
</div>
)
}
/* Fade: only the opacity changes on hover and press, no transform. */
export function FadeButtonGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const reduce = useReducedMotion()
return (
<div
data-slot="styled-button-group"
role="group"
aria-label="Fade actions"
className={cn(shell, className)}
>
{actions.map((action, i) => (
<motion.button
key={i}
type="button"
onClick={action.onClick}
data-slot="styled-button-group-item"
initial={false}
whileHover={reduce ? undefined : { opacity: 1 }}
whileTap={reduce ? undefined : { opacity: 0.6 }}
transition={{ duration: reduce ? 0 : 0.18, ease: "easeOut" }}
className={cn(
item,
sizes[size],
"text-foreground opacity-70 hover:bg-secondary hover:text-secondary-foreground"
)}
>
{action.icon}
{action.label}
</motion.button>
))}
</div>
)
}
/* Spring: it pulls down on press and settles back with a spring on release. */
export function SpringButtonGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const reduce = useReducedMotion()
return (
<div
data-slot="styled-button-group"
role="group"
aria-label="Spring actions"
className={cn(shell, className)}
>
{actions.map((action, i) => (
<motion.button
key={i}
type="button"
onClick={action.onClick}
data-slot="styled-button-group-item"
whileTap={reduce ? undefined : { y: 2 }}
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 600, damping: 18 }}
className={cn(
item,
sizes[size],
"border border-field-border text-foreground hover:bg-secondary hover:text-secondary-foreground"
)}
>
{action.icon}
{action.label}
</motion.button>
))}
</div>
)
}
/* Morph: the active highlight moves from one button to another by changing shape. */
export function MorphButtonGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const [active, setActive] = React.useState(0)
const reduce = useReducedMotion()
const uid = React.useId()
return (
<div
data-slot="styled-button-group"
role="group"
aria-label="Morph actions"
className={cn(
"inline-flex items-center gap-1 rounded-full border border-field-border bg-secondary/40 p-1 isolate",
className
)}
>
{actions.map((action, i) => (
<button
key={i}
type="button"
onClick={() => {
setActive(i)
action.onClick?.()
}}
data-slot="styled-button-group-item"
className={cn(
item,
sizes[size],
"rounded-full",
active === i
? "text-primary-foreground"
: "text-muted-foreground hover:text-foreground"
)}
>
{active === i ? (
<motion.span
layoutId={reduce ? undefined : `${uid}-morph`}
aria-hidden="true"
className="absolute inset-0 -z-10 rounded-full bg-primary"
transition={reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 380, damping: 30 }}
/>
) : null}
<span className="relative inline-flex items-center gap-2">
{action.icon}
{action.label}
</span>
</button>
))}
</div>
)
}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
An indicator slides behind the button you press.
import { SlideButtonGroup } from "@/components/ui/button-group-motion"
<SlideButtonGroup />Pop
A short scale pulse on press and a lift on hover.
import { PopButtonGroup } from "@/components/ui/button-group-motion"
<PopButtonGroup />Fade
Opacity alone reacts; no transform is animated.
import { FadeButtonGroup } from "@/components/ui/button-group-motion"
<FadeButtonGroup />Spring
The button pushes down and springs back on release.
import { SpringButtonGroup } from "@/components/ui/button-group-motion"
<SpringButtonGroup />Morph
A pill highlight morphs from one button to the next.
import { MorphButtonGroup } from "@/components/ui/button-group-motion"
<MorphButtonGroup />ai2 Motion button groups: 5 styled variations on the token system
The ai2 Motion button groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around grouped action buttons with press and hover motion. 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 drives the press and hover states, and a shared layout indicator moves between buttons. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the transforms and the sliding indicator are skipped, and the state still changes instantly.
What is in the ai2 Motion button 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 drives the press and hover states, and a shared layout indicator moves between buttons.
- Reduced-motion aware: Under prefers-reduced-motion, the transforms and the sliding indicator are skipped, and the state still changes instantly.
- 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 button 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.