Color slider
Sliders for one color channel each (hue, saturation, lightness, opacity), with the channel painted on the track and a live swatch.
- Version 1.0.1
- Bootstrap 5.3+
- Requires Bootstrap CSS
- Source on GitHub
html
<div
class="bootcn-color-slider"
data-bootcn-color-slider
role="group"
aria-labelledby="bootcn-color-slider-title"
style="--bootcn-color-h: 262; --bootcn-color-s: 83%; --bootcn-color-l: 58%; --bootcn-color-a: 1"
>
<div class="bootcn-color-slider__head">
<p class="bootcn-label" id="bootcn-color-slider-title">Highlight color</p>
<span class="bootcn-color-slider__swatch" aria-hidden="true"></span>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-hue">Hue</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-hue">262°</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--hue"
type="range"
id="bootcn-color-slider-hue"
min="0"
max="360"
value="262"
data-channel="h"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-saturation"
>Saturation</label
>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-saturation">83%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--saturation"
type="range"
id="bootcn-color-slider-saturation"
min="0"
max="100"
value="83"
data-channel="s"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-lightness">Lightness</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-lightness">58%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--lightness"
type="range"
id="bootcn-color-slider-lightness"
min="0"
max="100"
value="58"
data-channel="l"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-alpha">Opacity</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-alpha">100%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--alpha"
type="range"
id="bootcn-color-slider-alpha"
min="0"
max="100"
value="100"
data-channel="a"
/>
</div>
<input
type="hidden"
name="highlight"
value="hsl(262 83% 58% / 1)"
data-bootcn-color-slider-value
/>
</div>Installation
bash
npx bootcn add color-sliderbash
pnpm dlx bootcn add color-sliderbash
yarn dlx bootcn add color-sliderbash
bunx --bun bootcn add color-sliderThe CLI also installs the design tokens and these components: label. It prints the <link> and <script> tags to add to your layout.
- Load Bootstrap 5.3 (Bootstrap CSS) and the design tokens.
- Install the components it uses:
label. - Copy the files from Source into your project. With the default
bootcn.jsonthey go here:
| File | Default location |
|---|---|
color-slider.html | components/ui/color-slider.html |
color-slider.css | assets/css/color-slider.css |
color-slider.js | assets/js/color-slider.js |
Usage
Set the starting color in the style attribute of the group (and the matching value of each input) on the server. The hue slider class bootcn-color-slider__input--hue also works on its own, as in the Color area component.
JavaScript API
data-bootcn-color-slideron the group; the color lives in its CSS variables--bootcn-color-h,-s,-land-a. Each native range input names its channel withdata-channel="h|s|l|a"(hue 0–360, the others 0–100). Use any subset of the four.- The tracks paint the channel with the other channels' current values; the
<output for>of each input and the hidden input markeddata-bootcn-color-slider-value(hsl(h s% l% / a)) stay in sync. - Event
bootcn:color-slider:change(bubbles),detail: { value, h, s, l, a }. Bootcn.colorSlider.setColor(root, { h, s, l, a });init()/destroy().
Accessibility
- Native range inputs: arrow keys, Page Up/Down, Home and End work, and each slider has a visible label and a value with its unit (
aria-valuetext, "262 degrees"). - The group is named by its title; the swatch is decorative and the value is also given as text.
Dependencies
- Bootstrap 5.3+ CSS.
themes/tokens/tokens.css.- The
labelcomponent.
Source
The exact files the CLI installs (color-slider@1.0.1). View on GitHub · Report an issue
html
<div
class="bootcn-color-slider"
data-bootcn-color-slider
role="group"
aria-labelledby="bootcn-color-slider-title"
style="--bootcn-color-h: 262; --bootcn-color-s: 83%; --bootcn-color-l: 58%; --bootcn-color-a: 1"
>
<div class="bootcn-color-slider__head">
<p class="bootcn-label" id="bootcn-color-slider-title">Highlight color</p>
<span class="bootcn-color-slider__swatch" aria-hidden="true"></span>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-hue">Hue</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-hue">262°</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--hue"
type="range"
id="bootcn-color-slider-hue"
min="0"
max="360"
value="262"
data-channel="h"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-saturation"
>Saturation</label
>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-saturation">83%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--saturation"
type="range"
id="bootcn-color-slider-saturation"
min="0"
max="100"
value="83"
data-channel="s"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-lightness">Lightness</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-lightness">58%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--lightness"
type="range"
id="bootcn-color-slider-lightness"
min="0"
max="100"
value="58"
data-channel="l"
/>
</div>
<div class="bootcn-color-slider__row">
<label class="bootcn-color-slider__label" for="bootcn-color-slider-alpha">Opacity</label>
<output class="bootcn-color-slider__output" for="bootcn-color-slider-alpha">100%</output>
<input
class="bootcn-color-slider__input bootcn-color-slider__input--alpha"
type="range"
id="bootcn-color-slider-alpha"
min="0"
max="100"
value="100"
data-channel="a"
/>
</div>
<input
type="hidden"
name="highlight"
value="hsl(262 83% 58% / 1)"
data-bootcn-color-slider-value
/>
</div>css
.bootcn-color-slider {
--bootcn-color-h: 0;
--bootcn-color-s: 100%;
--bootcn-color-l: 50%;
--bootcn-color-a: 1;
--bootcn-color-slider-checker: repeating-conic-gradient(#d4d4d8 0% 25%, #fff 0% 50%) 50% / 0.75rem
0.75rem;
display: flex;
flex-direction: column;
gap: 1rem;
width: 20rem;
max-width: 100%;
}
.bootcn-color-slider__head {
display: flex;
align-items: center;
justify-content: space-between;
}
.bootcn-color-slider__head .bootcn-label {
margin: 0;
}
.bootcn-color-slider__swatch {
width: 2rem;
height: 2rem;
border: var(--bootcn-border-width) solid var(--bootcn-border-color);
border-radius: var(--bootcn-radius);
background:
linear-gradient(
hsl(
var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l) / var(--bootcn-color-a)
),
hsl(var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l) / var(--bootcn-color-a))
),
var(--bootcn-color-slider-checker);
}
.bootcn-color-slider__row {
display: grid;
grid-template-columns: 1fr auto;
gap: 0.375rem 1rem;
}
.bootcn-color-slider__label {
font-size: 0.875rem;
font-weight: 500;
}
.bootcn-color-slider__output {
color: var(--bootcn-muted-foreground);
font-size: 0.875rem;
font-variant-numeric: tabular-nums;
}
/* The input: a painted track and a white ring thumb. */
.bootcn-color-slider__input {
--bootcn-color-slider-track: linear-gradient(to right, #000, #fff);
grid-column: 1 / -1;
width: 100%;
height: 1.25rem;
margin: 0;
background: transparent;
appearance: none;
}
.bootcn-color-slider__input:focus {
outline: 0;
}
.bootcn-color-slider__input::-webkit-slider-runnable-track {
height: 0.75rem;
border-radius: 9999px;
background: var(--bootcn-color-slider-track);
box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.1);
}
.bootcn-color-slider__input::-moz-range-track {
height: 0.75rem;
border-radius: 9999px;
background: var(--bootcn-color-slider-track);
box-shadow: inset 0 0 0 1px rgb(0 0 0 / 0.1);
}
.bootcn-color-slider__input::-webkit-slider-thumb {
width: 1.25rem;
height: 1.25rem;
margin-top: -0.25rem;
border: 3px solid #fff;
border-radius: 9999px;
background: hsl(var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l));
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.25),
var(--bootcn-shadow-sm);
appearance: none;
}
.bootcn-color-slider__input::-moz-range-thumb {
width: 0.875rem;
height: 0.875rem;
border: 3px solid #fff;
border-radius: 9999px;
background: hsl(var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l));
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.25),
var(--bootcn-shadow-sm);
}
.bootcn-color-slider__input:focus-visible::-webkit-slider-thumb {
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.25),
var(--bootcn-focus-ring);
}
.bootcn-color-slider__input:focus-visible::-moz-range-thumb {
box-shadow:
0 0 0 1px rgb(0 0 0 / 0.25),
var(--bootcn-focus-ring);
}
.bootcn-color-slider__input--hue {
--bootcn-color-slider-track: linear-gradient(
to right,
hsl(0 100% 50%),
hsl(60 100% 50%),
hsl(120 100% 50%),
hsl(180 100% 50%),
hsl(240 100% 50%),
hsl(300 100% 50%),
hsl(360 100% 50%)
);
}
.bootcn-color-slider__input--saturation {
--bootcn-color-slider-track: linear-gradient(
to right,
hsl(var(--bootcn-color-h) 0% var(--bootcn-color-l)),
hsl(var(--bootcn-color-h) 100% var(--bootcn-color-l))
);
}
.bootcn-color-slider__input--lightness {
--bootcn-color-slider-track: linear-gradient(
to right,
hsl(var(--bootcn-color-h) var(--bootcn-color-s) 0%),
hsl(var(--bootcn-color-h) var(--bootcn-color-s) 50%),
hsl(var(--bootcn-color-h) var(--bootcn-color-s) 100%)
);
}
.bootcn-color-slider__input--alpha {
--bootcn-color-slider-track:
linear-gradient(
to right,
hsl(var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l) / 0),
hsl(var(--bootcn-color-h) var(--bootcn-color-s) var(--bootcn-color-l) / 1)
),
var(--bootcn-color-slider-checker);
}
/* Color channels read left to right in every language. */
.bootcn-color-slider__input {
direction: ltr;
}js
(function () {
'use strict';
const ns = (window.Bootcn = window.Bootcn || {});
if (ns.colorSlider) return; // already loaded: idempotent
const ROOT = '[data-bootcn-color-slider]';
const UNITS = { h: '°', s: '%', l: '%', a: '%' };
const NAMES = { h: 'degrees', s: 'percent', l: 'percent', a: 'percent' };
let listening = false;
function read(root) {
const style = root.style;
const value = {
h: parseFloat(style.getPropertyValue('--bootcn-color-h')) || 0,
s: parseFloat(style.getPropertyValue('--bootcn-color-s')) || 0,
l: parseFloat(style.getPropertyValue('--bootcn-color-l')) || 0,
a: parseFloat(style.getPropertyValue('--bootcn-color-a')),
};
if (Number.isNaN(value.a)) value.a = 1;
return value;
}
function cssColor(c) {
return 'hsl(' + c.h + ' ' + c.s + '% ' + c.l + '% / ' + c.a + ')';
}
// Writes the channels to the CSS variables, outputs, inputs and the
// hidden value; emits the change event.
function render(root, c, emit) {
root.style.setProperty('--bootcn-color-h', String(c.h));
root.style.setProperty('--bootcn-color-s', c.s + '%');
root.style.setProperty('--bootcn-color-l', c.l + '%');
root.style.setProperty('--bootcn-color-a', String(c.a));
root.querySelectorAll('input[data-channel]').forEach(function (input) {
const channel = input.getAttribute('data-channel');
const shown = channel === 'a' ? Math.round(c.a * 100) : c[channel];
input.value = String(shown);
input.setAttribute('aria-valuetext', shown + ' ' + NAMES[channel]);
const output = root.querySelector('output[for="' + input.id + '"]');
if (output) output.textContent = shown + UNITS[channel];
});
const hidden = root.querySelector('[data-bootcn-color-slider-value]');
if (hidden) hidden.value = cssColor(c);
if (emit) {
root.dispatchEvent(
new CustomEvent('bootcn:color-slider:change', {
bubbles: true,
detail: { value: cssColor(c), h: c.h, s: c.s, l: c.l, a: c.a },
}),
);
}
}
function onInput(event) {
const input = event.target;
if (!input.matches || !input.matches(ROOT + ' input[data-channel]')) return;
const root = input.closest(ROOT);
const c = read(root);
const channel = input.getAttribute('data-channel');
const number = Number(input.value);
c[channel] = channel === 'a' ? number / 100 : number;
render(root, c, true);
}
function setColor(root, c) {
render(root, Object.assign(read(root), c), true);
}
function init() {
if (!listening) {
document.addEventListener('input', onInput);
listening = true;
}
document.querySelectorAll(ROOT).forEach(function (root) {
render(root, read(root), false);
});
}
function destroy() {
if (!listening) return;
document.removeEventListener('input', onInput);
listening = false;
}
ns.colorSlider = { init: init, destroy: destroy, setColor: setColor };
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', init);
} else {
init();
}
})();
