Skip to content

Slider ​

A native range input with a filled track, a visible value and keyboard support.

Installation ​

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

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

Usage ​

It is a native <input type="range">, so forms submit it as usual. Show the value in an <output for="…">. Firefox fills the track natively; the script does it for Chromium and Safari.

JavaScript API ​

  • data-bootcn-slider on the input turns on the track fill and output updates.
  • data-bootcn-slider-prefix / data-bootcn-slider-suffix: text around the value in the <output> (for example € or %).
  • Bootcn.slider.update(input): refresh after changing the value from script.
  • Bootcn.slider.init(root) / destroy().

Without JavaScript the slider still works; Chromium and Safari then show the initial fill from the inline --bootcn-slider-fill.

Accessibility ​

  • Native range input: arrow keys, Page Up/Down, Home and End work, and screen readers announce the value.
  • The visible <label> names it; the <output> shows the current value.

Dependencies ​

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

Source ​

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

html
<div class="bootcn-slider">
  <div class="bootcn-slider__header">
    <label class="bootcn-label" for="bootcn-slider-volume">Volume</label>
    <output class="bootcn-slider__value" for="bootcn-slider-volume">50</output>
  </div>
  <input
    type="range"
    class="form-range bootcn-slider__input"
    id="bootcn-slider-volume"
    name="volume"
    min="0"
    max="100"
    step="1"
    value="50"
    style="--bootcn-slider-fill: 50%"
    data-bootcn-slider
  />
</div>

<div class="bootcn-slider mt-4">
  <div class="bootcn-slider__header">
    <label class="bootcn-label" for="bootcn-slider-price">Maximum price</label>
    <output class="bootcn-slider__value" for="bootcn-slider-price">€200</output>
  </div>
  <input
    type="range"
    class="form-range bootcn-slider__input"
    id="bootcn-slider-price"
    name="price"
    min="0"
    max="500"
    step="10"
    value="200"
    style="--bootcn-slider-fill: 40%"
    data-bootcn-slider
    data-bootcn-slider-prefix="€"
  />
</div>
css
.bootcn-slider {
  max-width: 24rem;
}

.bootcn-slider__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}

.bootcn-slider__value {
  color: var(--bootcn-muted-foreground);
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}

.bootcn-slider__input {
  --bootcn-slider-track: 0.375rem;
  --bootcn-slider-thumb: 1.125rem;

  height: var(--bootcn-slider-thumb);
}

/* Track: primary up to the value (--bootcn-slider-fill, set by slider.js). */
.bootcn-slider__input::-webkit-slider-runnable-track {
  height: var(--bootcn-slider-track);
  border-radius: 9999px;
  background: linear-gradient(
    to right,
    var(--bootcn-primary) var(--bootcn-slider-fill, 50%),
    var(--bootcn-muted) var(--bootcn-slider-fill, 50%)
  );
}

.bootcn-slider__input::-moz-range-track {
  height: var(--bootcn-slider-track);
  border-radius: 9999px;
  background-color: var(--bootcn-muted);
}

.bootcn-slider__input::-moz-range-progress {
  height: var(--bootcn-slider-track);
  border-radius: 9999px;
  background-color: var(--bootcn-primary);
}

.bootcn-slider__input::-webkit-slider-thumb {
  width: var(--bootcn-slider-thumb);
  height: var(--bootcn-slider-thumb);
  margin-top: calc((var(--bootcn-slider-track) - var(--bootcn-slider-thumb)) / 2);
  border: 2px solid var(--bootcn-primary);
  background-color: var(--bootcn-background);
  box-shadow: var(--bootcn-shadow-sm);
}

.bootcn-slider__input::-moz-range-thumb {
  width: var(--bootcn-slider-thumb);
  height: var(--bootcn-slider-thumb);
  border: 2px solid var(--bootcn-primary);
  background-color: var(--bootcn-background);
  box-shadow: var(--bootcn-shadow-sm);
}

.bootcn-slider__input:focus::-webkit-slider-thumb {
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-slider__input:focus::-moz-range-thumb {
  box-shadow: var(--bootcn-focus-ring);
}

.bootcn-slider__input:active::-webkit-slider-thumb,
.bootcn-slider__input:active::-moz-range-thumb {
  background-color: var(--bootcn-background);
}

.bootcn-slider__input:disabled {
  opacity: 0.5;
}
js
(function () {
  'use strict';

  const ns = (window.Bootcn = window.Bootcn || {});
  if (ns.slider) return; // already loaded: idempotent

  const SELECTOR = '[data-bootcn-slider]';
  let listening = false;

  // Fills the track up to the value and mirrors it in the <output>.
  function update(input) {
    const min = Number(input.min || 0);
    const max = Number(input.max || 100);
    const percent = max > min ? ((Number(input.value) - min) / (max - min)) * 100 : 0;
    input.style.setProperty('--bootcn-slider-fill', percent + '%');
    if (!input.id) return;
    const output = document.querySelector('output[for~="' + input.id + '"]');
    if (output) {
      output.textContent =
        (input.getAttribute('data-bootcn-slider-prefix') || '') +
        input.value +
        (input.getAttribute('data-bootcn-slider-suffix') || '');
    }
  }

  function onInput(event) {
    if (event.target instanceof HTMLInputElement && event.target.matches(SELECTOR)) {
      update(event.target);
    }
  }

  function init(root = document) {
    root.querySelectorAll(SELECTOR).forEach(update);
    if (!listening) {
      document.addEventListener('input', onInput);
      listening = true;
    }
  }

  function destroy() {
    if (!listening) return;
    document.removeEventListener('input', onInput);
    listening = false;
  }

  ns.slider = { init: init, destroy: destroy, update: update };

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', function () {
      init();
    });
  } else {
    init();
  }
})();

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