SegmentedControl
A row of buttons for choosing one option from a few. Suited to switching views or settings with two to five choices.
View in Storybook (opens in a new tab)
Installation
import { SegmentedControl } from "@ginga-ui/core";Basic usage
Place SegmentedControlItem elements inside and give each an id. Specify the initial selection with defaultValue.
segmented-control.tsx
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
export function BasicExample() {
return (
<SegmentedControl aria-label="Time range" defaultValue="week">
<SegmentedControlItem id="day">Day</SegmentedControlItem>
<SegmentedControlItem id="week">Week</SegmentedControlItem>
<SegmentedControlItem id="month">Month</SegmentedControlItem>
</SegmentedControl>
);
}Icons only
When an item shows only an icon, give it an aria-label.
segmented-control.tsx
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
import { LayoutGrid, List, Rows3 } from "lucide-react";
export function IconExample() {
return (
<SegmentedControl aria-label="View" defaultValue="grid">
<SegmentedControlItem id="grid" aria-label="Grid">
<LayoutGrid />
</SegmentedControlItem>
<SegmentedControlItem id="list" aria-label="List">
<List />
</SegmentedControlItem>
<SegmentedControlItem id="compact" aria-label="Compact">
<Rows3 />
</SegmentedControlItem>
</SegmentedControl>
);
}Size
Three sizes are available: sm, md, and lg.
segmented-control.tsx
segmented-control.module.css
import { SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
import styles from "./segmented-control.module.css";
export function SizeExample() {
return (
<div className={styles.sizeRow}>
<SegmentedControl aria-label="Sort order" defaultValue="new" size="sm">
<SegmentedControlItem id="new">Newest</SegmentedControlItem>
<SegmentedControlItem id="popular">Popular</SegmentedControlItem>
</SegmentedControl>
<SegmentedControl aria-label="Sort order" defaultValue="new" size="md">
<SegmentedControlItem id="new">Newest</SegmentedControlItem>
<SegmentedControlItem id="popular">Popular</SegmentedControlItem>
</SegmentedControl>
<SegmentedControl aria-label="Sort order" defaultValue="new" size="lg">
<SegmentedControlItem id="new">Newest</SegmentedControlItem>
<SegmentedControlItem id="popular">Popular</SegmentedControlItem>
</SegmentedControl>
</div>
);
}Controlled
Use value and onChange to manage the selection in state.
Selected: Monthly
segmented-control.tsx
import { useState } from "react";
import { Paragraph, SegmentedControl, SegmentedControlItem } from "@ginga-ui/core";
export function ControlledExample() {
const [plan, setPlan] = useState("monthly");
return (
<div>
<SegmentedControl
aria-label="Billing period"
value={plan}
onChange={setPlan}
>
<SegmentedControlItem id="monthly">Monthly</SegmentedControlItem>
<SegmentedControlItem id="yearly">Yearly</SegmentedControlItem>
</SegmentedControl>
<Paragraph>
Selected: {plan === "monthly" ? "Monthly" : "Yearly"}
</Paragraph>
</div>
);
}Props
SegmentedControl
selectionMode, disallowEmptySelection, selectedKeys, defaultSelectedKeys, and onSelectionChange cannot be passed. Use value and onChange instead.
| Name | Type | Default | Description |
|---|---|---|---|
value | string | - | id of the selected item (controlled). |
defaultValue | string | - | id of the item selected initially (uncontrolled). |
onChange | (value: string) => void | - | Called with the id of the newly selected item. |
size | "sm" | "md" | "lg" | "md" | Size of the control. |
disabled | boolean | false | Disables every item. |
Also accepts the props of React Aria's ToggleButtonGroup (opens in a new tab).
SegmentedControlItem
| Name | Type | Default | Description |
|---|---|---|---|
id | string | - | Required. Value of the item, used by value and onChange. |
disabled | boolean | false | Disables the item. |
Also accepts the props of React Aria's ToggleButton (opens in a new tab).