Timace

Swatches

Install
npm install react-aria-components
components/ui/swatches.tsx
"use client";

import { ColorSwatch, ColorSwatchPicker, ColorSwatchPickerItem, type ColorSwatchPickerProps } from "react-aria-components";

const styles = {
  root: "flex flex-col gap-(--tui-swatches-label-gap) font-(family-name:--tui-font)",
  label: "text-(length:--tui-swatches-label-size) leading-none font-(--tui-swatches-label-weight) text-(--tui-swatches-label)",
  grid: "flex flex-wrap gap-(--tui-swatches-gap)",
  // The chosen colour keeps its size and gains a ring a step away from it, in the colour of the text.
  item: "flex cursor-default rounded-(--tui-swatches-radius) transition-[scale,outline-color] duration-(--tui-swatches-duration) ease-out [outline:2px_solid_transparent] [outline-offset:var(--tui-swatches-ring-gap)] data-hovered:scale-110 data-pressed:scale-95 data-selected:[outline-color:var(--tui-swatches-selected-ring)] data-focus-visible:[outline-color:var(--tui-swatches-ring)]",
  chip: "size-(--tui-swatches-size) rounded-(--tui-swatches-radius) [box-shadow:inset_0_0_0_1px_rgb(0_0_0_/_0.08)]",
};

export interface SwatchesProps extends Omit<ColorSwatchPickerProps, "className" | "children"> {
  /** The colours on offer, as CSS colours. */
  colors: string[];
  /** A name over the swatches. Without one, pass aria-label. */
  label?: string;
  className?: string;
}

/** A set of colours to choose one from, each a single press. */
export function Swatches({ colors, label, className, ...props }: SwatchesProps) {
  return (
    <div data-slot="swatches" className={[styles.root, className].filter(Boolean).join(" ")}>
      {label && (
        <span aria-hidden className={styles.label}>
          {label}
        </span>
      )}
      <ColorSwatchPicker aria-label={label} {...props} className={styles.grid}>
        {colors.map((color) => (
          <ColorSwatchPickerItem key={color} color={color} className={styles.item}>
            <ColorSwatch className={styles.chip} />
          </ColorSwatchPickerItem>
        ))}
      </ColorSwatchPicker>
    </div>
  );
}
styles/timace-ui.css
:root {
  color-scheme: light;
  --tui-bg: #ffffff;
  --tui-fg: #0a0a0a;
  --tui-muted: #f5f5f5;
  --tui-muted-fg: #737373;
  --tui-border: #e5e5e5;
  --tui-primary: #171717;
  --tui-primary-fg: #fafafa;
  --tui-ring: #a3a3a3;
  --tui-success: #16a34a;
  --tui-warning: #d97706;
  --tui-danger: #dc2626;
  --tui-radius: 8px;
  --tui-border-width: 1px;
  --tui-space: 4px;
  --tui-shadow: none;
  --tui-font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --tui-text: 14px;
  --tui-duration: 150ms;

  /* Swatches */
  --tui-swatches-size: calc(var(--tui-space) * 8);
  --tui-swatches-gap: calc(var(--tui-space) * 3);
  --tui-swatches-label-gap: calc(var(--tui-space) * 3.5);
  --tui-swatches-radius: calc(var(--tui-radius) * 4);
  --tui-swatches-ring-gap: 3px;
  --tui-swatches-label-size: calc(var(--tui-text) - 2px);
  --tui-swatches-label-weight: 500;
  --tui-swatches-duration: 120ms;
  --tui-swatches-label: var(--tui-muted-fg);
  --tui-swatches-selected-ring: var(--tui-fg);
  --tui-swatches-ring: var(--tui-primary);
}

.dark,
[data-theme="dark"] {
  color-scheme: dark;
  --tui-bg: #0a0a0a;
  --tui-fg: #fafafa;
  --tui-muted: #1c1c1c;
  --tui-muted-fg: #a3a3a3;
  --tui-border: #2a2a2a;
  --tui-primary: #fafafa;
  --tui-primary-fg: #171717;
  --tui-ring: #737373;
  --tui-success: #22c55e;
  --tui-warning: #f59e0b;
  --tui-danger: #ef4444;
}

Needs Tailwind CSS v4. Import the stylesheet once, after Tailwind.