Components / Inputs
ToggleGroup
Segmented control. Single choice by default (a view switcher); multiple for independent toggles. ref points at the button that takes focus (the first pressed one, else the first enabled one).
import { ToggleGroup } from "@stylhero/ui";
Example
Code
import { useState } from "react";
import { ToggleGroup } from "@stylhero/ui";
function ToggleGroupExample() {
const [view, setView] = useState("grid");
return (
<ToggleGroup label="View" value={view} onValueChange={setView}
options={[{ value: "grid", label: "Grid" }, { value: "list", label: "List" }, { value: "map", label: "Map" }]} />
);
}
Props
| Prop | Type | Default | Description |
|---|---|---|---|
options required |
ToggleOption[] |
- | |
className |
string |
- | |
defaultValue |
string | string[] |
- | |
label |
string |
- | |
multiple |
boolean |
- | |
onBlur |
FocusEventHandler<HTMLDivElement> |
- | Runs when focus leaves the group (react-hook-form's field.onBlur). |
onValueChange |
((value: string) => void) | ((value: string[]) => void) |
- | |
size |
"sm" | "md" |
"md" |
|
value |
string | string[] |
- |
Takes a ref.
Types
type ToggleOption = { value: string; label: ReactNode; disabled?: boolean };
Keyboard
| Key | Does |
|---|---|
| Tab | Moves between the buttons |
| Enter / Space | Toggles the focused button |
Accessibility
- A
role="group"of buttons witharia-pressed. Give it alabel.
Generated from src/components/ToggleGroup.tsx. Something wrong or missing? Open an issue.