Color picker
newFifty shades of grey. And 16.7 million others. A colour picker built from parts an area, channel sliders, a field, a preview and swatches.
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 squareColorPickerSlider: onechannel,hueby default; a regular Slider with the same variants, sizes and touch targetsColorPickerField: hex, or a single channel as a number withchannelColorPickerPreview: the current colourColorPickerSwatches,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.
Channels
Sliders and fields for red, green, blue and alpha instead of an area; the parts compose into any picker.
Formats
format picks how the model is written; changing it rewrites the model at once.
#f97316Field
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.
Used on buttons and links.
API Reference
Every part accepts class, merged over its base classes.
ColorPicker
| Prop | Default | Type and description |
|---|---|---|
| id | — | stringThe id of the group, which a |
| defaultValue | — | stringThe colour at first when |
| format | "hex" | ColorPickerFormatHow the model is written: |
| size | "md" | ColorPickerSizeScales the area height from 96px for |
| disabled | — | booleanDisables every part. Follows a surrounding |
| required | — | booleanMarks the group required; follows a surrounding |
| name | — | stringAdds a hidden input with the model, for a form submission. |
| modelValue | — | stringThe colour as a string in |
| Event | Payload and description |
|---|---|
| update:modelValue | [value: string | undefined]Fires with the colour in |
| Slot | Scope and description |
|---|---|
| default | {}The parts, in any layout. |
ColorPickerArea
| Prop | Default | Type and description |
|---|---|---|
| colorSpace | "hsb" | ColorSpaceThe space the two axes live in, |
| xChannel | "saturation" | ColorChannelThe channel on the horizontal axis, |
| yChannel | "brightness" | ColorChannelThe channel on the vertical axis, |
| xName | — | stringAdds a hidden input with the horizontal channel's value, for a form submission. |
| yName | — | stringAdds a hidden input with the vertical channel's value, for a form submission. |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| name | — | stringPassed to Reka UI's area; the picker's own |
| required | — | booleanMarks the hidden inputs required. |
ColorPickerSlider
| Prop | Default | Type and description |
|---|---|---|
| colorSpace | "hsb" | ColorSpaceThe space |
| orientation | — | DataOrientation
|
| dir | — | DirectionReading direction. Inherits from a surrounding |
| inverted | — | booleanPuts the channel's maximum at the start. |
| step | — | numberThe keyboard step. One unit of the channel by default. |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| name | — | stringAdds a hidden input with the colour as hex, for a form submission. |
| required | — | booleanMarks the hidden input required. |
| channel | "hue" | ColorChannelThe channel the slider moves: |
| variant | — | "default" | "inset" | nullAs on |
| touchTarget | — | "none" | "expand" | "wrapper" | nullAs on |
ColorPickerField
| Prop | Default | Type and description |
|---|---|---|
| colorSpace | "hsb" | ColorSpaceThe space |
| channel | — | ColorChannelMakes the field a number input for one channel instead of a hex input: |
| placeholder | — | stringShown while the input is empty. |
| readonly | — | booleanShows the value without letting it be edited. |
| disableWheelChange | — | booleanStops the mouse wheel from changing the value while the input has focus. |
| locale | — | stringThe locale numbers are read in. Inherits from a surrounding |
| step | — | numberThe keyboard step. One unit of the channel by default, or one of the hex integer. |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render around the input. |
| name | — | stringAdds a hidden input with the colour as hex, for a form submission. |
| required | — | booleanMarks the hidden input required. |
| variant | — | "outline" | "soft" | "filled" | "ghost" | "subtle" | nullThe text control look, as on |
ColorPickerPreview
| Prop | Default | Type and description |
|---|---|---|
| color | — | string | ColorA colour to show instead of the picker's current one. |
| label | — | stringThe accessible name. The colour's own name, or its string, by default. |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
ColorPickerSwatches
| Prop | Default | Type and description |
|---|---|---|
| orientation | — | DataOrientation
|
| dir | — | DirectionReading direction. Inherits from a surrounding |
| highlightOnHover | — | booleanHighlights the swatch under the pointer. |
| loop | — | booleanWraps arrow-key navigation from the last swatch to the first and back. Needs |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| name | — | stringAdds a hidden input with the selected swatch, for a form submission. |
| required | — | booleanMarks the hidden input required. |
| Slot | Scope and description |
|---|---|
| default | {}The swatches. |
ColorPickerSwatch
| Prop | Default | Type and description |
|---|---|---|
| value* | — | stringThe swatch's colour, in any accepted format. Selected while it matches the picker's colour. |
| disabled | — | booleanSkips the swatch for the arrow keys and ignores clicks. |
| asChild | — | booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it. |
| as | — | AsTag | ComponentThe element or component to render. |
| Slot | Scope and description |
|---|---|
| default | {}The indicator shown while selected. A check icon by default. |
Props
- id
stringThe id of the group, which aFieldLabelpoints at. Comes from a surroundingFieldwhen not set.- defaultValue
stringThe colour at first whenv-modelis not used. White by default.- format
"hex" ColorPickerFormatHow the model is written:hex(#rrggbb,#rrggbbaawith alpha),rgb,hslorhsb. Any of them is accepted on the way in.- size
"md" ColorPickerSizeScales the area height from 96px forxsto 224px forxl, with the slider, field, preview, swatch and gap sizes to match.- disabled
booleanDisables every part. Follows a surroundingField.- required
booleanMarks the group required; follows a surroundingField.- name
stringAdds a hidden input with the model, for a form submission.- modelValue
stringThe colour as a string informat, bound withv-model.
Emits
- update:modelValue
[value: string | undefined]Fires with the colour informatwhenever a part changes it, and once on mount whenv-modelis not used.
Slots
- default
{}The parts, in any layout.
Props
- colorSpace
"hsb" ColorSpaceThe space the two axes live in,hsbby default, orhsl.- xChannel
"saturation" ColorChannelThe channel on the horizontal axis,saturationby default.- yChannel
"brightness" ColorChannelThe channel on the vertical axis,brightnessby default, orlightnessinhsl.- xName
stringAdds a hidden input with the horizontal channel's value, for a form submission.- yName
stringAdds a hidden input with the vertical channel's value, for a form submission.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.- name
stringPassed to Reka UI's area; the picker's ownnameis the one to use for forms.- required
booleanMarks the hidden inputs required.
Props
- colorSpace
"hsb" ColorSpaceThe spacesaturationis read in,hsbby default, orhsl.- orientation
DataOrientationhorizontalby default, orvertical. Arrow keys follow the orientation.- dir
DirectionReading direction. Inherits from a surroundingConfigProviderwhen not set. Otherwiseltr.- inverted
booleanPuts the channel's maximum at the start.- step
numberThe keyboard step. One unit of the channel by default.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.- name
stringAdds a hidden input with the colour as hex, for a form submission.- required
booleanMarks the hidden input required.- channel
"hue" ColorChannelThe channel the slider moves:hueby default,saturation,brightness,lightness,red,green,blueoralpha. The track shows the channel's gradient through the current colour, andalphadraws a checkerboard under it.- variant
"default" | "inset" | nullAs onSlider:defaultruns the thumb over a thin track,insetsets it inside a thick one.- touchTarget
"none" | "expand" | "wrapper" | nullAs onSlider:expandgrows the hit area of the track and the thumb to 48px without changing the layout,wrapperreserves that room.
Props
- colorSpace
"hsb" ColorSpaceThe spacesaturationis read in,hsbby default, orhsl.- channel
ColorChannelMakes the field a number input for one channel instead of a hex input:hue,saturation,brightness,lightness,red,green,blueoralpha.- placeholder
stringShown while the input is empty.- readonly
booleanShows the value without letting it be edited.- disableWheelChange
booleanStops the mouse wheel from changing the value while the input has focus.- locale
stringThe locale numbers are read in. Inherits from a surroundingConfigProviderwhen not set.- step
numberThe keyboard step. One unit of the channel by default, or one of the hex integer.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render around the input.- name
stringAdds a hidden input with the colour as hex, for a form submission.- required
booleanMarks the hidden input required.- variant
"outline" | "soft" | "filled" | "ghost" | "subtle" | nullThe text control look, as onInput:outlineby default,soft,filled,ghostorsubtle.
Props
- color
string | ColorA colour to show instead of the picker's current one.- label
stringThe accessible name. The colour's own name, or its string, by default.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.
Props
- orientation
DataOrientationhorizontalby default, orvertical. Arrow keys follow the orientation.- dir
DirectionReading direction. Inherits from a surroundingConfigProviderwhen not set. Otherwiseltr.- highlightOnHover
booleanHighlights the swatch under the pointer.- loop
booleanWraps arrow-key navigation from the last swatch to the first and back. Needsreka-ui2.11 or later.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.- name
stringAdds a hidden input with the selected swatch, for a form submission.- required
booleanMarks the hidden input required.
Slots
- default
{}The swatches.
Props
- value*
stringThe swatch's colour, in any accepted format. Selected while it matches the picker's colour.- disabled
booleanSkips the swatch for the arrow keys and ignores clicks.- asChild
booleanRender the single child instead of a wrapper, merging this part's attributes and classes onto it.- as
AsTag | ComponentThe element or component to render.
Slots
- default
{}The indicator shown while selected. A check icon by default.
Data attributes
| data-slot | Attributes | Description |
|---|---|---|
| color-picker | data-size, data-disabled, role="group", aria-labelledby, aria-required | The root, with the hidden input when name is set. |
| color-picker-area | data-disabled, aria-disabled | The area's root. |
| color-picker-area-canvas | data-disabled | The gradient plane, a role="application" that takes the pointer. |
| color-picker-area-thumb | data-hovered, data-disabled, aria-valuetext, aria-invalid, aria-describedby | The thumb, a role="slider" with the halo. |
| color-picker-area-handle | — | The visible dot, filled with the colour. |
| color-picker-slider | data-channel, data-variant, data-touch-target, data-orientation, data-disabled | The slider's root, with Slider's variables. |
| color-picker-slider-track | — | The gradient track. |
| color-picker-slider-thumb | data-hovered, data-disabled, aria-valuenow, aria-valuetext, aria-invalid, aria-describedby | The thumb, a role="slider" with the halo. |
| color-picker-slider-handle | — | The visible dot, in the colour the thumb points at. |
| color-picker-field | data-disabled, data-readonly | The field's root group. |
| color-picker-field-input | data-variant, data-disabled, data-readonly, aria-invalid, aria-describedby | The text input. |
| color-picker-preview | data-color-contrast, data-no-color | The swatch, a role="img" named by the colour. |
| color-picker-swatches | data-orientation, data-disabled | The listbox of presets. |
| color-picker-swatch | data-state, data-color, data-highlighted, data-disabled, aria-selected | One preset, a round option; data-state is checked while it matches. |
| color-picker-swatch-color | data-color-contrast | The 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
| Key | Action |
|---|---|
| ArrowLeft, ArrowRight | Moves the area thumb along its horizontal channel, or a horizontal slider by one step. |
| ArrowUp, ArrowDown | Moves the area thumb along its vertical channel, a vertical slider by one step, or a field's value by one step. |
| Shift + Arrow | Moves the area thumb by ten steps. |
| PageUp, PageDown | Moves a slider or a field by ten steps. |
| Home, End | Jumps a slider or a field to its minimum or maximum; jumps the area thumb to the horizontal ends. |
| Enter | Commits the field's text, or selects the focused swatch. |
| Space | Selects the focused swatch. |