ColorPicker
Pick hex colours, with an optional alpha channel, from a popover swatch.
ColorPicker is a client component that renders a react-colorful hex picker inside Reactberry's Popover, so it inherits click-outside, positioning and animation behaviour. Use it for theme, label and brand colour settings.
#3b82f6Import#
import { ColorPicker } from "@reactberry/system/blocks";
Usage#
import { useState } from "react";
import { ColorPicker } from "@reactberry/system/blocks";
export default function LabelColor() {
const [color, setColor] = useState("#3b82f6");
return <ColorPicker color={color} onChange={setColor} alpha={false} />;
}
Examples#
Alpha channel#
alpha defaults to true, which adds an alpha slider and produces #RRGGBBAA values.
#10b98180Custom trigger#
Pass trigger to replace the default swatch, for example inside a settings row. It is still wrapped in the popover's trigger button.
Disabled#
disabled disables the trigger button and dims the default swatch. swatchSize sets the swatch's edge length.
API#
ColorPicker#
| Prop | Type | Default | Description |
|---|---|---|---|
color* | string | — | Current colour value in hex form ( |
onChange* | (color: string) => void | — | Fired whenever the user picks a new colour. |
alpha | boolean | true | Show the alpha-channel slider; defaults to |
trigger | ReactNode | — | Custom trigger. When omitted, a coloured swatch button is rendered. |
placement | string | bottom end | Side of the trigger the picker pops out from, e.g. |
swatchSize | string | 2rem | Edge length of the default swatch trigger. |
disabled | boolean | false | Disable the trigger button, including a custom |
panelProps | any | — | Forwarded to the underlying |
triggerProps | any | — | Forwarded to the underlying |
containerProps | any | — | Forwarded to the underlying |
triggerProps are spread after the defaults ($size="none", variant="ghost", p="0", disabled and aria-label="Pick a color"), so they can override them. panelProps are spread after width="auto" and p="xs".
Accessibility#
- The trigger is a Headless UI
PopoverButtonrendered as a ReactberryButton, witharia-label="Pick a color"by default. Override it withtriggerProps={{ "aria-label": "..." }}. disabledsets thedisabledattribute on the trigger button, including when a customtriggeris used.- The default swatch shows the colour over a checkerboard so transparent values stay visible.
Notes#
- The picker is controlled: it always shows
color, andonChangereceives the new hex string. alpha={false}rendersHexColorPicker; otherwiseHexAlphaColorPickeris used.- Don't put a
buttoninside a customtrigger, because the trigger is already a button. - The panel is positioned absolutely, not in a portal, so it can be clipped by an ancestor with
overflow: hidden.