Color picker

new

Fifty shades of grey. And 16.7 million others. A colour picker built from parts an area, channel sliders, a field, a preview and swatches.

Demo

Open Props in the corner of the example to try size and disabled.

size goes from xs (96px area) to xl (224px) and scales every part with it.

Installation

Usage

<script setup lang="ts">
import { ref } from 'vue'

import {
  ColorPicker,
  ColorPickerArea,
  ColorPickerField,
  ColorPickerPreview,
  ColorPickerSlider,
} from '@/components/ui/color-picker'

const color = ref('#3b82f6')
</script>

<template>
  <ColorPicker v-model="color" class="w-64">
    <ColorPickerArea />
    <ColorPickerSlider channel="hue" />
    <ColorPickerSlider channel="alpha" />
    <div class="flex gap-(--color-picker-gap)">
      <ColorPickerPreview />
      <ColorPickerField />
    </div>
  </ColorPicker>
</template>

ColorPicker holds one colour and shares it with every part inside. Arrange the parts however you like. It has no width of its own, so set one with class. size sizes all the parts at once.

v-model is a string. It comes out as hex by default (eight digits once there's transparency), or as rgb, hsl or hsb via format. Any of those work as input. Garbage in? Ignored.

  • ColorPickerArea: the saturation and brightness square
  • ColorPickerSlider: one channel, hue by default; a regular Slider with the same variants, sizes and touch targets
  • ColorPickerField: hex, or a single channel as a number with channel
  • ColorPickerPreview: the current colour
  • ColorPickerSwatches, ColorPickerSwatch: presets; the matching one gets a check, which you can replace through the slot

Want a thin border around the preview and swatches? Set --surface-border: var(--border) on them or on :root.

Examples

Chooser

A Button with a colour chip that opens the picker in a Popover.

Chooser

Channels

Sliders and fields for red, green, blue and alpha instead of an area; the parts compose into any picker.

Channels

Formats

format picks how the model is written; changing it rewrites the model at once.

Formats
#f97316

Field

Inside a Field the group takes its label, the parts take the description and error as their aria-describedby, the field input turns aria-invalid, and disabled and required follow the Field. name adds a hidden input for the form.

Field

Used on buttons and links.

Too light for white text.

API Reference

Every part accepts class, merged over its base classes.

Data attributes

data-slotAttributesDescription
color-pickerdata-size, data-disabled, role="group", aria-labelledby, aria-requiredThe root, with the hidden input when name is set.
color-picker-areadata-disabled, aria-disabledThe area's root.
color-picker-area-canvasdata-disabledThe gradient plane, a role="application" that takes the pointer.
color-picker-area-thumbdata-hovered, data-disabled, aria-valuetext, aria-invalid, aria-describedbyThe thumb, a role="slider" with the halo.
color-picker-area-handle—The visible dot, filled with the colour.
color-picker-sliderdata-channel, data-variant, data-touch-target, data-orientation, data-disabledThe slider's root, with Slider's variables.
color-picker-slider-track—The gradient track.
color-picker-slider-thumbdata-hovered, data-disabled, aria-valuenow, aria-valuetext, aria-invalid, aria-describedbyThe thumb, a role="slider" with the halo.
color-picker-slider-handle—The visible dot, in the colour the thumb points at.
color-picker-fielddata-disabled, data-readonlyThe field's root group.
color-picker-field-inputdata-variant, data-disabled, data-readonly, aria-invalid, aria-describedbyThe text input.
color-picker-previewdata-color-contrast, data-no-colorThe swatch, a role="img" named by the colour.
color-picker-swatchesdata-orientation, data-disabledThe listbox of presets.
color-picker-swatchdata-state, data-color, data-highlighted, data-disabled, aria-selectedOne preset, a round option; data-state is checked while it matches.
color-picker-swatch-colordata-color-contrastThe painted swatch; data-color-contrast is dark on a light colour and light on a dark one.
color-picker-swatch-indicator—The check, present only while selected, black or white after the swatch's contrast.

Keyboard

KeyAction
ArrowLeft, ArrowRightMoves the area thumb along its horizontal channel, or a horizontal slider by one step.
ArrowUp, ArrowDownMoves the area thumb along its vertical channel, a vertical slider by one step, or a field's value by one step.
Shift + ArrowMoves the area thumb by ten steps.
PageUp, PageDownMoves a slider or a field by ten steps.
Home, EndJumps a slider or a field to its minimum or maximum; jumps the area thumb to the horizontal ends.
EnterCommits the field's text, or selects the focused swatch.
SpaceSelects the focused swatch.

Changelog

  1. registry 0.22.0

    89ede95 — ColorPicker snaps back to its model when the parent turns an update down, as Rating does, instead of showing a colour the model never took.

    e04cde3 — ColorPicker follows a controlled v-model that an undo or a reset sets back to the colour the picker last wrote; it used to skip that value as its own echo and stay on the colour in between.

    750df71 — A Field or FieldSet inside a disabled FieldSet is disabled too, so a Slider, Rating or ColorPicker in its own Field stops taking focus and input while the form is locked. Only disabled passes down; invalid and required stay with the part that sets them.

    70d1620 — Colours written inside arbitrary values and variables now resolve where the element sits, so a .dark island (or any subtree that sets a token) gets its own: the filled Input, InputGroup and DatePicker underline, the Table hover, selected and stripe tints, the disabled Slider and Rating fills and the ColorPicker checkerboard read --theme(--color-x) instead of var(--color-x), which Tailwind resolves once on :root. The ripple wave reads --state-pressed where it is pressed (the ripple item no longer pins --kappa-ripple-opacity on :root), so a subtree that changes --state-pressed changes the wave as well as the state layer.

  2. core 0.16.0

    70d1620 — Colours written inside arbitrary values and variables now resolve where the element sits, so a .dark island (or any subtree that sets a token) gets its own: the filled Input, InputGroup and DatePicker underline, the Table hover, selected and stripe tints, the disabled Slider and Rating fills and the ColorPicker checkerboard read --theme(--color-x) instead of var(--color-x), which Tailwind resolves once on :root. The ripple wave reads --state-pressed where it is pressed (the ripple item no longer pins --kappa-ripple-opacity on :root), so a subtree that changes --state-pressed changes the wave as well as the state layer.

  3. registry 0.12.0

    c79028b — The control scale is now a set of tokens: --control-height-*, --control-padding-*, --control-icon-* and --control-gap-* for xs to xl, added to :root by the tokens item. Button, Input, InputFloating, Select, Combobox, InputNumber, TagsInput, PinInput, InputTime, InputGroup, Menu, Tabs, ColorPicker and Pagination read them, so overriding one on :root resizes every control of that size.

    Some sizes that strayed from the scale now follow it. Button icons are 20px at lg and xl instead of 16px, and so are the icons of Toggle, ToggleGroup, Toolbar and Pagination at those sizes. The gap between an icon and its label is 6, 8, 8, 10 and 12px from xs to xl, about half the icon, everywhere that has one: Button and the components built on it (where it was 4, 6, 8, 8 and 8px), Menu items and the Select, Combobox and Command lists (8, 10, 12, 12 and 12px), and Tabs triggers (6, 8, 8, 8 and 10px). Inset and checkbox or radio items move their labels with it, so they stay in line with items that have an icon. Everything else keeps its size.

  4. registry 0.8.0

    8c1319e — ColorPicker: ColorPicker, ColorPickerArea, ColorPickerSlider, ColorPickerField, ColorPickerPreview, ColorPickerSwatches and ColorPickerSwatch over Reka UI's colour primitives. One v-model string in hex, rgb, hsl or hsb feeds every part; the area, the sliders and the swatches work from the keyboard and read out as sliders and options; the sliders are drawn on Slider's parts with its variant, touch-target, sizes, halo and press scale, one size from xs to xl scales the parts together, and a surrounding Field supplies the label, description, error and disabled state.