Skip to content

Color Input HSL Slider Picker

Add a swatch to an HSL color field that opens hue, saturation, lightness, and alpha sliders in a popup. format="hsl" splits the field into hsl() segments, a button in the before slot opens an sv-flyout, and color-space="hsl" with channel on each sv-color-slider in an sv-color-group picks the channel it edits.

adaptive paints each track at the current color to show how the channel changes it.

The script keeps the field and the group in sync the same way as the Color Picker does, and it focuses the hue slider when the flyout opens.

Code

The following example assumes that you have imported the components and set up the theme.

HSL Slider Picker
<div class="color-input-hsl-slider-picker">
67 collapsed lines
<label for="color-input-hsl-slider-picker-input">Brand color</label>
<sv-color-input
id="color-input-hsl-slider-picker-input"
alpha
format="hsl"
value="hsl(217 91% 60%)"
>
<button
id="color-input-hsl-slider-picker-button"
type="button"
class="picker-button"
aria-label="Choose color"
slot="before"
></button>
</sv-color-input>
<sv-flyout popup-role="dialog" trigger-element="#color-input-hsl-slider-picker-button">
<sv-color-group class="panel" value="#3b82f6ff">
<label for="color-input-hsl-slider-picker-hue">Hue</label>
<sv-color-slider
id="color-input-hsl-slider-picker-hue"
adaptive
channel="hue"
color-space="hsl"
thumb-contained
></sv-color-slider>
<label for="color-input-hsl-slider-picker-saturation">Saturation</label>
<sv-color-slider
id="color-input-hsl-slider-picker-saturation"
adaptive
channel="saturation"
color-space="hsl"
thumb-contained
></sv-color-slider>
<label for="color-input-hsl-slider-picker-lightness">Lightness</label>
<sv-color-slider
id="color-input-hsl-slider-picker-lightness"
adaptive
channel="lightness"
color-space="hsl"
thumb-contained
></sv-color-slider>
<label for="color-input-hsl-slider-picker-alpha">Alpha</label>
<sv-color-slider
id="color-input-hsl-slider-picker-alpha"
adaptive
channel="alpha"
thumb-contained
></sv-color-slider>
<sv-divider></sv-divider>
<div class="swatches" aria-label="Presets" role="radiogroup">
<sv-color-swatch aria-label="Red" name="hsl-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch aria-label="Orange" name="hsl-preset" value="#f97316"></sv-color-swatch>
<sv-color-swatch aria-label="Yellow" name="hsl-preset" value="#eab308"></sv-color-swatch>
<sv-color-swatch aria-label="Green" name="hsl-preset" value="#22c55e"></sv-color-swatch>
<sv-color-swatch aria-label="Blue" name="hsl-preset" value="#3b82f6"></sv-color-swatch>
<sv-color-swatch aria-label="Violet" name="hsl-preset" value="#8b5cf6"></sv-color-swatch>
<sv-color-swatch aria-label="Pink" name="hsl-preset" value="#ec4899"></sv-color-swatch>
</div>
</sv-color-group>
</sv-flyout>
</div>
<script type="application/x-typescript" module>
32 collapsed lines
const picker = document.querySelector<HTMLDivElement>(".color-input-hsl-slider-picker")!;
const colorInput = picker.querySelector<ColorInput>("sv-color-input")!;
const flyout = picker.querySelector<Flyout>("sv-flyout")!;
const colorGroup = picker.querySelector<ColorGroup>("sv-color-group")!;
const hueSlider = picker.querySelector<ColorSlider>("sv-color-slider")!;
flyout.anchorElement = colorInput;
flyout.addEventListener("transitionfinish", () => {
if (flyout.opened) {
hueSlider.focus();
}
});
colorInput.addEventListener("input", () => {
if (colorInput.value) {
colorGroup.value = colorInput.value;
}
});
colorGroup.addEventListener("input", () => {
colorInput.value = colorGroup.value;
});
// Enter on a slider closes the picker and does not submit the form
colorGroup.addEventListener("keydown", event => {
if (event.key !== "Enter") return;
event.preventDefault();
flyout.close();
picker.querySelector<HTMLButtonElement>("#color-input-hsl-slider-picker-button")!.focus();
});
</script>
<style>
76 collapsed lines
.color-input-hsl-slider-picker {
display: grid;
gap: 0.5em;
place-content: center;
justify-items: center;
label {
font-weight: 600;
}
.picker-button {
flex: none;
inline-size: 1.5em;
block-size: 1.5em;
padding: 0;
margin-inline-start: 0.4em;
cursor: pointer;
background:
linear-gradient(var(--sv-clr), var(--sv-clr)),
repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 0.5em 0.5em;
border: none;
border-radius: var(--sv-bdr-rad-sm);
box-shadow: inset 0 0 0 1px rgb(0 0 0 / 15%);
}
.panel {
display: grid;
grid-template-columns: auto 1fr;
gap: 0.75em;
align-items: center;
padding: 0.5em 0.25em;
label {
font-size: 0.875em;
font-weight: 400;
color: var(--sv-gray-2);
}
}
sv-divider {
grid-column: 1 / -1;
}
.swatches {
--swatch-count: 7;
--swatch-gap: 0.4em;
display: flex;
grid-column: 1 / -1;
gap: var(--swatch-gap);
container-type: inline-size;
sv-color-swatch {
--sv-color-swatch-sz: calc(
(100cqi - (var(--swatch-count) - 1) * var(--swatch-gap)) / var(--swatch-count)
);
&::part(check) {
display: none;
}
}
}
sv-flyout {
--sv-flyout-bdr-rad: 1em;
&::part(content) {
inline-size: var(--sv-flyout-anchor-width);
max-block-size: none;
}
}
sv-color-slider {
margin-block: 0.2em;
}
}
</style>