Skip to content

Color Area Color Picker

Build a compact color picker from an area, a hue slider, an alpha slider, 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.

The area edits saturation and brightness, and adaptive paints it with the hue of the group.

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="area-picker" value="#8b5cf6">
31 collapsed lines
<sv-color-area adaptive aria-label="Saturation and brightness" thumb-contained></sv-color-area>
<sv-color-slider adaptive aria-label="Hue" 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="area-picker-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch
aria-label="Orange"
name="area-picker-preset"
value="#f97316"
></sv-color-swatch>
<sv-color-swatch
aria-label="Yellow"
name="area-picker-preset"
value="#eab308"
></sv-color-swatch>
<sv-color-swatch aria-label="Green" name="area-picker-preset" value="#22c55e"></sv-color-swatch>
<sv-color-swatch aria-label="Cyan" name="area-picker-preset" value="#06b6d4"></sv-color-swatch>
<sv-color-swatch
aria-label="Violet"
name="area-picker-preset"
value="#8b5cf6"
></sv-color-swatch>
<sv-color-swatch aria-label="Pink" name="area-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>(".area-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>
69 collapsed lines
.area-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;
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-area {
--sv-area-slider-width: 100%;
--sv-area-slider-aspect-ratio: 4 / 3;
--sv-area-slider-bdr-rad: 0.75em;
}
&.gliding {
sv-color-area::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;
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;
}
}
/* One row, with every swatch shrinking to fit. */
.swatches {
display: grid;
grid-template-columns: repeat(7, minmax(0, 1fr));
gap: 0.5em;
}
sv-color-swatch {
--sv-color-swatch-sz: 100%;
&::part(check) {
display: none;
}
}
}
</style>