Skip to content

Color Input Color Picker

Add a swatch to a color field that opens a color picker in a popup. A button in the before slot shows the color through --sv-clr and opens an sv-flyout, and the input events of the field and of the sv-color-group inside the flyout keep the two in sync.

The group's input event writes the picked color to the field, and the field's input event writes typed and pasted colors to the group. Since the script skips the empty value, clearing the field keeps the picker color. A color that you set on the field from code fires no input event, and you need to set it on the group too.

Since the flyout does not move the focus, the script focuses the color area when it opens. Escape closes the flyout and returns the focus to the swatch, and Enter in the picker does the same without submitting the form.

Code

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

Color Picker
<div class="color-input-picker">
35 collapsed lines
<label for="color-input-picker-input">Brand color</label>
<sv-color-input id="color-input-picker-input" alpha value="#3b82f6ff">
<button
id="color-input-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-picker-button">
<sv-color-group class="panel" value="#3b82f6ff">
<sv-color-area adaptive aria-label="Color" thumb-contained></sv-color-area>
<sv-color-slider aria-label="Hue" thumb-contained></sv-color-slider>
<sv-color-slider
adaptive
aria-label="Alpha"
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="picker-preset" value="#ef4444"></sv-color-swatch>
<sv-color-swatch aria-label="Orange" name="picker-preset" value="#f97316"></sv-color-swatch>
<sv-color-swatch aria-label="Yellow" name="picker-preset" value="#eab308"></sv-color-swatch>
<sv-color-swatch aria-label="Green" name="picker-preset" value="#22c55e"></sv-color-swatch>
<sv-color-swatch aria-label="Blue" name="picker-preset" value="#3b82f6"></sv-color-swatch>
<sv-color-swatch aria-label="Violet" name="picker-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-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 colorArea = picker.querySelector<ColorArea>("sv-color-area")!;
flyout.anchorElement = colorInput;
flyout.addEventListener("transitionfinish", () => {
if (flyout.opened) {
colorArea.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-picker-button")!.focus();
});
</script>
<style>
68 collapsed lines
.color-input-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;
gap: 0.75em;
inline-size: 12.5em;
padding: 0.25em;
}
.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) {
max-block-size: none;
}
}
sv-color-area {
--sv-area-slider-aspect-ratio: 4 / 3;
--sv-area-slider-bdr-rad: 0.25em;
}
sv-color-slider {
margin-block: 0.2em;
}
}
</style>