Components
Color Swatch
A small square, circle or rounded square filled with a color, to show a color next to its name or in a list.
Introduction
Color Swatch is a small piece of a color: a square, a circle or a rounded square. Use it to show a color that is chosen or given, beside its name, in a list of tokens, in a table, or next to a field. It only shows a color: to pick one, see ColorSwatchPicker.
- Built on react-aria's ColorSwatch: it is an image for a screen reader, which hears the name of the color, such as "vibrant cyan blue", instead of a string of digits. Give it an
aria-labelto say what the color is for. - Any color in CSS. One with some transparency is drawn over a checkerboard, so it can be told from a solid one.
- Seven sizes from 12 to 64 pixels, plus
fillto take the size of its parent, and three shapes. showValuewrites the color beside it, as hex, rgb, hsl or hsb.ColorSwatchGroupshows several colors side by side, a little over each other like a stack of avatars, with a "+3" for the ones that do not fit.
A thin edge, dark and faint, keeps a swatch of a color close to the page's own visible, such as white on a white page. It is a Client Component.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx
# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx
# npm
npm install react-aria-components tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/color-swatch-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for ColorSwatch. The color, and the checkerboard behind a color that is
// not solid, are set by react-aria as inline styles, so they are not here. A thin
// dark line inside the edge keeps a swatch of a color close to the page's own
// visible.
export const colorSwatchStyles = tv({
base: "block shrink-0 border shadow-[inset_0_0_0_1px_rgb(0_0_0/0.06)]",
variants: {
size: {
xs: "size-3",
sm: "size-4",
md: "size-6",
lg: "size-8",
xl: "size-12",
"2xl": "size-16",
// As big as its parent, for a swatch drawn inside a box of another control.
fill: "size-full",
},
shape: {
circle: "rounded-full",
rounded: "rounded-md",
square: "rounded-none",
},
},
defaultVariants: { size: "md", shape: "rounded" },
});
export const colorSwatchGroupStyles = tv({
slots: {
root: "flex items-center",
// The ring in the color of the page separates one swatch from the next.
item: "ring-2 ring-bg",
more: "flex shrink-0 items-center justify-center bg-muted font-medium text-muted-fg tabular-nums ring-2 ring-bg",
caption: "ml-3 text-sm text-muted-fg",
},
variants: {
overlap: {
none: { root: "gap-1.5", item: "ring-0" },
sm: { root: "[&>*+*]:-ml-1" },
md: { root: "[&>*+*]:-ml-2" },
lg: { root: "[&>*+*]:-ml-3" },
},
shape: {
circle: { more: "rounded-full" },
rounded: { more: "rounded-md" },
square: { more: "rounded-none" },
},
size: {
xs: { more: "size-3 text-[8px]" },
sm: { more: "size-4 text-[9px]" },
md: { more: "size-6 text-[10px]" },
lg: { more: "size-8 text-xs" },
xl: { more: "size-12 text-sm" },
"2xl": { more: "size-16 text-base" },
},
},
defaultVariants: { overlap: "sm", shape: "circle", size: "md" },
});2. Add the Color Swatch file
src/components/ui/color-swatch/color-swatch.tsxShow code
"use client";
import {
ColorSwatch as AriaColorSwatch,
type Color,
parseColor,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import {
colorSwatchGroupStyles,
colorSwatchStyles,
} from "@/types/color-swatch-styles";
/** How the color is written next to a swatch. */
export type ColorSwatchFormat = "hex" | "rgb" | "hsl" | "hsb";
export interface ColorSwatchProps {
/**
* The color, written in CSS: `"#3b82f6"`, `"rgb(59, 130, 246)"`,
* `"hsl(217, 91%, 60%)"` or `"hsb(217, 76%, 96%)"`. Inside a color swatch
* picker it is left out, and the swatch takes the color of its option.
*/
color?: string;
/** How big it is. `fill` is as big as its parent. Defaults to `md`. */
size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "fill";
/** Round, rounded squares or square. Defaults to `rounded`. */
shape?: "circle" | "rounded" | "square";
/**
* Words for a screen reader, added after the name of the color, which is
* always said: "vibrant cyan blue, Brand color". The name is worked out from the
* color, so `#3b82f6` is not read as a string of digits.
*/
"aria-label"?: string;
/** Write the color next to the swatch, in this format, or in hex if `true`. Defaults to `false`. */
showValue?: boolean | ColorSwatchFormat;
className?: string;
}
const write = (color: Color, format: ColorSwatchFormat) => {
const alpha = color.getChannelValue("alpha") < 1;
const text = color.toString(alpha ? (`${format}a` as const) : format);
return format === "hex" ? text.toLowerCase() : text;
};
// The checkerboard behind a color that is not fully opaque.
const checker =
"repeating-conic-gradient(#cccccc 0% 25%, #ffffff 0% 50%) 50% / 8px 8px";
function safeParse(text: string | undefined): Color | null {
if (!text) return null;
try {
return parseColor(text);
} catch {
return null;
}
}
/**
* A small square, circle or rounded square filled with a color. Use it to show a
* color that is chosen or given, next to its name or in a list, in a table of
* tokens or beside a field. A color with some transparency is drawn over a
* checkerboard, so it can be told from a solid one.
*
* Built on react-aria's ColorSwatch, so it is an image for a screen reader, which
* hears the name of the color ("vibrant cyan blue") instead of a hex.
*
* It is a `div`, so it cannot go inside a `p` (or any element that only holds
* text): put it in a `div`, a flex row or a list item, next to the text.
*
* @example
* <ColorSwatch color="#3b82f6" />
* <ColorSwatch color="rgba(59, 130, 246, 0.5)" size="lg" shape="circle" showValue />
*/
export function ColorSwatch({
color,
size,
shape,
"aria-label": ariaLabel,
showValue = false,
className,
}: ColorSwatchProps) {
const parsed = safeParse(color);
const swatch = (
<AriaColorSwatch
color={parsed ?? undefined}
aria-label={ariaLabel}
style={({ color: current }) =>
// react-aria paints the color, but not a checkerboard under it.
current.getChannelValue("alpha") < 1
? {
background: `linear-gradient(${current.toString("css")}, ${current.toString("css")}), ${checker}`,
}
: {}
}
className={cn(colorSwatchStyles({ size, shape }), className)}
/>
);
if (!showValue || !parsed) return swatch;
return (
// A div, not a span: the swatch is a div, which a span cannot hold.
<div className="inline-flex items-center gap-2">
{swatch}
<code className="font-mono text-xs tabular-nums">
{write(parsed, showValue === true ? "hex" : showValue)}
</code>
</div>
);
}
export interface ColorSwatchGroupProps {
/** The colors, written in CSS. */
colors: string[];
size?: "xs" | "sm" | "md" | "lg" | "xl" | "2xl";
/** Defaults to `circle`. */
shape?: "circle" | "rounded" | "square";
/** How much each swatch covers the one before it. Defaults to `sm`. */
overlap?: "none" | "sm" | "md" | "lg";
/** Show only this many, and a "+3" for the rest. */
max?: number;
/** Names the group for screen readers, such as "Palette". Defaults to the number of colors. */
"aria-label"?: string;
className?: string;
}
/**
* Several colors side by side, a little over each other like a stack of avatars:
* the colors of a palette or a theme in a small space. With `max` it shows the
* first few and how many more there are.
*
* @example
* <ColorSwatchGroup colors={["#ef4444", "#22c55e", "#3b82f6", "#a855f7"]} max={3} />
*/
export function ColorSwatchGroup({
colors,
size = "md",
shape = "circle",
overlap = "sm",
max,
"aria-label": ariaLabel,
className,
}: ColorSwatchGroupProps) {
const styles = colorSwatchGroupStyles({ overlap, shape, size });
const shown = max === undefined ? colors : colors.slice(0, max);
const rest = colors.length - shown.length;
return (
// biome-ignore lint/a11y/useSemanticElements: a group of images, not a set of form fields
<div
role="group"
aria-label={ariaLabel ?? `${colors.length} colors`}
className={cn(styles.root(), className)}
>
{shown.map((color, i) => (
<ColorSwatch
// biome-ignore lint/suspicious/noArrayIndexKey: two entries can be the same color
key={i}
color={color}
size={size}
shape={shape}
className={overlap === "none" ? undefined : styles.item()}
/>
))}
{rest > 0 ? (
<span className={styles.more()} role="img" aria-label={`${rest} more`}>
+{rest}
</span>
) : null}
</div>
);
}3. Use it
import { ColorSwatch, ColorSwatchGroup } from "@/components/ui/color-swatch/color-swatch";Anatomy
A swatch, and beside it, if you ask, the color as text.
<ColorSwatch color="#3b82f6" size="lg" shape="circle" showValue />| Part | Rendered as | Notes |
|---|---|---|
| Swatch | div, role img | The colored square, circle or rounded square. It has a thin edge, so a color close to the page's own can still be seen. |
| Checkerboard | - | Behind a color with some transparency, so it can be told from a solid one. |
| Value | code | The color as text beside the swatch, in the format you choose. Off unless showValue. |
Examples
Usage
Sizes
12, 16, 24, 32, 48 and 64 pixels.
Shapes
Transparency
A color with some transparency is drawn over a checkerboard.
Showing the value
showValue writes the color beside the swatch, in hex or in the format you name.
#3b82f6rgb(59, 130, 246)hsl(217.22, 91.22%, 59.8%)hsb(217.22, 76.02%, 96.47%)#3b82f680For screen readers
A swatch is an image, and a screen reader says the name of its color: not a hex, which is a string of digits. Set aria-label to add what the color is for, which is said after the name.
Inside text
A small swatch sits in a line of text. It is a div, so the line is a flex row and not a p, which cannot hold a div.
In a list
A swatch before each name, for a list of tokens or statuses.
- Danger
- Success
- Warning
A group
ColorSwatchGroup shows several colors side by side, a little over each other, with a ring in the color of the page between them.
A group with a limit
max shows the first few and how many more there are.
Overlap
Group shapes
Component API
ColorSwatch and ColorSwatchGroup take the props below.
ColorSwatch
| Prop | Type | Default |
|---|---|---|
| color | string | - |
| size | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "fill" | "md" |
| shape | "circle" | "rounded" | "square" | "rounded" |
| aria-label | string | - (the name of the color is always said) |
| showValue | boolean | "hex" | "rgb" | "hsl" | "hsb" | false |
| className | string | - |
ColorSwatchGroup
| Prop | Type | Default |
|---|---|---|
| colors | string[] | - |
| size | "xs" | "sm" | "md" | "lg" | "xl" | "2xl" | "md" |
| shape | "circle" | "rounded" | "square" | "circle" |
| overlap | "none" | "sm" | "md" | "lg" | "sm" |
| max | number | - (all) |
| aria-label | string | "N colors" |
| className | string | - |