ToggleGroup
Segmented control for picking one or several options.
Example
import { ToggleGroup, ToggleGroupItem } from "@porcelana/ui";
export default function Example() {
return (
<ToggleGroup type="single" defaultValue="week" aria-label="Range">
<ToggleGroupItem value="day">Day</ToggleGroupItem>
<ToggleGroupItem value="week">Week</ToggleGroupItem>
<ToggleGroupItem value="month">Month</ToggleGroupItem>
</ToggleGroup>
);
}Import
import { ToggleGroup, ToggleGroupItem } from "@porcelana/ui";
import "@porcelana/ui/styles.css"; // once, at your app rootProps
Generated from the TypeScript types. Standard HTML attributes are passed through and not listed.
ToggleGroup
| Prop | Type | Default | Description |
|---|---|---|---|
typerequired | "single" | "multiple" | – | Button type (defaults to "button"). |
aria-label | string | – | Defines a string value that labels the current element. @see aria-labelledby. |
asChild | boolean | – | Render the single child element instead, merging props and behaviour onto it. |
block | boolean | – | Stretch items to fill the width (good on phones). |
defaultValue | string | string[] | – | The value of the item that is pressed when initially rendered. Use `defaultValue` if you do not need to control the state of a toggle group. The value of the items that are pressed when initially rendered. Use `defaultValue` if you do not need to control the state of a toggle group. |
dir | Direction | – | Reading direction for keyboard navigation ("ltr" or "rtl"). |
disabled | boolean | – | Whether the group is disabled from user interaction. @defaultValue false |
id | string | – | Element id. Generated automatically when omitted. |
loop | boolean | – | Whether keyboard navigation wraps from the last item to the first. |
onValueChange | ((value: string) => void) | ((value: string[]) => void) | – | The callback that fires when the value of the toggle group changes. The callback that fires when the state of the toggle group changes. |
orientation | Orientation | – | Layout and arrow-key direction. |
rovingFocus | boolean | – | Whether the group should maintain roving focus of its buttons. @defaultValue true |
value | string | string[] | – | The controlled stateful value of the item that is pressed. The controlled stateful value of the items that are pressed. |
ToggleGroupItem
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | Defines a string value that labels the current element. @see aria-labelledby. |
asChild | boolean | – | Render the single child element instead, merging props and behaviour onto it. |
disabled | boolean | – | Prevents interaction and dims the control. |
id | string | – | Element id. Generated automatically when omitted. |
type | "button" | "submit" | "reset" | – | Button type (defaults to "button"). |
value | string | string[] | – | The controlled stateful value of the item that is pressed. The controlled stateful value of the items that are pressed. |
Accessibility
- type="single" behaves like a radio group; type="multiple" like toggle buttons.
- Give the group an aria-label.
Mobile
- block stretches items to the full width; long groups scroll sideways instead of overflowing.