Color Picker
People choose a color from the colorspace, fine-tune hue and opacity, type a HEX value or reuse a saved swatch. In Arabic the field row, the header row and the swatches mirror, while the hue and opacity bars keep running left to right: they map a color scale, not reading order.
Preview
Mirrored is the same English content laid out right to left: it separates a mirroring bug from a translation one.
Installation
Add the component
npx ritla add color-pickerAlso works with shadcn:
npx shadcn@latest add @ritla/color-pickerDependencies it brings
@base-ui/react@phosphor-icons/reactOr copy by hand
Installing by hand copies the component's source into your project. The source comes with a Ritla UI license, so the CLI is the short way to the same files.
Usage
import { ColorPicker } from "@/components/ui/color-picker";const [color, setColor] = useState("#4F46E5"); <ColorPicker aria-label="Brand color" value={color} onValueChange={setColor} />Examples
In a popover, with a contrast check
The field shows the color and its code and opens the picker in a Popover. contrastWith measures white text on the chosen color, warns below 4.5:1 (contrastTarget={3} for icons and borders) and offers the nearest shade that passes, without blocking the choice. showAlpha={false} for colors that are always opaque.
API reference
ColorPicker
| Prop | Type | Default | Description |
|---|---|---|---|
value / onValueChange | string / (value: string) => void | None | The color: #RRGGBB, or #RRGGBBAA when it isn't fully opaque. |
swatches | { value, name? }[] | None | The saved colors: a short row, most recent first. name is read with the code ("Brand indigo, #4F46E5"). Leave it out to hide the row. |
onAddSwatch | (value: string) => void | None | The Add link: saves the current color. |
showAlpha | boolean | true | The opacity bar and its field. |
contrastWith / contrastTarget | string / number | contrastTarget: 4.5 | The color that will sit with the chosen one (the text on it). Below the target a warning shows with the nearest shade that passes. |
aria-label | string | None | The picker's name ("Brand color"). |
Usage guidelines
A color picker chooses an exact color. When a full picker is worth it, how exact values and saved colors work, when to warn about contrast, and the checks before handoff.
1. When a full picker is worth it
A full picker suits people who need an exact color: a brand color for a storefront, a theme, a chart series. When colors only need to be different from each other, like tag or calendar colors, a short row of preset swatches is faster and keeps the product looking consistent.
| You need to | Use | Why |
|---|---|---|
| Match an exact brand or theme color | Color picker with HEX | People paste the value they already have. |
| Tell items apart: tags, labels, calendar events | A few preset swatches | Fewer choices, colors that already work together. |
| Choose light or dark appearance | Switch or Toggle group | A theme is a setting, not a color. |
2. Exact values and saved colors
The HEX field is how people enter a color they already know. Accept pasted values with or without #, in any case, and normalize them. Saved swatches keep a short row of recent or brand colors to reuse; the selected one shows a ring, not only its color.
Do
- Accept
#1570EF,1570efand short forms like#FFF. - Keep saved colors to one short row, most recent first.
- Show which swatch is selected with a ring.
- Give each saved color a name when people can: “Brand blue”.
Don't
- Reject a pasted value because of a missing # or lowercase letters.
- Fill the saved row with dozens of near-identical colors.
- Rely on the swatch color alone to show the selection.
3. Contrast
A color people pick will often end up behind text or next to it. When the picked color is used for text or a background, show a live preview and warn when contrast drops below AA (4.5:1 for text, 3:1 for icons and borders). Suggest a darker or lighter shade instead of blocking the choice.
Do
- Preview the picked color where it will be used.
- Warn when text on the color falls below 4.5:1.
- Offer the nearest shade that passes.
Don't
- Let a brand color make the product's text unreadable without a warning.
- Block a color without explaining why.
4. Arabic and RTL
In the RTL variant the header, the field row and the saved swatches mirror, and the labels switch to Arabic. The hue and alpha bars don't mirror: they map a color scale, not reading order, so red stays at the start of the hue bar in both directions. HEX values are always Latin and left to right.
Fields mirror, scales don't
A storefront color setting in Arabic.
Before handoff
- A full picker only where exact colors matter; presets elsewhere.
- HEX input rules and the saved-colors limit are noted.
- Contrast preview and warning are designed where the color is used on text.
- Arabic screens use the RTL variant, with the bars in LTR order.
Accessibility
Keyboard
Keys are written for left-to-right. In right-to-left the arrow keys swap: the one pointing toward the reading direction moves forward.
| Key | Action |
|---|---|
| Tab | The colorspace, then hue, then opacity, then the fields, then the swatches. |
| ArrowLeftArrowRight | In the colorspace: saturation; on a bar: its value. Right raises it in both languages, since the bars run left to right. |
| ArrowUpArrowDown | In the colorspace: brightness; with Shift, in 10% steps. |
| PageUpPageDownHomeEnd | On a bar: a tenth of the range, and the ends. |
| Enter | In the HEX field, applies the value. |
ARIA
- The colorspace and both bars are
role="slider"with their values in words ("Saturation 70%, brightness 90%"). - The swatches are a radio group, each named with its name and code; the selected one keeps a visible ring, never color alone.
- The HEX field is the precise keyboard way in: it takes the code with or without #, in any case, and the short form.
Do
- Arrows move the colorspace handle and both bars.
- Name each swatch with its color: “Brand blue, #1570EF”.
- Keep HEX as the precise way in from the keyboard.
- A visible ring on the selected swatch and on the focused handle.
Don't
- Dragging as the only way.
- Color alone to show the selected swatch.
Arabic and RTL notes
Mirrors
- The field row (the HEX format, then the code, then opacity), the Saved colors row with Add, and the swatches.
Does not mirror
- The colorspace and the hue and opacity bars: a fixed color scale, red at the left in both languages.
- The HEX code stays left to right, with Latin letters and Western digits.
- Numerals
- Percentages (opacity, saturation, brightness, contrast) follow
numerals; the HEX code never does. - Arabic typography
- The fields are
label-14Medium and the swatch headinglabel-12; no fixed heights on Arabic text.
Do
- The RTL variant.
- HEX values left to right, in Latin letters.
- The hue and alpha bars in their left-to-right order.
Don't
- Mirror the hue or alpha bar.
- HEX with Arabic-Indic digits.
- The English variant on an Arabic screen.