Ir al contenido
Español

Cómo funciona

The preview holds one layer: the whole preview drawn in the settled colour. A new pick adds a second layer, the whole preview drawn again in the new colour and its ink, clipped to a circle centred on the pressed swatch (measured before paint, relative to the preview) that grows to the farthest corner. Because each layer carries its own copy of the content, text changes colour exactly where the edge passes, and the old colour stays underneath until it is covered. When a layer finishes it becomes the settled one and the layers under it are dropped, so fast picks stack without a jump. The ring is one element at the top left of the grid, moved by transform to the chosen column and row with an overshoot, so it never depends on layout measured in the browser.

Úsalo para

  • Choosing a colour from a short, fixed set: a card, a project, a label, a calendar, a theme.
  • When people should see the result on the real object while they choose.

Busca otra opción cuando

  • Any colour at all: use a full colour picker with a field and a hue control.
  • More than about sixteen choices: the row becomes hard to scan; group them or use a palette grid with names.

Uso

import { useState } from 'react'
import { SwatchPicker } from './SwatchPicker.jsx'

const colors = [
  { value: '#4f46e5', label: 'Indigo' },
  { value: '#0f766e', label: 'Lagoon' },
  { value: '#f5b301', label: 'Saffron' },
  { value: '#dc3d43', label: 'Coral' },
]

export function ProjectColour({ name }) {
  const [color, setColor] = useState('#4f46e5')
  return (
    <SwatchPicker
      label="Project colour"
      colors={colors}
      value={color}
      onChange={setColor}
      ratio={3}
      // Text inside inherits the ink that reads best on the chosen colour
      renderPreview={() => <strong style={{ display: 'block', padding: 16 }}>{name}</strong>}
    />
  )
}

Propiedades

PropiedadTipoPor defectoQué hace
colors{ value, label, ink? }[]—The swatches in order. value is any CSS colour; label names it for screen readers; ink sets the preview text colour, which otherwise is dark or white by contrast (worked out for #rgb and #rrggbb values, white for anything else).
value / defaultValue / onChangestring / string / (value, color) => void- / first colour / -The chosen colour's value, controlled or uncontrolled. onChange also receives the colour object.
renderPreview(color, { index, ink }) => ReactNode—Draws the preview for one colour. It is called for the settled colour and again for each colour still growing over it; those copies are aria-hidden and inert, so keep controls out of the preview. ink is the text colour chosen for that colour. Without it (and without children) only the swatches show; children may be a node or the same function.
rationumber1.586Aspect ratio of the preview; the default is a payment card.
columnsnumbercolors.lengthSwatches per row. With several rows, Up and Down move by a row.
size / gapnumber / number32 / 12Swatch diameter and the space between swatches, in pixels. The hit area of each swatch reaches halfway into the gap.
labelstring'Colour'Accessible name of the radio group.
...restHTMLAttributes—Other attributes go to the root <div>.

Accesibilidad

  • The swatches are role="radio" buttons in a role="radiogroup" named by label, each named by its colour's label; only the chosen one is in the tab order.
  • Arrow keys select the next or previous colour and move focus with it (Up and Down move by a row when there are several), Home and End go to the ends.
  • Every swatch has a hairline edge, so white and pale colours stay visible on any surface, and its hit area reaches halfway into the gap.
  • Keyboard focus on the chosen swatch turns the ring to the accent colour with a halo; on another swatch it draws an outline.
  • The preview text uses dark or white by contrast, or a colour's own ink; the copies drawn while a colour spreads are aria-hidden and inert, so the content is read once.
  • Reduced motion replaces the growing circle with a short fade and moves the ring at once.

Movimiento

Flood: clip-path circle from 0 to the farthest corner in 640ms cubic-bezier(0.3, 0.6, 0.1, 1), the text inside changing with it. Ring: transform 560ms cubic-bezier(0.34, 1.3, 0.5, 1). Swatches grow to 1.1 on hover and press to 0.88 with a spring.

Añádelo a tu proyecto

Copia la instrucción en Codex o Claude Code. El agente lee el código publicado, te muestra lo que va a escribir y añade el componente junto a los tuyos.

SwatchPicker.jsxswatch-picker.csstokens.cssREADME.md

Los dos archivos se instalan juntos. La hoja de estilos lee las variables de tokens.css y lleva valores de reserva, así que también funciona sola.

Formularios
Cuadrícula de disponibilidad viene con Pro. La vista previa aparece al desbloquearlo.
Operaciones booleanas viene con Pro. La vista previa aparece al desbloquearlo.
Selector en cascada viene con Pro. La vista previa aparece al desbloquearlo.