Styled toggle group
Five toggle groups: a segmented control, pills, icon multi-select, outline and ghost. Each is sized, token-driven and supports single or multiple selection 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-styledDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggle-group-styled.tsx"use client"
import * as React from "react"
import { AlignCenter, AlignLeft, AlignRight, Bold, Italic, Underline } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Toggle group family: 5 decorative connected-button rows. A row of toggle buttons;
single-select (like a segmented control) or multi-select (like a formatting bar)
through the `type` prop. Every button is a real <button> + aria-pressed + a
focus-visible ring. Color comes ONLY from tokens (via alpha color-mix, never
var(--x)/0.4). The framer-motion layoutId sliding indicator is disabled through
useReducedMotion. Controlled (value) or uncontrolled (defaultValue). */
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 sizeIconBtn: Record<StyledSize, string> = {
sm: "size-8 text-xs",
md: "size-9 text-sm",
lg: "size-10 text-sm",
xl: "size-11 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 /> },
]
const ICON_OPTIONS: Option[] = [
{ value: "bold", label: "Bold", icon: <Bold /> },
{ value: "italic", label: "Italic", icon: <Italic /> },
{ value: "underline", label: "Underline", icon: <Underline /> },
]
function toArray(v: string | string[] | undefined): string[] {
if (v === undefined) return []
return Array.isArray(v) ? v : [v]
}
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 }
}
function useSlide() {
const reduce = useReducedMotion()
return reduce ? { duration: 0 } : ({ type: "spring", stiffness: 420, damping: 34 } as const)
}
function labelText(o: Option): string {
return typeof o.label === "string" ? o.label : o.value
}
/* Segmented: tek-secim segment kontrol; aktifin altinda token dolgulu gosterge
framer layoutId ile kayar. */
export function SegmentedToggleGroup({ className, size = "md", options, value, defaultValue, onValueChange }: ToggleGroupProps) {
const { list, isOn, toggle } = useToggleGroup({ type: "single", options, value, defaultValue, onValueChange }, TEXT_OPTIONS)
const id = React.useId()
const transition = useSlide()
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"
className={cn("inline-flex items-center gap-0.5 rounded-lg border border-border bg-muted p-1", 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], "rounded-md", on ? "text-foreground" : "text-muted-foreground hover:text-foreground")}
>
{on && (
<motion.span
layoutId={`${id}-seg`}
transition={transition}
className="absolute inset-0 -z-10 rounded-md border border-border bg-background shadow-sm"
/>
)}
{o.icon}
{o.label}
</button>
)
})}
</div>
</div>
)
}
/* Pill: every option is a pill; the active one fills with primary. */
export function PillToggleGroup({ className, size = "md", type = "single", options, value, defaultValue, onValueChange }: ToggleGroupProps) {
const { list, isOn, toggle } = useToggleGroup({ type, options, value, defaultValue, onValueChange }, TEXT_OPTIONS)
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" className={cn("inline-flex flex-wrap items-center gap-2", 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],
"rounded-full border",
on ? "border-transparent bg-primary text-primary-foreground" : "border-border bg-background text-muted-foreground hover:text-foreground"
)}
>
{o.icon}
{o.label}
</button>
)
})}
</div>
</div>
)
}
/* Icon: ikon dugmeler, coklu-secim bicimlendirme cubugu; basili = token tint. */
export function IconToggleGroup({ className, size = "md", type = "multiple", options, value, defaultValue, onValueChange }: ToggleGroupProps) {
const { list, isOn, toggle } = useToggleGroup({ type, options, value, defaultValue, onValueChange }, ICON_OPTIONS)
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"
className={cn("inline-flex items-center gap-1 rounded-lg border border-border bg-background p-1", className)}
>
{list.map((o) => {
const on = isOn(o.value)
return (
<button
key={o.value}
type="button"
aria-pressed={on}
aria-label={labelText(o)}
title={labelText(o)}
data-slot="styled-toggle-group-item"
onClick={() => toggle(o.value)}
style={on ? { backgroundColor: "color-mix(in oklab, var(--primary) 14%, transparent)" } : undefined}
className={cn(btnBase, sizeIconBtn[size], "rounded-md", on ? "text-primary" : "text-muted-foreground hover:text-foreground")}
>
{o.icon ?? o.label}
</button>
)
})}
</div>
</div>
)
}
/* Outline: bagli outline dugmeler; aktif token dolgu alir. */
export function OutlineToggleGroup({ className, size = "md", type = "single", options, value, defaultValue, onValueChange }: ToggleGroupProps) {
const { list, isOn, toggle } = useToggleGroup({ type, options, value, defaultValue, onValueChange }, TEXT_OPTIONS)
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"
className={cn("inline-flex items-center overflow-hidden rounded-lg border border-border", 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],
"rounded-none [&:not(:first-child)]:border-l [&:not(:first-child)]:border-border",
on ? "bg-primary text-primary-foreground" : "text-muted-foreground hover:bg-muted hover:text-foreground"
)}
>
{o.icon}
{o.label}
</button>
)
})}
</div>
</div>
)
}
/* Ghost: kenarliksiz; aktif yumusak token arka plan alir. */
export function GhostToggleGroup({ className, size = "md", type = "single", options, value, defaultValue, onValueChange }: ToggleGroupProps) {
const { list, isOn, toggle } = useToggleGroup({ type, options, value, defaultValue, onValueChange }, TEXT_OPTIONS)
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" className={cn("inline-flex items-center gap-1", 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)}
style={on ? { backgroundColor: "color-mix(in oklab, var(--primary) 12%, transparent)" } : undefined}
className={cn(
btnBase,
sizeBtn[size],
"rounded-md",
on ? "text-primary" : "text-muted-foreground hover:bg-muted hover:text-foreground"
)}
>
{o.icon}
{o.label}
</button>
)
})}
</div>
</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.
Segmented
A single-select segmented control with a sliding highlight.
import { SegmentedToggleGroup } from "@/components/ui/toggle-group-styled"
<SegmentedToggleGroup />Pill
Pills, the active one filled with primary.
import { PillToggleGroup } from "@/components/ui/toggle-group-styled"
<PillToggleGroup />Icon
Multi-select icon buttons like a formatting toolbar.
import { IconToggleGroup } from "@/components/ui/toggle-group-styled"
<IconToggleGroup />Outline
Connected outline buttons with a token active fill.
import { OutlineToggleGroup } from "@/components/ui/toggle-group-styled"
<OutlineToggleGroup />Ghost
Borderless, active gets a soft-token background.
import { GhostToggleGroup } from "@/components/ui/toggle-group-styled"
<GhostToggleGroup />ai2 Styled toggle group: 5 styled variations on the token system
The ai2 Styled toggle group are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around grouped toggle 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: framer-motion slides the active highlight in the segmented group. The styled layer is opt-in, so the dependency only lands if you use it; the base components stay lean. Under reduced motion, the highlight jumps to the active option with no animation.
What is in the ai2 Styled toggle group?
5 exports in one file: Segmented, Pill, Icon, Outline and Ghost. 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 slides the active highlight in the segmented group.
- Reduced-motion aware: Under prefers-reduced-motion, the highlight jumps to the active option 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 toggle 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.