Reka UI logoReka
backdrop
Components

Color Swatch Picker

Alpha
A component that allows users to select from a set of predefined colors, often used for themes or branding.

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.

sh
$ npm add reka-ui
tip

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

vue
<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

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