本文へ移動
日本語

仕組み

The picker works in OKLCH so equal steps look equal. The area runs across from grey to the most chroma sRGB can show at that lightness and hue (found by bisection, so every point is a real colour) and up from black to white in OKLab lightness; it is drawn on a small canvas the browser scales smoothly. The hue slider's track samples OKLCH hues at one lightness, so no stretch of it is wider than another. The hex value is the truth: the thumbs are placed from it, except during a drag, when the thumbs lead and the hex follows. A grey keeps the last hue. The field parses #rgb, #rrggbb with or without #, rgb() and oklch(); pasting text that contains a colour (a CSS line, a token file) takes the first colour in it.

向いている場面

  • Choosing any colour: a brand colour, a theme, a chart series, a label people define themselves.
  • When people may bring a value from elsewhere: paste a hex, an rgb() or an oklch(), or pick it from the screen.

向かない場面

  • A short fixed set of colours: use the swatch picker, which shows the result as people choose.

使い方

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

const swatches = [
  { value: '#2563eb', label: 'Cobalt' },
  { value: '#0d9488', label: 'Teal' },
  { value: '#e11d48', label: 'Rose' },
]

export function BrandColour() {
  const [colour, setColour] = useState('#2563eb')
  return (
    <ColourPicker
      label="Brand colour"
      value={colour}
      onChange={setColour}
      swatches={swatches}
      storageKey="brand-colour-recent"
    />
  )
}

プロパティ

プロパティ型既定値説明
value / defaultValue / onChangestring / string / (hex) => void- / '#2563eb' / -The colour as #rrggbb, controlled or uncontrolled. onChange fires as the colour changes, also during a drag; the value is always lower-case #rrggbb whatever format is shown.
onCommit(hex) => void—Called once a change is finished: a drag released, a key step, a swatch or the eyedropper picked, a paste, Enter or leaving the field with a valid colour. Use it for work too heavy to repeat on every pointer move.
swatches(string | { value, label })[][]Preset colours shown under the field; label names one for screen readers and the tooltip. Values that are not hex colours are left out.
storageKey / recentLimitstring / number- / 8Recent colours are added when the popover closes on a new colour. With storageKey they are kept in localStorage under that key; without it they last for the page.
open / defaultOpen / onOpenChangeboolean / boolean / (open) => void- / false / -Whether the popover is open, controlled or uncontrolled. Opening with the trigger moves focus to the area; Escape closes and returns focus to the trigger; a press outside closes.
inlinebooleanfalseShows the panel in the flow, without the trigger, for a side panel or a settings page.
align'start' | 'end''start'Which edge of the trigger the popover lines up with.
format / defaultFormat / onFormatChange'hex' | 'rgb' | 'oklch'- / 'hex' / -The format the field shows and copies. The field accepts any of the three whatever is shown.
label / labelsstring / object'Colour' / Englishlabel names the trigger (followed by the value). labels replaces any built-in text: dialog, area, saturation, lightness, hue, field, format, eyedropper, copy, copied, recent, presets, invalid.
disabledbooleanfalseDisables the trigger.
...restHTMLAttributes—Other attributes go to the root <div>.

アクセシビリティ

  • The trigger is a button named by label and the value, with aria-haspopup="dialog" and aria-expanded; its text is dark or white, whichever reads better on the colour.
  • The area is a role="slider" with aria-roledescription="2D slider" and a value text giving saturation and lightness; Left and Right change saturation, Up and Down lightness, Shift or Page Up and Page Down by 10, Home and End to grey or full colour.
  • The hue is a role="slider" from 0 to 359; arrows move by 1 degree, Shift by 10, Home and End to the ends.
  • Opening moves focus to the area; Escape closes and returns focus to the trigger.
  • The field is labelled, marks a value it cannot read with aria-invalid and a message, and copying is announced in a status region.
  • On touch screens every button, field and slider row is 44px and the thumbs grow; dragging uses pointer capture with touch-action: none.
  • The eyedropper only appears where the browser offers one. Reduced motion removes the open animation and the transitions.

モーション

Open: 200ms ease-out from 6px above and scale 0.96. Thumbs grow to 1.12 on focus and 1.18 while dragged in 100ms. Swatches grow to 1.1 on hover with a spring. The trigger's colour follows in 160ms.

プロジェクトに追加

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

ColourPicker.jsxcolour-picker.csstokens.cssREADME.md

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

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