本文へ移動
日本語

仕組み

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.

向いている場面

  • 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.

向かない場面

  • 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.

使い方

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>}
    />
  )
}

プロパティ

プロパティ型既定値説明
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>.

アクセシビリティ

  • 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.

モーション

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.

プロジェクトに追加

プロンプトを Codex または Claude Code に貼り付けます。エージェントが公開ソースを読み、書き込む内容を示したうえで、既存のコンポーネントの隣に追加します。

SwatchPicker.jsxswatch-picker.csstokens.cssREADME.md

2 つのファイルを一緒に導入します。スタイルは tokens.css の変数を読み、予備の値も持つため、単体でも表示できます。

フォーム入力
「空き時間グリッド」は Pro に含まれます。解除するとプレビューが表示されます。
「図形のブール演算」は Pro に含まれます。解除するとプレビューが表示されます。
「カスケード選択」は Pro に含まれます。解除するとプレビューが表示されます。