Color picker
A color chooser with a saturation area, hue and opacity sliders, swatches, and any CSS color typed in.
Example
<div class="stack"> <focsd-color-picker label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></focsd-color-picker> <focsd-color-picker label="Accent" inline alpha format="oklch" contrast-with="#ffffff" value="#123456"></focsd-color-picker></div>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-color-picker.js';</script>
<focsd-color-picker label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></focsd-color-picker>import { ColorPicker } from '@focsd-dev/react';
export function Example() { return <ColorPicker onChange={(e) => console.log(e.target)} label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></ColorPicker>;}<script setup>import '@focsd-dev/components/focsd-color-picker.js';</script>
<template> <focsd-color-picker @change="onChange" label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></focsd-color-picker></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-color-picker.js';
// template<focsd-color-picker (change)="onChange($event)" label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></focsd-color-picker><script> import '@focsd-dev/components/focsd-color-picker.js';</script>
<focsd-color-picker onchange={(e) => console.log(e.target)} label="Brand color" value="#3366ff" swatches="#141a18,#f2b705,#e11d48,#16a34a"></focsd-color-picker>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
name | name | string | — |
disabled | disabled | boolean | false |
required | required | boolean | false |
readonly | readOnly | boolean | false |
aria-label | hostAriaLabel | string | null | null |
aria-description | hostAriaDescription | string | null | null |
aria-labelledby | hostAriaLabelledBy | string | null | null |
aria-describedby | hostAriaDescribedBy | string | null | null |
label-position | labelPosition | 'top' | 'start' | 'end' | undefined | — |
hint | hint | string | — |
error | error | — | — |
warning | warning | string | undefined | — |
success | success | string | undefined | — |
error-message | errorMessage | string | undefined | — |
warning-message | warningMessage | string | undefined | — |
warning-pattern | warningPattern | string | undefined | — |
label | label | string | — |
value | value | string | #000000 |
format | format | 'hex' | 'rgb' | 'hsl' | 'oklch' | hex |
alpha | alpha | boolean | false |
swatches | swatches | string | — |
inline | inline | boolean | false |
contrast-with | contrastWith | string | — |
Methods
| Method | Description |
|---|---|
describedByIds() | IDs to add to the inner control's aria-describedby: the hint, then the shown message. |
checkValidity() | True when the value is valid; otherwise fires invalid and returns false, like native. |
reportValidity() | Like checkValidity(), and also shows the browser's validation message when invalid. |
setCustomValidity(message) | Set (non-empty) or clear (empty string) a custom validation error, like native controls. |
setValidityState(flags, message, anchor) | For subclasses: set the validity flags and message. anchor is the element the browser points its message at. |
validate() | Show (or clear) the error from the control's own checks: required, type, pattern, min,
max and the like. Uses error-message, else the browser's localized message. An error the
author set (a server error, say) is left as it is. |
clearValidation() | Clear the error and warning that validate() and warning-pattern showed (a form reset does). |
emitValue(commit) | For subclasses: fire the value events, as a native control does on a user edit: input
(bubbles, composed), then with commit also change (bubbles, stays in its tree). |
idrefText(ids) | For subclasses: the text of the elements an ID reference list names, resolved in the host's tree (ID references cannot cross a shadow root), joined by spaces. |
show() | Open the picker and focus the color area (not with inline). |
hide() | Close the picker. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired when the user commits a change to the value. |
CSS parts
Style the inside of the component with ::part(), for example focsd-color-picker::part(label).
| Part | Description |
|---|---|
label | The label. |
layout | The label and the body (see label position). |
body | The control with its hint and message. |
hint | The author hint. |
message | The shown error, warning or success message. |
message-icon | The icon of the message. |
trigger | The button that opens the picker. |
swatch | The color chip in the trigger. |
panel | The popover (or inline container) holding the controls. |
area | The saturation and brightness area. |
area-thumb | The area's thumb. |
hue | The hue slider. |
alpha | The opacity slider. |
thumb | A slider thumb. |
field | The text field. |
swatches | The swatch group. |
swatch-option | One swatch. |
eyedropper | The pick-from-screen button. |
contrast | The contrast status. |
announcer | The visually hidden status region that announces a new message. |
Accessibility
- The saturation and brightness area is one
role="slider"whosearia-valuetextreads both values ("Saturation 80%, brightness 40%"). - Every part works by keyboard, and the text field accepts any CSS color, so dragging is never required (WCAG 2.5.7).
- Swatches are a
role="radiogroup"with roving focus; each swatch is named by its color or label. - With
contrast-with, a polite status reports the WCAG contrast ratio against that color. - The pick-from-screen button only appears where the browser supports the EyeDropper API.
Keyboard
| Key | Action |
|---|---|
| Left / Right | Saturation (area), hue or opacity (sliders) |
| Up / Down | Brightness (area), hue or opacity (sliders) |
| Shift + arrows | Bigger steps |
| Page Up / Down | Brightness ±10 (area), hue ±30° or opacity ±10% (sliders) |
| Home / End | Saturation 0 / 100 (area), minimum / maximum (sliders) |
| Arrow keys (swatches) | Move between swatches; Enter or Space picks one |
| Enter | Apply the typed color |
| Escape | Close the popover and return to the button |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.