Swatch picker
A short list of colors to choose one from, with an optional custom color.
Example
<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>import { SwatchPicker } from '@focsd-dev/react';
export function Example() { return <SwatchPicker onChange={(e) => console.log(e.target)} label="Label color" name="color" value="#ff6347" swatches="#ff6347,#2e8b57,#123456"></SwatchPicker>;}<script setup>import '@focsd-dev/components/focsd-swatch-picker.js';</script>
<template> <focsd-swatch-picker @change="onChange" label="Label color" name="color" value="#ff6347" swatches="#ff6347,#2e8b57,#123456"></focsd-swatch-picker></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-swatch-picker.js';
// template<focsd-swatch-picker (change)="onChange($event)" label="Label color" name="color" value="#ff6347" swatches="#ff6347,#2e8b57,#123456"></focsd-swatch-picker><script> import '@focsd-dev/components/focsd-swatch-picker.js';</script>
<focsd-swatch-picker onchange={(e) => console.log(e.target)} label="Label color" name="color" value="#ff6347" swatches="#ff6347,#2e8b57,#123456"></focsd-swatch-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 | — |
swatches | swatches | string | — |
allow-custom | allowCustom | boolean | false |
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. |
Events
| Event | Description |
|---|---|
input | Fired on every user edit of the value. |
change | Fired 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).
| Part | Description |
|---|---|
label | The label element. |
layout | The wrapper that places the label beside or above the body. |
body | The swatches, the custom color control, the hint and the message. |
row | The swatches and the custom color button on one line. |
group | The radio group holding the swatches. |
swatch | One swatch (a radio button). |
chip | The color chip inside a swatch or the custom color button. |
check | The check mark in the chosen swatch, or on the custom color button when a custom color is chosen. |
icon | The plus icon on the custom color button. |
custom | The button that opens the custom color popup. |
popup | The popup holding the color picker. |
picker | The color picker in the popup. |
hint | The hint text. |
message | The shown error, warning or success message (also `error`, `warning` or `success`). |
message-icon | The message's icon. |
announcer | The visually hidden status region. |
Accessibility
- A
role="radiogroup"named by the label. Thecolorattribute of an option is its color (itsvalueis used only when there is nocolor), 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 alabelto 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, fieldsetdisabledand restore. A restored value that is not a color, or not a swatch withoutallow-custom, becomes empty.
Keyboard
| Key | Action |
|---|---|
| Tab | Moves to the chosen swatch (or the first), then to the custom color button |
| Arrow keys | Move to the next or previous swatch and choose it |
| Home / End | First or last swatch |
| Space / Enter | Choose the focused swatch, or open the custom color popup |
| Escape | Close the custom color popup |
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.