Color Picker

A panel to pick a color. Drag in the square for the saturation and the brightness, use the bar for the hue, or type the hex code. For a field that opens this panel in a popover, see Color Input.

General

Start with a color using initialValue, and name the group with aria-label.

With swatches

swatches adds colors to pick with one click, like the ones a team already uses.

Controlled

Use value and onChange to keep the color in your own state, for example to show a preview or to reset it.

Disabled

A disabled picker shows the color and cannot change it.

APIs

ColorPicker.Props

AttributeDescriptionTypeAccepted valuesDefault
valuethe color, to control the componentstring'#rrggbb', '#rgb'-
initialValuethe color on the first renderstring'#rrggbb', '#rgb'#000000
onChangecalled with the color as #rrggbb in lowercase(hex: string) => void--
swatchescolors to pick with one clickstring[]--
hexInputshow the field for the hex codeboolean-true
disableddisable the componentboolean-false
panelLabelname of the square, for screen readersstring-Saturation and brightness
hueLabelname of the hue barstring-Hue
hexLabelname of the hex fieldstring-Hex color
...native props, passed to the root divHTMLAttributes'aria-label', 'id', ...-

Hex field

The field changes the color as soon as it holds six digits, with or without the #. A short code like #abc is read when the field loses the focus, and becomes #aabbcc. Any other text is dropped on blur, and the field goes back to the current color.

Ref

The ref points at the root div.

Accessibility

The square is a slider named by panelLabel, whose value reads like "40% saturation, 80% brightness". With the focus on it, Left and Right change the saturation and Up and Down the brightness, by 1%, or by 10% with Shift. The hue bar is a native range input, and the swatches are buttons named by their code, with aria-pressed on the one in use. The color is never conveyed by the swatch alone, because the hex code is also a text.

Edit this page on GitHub