SegmentedControl
Toggle buttons for switching between different values or views.
Want to skip the docs? Try our MCP Server
#
Documentation
#
#
Usage
#
Use SegmentedControl and SegmentedControlItem components together to create sets of toggle buttons.
SegmentedControlItem extends the Button component.
import { IconGrid, IconRectangleList } from "@optiaxiom/icons";
import {
SegmentedControl,
SegmentedControlItem,
Tooltip,
} from "@optiaxiom/react";
export function App() {
return (
<SegmentedControl defaultValue="list">
<Tooltip content="List">
<SegmentedControlItem
aria-label="List"
icon={<IconRectangleList />}
value="list"
/>
</Tooltip>
<Tooltip content="Grid">
<SegmentedControlItem
aria-label="Grid"
icon={<IconGrid />}
value="grid"
/>
</Tooltip>
</SegmentedControl>
);
}#
Anatomy
#
import { SegmentedControl, SegmentedControlItem } from "@optiaxiom/react";
export default () => (
<SegmentedControl>
<SegmentedControlItem />
</SegmentedControl>
);#
Controlled
#
Use the value and defaultValue props to toggle between controlled and uncontrolled usage. And combine it with onValueChange to listen for changes to the state.
In single mode, clicking the active item deselects it and calls onValueChange with an empty string. To keep one item always active, as in a view switcher, ignore empty values in onValueChange.
"use client";
import { IconDesktop, IconMobile, IconTablet } from "@optiaxiom/icons";
import {
Button,
Group,
SegmentedControl,
SegmentedControlItem,
Tooltip,
} from "@optiaxiom/react";
import { useState } from "react";
export function App() {
const [value, setValue] = useState("desktop");
return (
<Group gap="16">
<SegmentedControl
onValueChange={(value: string) => value && setValue(value)}
value={value}
>
<Tooltip content="Desktop">
<SegmentedControlItem
aria-label="Desktop"
icon={<IconDesktop />}
key="desktop"
value="desktop"
/>
</Tooltip>
<Tooltip content="Tablet">
<SegmentedControlItem
aria-label="Tablet"
icon={<IconTablet />}
key="tablet"
value="tablet"
/>
</Tooltip>
<Tooltip content="Mobile">
<SegmentedControlItem
aria-label="Mobile"
icon={<IconMobile />}
key="mobile"
value="mobile"
/>
</Tooltip>
</SegmentedControl>
<Button
disabled={value === "desktop"}
onClick={() => setValue("desktop")}
>
Reset
</Button>
</Group>
);
}#
Related
#
Button
Button component is used to trigger actions.
ToggleButton
ToggleButton component represents a button that can be toggled on or off.
Tabs
Organize content into multiple sections with horizontal navigation.
#
Props
#
#
SegmentedControl
#
import { SegmentedControl } from "@optiaxiom/react";
// or
import { SegmentedControl } from "@optiaxiom/react/segmented-control";Supports all Box props in addition to its own. Renders a <div> element.
Prop |
|---|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
defaultValueThe value of the item that is pressed when initially rendered. Use
|
dir
|
disabledWhether the group is disabled from user interaction. @defaultValue false
|
loop
|
onValueChangeThe callback that fires when the value of the toggle group changes. The callback that fires when the state of the toggle group changes.
|
orientation
|
rovingFocusWhether the group should maintain roving focus of its buttons. @defaultValue true
|
typeWhether to allow multiple values or only activate one item at a time. In
Default: |
valueThe controlled stateful value of the item that is pressed. The controlled stateful value of the items that are pressed.
|
#
SegmentedControlItem
#
import { SegmentedControlItem } from "@optiaxiom/react";
// or
import { SegmentedControlItem } from "@optiaxiom/react/segmented-control";Supports all Button props in addition to its own. Renders a <button> element.
Prop |
|---|
addonAfterDisplay content inside the button after
|
addonBeforeDisplay content inside the button before
|
appearanceControl the appearance by selecting between the different button types.
|
asChildChange the default rendered element for the one passed as a child, merging their props and behavior. Read the Composition guide for more details.
|
children
|
className
|
disabledWhether the button is disabled.
|
iconDisplay an icon before or after the button content or omit
|
iconPositionControl whether to show the icon before or after the button content.
|
loadingShow a loading spinner and mark the button
|
sizeControl the size of the button.
|
squareWhether button should have square shape (an icon-only button with no label).
|
typeThe default behavior of the button.
|
value*A string value for the toggle group item. All items within a toggle group should use a unique value.
|
#
Accessibility
#
#
Keyboard interactions
#
Key | Description |
|---|---|
Tab | Moves focus to either the pressed item or the first item in the group. |
SpaceEnter | Activates/deactivates the item. |
ArrowDownArrowRight | Moves focus to the next item in the group. |
ArrowUpArrowLeft | Moves focus to the previous item in the group. |
HomeEnd | Moves focus to the first/last item. |
#
Changelog
#
#
0.2.0
#
- Added component