Color Picker
One colour, picked on a square and a hue strip, typed as hex, or taken from a swatch or the screen.
Installation
pnpm dlx @human-kit/design add color-pickernpx @human-kit/design add color-pickeryarn dlx @human-kit/design add color-pickerbunx @human-kit/design add color-pickerThe command copies the files to src/lib/components/ui/color-picker. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as ColorPicker from '$lib/components/ui/color-picker';
let colour = $state('#4f46e5');
</script>
<ColorPicker.Root label="Calendar colour" bind:value={colour} /> Guidelines
- Use the colour picker when any colour is a fair answer: a calendar, a tag, a brand. When only a few colours are allowed, give them as
swatches, or offer them alone in a radio group. - Give
swatcheswith the colours the product already uses, so most people take one with a press and the screen stays in tune. - Turn
alphaon only when a see-through colour means something where it is used. The value then carries a fourth number, which not every consumer reads. - Leave
eyeDropperon: a browser without one gets no button, and one with it lets people match a colour on the screen. - Pick the
formatthat the code storing the colour expects. Hex is the default and the shortest to store.
Accessibility
- The root is a
role="group"named bylabel. Without a label, give it anaria-label. - The square holds two native sliders, one for saturation and one for brightness, each named and with a value a screen reader reads aloud. The hue and alpha strips are a native slider each.
- The arrows step, Shift with an arrow or Page Up and Page Down take a large step, and Home and End go to the ends. The horizontal arrows follow the text direction.
- The hex field reads what was typed at Enter or when the focus leaves. Text that names no colour goes back to the current one.
- The swatches are a
role="listbox": one swatch is in the tab order, the arrows move between them, and Enter or Space takes the colour. - The eye dropper button is named "Pick a colour from the screen", and it is not drawn where the browser cannot pick.
API reference
| Prop | Type | Default |
|---|---|---|
value | string The colour, as text in the | '#4f46e5' |
defaultValue | string The colour at the start, when no | — |
onChange | (value: string, details: ColorPickerChangeDetails) => void Runs on each change, every move of a drag included. | — |
onChangeEnd | (value: string, details: ColorPickerChangeDetails) => void Runs when a run of changes ends: a key released, a drag let go. For work too heavy to run on each move. | — |
label | string The name over the picker. | — |
swatches | string[] Colours to take with one press, under the field. | — |
variant | 'outline' | 'filled' | 'ghost' On a card of its own, on a soft grey, or with no card, inside a popover or a panel. | 'outline' |
fieldVariant | 'default' | 'filled' | 'ghost' | 'underline' The look of the field the colour is typed in, as any field of the kit. | 'default' |
eyeDropper | boolean The button that takes a colour from the screen, where the browser has one. | true |
alpha | boolean Adds an alpha strip and keeps the alpha in the value. | false |
format | 'hex' | 'rgb' | 'hsl' | 'hsb' The text format of the value. | 'hex' |
invalid | boolean Marks the colour as refused: the label turns red and each control gets | false |
disabled | boolean Turns every control off and takes it out of the tab order. | false |
readonly | boolean Keeps the controls in the tab order, but the colour does not change. | false |
name | string The name of the hidden input that carries the colour in a form. | — |
form | string The id of the form that the hidden input belongs to, when the picker is outside it. | — |
Plus every attribute of a <div> but role.