Skip to content

Color Wheel Color Picker

Build a color picker from a hue and saturation wheel, brightness and alpha sliders, and preset swatches. sv-color-group shares one color between them, each sv-color-swatch inside it sets that color, and a transition on ::part(thumb) glides every thumb to the color of a picked swatch.

adaptive on the wheel darkens it as the brightness slider moves.

Reduced Motion

Your system is set to reduce motion, so the animations shown on this page will not play for you.

Code

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

Color Picker
<sv-color-group class="wheel-picker" value="#8b5cf6">
40 collapsed lines
<sv-color-wheel adaptive aria-label="Hue and saturation" thumb-contained></sv-color-wheel>
<sv-color-slider
adaptive
aria-label="Brightness"
channel="brightness"
thumb-contained
></sv-color-slider>
<sv-color-slider adaptive aria-label="Alpha" channel="alpha" thumb-contained></sv-color-slider>
<div class="preview">
<sv-text-flow class="hex-value" value="#8B5CF6"></sv-text-flow>
</div>
<sv-divider></sv-divider>
<div class="swatches" aria-label="Presets" role="radiogroup">
<sv-color-swatch aria-label="Red" name="wheel-picker-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch
aria-label="Orange"
name="wheel-picker-preset"
value="#f97316"
></sv-color-swatch>
<sv-color-swatch
aria-label="Yellow"
name="wheel-picker-preset"
value="#eab308"
></sv-color-swatch>
<sv-color-swatch
aria-label="Green"
name="wheel-picker-preset"
value="#22c55e"
></sv-color-swatch>
<sv-color-swatch aria-label="Cyan" name="wheel-picker-preset" value="#06b6d4"></sv-color-swatch>
<sv-color-swatch
aria-label="Violet"
name="wheel-picker-preset"
value="#8b5cf6"
></sv-color-swatch>
<sv-color-swatch aria-label="Pink" name="wheel-picker-preset" value="#ec4899"></sv-color-swatch>
</div>
</sv-color-group>
<script type="application/x-typescript" module>
17 collapsed lines
const picker = document.querySelector<ColorGroup>(".wheel-picker")!;
const hexValue = picker.querySelector<TextFlow>(".hex-value")!;
const swatches = picker.querySelector<HTMLDivElement>(".swatches")!;
// Fires for every color change in the group, from the user or from code.
picker.addEventListener("valuechange", () => {
picker.style.setProperty("--picker-color", picker.value);
hexValue.value = picker.value.toUpperCase();
});
// Only a swatch glides the thumbs, never a drag or a key on the sliders.
for (const eventType of ["pointerdown", "keydown"]) {
picker.addEventListener(eventType, () => picker.classList.remove("gliding"));
}
// A swatch sets the group color by itself. Its `input` reaches the swatches before the group.
swatches.addEventListener("input", () => picker.classList.add("gliding"));
</script>
<style>
78 collapsed lines
.wheel-picker {
--picker-color: #8b5cf6;
--glide: 500ms cubic-bezier(0.25, 1, 0.5, 1);
@media (prefers-reduced-motion: reduce) {
--glide: 0s;
}
display: grid;
gap: 1em;
justify-items: center;
inline-size: min(100%, 18em);
padding: 1em;
margin: 1em auto;
background: var(--sv-gray-6);
border: solid 1px var(--sv-gray-5);
border-radius: 1.25em;
sv-color-wheel {
--sv-polar-slider-sz: 100%;
}
sv-color-slider {
inline-size: 100%;
}
&.gliding {
sv-color-wheel::part(thumb) {
transition: inset var(--glide);
}
sv-color-slider::part(thumb) {
transition: inset-inline-start var(--glide);
}
}
/* The color sits on a checkerboard. This makes the alpha visible. */
.preview {
display: grid;
place-items: center;
justify-self: stretch;
block-size: 2.75em;
font-weight: 600;
font-variant-numeric: tabular-nums;
color: #fff;
text-shadow: 0 1px 2px rgb(0 0 0 / 60%);
background:
linear-gradient(var(--picker-color), var(--picker-color)),
repeating-conic-gradient(#ccc 0 25%, #fff 0 50%) 0 0 / 0.5em 0.5em;
border: solid 1px var(--sv-gray-5);
border-radius: 0.5em;
@supports (color: contrast-color(red)) {
color: contrast-color(var(--picker-color));
text-shadow: none;
}
}
sv-divider {
justify-self: stretch;
}
/* One row, with every swatch shrinking to fit. */
.swatches {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
gap: 0.5em;
justify-self: stretch;
}
sv-color-swatch {
--sv-color-swatch-sz: 100%;
&::part(check) {
display: none;
}
}
}
</style>