Segmented control

A Segmented control lets users switch between related views or modes within a compact control.

UsageCodeAccessibility
DayWeekMonth
<sl-segmented-control aria-label="Timetable view" value="week">
  <sl-segmented-control-item value="day">
    <sl-icon name="far-calendar-day" slot="icon"></sl-icon>
    <sl-icon name="fas-calendar-day" slot="selected-icon"></sl-icon>
    Day
  </sl-segmented-control-item>
  <sl-segmented-control-item value="week">
    <sl-icon name="far-calendar-week" slot="icon"></sl-icon>
    <sl-icon name="fas-calendar-week" slot="selected-icon"></sl-icon>
    Week
  </sl-segmented-control-item>
  <sl-segmented-control-item value="month">
    <sl-icon name="far-calendar-days" slot="icon"></sl-icon>
    <sl-icon name="fas-calendar-days" slot="selected-icon"></sl-icon>
    Month
  </sl-segmented-control-item>
</sl-segmented-control>

Similar components

Segmented control has similar behaviour to 2 other components: Tabs and Toggle group. To ensure good user experience and accessibility it is important to choose the appropriate component for a given scenario.

Differences

  • Segmented control changes how the content is presented. One section is always active.
  • Tabs change which content is shown. One section is always active. Can be linked to.
  • Toggle group applies an action to content, or filters the content (chips variant). Any number of sections can be active, including none.

How to decide

Most important:

  • Does choosing an option show a type of content that wasn't on the page before? Yes → Tabs
  • Could a user want to select two of the options at once, or none at all? Yes → Toggle group (actions) / to be decided (filters)
  • Can the user apply different actions to content between options? Yes → Tabs

Also check:

  • Is each option an action being applied, rather than a value being chosen? Yes → Toggle group
  • Do you need a URL to a selected option? Yes → Tabs
  • Do you need more than 5 options? Yes → Tabs / Select

Use a segmented control only when all answers are no.

Comparison table

AspectTabsSegmented controlToggle group
What changesWhich content is shownHow the content is displayedWhat action is applied to the content
Selection modelAlways one option selectedAlways one option selectedOne, several or no options selected
"Nothing selected" valid?NoNoYes
Linkable / routableYesNoNo
Option countMany (with overflow)2–5, fixedSmall, fixed
Overflow handling"More" buttonAutomatically switches to SelectNone
ContentLabel, icon or bothLabel, icon or bothLabel, icon or both
Typical placementAbove the section it controlsAbove or beside the content it rendersToolbars, above the content it acts on

Anatomy

ItemNameDescriptionOptional
1ContainerHolds the controls.no
2ControlOne option. One control is selected at a time, and one is selected by default.no
3LabelShown in the icon + text and text only variants.yes
4IconShown in the icon + text and icon only variants.yes

Content

Sets what each control shows.

  • Icon + text (default): icon and label.
  • Text only: label only.
  • Icon only: icon only. Has a shorter selection indicator.

Emphasis

  • Subtle (default): the selected control has a short selection indicator under it.

  • Bold: use to put more visual emphasis on the component, when:

    • it's the page's main view switch,
    • it's the only segmented control on the screen,
    • it isn't placed under tabs, and
    • it isn't next to a primary button.

    Bold emphasis can be also used in applications targeting younger users.

Size

  • Medium (md) (default)
  • Large (lg): use in touch-first and child-facing products, and for a page's single main view switch in a spacious layout.

Width

  • Hug (default): width of each control is set by the control with longest content
  • Fill: component fills the width of its parent container. use to put more visual emphasis on the component, or to fit it better within page layout.

Shape

Visual treatment of the segmented control. Use the value that matches the context.

  • Rect (default)
  • Pill

Fill

Visual treatment of the segmented control. Use the value that matches the context.

  • Solid (default)
  • Outline

Number of controls

  • 2 to 5 controls. Controls 3, 4 and 5 can be turned on or off; all 5 are on by default.

Content guidelines

  • Use short, clear labels.
  • Keep labels to 1–2 words. They may be longer in other languages and break the layout.
  • Use the icon-only variant only when icons for the provided options are universally recognizable.
  • Replace with Select when there isn't enough horizontal space for a Segmented control.
  • Tabs: change which content is shown. Use for navigation, linkable options or a different subject.
  • Toggle group: applies actions to content. Any number of options can be active, including none.
  • Select: for more than 5 options, and the fallback when the segmented control doesn't fit.
  • Radio group: for a choice that is part of a form.
  • Switch: for a single on/off setting.
  • Combobox: for long option labels.
OverviewGradesAttendance
<sl-segmented-control
  aria-label="Class report"
  emphasis="bold"
  fill="outline"
  shape="pill"
  size="lg"
  value="grades">
    <sl-segmented-control-item value="overview">Overview</sl-segmented-control-item>
    <sl-segmented-control-item value="grades">Grades</sl-segmented-control-item>
    <sl-segmented-control-item value="attendance">Attendance</sl-segmented-control-item>
</sl-segmented-control>

Segmented control item API

A single option in the segmented control. It can show a label, an icon or both. Use the icon and selected-icon slots to set the icons.

Properties

NameAttributeTypeDefaultDescription
ariaDisabledStatearia-disabledstring | undefinedWhether the option is aria-disabled. When set, the option stays focusable but is inactive (disabled).
valuevaluestringThe value of the option.

Slots

NameDescription
defaultThe label of the option.
iconThe icon shown before the label.
selected-iconThe icon shown instead of the icon when the option is selected.

Segmented control API

The segmented control is the wrapper for the options of one value. Only one option can be selected at a time. Use the properties to set the size, emphasis, fill, shape and so on.

Properties

NameAttributeTypeDefaultDescription
emphasisemphasis'subtle' | 'bold''subtle'The emphasis of the selected option.
fillfill'outline' | 'solid''solid'The fill of the control.
iconOnlyicon-onlybooleanfalseWhether the segmented control should hide option labels and render only icons.
shapeshape'pill' | 'rect''rect'The shape of the control.
sizesize'md' | 'lg''md'The size of the control.
valuevaluestring | undefinedThe value of the selected option.

Methods

NameParametersReturnDescription
removeAttributename: stringvoid

Events

NameEvent typeDescription
sl-changeSlChangeEvent<string>Emits when the selected option changes.

Slots

NameDescription
defaultThe sl-segmented-control-item elements.

CSS Parts

NameDescription
selectThe sl-select that is shown when the options don't fit. Use it to change the width of the select, for example to let it fill the full width of the control.

Keyboard interactions

Use Tab to move focus into or out of the control. Focus enters on the selected option, or the first option if none is selected. Use the left and right arrow keys to move between options; navigation wraps. Press Space or Enter to activate the focused option.

CommandDescription
TabMoves focus into the segmented control (to the first element, or selected element when one exists) and out of it.
Arrow LeftMoves focus to the previous focusable element in the segmented control.
Arrow RightMoves focus to the next focusable element in the segmented control.
HomeMoves focus to the first item.
EndMoves focus to the last item.
Enter/SpaceActivates the focused item.

Keyboard interactions when options do not fit the screen

When the options don’t fit in the available space, the segmented control switches to a select. Detailed information about keyboard interactions for that component can be found in the select documentation.

WAI-ARIA

In the component itself we use multiple aria-attributes to assure the component works well with a range of assistive technologies. For some attributes however it is not possible for the Design System to add a meaningful value, because it relies on the context or way a component is used.

Attributes that we recommend you add in certain scenarios are mentioned below.

The component sets aria-current="true" on the selected option and aria-current="false" on the other options. This state is managed automatically; you don’t need to set aria-current yourself.

AttributeValueDescription
aria-labelstringThe aria-label can be added on the segmented control to describe its purpose. Individual button labels are provided with text. For icon only variants this text is not visible, but it's announced by screen readers. There is no need to add aria-label to buttons inside segmented control.
aria-labelledbystringUse aria-labelledby to name the segmented control by referencing an existing element, rather than providing a label string with aria-label.
aria-disabledbooleanAnnounces the segmented control item as disabled with a screen reader, prevents it from being selected and dims it while keeping it focusable.