Color Swatch Picker
Features
- Can be controlled or uncontrolled.
- Focus is fully managed.
- Full keyboard navigation.
- Supports Right to Left direction.
- Different selection behavior.
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>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.
Accessibility
Adheres to the Listbox WAI-ARIA design pattern. The root renders with role="listbox" and each item with role="option", labelled with the name of its color. These names are approximate, so similar shades can end up with the same one; pass your own aria-label to the items when the palette needs more precise names. The picker itself has no built-in label, so give ColorSwatchPickerRoot an accessible name with aria-label or aria-labelledby.
Focus is managed with a roving tabindex, so the picker is a single tab stop. Moving focus with the arrow keys does not change the selection; press Space or Enter to select. The one exception is multiple combined with selectionBehavior="replace", where holding Shift while moving focus extends the selection from the item you last selected, as long as the range stays within max when one is set.
Keyboard Interactions
| Key | Description |
|---|---|
Tab | Moves focus into or out of the picker. |
ArrowRight | Moves focus to the next item when orientation is horizontal (default). Reversed when dir is rtl. |
ArrowLeft | Moves focus to the previous item when orientation is horizontal (default). Reversed when dir is rtl. |
ArrowDown | Moves focus to the next item when orientation is vertical. |
ArrowUp | Moves focus to the previous item when orientation is vertical. |
Home | Moves focus to the first item. |
End | Moves focus to the last item. |
Space | Selects the focused item. With the default selectionBehavior of toggle, deselects it when it is already selected. |
Enter | Selects the focused item. With the default selectionBehavior of toggle, deselects it when it is already selected. |
Ctrl/Cmd + A | Selects all items when multiple is set, unless a max is set and would be exceeded. |
