beta

ColorSwatch

A ColorSwatch displays a preview of a selected color.

installyarn add react-aria-components
version1.2.0
usageimport {ColorSwatch} from 'react-aria-components'

Example#


import {ColorSwatch} from 'react-aria-components';

<ColorSwatch color="#f00" />
import {ColorSwatch} from 'react-aria-components';

<ColorSwatch color="#f00" />
import {ColorSwatch} from 'react-aria-components';

<ColorSwatch color="#f00" />
Show CSS
.react-aria-ColorSwatch {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}
.react-aria-ColorSwatch {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}
.react-aria-ColorSwatch {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
}

Features#


A color swatch may seem simple to build with a <div>, but requires additional semantics and labeling for accessibility.

  • Accessible – Includes a localized color description for screen reader users (e.g. "dark vibrant blue"). Uses the img role with a custom aria-roledescription of "color swatch".
  • International – Accessible color description and role description are translated into over 30 languages.

Anatomy#


A color swatch consists of a color preview, which is exposed to assistive technology with a localized color description.

import {ColorSwatch} from 'react-aria-components';

<ColorSwatch />
import {ColorSwatch} from 'react-aria-components';

<ColorSwatch />
import {ColorSwatch} from 'react-aria-components';

<ColorSwatch />

Reusable wrappers#


If you will use a ColorSwatch in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.

This example wraps ColorSwatch and shows how to use the color render prop to add a checkerboard pattern behind partially transparent colors.

import type {ColorSwatchProps} from 'react-aria-components';

export function MyColorSwatch(props: ColorSwatchProps) {
  return (
    <ColorSwatch 
      {...props}
      style={({color}) => ({
        background: `linear-gradient(${color}, ${color}),
          repeating-conic-gradient(#CCC 0% 25%, white 0% 50%) 50% / 16px 16px`
      })} />
  );
}

<MyColorSwatch color="#f00a" />
import type {ColorSwatchProps} from 'react-aria-components';

export function MyColorSwatch(props: ColorSwatchProps) {
  return (
    <ColorSwatch
      {...props}
      style={({ color }) => ({
        background: `linear-gradient(${color}, ${color}),
          repeating-conic-gradient(#CCC 0% 25%, white 0% 50%) 50% / 16px 16px`
      })}
    />
  );
}

<MyColorSwatch color="#f00a" />
import type {ColorSwatchProps} from 'react-aria-components';

export function MyColorSwatch(
  props: ColorSwatchProps
) {
  return (
    <ColorSwatch
      {...props}
      style={(
        { color }
      ) => ({
        background:
          `linear-gradient(${color}, ${color}),
          repeating-conic-gradient(#CCC 0% 25%, white 0% 50%) 50% / 16px 16px`
      })}
    />
  );
}

<MyColorSwatch color="#f00a" />

Value#


ColorSwatch accepts a value via the color prop. The value should be a color string or Color object.

In the example below, the parseColor function is used to parse the initial color from an HSL string. This is passed to the value prop of a ColorSlider, and the color prop of a ColorSwatch to show a preview of the selected color.

import {ColorSlider, ColorThumb, parseColor, SliderTrack} from 'react-aria-components';

function Example() {
  let [color, setColor] = React.useState(parseColor('hsl(0, 100%, 50%)'));
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
      <ColorSlider value={color} onChange={setColor} channel="hue">
        <SliderTrack>
          <ColorThumb />
        </SliderTrack>
      </ColorSlider>
      <MyColorSwatch color={color} />
    </div>
  );
}
import {
  ColorSlider,
  ColorThumb,
  parseColor,
  SliderTrack
} from 'react-aria-components';

function Example() {
  let [color, setColor] = React.useState(
    parseColor('hsl(0, 100%, 50%)')
  );
  return (
    <div
      style={{
        display: 'flex',
        flexDirection: 'column',
        gap: 8
      }}
    >
      <ColorSlider
        value={color}
        onChange={setColor}
        channel="hue"
      >
        <SliderTrack>
          <ColorThumb />
        </SliderTrack>
      </ColorSlider>
      <MyColorSwatch color={color} />
    </div>
  );
}
import {
  ColorSlider,
  ColorThumb,
  parseColor,
  SliderTrack
} from 'react-aria-components';

function Example() {
  let [color, setColor] =
    React.useState(
      parseColor(
        'hsl(0, 100%, 50%)'
      )
    );
  return (
    <div
      style={{
        display: 'flex',
        flexDirection:
          'column',
        gap: 8
      }}
    >
      <ColorSlider
        value={color}
        onChange={setColor}
        channel="hue"
      >
        <SliderTrack>
          <ColorThumb />
        </SliderTrack>
      </ColorSlider>
      <MyColorSwatch
        color={color}
      />
    </div>
  );
}

Labeling#


By default, ColorSwatch includes a localized color description for screen reader users (e.g. "dark vibrant blue") as an aria-label. If you have a more specific color name (e.g. Pantone colors), the automatically generated color description can be overridden via the colorName prop. An additional label describing the context of the color swatch (e.g. "Background color") can also be provided via the aria-label or aria-labelledby props.

In the example below, the full accessible name of the color swatch will be "Fire truck red, Background color".

<MyColorSwatch
  color="#f00"
  aria-label="Background color"
  colorName="Fire truck red"
/>
<MyColorSwatch
  color="#f00"
  aria-label="Background color"
  colorName="Fire truck red"
/>
<MyColorSwatch
  color="#f00"
  aria-label="Background color"
  colorName="Fire truck red"
/>

Props#


NameTypeDescription
colorstringColornullThe color value to display in the swatch.
colorNamestring

A localized accessible name for the color. By default, a description is generated from the color value, but this can be overridden if you have a more specific color name (e.g. Pantone colors).

classNamestring( (values: ColorSwatchRenderProps{
defaultClassName: stringundefined
} )) => string
The CSS className for the element. A function may be provided to compute the class based on component state.
styleCSSProperties( (values: ColorSwatchRenderProps{
defaultStyle: CSSProperties
} )) => CSSProperties
The inline style for the element. A function may be provided to compute the style based on component state.
Layout
NameTypeDescription
slotstringnull

A slot name for the component. Slots allow the component to receive props from a parent component. An explicit null value indicates that the local props completely override all props received from a parent.

Accessibility
NameTypeDescription
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.

Styling#


React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName naming convention.

.react-aria-ColorSwatch {
  /* ... */
}
.react-aria-ColorSwatch {
  /* ... */
}
.react-aria-ColorSwatch {
  /* ... */
}

A custom className can also be specified on any component. This overrides the default className provided by React Aria with your own.

<ColorSwatch className="my-color-swatch">
  {/* ... */}
</ColorSwatch>
<ColorSwatch className="my-color-swatch">
  {/* ... */}
</ColorSwatch>
<ColorSwatch className="my-color-swatch">
  {/* ... */}
</ColorSwatch>

The className and style props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.

<ColorSwatch style={({ color }) => ({ background: color.toString('css') })} />
<ColorSwatch
  style={({ color }) => ({
    background: color.toString('css')
  })}
/>
<ColorSwatch
  style={(
    { color }
  ) => ({
    background: color
      .toString('css')
  })}
/>

The render props for ColorSwatch are documented below.

NameDescription
colorThe color of the swatch.

Advanced customization#


Contexts#

All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).

ComponentContextPropsRef
ColorSwatchColorSwatchContextColorSwatchPropsHTMLDivElement
import {ColorSwatchContext} from 'react-aria-components';

<ColorSwatchContext.Provider value={{color: '#ff0'}}>
  <ColorSwatch />
</ColorSwatchContext.Provider>
import {ColorSwatchContext} from 'react-aria-components';

<ColorSwatchContext.Provider value={{color: '#ff0'}}>
  <ColorSwatch />
</ColorSwatchContext.Provider>
import {ColorSwatchContext} from 'react-aria-components';

<ColorSwatchContext.Provider
  value={{
    color: '#ff0'
  }}
>
  <ColorSwatch />
</ColorSwatchContext.Provider>

Hooks#

If you need to customize things even further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useColorSwatch for more details.