Skip to the content
human-kit design
22 — Pickers

Color Picker

One colour, picked on a square and a hue strip, typed as hex, or taken from a swatch or the screen.

Calendar colour

Installation

pnpm dlx @human-kit/design add color-picker
npx @human-kit/design add color-picker
yarn dlx @human-kit/design add color-picker
bunx @human-kit/design add color-picker

The 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 swatches with the colours the product already uses, so most people take one with a press and the screen stays in tune.
  • Turn alpha on 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 eyeDropper on: a browser without one gets no button, and one with it lets people match a colour on the screen.
  • Pick the format that the code storing the colour expects. Hex is the default and the shortest to store.

Accessibility

  • The root is a role="group" named by label. Without a label, give it an aria-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

PropTypeDefault
valuestring

The colour, as text in the format. Bindable.

'#4f46e5'
defaultValuestring

The colour at the start, when no value is bound.

—
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.

—
labelstring

The name over the picker.

—
swatchesstring[]

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'
eyeDropperboolean

The button that takes a colour from the screen, where the browser has one.

true
alphaboolean

Adds an alpha strip and keeps the alpha in the value.

false
format'hex' | 'rgb' | 'hsl' | 'hsb'

The text format of the value.

'hex'
invalidboolean

Marks the colour as refused: the label turns red and each control gets aria-invalid.

false
disabledboolean

Turns every control off and takes it out of the tab order.

false
readonlyboolean

Keeps the controls in the tab order, but the colour does not change.

false
namestring

The name of the hidden input that carries the colour in a form.

—
formstring

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.