Tearsheet

Issue 01 · Forms · p.10

Segmented control

Switch between two to four views. Radios underneath, so it works without JavaScript.

Why it works: Best for mutually exclusive views of the same content (List / Board / Calendar). For more than four options use tabs or a select.

Try it:
Receipt: axe clean · contrast 6.09:1 · 1 tab stop · RTL ✓ · long text ✓

Tab order

  1. radio "List" [checked]

What assistive tech is given

- group "View":
  - text: View
  - radio "List" [checked]
  - text: List
  - radio "Board"
  - text: Board
  - radio "Calendar"
  - text: Calendar

Automated checks in CI (axe in Chromium, reduced motion on). They catch a lot, not everything: still try it with a screen reader.

See it in the zine npx -y tearsheet add segmented-control

Needs the shared tokens.css once per project.

HTML + CSS

index.html
<!-- tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html -->
<fieldset class="segmented">
  <legend class="sr-only">View</legend>
  <label><input type="radio" name="view" value="list" checked><span>List</span></label>
  <label><input type="radio" name="view" value="board"><span>Board</span></label>
  <label><input type="radio" name="view" value="calendar"><span>Calendar</span></label>
</fieldset>
styles.css
/* tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.segmented {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding: 4px;
  background: var(--ui-surface-2);
  border: 0;
  border-radius: calc(var(--ui-radius-sm) + 4px);
}
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: block;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ui-muted);
  border-radius: var(--ui-radius-sm);
  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
}
.segmented label:hover span { color: var(--ui-text); }
.segmented input:checked + span { color: var(--ui-text); background: var(--ui-surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
.segmented input:focus-visible + span { box-shadow: var(--ui-focus); }

React

SegmentedControl.jsx
/* tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html */
import './SegmentedControl.css';

export function SegmentedControl() {
  return (
    <fieldset className="segmented">
      <legend className="sr-only">View</legend>
      <label><input type="radio" name="view" defaultValue="list" defaultChecked /><span>List</span></label>
      <label><input type="radio" name="view" defaultValue="board" /><span>Board</span></label>
      <label><input type="radio" name="view" defaultValue="calendar" /><span>Calendar</span></label>
    </fieldset>
  );
}
SegmentedControl.css
/* tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.segmented {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding: 4px;
  background: var(--ui-surface-2);
  border: 0;
  border-radius: calc(var(--ui-radius-sm) + 4px);
}
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: block;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ui-muted);
  border-radius: var(--ui-radius-sm);
  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
}
.segmented label:hover span { color: var(--ui-text); }
.segmented input:checked + span { color: var(--ui-text); background: var(--ui-surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
.segmented input:focus-visible + span { box-shadow: var(--ui-focus); }

Vue

SegmentedControl.vue
<!-- tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html -->
<template>
  <fieldset class="segmented">
    <legend class="sr-only">View</legend>
    <label><input type="radio" name="view" value="list" checked><span>List</span></label>
    <label><input type="radio" name="view" value="board"><span>Board</span></label>
    <label><input type="radio" name="view" value="calendar"><span>Calendar</span></label>
  </fieldset>
</template>

<style scoped>
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.segmented {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding: 4px;
  background: var(--ui-surface-2);
  border: 0;
  border-radius: calc(var(--ui-radius-sm) + 4px);
}
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: block;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ui-muted);
  border-radius: var(--ui-radius-sm);
  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
}
.segmented label:hover span { color: var(--ui-text); }
.segmented input:checked + span { color: var(--ui-text); background: var(--ui-surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
.segmented input:focus-visible + span { box-shadow: var(--ui-focus); }
</style>

Svelte

SegmentedControl.svelte
<!-- tearsheet segmented-control v48655c56 · https://qusaialjbali.github.io/tearsheet/zine/parts/segmented-control.html -->
<fieldset class="segmented">
  <legend class="sr-only">View</legend>
  <label><input type="radio" name="view" value="list" checked><span>List</span></label>
  <label><input type="radio" name="view" value="board"><span>Board</span></label>
  <label><input type="radio" name="view" value="calendar"><span>Calendar</span></label>
</fieldset>

<style>
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}
.segmented {
  display: inline-flex;
  gap: 2px;
  margin: 0;
  padding: 4px;
  background: var(--ui-surface-2);
  border: 0;
  border-radius: calc(var(--ui-radius-sm) + 4px);
}
.segmented label { position: relative; }
.segmented input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.segmented span {
  display: block;
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  color: var(--ui-muted);
  border-radius: var(--ui-radius-sm);
  transition: background-color 0.15s, color 0.15s, box-shadow 0.15s;
}
.segmented label:hover span { color: var(--ui-text); }
.segmented input:checked + span { color: var(--ui-text); background: var(--ui-surface); box-shadow: 0 1px 3px rgb(0 0 0 / 0.12); }
.segmented input:focus-visible + span { box-shadow: var(--ui-focus); }
</style>