Skip to content

Toggle ​

A two-state button, such as Bold in a text editor, built on a native checkbox.

Installation ​

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

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

Usage ​

A toggle is a native checkbox (Bootstrap's .btn-check, which hides it accessibly) followed by its <label class="bootcn-toggle">. Read its state from the checkbox (checked), or submit it with a form. Variants: --outline; sizes: --sm, --lg.

No JavaScript is needed.

Accessibility ​

  • It is a real checkbox: Space toggles it and screen readers announce "checked" / "not checked".
  • An icon-only toggle needs visually hidden text in its label ("Bold").
  • The focus ring shows on the label when the checkbox has keyboard focus.

Dependencies ​

  • Bootstrap 5.3+ CSS.
  • themes/tokens/tokens.css.

Source ​

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

html
<input type="checkbox" class="btn-check" id="bootcn-toggle-bold" name="bold" />
<label class="bootcn-toggle" for="bootcn-toggle-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-italic" name="italic" checked />
<label class="bootcn-toggle bootcn-toggle--outline" for="bootcn-toggle-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>
  Italic
</label>

<input type="checkbox" class="btn-check" id="bootcn-toggle-disabled" disabled />
<label class="bootcn-toggle" for="bootcn-toggle-disabled">
  <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>
css
.bootcn-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  min-width: var(--bootcn-control-height);
  height: var(--bootcn-control-height);
  padding: 0 0.625rem;
  border: var(--bootcn-border-width) solid transparent;
  border-radius: var(--bootcn-radius);
  background-color: transparent;
  color: var(--bootcn-foreground);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  transition:
    background-color var(--bootcn-transition),
    color var(--bootcn-transition),
    box-shadow var(--bootcn-transition);
}

.bootcn-toggle:hover {
  background-color: var(--bootcn-muted);
}

.bootcn-toggle--outline {
  border-color: var(--bootcn-input);
  box-shadow: var(--bootcn-shadow-sm);
}

.bootcn-toggle--sm {
  min-width: var(--bootcn-control-height-sm);
  height: var(--bootcn-control-height-sm);
  padding: 0 0.5rem;
}

.bootcn-toggle--lg {
  min-width: var(--bootcn-control-height-lg);
  height: var(--bootcn-control-height-lg);
  padding: 0 0.75rem;
}

/* State comes from the Bootstrap .btn-check input before the label. */
.btn-check:checked + .bootcn-toggle {
  background-color: var(--bootcn-accent);
  color: var(--bootcn-accent-foreground);
}

.btn-check:focus-visible + .bootcn-toggle {
  box-shadow: var(--bootcn-focus-ring);
}

.btn-check:disabled + .bootcn-toggle {
  cursor: not-allowed;
  opacity: 0.5;
}

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