Color Picker
ProNewA saturation and brightness square, hue and opacity sliders, a hex field, named swatches, an eyedropper where the browser has one, and a contrast reading against another color. Inline, or as a field that opens it.
Installation
pnpm dlx shadcn@latest add @viewport/color-picker
First time? Add the @viewport registry to components.json. Setup takes one minute.
Usage
The code behind the preview above comes with Truffle Pro.
Props
<ColorPicker>
| Prop | Type | Default |
|---|---|---|
| value* | stringThe color as a hex string: "#3b82f6", or "#3b82f680" with transparency. | — |
| onValueChange* | (value: string) => void | — |
| alpha | booleanAdd a transparency slider. | false |
| swatches | Swatch[]Colors offered below the sliders. Give them names so they can be told apart without seeing them. | [] |
| contrastWith | stringA color to check against, e.g. the page background. The contrast ratio is shown, with whether text would pass. | — |
| label | stringAccessible name of the picker. | "Color" |
| className | string | — |
<ColorField>
| Prop | Type | Default |
|---|---|---|
| size | "sm" | "md" | "md" |
| disabled | boolean | — |
Accessibility
- The square and the sliders are sliders: arrow keys move them, Shift in larger steps.
- Swatches are named buttons with a pressed state.
- The contrast reading says in words whether text would pass.
- The hex field keeps what you type while it is invalid and marks it.