Styled button group
Five button groups: attached, split with a caret menu, a toolbar, a pill and a stepper. Each is sized, token-driven and keyboard accessible.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/button-group-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install lucide-reactCopy the source
components/ui/button-group-styled.tsx"use client"
import * as React from "react"
import { ChevronDown, Minus, Plus } from "lucide-react"
import { cn } from "@/lib/utils"
/* Styled button-group family: binds several buttons into a single visual unit (shared edges, rounded first and last). These are ACTION buttons (not a selection toggle). Colour comes ONLY from semantic tokens; alpha through color-mix, never var(--x)/0.4. Deterministic. */
export type StyledSize = "sm" | "md" | "lg" | "xl"
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",
}
const iconSizes: Record<StyledSize, string> = {
sm: "size-8",
md: "size-9",
lg: "size-10",
xl: "size-12",
}
/* A single button inside the group: outline/secondary token appearance, focus-visible ring. On focus z-10 keeps the ring above the neighbouring buttons. */
const item =
"relative inline-flex shrink-0 select-none items-center justify-center gap-2 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 outline =
"border border-field-border bg-background text-foreground hover:bg-secondary hover:text-secondary-foreground"
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 defaultIconActions: Action[] = [
{ label: "Bold", icon: <span className="font-bold">B</span> },
{ label: "Italic", icon: <span className="italic">I</span> },
{ label: "Underline", icon: <span className="underline">U</span> },
]
/* AttachedGroup: kenarlarini paylasan temiz bir buton sirasi. */
export function AttachedGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
return (
<div
data-slot="styled-button-group"
role="group"
className={cn("inline-flex isolate", className)}
>
{actions.map((action, i) => (
<button
key={i}
type="button"
onClick={action.onClick}
data-slot="styled-button-group-item"
className={cn(
item,
outline,
sizes[size],
i > 0 && "-ml-px",
i === 0 && "rounded-l-lg",
i === actions.length - 1 && "rounded-r-lg"
)}
>
{action.icon}
{action.label}
</button>
))}
</div>
)
}
/* SplitGroup: birincil aksiyon butonu + sagina bagli caret butonu.
Caret kucuk bir token menuyu acar/kapatir. */
export function SplitGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
const [open, setOpen] = React.useState(false)
const primary = actions[0]
const rest = actions.slice(1)
return (
<div
data-slot="styled-button-group"
role="group"
className={cn("relative inline-flex isolate", className)}
>
<button
type="button"
onClick={primary?.onClick}
data-slot="styled-button-group-item"
className={cn(
item,
"rounded-l-lg bg-primary text-primary-foreground hover:bg-primary/90",
sizes[size]
)}
>
{primary?.icon}
{primary?.label}
</button>
<button
type="button"
aria-haspopup="menu"
aria-expanded={open}
aria-label="More actions"
onClick={() => setOpen((v) => !v)}
data-slot="styled-button-group-caret"
className={cn(
item,
"-ml-px rounded-r-lg border-l border-primary-foreground/20 bg-primary px-2 text-primary-foreground hover:bg-primary/90",
iconSizes[size]
)}
>
<ChevronDown className={cn("transition-transform", open && "rotate-180")} />
</button>
{open ? (
<div
role="menu"
data-slot="styled-button-group-menu"
className="absolute top-full right-0 z-20 mt-1 min-w-40 overflow-hidden rounded-md border border-border bg-popover p-1 text-popover-foreground shadow-md"
>
{rest.map((action, i) => (
<button
key={i}
type="button"
role="menuitem"
onClick={() => {
action.onClick?.()
setOpen(false)
}}
className={cn(
"flex w-full items-center gap-2 rounded-sm px-2 py-1.5 text-left text-sm outline-none transition-colors hover:bg-secondary hover:text-secondary-foreground focus-visible:bg-secondary focus-visible:text-secondary-foreground [&_svg]:pointer-events-none [&_svg]:size-4 [&_svg]:shrink-0 [&_i]:text-base [&_i]:leading-none"
)}
>
{action.icon}
{action.label}
</button>
))}
</div>
) : null}
</div>
)
}
/* ToolbarGroup: kume halinde ikon butonlari, kumeler arasinda ince token ayirici. */
export function ToolbarGroup({ className, size = "md", actions = defaultIconActions }: GroupProps) {
const mid = Math.ceil(actions.length / 2)
const clusters = [actions.slice(0, mid), actions.slice(mid)].filter((c) => c.length > 0)
return (
<div
data-slot="styled-button-group"
role="group"
className={cn(
"inline-flex items-center gap-1 rounded-lg border border-field-border bg-background p-1",
className
)}
>
{clusters.map((cluster, ci) => (
<React.Fragment key={ci}>
{ci > 0 ? (
<span
aria-hidden="true"
data-slot="styled-button-group-divider"
className="mx-0.5 h-5 w-px bg-border"
/>
) : null}
<div className="flex items-center gap-0.5">
{cluster.map((action, i) => (
<button
key={i}
type="button"
onClick={action.onClick}
aria-label={typeof action.label === "string" ? action.label : undefined}
data-slot="styled-button-group-item"
className={cn(
item,
"rounded-md text-foreground hover:bg-secondary hover:text-secondary-foreground",
iconSizes[size]
)}
>
{action.icon ?? action.label}
</button>
))}
</div>
</React.Fragment>
))}
</div>
)
}
/* PillGroup: butonlari tasiyan tamamen yuvarlatilmis pill kabi. */
export function PillGroup({ className, size = "md", actions = defaultActions }: GroupProps) {
return (
<div
data-slot="styled-button-group"
role="group"
className={cn(
"inline-flex items-center gap-1 rounded-full border border-field-border bg-secondary/40 p-1",
className
)}
>
{actions.map((action, i) => (
<button
key={i}
type="button"
onClick={action.onClick}
data-slot="styled-button-group-item"
className={cn(
item,
"rounded-full text-foreground hover:bg-background hover:text-foreground",
sizes[size]
)}
>
{action.icon}
{action.label}
</button>
))}
</div>
)
}
/* StepperGroup: a minus button, a value display and a plus button (a numeric stepper). The number is held in state. */
export function StepperGroup({ className, size = "md", actions }: GroupProps) {
const [value, setValue] = React.useState(0)
void actions
return (
<div
data-slot="styled-button-group"
role="group"
className={cn("inline-flex isolate", className)}
>
<button
type="button"
aria-label="Decrease"
onClick={() => setValue((v) => v - 1)}
data-slot="styled-button-group-item"
className={cn(item, outline, "rounded-l-lg", iconSizes[size])}
>
<Minus />
</button>
<output
aria-live="polite"
data-slot="styled-button-group-value"
className={cn(
item,
"-ml-px -mr-px min-w-12 border-y border-field-border bg-background tabular-nums text-foreground",
sizes[size]
)}
>
{value}
</output>
<button
type="button"
aria-label="Increase"
onClick={() => setValue((v) => v + 1)}
data-slot="styled-button-group-item"
className={cn(item, outline, "rounded-r-lg", iconSizes[size])}
>
<Plus />
</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.
Attached
Buttons sharing borders in one unit.
import { AttachedGroup } from "@/components/ui/button-group-styled"
<AttachedGroup />Split
A primary action plus an attached caret menu.
import { SplitGroup } from "@/components/ui/button-group-styled"
<SplitGroup />Toolbar
Icon buttons grouped with token dividers.
import { ToolbarGroup } from "@/components/ui/button-group-styled"
<ToolbarGroup />Pill
A rounded pill container of buttons.
import { PillGroup } from "@/components/ui/button-group-styled"
<PillGroup />Stepper
Minus, a value, plus - a numeric stepper.
import { StepperGroup } from "@/components/ui/button-group-styled"
<StepperGroup />ai2 Styled button group: 5 styled variations on the token system
The ai2 Styled button group are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around grouped action buttons. 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: the split caret menu fades in; the rest are static. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the menu appears instantly with no animation.
What is in the ai2 Styled button group?
5 exports in one file: Attached, Split, Toolbar, Pill and Stepper. 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: the split caret menu fades in; the rest are static.
- Reduced-motion aware: Under prefers-reduced-motion, the menu appears 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 Styled button group 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.