Color picker
A color picker with a saturation and brightness area, hue and alpha sliders, hex, RGB and HSL inputs, an eyedropper, preset swatches and recent colors. Keyboard and touch friendly, controlled or uncontrolled, no extra packages.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker.json5 examples below; each installs the component with it. They wear whichever DressCode theme you install.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker-popover.jsoncomponents/dresscode/color-picker-popover.tsx
"use client"
// A settings form where the brand color is a swatch button that opens the full picker in a popover.
// The form owns the value and the recent colors, so both survive the popover closing.
import * as React from "react"
import { AlertTriangleIcon, CheckIcon, ChevronDownIcon, RotateCcwIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import {
ColorPicker,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerFormatSelect,
ColorPickerHue,
ColorPickerInput,
ColorPickerRecent,
ColorPickerSwatch,
ColorPickerSwatches,
getContrastRatio,
getReadableTextColor,
} from "@/components/ui/color-picker"
const DEFAULT_COLOR = "#4f46e5"
const PRESETS = [
{ value: "#4f46e5", label: "Indigo" },
{ value: "#2563eb", label: "Ocean" },
{ value: "#0e7490", label: "Lagoon" },
{ value: "#047857", label: "Fern" },
{ value: "#b45309", label: "Amber" },
{ value: "#c2410c", label: "Tangerine" },
{ value: "#be123c", label: "Rose" },
{ value: "#7e22ce", label: "Orchid" },
{ value: "#1f2937", label: "Ink" },
]
export default function ColorPickerPopover() {
const [name, setName] = React.useState("Fernwood Studio")
const [color, setColor] = React.useState(DEFAULT_COLOR)
const [saved, setSaved] = React.useState(DEFAULT_COLOR)
const [recent, setRecent] = React.useState(["#0f766e", "#9d174d", "#ca8a04"])
const [justSaved, setJustSaved] = React.useState(false)
const text = getReadableTextColor(color)
const ratio = getContrastRatio(text, color)
const dirty = color !== saved
return (
<Card className="mx-auto w-full max-w-2xl">
<CardHeader>
<CardTitle>Workspace branding</CardTitle>
<CardDescription>Shown on invoices, reminder emails and your client portal.</CardDescription>
</CardHeader>
<CardContent className="grid gap-6 sm:grid-cols-[1fr_15rem]">
<div className="grid content-start gap-5">
<div className="grid gap-2">
<Label htmlFor="cp-workspace">Workspace name</Label>
<Input id="cp-workspace" value={name} onChange={(e) => setName(e.target.value)} />
</div>
<div className="grid gap-2">
<Label id="cp-brand-label">Brand color</Label>
<div className="flex items-center gap-2">
<Popover>
<PopoverTrigger
aria-labelledby="cp-brand-label cp-brand-value"
render={<Button variant="outline" className="w-40 justify-start gap-2 pr-2 pl-1.5 font-normal" />}
>
<ColorPickerSwatch color={color} />
<span id="cp-brand-value" className="tabular-nums">
{color}
</span>
<ChevronDownIcon className="ml-auto text-muted-foreground" />
</PopoverTrigger>
<PopoverContent align="start" className="w-72 gap-0">
<ColorPicker
aria-label="Brand color"
value={color}
onValueChange={setColor}
recentColors={recent}
onRecentColorsChange={setRecent}
maxRecentColors={9}
>
<ColorPickerArea />
<div className="flex items-center gap-2">
<ColorPickerEyeDropper />
<ColorPickerHue />
</div>
<div className="flex items-start gap-1.5">
<ColorPickerFormatSelect />
<ColorPickerInput />
</div>
<ColorPickerSwatches label="Presets" colors={PRESETS} />
<ColorPickerRecent />
</ColorPicker>
</PopoverContent>
</Popover>
{color !== DEFAULT_COLOR ? (
<Button variant="ghost" size="sm" onClick={() => setColor(DEFAULT_COLOR)}>
<RotateCcwIcon />
Reset
</Button>
) : null}
</div>
<p className="text-sm text-muted-foreground">Used for buttons, links and the header of every email.</p>
{ratio < 4.5 ? (
<p className="flex items-start gap-1.5 text-sm text-destructive" role="status">
<AlertTriangleIcon className="mt-0.5 size-4 shrink-0" />
Text on this color is hard to read ({ratio.toFixed(1)}:1). Try a deeper shade.
</p>
) : null}
</div>
</div>
<div className="grid content-start gap-2">
<p className="text-sm font-medium">Email preview</p>
<div className="overflow-hidden rounded-lg border bg-background text-xs" aria-label="Email preview" role="img">
<div className="flex items-center gap-2 px-3 py-2.5 font-medium" style={{ background: color, color: text }}>
<span className="flex size-5 items-center justify-center rounded-sm text-[0.625rem] font-semibold" style={{ background: text, color }}>
{name.trim().charAt(0).toUpperCase() || "F"}
</span>
<span className="truncate">{name.trim() || "Your workspace"}</span>
</div>
<div className="grid gap-2 p-3">
<p className="font-medium text-foreground">Invoice INV-1043 is ready</p>
<p className="text-muted-foreground">$4,200.00 for November's design work, due Dec 8.</p>
<span className="mt-1 inline-flex h-7 w-fit items-center rounded-md px-3 font-medium" style={{ background: color, color: text }}>
Pay invoice
</span>
</div>
</div>
</div>
</CardContent>
<CardFooter className="justify-end gap-2">
{justSaved ? (
<span className="mr-auto flex items-center gap-1.5 text-sm text-muted-foreground" role="status">
<CheckIcon className="size-4" />
Saved
</span>
) : null}
<Button variant="outline" disabled={!dirty} onClick={() => setColor(saved)}>
Discard
</Button>
<Button
disabled={!dirty}
onClick={() => {
setSaved(color)
setJustSaved(true)
window.setTimeout(() => setJustSaved(false), 2000)
}}
>
Save changes
</Button>
</CardFooter>
</Card>
)
}npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker-inline.jsoncomponents/dresscode/color-picker-inline.tsx
"use client"
// The picker as an always-open panel beside a live preview: a shared calendar's color, shown on its
// events, its all-day banner and its sidebar entry as you drag.
import * as React from "react"
import { CheckIcon, UsersIcon } from "lucide-react"
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"
import {
ColorPicker,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerFormatSelect,
ColorPickerHue,
ColorPickerInput,
ColorPickerRecent,
ColorPickerSwatches,
getReadableTextColor,
} from "@/components/ui/color-picker"
const PRESETS = [
{ value: "#d50000", label: "Tomato" },
{ value: "#f4511e", label: "Tangerine" },
{ value: "#f6bf26", label: "Banana" },
{ value: "#33b679", label: "Sage" },
{ value: "#0b8043", label: "Basil" },
{ value: "#039be5", label: "Peacock" },
{ value: "#3f51b5", label: "Blueberry" },
{ value: "#8e24aa", label: "Grape" },
{ value: "#616161", label: "Graphite" },
]
const EVENTS = [
{ time: "09:30 – 10:00", title: "Launch readiness check-in", where: "Room 4B" },
{ time: "13:00 – 14:00", title: "Release notes review", where: "Video call" },
{ time: "16:00 – 16:30", title: "Go or no-go for version 4.2", where: "Room 2A" },
]
export default function ColorPickerInline() {
const [color, setColor] = React.useState("#039be5")
const tint = `color-mix(in oklch, ${color} 16%, var(--card))`
const onSolid = getReadableTextColor(color)
return (
<div className="grid w-full max-w-3xl gap-4 md:grid-cols-[18rem_1fr]">
<section aria-labelledby="cp-inline-title" className="flex flex-col gap-3 rounded-xl border bg-card p-3 text-card-foreground">
<div className="flex items-baseline justify-between gap-2">
<h2 id="cp-inline-title" className="text-sm font-medium">
Calendar color
</h2>
<span className="text-xs text-muted-foreground tabular-nums">{color}</span>
</div>
<ColorPicker aria-label="Calendar color" value={color} onValueChange={setColor} defaultRecentColors={["#e67c73", "#7986cb"]} maxRecentColors={9}>
<ColorPickerArea className="h-36" />
<div className="flex items-center gap-2">
<ColorPickerEyeDropper />
<ColorPickerHue />
</div>
<div className="flex items-start gap-1.5">
<ColorPickerFormatSelect />
<ColorPickerInput />
</div>
<ColorPickerSwatches label="Calendar colors" colors={PRESETS} columns={9} />
<ColorPickerRecent label="Custom" emptyText="Drag in the square to make your own." columns={9} />
</ColorPicker>
</section>
<Card className="min-w-0">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<span className="size-3 shrink-0 rounded-full" style={{ background: color }} />
Product launches
</CardTitle>
<CardDescription className="flex items-center gap-1.5">
<UsersIcon className="size-3.5" />
Shared with 14 people
</CardDescription>
</CardHeader>
<CardContent className="grid gap-4">
<div>
<p className="mb-2 text-xs font-medium text-muted-foreground">Tuesday, December 2</p>
<div className="rounded-md px-2.5 py-1.5 text-sm font-medium" style={{ background: color, color: onSolid }}>
Code freeze starts · all day
</div>
</div>
<ul className="grid gap-2">
{EVENTS.map((e) => (
<li key={e.title} className="rounded-md border-l-4 py-2 pr-3 pl-3" style={{ background: tint, borderColor: color }}>
<p className="text-sm font-medium">{e.title}</p>
<p className="text-xs text-muted-foreground tabular-nums">
{e.time} · {e.where}
</p>
</li>
))}
</ul>
<div className="grid gap-1.5 border-t pt-4">
<p className="text-xs font-medium text-muted-foreground">My calendars</p>
<div className="flex items-center gap-2.5 text-sm">
<span className="flex size-4 items-center justify-center rounded-[4px]" style={{ background: color, color: onSolid }}>
<CheckIcon className="size-3" strokeWidth={3} />
</span>
Product launches
</div>
<div className="flex items-center gap-2.5 text-sm text-muted-foreground">
<span className="size-4 rounded-[4px] border border-input" />
Team holidays
</div>
</div>
</CardContent>
</Card>
</div>
)
}npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker-swatches.jsoncomponents/dresscode/color-picker-swatches.tsx
"use client"
// A compact grid of preset swatches for a project label. "Custom…" opens the full picker; colors
// made there are kept in a row of their own under the presets.
import * as React from "react"
import { PlusIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import { Input } from "@/components/ui/input"
import { Label } from "@/components/ui/label"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import {
ColorPicker,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerHue,
ColorPickerInput,
ColorPickerRecent,
ColorPickerSwatches,
getReadableTextColor,
} from "@/components/ui/color-picker"
const PRESETS = [
{ value: "#ef4444", label: "Red" },
{ value: "#f97316", label: "Orange" },
{ value: "#f59e0b", label: "Amber" },
{ value: "#eab308", label: "Yellow" },
{ value: "#84cc16", label: "Lime" },
{ value: "#22c55e", label: "Green" },
{ value: "#14b8a6", label: "Teal" },
{ value: "#06b6d4", label: "Cyan" },
{ value: "#3b82f6", label: "Blue" },
{ value: "#6366f1", label: "Indigo" },
{ value: "#8b5cf6", label: "Violet" },
{ value: "#d946ef", label: "Fuchsia" },
{ value: "#ec4899", label: "Pink" },
{ value: "#78716c", label: "Stone" },
{ value: "#64748b", label: "Slate" },
{ value: "#171717", label: "Black" },
]
export default function ColorPickerSwatchesExample() {
const [name, setName] = React.useState("Needs design review")
const [color, setColor] = React.useState("#8b5cf6")
const [open, setOpen] = React.useState(false)
const label = name.trim() || "Untitled label"
return (
<Card className="w-full max-w-sm">
<CardHeader>
<CardTitle>Edit label</CardTitle>
<CardDescription>Labels group issues across every project in the workspace.</CardDescription>
</CardHeader>
<CardContent className="grid gap-5">
<div className="grid gap-2">
<Label htmlFor="cp-label-name">Name</Label>
<Input id="cp-label-name" value={name} onChange={(e) => setName(e.target.value)} />
</div>
<ColorPicker aria-label="Label color" value={color} onValueChange={setColor} defaultRecentColors={["#0f766e", "#b45309"]} maxRecentColors={8} className="gap-4">
<ColorPickerSwatches label="Color" colors={PRESETS} columns={8} />
<div className="grid gap-2">
<div className="flex items-center justify-between gap-2">
<p id="cp-custom-colors" className="text-xs font-medium text-muted-foreground">
Custom colors
</p>
<Popover open={open} onOpenChange={setOpen}>
<PopoverTrigger render={<Button variant="outline" size="xs" />}>
<PlusIcon />
Custom…
</PopoverTrigger>
<PopoverContent align="end" className="w-64 gap-3" aria-label="Custom label color">
<ColorPickerArea className="h-36" />
<div className="flex items-center gap-2">
<ColorPickerEyeDropper size="icon-sm" />
<ColorPickerHue />
</div>
<ColorPickerInput />
<Button size="sm" onClick={() => setOpen(false)}>
Done
</Button>
</PopoverContent>
</Popover>
</div>
<ColorPickerRecent label="" aria-labelledby="cp-custom-colors" columns={8} emptyText="Colors you make with Custom… show up here." />
</div>
</ColorPicker>
<div className="grid gap-2 rounded-lg border p-3">
<p className="text-xs font-medium text-muted-foreground">Preview</p>
<div className="flex min-w-0 items-center gap-2 text-sm">
<span className="shrink-0 text-muted-foreground tabular-nums">FW-482</span>
<span className="truncate">Checkout page spacing on small screens</span>
</div>
<div className="flex flex-wrap gap-1.5">
<span className="inline-flex h-6 max-w-full items-center truncate rounded-full px-2.5 text-xs font-medium" style={{ background: color, color: getReadableTextColor(color) }}>
{label}
</span>
<span
className="inline-flex h-6 max-w-full items-center gap-1.5 truncate rounded-full border px-2.5 text-xs font-medium"
style={{ background: `color-mix(in oklch, ${color} 14%, var(--card))`, borderColor: `color-mix(in oklch, ${color} 40%, var(--card))` }}
>
<span className="size-2 shrink-0 rounded-full" style={{ background: color }} />
{label}
</span>
</div>
</div>
</CardContent>
<CardFooter className="justify-end gap-2">
<Button variant="outline">Cancel</Button>
<Button>Save label</Button>
</CardFooter>
</Card>
)
}npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker-alpha.jsoncomponents/dresscode/color-picker-alpha.tsx
"use client"
// Picking a translucent overlay for a hero image: the alpha slider and the A field set the opacity,
// and the value is written out as rgba() and as an 8-digit hex, ready to copy.
import * as React from "react"
import { CheckIcon, CopyIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import {
ColorPicker,
ColorPickerAlpha,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerFormatSelect,
ColorPickerHue,
ColorPickerInput,
ColorPickerSwatches,
getColorDetails,
} from "@/components/ui/color-picker"
const PRESETS = [
{ value: "#0f172a8c", label: "Night, 55%" },
{ value: "#00000066", label: "Black, 40%" },
{ value: "#ffffff4d", label: "Mist, 30%" },
{ value: "#7c2d1299", label: "Ember, 60%" },
{ value: "#1e3a8a80", label: "Deep blue, 50%" },
{ value: "#065f4673", label: "Pine, 45%" },
{ value: "#581c8766", label: "Plum, 40%" },
{ value: "#f59e0b40", label: "Golden hour, 25%" },
]
function CopyRow({ label, value }: { label: string; value: string }) {
const [copied, setCopied] = React.useState(false)
const copy = async () => {
try {
await navigator.clipboard.writeText(value)
setCopied(true)
window.setTimeout(() => setCopied(false), 1500)
} catch {
// Clipboard blocked: the value is still selectable.
}
}
return (
<div className="flex h-8 items-center gap-2 rounded-md border pr-1 pl-2.5 text-xs">
<span className="w-9 shrink-0 text-muted-foreground">{label}</span>
<span className="min-w-0 flex-1 truncate tabular-nums select-all">{value}</span>
<Button variant="ghost" size="icon-xs" aria-label={copied ? `Copied ${label}` : `Copy ${label} value`} onClick={copy}>
{copied ? <CheckIcon /> : <CopyIcon />}
</Button>
</div>
)
}
export default function ColorPickerAlphaExample() {
const [overlay, setOverlay] = React.useState("#0f172a8c")
const details = getColorDetails(overlay)
return (
<div className="grid w-full max-w-3xl gap-4 md:grid-cols-[1fr_18rem]">
<figure className="flex min-w-0 flex-col gap-2">
<div
role="img"
aria-label={`Hero image with a ${Math.round(details.alpha * 100)}% overlay`}
className="relative aspect-[4/3] overflow-hidden rounded-xl sm:aspect-[16/11]"
style={{ background: "linear-gradient(180deg, #fbd3a0 0%, #f4a77c 40%, #c8716c 64%, #6d4f73 100%)" }}
>
<div className="absolute top-[22%] left-[58%] size-14 rounded-full" style={{ background: "#fff3d1", boxShadow: "0 0 70px 24px rgb(255 226 168 / 0.65)" }} />
<div className="absolute -bottom-[28%] -left-[12%] h-[62%] w-[78%] rounded-[50%]" style={{ background: "#4a6b58" }} />
<div className="absolute -right-[18%] -bottom-[34%] h-[66%] w-[86%] rounded-[50%]" style={{ background: "#2c463b" }} />
<div className="absolute inset-x-0 bottom-0 h-[18%]" style={{ background: "linear-gradient(180deg, #5d7f9c, #3c5670)" }} />
<div className="absolute inset-0" style={{ background: overlay }} />
<div className="absolute inset-x-0 bottom-0 grid gap-1 p-5" style={{ color: "#ffffff" }}>
<p className="text-xs font-medium opacity-90">Oct 18 – 20 · Alpine lake lodge</p>
<p className="text-2xl leading-tight font-semibold text-balance">Autumn team retreat</p>
<span className="mt-2 inline-flex h-8 w-fit items-center rounded-md px-3 text-sm font-medium" style={{ background: "#ffffff", color: "#1c1917" }}>
Save your spot
</span>
</div>
</div>
<figcaption className="text-xs text-muted-foreground">The overlay keeps the headline readable on any photo.</figcaption>
</figure>
<section aria-labelledby="cp-alpha-title" className="flex flex-col gap-3 rounded-xl border bg-card p-3 text-card-foreground">
<div className="flex items-baseline justify-between gap-2">
<h2 id="cp-alpha-title" className="text-sm font-medium">
Image overlay
</h2>
<span className="text-xs text-muted-foreground tabular-nums">{Math.round(details.alpha * 100)}% opacity</span>
</div>
<ColorPicker aria-label="Overlay color" alpha value={overlay} onValueChange={setOverlay}>
<ColorPickerArea className="h-36" />
<div className="flex items-center gap-2">
<ColorPickerEyeDropper />
<div className="flex min-w-0 flex-1 flex-col gap-2">
<ColorPickerHue />
<ColorPickerAlpha />
</div>
</div>
<div className="flex items-start gap-1.5">
<ColorPickerFormatSelect />
<ColorPickerInput />
</div>
<ColorPickerSwatches label="Overlays" colors={PRESETS} />
</ColorPicker>
<div className="grid gap-1.5 border-t pt-3">
<CopyRow label={details.alpha < 1 ? "rgba" : "rgb"} value={details.rgb} />
<CopyRow label="hex8" value={details.hex8} />
</div>
</section>
</div>
)
}Theme editor
Three pickers restyle a button, a badge and a card, with a contrast check for each text pair.
npx shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker-theme-editor.jsoncomponents/dresscode/color-picker-theme-editor.tsx
"use client"
// A small theme editor: three color pickers (primary, accent, background) restyle a preview of a
// button, a badge and a card. Text colors are picked for contrast, and each text pair shows its
// WCAG contrast ratio with a pass or fail for normal-size text.
import * as React from "react"
import { CheckIcon, ChevronDownIcon, CopyIcon, RotateCcwIcon, XIcon } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import {
ColorPicker,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerFormatSelect,
ColorPickerHue,
ColorPickerInput,
ColorPickerRecent,
ColorPickerSwatch,
ColorPickerSwatches,
getContrastRatio,
getReadableTextColor,
type SwatchColor,
} from "@/components/ui/color-picker"
const DEFAULTS = { primary: "#2563eb", accent: "#fde68a", background: "#ffffff" }
type Role = keyof typeof DEFAULTS
const FIELDS: { role: Role; label: string; hint: string; presets: SwatchColor[] }[] = [
{
role: "primary",
label: "Primary",
hint: "Buttons, links and checks",
presets: [
{ value: "#2563eb", label: "Ocean" },
{ value: "#4f46e5", label: "Indigo" },
{ value: "#7c3aed", label: "Violet" },
{ value: "#db2777", label: "Rose" },
{ value: "#dc2626", label: "Tomato" },
{ value: "#ea580c", label: "Tangerine" },
{ value: "#16a34a", label: "Green" },
{ value: "#0d9488", label: "Teal" },
{ value: "#0f172a", label: "Ink" },
],
},
{
role: "accent",
label: "Accent",
hint: "Badges and highlights",
presets: [
{ value: "#fde68a", label: "Butter" },
{ value: "#fed7aa", label: "Peach" },
{ value: "#fecdd3", label: "Blush" },
{ value: "#e9d5ff", label: "Lilac" },
{ value: "#bfdbfe", label: "Sky" },
{ value: "#a7f3d0", label: "Mint" },
{ value: "#d9f99d", label: "Lime" },
{ value: "#facc15", label: "Sunflower" },
{ value: "#f97316", label: "Orange" },
],
},
{
role: "background",
label: "Background",
hint: "The page and its cards",
presets: [
{ value: "#ffffff", label: "White" },
{ value: "#fafaf9", label: "Paper" },
{ value: "#fdf6ec", label: "Cream" },
{ value: "#f1f5f9", label: "Mist" },
{ value: "#ecfdf5", label: "Mint wash" },
{ value: "#18181b", label: "Charcoal" },
{ value: "#0b1120", label: "Midnight" },
{ value: "#1c1917", label: "Espresso" },
{ value: "#111827", label: "Graphite" },
],
},
]
function ColorField({
role,
label,
hint,
presets,
value,
onChange,
recent,
onRecentChange,
}: (typeof FIELDS)[number] & { value: string; onChange: (v: string) => void; recent: string[]; onRecentChange: (c: string[]) => void }) {
const id = `cp-theme-${role}`
return (
<div className="flex items-center justify-between gap-3">
<div className="min-w-0">
<p id={`${id}-label`} className="text-sm font-medium">
{label}
</p>
<p className="truncate text-xs text-muted-foreground">{hint}</p>
</div>
<Popover>
<PopoverTrigger
aria-labelledby={`${id}-label ${id}-value`}
render={<Button variant="outline" className="w-32 shrink-0 justify-start gap-2 pr-2 pl-1.5 font-normal" />}
>
<ColorPickerSwatch color={value} />
<span id={`${id}-value`} className="tabular-nums">
{value}
</span>
<ChevronDownIcon className="ml-auto text-muted-foreground" />
</PopoverTrigger>
<PopoverContent align="end" className="w-72">
<ColorPicker aria-label={`${label} color`} value={value} onValueChange={onChange} recentColors={recent} onRecentColorsChange={onRecentChange} maxRecentColors={9}>
<ColorPickerArea />
<div className="flex items-center gap-2">
<ColorPickerEyeDropper />
<ColorPickerHue />
</div>
<div className="flex items-start gap-1.5">
<ColorPickerFormatSelect />
<ColorPickerInput />
</div>
<ColorPickerSwatches label="Presets" colors={presets} />
<ColorPickerRecent />
</ColorPicker>
</PopoverContent>
</Popover>
</div>
)
}
function ContrastRow({ label, fg, bg }: { label: string; fg: string; bg: string }) {
const ratio = getContrastRatio(fg, bg)
const level = ratio >= 7 ? "AAA" : ratio >= 4.5 ? "AA" : "Fail"
return (
<li className="flex items-center justify-between gap-3 py-1.5 text-sm">
<span className="flex min-w-0 items-center gap-2">
<span aria-hidden="true" className="flex size-5 shrink-0 items-center justify-center rounded-sm text-[0.625rem] font-semibold ring-1 ring-foreground/15" style={{ background: bg, color: fg }}>
Aa
</span>
<span className="truncate">{label}</span>
</span>
<span className="flex shrink-0 items-center gap-2">
<span className="text-muted-foreground tabular-nums">{ratio.toFixed(2)}:1</span>
<Badge variant={level === "Fail" ? "destructive" : "secondary"} className="w-14">
{level === "Fail" ? <XIcon /> : <CheckIcon />}
{level}
</Badge>
</span>
</li>
)
}
export default function ColorPickerThemeEditor() {
const [colors, setColors] = React.useState(DEFAULTS)
const [recent, setRecent] = React.useState<string[]>(["#0891b2", "#fbcfe8"])
const [copied, setCopied] = React.useState(false)
const { primary, accent, background } = colors
const foreground = getReadableTextColor(background, "#fafafa", "#0a0a0a")
const onPrimary = getReadableTextColor(primary, "#ffffff", "#0a0a0a")
const onAccent = getReadableTextColor(accent, "#ffffff", "#0a0a0a")
const muted = `color-mix(in oklch, ${foreground} 64%, ${background})`
const border = `color-mix(in oklch, ${foreground} 14%, ${background})`
const card = `color-mix(in oklch, ${foreground} 3%, ${background})`
const css = [
":root {",
` --background: ${background};`,
` --foreground: ${foreground};`,
` --primary: ${primary};`,
` --primary-foreground: ${onPrimary};`,
` --accent: ${accent};`,
` --accent-foreground: ${onAccent};`,
"}",
].join("\n")
const changed = FIELDS.some((f) => colors[f.role] !== DEFAULTS[f.role])
const copy = async () => {
try {
await navigator.clipboard.writeText(css)
setCopied(true)
window.setTimeout(() => setCopied(false), 1500)
} catch {
// Clipboard blocked; nothing to do.
}
}
return (
<div className="mx-auto grid w-full max-w-4xl gap-4 md:grid-cols-[20rem_1fr]">
<section aria-labelledby="cp-theme-title" className="flex flex-col gap-4 rounded-xl border bg-card p-4 text-card-foreground">
<div>
<h2 id="cp-theme-title" className="text-sm font-medium">
Theme colors
</h2>
<p className="text-xs text-muted-foreground">Text colors follow, picked for contrast.</p>
</div>
<div className="grid gap-3">
{FIELDS.map((f) => (
<ColorField key={f.role} {...f} value={colors[f.role]} onChange={(v) => setColors((c) => ({ ...c, [f.role]: v }))} recent={recent} onRecentChange={setRecent} />
))}
</div>
<div className="border-t pt-3">
<p className="text-xs font-medium text-muted-foreground">Contrast for normal text (AA needs 4.5:1)</p>
<ul className="mt-1 divide-y">
<ContrastRow label="Body text" fg={foreground} bg={background} />
<ContrastRow label="Button label" fg={onPrimary} bg={primary} />
<ContrastRow label="Badge label" fg={onAccent} bg={accent} />
<ContrastRow label="Links" fg={primary} bg={background} />
</ul>
</div>
<div className="mt-auto flex gap-2">
<Button variant="outline" size="sm" disabled={!changed} onClick={() => setColors(DEFAULTS)}>
<RotateCcwIcon />
Reset
</Button>
<Button variant="outline" size="sm" className="ml-auto" onClick={copy}>
{copied ? <CheckIcon /> : <CopyIcon />}
{copied ? "Copied" : "Copy CSS variables"}
</Button>
</div>
</section>
<section aria-label="Theme preview" className="flex min-w-0 items-center justify-center rounded-xl border p-4 sm:p-6" style={{ background, color: foreground }}>
<div className="w-full max-w-sm rounded-xl border p-5" style={{ background: card, borderColor: border }}>
<div className="flex items-start justify-between gap-3">
<div className="min-w-0">
<p className="font-semibold">Studio plan</p>
<p className="text-sm" style={{ color: muted }}>
For teams of up to 20
</p>
</div>
<span className="inline-flex h-6 shrink-0 items-center rounded-full px-2.5 text-xs font-medium" style={{ background: accent, color: onAccent }}>
Most popular
</span>
</div>
<p className="mt-4 text-3xl font-semibold tabular-nums">
$24
<span className="text-sm font-normal" style={{ color: muted }}>
{" "}
per seat a month
</span>
</p>
<ul className="mt-4 grid gap-2 text-sm">
{["Unlimited projects", "Client portal with your logo", "Priority support"].map((item) => (
<li key={item} className="flex items-center gap-2">
<CheckIcon className="size-4 shrink-0" style={{ color: primary }} />
{item}
</li>
))}
</ul>
<div className="mt-5 flex flex-wrap gap-2">
<span className="inline-flex h-9 items-center rounded-lg px-4 text-sm font-medium" style={{ background: primary, color: onPrimary }}>
Start free trial
</span>
<span className="inline-flex h-9 items-center rounded-lg border px-4 text-sm font-medium" style={{ borderColor: border }}>
Compare plans
</span>
</div>
<p className="mt-4 text-xs" style={{ color: muted }}>
Questions?{" "}
<span className="font-medium underline underline-offset-2" style={{ color: primary }}>
Talk to sales
</span>
</p>
</div>
</section>
</div>
)
}The component
What shadcn@latest add https://dresscodeui.com/r/dresscode/color-picker.json writes. It uses only shadcn/ui's variables, so it follows the theme.
components/ui/color-picker.tsx
"use client"
// Color picker: a saturation/brightness area, a hue slider, an optional alpha slider, hex, RGB and
// HSL inputs, an eyedropper (where the browser has one), preset swatches and recent colors.
// Compose the parts inside <ColorPicker>; it works controlled (value, onValueChange) or uncontrolled
// (defaultValue). The value is a hex string: #rrggbb, or #rrggbbaa when `alpha` is on and the color
// is translucent. The conversions are below and exported, with a WCAG contrast helper.
// Built for shadcn/ui; the frame, labels and inputs read only the theme's variables.
import * as React from "react"
import { CheckIcon, PipetteIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Input } from "@/components/ui/input"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { cn } from "@/lib/utils"
// ---------------------------------------------------------------------------------------------
// Color math
/** h 0-360, s and v 0-100, a 0-1 */
type Hsva = { h: number; s: number; v: number; a: number }
/** r, g and b 0-255, a 0-1 */
type Rgba = { r: number; g: number; b: number; a: number }
/** h 0-360, s and l 0-100, a 0-1 */
type Hsla = { h: number; s: number; l: number; a: number }
type ColorFormat = "hex" | "rgb" | "hsl"
const clamp = (n: number, min: number, max: number) => Math.min(max, Math.max(min, n))
const round = (n: number, digits = 0) => {
const p = 10 ** digits
return Math.round(n * p) / p
}
function hsvaToRgba({ h, s, v, a }: Hsva): Rgba {
const S = s / 100
const V = v / 100
const f = (n: number) => {
const k = (n + h / 60) % 6
return Math.round((V - V * S * Math.max(0, Math.min(k, 4 - k, 1))) * 255)
}
return { r: f(5), g: f(3), b: f(1), a }
}
function rgbaToHsva({ r, g, b, a }: Rgba): Hsva {
const R = r / 255
const G = g / 255
const B = b / 255
const max = Math.max(R, G, B)
const d = max - Math.min(R, G, B)
let h = 0
if (d) {
if (max === R) h = ((G - B) / d) % 6
else if (max === G) h = (B - R) / d + 2
else h = (R - G) / d + 4
h *= 60
if (h < 0) h += 360
}
return { h, s: max ? (d / max) * 100 : 0, v: max * 100, a }
}
function hsvaToHsla({ h, s, v, a }: Hsva): Hsla {
const S = s / 100
const V = v / 100
const l = V * (1 - S / 2)
const sl = l === 0 || l === 1 ? 0 : (V - l) / Math.min(l, 1 - l)
return { h, s: sl * 100, l: l * 100, a }
}
function hslaToHsva({ h, s, l, a }: Hsla): Hsva {
const S = s / 100
const L = l / 100
const v = L + S * Math.min(L, 1 - L)
return { h, s: v === 0 ? 0 : 2 * (1 - L / v) * 100, v: v * 100, a }
}
const hex2 = (n: number) => Math.round(n).toString(16).padStart(2, "0")
const HEX = /^#?([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i
function parseHex(input: string): Rgba | null {
const m = HEX.exec(input.trim())
if (!m) return null
const x = m[1].length <= 4 ? [...m[1]].map((c) => c + c).join("") : m[1]
const n = (i: number) => parseInt(x.slice(i, i + 2), 16)
return { r: n(0), g: n(2), b: n(4), a: x.length === 8 ? round(n(6) / 255, 3) : 1 }
}
/** Reads #rgb, #rgba, #rrggbb, #rrggbbaa, rgb()/rgba() and hsl()/hsla() (comma or space syntax). */
function parseColor(input: string | null | undefined): Hsva | null {
if (!input) return null
const s = input.trim().toLowerCase()
const hex = parseHex(s)
if (hex) return rgbaToHsva(hex)
const fn = /^(rgba?|hsla?)\(([^)]*)\)$/.exec(s)
if (!fn) return null
const parts = fn[2].split(/[\s,/]+/).filter(Boolean)
if (parts.length < 3 || parts.length > 4) return null
const num = (p: string, scale: number) => (p.endsWith("%") ? (parseFloat(p) / 100) * scale : parseFloat(p))
const a = parts[3] === undefined ? 1 : clamp(num(parts[3], 1), 0, 1)
if (fn[1].startsWith("rgb")) {
const [r, g, b] = parts.slice(0, 3).map((p) => clamp(num(p, 255), 0, 255))
if (![r, g, b, a].every(Number.isFinite)) return null
return rgbaToHsva({ r, g, b, a })
}
const h = parseFloat(parts[0])
const [sat, l] = parts.slice(1, 3).map((p) => clamp(parseFloat(p), 0, 100))
if (![h, sat, l, a].every(Number.isFinite)) return null
return hslaToHsva({ h: ((h % 360) + 360) % 360, s: sat, l, a })
}
/** The color as #rrggbb, #rrggbbaa, rgb()/rgba() or hsl()/hsla(). */
function formatColor(color: Hsva, format: ColorFormat | "hex8" = "hex"): string {
const { r, g, b, a } = hsvaToRgba(color)
const alpha = round(a, 2)
if (format === "hex") return `#${hex2(r)}${hex2(g)}${hex2(b)}`
if (format === "hex8") return `#${hex2(r)}${hex2(g)}${hex2(b)}${hex2(a * 255)}`
if (format === "rgb") return alpha < 1 ? `rgba(${r}, ${g}, ${b}, ${alpha})` : `rgb(${r}, ${g}, ${b})`
const hsl = hsvaToHsla(color)
const body = `${Math.round(hsl.h) % 360}, ${Math.round(hsl.s)}%, ${Math.round(hsl.l)}%`
return alpha < 1 ? `hsla(${body}, ${alpha})` : `hsl(${body})`
}
/** What the picker emits: #rrggbb, or #rrggbbaa when alpha is allowed and the color is translucent. */
const toValue = (color: Hsva, alpha: boolean) => (alpha && color.a < 1 ? formatColor(color, "hex8") : formatColor(color))
/** Every way of writing a color, for readouts and copy buttons. */
function getColorDetails(color: string | Hsva) {
const hsva = typeof color === "string" ? (parseColor(color) ?? { h: 0, s: 0, v: 0, a: 1 }) : color
return {
hex: formatColor(hsva, "hex"),
hex8: formatColor(hsva, "hex8"),
rgb: formatColor(hsva, "rgb"),
hsl: formatColor(hsva, "hsl"),
alpha: round(hsva.a, 2),
hsva,
rgba: hsvaToRgba(hsva),
hsla: hsvaToHsla(hsva),
}
}
type ColorDetails = ReturnType<typeof getColorDetails>
const linear = (c: number) => {
const s = c / 255
return s <= 0.04045 ? s / 12.92 : ((s + 0.055) / 1.055) ** 2.4
}
const luminance = ({ r, g, b }: Rgba) => 0.2126 * linear(r) + 0.7152 * linear(g) + 0.0722 * linear(b)
/** WCAG 2 contrast ratio, 1 to 21. A translucent foreground is laid over the background first. */
function getContrastRatio(foreground: string, background: string): number {
const bgColor = parseColor(background)
const fgColor = parseColor(foreground)
if (!bgColor || !fgColor) return 1
const bg = hsvaToRgba({ ...bgColor, a: 1 })
const fgRaw = hsvaToRgba(fgColor)
const mix = (f: number, b: number) => f * fgRaw.a + b * (1 - fgRaw.a)
const fg = { r: mix(fgRaw.r, bg.r), g: mix(fgRaw.g, bg.g), b: mix(fgRaw.b, bg.b), a: 1 }
const [hi, lo] = [luminance(fg), luminance(bg)].sort((x, y) => y - x)
return (hi + 0.05) / (lo + 0.05)
}
/** Whichever of two text colors reads better on the background. */
function getReadableTextColor(background: string, light = "#ffffff", dark = "#0a0a0a") {
return getContrastRatio(light, background) >= getContrastRatio(dark, background) ? light : dark
}
const HUE_NAMES: [number, string][] = [
[15, "red"],
[40, "orange"],
[65, "yellow"],
[90, "lime"],
[150, "green"],
[175, "teal"],
[200, "cyan"],
[250, "blue"],
[280, "indigo"],
[310, "purple"],
[335, "pink"],
]
const hueName = (h: number) => HUE_NAMES.find(([max]) => h < max)?.[1] ?? "red"
/** Grays and black have no hue (and black no saturation): keep the previous ones so the thumbs stay put. */
function keepHue(next: Hsva, prev: Hsva): Hsva {
return { ...next, h: next.s < 0.5 || next.v < 0.5 ? prev.h : next.h, s: next.v < 0.5 ? prev.s : next.s }
}
/** A checkerboard from the theme, behind anything translucent. */
const CHECKER =
"repeating-conic-gradient(color-mix(in oklch, var(--foreground) 22%, var(--background)) 0 25%, var(--background) 0 50%)"
// ---------------------------------------------------------------------------------------------
// State
type CommitOptions = { recent?: boolean }
type ColorPickerContextValue = {
color: Hsva
value: string
alpha: boolean
format: ColorFormat
setFormat: (format: ColorFormat) => void
setColor: (color: Hsva) => void
commit: (color: Hsva, options?: CommitOptions) => void
recent: string[]
id: string
}
const ColorPickerContext = React.createContext<ColorPickerContextValue | null>(null)
function useColorPicker() {
const ctx = React.useContext(ColorPickerContext)
if (!ctx) throw new Error("Color picker parts must be inside <ColorPicker>")
return ctx
}
function useControllable<T>(prop: T | undefined, initial: T, onChange?: (value: T) => void) {
const [inner, setInner] = React.useState(initial)
const controlled = prop !== undefined
const set = React.useCallback(
(next: T) => {
if (!controlled) setInner(next)
onChange?.(next)
},
[controlled, onChange],
)
return [controlled ? prop : inner, set] as const
}
type ColorPickerProps = Omit<React.ComponentProps<"div">, "defaultValue" | "onChange"> & {
/** Any CSS hex, rgb() or hsl() color. */
value?: string
defaultValue?: string
/** While the color changes (every drag step and keystroke). */
onValueChange?: (value: string, details: ColorDetails) => void
/** When the person settles on a color: end of a drag, Enter or blur in an input, a swatch, the eyedropper. */
onValueCommit?: (value: string) => void
/** Allow translucent colors: shows ColorPickerAlpha and the A inputs, and emits #rrggbbaa. */
alpha?: boolean
format?: ColorFormat
defaultFormat?: ColorFormat
onFormatChange?: (format: ColorFormat) => void
recentColors?: string[]
defaultRecentColors?: string[]
onRecentColorsChange?: (colors: string[]) => void
maxRecentColors?: number
}
function ColorPicker({
value,
defaultValue = "#2563eb",
onValueChange,
onValueCommit,
alpha = false,
format: formatProp,
defaultFormat = "hex",
onFormatChange,
recentColors,
defaultRecentColors = [],
onRecentColorsChange,
maxRecentColors = 8,
className,
children,
...props
}: ColorPickerProps) {
const id = React.useId()
const [color, setColorState] = React.useState<Hsva>(() => {
const parsed = parseColor(value ?? defaultValue) ?? { h: 0, s: 0, v: 0, a: 1 }
return alpha ? parsed : { ...parsed, a: 1 }
})
// Follow the value prop, but keep our own hue and saturation when it is the color we already show.
const [syncedValue, setSyncedValue] = React.useState(value)
if (value !== syncedValue) {
setSyncedValue(value)
const parsed = parseColor(value)
if (parsed && toValue(parsed, alpha) !== toValue(color, alpha)) setColorState(keepHue(alpha ? parsed : { ...parsed, a: 1 }, color))
}
const [format, setFormat] = useControllable(formatProp, defaultFormat, onFormatChange)
const [recent, setRecent] = useControllable(recentColors, defaultRecentColors, onRecentColorsChange)
const current = toValue(color, alpha)
const setColor = (next: Hsva) => {
const n = alpha ? next : { ...next, a: 1 }
setColorState(n)
const v = toValue(n, alpha)
if (v !== current) onValueChange?.(v, getColorDetails(n))
}
const commit = (next: Hsva, { recent: addToRecent = true }: CommitOptions = {}) => {
const v = toValue(alpha ? next : { ...next, a: 1 }, alpha)
onValueCommit?.(v)
if (addToRecent) setRecent([v, ...recent.filter((c) => c.toLowerCase() !== v)].slice(0, maxRecentColors))
}
return (
<ColorPickerContext.Provider value={{ color, value: current, alpha, format, setFormat, setColor, commit, recent, id }}>
<div
role="group"
aria-label="Color picker"
data-slot="color-picker"
className={cn("flex w-full min-w-0 flex-col gap-3", className)}
{...props}
>
{children}
</div>
</ColorPickerContext.Provider>
)
}
// ---------------------------------------------------------------------------------------------
// Area and sliders
const thumbClass =
"pointer-events-none absolute size-4 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-[0_0_0_1px_rgb(0_0_0/0.3),0_1px_3px_rgb(0_0_0/0.35)] outline-none focus-visible:ring-3 focus-visible:ring-ring/60"
/** Pointer dragging with capture: works for mouse, pen and touch. */
function useDrag(onMove: (x: number, y: number) => void, onEnd: () => void) {
const dragging = React.useRef(false)
const at = (e: React.PointerEvent<HTMLElement>) => {
const r = e.currentTarget.getBoundingClientRect()
onMove(clamp((e.clientX - r.left) / r.width, 0, 1), clamp((e.clientY - r.top) / r.height, 0, 1))
}
const end = () => {
if (!dragging.current) return
dragging.current = false
onEnd()
}
return {
onPointerDown: (e: React.PointerEvent<HTMLElement>) => {
if (e.button !== 0) return
e.preventDefault()
e.currentTarget.setPointerCapture(e.pointerId)
dragging.current = true
;(e.currentTarget.querySelector("[role=slider]") as HTMLElement | null)?.focus({ preventScroll: true })
at(e)
},
onPointerMove: (e: React.PointerEvent<HTMLElement>) => {
if (dragging.current) at(e)
},
onPointerUp: end,
onPointerCancel: end,
onLostPointerCapture: end,
}
}
/** Arrow keys move by 1, Shift + arrows by 10; Page Up/Down by 10; Home/End jump to the ends. */
function stepFromKey(e: React.KeyboardEvent): { x: number; y: number; jump?: "min" | "max" } | null {
const step = e.shiftKey ? 10 : 1
switch (e.key) {
case "ArrowLeft":
return { x: -step, y: 0 }
case "ArrowRight":
return { x: step, y: 0 }
case "ArrowUp":
return { x: 0, y: step }
case "ArrowDown":
return { x: 0, y: -step }
case "PageUp":
return { x: 0, y: 10 }
case "PageDown":
return { x: 0, y: -10 }
case "Home":
return { x: 0, y: 0, jump: "min" }
case "End":
return { x: 0, y: 0, jump: "max" }
default:
return null
}
}
/** Commits once when focus leaves a control that the keyboard changed. */
function useKeyboardCommit() {
const { color, commit } = useColorPicker()
const changed = React.useRef(false)
return {
mark: () => {
changed.current = true
},
onBlur: () => {
if (!changed.current) return
changed.current = false
commit(color)
},
}
}
function ColorPickerArea({ className, style, ...props }: React.ComponentProps<"div">) {
const { color, setColor, commit } = useColorPicker()
const latest = React.useRef(color)
const keys = useKeyboardCommit()
const drag = useDrag(
(x, y) => {
latest.current = { ...color, s: x * 100, v: (1 - y) * 100 }
setColor(latest.current)
},
() => commit(latest.current),
)
const s = Math.round(color.s)
const v = Math.round(color.v)
const onKeyDown = (e: React.KeyboardEvent) => {
const d = stepFromKey(e)
if (!d) return
e.preventDefault()
const ns = d.jump ? (d.jump === "min" ? 0 : 100) : clamp(s + d.x, 0, 100)
setColor({ ...color, s: ns, v: clamp(v + d.y, 0, 100) })
keys.mark()
}
return (
<div
data-slot="color-picker-area"
className={cn("relative h-40 w-full shrink-0 cursor-crosshair touch-none rounded-lg select-none", className)}
style={{
background: `linear-gradient(to top, #000, transparent), linear-gradient(to right, #fff, hsl(${color.h} 100% 50%))`,
...style,
}}
{...drag}
{...props}
>
<div aria-hidden="true" className="pointer-events-none absolute inset-0 rounded-[inherit] ring-1 ring-foreground/10 ring-inset" />
<div
role="slider"
tabIndex={0}
aria-label="Saturation and brightness"
aria-roledescription="2D slider"
aria-valuemin={0}
aria-valuemax={100}
aria-valuenow={s}
aria-valuetext={`Saturation ${s}%, brightness ${v}%`}
data-slot="color-picker-area-thumb"
className={thumbClass}
style={{ left: `${color.s}%`, top: `${100 - color.v}%`, background: formatColor({ ...color, a: 1 }) }}
onKeyDown={onKeyDown}
onBlur={keys.onBlur}
/>
</div>
)
}
type SliderProps = Omit<React.ComponentProps<"div">, "onChange"> & {
label: string
value: number
max: number
valueText: string
track: React.CSSProperties
thumb: string
/** The picker color for a slider value. */
toColor: (value: number) => Hsva
}
function ColorSlider({ label, value, max, valueText, track, thumb, toColor, className, ...props }: SliderProps) {
const { color, setColor, commit } = useColorPicker()
const keys = useKeyboardCommit()
const latest = React.useRef(color)
const drag = useDrag(
(x) => {
latest.current = toColor(round(x * max))
setColor(latest.current)
},
() => commit(latest.current),
)
const onKeyDown = (e: React.KeyboardEvent) => {
const d = stepFromKey(e)
if (!d) return
e.preventDefault()
setColor(toColor(d.jump ? (d.jump === "min" ? 0 : max) : clamp(Math.round(value) + d.x + d.y, 0, max)))
keys.mark()
}
return (
<div
className={cn("relative h-3 w-full min-w-0 cursor-pointer touch-none rounded-full select-none", className)}
style={track}
{...drag}
{...props}
>
<div aria-hidden="true" className="pointer-events-none absolute inset-0 rounded-[inherit] ring-1 ring-foreground/10 ring-inset" />
<div
role="slider"
tabIndex={0}
aria-label={label}
aria-orientation="horizontal"
aria-valuemin={0}
aria-valuemax={max}
aria-valuenow={Math.round(value)}
aria-valuetext={valueText}
className={cn(thumbClass, "top-1/2")}
style={{ left: `${(value / max) * 100}%`, background: thumb }}
onKeyDown={onKeyDown}
onBlur={keys.onBlur}
/>
</div>
)
}
const HUE_GRADIENT = "linear-gradient(to right, #f00 0%, #ff0 16.67%, #0f0 33.33%, #0ff 50%, #00f 66.67%, #f0f 83.33%, #f00 100%)"
function ColorPickerHue({ className, ...props }: React.ComponentProps<"div">) {
const { color } = useColorPicker()
const h = Math.round(color.h)
return (
<ColorSlider
data-slot="color-picker-hue"
label="Hue"
value={color.h}
max={360}
valueText={`${h} degrees, ${hueName(h)}`}
track={{ background: HUE_GRADIENT }}
thumb={`hsl(${color.h} 100% 50%)`}
toColor={(h) => ({ ...color, h })}
className={className}
{...props}
/>
)
}
/** Renders only when the ColorPicker has `alpha`. */
function ColorPickerAlpha({ className, ...props }: React.ComponentProps<"div">) {
const { color, alpha } = useColorPicker()
if (!alpha) return null
const { r, g, b } = hsvaToRgba(color)
const pct = Math.round(color.a * 100)
return (
<ColorSlider
data-slot="color-picker-alpha"
label="Opacity"
value={pct}
max={100}
valueText={`${pct}%`}
track={{
backgroundImage: `linear-gradient(to right, rgb(${r} ${g} ${b} / 0), rgb(${r} ${g} ${b})), ${CHECKER}`,
backgroundSize: "100% 100%, 8px 8px",
}}
thumb={`rgb(${r} ${g} ${b} / ${color.a})`}
toColor={(p) => ({ ...color, a: p / 100 })}
className={className}
{...props}
/>
)
}
// ---------------------------------------------------------------------------------------------
// Eyedropper
type EyeDropperCtor = new () => { open: () => Promise<{ sRGBHex: string }> }
const noSubscribe = () => () => {}
/** A button that samples a color from anywhere on screen. Renders nothing where the browser has no EyeDropper. */
function ColorPickerEyeDropper({ className, ...props }: React.ComponentProps<typeof Button>) {
const { color, setColor, commit } = useColorPicker()
const supported = React.useSyncExternalStore(
noSubscribe,
() => "EyeDropper" in window,
() => false,
)
if (!supported) return null
const pick = async () => {
try {
const Dropper = (window as unknown as { EyeDropper: EyeDropperCtor }).EyeDropper
const { sRGBHex } = await new Dropper().open()
const picked = parseColor(sRGBHex)
if (!picked) return
const next = { ...picked, a: color.a }
setColor(next)
commit(next)
} catch {
// Cancelled with Escape.
}
}
return (
<Button
type="button"
variant="outline"
size="icon"
aria-label="Pick a color from the screen"
title="Pick a color from the screen"
data-slot="color-picker-eyedropper"
className={className}
onClick={pick}
{...props}
>
<PipetteIcon />
</Button>
)
}
// ---------------------------------------------------------------------------------------------
// Inputs
const FORMATS = [
{ value: "hex", label: "Hex" },
{ value: "rgb", label: "RGB" },
{ value: "hsl", label: "HSL" },
]
/** Switches ColorPickerInput between hex, RGB and HSL. */
function ColorPickerFormatSelect({ className }: { className?: string }) {
const { format, setFormat } = useColorPicker()
return (
<Select items={FORMATS} value={format} onValueChange={(v) => v && setFormat(v as ColorFormat)}>
<SelectTrigger size="sm" aria-label="Color format" data-slot="color-picker-format" className={cn("h-8 w-[4.75rem] shrink-0", className)}>
<SelectValue />
</SelectTrigger>
<SelectContent>
{FORMATS.map((f) => (
<SelectItem key={f.value} value={f.value}>
{f.label}
</SelectItem>
))}
</SelectContent>
</Select>
)
}
function Field({ short, className, children }: { short: string; className?: string; children: React.ReactNode }) {
return (
<label className={cn("flex min-w-0 flex-1 flex-col items-center gap-1", className)}>
{children}
<span aria-hidden="true" className="text-xs leading-none text-muted-foreground">
{short}
</span>
</label>
)
}
/** A whole-number field: type a value, or Arrow Up/Down (Shift for 10). Out of range values clamp on blur. */
function NumberField({ label, short, value, max, toColor }: { label: string; short: string; value: number; max: number; toColor: (n: number) => Hsva }) {
const { color, alpha, setColor, commit } = useColorPicker()
const shown = String(Math.round(value))
const [draft, setDraft] = React.useState<{ text: string; for: string } | null>(null)
const start = React.useRef<string | null>(null)
const text = draft && draft.for === shown ? draft.text : shown
/** Applies what was typed (clamped) and returns the color the field settled on. */
const finish = () => {
let next = color
if (draft && draft.for === shown) {
const n = parseFloat(draft.text)
if (Number.isFinite(n)) {
next = toColor(clamp(Math.round(n), 0, max))
setColor(next)
}
}
setDraft(null)
return next
}
const commitIfChanged = (next: Hsva) => {
if (start.current !== null && start.current !== toValue(next, alpha)) commit(next)
}
return (
<Field short={short}>
<Input
inputMode="numeric"
aria-label={label}
value={text}
className="h-8 px-1 text-center tabular-nums"
onFocus={(e) => {
start.current = toValue(color, alpha)
e.currentTarget.select()
}}
onChange={(e) => {
const t = e.target.value.replace(/[^\d.]/g, "").slice(0, 3)
const n = parseFloat(t)
if (Number.isFinite(n) && n >= 0 && n <= max) {
setColor(toColor(n))
setDraft({ text: t, for: String(Math.round(n)) })
} else setDraft({ text: t, for: shown })
}}
onKeyDown={(e) => {
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
e.preventDefault()
const step = (e.shiftKey ? 10 : 1) * (e.key === "ArrowUp" ? 1 : -1)
setColor(toColor(clamp(Math.round(value) + step, 0, max)))
setDraft(null)
} else if (e.key === "Enter") {
const next = finish()
commitIfChanged(next)
start.current = toValue(next, alpha)
}
}}
onBlur={() => {
commitIfChanged(finish())
start.current = null
}}
/>
</Field>
)
}
/** What is typed, the color it was typed over, and whether typing already changed the color. */
type HexDraft = { text: string; for: string; invalid?: boolean; applied?: boolean } | null
/** Hex digits apply as you type once there are six or eight; three, four or a pasted rgb()/hsl() wait for Enter or blur. */
function HexField({ draft, setDraft, errorId }: { draft: HexDraft; setDraft: (d: HexDraft) => void; errorId: string }) {
const { color, alpha, setColor, commit } = useColorPicker()
const shown = formatColor(color, "hex")
const active = draft && draft.for === shown ? draft : null
const read = (text: string) => {
const rgba = parseHex(text)
if (rgba) {
const digits = text.trim().replace("#", "").length
return keepHue({ ...rgbaToHsva(rgba), a: alpha && (digits === 4 || digits === 8) ? rgba.a : color.a }, color)
}
const parsed = parseColor(text)
return parsed ? keepHue(alpha ? parsed : { ...parsed, a: color.a }, color) : null
}
const apply = () => {
if (!active) return
if (active.text.trim().replace("#", "").toLowerCase() === shown.slice(1)) {
setDraft(null)
if (active.applied) commit(color)
return
}
const next = read(active.text)
if (!next) return setDraft({ ...active, invalid: true })
setDraft(null)
setColor(next)
commit(next)
}
return (
<Field short="Hex" className="flex-[2.4]">
<Input
aria-label="Hex color"
spellCheck={false}
autoComplete="off"
value={active ? active.text : shown}
aria-invalid={active?.invalid || undefined}
aria-describedby={active?.invalid ? errorId : undefined}
className="h-8 px-2 tabular-nums"
onFocus={(e) => e.currentTarget.select()}
onChange={(e) => {
const text = e.target.value
const next = /^#?([0-9a-f]{6}|[0-9a-f]{8})$/i.test(text.trim()) ? read(text) : null
if (next) {
setColor(next)
setDraft({ text, for: formatColor(next, "hex"), applied: true })
} else setDraft({ text, for: shown, applied: active?.applied })
}}
onKeyDown={(e) => {
if (e.key === "Enter") apply()
if (e.key === "Escape" && active) {
// Undo the typing instead of closing a surrounding popover.
e.stopPropagation()
setDraft(null)
}
}}
onBlur={apply}
/>
</Field>
)
}
/** The fields for the current format (hex, RGB or HSL), plus opacity when the picker has alpha. */
function ColorPickerInput({ className, ...props }: React.ComponentProps<"div">) {
const { color, alpha, format, id } = useColorPicker()
const [hexDraft, setHexDraft] = React.useState<HexDraft>(null)
const rgba = hsvaToRgba(color)
const hsla = hsvaToHsla(color)
const fromRgb = (patch: Partial<Rgba>) => keepHue(rgbaToHsva({ ...rgba, ...patch }), color)
const fromHsl = (patch: Partial<Hsla>) => hslaToHsva({ ...hsla, ...patch })
const errorId = `${id}-hex-error`
const hexInvalid = format === "hex" && !!hexDraft?.invalid && hexDraft.for === formatColor(color, "hex")
return (
<div data-slot="color-picker-input" className={cn("flex min-w-0 flex-1 flex-col gap-1.5", className)} {...props}>
<div className="flex min-w-0 gap-1.5">
{format === "hex" ? <HexField draft={hexDraft} setDraft={setHexDraft} errorId={errorId} /> : null}
{format === "rgb" ? (
<>
<NumberField label="Red" short="R" value={rgba.r} max={255} toColor={(r) => fromRgb({ r })} />
<NumberField label="Green" short="G" value={rgba.g} max={255} toColor={(g) => fromRgb({ g })} />
<NumberField label="Blue" short="B" value={rgba.b} max={255} toColor={(b) => fromRgb({ b })} />
</>
) : null}
{format === "hsl" ? (
<>
<NumberField label="Hue, degrees" short="H" value={hsla.h} max={360} toColor={(h) => fromHsl({ h })} />
<NumberField label="Saturation, percent" short="S" value={hsla.s} max={100} toColor={(s) => fromHsl({ s })} />
<NumberField label="Lightness, percent" short="L" value={hsla.l} max={100} toColor={(l) => fromHsl({ l })} />
</>
) : null}
{alpha ? (
<NumberField label="Opacity, percent" short="A" value={color.a * 100} max={100} toColor={(n) => ({ ...color, a: n / 100 })} />
) : null}
</div>
{hexInvalid ? (
<p id={errorId} role="alert" data-slot="color-picker-input-error" className="text-xs text-destructive">
Use 3, 6 or 8 hex digits, like #3b82f6.
</p>
) : null}
</div>
)
}
// ---------------------------------------------------------------------------------------------
// Swatches
type SwatchColor = string | { value: string; label?: string }
/** A color chip with a checkerboard behind it, for triggers and readouts. Shows the picker's color by default. */
function ColorPickerSwatch({ color, className, ...props }: React.ComponentProps<"span"> & { color?: string }) {
const ctx = React.useContext(ColorPickerContext)
const c = color ?? ctx?.value ?? "transparent"
return (
<span
data-slot="color-picker-swatch"
aria-hidden="true"
className={cn("relative inline-block size-5 shrink-0 overflow-hidden rounded-md", className)}
style={{ backgroundImage: CHECKER, backgroundSize: "8px 8px" }}
{...props}
>
<span className="absolute inset-0" style={{ background: c }} />
<span className="absolute inset-0 rounded-[inherit] ring-1 ring-foreground/15 ring-inset" />
</span>
)
}
type SwatchGridProps = React.ComponentProps<"div"> & {
colors: SwatchColor[]
/** A visible heading for the row; without one, pass aria-label or aria-labelledby (they go on the swatch group). */
label?: string
/** Shown in place of the swatches when there are none. */
emptyText?: string
/** Lay the swatches out in this many equal columns that fill the width, instead of a wrapping row. */
columns?: number
}
/** Swatch buttons in a wrapping row. Arrow keys move between them; Enter or Space picks one. `children` go at the end of the row. */
function SwatchGrid({ colors, label, emptyText, columns, recent, className, children, "aria-label": ariaLabel, "aria-labelledby": ariaLabelledBy, ...props }: SwatchGridProps & { recent?: boolean }) {
const { color, value, alpha, setColor, commit } = useColorPicker()
const labelId = React.useId()
const refs = React.useRef<(HTMLButtonElement | null)[]>([])
const [focusIndex, setFocusIndex] = React.useState<number | null>(null)
const items = colors
.map((c) => (typeof c === "string" ? { value: c } : c))
.map((c) => ({ ...c, color: parseColor(c.value) }))
.filter((c): c is { value: string; label?: string; color: Hsva } => !!c.color)
const selected = items.findIndex((c) => toValue(alpha ? c.color : { ...c.color, a: 1 }, alpha) === value)
const tabbable = focusIndex !== null && focusIndex < items.length ? focusIndex : Math.max(selected, 0)
const onKeyDown = (e: React.KeyboardEvent, i: number) => {
const buttons = refs.current.slice(0, items.length).filter(Boolean) as HTMLButtonElement[]
const top = buttons[0]?.offsetTop
const cols = Math.max(1, buttons.filter((b) => b.offsetTop === top).length)
const keys: Record<string, number> = { ArrowRight: i + 1, ArrowLeft: i - 1, ArrowDown: i + cols, ArrowUp: i - cols, Home: 0, End: items.length - 1 }
const next = keys[e.key]
if (next === undefined) return
e.preventDefault()
const n = clamp(next, 0, items.length - 1)
refs.current[n]?.focus()
setFocusIndex(n)
}
return (
<div data-slot={recent ? "color-picker-recent" : "color-picker-swatches"} className={cn("flex flex-col gap-2", className)} {...props}>
{label ? (
<p id={labelId} className="text-xs font-medium text-muted-foreground">
{label}
</p>
) : null}
<div
role="group"
aria-labelledby={label ? labelId : ariaLabelledBy} aria-label={label ? undefined : ariaLabel}
className={columns ? "grid gap-1.5" : "flex flex-wrap items-center gap-1.5"}
style={columns ? { gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))` } : undefined}
>
{!items.length && emptyText ? <p className="col-span-full mr-1 text-xs text-muted-foreground">{emptyText}</p> : null}
{items.map((c, i) => {
const on = i === selected
const opaque = formatColor({ ...c.color, a: 1 })
return (
<button
key={`${c.value}-${i}`}
ref={(el) => {
refs.current[i] = el
}}
type="button"
aria-label={c.label ? `${c.label}, ${c.value}` : c.value}
aria-pressed={on}
title={c.label ?? c.value}
tabIndex={i === tabbable ? 0 : -1}
data-slot="color-picker-swatch-button"
className={cn(
"relative flex shrink-0 cursor-pointer",
columns ? "aspect-square w-full" : "size-[var(--swatch-size,1.5rem)]",
"items-center justify-center overflow-hidden rounded-md outline-none focus-visible:ring-3 focus-visible:ring-ring/60 aria-pressed:ring-2 aria-pressed:ring-ring aria-pressed:ring-offset-2 aria-pressed:ring-offset-background",
)}
style={{ backgroundImage: CHECKER, backgroundSize: "8px 8px" }}
onFocus={() => setFocusIndex(i)}
onKeyDown={(e) => onKeyDown(e, i)}
onClick={() => {
const next = keepHue(alpha ? c.color : { ...c.color, a: 1 }, color)
setColor(next)
commit(next, { recent: false })
}}
>
<span className="absolute inset-0" style={{ background: c.value }} />
<span className="absolute inset-0 rounded-[inherit] ring-1 ring-foreground/15 ring-inset" />
{on ? <CheckIcon className="relative size-3.5" strokeWidth={3} style={{ color: getReadableTextColor(opaque) }} /> : null}
</button>
)
})}
{children}
</div>
</div>
)
}
/** Preset colors. Picking one sets the color without adding it to the recent list. */
function ColorPickerSwatches(props: SwatchGridProps) {
return <SwatchGrid {...props} />
}
/** Colors the person settled on, newest first (see onValueCommit). Shows `emptyText` until there are some. */
function ColorPickerRecent({ label = "Recent", emptyText = "Colors you pick show up here.", ...props }: Omit<SwatchGridProps, "colors">) {
const { recent } = useColorPicker()
return <SwatchGrid colors={recent} label={label} emptyText={emptyText} recent {...props} />
}
export {
ColorPicker,
ColorPickerAlpha,
ColorPickerArea,
ColorPickerEyeDropper,
ColorPickerFormatSelect,
ColorPickerHue,
ColorPickerInput,
ColorPickerRecent,
ColorPickerSwatch,
ColorPickerSwatches,
formatColor,
getColorDetails,
getContrastRatio,
getReadableTextColor,
parseColor,
useColorPicker,
}
export type { ColorDetails, ColorFormat, Hsla, Hsva, Rgba, SwatchColor }