Segmented toggle group
A segmented control whose pressed fill glides to the next pick.
A @khaeli/toggle-group for a single choice, with one difference: the selected segment's fill is one highlight that glides to the next pick. It jumps under reduced motion, and below 768px there is no track, so each item shows its own pressed fill. Its items are ToggleGroupItem. Like any toggle group it lets the selected segment be pressed off: keep one selected in onValueChange, as these examples do.
Single selection
"use client"
import * as React from "react"
import { SegmentedToggleGroup } from "@/components/ui/segmented-toggle-group"
import { ToggleGroupItem } from "@/components/ui/toggle-group"
export default function SegmentedToggleGroupSingleSelection() {
const [view, setView] = React.useState(["board"])
return (
<SegmentedToggleGroup
aria-label="View"
value={view}
// Pressing the selected segment again would clear it: keep one selected.
onValueChange={(next) => next.length > 0 && setView(next)}
>
<ToggleGroupItem value="list">List</ToggleGroupItem>
<ToggleGroupItem value="board">Board</ToggleGroupItem>
<ToggleGroupItem value="calendar">Calendar</ToggleGroupItem>
</SegmentedToggleGroup>
)
}
With icons
"use client"
import * as React from "react"
import { CalendarIcon, LayoutColumnsIcon, ListIcon } from "@keyline-icons/react"
import { SegmentedToggleGroup } from "@/components/ui/segmented-toggle-group"
import { ToggleGroupItem } from "@/components/ui/toggle-group"
export default function SegmentedToggleGroupWithIcons() {
const [view, setView] = React.useState(["list"])
return (
<SegmentedToggleGroup aria-label="View" value={view} onValueChange={(next) => next.length > 0 && setView(next)}>
<ToggleGroupItem value="list">
<ListIcon data-icon="inline-start" />
List
</ToggleGroupItem>
<ToggleGroupItem value="board">
<LayoutColumnsIcon data-icon="inline-start" />
Board
</ToggleGroupItem>
<ToggleGroupItem value="calendar">
<CalendarIcon data-icon="inline-start" />
Calendar
</ToggleGroupItem>
</SegmentedToggleGroup>
)
}
Icon only
"use client"
import * as React from "react"
import { MonitorIcon, MoonIcon, SunIcon } from "@keyline-icons/react"
import { SegmentedToggleGroup } from "@/components/ui/segmented-toggle-group"
import { ToggleGroupItem } from "@/components/ui/toggle-group"
export default function SegmentedToggleGroupIconOnly() {
const [appearance, setAppearance] = React.useState(["system"])
return (
<SegmentedToggleGroup
aria-label="Appearance"
value={appearance}
onValueChange={(next) => next.length > 0 && setAppearance(next)}
>
<ToggleGroupItem value="system" aria-label="System">
<MonitorIcon />
</ToggleGroupItem>
<ToggleGroupItem value="light" aria-label="Light">
<SunIcon />
</ToggleGroupItem>
<ToggleGroupItem value="dark" aria-label="Dark">
<MoonIcon />
</ToggleGroupItem>
</SegmentedToggleGroup>
)
}
Disabled
import { SegmentedToggleGroup } from "@/components/ui/segmented-toggle-group"
import { ToggleGroupItem } from "@/components/ui/toggle-group"
export default function SegmentedToggleGroupDisabled() {
return (
<div className="flex flex-col items-center gap-3">
<SegmentedToggleGroup aria-label="Billing period" defaultValue={["monthly"]}>
<ToggleGroupItem value="monthly">Monthly</ToggleGroupItem>
<ToggleGroupItem value="yearly">Yearly</ToggleGroupItem>
<ToggleGroupItem value="custom" disabled>
Custom
</ToggleGroupItem>
</SegmentedToggleGroup>
<SegmentedToggleGroup disabled aria-label="Billing period, locked" defaultValue={["monthly"]}>
<ToggleGroupItem value="monthly">Monthly</ToggleGroupItem>
<ToggleGroupItem value="yearly">Yearly</ToggleGroupItem>
<ToggleGroupItem value="custom">Custom</ToggleGroupItem>
</SegmentedToggleGroup>
</div>
)
}
Installation
npx shadcn@latest add @khaeli/segmented-toggle-groupbunx --bun shadcn@latest add @khaeli/segmented-toggle-groupInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
Keyboard
Tab- Moves into the group, to its first segment or to the one that last had focus, and out of it again. A
disabledgroup leaves the tab order. ArrowRightArrowLeft- Move focus to the next or previous segment, from the last round to the first. A disabled segment is passed over. The selection does not follow focus.
HomeEnd- Move focus to the first or last segment.
SpaceEnter- Select the focused segment.
Accessible name
- The group is a
role="group"of toggle buttons, not a radio group. Give it anaria-labelthat says what is being chosen, such as View. - Each segment's name is its text; an icon-only segment needs an
aria-label. The selected segment is the one witharia-pressed="true".
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/segmented-toggle-group.json