Skip to content
Focsd homeFocsd homeFocsd
Forms

Swatch picker

A short list of colors to choose one from, with an optional custom color.

  • Tag: focsd-swatch-picker
  • Pattern: Radio group of swatches + custom color popover
  • Category: Forms

Example

Teal Rust
HTML
<div class="stack">
<focsd-swatch-picker
label="Label color"
name="color"
value="#ff6347"
swatches="#ff6347,#2e8b57,#123456"></focsd-swatch-picker>
<focsd-swatch-picker
label="Theme"
name="theme"
allow-custom
required
hint="Pick one, or add your own">
<focsd-option value="#0f766e" color="#0f766e">Teal</focsd-option>
<focsd-option value="#7c2d12" color="#7c2d12">Rust</focsd-option>
</focsd-swatch-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-swatch-picker.js';
</script>
<focsd-swatch-picker label="Label color" name="color" value="#ff6347" swatches="#ff6347,#2e8b57,#123456"></focsd-swatch-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—
swatchesswatchesstring—
allow-customallowCustombooleanfalse

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.

Events

EventDescription
inputFired on every user edit of the value.
changeFired when the user commits a change to the value or state.

CSS parts

Style the inside of the component with ::part(), for example focsd-swatch-picker::part(label).

PartDescription
labelThe label element.
layoutThe wrapper that places the label beside or above the body.
bodyThe swatches, the custom color control, the hint and the message.
rowThe swatches and the custom color button on one line.
groupThe radio group holding the swatches.
swatchOne swatch (a radio button).
chipThe color chip inside a swatch or the custom color button.
checkThe check mark in the chosen swatch, or on the custom color button when a custom color is chosen.
iconThe plus icon on the custom color button.
customThe button that opens the custom color popup.
popupThe popup holding the color picker.
pickerThe color picker in the popup.
hintThe hint text.
messageThe shown error, warning or success message (also `error`, `warning` or `success`).
message-iconThe message's icon.
announcerThe visually hidden status region.

Accessibility

  • A role="radiogroup" named by the label. The color attribute of an option is its color (its value is used only when there is no color), and the value that is set and submitted is that color as lower-case hex. Each swatch is a radio named by its option text, else the CSS color name that matches exactly (such as "tomato"), else its hex value. Color names are English; give options a label to localize them.
  • The chosen swatch shows a check mark and a ring, so the choice is not shown by color alone. Swatch borders keep 3:1 against the surface, and swatches keep their colors in forced-colors mode.
  • One tab stop. Arrow keys move to the next swatch and choose it, and wrap at the ends; skipped swatches are disabled ones.
  • With allow-custom, a "Custom color" button opens a popup with a color picker. Escape closes it and returns focus to the button. A custom color shows a check mark on the button; no radio is checked.
  • Form-associated: submits the lower-case hex value, supports required, reset, fieldset disabled and restore. A restored value that is not a color, or not a swatch without allow-custom, becomes empty.

Keyboard

KeyAction
TabMoves to the chosen swatch (or the first), then to the custom color button
Arrow keysMove to the next or previous swatch and choose it
Home / EndFirst or last swatch
Space / EnterChoose the focused swatch, or open the custom color popup
EscapeClose the custom color popup