shadcn.io is an independent project built on shadcn/ui
On this page9 sections
A segment group is a short row of options where one is picked at a time, often called a segmented control, with a highlight that slides to the choice. It suits a setting that changes something in place: list or grid, monthly or yearly, day, week or month.
When each option should show its own panel, use Tabs. In a form, or when the options need a line of description, a Radio Group reads better.
Installation
Anatomy

Usage
<script lang="ts">
import {
SegmentGroup, SegmentGroupItem, SegmentGroupItemText,
} from "@/components/ui/segment-group";
</script>
const SegmentGroupDemo = () => (
<SegmentGroup
aria-label="Settings section"
className="rounded-lg"
defaultValue="Profile"
>
{items.map((item) => (
<SegmentGroupItem className="px-2 py-1.5 text-sm" key={item} value={item}>
<SegmentGroupItemText>{item}</SegmentGroupItemText>
</SegmentGroupItem>
))}
</SegmentGroup>
);
const items = ["Profile", "Account", "Security", "Notifications"];The group renders its own indicator, so the snippet has none. The selected SegmentGroupItem carries data-checked on every base and framework: style it with a variant such as data-checked:font-medium, and the other items with not-data-checked:.
Accessibility
SegmentGroup is a radiogroup and each item is a radio that reports whether it is checked, so a screen reader announces the options as one set and says which is on. The group is a single tab stop on the checked item, and the arrow keys move the choice. Naming the group is left to you: pass aria-label, or aria-labelledby with the id of a visible heading, to SegmentGroup. Without either, a screen reader announces a radio group with no name. An item that shows only an icon needs aria-label on its SegmentGroupItem.
- Tab
- Moves focus into the group, onto the checked item when there is one.
- Space
- When focus is on an unchecked item, checks it.
- Arrow Right
- Moves focus to the next item and checks it, skipping disabled items, and from the last item back to the first. Works in either orientation.
- Arrow Down
- Moves focus to the next item and checks it, skipping disabled items, and from the last item back to the first. Works in either orientation.
- Arrow Left
- Moves focus to the previous item and checks it, skipping disabled items, and from the first item to the last. Works in either orientation.
- Arrow Up
- Moves focus to the previous item and checks it, skipping disabled items, and from the first item to the last. Works in either orientation.
Demos
Controlled
Profile starts picked among four settings sections, and the parent's state holds the value. Each change hands over the new string first, and the demo writes it back. Hold the value when something else on the page follows the choice, such as a heading or the content below. Otherwise defaultValue is enough.
Custom indicator
The group already renders its indicator, so this demo restyles it instead of drawing a second pill: a class on SegmentGroup tints the element whose data-slot is segment-group-indicator with a translucent primary. Security is disabled, and the arrow keys skip it. Restyle the indicator this way to match a brand color or the surface behind the group.
Disabled
With disabled on SegmentGroup, every item is off, the selected Profile included, and the highlight stays where it was. Nothing answers a click or a key. Use it while the choice is shown but locked, for example until a form above it is valid. To lock a single option, disable that item and leave the rest alone.
Disabled item
Security has disabled on its own SegmentGroupItem, so a click does nothing and the arrow keys pass over it, while the other three still work. Use it for a section the current plan doesn't include, or a step the person hasn't reached yet. The item stays in the row, dimmed, so the missing choice isn't a mystery.
Form
Monthly and Yearly sit in a form under a Billing period heading, which names the group through aria-labelledby. With name set to billing, the checked item's value goes out with the form, and the line under Continue shows what was sent. Use it for a choice that travels with the rest of a form, such as a plan or a delivery speed.
Indicator on hover
The highlight follows the pointer: hovering an item points the controlled value at it, a click commits it, and leaving returns the highlight to the committed choice. There is no hover prop, only state. While the pointer rests on an item, that item is the group's value, so keep this to a view switcher that sits outside a form.
Orientation horizontal
Setting orientation to horizontal, the default, puts the four items in one row exactly as in the first demo, with the highlight sliding sideways. Writing it out helps when the orientation comes from a prop that is vertical on other screens. The row suits labels short enough to fit the width without wrapping.
Orientation vertical
orientation set to vertical stacks the four items in a column, each row as wide as the group, and the highlight moves up and down. Use a column in a sidebar, or when the labels are too long for one row. The arrow keys work as they do in a row, with Left and Right moving the choice as well as Up and Down.
Variant underline
variant set to underline drops the filled pill: the group draws a bottom border, and the indicator becomes a 2-pixel primary bar under the selected label. Use it when the row should read like section links on a settings page while still only changing a value. Leave variant out for the filled highlight.
Variant underline orientation vertical
The underline style also works in a column, with orientation set to vertical and variant set to underline. The border runs down the left edge and the indicator is a 2-pixel bar on it, beside the selected row. Use it for a settings sidebar that should look like a list of links, where a filled pill would be heavy in a tall stack.
FAQ
How do I control the selected Segment Group value in Svelte?
Keep the selected string in state and bind it with bind:value on SegmentGroup, or pass value and update it in onValueChange.
The callback receives the new string first and the change's details second, and the value is null while no item is checked.
The group's value is the value of the checked SegmentGroupItem, as the Controlled demo shows. Set defaultValue instead when nothing else needs to know, and leave both out to start with no item picked and no highlight.
Should I use a Segment Group, Tabs or a Radio Group?
Use a segment group for a short, single choice that changes something in place, such as a list or grid view or a date range, with labels that fit on one row.
Tabs fit when each option has a panel of its own, since they tie each trigger to its panel for screen readers. A Radio Group is the better choice for a question in a form, or when an option needs a sentence of explanation beside it.
How do I submit a Segment Group value with a form?
Pass name to SegmentGroup, and the checked item's value is sent under that name with the rest of the form. The Form demo does this for a billing period and shows what the form sent. With a form library, set its field from onValueChange instead.
UI SDK renders a hidden native radio for each item, and without a name nothing is sent.
How do I stack a Segment Group vertically?
Set orientation to vertical on SegmentGroup. The items stack, each stretches to the group's width, and the highlight moves up and down with the selection, as in the Orientation vertical demo.
All four arrow keys keep working in a column, so nobody has to know which way the group runs. For the underline look in a column, add variant set to underline too.
How do I make a Segment Group look like underlined tabs?
Set variant to underline on SegmentGroup. A horizontal group draws a bottom border with a 2-pixel bar under the selected label, and a vertical one draws the border and the bar down its left edge, as the Variant underline demo shows.
The look changes, the behavior doesn't: it is still a radio group that holds one value. When each option should reveal its own content, use Tabs, whose line variant draws a similar bar.
How do I style the selected Segment Group item and its sliding indicator?
The selected item carries data-checked on every base, so a data-checked: class styles it and not-data-checked: the rest. SegmentGroup renders the indicator itself, so restyle the element whose data-slot is segment-group-indicator instead of adding one, as the Custom indicator demo does.
The indicator is placed with --left, --top, --width and --height, which the component measures from the checked item, so keep the classes that read them. To change its speed, edit the duration class on SegmentGroupIndicator in components/ui/segment-group, which is your file once installed.
How do I disable one option in a Segment Group?
Set disabled on that SegmentGroupItem. It dims, ignores clicks, and the arrow keys pass over it, as Security does in the Disabled item demo. A disabled item can't become the value through the interface, though your code can still set it.
To lock every option at once, set disabled on SegmentGroup. The selected item keeps its highlight, so people can still read the current choice.
Is the UI SDK Segment Group accessible with a keyboard?
Yes. The group is one stop in the tab order, landing on the checked item, and the arrow keys move to the next or previous item and select it in either orientation, looping at the ends. Space checks the focused item when it isn't checked yet.
A screen reader needs a name for the group as well, so pass aria-label to SegmentGroup. The Accessibility section lists every key.
API Reference
SegmentGroup
<div> data-slot="segmented-control"
value?string | null
The value. Pair with onValueChange to control it. Two-way with bind:value.
- Type
string | null
defaultValue?string
The value it starts with, when you don't control value.
onValueChange?(details: { value: string | null }) → void
Called when the value changes.
- Type
(details: { value: string | null }) → void- Parameters
details– { value: string | null }- Returns
void
controller?{ getValue: () → T | null; setValue: (value: T | null, details?: SegmentedControlChangeOptions) → boolean; focus: () → void; }
Checks items from anywhere. Create one with createSegmentedControlController().
- Type
{ getValue: () → T | null; setValue: (value: T | null, details?: SegmentedControlChangeOptions) → boolean; focus: () → void; }
debug?boolean
Logs every state change with its reason to the console, in development builds.
disabled?boolean
Whether it's disabled.
- Default
false
form?string
The id of a form to belong to, when the segmented control isn't inside that form.
loop?boolean
Whether arrow keys wrap from the last item to the first.
- Default
true
name?string
The name the value submits under. Without one, the segmented control submits nothing.
orientation?Orientation
Orientation of the radio group.
- Default
"horizontal"
readOnly?boolean
Keeps the items focusable and the value submitted, but stops changes.
required?boolean
Blocks form submission until an item is checked, with the browser's own message.
variant?"default" | "underline"
The visual style.
- Type
"default" | "underline"- Default
"default"
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-disabled?string
Present when disabled, by its own prop, the control or a <fieldset>.
data-orientation"horizontal" | "vertical"
Which way the items run.
- Type
"horizontal" | "vertical"
data-readonly?string
Present when the control is read-only.
data-required?string
Present when the control is required.
SegmentGroupItem
<button> data-slot="segmented-control-item"
valuestring
The item's value.
disabled?boolean
Whether the item is disabled.
render?Snippet<[{ props, state, children }]>
Renders your own element: a snippet that gets the part's props to spread on it, its state, and children, its content.
- Type
Snippet<[{ props, state, children }]>
class?string
Merged with the part's classes.
data-checked?string
Present on the checked item.
data-disabled?string
Present when disabled, by its own prop, the control or a <fieldset>.
data-orientation?string
Mirrors orientation.
data-readonly?string
Present when the control is read-only.
data-required?string
Present when the control is required.
data-unchecked?string
Present on items that aren't checked.
SegmentGroupItemText
<span> data-slot="segment-group-item-text"
class?string
Merged with the part's classes.
SegmentGroupIndicator
<span> data-slot="segment-group-indicator"
class?string
Merged with the part's classes.
data-activation-direction"none"data-disabled?string
Present when disabled is set.
data-orientation"horizontal" | "vertical"
Which way the items run.
- Type
"horizontal" | "vertical"
data-visible?string
Present once the indicator has a position. Its variables are unset until then.
--heightstring
The measured height, for animating open and closed.
--leftstring--segmented-control-indicator-bottomstring
Distance from the control's bottom edge to the checked item.
--segmented-control-indicator-heightstring
Height of the checked item.
--segmented-control-indicator-leftstring
Distance from the control's left edge to the checked item.
--segmented-control-indicator-rightstring
Distance from the control's right edge to the checked item.
--segmented-control-indicator-topstring
Distance from the control's top edge to the checked item.
--segmented-control-indicator-widthstring
Width of the checked item.
--topstring--widthstring
The measured width, for animating open and closed.











