Color Swatch Picker
Features
- Can be controlled or uncontrolled.
- Focus is fully managed.
- Full keyboard navigation.
- Supports Right to Left direction.
- Different selection behavior.
- Accepts color strings and Color objects, compared structurally.
Installation
Install the component from your command line.
$ npm add reka-uiLooking for a complete color picker? Check out the Color Picker example that combines Color Area, Color Slider, Color Field, and Color Swatch components.
Anatomy
Import all parts and piece them together.
<script setup>
import {
ColorSwatchPickerItem,
ColorSwatchPickerItemIndicator,
ColorSwatchPickerItemSwatch,
ColorSwatchPickerRoot,
} from 'reka-ui'
</script>
<template>
<ColorSwatchPickerRoot>
<ColorSwatchPickerItem value="#ff0000">
<ColorSwatchPickerItemSwatch />
<ColorSwatchPickerItemIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem value="#00ff00">
<ColorSwatchPickerItemSwatch />
<ColorSwatchPickerItemIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem value="#0000ff">
<ColorSwatchPickerItemSwatch />
<ColorSwatchPickerItemIndicator />
</ColorSwatchPickerItem>
</ColorSwatchPickerRoot>
</template>Headless composable
These composables are experimental. Reactive options accept refs or getters. Pass a writable modelValue ref for ref-owned state, or use onUpdate to handle controlled updates. onBeforeUpdate(value, details) can synchronously call details.cancel() to prevent a change. Component users can use @before-update:model-value for the same behavior.
useColorSwatchPicker() is pure and exposes the model, setValue, programmatic select, and color-specific part surfaces. Compose them with the Listbox components for collection registration, selection interactions, keyboard navigation, focus, and indicator presence.
<script setup>
import { ListboxContent, ListboxItem, ListboxRoot, useColorSwatchPicker } from 'reka-ui'
const colors = ['#ff0000', '#00ff00', '#0000ff']
const { listboxProps, getItemSurface } = useColorSwatchPicker({ defaultValue: '#ff0000' })
</script>
<template>
<ListboxRoot v-bind="listboxProps">
<ListboxContent aria-label="Color">
<ListboxItem
v-for="color in colors"
:key="color"
:value="color"
v-bind="getItemSurface(color).attrs.value"
>
{{ color }}
</ListboxItem>
</ListboxContent>
</ListboxRoot>
</template>getItemSwatchSurface(color) supplies props for ColorSwatch; itemIndicator composes with ListboxItemIndicator. These surfaces do not implement Listbox behavior on plain elements. Delegated model updates have the selection reason; Listbox's model event does not supply a native event.
root.attrs contains only DOM attributes. listboxProps contains the bindings for the child component shown above.
Values
Item values and the model accept a color string or a Color object. Values are compared structurally, so a model of '#ff0000' selects an item declared with { space: 'hsl', h: 0, s: 100, l: 50, alpha: 1 }, and the reverse. The picker emits the value of the selected item as you declared it.
<script setup>
import { ColorSwatchPickerItem, ColorSwatchPickerItemSwatch, ColorSwatchPickerRoot } from 'reka-ui'
import { ref } from 'vue'
const colors = [
{ space: 'hsl', h: 0, s: 100, l: 50, alpha: 1 },
{ space: 'hsl', h: 210, s: 100, l: 50, alpha: 1 },
]
const color = ref('#ff0000') // selects the first item
</script>
<template>
<ColorSwatchPickerRoot v-model="color">
<ColorSwatchPickerItem
v-for="(item, index) in colors"
:key="index"
:value="item"
>
<ColorSwatchPickerItemSwatch />
</ColorSwatchPickerItem>
</ColorSwatchPickerRoot>
</template>API Reference
ColorSwatchPickerRoot
The main component that displays a color swatch picker.
ColorSwatchPickerItem
The item that represents a selectable color swatch.
ColorSwatchPickerItemSwatch
The component that displays the color swatch within an item.
ColorSwatchPickerItemIndicator
The component that indicates the selected color swatch within an item.
