Skip to content

Pill Group

A wrapping row of pills where exactly one is chosen, such as a frequency or a mode.

Decision

When Use
A short list of exclusive choices Pill Group
Choices where any number may be on Toggle Group
Options that each need a sentence Choice Group

Usage

<div class="stolon-pill-group" id="frequency" role="group" aria-label="Frequency">
    <button type="button" class="stolon-pill" data-value="daily" aria-pressed="false">Daily</button>
    <button type="button" class="stolon-pill" data-value="weekly" aria-pressed="true">Weekly</button>
</div>
import { createPillGroup } from './configurationpage?name=myplugin_stolon.js';

var frequency = createPillGroup(view.querySelector('#frequency'), { onChange: function (value) { tracker.check(); } });
frequency.set(config.Frequency);

Classes: stolon-pill-group, stolon-pill. The handle is { get, set, destroy }.

States and Modifiers

  • get() returns the data-value of the chosen pill, or its text when it has none. It returns null when none is chosen.
  • set(value) chooses a pill without calling onChange. An unknown value clears the choice.
  • The chosen pill carries aria-pressed="true" and stolon-selected in step. Choosing it again does nothing, and a disabled pill cannot be chosen.
  • The markup works with no JavaScript when the page sets aria-pressed itself.
  • small and large on a pill take the small and large steps of the size scale.

Gotchas

  • createPillGroup throws without a group element.

Reference

Generated from the code by scripts/build-docs.mjs. Edit the code, not this section.

createPillGroup

function createPillGroup(groupEl: HTMLElement, options?: PillGroupOptions): PillGroupHandle

Wires a stolon-pill-group of buttons as a single choice, keeping aria-pressed and the stolon-selected class on the chosen pill. It reports each change to onChange.

Option Type What it does
onChange (value: string | null): void
Handle member Type
get (): string | null
set (value: string | null): void
destroy (): void
Class What it looks like
stolon-pill-group A wrapping row of pills where exactly one is chosen, wired by createPillGroup. The toggle group shares the row
stolon-pill One pill, outlined when off and filled with the accent and white text when aria-pressed is true or it has selected
stolon-pill.stolon-small A pill at the small or large size of the scale

Example

Example Code

<!-- title: Pill Group -->
<div class="stolon-pill-group" id="frequency" role="group" aria-label="Frequency">
    <button type="button" class="stolon-pill" data-value="daily" aria-pressed="false">Daily</button>
    <button type="button" class="stolon-pill" data-value="weekly" aria-pressed="false">Weekly</button>
    <button type="button" class="stolon-pill" data-value="monthly" aria-pressed="false">Monthly</button>
</div>
<span id="frequencyStatus" class="stolon-status-text"></span>
<script type="module">
const base = await window.__baseReady;
const shared = base.createShared(document.getElementById('view'), 'preview-plugin', 'Preview');
const frequency = base.createPillGroup(document.getElementById('frequency'), {
    onChange: (value) => shared.setStatus('frequencyStatus', 'Chose ' + value + '.', false, { timeout: 0 })
});
frequency.set('weekly');
</script>