Tone toggle groups
Five single-select toggle groups that share one structure and vary only in meaning: each is painted with a single semantic tone, so the active option carries that tone's fill and its matching foreground while the rest stay neutral. Each root carries data-tone, is self-contained, sized, and token-driven.
Installation
The styled layer is free and installs like any other ai2 component.
Run the following command
npx shadcn@latest add @ai2/toggle-group-toneDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggle-group-tone.tsx"use client"
import * as React from "react"
import { CircleAlert, CircleCheck, CircleMinus, Info, TriangleAlert } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Tone toggle group family: 5 decorative single-select controls. The shared idea is
the SEMANTIC TONE: each variant is painted with a single meaning color (info,
success, warning, danger, muted); the active option carries that tone's fill and
the inactive ones carry neutral text. The structure is the same in every variant,
so the only difference is color. The root carries `data-tone`.
Color comes ONLY from semantic tokens; alpha via color-mix. The active indicator
slides with a framer layoutId and moves instantly while useReducedMotion is on;
the layoutId is unique per instance via React.useId(). */
export type StyledSize = "sm" | "md" | "lg" | "xl"
export type StyledTone = "info" | "success" | "warning" | "danger" | "muted"
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 LEVEL_OPTIONS: Option[] = [
{ value: "low", label: "Low" },
{ value: "normal", label: "Normal" },
{ value: "high", label: "High" },
]
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 }
}
/* Aktif gosterge: dolu ton zemini. */
const toneIndicator: Record<StyledTone, string> = {
info: "bg-info",
success: "bg-success",
warning: "bg-warning",
danger: "bg-danger",
muted: "bg-foreground",
}
/* Active text: the -foreground matching the tone, so the contrast passes in every
theme. */
const toneOnText: Record<StyledTone, string> = {
info: "text-info-foreground",
success: "text-success-foreground",
warning: "text-warning-foreground",
danger: "text-danger-foreground",
muted: "text-background",
}
/* Inactive text: neutral; it moves toward the tone on hover. */
const toneOffText: Record<StyledTone, string> = {
info: "text-muted-foreground hover:text-info",
success: "text-muted-foreground hover:text-success",
warning: "text-muted-foreground hover:text-warning",
danger: "text-muted-foreground hover:text-danger",
muted: "text-muted-foreground hover:text-foreground",
}
/* Kabuk: ton renkli ince kenarlik + cok hafif ton zemini. */
const toneRoot: Record<StyledTone, string> = {
info: "border-[color-mix(in_oklab,var(--color-info)_35%,transparent)] bg-[color-mix(in_oklab,var(--color-info)_8%,transparent)]",
success:
"border-[color-mix(in_oklab,var(--color-success)_35%,transparent)] bg-[color-mix(in_oklab,var(--color-success)_8%,transparent)]",
warning:
"border-[color-mix(in_oklab,var(--color-warning)_35%,transparent)] bg-[color-mix(in_oklab,var(--color-warning)_8%,transparent)]",
danger:
"border-[color-mix(in_oklab,var(--color-danger)_35%,transparent)] bg-[color-mix(in_oklab,var(--color-danger)_8%,transparent)]",
muted: "border-border bg-muted",
}
/* Tone -> the default lucide icon; it sits in front of the first option. */
const toneIcon: Record<StyledTone, React.ReactNode> = {
info: <Info />,
success: <CircleCheck />,
warning: <TriangleAlert />,
danger: <CircleAlert />,
muted: <CircleMinus />,
}
/* The shared shell: only the tone changes. */
function ToneGroup({ props, tone, label }: { props: ToggleGroupProps; tone: StyledTone; label: string }) {
const { className, size = "md" } = props
const { list, isOn, toggle } = useToggleGroup({ ...props, type: "single" }, LEVEL_OPTIONS)
const id = React.useId()
const reduce = useReducedMotion()
const transition = reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 420, damping: 34 }
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"
data-tone={tone}
role="group"
aria-label={label}
className={cn("inline-flex items-center gap-0.5 rounded-lg border p-1", toneRoot[tone], className)}
>
{list.map((o, i) => {
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 ? toneOnText[tone] : toneOffText[tone])}
>
{on && (
<motion.span
layoutId={`${id}-tone-indicator`}
transition={transition}
className={cn("absolute inset-0 -z-10 rounded-md shadow-sm", toneIndicator[tone])}
/>
)}
{o.icon ?? (i === 0 ? toneIcon[tone] : null)}
{o.label}
</button>
)
})}
</div>
</div>
)
}
/* Info: an informational tone; for a neutral series of choices. */
export function InfoToggleGroup(props: ToggleGroupProps) {
return <ToneGroup props={props} tone="info" label="Priority level, info tone" />
}
/* Success: olumlu ton; onaylanan bir secimi isaretler. */
export function SuccessToggleGroup(props: ToggleGroupProps) {
return <ToneGroup props={props} tone="success" label="Priority level, success tone" />
}
/* Warning: a caution tone; for a risky series of choices. */
export function WarningToggleGroup(props: ToggleGroupProps) {
return <ToneGroup props={props} tone="warning" label="Priority level, warning tone" />
}
/* Danger: yikici ton; geri alinamaz secimleri isaretler. */
export function DangerToggleGroup(props: ToggleGroupProps) {
return <ToneGroup props={props} tone="danger" label="Priority level, danger tone" />
}
/* Muted: a colorless tone; for choices that should not stand out in the interface. */
export function MutedToggleGroup(props: ToggleGroupProps) {
return <ToneGroup props={props} tone="muted" label="Priority level, muted tone" />
}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.
Info
The informative tone for a neutral run of choices.
import { InfoToggleGroup } from "@/components/ui/toggle-group-tone"
<InfoToggleGroup />Success
The positive tone, marking a confirmed selection.
import { SuccessToggleGroup } from "@/components/ui/toggle-group-tone"
<SuccessToggleGroup />Warning
The caution tone for a risky run of choices.
import { WarningToggleGroup } from "@/components/ui/toggle-group-tone"
<WarningToggleGroup />Danger
The destructive tone for selections you cannot undo.
import { DangerToggleGroup } from "@/components/ui/toggle-group-tone"
<DangerToggleGroup />Muted
The colourless tone for choices that should stay quiet.
import { MutedToggleGroup } from "@/components/ui/toggle-group-tone"
<MutedToggleGroup />ai2 Tone toggle groups: 5 styled variations on the token system
The ai2 Tone toggle groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around toggle groups painted with a single semantic tone. 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 filled tone indicator to the active 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 Tone toggle groups?
5 exports in one file: Info, Success, Warning, Danger and Muted. 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 filled tone indicator to the active 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 Tone 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.