ColorSwatchPicker
A swatch picker offers the colours somebody has already decided on — a brand palette, a set of label colours. That is the whole difference from the rest of the colour components: a ColorArea or a ColorSlider lets the reader reach any colour, and this one deliberately does not.
import {
ColorSwatchPicker,
ColorSwatchPickerIndicator,
ColorSwatchPickerItem,
ColorSwatchPickerSwatch,
} from "ropav";Each ColorSwatchPickerItem carries a color and holds two parts: ColorSwatchPickerSwatch paints it, and ColorSwatchPickerIndicator marks the selected one.
<ColorSwatchPicker aria-label="Brand colour" default-value="#3b82f6">
<ColorSwatchPickerItem color="#f43f5e">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem color="#8b5cf6">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem color="#3b82f6">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
<ColorSwatchPickerItem color="#10b981">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
</ColorSwatchPicker>A palette
variant is the shape of a swatch and layout is how they are arranged — grid wraps into rows, stack runs down a column.
color-swatch-picker-presets.vue
<script setup lang="ts">
import {
ColorSwatchPicker,
ColorSwatchPickerIndicator,
ColorSwatchPickerItem,
ColorSwatchPickerSwatch,
} from "ropav";
const presets = ["#f43f5e", "#d946ef", "#8b5cf6", "#3b82f6", "#06b6d4", "#10b981", "#84cc16"];
</script>
<template>
<ColorSwatchPicker aria-label="Brand colour" default-value="#3b82f6" variant="square">
<ColorSwatchPickerItem v-for="preset in presets" :key="preset" :color="preset">
<ColorSwatchPickerSwatch />
<ColorSwatchPickerIndicator />
</ColorSwatchPickerItem>
</ColorSwatchPicker>
</template>layout picks the modifier and nothing else: the keyboard stays two-dimensional either way, so the arrow keys work the same in a column as in a grid.
Inside a ColorPicker the picker reads and writes the shared colour, which is how a palette sits beside a slider and the two stay in step.
Accessibility
- Each swatch is a
role="img"announced by the colour's own name — "vibrant red", not#f43f5e— generated from the value, so a palette is usable without seeing it. color-nameon a swatch replaces that generated name where your own is better. A brand name belongs there: "Ocean" says more than "medium blue" to somebody choosing from your palette.- The picker names itself if nothing else does, so
aria-labelis what says which colour on the page is being chosen — the swatches only say what they are, not what they are for.
API
ColorSwatchPicker
| Prop | Type | Default | Description |
|---|---|---|---|
ariaLabel | string | — | Labels the picker. Without one — and without ariaLabelledby — it names itself. |
ariaLabelledby | string | — | Id of an element labelling the picker. |
defaultValue | string | Color | "#000000" | Colour the picker starts on while it is uncontrolled. |
layout | "grid" | "stack" | "grid" | Whether the swatches wrap into rows or stack into a column. Only picks the BEM modifier: the keyboard stays two-dimensional either way, and so does data-layout, because the React build destructures this prop out and never forwards it to the collection underneath. Mirrored rather than fixed so both builds navigate alike. |
size | "xs" | "sm" | "md" | "lg" | "xl" | "md" | |
value | string | Color | — | Controlled colour. A string is parsed. |
variant | "circle" | "square" | "circle" |
The default slot receives:
| Slot prop | Type |
|---|---|
color | Color |
| Event | Payload |
|---|---|
change | Color |
update:value | Color |
ColorSwatchPickerIndicator
The default slot receives:
| Slot prop | Type |
|---|---|
color | Color |
isDisabled | boolean |
isFocused | boolean |
isFocusVisible | boolean |
isHovered | boolean |
isPressed | boolean |
isSelected | boolean |
ColorSwatchPickerItem
| Prop | Type | Default | Description |
|---|---|---|---|
color | string | Color | "#0000" | The colour this swatch stands for. A string is parsed. |
isDisabled | boolean | — |
The default slot receives:
| Slot prop | Type |
|---|---|
color | Color |
isDisabled | boolean |
isFocused | boolean |
isFocusVisible | boolean |
isHovered | boolean |
isPressed | boolean |
isSelected | boolean |
ColorSwatchPickerSwatch
| Prop | Type | Default | Description |
|---|---|---|---|
ariaLabel | string | — | Labels the swatch in addition to the colour's own name, which is kept. |
ariaLabelledby | string | — | Id of an element labelling the swatch, appended after the swatch's own id. |
color | string | Color | — | The colour to show. Defaults to the item's own, which is the ordinary case — a swatch inside an item exists to show that item's colour. |
colorName | string | — | A localized accessible name for the colour, replacing the generated one. |
id | string | — | Id override for the swatch element. |