Segmented control

On this page

A segmented control picks one of two to five options that change what the page shows: a period (Day, Week, Month), a view (Chart, Table) or a scope. The kit keeps the selection in step and tells your code when it changes.

A segmented controlA segmented control

Figure 1: A segmented control with Week selected

<div class="ph-segmented" role="group" aria-label="Period" id="period">
  <button type="button" value="day" aria-pressed="false">Day</button>
  <button type="button" value="week" aria-pressed="true">Week</button>
  <button type="button" value="month" aria-pressed="false">Month</button>
</div>
document.getElementById('period').addEventListener('ph-change', (event) => {
  loadDashboard(event.detail.value); // 'day', 'week' or 'month'
});

Behaviour

  • Clicking a button sets aria-pressed="true" on it and false on the others.
  • The group fires a ph-change event. event.detail.value is the button's value, or its text when it has no value.
  • Each option is a normal button, so Tab moves between them and Space or Enter picks one.

React

import { SegmentedToggle } from 'ph-app-kit-react';

<SegmentedToggle
  ariaLabel="Period"
  value={period}
  onChange={setPeriod}
  options={[{ value: 'day', label: 'Day' }, { value: 'week', label: 'Week' }, { value: 'month', label: 'Month' }]}
/>

Guidelines

  • Two to five short options. For more, use a select.
  • Label the group with aria-label ("Period").
  • Use it to change a view, not to submit something. For switching between sections of a page, use tabs.