Skip to content

Toggle group ​

A set of toggles: several independent ones, or one choice from a set, built on native inputs.

Installation ​

bash
npx bootcn add toggle-group
bash
pnpm dlx bootcn add toggle-group
bash
yarn dlx bootcn add toggle-group
bash
bunx --bun bootcn add toggle-group

The CLI also installs the design tokens and these components: toggle. It prints the <link> and <script> tags to add to your layout.

Usage ​

For independent options (bold, italic), use checkboxes in a <div role="group" aria-label="…">. For one choice from a set (alignment), use radio buttons with the same name in a <fieldset> with a legend. Both use the Toggle component's labels.

Accessibility ​

  • Checkbox groups are announced as a named group; radio groups by their legend.
  • In a radio group, arrow keys move the choice, as in any native radio group.
  • Icon-only toggles have visually hidden names.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.
  • The toggle component.

Source ​

The exact files the CLI installs (toggle-group@1.0.0). View on GitHub · Report an issue

html
<div class="bootcn-toggle-group" role="group" aria-label="Text formatting">
  <input type="checkbox" class="btn-check" id="bootcn-toggle-group-bold" name="bold" />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-bold">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M7 5h6a3.5 3.5 0 0 1 0 7H7z" />
      <path d="M7 12h7a3.5 3.5 0 0 1 0 7H7z" />
    </svg>
    <span class="visually-hidden">Bold</span>
  </label>
  <input type="checkbox" class="btn-check" id="bootcn-toggle-group-italic" name="italic" checked />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-italic">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M19 4h-9" />
      <path d="M14 20H5" />
      <path d="M15 4 9 20" />
    </svg>
    <span class="visually-hidden">Italic</span>
  </label>
  <input type="checkbox" class="btn-check" id="bootcn-toggle-group-underline" name="underline" />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-underline">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M6 4v6a6 6 0 0 0 12 0V4" />
      <path d="M4 20h16" />
    </svg>
    <span class="visually-hidden">Underline</span>
  </label>
</div>

<fieldset class="bootcn-toggle-group mt-3">
  <legend class="visually-hidden">Text alignment</legend>
  <input
    type="radio"
    class="btn-check"
    id="bootcn-toggle-group-left"
    name="align"
    value="left"
    checked
  />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-left">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M21 6H3" />
      <path d="M15 12H3" />
      <path d="M17 18H3" />
    </svg>
    <span class="visually-hidden">Align left</span>
  </label>
  <input
    type="radio"
    class="btn-check"
    id="bootcn-toggle-group-center"
    name="align"
    value="center"
  />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-center">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M21 6H3" />
      <path d="M17 12H7" />
      <path d="M19 18H5" />
    </svg>
    <span class="visually-hidden">Align center</span>
  </label>
  <input type="radio" class="btn-check" id="bootcn-toggle-group-right" name="align" value="right" />
  <label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-group-right">
    <svg
      class="bootcn-icon"
      width="16"
      height="16"
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      stroke-width="2"
      stroke-linecap="round"
      stroke-linejoin="round"
      aria-hidden="true"
      focusable="false"
    >
      <path d="M21 6H3" />
      <path d="M21 12H9" />
      <path d="M21 18H7" />
    </svg>
    <span class="visually-hidden">Align right</span>
  </label>
</fieldset>
css
/* Joined toggles: one border, inner corners squared. */
.bootcn-toggle-group {
  display: inline-flex;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: var(--bootcn-radius);
  box-shadow: var(--bootcn-shadow-sm);
}

.bootcn-toggle-group > .bootcn-toggle {
  border-radius: 0;
  box-shadow: none;
}

.bootcn-toggle-group > .bootcn-toggle + .btn-check + .bootcn-toggle {
  margin-left: calc(var(--bootcn-border-width) * -1);
}

.bootcn-toggle-group > .btn-check:first-of-type + .bootcn-toggle {
  border-top-left-radius: var(--bootcn-radius);
  border-bottom-left-radius: var(--bootcn-radius);
}

.bootcn-toggle-group > .bootcn-toggle:last-child {
  border-top-right-radius: var(--bootcn-radius);
  border-bottom-right-radius: var(--bootcn-radius);
}

.bootcn-toggle-group > .btn-check:focus-visible + .bootcn-toggle {
  position: relative;
  z-index: 1;
}

Released under the MIT License. Not affiliated with Bootstrap or shadcn/ui.