Color Picker

A field that opens a list of colours, each with its hex, and a picker for any other colour and its opacity.

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-2">
    <Label htmlFor="color-picker-preview">Label</Label>
    <ColorPicker defaultValue="#2563eb" id="color-picker-preview" />
  </div>
);

export default Example;

Usage

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";
const [color, setColor] = useState("#2563eb");

<div className="flex flex-col gap-2">
  <Label htmlFor="accent">Label</Label>
  <ColorPicker id="accent" value={color} onValueChange={setColor} />
</div>

Examples

Appearances

appearance paints the field on the kit's one scale: solid, the default, soft on the grey wash, outline with only its line, and transparent with neither until it is hovered.

import {
  ColorPicker,
  type ColorPickerAppearance,
} from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const variants: { appearance: ColorPickerAppearance; label: string }[] = [
  { appearance: "solid", label: "Solid" },
  { appearance: "soft", label: "Soft" },
  { appearance: "outline", label: "Outline" },
  { appearance: "transparent", label: "Transparent" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.appearance}>
        <Label htmlFor={`color-picker-appearance-${variant.appearance}`}>
          {variant.label}
        </Label>
        <ColorPicker
          defaultValue="#2563eb"
          id={`color-picker-appearance-${variant.appearance}`}
          appearance={variant.appearance}
        />
      </div>
    ))}
  </div>
);

export default Example;

Sizes

size sets its height: sm 32px, base 40px (the default) or lg 48px, a Button's height at the same size, the large one written at 16px on a phone.

import {
  ColorPicker,
  type ColorPickerSize,
} from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const variants: { size: ColorPickerSize; label: string }[] = [
  { size: "sm", label: "Small" },
  { size: "base", label: "Base" },
  { size: "lg", label: "Large" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.size}>
        <Label htmlFor={`color-picker-size-${variant.size}`}>
          {variant.label}
        </Label>
        <ColorPicker
          defaultValue="#2563eb"
          id={`color-picker-size-${variant.size}`}
          size={variant.size}
        />
      </div>
    ))}
  </div>
);

export default Example;

Border radius

borderRadius sets its corners on the kit's one scale: none, xs 4px, sm 6px, base 8px (the default), lg 12px, xl 16px, or full to round it.

import {
  ColorPicker,
  type ColorPickerBorderRadius,
} from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const variants: { borderRadius: ColorPickerBorderRadius; label: string }[] = [
  { borderRadius: "none", label: "None" },
  { borderRadius: "xs", label: "xs" },
  { borderRadius: "sm", label: "sm" },
  { borderRadius: "base", label: "base" },
  { borderRadius: "lg", label: "lg" },
  { borderRadius: "xl", label: "xl" },
  { borderRadius: "full", label: "full" },
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    {variants.map((variant) => (
      <div className="flex flex-col gap-2" key={variant.borderRadius}>
        <Label htmlFor={`color-picker-radius-${variant.borderRadius}`}>
          {variant.label}
        </Label>
        <ColorPicker
          defaultValue="#2563eb"
          id={`color-picker-radius-${variant.borderRadius}`}
          borderRadius={variant.borderRadius}
        />
      </div>
    ))}
  </div>
);

export default Example;

Swatches

swatches lists your own colours, each a row with its hex, above the default palette with a line between them: a brand's colours first, every Tailwind shade after.

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const swatches: string[] = [
  "#0f172a",
  "#334155",
  "#64748b",
  "#94a3b8",
  "#e2e8f0",
  "#dc2626",
  "#ea580c",
  "#16a34a",
  "#2563eb",
  "#9333ea",
  "#fecaca",
  "#fed7aa",
  "#bbf7d0",
  "#bfdbfe",
  "#e9d5ff",
];

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-2">
    <Label htmlFor="color-picker-swatches">Label</Label>
    <ColorPicker
      defaultValue="#2563eb"
      id="color-picker-swatches"
      swatches={swatches}
    />
  </div>
);

export default Example;

Clearable

clearable shows an X at the field's end while it holds a colour, which empties it and gives the field the focus back; by default a field has none.

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-clearable-off">Label</Label>
      <ColorPicker defaultValue="#2563eb" id="color-picker-clearable-off" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-clearable">Label</Label>
      <ColorPicker
        clearable
        defaultValue="#2563eb"
        id="color-picker-clearable"
      />
    </div>
  </div>
);

export default Example;

Disabled

disabled dims the field, keeps the palette shut and leaves its colour out of a form, beside one as it is by default.

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-enabled">Label</Label>
      <ColorPicker defaultValue="#2563eb" id="color-picker-enabled" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-disabled">Label</Label>
      <ColorPicker defaultValue="#2563eb" disabled id="color-picker-disabled" />
    </div>
  </div>
);

export default Example;

Invalid

error writes what is wrong under the field, turns its border to the destructive colour and describes it to screen readers, beside one without an error.

import { ColorPicker } from "@produkto/ui/ColorPicker";
import { Label } from "@produkto/ui/Label";

const Example = () => (
  <div className="flex w-full max-w-xs flex-col gap-4">
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-valid">Label</Label>
      <ColorPicker defaultValue="#2563eb" id="color-picker-valid" />
    </div>
    <div className="flex flex-col gap-2">
      <Label htmlFor="color-picker-invalid">Label</Label>
      <ColorPicker
        defaultValue="#2563eb"
        error="Description"
        id="color-picker-invalid"
      />
    </div>
  </div>
);

export default Example;

API Reference

ColorPicker

ColorPickerProps · components/ColorPicker/ColorPicker.types.ts
PropTypeDefault
valuestring-
defaultValuestring-
onValueChange(value: string) => void-
swatchesreadonly string[]-
clearablebooleanfalse
placeholderstring-
appearanceColorPickerAppearance"solid" | "soft" | "outline" | "transparent""solid"
sizeColorPickerSize"sm" | "base" | "lg""base"
borderRadiusColorPickerBorderRadius"none" | "xs" | "sm" | "base" | "lg" | "xl" | "full""base"
errorReactNode-
openboolean-
defaultOpenboolean-
onOpenChange(open: boolean) => void-

It also takes the props of <button>.