Skip to content

Switch ​

An on/off toggle built on Bootstrap form switches, with sizes and descriptions.

Installation ​

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

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

Usage ​

html
<div class="form-check form-switch bootcn-switch">
  <input
    class="form-check-input bootcn-switch__input"
    type="checkbox"
    role="switch"
    id="wifi"
    name="wifi"
    checked
  />
  <label class="form-check-label bootcn-switch__label" for="wifi">Wi-Fi</label>
</div>

Add a .bootcn-switch__description (referenced with aria-describedby) for helper text, and bootcn-switch--lg for a larger switch.

Customizing ​

VariableDefault
--bootcn-switch-width2.25rem
--bootcn-switch-height1.25rem
--bootcn-switch-gap0.75rem

Dependencies ​

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

No JavaScript.

Accessibility ​

  • role="switch" makes screen readers announce "on/off" instead of "checked/not checked".
  • Use a switch for settings that apply immediately; use a checkbox for choices submitted with a form.
  • The thumb animation is disabled under prefers-reduced-motion.

Source ​

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

html
<div class="form-check form-switch bootcn-switch">
  <input
    class="form-check-input bootcn-switch__input"
    type="checkbox"
    role="switch"
    id="bootcn-switch-wifi"
    name="wifi"
    checked
  />
  <label class="form-check-label bootcn-switch__label" for="bootcn-switch-wifi">Wi-Fi</label>
</div>

<div class="form-check form-switch bootcn-switch mt-3">
  <input
    class="form-check-input bootcn-switch__input"
    type="checkbox"
    role="switch"
    id="bootcn-switch-marketing"
    name="marketing"
    aria-describedby="bootcn-switch-marketing-description"
  />
  <label class="form-check-label bootcn-switch__label" for="bootcn-switch-marketing"
    >Marketing emails</label
  >
  <p class="bootcn-switch__description" id="bootcn-switch-marketing-description">
    Receive emails about new products and features.
  </p>
</div>

<div class="form-check form-switch bootcn-switch bootcn-switch--lg mt-3">
  <input
    class="form-check-input bootcn-switch__input"
    type="checkbox"
    role="switch"
    id="bootcn-switch-large"
  />
  <label class="form-check-label bootcn-switch__label" for="bootcn-switch-large"
    >Large switch</label
  >
</div>

<div class="form-check form-switch bootcn-switch mt-3">
  <input
    class="form-check-input bootcn-switch__input"
    type="checkbox"
    role="switch"
    id="bootcn-switch-disabled"
    disabled
  />
  <label class="form-check-label bootcn-switch__label" for="bootcn-switch-disabled">Disabled</label>
</div>
css
.bootcn-switch {
  --bootcn-switch-width: 2.25rem;
  --bootcn-switch-height: 1.25rem;
  --bootcn-switch-gap: 0.75rem;

  display: grid;
  grid-template-columns: var(--bootcn-switch-width) 1fr;
  column-gap: var(--bootcn-switch-gap);
  align-items: start;
  padding-left: 0;
}

.bootcn-switch--lg {
  --bootcn-switch-width: 3rem;
  --bootcn-switch-height: 1.625rem;
}

.bootcn-switch .bootcn-switch__input {
  width: var(--bootcn-switch-width);
  height: var(--bootcn-switch-height);
  margin: 0;
  margin-left: 0;
  /* Off: a muted track with a visible border (3:1). */
  border-color: var(--bootcn-ring);
  background-color: var(--bootcn-input);
  cursor: pointer;
}

.bootcn-switch .bootcn-switch__input:checked {
  border-color: var(--bootcn-primary);
  background-color: var(--bootcn-primary);
}

/*
 * Thumb color. Bootstrap draws the thumb from --bs-form-switch-bg and sets it
 * in several state rules (checked, focus, dark + unchecked), so these
 * selectors repeat the classes to win in every state: light thumb, except
 * when on in dark mode, where the track is light and the thumb dark.
 */
.form-switch.bootcn-switch .form-check-input.bootcn-switch__input,
.form-switch.bootcn-switch .form-check-input.bootcn-switch__input:focus,
.form-switch.bootcn-switch .form-check-input.bootcn-switch__input:checked {
  --bs-form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fff'/%3e%3c/svg%3e");
}

[data-bs-theme='dark']
  .form-switch.bootcn-switch
  .form-check-input.bootcn-switch__input:not(:checked),
[data-bs-theme='dark']
  .form-switch.bootcn-switch
  .form-check-input.bootcn-switch__input:not(:checked):focus {
  --bs-form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%23fafafa'/%3e%3c/svg%3e");
}

[data-bs-theme='dark'] .form-switch.bootcn-switch .form-check-input.bootcn-switch__input:checked {
  --bs-form-switch-bg: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='-4 -4 8 8'%3e%3ccircle r='3' fill='%2318181b'/%3e%3c/svg%3e");
}

.bootcn-switch .bootcn-switch__input:focus-visible {
  border-color: var(--bootcn-primary);
  outline: 0;
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-switch .bootcn-switch__input:focus:not(:focus-visible) {
  box-shadow: none;
}

.bootcn-switch .bootcn-switch__input:disabled {
  cursor: not-allowed;
}

.bootcn-switch__label {
  grid-column: 2;
  align-self: center;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
}

.bootcn-switch__description {
  grid-column: 2;
  margin: 0.125rem 0 0;
  color: var(--bs-secondary-color);
  font-size: 0.875rem;
}

@media (prefers-reduced-motion: reduce) {
  .bootcn-switch .bootcn-switch__input {
    transition: none;
  }
}

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