Skip to content
Primitives

Color Picker

Swatch palette in a popover, plus a native picker and hex input for custom colors.

Motionpopover scale/opacity on tween.quick

Install

Install Kineto and follow the theme setup once in your app.

bash
npm install kineto
tsx
import { ColorPicker } from "kineto/color-picker";

Preview

tsx
"use client";

import * as React from "react";
import { Check } from "lucide-react";

import { cn } from "@/lib/cn";
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover";

const DEFAULT_SWATCHES = [
  "#ef4444", "#f97316", "#f59e0b", "#84cc16", "#22c55e", "#10b981",
  "#06b6d4", "#3b82f6", "#6366f1", "#8b5cf6", "#ec4899", "#0a0a0a",
];

export interface ColorPickerProps {
  value?: string;
  defaultValue?: string;
  onValueChange?: (color: string) => void;
  swatches?: string[];
  className?: string;
}

export function ColorPicker({
  value,
  defaultValue = "#f97316",
  onValueChange,
  swatches = DEFAULT_SWATCHES,
  className,
}: ColorPickerProps) {
  const [internal, setInternal] = React.useState(defaultValue);
  const current = value ?? internal;

  const set = (color: string) => {
    if (value === undefined) setInternal(color);
    onValueChange?.(color);
  };

  return (
    <Popover>
      <PopoverTrigger asChild>
        <button
          type="button"
          className={cn(
            "inline-flex items-center gap-2 rounded-lg border border-input bg-card px-3 py-2 text-sm shadow-xs transition-colors hover:bg-accent focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring",
            className
          )}
        >
          <span
            className="size-5 rounded-md border border-border"
            style={{ background: current }}
          />
          <span className="font-mono text-xs uppercase">{current}</span>
        </button>
      </PopoverTrigger>
      <PopoverContent className="w-56" align="start">
        <div className="grid grid-cols-6 gap-2">
          {swatches.map((c) => (
            <button
              key={c}
              type="button"
              aria-label={c}
              onClick={() => set(c)}
              style={{ background: c }}
              className="flex size-7 items-center justify-center rounded-md border border-border/50 transition-transform hover:scale-110 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
            >
              {current.toLowerCase() === c.toLowerCase() ? (
                <Check className="size-4 text-white drop-shadow" strokeWidth={3} />
              ) : null}
            </button>
          ))}
        </div>
        <div className="mt-3 flex items-center gap-2">
          <input
            type="color"
            value={current}
            onChange={(e) => set(e.target.value)}
            aria-label="Custom color"
            className="size-8 cursor-pointer rounded-md border border-border bg-transparent p-0.5"
          />
          <input
            value={current}
            onChange={(e) => set(e.target.value)}
            className="h-8 flex-1 rounded-md border border-input bg-card px-2 font-mono text-xs uppercase outline-none focus-visible:ring-2 focus-visible:ring-ring/40"
          />
        </div>
      </PopoverContent>
    </Popover>
  );
}

src/components/ui/color-picker.tsx

Props

PropTypeDefaultDescription
value / defaultValuestring—Hex color.
swatchesstring[]—Preset palette.