Toggle group
A native radio segmented control with one selected key that glides between options when enhanced.
pnpm dlx shadcn@latest add @haze/toggle-group---
import { cn } from "../../../lib/haze/utils";
interface Option {
value: string;
label: string;
}
interface Props {
/** Shared radio name. */
name: string;
legend: string;
/** Hides the legend visually; screen readers still read it. */
hideLegend?: boolean;
options: Option[];
/** The value that is selected on load. */
value?: string;
size?: "sm" | "md";
class?: string;
}
const { name, legend, hideLegend = true, options, value, size = "md", class: className } = Astro.props;
const sizes = { sm: "h-6 px-2.5 text-xs", md: "h-8 px-3 text-[0.8125rem]" };
---
<fieldset data-haze-toggle-group class={cn("group/toggles grid min-w-0 gap-2 border-0 p-0", className)}>
<legend class={cn("text-[0.8125rem]/5 font-medium text-foreground", hideLegend ? "sr-only" : "mb-2")}>{legend}</legend>
<div data-toggle-track class="relative isolate inline-flex w-fit max-w-full gap-1 overflow-x-auto [scrollbar-width:none] [&::-webkit-scrollbar]:hidden rounded-lg border border-frame bg-muted p-1">
<span data-toggle-indicator aria-hidden="true" hidden class="pointer-events-none absolute top-1 bottom-1 left-0 -z-10 w-(--toggle-w) translate-x-(--toggle-x) rounded-sm border border-frame bg-card shadow-key group-data-[enhanced]/toggles:block group-data-[moving]/toggles:transition-[translate,width] group-data-[moving]/toggles:duration-300 group-data-[moving]/toggles:ease-snappy forced-colors:hidden"></span>
{
options.map((option) => (
<label class="relative inline-flex shrink-0">
<input type="radio" name={name} value={option.value} checked={option.value === value} class="peer sr-only" />
<span
class={cn(
"inline-flex cursor-pointer items-center justify-center whitespace-nowrap rounded-sm border border-transparent font-medium text-muted-foreground transition-[background-color,border-color,color,box-shadow,scale] duration-150 ease-snappy select-none hover:text-foreground active:scale-[0.97] active:duration-75 peer-checked:border-border peer-checked:bg-card peer-checked:text-foreground peer-checked:shadow-key peer-focus-visible:outline-2 peer-focus-visible:-outline-offset-2 peer-focus-visible:outline-ring forced-colors:peer-checked:border-[Highlight]",
"group-data-[enhanced]/toggles:peer-checked:border-transparent group-data-[enhanced]/toggles:peer-checked:bg-transparent group-data-[enhanced]/toggles:peer-checked:shadow-none forced-colors:group-data-[enhanced]/toggles:peer-checked:border-[Highlight]",
sizes[size],
)}
>
{option.label}
</span>
</label>
))
}
</div>
</fieldset>
<script>
function initToggleGroups() {
document.querySelectorAll<HTMLElement>("[data-haze-toggle-group]").forEach(root => {
if (root.hasAttribute("data-enhanced")) return;
const track = root.querySelector<HTMLElement>("[data-toggle-track]");
const indicator = root.querySelector<HTMLElement>("[data-toggle-indicator]");
const inputs = Array.from(root.querySelectorAll<HTMLInputElement>("input[type=radio]"));
if (!track || !indicator || !inputs.length) return;
const place = (moving = false) => {
const input = inputs.find(input => input.checked);
const label = input?.parentElement;
root.toggleAttribute("data-moving", moving);
indicator.hidden = !label;
indicator.style.visibility = label ? "" : "hidden";
if (!label) return;
indicator.style.setProperty("--toggle-x", `${label.offsetLeft}px`);
indicator.style.setProperty("--toggle-w", `${label.offsetWidth}px`);
};
inputs.forEach(input => input.addEventListener("change", () => place(true)));
inputs[0]?.form?.addEventListener("reset", () => queueMicrotask(() => place(true)));
root.setAttribute("data-enhanced", "");
place();
if ("ResizeObserver" in window) new ResizeObserver(() => place()).observe(track);
document.fonts?.ready.then(() => { if (root.isConnected) place(); });
});
}
initToggleGroups();
if (!document.documentElement.hasAttribute("data-haze-toggle-group-listener")) {
document.documentElement.setAttribute("data-haze-toggle-group-listener", "");
document.addEventListener("astro:page-load", initToggleGroups);
}
</script>When to use
- Switching a view or a filter between two to four short options, such as list, grid or board
- Choosing a time range or a billing period next to a chart or table
When not to use
- Navigating between panels of content; use tabs
- Options with descriptions or more than five items; use radio-group or select
- Turning a single setting on or off; use switch
Installation
Add the Haze registry to components.json once, then install Toggle group with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/toggle-groupnpx shadcn@latest add @haze/toggle-groupyarn dlx shadcn@latest add @haze/toggle-groupbunx --bun shadcn@latest add @haze/toggle-groupFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| name* | string | – | Shared radio name; the form submits the selected value under it. |
| legend* | string | – | Accessible name of the group, for example "Time range". |
| hideLegend | boolean | true | Hides the legend visually with sr-only; set false to show it above the control. |
| options* | { value: string; label: string }[] | – | The segments, in display order. |
| value | string | – | Value of the segment that is selected on load. |
| size | "sm" | "md" | "md" | Segment height and text size. |
| class | string | – | Extra classes on the fieldset. |
Keyboard interactions
- Arrow LeftArrow Right
- Moves to the neighbouring segment and selects it
- Tab
- Enters or leaves the group at the selected segment
Accessibility
- Each segment is a real radio inside its label, so the group announces as a radio group with the legend as its name.
- The selected segment is drawn as a raised key with a border and a shadow, not only a color change, and the radio state is exposed to assistive technology. Under forced colors the selected segment keeps a highlighted border.
- A segment gets a short press animation, which the theme switches off under prefers-reduced-motion.
- The shared key moves after a real selection change, follows native form resets, and remeasures without motion after layout changes.
- The focus outline is drawn inside the segment because the container clips overflow.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
Install and use
Install the Haze UI component "Toggle group" with `npx shadcn@latest add @haze/toggle-group`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "http://localhost:4329/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import ToggleGroup from "@/components/haze/ui/toggle-group.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: http://localhost:4329/ui/toggle-group.md
Customize with real content
Adapt the Haze toggle-group ui for our product. Replace the demo values of these props with our real content: name, legend, hideLegend, options, value, size, class. Keep the layout, the heading level rules and these accessibility details: Each segment is a real radio inside its label, so the group announces as a radio group with the legend as its name. The selected segment is drawn as a raised key with a border and a shadow, not only a color change, and the radio state is exposed to assistive technology. Under forced colors the selected segment keeps a highlighted border. A segment gets a short press animation, which the theme switches off under prefers-reduced-motion. The shared key moves after a real selection change, follows native form resets, and remeasures without motion after layout changes. The focus outline is drawn inside the segment because the container clips overflow. Do not add raw color classes; use the Haze tokens.
Choose the right block
Before using toggle-group, check it fits: use it when switching a view or a filter between two to four short options, such as list, grid or board; choosing a time range or a billing period next to a chart or table. Avoid it when navigating between panels of content; use tabs; options with descriptions or more than five items; use radio-group or select; turning a single setting on or off; use switch. Alternatives: @haze/tabs, @haze/radio-group, @haze/select, @haze/button-group.
Dependencies
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.