Skip to content

Color Input Wheel Picker

Add a swatch to an HSV color field that opens a color wheel and presets in a popup. format="hsv" splits the field into hsv() segments, and in an sv-color-group, a ring wheel edits the brightness around a second sv-color-wheel that edits the hue and the saturation.

The sv-color-swatch presets set the group color.

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

Code

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

Wheel Picker
<div class="color-input-wheel-picker">
40 collapsed lines
<label for="color-input-wheel-picker-input">Brand color</label>
<sv-color-input id="color-input-wheel-picker-input" format="hsv" value="#3b82f6">
<button
id="color-input-wheel-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-wheel-picker-button">
<sv-color-group class="panel" value="#3b82f6">
<div class="wheels">
<sv-color-wheel
adaptive
aria-label="Brightness"
channels="brightness"
ring
></sv-color-wheel>
<sv-color-wheel
adaptive
aria-label="Hue and saturation"
channels="hue saturation"
></sv-color-wheel>
</div>
<sv-divider></sv-divider>
<div class="swatches" aria-label="Presets" role="radiogroup">
<sv-color-swatch aria-label="Red" name="wheel-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch aria-label="Orange" name="wheel-preset" value="#f97316"></sv-color-swatch>
<sv-color-swatch aria-label="Yellow" name="wheel-preset" value="#eab308"></sv-color-swatch>
<sv-color-swatch aria-label="Green" name="wheel-preset" value="#22c55e"></sv-color-swatch>
<sv-color-swatch aria-label="Blue" name="wheel-preset" value="#3b82f6"></sv-color-swatch>
<sv-color-swatch aria-label="Violet" name="wheel-preset" value="#8b5cf6"></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-wheel-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 hueSaturationWheel = picker.querySelector<ColorWheel>("sv-color-wheel:not([ring])")!;
flyout.anchorElement = colorInput;
flyout.addEventListener("transitionfinish", () => {
if (flyout.opened) {
hueSaturationWheel.focus();
}
});
colorInput.addEventListener("input", () => {
if (colorInput.value) {
colorGroup.value = colorInput.value;
}
});
colorGroup.addEventListener("input", () => {
colorInput.value = colorGroup.value;
});
// Enter on a wheel 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-wheel-picker-button")!.focus();
});
</script>
<style>
76 collapsed lines
.color-input-wheel-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: var(--sv-clr);
border: none;
border-radius: var(--sv-bdr-rad-sm);
box-shadow: inset 0 0 0 1px rgb(0 0 0 / 15%);
}
.panel {
display: grid;
gap: 0.75em;
padding: 0.5em 0.25em;
}
.wheels {
display: grid;
place-items: center;
container-type: inline-size;
> * {
grid-area: 1 / 1;
}
}
sv-color-wheel[ring] {
--sv-polar-slider-sz: 100cqi;
--sv-polar-slider-track-thickness: 1.25em;
}
sv-color-wheel:not([ring]) {
--sv-polar-slider-sz: 75cqi;
}
.swatches {
--swatch-count: 6;
--swatch-gap: 0.4em;
display: flex;
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;
}
}
}
</style>