Skip to content
Focsd homeFocsd homeFocsd
Forms

Color picker

A color chooser with a saturation area, hue and opacity sliders, swatches, and any CSS color typed in.

  • Tag: focsd-color-picker
  • Pattern: 2D slider + sliders + text field + swatches
  • Category: Forms

Example

HTML
<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>

API

Attributes

AttributePropertyTypeDefault
namenamestring—
disableddisabledbooleanfalse
requiredrequiredbooleanfalse
readonlyreadOnlybooleanfalse
aria-labelhostAriaLabelstring | nullnull
aria-descriptionhostAriaDescriptionstring | nullnull
aria-labelledbyhostAriaLabelledBystring | nullnull
aria-describedbyhostAriaDescribedBystring | nullnull
label-positionlabelPosition'top' | 'start' | 'end' | undefined—
hinthintstring—
errorerror——
warningwarningstring | undefined—
successsuccessstring | undefined—
error-messageerrorMessagestring | undefined—
warning-messagewarningMessagestring | undefined—
warning-patternwarningPatternstring | undefined—
labellabelstring—
valuevaluestring#000000
formatformat'hex' | 'rgb' | 'hsl' | 'oklch'hex
alphaalphabooleanfalse
swatchesswatchesstring—
inlineinlinebooleanfalse
contrast-withcontrastWithstring—

Methods

MethodDescription
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

EventDescription
inputFired on every user edit of the value.
changeFired 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).

PartDescription
labelThe label.
layoutThe label and the body (see label position).
bodyThe control with its hint and message.
hintThe author hint.
messageThe shown error, warning or success message.
message-iconThe icon of the message.
triggerThe button that opens the picker.
swatchThe color chip in the trigger.
panelThe popover (or inline container) holding the controls.
areaThe saturation and brightness area.
area-thumbThe area's thumb.
hueThe hue slider.
alphaThe opacity slider.
thumbA slider thumb.
fieldThe text field.
swatchesThe swatch group.
swatch-optionOne swatch.
eyedropperThe pick-from-screen button.
contrastThe contrast status.
announcerThe visually hidden status region that announces a new message.

Accessibility

  • The saturation and brightness area is one role="slider" whose aria-valuetext reads 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

KeyAction
Left / RightSaturation (area), hue or opacity (sliders)
Up / DownBrightness (area), hue or opacity (sliders)
Shift + arrowsBigger steps
Page Up / DownBrightness ±10 (area), hue ±30° or opacity ±10% (sliders)
Home / EndSaturation 0 / 100 (area), minimum / maximum (sliders)
Arrow keys (swatches)Move between swatches; Enter or Space picks one
EnterApply the typed color
EscapeClose the popover and return to the button