Vertical toggle groups
Five single-select toggle groups turned onto the vertical axis: options stack, the indicator travels up and down, and the Up and Down arrow keys move focus through the options while Home and End jump to the ends. Each is self-contained, sized, token-driven, and every option 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-verticalDependencies, the @ai2/tokens theme and the component file are installed together.
Install dependencies
npm install motion lucide-reactCopy the source
components/ui/toggle-group-vertical.tsx"use client"
import * as React from "react"
import { Bell, Inbox, Star } from "lucide-react"
import { motion, useReducedMotion } from "motion/react"
import { cn } from "@/lib/utils"
/* Vertical toggle group family: 5 decorative VERTICAL single-select controls. The
shared idea is turning the horizontal segmented bar onto the vertical axis: the
options are stacked, the active indicator slides up and down, and the Up/Down
arrow keys move between options on the keyboard (Home/End go to the ends). The
variants change the vertical surface (a stack, an edge rail, a card, a tight
list, a plain list). Color comes ONLY from semantic tokens; alpha via color-mix.
The framer layoutId indicator moves instantly while useReducedMotion is on; the
layoutId is unique per instance via React.useId(). */
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
}
/* In the vertical case the height stays fixed and the width comes from the shell;
the alignment is flush left. */
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 w-full select-none items-center justify-start 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 VIEW_OPTIONS: Option[] = [
{ value: "inbox", label: "Inbox", icon: <Inbox /> },
{ value: "starred", label: "Starred", icon: <Star /> },
{ value: "alerts", label: "Alerts", icon: <Bell /> },
]
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 }
}
/* Vertical keyboard navigation: Up/Down moves to the neighboring button and wraps;
Home/End go to the ends. Focus moves in DOM order. */
function useVerticalKeys() {
return React.useCallback((e: React.KeyboardEvent<HTMLDivElement>) => {
const keys = ["ArrowUp", "ArrowDown", "Home", "End"]
if (!keys.includes(e.key)) return
const root = e.currentTarget
const items = Array.from(
root.querySelectorAll<HTMLButtonElement>('button[data-slot="styled-toggle-group-item"]')
)
if (items.length === 0) return
const current = items.indexOf(document.activeElement as HTMLButtonElement)
if (current === -1) return
e.preventDefault()
let next = current
if (e.key === "ArrowDown") next = (current + 1) % items.length
if (e.key === "ArrowUp") next = (current - 1 + items.length) % items.length
if (e.key === "Home") next = 0
if (e.key === "End") next = items.length - 1
items[next]?.focus()
}, [])
}
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
/** Erisilebilir ad. */
label: string
}
/* The shared shell: only the vertical surface changes. */
function VerticalGroup({ props, spec }: { props: ToggleGroupProps; spec: Spec }) {
const { className, size = "md" } = props
const { list, isOn, toggle } = useToggleGroup({ ...props, type: "single" }, VIEW_OPTIONS)
const id = React.useId()
const reduce = useReducedMotion()
const onKeyDown = useVerticalKeys()
const transition = reduce ? { duration: 0 } : { type: "spring" as const, stiffness: 420, damping: 34 }
return (
<div
data-slot="styled-toggle-group"
role="group"
aria-label={spec.label}
aria-orientation="vertical"
onKeyDown={onKeyDown}
className={cn("inline-flex flex-col items-stretch", 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}-vertical-indicator`}
transition={transition}
className={cn("absolute inset-0 -z-10", spec.indicator)}
/>
)}
{o.icon}
{o.label}
</button>
)
})}
</div>
)
}
/* Stack: dikey segment yigini; kutulu gosterge secenekler arasinda kayar. */
export function StackToggleGroup(props: ToggleGroupProps) {
return (
<VerticalGroup
props={props}
spec={{
label: "Mailbox view, vertical stack",
root: "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",
}}
/>
)
}
/* Rail: sol kenarda dikey ray; aktif secenegi kalin bir cubuk isaretler. */
export function RailToggleGroup(props: ToggleGroupProps) {
return (
<VerticalGroup
props={props}
spec={{
label: "Mailbox view, side rail",
root: "gap-0.5 border-l border-border pl-1",
item: "rounded-r-md rounded-l-none",
onText: "text-primary",
offText: "text-muted-foreground hover:text-foreground",
indicator:
"rounded-r-md bg-[color-mix(in_oklab,var(--color-primary)_10%,transparent)] before:absolute before:inset-y-0 before:-left-1 before:w-0.5 before:rounded-full before:bg-primary",
}}
/>
)
}
/* Card: every option is a separate card; the active card gets a primary ring and
elevation. */
export function CardToggleGroup(props: ToggleGroupProps) {
return (
<VerticalGroup
props={props}
spec={{
label: "Mailbox view, card list",
root: "gap-2",
item: "rounded-lg",
onText: "text-foreground",
offText: "text-muted-foreground hover:text-foreground",
indicator:
"rounded-lg bg-card shadow-sm ring-2 ring-[color-mix(in_oklab,var(--color-primary)_45%,transparent)]",
}}
/>
)
}
/* Compact: sifir bosluklu sik liste; bolucu cizgiler dugmeleri ayirir. */
export function CompactToggleGroup(props: ToggleGroupProps) {
return (
<VerticalGroup
props={props}
spec={{
label: "Mailbox view, compact list",
root: "overflow-hidden rounded-lg border border-border bg-background",
item: "rounded-none [&:not(:first-child)]:border-t [&:not(:first-child)]:border-border",
onText: "text-primary-foreground",
offText: "text-muted-foreground hover:bg-muted hover:text-foreground",
indicator: "bg-primary",
}}
/>
)
}
/* List: a plain borderless list; the active row takes a soft token background. */
export function ListToggleGroup(props: ToggleGroupProps) {
return (
<VerticalGroup
props={props}
spec={{
label: "Mailbox view, plain list",
root: "gap-1",
item: "rounded-md",
onText: "text-foreground",
offText: "text-muted-foreground hover:bg-muted hover:text-foreground",
indicator: "rounded-md bg-[color-mix(in_oklab,var(--color-foreground)_8%,transparent)]",
}}
/>
)
}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.
Stack
A vertical segmented stack with a sliding boxed indicator.
import { StackToggleGroup } from "@/components/ui/toggle-group-vertical"
<StackToggleGroup />Rail
A side rail where a thick bar marks the active option.
import { RailToggleGroup } from "@/components/ui/toggle-group-vertical"
<RailToggleGroup />Card
Each option is a card; the active one gets a primary ring.
import { CardToggleGroup } from "@/components/ui/toggle-group-vertical"
<CardToggleGroup />Compact
A tight list with dividers and a solid primary fill.
import { CompactToggleGroup } from "@/components/ui/toggle-group-vertical"
<CompactToggleGroup />List
A borderless list; the active row takes a soft token background.
import { ListToggleGroup } from "@/components/ui/toggle-group-vertical"
<ListToggleGroup />ai2 Vertical toggle groups: 5 styled variations on the token system
The ai2 Vertical toggle groups are a set of 5 decorative button variations from the styled layer of the @ai2 design system, built around toggle groups laid out on the vertical axis with arrow key navigation. 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 indicator up and down between options. 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 Vertical toggle groups?
5 exports in one file: Stack, Rail, Card, Compact and List. 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 indicator up and down between options.
- 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 Vertical 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.