PeakUI

SegmentedControl

Picks one option from a small set, with an indicator that slides to the chosen segment

Usage

import {SegmentedControl} from "@peakui/react";

Each segment selects by its id, which is also the key in selectedKey and defaultSelectedKey.

Use SegmentedControl when the choice changes what the interface shows — a theme, a view mode, a chart period, a table density. It is not a form control: nothing is submitted, and there is no name or value. For a choice that travels with a form, use RadioGroup. For panels that swap with the selection, use Tabs. For several independent toggles, or for multiple selection, use ToggleButtonGroup.

An accessible name is required, because the control carries no visible label: pass aria-label, or aria-labelledby pointing at a label you render yourself.

Anatomy

import {SegmentedControl} from "@peakui/react";

export default () => (
  <SegmentedControl aria-label="View" defaultSelectedKey="grid">
    <SegmentedControl.Item id="grid">Grid</SegmentedControl.Item>
    <SegmentedControl.Item id="list">List</SegmentedControl.Item>
    <SegmentedControl.Item id="table">Table</SegmentedControl.Item>
  </SegmentedControl>
);

The indicator — the capsule around the selected segment — is rendered for you and is not a part you place. It animates from the previous segment to the new one, and respects prefers-reduced-motion: reduce, where it appears without animating.

The selection can never be cleared: SegmentedControl fixes single selection and refuses an empty one, so clicking the selected segment again keeps it selected. It does not follow that a segment is always selected — a control with neither selectedKey nor defaultSelectedKey, or one whose selectedKey matches no segment, starts with nothing selected and renders no indicator. Passing selectedKey={undefined} keeps the control controlled with nothing selected, which is how a switcher waits for a value it does not have yet.

Examples

Sizes

size names the outer height of the capsule, matching a Button of the same size, so the control lines up in a row with buttons.

At sm a segment is 24×24 on desktop — exactly the minimum of WCAG 2.2 AA SC 2.5.8 Target Size, with no room to spare. Prefer md in headers, toolbars, and anywhere pointer precision matters; keep sm for dense layouts you control.

Icon-only segments

isIconOnly squares the segment, so the capsule reads as a circle rather than an oval. An icon carries no accessible name, so give each segment its own aria-label.

Controlled

onSelectionChange hands over the newly selected key itself, not a set.

Disabled State

isDisabled on the control disables every segment; on a segment it disables that one.

Width

The control hugs its content. Pass className="w-full" and it fills its container, sharing the width equally between segments — there is no fullWidth prop, because a segment distributes by design. A label is never truncated: in a container narrower than its content the segments keep their labels and overflow instead.

<SegmentedControl aria-label="Period" className="w-full" defaultSelectedKey="day">
  <SegmentedControl.Item id="day">Day</SegmentedControl.Item>
  <SegmentedControl.Item id="week">Week</SegmentedControl.Item>
  <SegmentedControl.Item id="month">Month</SegmentedControl.Item>
</SegmentedControl>

Keyboard

KeyBehavior
TabMoves focus into the control, onto the selected segment
← →Moves focus between segments without changing the selection
Space EnterSelects the focused segment

The control reports itself as a radio group, while arrow keys move focus without selecting. ARIA practices describe a radio group whose arrow keys also select; this behavior comes from the underlying React Aria primitive and is documented here rather than worked around, so it stays consistent with every other grouped control in the library.

Customization

Tailwind CSS

Pass utilities through className. They are merged with the control's own classes.

<SegmentedControl aria-label="View" className="w-full" defaultSelectedKey="grid">
  <SegmentedControl.Item className="font-semibold" id="grid">
    Grid
  </SegmentedControl.Item>
  <SegmentedControl.Item id="list">List</SegmentedControl.Item>
</SegmentedControl>

CSS Variables

Each custom property is the supported way to retheme the control.

@layer components {
  .segmented-control {
    --segmented-control-bg: var(--color-surface-secondary);
    --segmented-control-radius: var(--radius-lg);
    --segmented-control-indicator-bg: var(--color-background);
    --segmented-control-indicator-radius: var(--radius-md);
    --segmented-control-indicator-shadow: none;
  }
}
VariableDefaultControls
--segmented-control-bgvar(--color-default)The capsule's background
--segmented-control-radiusvar(--radius-3xl)The capsule's corner radius
--segmented-control-item-fgvar(--color-muted)An unselected segment's label
--segmented-control-item-fg-selectedvar(--color-segment-foreground)The selected segment's label
--segmented-control-indicator-bgvar(--color-segment)The sliding indicator's background
--segmented-control-indicator-radiusvar(--radius-3xl)The indicator's corner radius
--segmented-control-indicator-shadowvar(--shadow-surface)The indicator's shadow

Global CSS

Override the classes in the components layer.

@layer components {
  .segmented-control__item {
    @apply px-6;
  }
}

Styling Reference

PeakUI follows the BEM methodology, so variants and states are plain class names you can target.

CSS Classes

The SegmentedControl component uses these CSS classes (view source styles):

Base & Layout Classes

  • .segmented-control - The capsule container, and the md size
  • .segmented-control__item - One segment
  • .segmented-control__indicator - The sliding indicator behind the selected segment

Modifier Classes

  • .segmented-control--sm - Small size
  • .segmented-control--md - Medium size (default)
  • .segmented-control--lg - Large size
  • .segmented-control__item--icon-only - Square segment for a lone icon

States

State lives on the segment as data attributes:

  • [data-selected] - The selected segment
  • [data-hovered] - Pointer over an unselected, enabled segment
  • [data-focus-visible] - Keyboard focus
  • [data-disabled] - Disabled segment, or every segment of a disabled control

Slots

  • [data-slot="segmented-control"]
  • [data-slot="segmented-control-item"]
  • [data-slot="segmented-control-indicator"]

API Reference

SegmentedControl

PropTypeDefaultDescription
selectedKeyKey-Controlled selection
defaultSelectedKeyKey-Initial selection (uncontrolled)
onSelectionChange(key: Key) => void-Called with the newly selected key
size'sm' | 'md' | 'lg''md'Outer height of the capsule
isDisabledboolean-Disables every segment
aria-labelstring-Accessible name. Required unless aria-labelledby is given
classNamestring-Additional CSS classes
childrenReact.ReactNode-SegmentedControl.Items, each selecting by its id

Single selection is fixed and the selection can never be empty, so selectionMode and disallowEmptySelection are not accepted. orientation is not accepted either: the control is horizontal.

Every other React Aria ToggleButtonGroup prop is accepted.

SegmentedControl.Item

PropTypeDefaultDescription
idKey-The segment's key, used by selectedKey. Required — without it the segment leaves the group's selection entirely
isDisabledboolean-Disables this segment
isIconOnlybooleanfalseSquares the segment for a lone icon, so the capsule is a circle
aria-labelstring-Accessible name. Required when the segment holds only an icon
classNamestring-Additional CSS classes
childrenReact.ReactNode-The segment's label, an icon, or both

The indicator is rendered inside each segment automatically; there is no SegmentedControl.Indicator to place.

On this page