Color Input RGB Slider Picker
Add a swatch to an RGB color field that opens red, green, blue, and alpha sliders in a popup. format="rgb" splits the field into rgb() segments, a button in the before slot opens an sv-flyout, and color-space="rgb" 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 red slider when the flyout opens.
Code
The following example assumes that you have imported the components and set up the theme.
<div class="color-input-rgb-slider-picker">67 collapsed lines
<label for="color-input-rgb-slider-picker-input">Brand color</label>
<sv-color-input id="color-input-rgb-slider-picker-input" alpha format="rgb" value="rgb(59 130 246)" > <button id="color-input-rgb-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-rgb-slider-picker-button"> <sv-color-group class="panel" value="#3b82f6ff"> <label for="color-input-rgb-slider-picker-red">Red</label> <sv-color-slider id="color-input-rgb-slider-picker-red" adaptive channel="red" color-space="rgb" thumb-contained ></sv-color-slider>
<label for="color-input-rgb-slider-picker-green">Green</label> <sv-color-slider id="color-input-rgb-slider-picker-green" adaptive channel="green" color-space="rgb" thumb-contained ></sv-color-slider>
<label for="color-input-rgb-slider-picker-blue">Blue</label> <sv-color-slider id="color-input-rgb-slider-picker-blue" adaptive channel="blue" color-space="rgb" thumb-contained ></sv-color-slider>
<label for="color-input-rgb-slider-picker-alpha">Alpha</label> <sv-color-slider id="color-input-rgb-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="rgb-preset" value="#ef4444"></sv-color-swatch> <sv-color-swatch aria-label="Orange" name="rgb-preset" value="#f97316"></sv-color-swatch> <sv-color-swatch aria-label="Yellow" name="rgb-preset" value="#eab308"></sv-color-swatch> <sv-color-swatch aria-label="Green" name="rgb-preset" value="#22c55e"></sv-color-swatch> <sv-color-swatch aria-label="Blue" name="rgb-preset" value="#3b82f6"></sv-color-swatch> <sv-color-swatch aria-label="Violet" name="rgb-preset" value="#8b5cf6"></sv-color-swatch> <sv-color-swatch aria-label="Pink" name="rgb-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-rgb-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 redSlider = picker.querySelector<ColorSlider>("sv-color-slider")!;
flyout.anchorElement = colorInput;
flyout.addEventListener("transitionfinish", () => { if (flyout.opened) { redSlider.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-rgb-slider-picker-button")!.focus(); });</script>
<style>76 collapsed lines
.color-input-rgb-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>