Web and native

SegmentedButton in kanso-ui 0.59.0, for React.

import { SegmentedButton } from '@kanso-labs/kanso-ui'

A row of two to five joined segments, of which one, or with selectionMode="multiple" several, is chosen.

It follows the Material Design segmented buttons page's outlined set: one 40px track with a shared 1dp rule, fully rounded ends and equal-width segments, and a check before a chosen segment's label. Two deliberate departures: choosing one behaves as a full radio group, and with the check turned off a chosen segment is drawn on the stronger secondary colour, since the spec's own container is too faint to show the choice alone.

What you provide

  • children: two to five SegmentedButton.Segments, each with an id, which is the key selection is reported by.
  • aria-label or aria-labelledby, one of which is required: nothing else names the set.

Props

PropTypeDefaultDescription
classNameToggleButtonGroupProps['className']—A function may compute the class from the set's render state.
disallowEmptySelectionbooleantrue while one is chosen, false while several may beWhether the chosen segment stays chosen when it is pressed again. On by default while one is chosen, since a radio group always holds a choice; `false` lets a press clear it, as React Aria's own default does. Off by default while several may be, where any of them can be turned off.
showSelectedIconbooleanfalseDeprecated: Kanso's chosen segment draws no check, since its lifted surface and heavier label say which it is. `true` still draws Material Design's check before a chosen segment's label, in place of its icon, and warns once in development.
styleToggleButtonGroupProps['style']—A function may compute the style from the set's render state.

Also takes the props of LabelledByAttribute, Omit<ToggleButtonGroupProps, 'className' | 'disallowEmptySelection' | 'orientation' | 'style'>.

Parts

  • SegmentedButton.Segment: one segment. Its children are the label and its id (required) the selection key: a segment without one would keep a pressed state of its own, apart from the group's selection. icon sets an 18dp icon before the label, replaced by the check while chosen; given with no label, the icon is the segment, kept while chosen, and the segment needs an aria-label. isDisabled turns off one segment; disableRipple turns off its ripple.

Example

import { SegmentedButton } from '@kanso-labs/kanso-ui'

<SegmentedButton aria-label="Label" defaultSelectedKeys={['first']}>
  <SegmentedButton.Segment id="first">First item</SegmentedButton.Segment>
  <SegmentedButton.Segment id="second">Second item</SegmentedButton.Segment>
  <SegmentedButton.Segment id="third">Third item</SegmentedButton.Segment>
</SegmentedButton>

The design’s classes

k-seg, k-seg--fill, k-seg__item with aria-pressed; bundle.css gives aria-selected and aria-checked the same look, for tab and radio markup.

Maps to SegmentedButton in kanso-ui and react-native-kanso-ui; Kanso calls it SegmentedControl.