Beta Preview

ColorSwatchPicker

A ColorSwatchPicker displays a list of color swatches and allows a user to select one of them.

size 
density 
rounding 
import {ColorSwatchPicker, ColorSwatch} from '@react-spectrum/s2';

<ColorSwatchPicker defaultValue="#f00">
  <ColorSwatch color="#f00" />
  <ColorSwatch color="#0f0" />
  <ColorSwatch color="#00f" />
  <ColorSwatch color="#ff0" />
  <ColorSwatch color="#f0f" />
  <ColorSwatch color="#0ff" />
</ColorSwatchPicker>

Value

Use the value or defaultValue prop to set the selected color, and onChange to handle selection changes. The value may be a string or object, parsed using the function.

Selected color: rgb(225, 29, 72)
import {ColorSwatchPicker, ColorSwatch, parseColor} from '@react-spectrum/s2';
import {useState} from 'react';

function Example() {
  let [value, setValue] = useState(parseColor('#e11d48'));

  return (
    <>
      <ColorSwatchPicker value={value} onChange={setValue}>
        <ColorSwatch color="#e11d48" />
        <ColorSwatch color="#f97316" />
        <ColorSwatch color="#eab308" />
        <ColorSwatch color="#22c55e" />
        <ColorSwatch color="#3b82f6" />
        <ColorSwatch color="#8b5cf6" />
        <ColorSwatch color="#ec4899" />
      </ColorSwatchPicker>
      <pre style={{fontSize: 12}}>Selected color: {value.toString('rgb')}</pre>
    </>
  );
}

API

<ColorSwatchPicker>
  <ColorSwatch />
</ColorSwatchPicker>

ColorSwatchPicker

NameTypeDefault
childrenReactNodeDefault:
The ColorSwatches within the ColorSwatchPicker.
density'compact''regular''spacious'Default: 'regular'
The amount of padding between the swatches.
size'XS''S''M''L'Default: 'M'
The size of the color swatches.
rounding'none''default''full'Default: 'none'
The corner rounding of the color swatches.
stylesDefault:
Spectrum-defined styles, returned by the style() macro.
valuestringDefault:
The current value (controlled).
defaultValuestringDefault:
The default value (uncontrolled).
onChange(value: ) => voidDefault:
Handler that is called when the value changes.