To source

a-color-picker

HSL color picker with sliders, hex input, palette presets, and eye dropper support.

The a-color-picker element provides an accessible color picker with a 2D saturation/value field, a hue slider, optional alpha slider, hex input, palette presets, and an eye dropper for sampling colors from the screen.

Installation

import "@sv/elements/color-picker";

Usage

Basic

<a-color-picker name="color" value="#1d4ed8"></a-color-picker>

With Alpha Channel

Add the alpha attribute to show a fourth slider and include the alpha channel in the value (#rrggbbaa).

<a-color-picker alpha value="#3b82f680"></a-color-picker>

Palette Presets

Pass a comma-separated list of hex colors to the palette attribute. Clicking a swatch sets the color immediately and marks it as selected.

<a-color-picker palette="#ef4444,#f97316,#eab308,#22c55e,#3b82f6,#8b5cf6,#ec4899"></a-color-picker>

Eye Dropper

In browsers that support the EyeDropper API (Chromium 95+), a pipette button appears next to the hex input. Clicking it lets the user sample any color from anywhere on the screen.

<a-color-picker value="#22c55e"></a-color-picker>

The button is rendered automatically when the API is available and hidden otherwise — no configuration needed.

Form Integration

Set the name attribute to participate in FormData. The element creates a hidden <input type="hidden"> that stays in sync with the selected color.

<form>
  <a-color-picker name="brand_color" value="#1d4ed8"></a-color-picker>
  <button type="submit">Submit</button>
</form>

The submitted value is a lowercase hex string: #rrggbb, or #rrggbbaa when alpha is enabled.

Disabled

<a-color-picker disabled value="#1d4ed8"></a-color-picker>

Events

EventWhen
inputFires continuously while a slider is being dragged.
changeFires when a value is committed: slider release, hex input blur or Enter, or palette swatch click.
picker.addEventListener("change", (e) => {
  console.log(e.target.value); // "#1d4ed8"
});

Value Format

ModeFormatExample
Default#rrggbb#1d4ed8
With alpha#rrggbbaa#1d4ed880

CSS Custom Properties

PropertyDefaultDescription
--color-picker-focus-outlineblackFocus ring color
--color-picker-borderrgba(0,0,0,0.2)Border color for tracks, inputs, and swatches
--color-picker-swatch-size1.75remWidth and height of each palette swatch

CSS Parts

PartElement
pickerRoot container
palettePalette swatch container
palette-swatchIndividual preset color button
sv-field2D saturation/value gradient field
sv-gradientGradient overlay inside the field
sv-thumbCircle cursor inside the field
controlsRow containing eye dropper + slider stack
slider-stackColumn of sliders
slider-trackIndividual slider gradient track
hue-trackHue slider track
alpha-trackAlpha slider track (only with alpha)
slider-input<input type="range"> element
hue-inputHue range input
alpha-inputAlpha range input
slider-thumb-dotCustom circular thumb indicator
eyedropper-btnEye dropper button
bottom-rowHex input + opacity row
hex-wrap# prefix + hex text input wrapper
hex-inputHex text <input>
opacity-wrapOpacity number input + % wrapper (only with alpha)
opacity-inputOpacity <input type="number">

Accessibility

  • The 2D field has role="img" with a descriptive label; it responds to pointer and touch events.
  • Hue and alpha sliders are native <input type="range"> with aria-label and aria-valuetext.
  • The hex input (aria-label="Hex color value") accepts a 6-character hex string; Enter or blur commits.
  • When alpha is set, the opacity <input type="number"> is labelled "Opacity percentage".
  • The eye dropper button has aria-label="Pick color from screen".
  • Palette swatches use aria-pressed to convey the selected state.
  • Focus is delegated from the host to the hex input.

References