Components · FormNot verified with Figma

Pill segment control

A pill segment control switches between two to four related views or filters in the same surface. A pill slides to the active segment.

How to add a pill segment control to a page. The files are components/css/pill-segment-control.css and components/js/pill-segment-control.js.

Basic Usage

Link the shared files once per page. Never copy the rules into a page, and never position the pill yourself.

StepWhat to do
1. Link<link rel="stylesheet" href="/components/css/pill-segment-control.css"> and <script src="/components/js/pill-segment-control.js"></script>. Both templates already have them.
2. MarkupA div.pill-segment-control[role="group"] with an aria-label, a span.pill-segment-control__thumb and two to four button.pill-segment-control__btn.
3. Active segmentpill-segment-control__btn--active and aria-pressed="true" on one button.
4. ReactListen for the segment-change event and swap the content.
5. SizeLarge is the default. Add pill-segment-control--md, --sm or --xs.

Pill segment control

The markup has no motion. The pill’s slide is set in Motion on Examples & Guidelines.


API

Classes

ClassUse
pill-segment-controlThe track. Large.
pill-segment-control--md, --sm, --xsMedium, Small, XSmall.
pill-segment-control__thumb, --lightThe pill. --light is the white pill.
pill-segment-control__btn, --activeA segment.

Events and custom properties

NameUse
segment-changeBubbling event on the button’s ancestors. e.detail.button is the segment that was clicked.
--sg-duration, --sg-easeDuration and easing of the pill. Defaults: duration-medium-2 and ease-standard.
ECO_SEGMENT.init(root)Wires controls added later.

Things to know

  • The script measures the active button, so the pill follows buttons of different widths.
  • Below 640px the control is fluid; from 640px it shrinks to its content. If the parent is a flex column with align-items: stretch, align-self: flex-start is needed (it is in the CSS).
  • Hover is forced on the documentation pages with data-state="hover"; it does nothing elsewhere.

CSS / Tailwind

The component is one CSS file and one script, shared by every page. Link them, never copy them.

CSS (components/css/pill-segment-control.css)
JS (components/js/pill-segment-control.js)