Color Swatch
A preview of a single color, transparency included.
Usage
ColorSwatch shows one color as a tile. It's the read-only member of the color family: Color Area and Color Slider pick a color, and the swatch reports the one you landed on. All three take the same Color value, so a swatch drops into a picker with no conversion.
color takes a Color or any CSS color string, so '#3b82f6', 'rgba(59, 130, 246, 0.7)' and a parsed object are all equivalent.
The swatch borrows the border and the disabled treatment from the other two controls, so a picker built out of the family reads as one control rather than three.
Inside a Color Picker, color is optional: a bare <ColorSwatch /> previews whatever the picker currently holds.
Examples
Opacity
A translucent color composited straight onto the page is indistinguishable from the lighter opaque color it resolves to. The swatch draws it over a checkerboard instead, the same backdrop a channel="alpha" slider uses, so the transparency is visible.
checkerboard={false} turns it off, which lets the swatch blend into whatever sits behind it instead. The row below is the default, the row under it the same colors without the backdrop.
Shapes
shape="rounded" is the default. Its corner is a percentage of the swatch, so it stays proportional at every size instead of flattening out as the tile grows - the same trick Avatar and Thumbnail use. shape="circle" is the alternative, and pairs well with an inline mention of a color in running text.
Sizing
The swatch is 40px square by default. size runs 3xs 16px through xl 64px: the same ladder Avatar and Thumbnail use, shifted one step down because a swatch is read beside a value rather than looked at. Every shared name keeps its pixel size, so sm is 32px on all three. A plain pixel number works too, and a size-* class on the root overrides both.
Building a picker
An area for two channels, a hue slider for the third, an alpha slider, and a swatch for the result - the same four parts the Color Area and Color Slider pages build. The swatch is the only one of them that shows the alpha as alpha.
Disabled
disabled swaps the color for a flat muted fill inside a dashed outline, dimmed. It's the same treatment the area and the slider use, for a color a palette can't offer right now.
API reference
Every other <span> attribute is forwarded to the root. The root carries data-disabled; the parts are addressable through data-slot (color-swatch, color-swatch-surface).
The color model - Color, parseColor, formatColor, convertColor and the channel helpers - ships from @appica/ui-react/color and is documented on the Color Area page.
Accessibility
- The swatch is a
role="img"with a name, not a decorative div, so a color that carries meaning is reachable rather than invisible. - That name is an English description built from the color -
vivid blue,dark muted green,light gray- because a hex string is announced digit by digit and tells a listener nothing. Below full opacity it ends with the opacity, e.g.vivid blue, 50% opacity. colorNamereplaces that description with the name your palette uses.aria-labelis appended to it rather than replacing it, socolorName="Fire truck red"witharia-label="Background color"announcesFire truck red, Background color.- The hairline border is drawn as an overlay rather than as a border on the root, so it survives a pale color without changing the swatch's box.
forced-color-adjust: nonekeeps the color and the checkerboard intact in forced-colors mode, where the whole point of the component would otherwise be overridden.- A swatch is not a control. When it needs to be clickable, wrap it in a
<button>: that gives you the focus ring, the pressed state and the keyboard handling for free, and keeps the swatch itself presentational.