ColorSwatch
Grid of color swatches with click-to-copy CSS values — for theme and design-system pages.
The ColorSwatch component from @publier/primitives renders a clickable grid of colors with their labels. Clicking a swatch copies its CSS value to the clipboard — ideal for design-system pages or theme references where readers need to grab a color quickly.
Usage
import { ColorSwatch } from '@publier/primitives';
<ColorSwatch columns={4} size="md" colors={[ { name: 'Primary 500', value: '#6366f1' }, { name: 'Primary 600', value: '#4f46e5' }, { name: 'Accent', value: 'hsl(210 80% 55%)' }, { name: 'Surface', value: 'var(--color-base-200)' }, ]}/>Props
ColorSwatch
| Prop | Type | Default | Description |
|---|---|---|---|
colors | ColorEntry[] | required | Array of color entries to render. |
columns | 2 | 3 | 4 | 5 | 6 | 4 | Grid column count on desktop. |
size | 'sm' | 'md' | 'lg' | 'md' | Swatch square size. |
ColorEntry
| Prop | Type | Default | Description |
|---|---|---|---|
name | string | required | Display name, e.g. "Primary 500". |
value | string | required | CSS value — hex, rgb/rgba, hsl, or var() reference. |
textDark | boolean | auto | Force dark label text. Auto-detected from luminance for hex/rgb/hsl; defaults to white for CSS variables and unresolvable HSL. |
Behaviour
- Click any swatch to copy its
valueto the clipboard. A brief “Copied” state flashes for 1800 ms. - Label text color is chosen per swatch from the value’s luminance (WCAG 2.1 relative luminance). Override with
textDark: true. - Clipboard errors (non-HTTPS origin, denied permission) are caught silently — no visible failure.
Usage in .astro and MDX
Drop <ColorSwatch> straight into MDX or .astro — no client directive required. The swatch becomes interactive on first user input.