Primitives

ToggleGroup

A compact set of mutually exclusive options, built on native radio inputs

Radio

basic
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-basic"
  label="Delivery"
  options={[
    { code: 'standard', label: 'Standard', selected: true },
    { code: 'express', label: 'Express' },
    { code: 'same-day', label: 'Same day', disabled: true },
    { code: 'pickup', label: 'Pickup' },
  ]}
/>

Listening to selection:change

ToggleGroup is a single selection group, so a subscriber sees the same payload it would get from a single-mode ItemsGrid. Both instances below publish to one selection group, so choosing in either updates the other. Instance B marks no option as selected, so it does not clear A’s selection when it connects. The two textures can share a group.

Instance A · radio · Gloss preselected

Instance B · cell · no option marked selected, so it shows the group's current Gloss

No change yet
events
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';

const finishOptions = [
  { code: 'matte', label: 'Matte' },
  { code: 'gloss', label: 'Gloss' },
  { code: 'satin', label: 'Satin' },
];
---

<div class="flex flex-col gap-4">
  <div class="flex flex-col gap-2">
    <p class="text-sm text-muted">Instance A · radio · Gloss preselected</p>
    <ToggleGroup
      group="togglegroup-doc-events"
      label="Finish"
      options={finishOptions.map((option) => ({ ...option, selected: option.code === 'gloss' }))}
    />
  </div>
  <div class="flex flex-col gap-2">
    <p class="text-sm text-muted">
      Instance B · cell · no option marked selected, so it shows the group's current Gloss
    </p>
    <ToggleGroup
      group="togglegroup-doc-events"
      label="Finish"
      appearance="cell"
      variant="outline"
      options={finishOptions}
    />
  </div>
  <div
    data-selection-group="togglegroup-doc-events"
    data-selection-mode="single"
    class="mt-4 flex items-center gap-4"
  >
    <button
      type="button"
      data-selection-external="satin"
      class="px-3 py-1 border border-border rounded"
    >
      Select Satin externally
    </button>
    <button
      type="button"
      data-selection-external="walnut"
      class="px-3 py-1 border border-border rounded"
    >
      Publish a foreign code
    </button>
    <output data-selection-log class="font-mono text-sm">
      No change yet
    </output>
  </div>
</div>

<script>
  import { getSelection, publish, subscribe } from '@alfadormx/blocks/selection';

  const GROUP = 'togglegroup-doc-events';
  const SECTION = '[data-doc-section="events"]';

  function initLog(): void {
    const log = document.querySelector<HTMLOutputElement>(`${SECTION} [data-selection-log]`);
    if (!log) return;
    // Blocks publish their initial selection before this script subscribes.
    const current = getSelection(GROUP);
    if (current.length) {
      log.textContent = JSON.stringify({ group: GROUP, mode: 'single', selection: current });
    }
    subscribe(GROUP, (detail) => (log.textContent = JSON.stringify(detail)));
    document
      .querySelectorAll<HTMLButtonElement>(`${SECTION} [data-selection-external]`)
      .forEach((button) => {
        // onclick, not addEventListener: re-running initLog replaces the handler instead of stacking it.
        button.onclick = () => publish(button, [{ code: button.dataset.selectionExternal! }]);
      });
  }

  document.addEventListener('astro:after-swap', initLog);
  initLog();
</script>

labelledBy

Engraving font

labelledby
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<div>
  <h4 id="togglegroup-labelledby-heading">Engraving font</h4>
  <ToggleGroup
    group="togglegroup-doc-labelledby"
    label="Font"
    labelledBy="togglegroup-labelledby-heading"
    options={[
      { code: 'serif', label: 'Serif', selected: true },
      { code: 'script', label: 'Script' },
    ]}
  />
</div>

Cell variants

solid

outline

ghost

cell
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<div class="flex flex-col gap-4">
  {(['solid', 'outline', 'ghost'] as const).map((variant) => (
    <div class="flex flex-col gap-2">
      <p class="text-sm text-muted">
        <code>{variant}</code>
      </p>
      <ToggleGroup
        group={`togglegroup-doc-cell-${variant}`}
        label="Reveal price"
        appearance="cell"
        variant={variant}
        options={[
          { code: 'yes', label: 'Yes' },
          { code: 'no', label: 'No', selected: true },
        ]}
      />
    </div>
  ))}
</div>

Neutral cells

intent="neutral" selects with the emphasis colour in every cell variant.

solid

outline

ghost

cell-neutral
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<div class="flex flex-col gap-4">
  {(['solid', 'outline', 'ghost'] as const).map((variant) => (
    <div class="flex flex-col gap-2">
      <p class="text-sm text-muted">
        <code>{variant}</code>
      </p>
      <ToggleGroup
        group={`togglegroup-doc-cell-neutral-${variant}`}
        label="Reveal price"
        appearance="cell"
        variant={variant}
        intent="neutral"
        options={[
          { code: 'yes', label: 'Yes' },
          { code: 'no', label: 'No', selected: true },
        ]}
      />
    </div>
  ))}
</div>

Vertical

vertical
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-vertical"
  label="Warranty"
  orientation="vertical"
  options={[
    { code: 'none', label: 'No warranty', selected: true },
    { code: '1y', label: '1 year' },
    { code: '3y', label: '3 years' },
  ]}
/>

Icons

media
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-media"
  label="View"
  appearance="cell"
  options={[
    { code: 'grid', label: 'Grid', icon: { name: 'heroicons:squares-2x2' }, selected: true },
    {
      code: 'list',
      label: 'List',
      icon: { name: 'heroicons:list-bullet' },
      iconPosition: 'right',
    },
  ]}
/>

Image-only cells

With hideLabel, the label is screen-reader-only but still names the input, and the image keeps its own size. An image without alt gets alt="".

font
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-font"
  label="Engraving font"
  appearance="cell"
  variant="outline"
  options={['serif', 'script'].map((code) => ({
    code,
    label: code === 'serif' ? 'Serif' : 'Script',
    hideLabel: true,
    image: {
      src: 'https://images.unsplash.com/photo-1506905925346-21bda4d32df4',
      width: 96,
      height: 48,
    },
  }))}
/>

Custom state config

custom
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-custom"
  label="Colour"
  appearance="cell"
  variant="outline"
  defaultOptionConfig={{ class: 'px-6' }}
  selectedOptionConfig={{ class: 'bg-accent/10 border-accent text-accent' }}
  disabledOptionConfig={{ class: 'line-through' }}
  options={[
    { code: 'black', label: 'Black', selected: true },
    { code: 'white', label: 'White' },
    { code: 'red', label: 'Red', disabled: true },
  ]}
/>

Disabled group

disabled
---
import ToggleGroup from '@alfadormx/blocks/primitives/ToggleGroup.astro';
---

<ToggleGroup
  group="togglegroup-doc-disabled"
  label="Shipping"
  disabled
  options={[
    { code: 'standard', label: 'Standard' },
    { code: 'express', label: 'Express' },
  ]}
/>

Props

appearance is 'radio' (default) or 'cell'; a cell also takes variant: 'solid' (default), 'outline' or 'ghost'. Option presentation is layered in order: internal defaults, defaultOptionConfig, selectedOptionConfig (selected state only), the option itself, then disabledOptionConfig (disabled options only). One of label / labelledBy is required; labelledBy wins. Invalid configurations throw at build time.

API Reference

PropTypeDefaultDescription
groupstring—Selection group name this block publishes to and follows.
labelstring—Accessible name of the group. One of `label` / `labelledBy` is required.
labelledBystring—Id of the element naming the group; wins over `label`.
optionsToggleGroupOption[]—The mutually exclusive options.
size'xs' | 'sm' | 'md' | 'lg' | 'xl''md'Size, matching Button (default: 'md').
shape'rounded' | 'square' | 'pill''rounded'Corner shape, matching Button (default: 'rounded'). The radio marker stays circular.
intent'primary' | 'secondary' | 'tertiary' | 'accent' | 'success' | 'warning' | 'error' | 'neutral''primary'Color intent of the selected state and focus ring, matching Button (default: 'primary').
orientation'horizontal' | 'vertical''horizontal'Arrangement of the options (default: 'horizontal').
disabledbooleanfalseDisables every option (default: false).
defaultOptionConfigLayerConfig<ToggleGroupOptionConfig>—Presentation layered onto every option, both states.
selectedOptionConfigLayerConfig<ToggleGroupOptionConfig>—Presentation layered onto the selected state only.
disabledOptionConfigLayerConfig<ToggleGroupOptionConfig>—Presentation layered onto disabled options, last.
classstring''Extra classes on the group root.