API Reference
Attributes
Section titled “Attributes”The <color-input> element accepts four HTML attributes:
The value attribute sets the color using standard CSS color syntax.
<color-input value="oklch(70% 20% 240)"></color-input>- Value
- oklch(70% 20% 240)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="oklch(70% 20% 240)"></color-input>- Value
- hsl(240 80% 60%)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="hsl(240 80% 60%)"></color-input>- Value
- #3b82f6
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="#3b82f6"></color-input>- Value
- color(display-p3 0.3 0.7 0.95)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="color(display-p3 0.3 0.7 0.95)"></color-input><color-input value="hsl(200 90% 50%)"></color-input>The theme attribute controls the visual appearance:
auto(default) - Respects user’s system preferencelight- Always uses light themedark- Always uses dark theme
- Value
- oklch(65% 20% 280)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="oklch(65% 20% 280)"></color-input>- Value
- oklch(65% 20% 280)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="oklch(65% 20% 280)" theme="light"></color-input>- Value
- oklch(65% 20% 280)
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="oklch(65% 20% 280)" theme="dark"></color-input>no-alpha
Section titled “no-alpha”The no-alpha attribute hides the alpha channel control from the color picker. When present, users cannot modify the alpha/opacity value through the UI, though the alpha value is still preserved in the color.
<color-input value="red" no-alpha></color-input>- Value
- red
- Color Space
- oklch
- Gamut
- —
- Contrast Color
-
—
View Code
<color-input value="red" no-alpha></color-input>Inline Text Input
Section titled “Inline Text Input”Every <color-input> now includes an inline text field for editing the current color string. It accepts anything the CSS Color Level 4 parser understands and keeps the sliders, preview, and computed properties in sync as you type.
- Validation runs on every keystroke. When parsing fails the host gains a
data-errorattribute, the textbox setsaria-invalid="true", and the inline error message becomes visible. - Restyle the field with
::part(input)and target invalid states with[data-error].
<color-input value="#9333ea"></color-input>Copy to Clipboard
Section titled “Copy to Clipboard”Hover or focus the preview area to reveal a copy button. Activating it copies the current color string with navigator.clipboard.writeText() and shows a “Copied!” status message inside the panel. No configuration is required—the control works for every supported color space, including hex.
Properties
Section titled “Properties”The <color-input> element exposes six properties:
Read/Write Properties
Section titled “Read/Write Properties”value - Current color as a CSS string
const picker = document.querySelector("color-input");console.log(picker.value); // "oklch(70% 20% 240)"picker.value = "oklch(80% 15% 180)";colorspace - Current color space
console.log(picker.colorspace); // "oklch"picker.colorspace = "hsl";theme - Visual theme
console.log(picker.theme); // "auto"picker.theme = "dark"; // "auto", "light", or "dark"noAlpha - Controls alpha channel visibility
console.log(picker.noAlpha); // falsepicker.noAlpha = true; // Hides alpha controlspicker.noAlpha = false; // Shows alpha controlsRead-Only Properties
Section titled “Read-Only Properties”gamut - Detected gamut (smallest gamut that contains the current color)
console.log(picker.gamut); // "srgb" | "p3" | "rec2020" | "xyz"contrastColor - Recommended text color for contrast
console.log(picker.contrastColor); // "white" or "black"Methods
Section titled “Methods”show()
Section titled “show()”Opens the picker popover programmatically:
const picker = document.querySelector("color-input");picker.show();showPicker()
Section titled “showPicker()”Alias for show() retained for compatibility with earlier versions:
picker.showPicker();close()
Section titled “close()”Closes the picker popover:
picker.close();setAnchor(element)
Section titled “setAnchor(element)”Sets a custom anchor element for popover positioning:
const customAnchor = document.querySelector("#my-anchor");picker.setAnchor(customAnchor);By default, the popover positions relative to the trigger button.
Command API Integration
Section titled “Command API Integration”<color-input> listens for Popover command events so you can wire it up with declarative popovertarget controls or custom command dispatchers.
- Buttons with
popovertargetautomatically sendshow,hide, ortogglecommands that the element consumes. - The component accepts either the short (
show,hide,toggle) or Popover-specific (show-popover,hide-popover,toggle-popover) command strings. - When a command originates from an invoker element, that invoker becomes the new popover anchor via
setAnchor().
<button popovertarget="picker" popovertargetaction="toggle"> Toggle color picker</button><color-input id="picker"></color-input>You can also trigger commands manually:
const cmd = new Event("command", { bubbles: true });Object.assign(cmd, { command: "show" });picker.dispatchEvent(cmd);Events
Section titled “Events”The <color-input> element dispatches three custom events:
change
Section titled “change”Fired when the color value changes. The event detail contains:
interface ChangeDetail { value: string; // CSS color string, e.g., "oklch(70% 20% 240)" colorspace: string; // Current color space, e.g., "oklch" gamut: string; // Detected gamut: "srgb", "p3", "rec2020", or "xyz"}picker.addEventListener("change", (event) => { const { value, colorspace, gamut } = event.detail; console.log(`New color: ${value}`); console.log(`Color space: ${colorspace}`); console.log(`Gamut: ${gamut}`);});Fired when the picker popover opens:
picker.addEventListener("open", () => { console.log("Picker opened");});Fired when the picker popover closes:
picker.addEventListener("close", () => { console.log("Picker closed");});CSS Parts
Section titled “CSS Parts”The <color-input> element exposes several CSS parts for styling with the ::part() selector:
trigger- The button that opens the pickerchip- Color swatch inside the trigger buttoninput- Inline text field for direct color entryerror- Text with color parsing error messagepanel- Popover containeroutput- CSS color string displaygamut- Gamut badge (srgb/p3/rec2020/xyz)controls- Container for channel sliders and inputs