1"use client";
2
3import { cva, type VariantProps } from "class-variance-authority";
4import { PipetteIcon } from "lucide-react";
5import {
6 Direction as DirectionPrimitive,
7 Slider as SliderPrimitive,
8 Slot as SlotPrimitive,
9} from "radix-ui";
10import * as React from "react";
11import { useComposedRefs } from "@/lib/compose-refs";
12import { cn } from "@/lib/utils";
13import { VisuallyHiddenInput } from "@/registry/bases/radix/components/visually-hidden-input";
14import { useAsRef } from "@/registry/bases/radix/hooks/use-as-ref";
15import { useIsomorphicLayoutEffect } from "@/registry/bases/radix/hooks/use-isomorphic-layout-effect";
16import { useLazyRef } from "@/registry/bases/radix/hooks/use-lazy-ref";
17import { Button } from "@/registry/bases/radix/ui/button";
18import { Input } from "@/registry/bases/radix/ui/input";
19import {
20 Popover,
21 PopoverContent,
22 PopoverTrigger,
23} from "@/registry/bases/radix/ui/popover";
24import {
25 Select,
26 SelectContent,
27 SelectItem,
28 SelectTrigger,
29 SelectValue,
30} from "@/registry/bases/radix/ui/select";
31
32const ROOT_NAME = "ColorPicker";
33const ROOT_IMPL_NAME = "ColorPickerImpl";
34const TRIGGER_NAME = "ColorPickerTrigger";
35const CONTENT_NAME = "ColorPickerContent";
36const AREA_NAME = "ColorPickerArea";
37const HUE_SLIDER_NAME = "ColorPickerHueSlider";
38const ALPHA_SLIDER_NAME = "ColorPickerAlphaSlider";
39const SWATCH_NAME = "ColorPickerSwatch";
40const EYE_DROPPER_NAME = "ColorPickerEyeDropper";
41const FORMAT_SELECT_NAME = "ColorPickerFormatSelect";
42const INPUT_NAME = "ColorPickerInput";
43
44const colorFormats = ["hex", "rgb", "hsl", "hsb"] as const;
45
46interface DivProps extends React.ComponentProps<"div"> {
47 asChild?: boolean;
48}
49
50type RootElement = React.ComponentRef<typeof ColorPicker>;
51type AreaElement = React.ComponentRef<typeof ColorPickerArea>;
52type InputElement = React.ComponentRef<typeof ColorPickerInput>;
53
54type ColorFormat = (typeof colorFormats)[number];
55
56/**
57 * @see https://gist.github.com/bkrmendy/f4582173f50fab209ddfef1377ab31e3
58 */
59interface EyeDropper {
60 open: (options?: { signal?: AbortSignal }) => Promise<{ sRGBHex: string }>;
61}
62
63declare global {
64 interface Window {
65 EyeDropper?: {
66 new (): EyeDropper;
67 };
68 }
69}
70
71interface ColorValue {
72 r: number;
73 g: number;
74 b: number;
75 a: number;
76}
77
78interface HSVColorValue {
79 h: number;
80 s: number;
81 v: number;
82 a: number;
83}
84
85function hexToRgb(hex: string, alpha?: number): ColorValue {
86 const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
87 return result
88 ? {
89 r: Number.parseInt(result[1] ?? "0", 16),
90 g: Number.parseInt(result[2] ?? "0", 16),
91 b: Number.parseInt(result[3] ?? "0", 16),
92 a: alpha ?? 1,
93 }
94 : { r: 0, g: 0, b: 0, a: alpha ?? 1 };
95}
96
97function rgbToHex(color: ColorValue): string {
98 const toHex = (n: number) => {
99 const hex = Math.round(n).toString(16);
100 return hex.length === 1 ? `0${hex}` : hex;
101 };
102 return `#${toHex(color.r)}${toHex(color.g)}${toHex(color.b)}`;
103}
104
105function rgbToHsv(color: ColorValue): HSVColorValue {
106 const r = color.r / 255;
107 const g = color.g / 255;
108 const b = color.b / 255;
109
110 const max = Math.max(r, g, b);
111 const min = Math.min(r, g, b);
112 const diff = max - min;
113
114 let h = 0;
115 if (diff !== 0) {
116 switch (max) {
117 case r:
118 h = ((g - b) / diff) % 6;
119 break;
120 case g:
121 h = (b - r) / diff + 2;
122 break;
123 case b:
124 h = (r - g) / diff + 4;
125 break;
126 }
127 }
128 h = Math.round(h * 60);
129 if (h < 0) h += 360;
130
131 const s = max === 0 ? 0 : diff / max;
132 const v = max;
133
134 return {
135 h,
136 s: Math.round(s * 100),
137 v: Math.round(v * 100),
138 a: color.a,
139 };
140}
141
142function hsvToRgb(hsv: HSVColorValue): ColorValue {
143 const h = hsv.h / 360;
144 const s = hsv.s / 100;
145 const v = hsv.v / 100;
146
147 const i = Math.floor(h * 6);
148 const f = h * 6 - i;
149 const p = v * (1 - s);
150 const q = v * (1 - f * s);
151 const t = v * (1 - (1 - f) * s);
152
153 let r: number;
154 let g: number;
155 let b: number;
156
157 switch (i % 6) {
158 case 0: {
159 r = v;
160 g = t;
161 b = p;
162 break;
163 }
164 case 1: {
165 r = q;
166 g = v;
167 b = p;
168 break;
169 }
170 case 2: {
171 r = p;
172 g = v;
173 b = t;
174 break;
175 }
176 case 3: {
177 r = p;
178 g = q;
179 b = v;
180 break;
181 }
182 case 4: {
183 r = t;
184 g = p;
185 b = v;
186 break;
187 }
188 case 5: {
189 r = v;
190 g = p;
191 b = q;
192 break;
193 }
194 default: {
195 r = 0;
196 g = 0;
197 b = 0;
198 }
199 }
200
201 return {
202 r: Math.round(r * 255),
203 g: Math.round(g * 255),
204 b: Math.round(b * 255),
205 a: hsv.a,
206 };
207}
208
209function colorToString(color: ColorValue, format: ColorFormat = "hex"): string {
210 switch (format) {
211 case "hex":
212 return rgbToHex(color);
213 case "rgb":
214 return color.a < 1
215 ? `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`
216 : `rgb(${color.r}, ${color.g}, ${color.b})`;
217 case "hsl": {
218 const hsl = rgbToHsl(color);
219 return color.a < 1
220 ? `hsla(${hsl.h}, ${hsl.s}%, ${hsl.l}%, ${color.a})`
221 : `hsl(${hsl.h}, ${hsl.s}%, ${hsl.l}%)`;
222 }
223 case "hsb": {
224 const hsv = rgbToHsv(color);
225 return color.a < 1
226 ? `hsba(${hsv.h}, ${hsv.s}%, ${hsv.v}%, ${color.a})`
227 : `hsb(${hsv.h}, ${hsv.s}%, ${hsv.v}%)`;
228 }
229 default:
230 return rgbToHex(color);
231 }
232}
233
234function rgbToHsl(color: ColorValue) {
235 const r = color.r / 255;
236 const g = color.g / 255;
237 const b = color.b / 255;
238
239 const max = Math.max(r, g, b);
240 const min = Math.min(r, g, b);
241 const diff = max - min;
242 const sum = max + min;
243
244 const l = sum / 2;
245
246 let h = 0;
247 let s = 0;
248
249 if (diff !== 0) {
250 s = l > 0.5 ? diff / (2 - sum) : diff / sum;
251
252 if (max === r) {
253 h = (g - b) / diff + (g < b ? 6 : 0);
254 } else if (max === g) {
255 h = (b - r) / diff + 2;
256 } else if (max === b) {
257 h = (r - g) / diff + 4;
258 }
259 h /= 6;
260 }
261
262 return {
263 h: Math.round(h * 360),
264 s: Math.round(s * 100),
265 l: Math.round(l * 100),
266 };
267}
268
269function hslToRgb(
270 hsl: { h: number; s: number; l: number },
271 alpha = 1,
272): ColorValue {
273 const h = hsl.h / 360;
274 const s = hsl.s / 100;
275 const l = hsl.l / 100;
276
277 const c = (1 - Math.abs(2 * l - 1)) * s;
278 const x = c * (1 - Math.abs(((h * 6) % 2) - 1));
279 const m = l - c / 2;
280
281 let r = 0;
282 let g = 0;
283 let b = 0;
284
285 if (h >= 0 && h < 1 / 6) {
286 r = c;
287 g = x;
288 b = 0;
289 } else if (h >= 1 / 6 && h < 2 / 6) {
290 r = x;
291 g = c;
292 b = 0;
293 } else if (h >= 2 / 6 && h < 3 / 6) {
294 r = 0;
295 g = c;
296 b = x;
297 } else if (h >= 3 / 6 && h < 4 / 6) {
298 r = 0;
299 g = x;
300 b = c;
301 } else if (h >= 4 / 6 && h < 5 / 6) {
302 r = x;
303 g = 0;
304 b = c;
305 } else if (h >= 5 / 6 && h < 1) {
306 r = c;
307 g = 0;
308 b = x;
309 }
310
311 return {
312 r: Math.round((r + m) * 255),
313 g: Math.round((g + m) * 255),
314 b: Math.round((b + m) * 255),
315 a: alpha,
316 };
317}
318
319function parseColorString(value: string): ColorValue | null {
320 const trimmed = value.trim();
321
322 // Parse hex colors
323 if (trimmed.startsWith("#")) {
324 const hexMatch = trimmed.match(/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/);
325 if (hexMatch) {
326 return hexToRgb(trimmed);
327 }
328 }
329
330 // Parse rgb/rgba colors
331 const rgbMatch = trimmed.match(
332 /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*([\d.]+))?\s*\)$/,
333 );
334 if (rgbMatch) {
335 return {
336 r: Number.parseInt(rgbMatch[1] ?? "0", 10),
337 g: Number.parseInt(rgbMatch[2] ?? "0", 10),
338 b: Number.parseInt(rgbMatch[3] ?? "0", 10),
339 a: rgbMatch[4] ? Number.parseFloat(rgbMatch[4]) : 1,
340 };
341 }
342
343 // Parse hsl/hsla colors
344 const hslMatch = trimmed.match(
345 /^hsla?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/,
346 );
347 if (hslMatch) {
348 const h = Number.parseInt(hslMatch[1] ?? "0", 10);
349 const s = Number.parseInt(hslMatch[2] ?? "0", 10) / 100;
350 const l = Number.parseInt(hslMatch[3] ?? "0", 10) / 100;
351 const a = hslMatch[4] ? Number.parseFloat(hslMatch[4]) : 1;
352
353 // Convert HSL to RGB
354 const c = (1 - Math.abs(2 * l - 1)) * s;
355 const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
356 const m = l - c / 2;
357
358 let r = 0;
359 let g = 0;
360 let b = 0;
361
362 if (h >= 0 && h < 60) {
363 r = c;
364 g = x;
365 b = 0;
366 } else if (h >= 60 && h < 120) {
367 r = x;
368 g = c;
369 b = 0;
370 } else if (h >= 120 && h < 180) {
371 r = 0;
372 g = c;
373 b = x;
374 } else if (h >= 180 && h < 240) {
375 r = 0;
376 g = x;
377 b = c;
378 } else if (h >= 240 && h < 300) {
379 r = x;
380 g = 0;
381 b = c;
382 } else if (h >= 300 && h < 360) {
383 r = c;
384 g = 0;
385 b = x;
386 }
387
388 return {
389 r: Math.round((r + m) * 255),
390 g: Math.round((g + m) * 255),
391 b: Math.round((b + m) * 255),
392 a,
393 };
394 }
395
396 // Parse hsb/hsba colors
397 const hsbMatch = trimmed.match(
398 /^hsba?\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*(?:,\s*([\d.]+))?\s*\)$/,
399 );
400 if (hsbMatch) {
401 const h = Number.parseInt(hsbMatch[1] ?? "0", 10);
402 const s = Number.parseInt(hsbMatch[2] ?? "0", 10);
403 const v = Number.parseInt(hsbMatch[3] ?? "0", 10);
404 const a = hsbMatch[4] ? Number.parseFloat(hsbMatch[4]) : 1;
405
406 return hsvToRgb({ h, s, v, a });
407 }
408
409 return null;
410}
411
412type Direction = "ltr" | "rtl";
413
414interface StoreState {
415 color: ColorValue;
416 hsv: HSVColorValue;
417 open: boolean;
418 format: ColorFormat;
419}
420
421interface Store {
422 subscribe: (cb: () => void) => () => void;
423 getState: () => StoreState;
424 setColor: (value: ColorValue) => void;
425 setHsv: (value: HSVColorValue) => void;
426 setOpen: (value: boolean) => void;
427 setFormat: (value: ColorFormat) => void;
428 notify: () => void;
429}
430
431const StoreContext = React.createContext<Store | null>(null);
432
433function useStoreContext(consumerName: string) {
434 const context = React.useContext(StoreContext);
435 if (!context) {
436 throw new Error(`\`${consumerName}\` must be used within \`${ROOT_NAME}\``);
437 }
438 return context;
439}
440
441function useStore<U>(selector: (state: StoreState) => U): U {
442 const store = useStoreContext("useStore");
443
444 const getSnapshot = React.useCallback(
445 () => selector(store.getState()),
446 [store, selector],
447 );
448
449 return React.useSyncExternalStore(store.subscribe, getSnapshot, getSnapshot);
450}
451
452interface ColorPickerContextValue {
453 dir: Direction;
454 disabled?: boolean;
455 inline?: boolean;
456 readOnly?: boolean;
457 required?: boolean;
458}
459
460const ColorPickerContext = React.createContext<ColorPickerContextValue | null>(
461 null,
462);
463
464function useColorPickerContext(consumerName: string) {
465 const context = React.useContext(ColorPickerContext);
466 if (!context) {
467 throw new Error(`\`${consumerName}\` must be used within \`${ROOT_NAME}\``);
468 }
469 return context;
470}
471
472interface ColorPickerProps
473 extends Omit<DivProps, "onValueChange">,
474 Pick<
475 React.ComponentProps<typeof Popover>,
476 "defaultOpen" | "open" | "onOpenChange" | "modal"
477 > {
478 value?: string;
479 defaultValue?: string;
480 onValueChange?: (value: string) => void;
481 dir?: Direction;
482 format?: ColorFormat;
483 defaultFormat?: ColorFormat;
484 onFormatChange?: (format: ColorFormat) => void;
485 name?: string;
486 asChild?: boolean;
487 disabled?: boolean;
488 inline?: boolean;
489 readOnly?: boolean;
490 required?: boolean;
491}
492
493function ColorPicker(props: ColorPickerProps) {
494 const {
495 value: valueProp,
496 defaultValue = "#000000",
497 onValueChange,
498 format: formatProp,
499 defaultFormat = "hex",
500 onFormatChange,
501 defaultOpen,
502 open: openProp,
503 onOpenChange,
504 name,
505 disabled,
506 inline,
507 readOnly,
508 required,
509 ...rootProps
510 } = props;
511
512 const listenersRef = useLazyRef(() => new Set<() => void>());
513 const stateRef = useLazyRef<StoreState>(() => {
514 const colorString = valueProp ?? defaultValue;
515 const color = hexToRgb(colorString);
516
517 return {
518 color,
519 hsv: rgbToHsv(color),
520 open: openProp ?? defaultOpen ?? false,
521 format: formatProp ?? defaultFormat,
522 };
523 });
524
525 const propsRef = useAsRef({
526 onValueChange,
527 onOpenChange,
528 onFormatChange,
529 });
530
531 const store = React.useMemo<Store>(() => {
532 return {
533 subscribe: (cb) => {
534 listenersRef.current.add(cb);
535 return () => listenersRef.current.delete(cb);
536 },
537 getState: () => stateRef.current,
538 setColor: (value: ColorValue) => {
539 if (Object.is(stateRef.current.color, value)) return;
540
541 const prevState = { ...stateRef.current };
542 stateRef.current.color = value;
543
544 if (propsRef.current.onValueChange) {
545 const colorString = colorToString(value, prevState.format);
546 propsRef.current.onValueChange(colorString);
547 }
548
549 store.notify();
550 },
551 setHsv: (value: HSVColorValue) => {
552 if (Object.is(stateRef.current.hsv, value)) return;
553
554 const prevState = { ...stateRef.current };
555 stateRef.current.hsv = value;
556
557 if (propsRef.current.onValueChange) {
558 const colorValue = hsvToRgb(value);
559 const colorString = colorToString(colorValue, prevState.format);
560 propsRef.current.onValueChange(colorString);
561 }
562
563 store.notify();
564 },
565 setOpen: (value: boolean) => {
566 if (Object.is(stateRef.current.open, value)) return;
567
568 stateRef.current.open = value;
569
570 if (propsRef.current.onOpenChange) {
571 propsRef.current.onOpenChange(value);
572 }
573
574 store.notify();
575 },
576 setFormat: (value: ColorFormat) => {
577 if (Object.is(stateRef.current.format, value)) return;
578
579 stateRef.current.format = value;
580
581 if (propsRef.current.onFormatChange) {
582 propsRef.current.onFormatChange(value);
583 }
584
585 store.notify();
586 },
587 notify: () => {
588 for (const cb of listenersRef.current) {
589 cb();
590 }
591 },
592 };
593 }, [listenersRef, stateRef, propsRef]);
594
595 return (
596 <StoreContext.Provider value={store}>
597 <ColorPickerImpl
598 {...rootProps}
599 value={valueProp}
600 defaultOpen={defaultOpen}
601 open={openProp}
602 name={name}
603 disabled={disabled}
604 inline={inline}
605 readOnly={readOnly}
606 required={required}
607 />
608 </StoreContext.Provider>
609 );
610}
611
612interface ColorPickerImplProps
613 extends Omit<
614 ColorPickerProps,
615 | "defaultValue"
616 | "onValueChange"
617 | "onOpenChange"
618 | "format"
619 | "defaultFormat"
620 | "onFormatChange"
621 > {}
622
623function ColorPickerImpl(props: ColorPickerImplProps) {
624 const {
625 value: valueProp,
626 dir: dirProp,
627 defaultOpen,
628 open: openProp,
629 name,
630 ref,
631 asChild,
632 disabled,
633 inline,
634 modal,
635 readOnly,
636 required,
637 ...rootProps
638 } = props;
639
640 const store = useStoreContext(ROOT_IMPL_NAME);
641
642 const dir = DirectionPrimitive.useDirection(dirProp);
643
644 const [formTrigger, setFormTrigger] = React.useState<RootElement | null>(
645 null,
646 );
647 const composedRef = useComposedRefs(ref, (node) => setFormTrigger(node));
648 const isFormControl = formTrigger ? !!formTrigger.closest("form") : true;
649
650 useIsomorphicLayoutEffect(() => {
651 if (valueProp !== undefined) {
652 const currentState = store.getState();
653 const color = hexToRgb(valueProp, currentState.color.a);
654 const hsv = rgbToHsv(color);
655 store.setColor(color);
656 store.setHsv(hsv);
657 }
658 }, [valueProp]);
659
660 useIsomorphicLayoutEffect(() => {
661 if (openProp !== undefined) {
662 store.setOpen(openProp);
663 }
664 }, [openProp]);
665
666 const contextValue = React.useMemo<ColorPickerContextValue>(
667 () => ({
668 dir,
669 disabled,
670 inline,
671 readOnly,
672 required,
673 }),
674 [dir, disabled, inline, readOnly, required],
675 );
676
677 const value = useStore((state) => rgbToHex(state.color));
678 const open = useStore((state) => state.open);
679
680 const RootPrimitive = asChild ? SlotPrimitive.Slot : "div";
681
682 if (inline) {
683 return (
684 <ColorPickerContext.Provider value={contextValue}>
685 <RootPrimitive {...rootProps} ref={composedRef} />
686 {isFormControl && (
687 <VisuallyHiddenInput
688 type="hidden"
689 control={formTrigger}
690 name={name}
691 value={value}
692 disabled={disabled}
693 readOnly={readOnly}
694 required={required}
695 />
696 )}
697 </ColorPickerContext.Provider>
698 );
699 }
700
701 return (
702 <ColorPickerContext.Provider value={contextValue}>
703 <Popover
704 defaultOpen={defaultOpen}
705 open={open}
706 onOpenChange={store.setOpen}
707 modal={modal}
708 >
709 <RootPrimitive {...rootProps} ref={composedRef} />
710 {isFormControl && (
711 <VisuallyHiddenInput
712 type="hidden"
713 control={formTrigger}
714 name={name}
715 value={value}
716 disabled={disabled}
717 readOnly={readOnly}
718 required={required}
719 />
720 )}
721 </Popover>
722 </ColorPickerContext.Provider>
723 );
724}
725
726function ColorPickerTrigger(
727 props: React.ComponentProps<typeof PopoverTrigger>,
728) {
729 const { asChild, disabled, ...triggerProps } = props;
730
731 const context = useColorPickerContext(TRIGGER_NAME);
732
733 const isDisabled = disabled || context.disabled;
734
735 const TriggerPrimitive = asChild ? SlotPrimitive.Slot : Button;
736
737 return (
738 <PopoverTrigger asChild disabled={isDisabled}>
739 <TriggerPrimitive data-slot="color-picker-trigger" {...triggerProps} />
740 </PopoverTrigger>
741 );
742}
743
744function ColorPickerContent(
745 props: React.ComponentProps<typeof PopoverContent>,
746) {
747 const { asChild, className, children, ...popoverContentProps } = props;
748
749 const context = useColorPickerContext(CONTENT_NAME);
750
751 if (context.inline) {
752 const ContentPrimitive = asChild ? SlotPrimitive.Slot : "div";
753
754 return (
755 <ContentPrimitive
756 data-slot="color-picker-content"
757 {...popoverContentProps}
758 className={cn("flex w-[340px] flex-col gap-4 p-4", className)}
759 >
760 {children}
761 </ContentPrimitive>
762 );
763 }
764
765 return (
766 <PopoverContent
767 data-slot="color-picker-content"
768 asChild={asChild}
769 {...popoverContentProps}
770 className={cn("flex w-[340px] flex-col gap-4 p-4", className)}
771 >
772 {children}
773 </PopoverContent>
774 );
775}
776
777function ColorPickerArea(props: DivProps) {
778 const {
779 asChild,
780 onPointerDown: onPointerDownProp,
781 onPointerMove: onPointerMoveProp,
782 onPointerUp: onPointerUpProp,
783 className,
784 ref,
785 ...areaProps
786 } = props;
787
788 const propsRef = useAsRef({
789 onPointerDown: onPointerDownProp,
790 onPointerMove: onPointerMoveProp,
791 onPointerUp: onPointerUpProp,
792 });
793
794 const context = useColorPickerContext(AREA_NAME);
795 const store = useStoreContext(AREA_NAME);
796
797 const hsv = useStore((state) => state.hsv);
798
799 const isDraggingRef = React.useRef(false);
800 const areaRef = React.useRef<HTMLDivElement>(null);
801 const composedRef = useComposedRefs(ref, areaRef);
802
803 const updateColorFromPosition = React.useCallback(
804 (clientX: number, clientY: number) => {
805 if (!areaRef.current) return;
806
807 const rect = areaRef.current.getBoundingClientRect();
808 const x = Math.max(0, Math.min(1, (clientX - rect.left) / rect.width));
809 const y = Math.max(
810 0,
811 Math.min(1, 1 - (clientY - rect.top) / rect.height),
812 );
813
814 const newHsv: HSVColorValue = {
815 h: hsv?.h ?? 0,
816 s: Math.round(x * 100),
817 v: Math.round(y * 100),
818 a: hsv?.a ?? 1,
819 };
820
821 store.setHsv(newHsv);
822 store.setColor(hsvToRgb(newHsv));
823 },
824 [hsv, store],
825 );
826
827 const onPointerDown = React.useCallback(
828 (event: React.PointerEvent<AreaElement>) => {
829 if (context.disabled) return;
830 propsRef.current.onPointerDown?.(event);
831 if (event.defaultPrevented) return;
832
833 isDraggingRef.current = true;
834 areaRef.current?.setPointerCapture(event.pointerId);
835 updateColorFromPosition(event.clientX, event.clientY);
836 },
837 [context.disabled, updateColorFromPosition, propsRef],
838 );
839
840 const onPointerMove = React.useCallback(
841 (event: React.PointerEvent<AreaElement>) => {
842 propsRef.current.onPointerMove?.(event);
843 if (event.defaultPrevented) return;
844
845 if (isDraggingRef.current) {
846 updateColorFromPosition(event.clientX, event.clientY);
847 }
848 },
849 [updateColorFromPosition, propsRef],
850 );
851
852 const onPointerUp = React.useCallback(
853 (event: React.PointerEvent<AreaElement>) => {
854 propsRef.current.onPointerUp?.(event);
855 if (event.defaultPrevented) return;
856
857 isDraggingRef.current = false;
858 areaRef.current?.releasePointerCapture(event.pointerId);
859 },
860 [propsRef],
861 );
862
863 const hue = hsv?.h ?? 0;
864 const backgroundHue = hsvToRgb({ h: hue, s: 100, v: 100, a: 1 });
865
866 const AreaPrimitive = asChild ? SlotPrimitive.Slot : "div";
867
868 return (
869 <AreaPrimitive
870 data-slot="color-picker-area"
871 {...areaProps}
872 className={cn(
873 "relative h-40 w-full cursor-crosshair touch-none rounded-sm border",
874 context.disabled && "pointer-events-none opacity-50",
875 className,
876 )}
877 ref={composedRef}
878 onPointerDown={onPointerDown}
879 onPointerMove={onPointerMove}
880 onPointerUp={onPointerUp}
881 >
882 <div className="absolute inset-0 overflow-hidden rounded-sm">
883 <div
884 className="absolute inset-0"
885 style={{
886 backgroundColor: `rgb(${backgroundHue.r}, ${backgroundHue.g}, ${backgroundHue.b})`,
887 }}
888 />
889 <div
890 className="absolute inset-0"
891 style={{
892 background: "linear-gradient(to right, #fff, transparent)",
893 }}
894 />
895 <div
896 className="absolute inset-0"
897 style={{
898 background: "linear-gradient(to bottom, transparent, #000)",
899 }}
900 />
901 </div>
902 <div
903 className="absolute size-3 -translate-x-1/2 -translate-y-1/2 rounded-full border-2 border-white shadow-sm"
904 style={{
905 left: `${hsv?.s ?? 0}%`,
906 top: `${100 - (hsv?.v ?? 0)}%`,
907 }}
908 />
909 </AreaPrimitive>
910 );
911}
912
913function ColorPickerHueSlider(
914 props: React.ComponentProps<typeof SliderPrimitive.Root>,
915) {
916 const { className, ...sliderProps } = props;
917
918 const context = useColorPickerContext(HUE_SLIDER_NAME);
919 const store = useStoreContext(HUE_SLIDER_NAME);
920
921 const hsv = useStore((state) => state.hsv);
922
923 const onValueChange = React.useCallback(
924 (values: number[]) => {
925 const newHsv: HSVColorValue = {
926 h: values[0] ?? 0,
927 s: hsv?.s ?? 0,
928 v: hsv?.v ?? 0,
929 a: hsv?.a ?? 1,
930 };
931 store.setHsv(newHsv);
932 store.setColor(hsvToRgb(newHsv));
933 },
934 [hsv, store],
935 );
936
937 return (
938 <SliderPrimitive.Root
939 data-slot="color-picker-hue-slider"
940 {...sliderProps}
941 max={360}
942 step={1}
943 className={cn(
944 "relative flex w-full touch-none select-none items-center",
945 className,
946 )}
947 value={[hsv?.h ?? 0]}
948 onValueChange={onValueChange}
949 disabled={context.disabled}
950 >
951 <SliderPrimitive.Track className="relative h-3 w-full grow overflow-hidden rounded-full bg-[linear-gradient(to_right,#ff0000_0%,#ffff00_16.66%,#00ff00_33.33%,#00ffff_50%,#0000ff_66.66%,#ff00ff_83.33%,#ff0000_100%)]">
952 <SliderPrimitive.Range className="absolute h-full" />
953 </SliderPrimitive.Track>
954 <SliderPrimitive.Thumb className="block size-4 rounded-full border border-primary/50 bg-background shadow transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50" />
955 </SliderPrimitive.Root>
956 );
957}
958
959function ColorPickerAlphaSlider(
960 props: React.ComponentProps<typeof SliderPrimitive.Root>,
961) {
962 const { className, ...sliderProps } = props;
963
964 const context = useColorPickerContext(ALPHA_SLIDER_NAME);
965 const store = useStoreContext(ALPHA_SLIDER_NAME);
966
967 const color = useStore((state) => state.color);
968 const hsv = useStore((state) => state.hsv);
969
970 const onValueChange = React.useCallback(
971 (values: number[]) => {
972 const alpha = (values[0] ?? 0) / 100;
973 const newColor = { ...color, a: alpha };
974 const newHsv = { ...hsv, a: alpha };
975 store.setColor(newColor);
976 store.setHsv(newHsv);
977 },
978 [color, hsv, store],
979 );
980
981 const gradientColor = `rgb(${color?.r ?? 0}, ${color?.g ?? 0}, ${color?.b ?? 0})`;
982
983 return (
984 <SliderPrimitive.Root
985 data-slot="color-picker-alpha-slider"
986 {...sliderProps}
987 max={100}
988 step={1}
989 disabled={context.disabled}
990 className={cn(
991 "relative flex w-full touch-none select-none items-center",
992 className,
993 )}
994 value={[Math.round((color?.a ?? 1) * 100)]}
995 onValueChange={onValueChange}
996 >
997 <SliderPrimitive.Track
998 className="relative h-3 w-full grow overflow-hidden rounded-full"
999 style={{
1000 background:
1001 "linear-gradient(45deg, #ccc 25%, transparent 25%), linear-gradient(-45deg, #ccc 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ccc 75%), linear-gradient(-45deg, transparent 75%, #ccc 75%)",
1002 backgroundSize: "8px 8px",
1003 backgroundPosition: "0 0, 0 4px, 4px -4px, -4px 0px",
1004 }}
1005 >
1006 <div
1007 className="absolute inset-0 rounded-full"
1008 style={{
1009 background: `linear-gradient(to right, transparent, ${gradientColor})`,
1010 }}
1011 />
1012 <SliderPrimitive.Range className="absolute h-full" />
1013 </SliderPrimitive.Track>
1014 <SliderPrimitive.Thumb className="block size-4 rounded-full border border-primary/50 bg-background shadow transition-colors focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:pointer-events-none disabled:opacity-50" />
1015 </SliderPrimitive.Root>
1016 );
1017}
1018
1019function ColorPickerSwatch(props: DivProps) {
1020 const { asChild, className, ...swatchProps } = props;
1021
1022 const context = useColorPickerContext(SWATCH_NAME);
1023
1024 const color = useStore((state) => state.color);
1025 const format = useStore((state) => state.format);
1026
1027 const backgroundStyle = React.useMemo(() => {
1028 if (!color) {
1029 return {
1030 background:
1031 "linear-gradient(to bottom right, transparent calc(50% - 1px), hsl(var(--destructive)) calc(50% - 1px) calc(50% + 1px), transparent calc(50% + 1px)) no-repeat",
1032 };
1033 }
1034
1035 const colorString = `rgba(${color.r}, ${color.g}, ${color.b}, ${color.a})`;
1036
1037 if (color.a < 1) {
1038 return {
1039 background: `linear-gradient(${colorString}, ${colorString}), repeating-conic-gradient(#ccc 0% 25%, #fff 0% 50%) 0% 50% / 8px 8px`,
1040 };
1041 }
1042
1043 return {
1044 backgroundColor: colorString,
1045 };
1046 }, [color]);
1047
1048 const ariaLabel = !color
1049 ? "No color selected"
1050 : `Current color: ${colorToString(color, format)}`;
1051
1052 const SwatchPrimitive = asChild ? SlotPrimitive.Slot : "div";
1053
1054 return (
1055 <SwatchPrimitive
1056 role="img"
1057 aria-label={ariaLabel}
1058 data-slot="color-picker-swatch"
1059 {...swatchProps}
1060 className={cn(
1061 "box-border size-8 rounded-sm border shadow-sm",
1062 context.disabled && "opacity-50",
1063 className,
1064 )}
1065 style={{
1066 ...backgroundStyle,
1067 forcedColorAdjust: "none",
1068 }}
1069 />
1070 );
1071}
1072
1073function ColorPickerEyeDropper(props: React.ComponentProps<typeof Button>) {
1074 const { size: sizeProp, children, disabled, ...buttonProps } = props;
1075
1076 const context = useColorPickerContext(EYE_DROPPER_NAME);
1077 const store = useStoreContext(EYE_DROPPER_NAME);
1078
1079 const color = useStore((state) => state.color);
1080
1081 const isDisabled = disabled || context.disabled;
1082
1083 const onEyeDropper = React.useCallback(async () => {
1084 if (!window.EyeDropper) return;
1085
1086 try {
1087 const eyeDropper = new window.EyeDropper();
1088 const result = await eyeDropper.open();
1089
1090 if (result.sRGBHex) {
1091 const currentAlpha = color?.a ?? 1;
1092 const newColor = hexToRgb(result.sRGBHex, currentAlpha);
1093 const newHsv = rgbToHsv(newColor);
1094 store.setColor(newColor);
1095 store.setHsv(newHsv);
1096 }
1097 } catch (error) {
1098 console.warn("EyeDropper error:", error);
1099 }
1100 }, [color, store]);
1101
1102 const hasEyeDropper = typeof window !== "undefined" && !!window.EyeDropper;
1103
1104 if (!hasEyeDropper) return null;
1105
1106 const size = sizeProp ?? (children ? "default" : "icon");
1107
1108 return (
1109 <Button
1110 data-slot="color-picker-eye-dropper"
1111 {...buttonProps}
1112 variant="outline"
1113 size={size}
1114 onClick={onEyeDropper}
1115 disabled={isDisabled}
1116 >
1117 {children ?? <PipetteIcon />}
1118 </Button>
1119 );
1120}
1121
1122interface ColorPickerFormatSelectProps
1123 extends Omit<React.ComponentProps<typeof Select>, "value" | "onValueChange">,
1124 Pick<React.ComponentProps<typeof SelectTrigger>, "size" | "className"> {}
1125
1126function ColorPickerFormatSelect(props: ColorPickerFormatSelectProps) {
1127 const { size, disabled, className, ...selectProps } = props;
1128
1129 const context = useColorPickerContext(FORMAT_SELECT_NAME);
1130 const store = useStoreContext(FORMAT_SELECT_NAME);
1131 const isDisabled = disabled || context.disabled;
1132
1133 const format = useStore((state) => state.format);
1134
1135 const onFormatChange = React.useCallback(
1136 (value: ColorFormat) => {
1137 store.setFormat(value);
1138 },
1139 [store],
1140 );
1141
1142 return (
1143 <Select
1144 data-slot="color-picker-format-select"
1145 {...selectProps}
1146 value={format}
1147 onValueChange={onFormatChange}
1148 disabled={isDisabled}
1149 >
1150 <SelectTrigger
1151 data-slot="color-picker-format-select-trigger"
1152 size={size ?? "sm"}
1153 className={cn(className)}
1154 >
1155 <SelectValue />
1156 </SelectTrigger>
1157 <SelectContent>
1158 {colorFormats.map((format) => (
1159 <SelectItem key={format} value={format}>
1160 {format.toUpperCase()}
1161 </SelectItem>
1162 ))}
1163 </SelectContent>
1164 </Select>
1165 );
1166}
1167
1168interface ColorPickerInputProps
1169 extends Omit<
1170 React.ComponentProps<typeof Input>,
1171 "value" | "onChange" | "color"
1172 > {
1173 withoutAlpha?: boolean;
1174}
1175
1176function ColorPickerInput(props: ColorPickerInputProps) {
1177 const store = useStoreContext(INPUT_NAME);
1178 const context = useColorPickerContext(INPUT_NAME);
1179
1180 const color = useStore((state) => state.color);
1181 const format = useStore((state) => state.format);
1182 const hsv = useStore((state) => state.hsv);
1183
1184 const onColorChange = React.useCallback(
1185 (newColor: ColorValue) => {
1186 const newHsv = rgbToHsv(newColor);
1187 store.setColor(newColor);
1188 store.setHsv(newHsv);
1189 },
1190 [store],
1191 );
1192
1193 if (format === "hex") {
1194 return (
1195 <HexInput
1196 color={color}
1197 onColorChange={onColorChange}
1198 context={context}
1199 {...props}
1200 />
1201 );
1202 }
1203
1204 if (format === "rgb") {
1205 return (
1206 <RgbInput
1207 color={color}
1208 onColorChange={onColorChange}
1209 context={context}
1210 {...props}
1211 />
1212 );
1213 }
1214
1215 if (format === "hsl") {
1216 return (
1217 <HslInput
1218 color={color}
1219 onColorChange={onColorChange}
1220 context={context}
1221 {...props}
1222 />
1223 );
1224 }
1225
1226 if (format === "hsb") {
1227 return (
1228 <HsbInput
1229 hsv={hsv}
1230 onColorChange={onColorChange}
1231 context={context}
1232 {...props}
1233 />
1234 );
1235 }
1236}
1237
1238const inputGroupItemVariants = cva(
1239 "h-8 [-moz-appearance:textfield] focus-visible:z-10 focus-visible:ring-1 [&::-webkit-inner-spin-button]:m-0 [&::-webkit-inner-spin-button]:appearance-none [&::-webkit-outer-spin-button]:m-0 [&::-webkit-outer-spin-button]:appearance-none",
1240 {
1241 variants: {
1242 position: {
1243 first: "rounded-e-none",
1244 middle: "-ms-px rounded-none border-l-0",
1245 last: "-ms-px rounded-s-none border-l-0",
1246 isolated: "",
1247 },
1248 },
1249 defaultVariants: {
1250 position: "isolated",
1251 },
1252 },
1253);
1254
1255interface InputGroupItemProps
1256 extends React.ComponentProps<typeof Input>,
1257 VariantProps<typeof inputGroupItemVariants> {}
1258
1259function InputGroupItem({
1260 className,
1261 position,
1262 ...props
1263}: InputGroupItemProps) {
1264 return (
1265 <Input
1266 data-slot="color-picker-input"
1267 className={cn(inputGroupItemVariants({ position, className }))}
1268 {...props}
1269 />
1270 );
1271}
1272
1273interface FormatInputProps extends ColorPickerInputProps {
1274 color: ColorValue;
1275 onColorChange: (color: ColorValue) => void;
1276 context: ColorPickerContextValue;
1277}
1278
1279function HexInput(props: FormatInputProps) {
1280 const {
1281 color,
1282 onColorChange,
1283 context,
1284 withoutAlpha,
1285 className,
1286 ...inputProps
1287 } = props;
1288
1289 const hexValue = rgbToHex(color);
1290 const alphaValue = Math.round((color?.a ?? 1) * 100);
1291
1292 const onHexChange = React.useCallback(
1293 (event: React.ChangeEvent<InputElement>) => {
1294 const value = event.target.value;
1295 const parsedColor = parseColorString(value);
1296 if (parsedColor) {
1297 onColorChange({ ...parsedColor, a: color?.a ?? 1 });
1298 }
1299 },
1300 [color, onColorChange],
1301 );
1302
1303 const onAlphaChange = React.useCallback(
1304 (event: React.ChangeEvent<InputElement>) => {
1305 const value = Number.parseInt(event.target.value, 10);
1306 if (!Number.isNaN(value) && value >= 0 && value <= 100) {
1307 onColorChange({ ...color, a: value / 100 });
1308 }
1309 },
1310 [color, onColorChange],
1311 );
1312
1313 if (withoutAlpha) {
1314 return (
1315 <InputGroupItem
1316 aria-label="Hex color value"
1317 position="isolated"
1318 {...inputProps}
1319 placeholder="#000000"
1320 className={cn("font-mono", className)}
1321 value={hexValue}
1322 onChange={onHexChange}
1323 disabled={context.disabled}
1324 />
1325 );
1326 }
1327
1328 return (
1329 <div
1330 data-slot="color-picker-input-wrapper"
1331 className={cn("flex items-center", className)}
1332 >
1333 <InputGroupItem
1334 aria-label="Hex color value"
1335 position="first"
1336 {...inputProps}
1337 placeholder="#000000"
1338 className="flex-1 font-mono"
1339 value={hexValue}
1340 onChange={onHexChange}
1341 disabled={context.disabled}
1342 />
1343 <InputGroupItem
1344 aria-label="Alpha transparency percentage"
1345 position="last"
1346 {...inputProps}
1347 placeholder="100"
1348 inputMode="numeric"
1349 pattern="[0-9]*"
1350 min="0"
1351 max="100"
1352 className="w-14"
1353 value={alphaValue}
1354 onChange={onAlphaChange}
1355 disabled={context.disabled}
1356 />
1357 </div>
1358 );
1359}
1360
1361function RgbInput(props: FormatInputProps) {
1362 const {
1363 color,
1364 onColorChange,
1365 context,
1366 withoutAlpha,
1367 className,
1368 ...inputProps
1369 } = props;
1370
1371 const rValue = Math.round(color?.r ?? 0);
1372 const gValue = Math.round(color?.g ?? 0);
1373 const bValue = Math.round(color?.b ?? 0);
1374 const alphaValue = Math.round((color?.a ?? 1) * 100);
1375
1376 const onChannelChange = React.useCallback(
1377 (channel: "r" | "g" | "b" | "a", max: number, isAlpha = false) =>
1378 (event: React.ChangeEvent<InputElement>) => {
1379 const value = Number.parseInt(event.target.value, 10);
1380 if (!Number.isNaN(value) && value >= 0 && value <= max) {
1381 const newValue = isAlpha ? value / 100 : value;
1382 onColorChange({ ...color, [channel]: newValue });
1383 }
1384 },
1385 [color, onColorChange],
1386 );
1387
1388 return (
1389 <div
1390 data-slot="color-picker-input-wrapper"
1391 className={cn("flex items-center", className)}
1392 >
1393 <InputGroupItem
1394 aria-label="Red color component (0-255)"
1395 position="first"
1396 {...inputProps}
1397 placeholder="0"
1398 inputMode="numeric"
1399 pattern="[0-9]*"
1400 min="0"
1401 max="255"
1402 className="w-14"
1403 value={rValue}
1404 onChange={onChannelChange("r", 255)}
1405 disabled={context.disabled}
1406 />
1407 <InputGroupItem
1408 aria-label="Green color component (0-255)"
1409 position="middle"
1410 {...inputProps}
1411 placeholder="0"
1412 inputMode="numeric"
1413 pattern="[0-9]*"
1414 min="0"
1415 max="255"
1416 className="w-14"
1417 value={gValue}
1418 onChange={onChannelChange("g", 255)}
1419 disabled={context.disabled}
1420 />
1421 <InputGroupItem
1422 aria-label="Blue color component (0-255)"
1423 position={withoutAlpha ? "last" : "middle"}
1424 {...inputProps}
1425 placeholder="0"
1426 inputMode="numeric"
1427 pattern="[0-9]*"
1428 min="0"
1429 max="255"
1430 className="w-14"
1431 value={bValue}
1432 onChange={onChannelChange("b", 255)}
1433 disabled={context.disabled}
1434 />
1435 {!withoutAlpha && (
1436 <InputGroupItem
1437 aria-label="Alpha transparency percentage"
1438 position="last"
1439 {...inputProps}
1440 placeholder="100"
1441 inputMode="numeric"
1442 pattern="[0-9]*"
1443 min="0"
1444 max="100"
1445 className="w-14"
1446 value={alphaValue}
1447 onChange={onChannelChange("a", 100, true)}
1448 disabled={context.disabled}
1449 />
1450 )}
1451 </div>
1452 );
1453}
1454
1455function HslInput(props: FormatInputProps) {
1456 const {
1457 color,
1458 onColorChange,
1459 context,
1460 withoutAlpha,
1461 className,
1462 ...inputProps
1463 } = props;
1464
1465 const hsl = React.useMemo(() => rgbToHsl(color), [color]);
1466 const alphaValue = Math.round((color?.a ?? 1) * 100);
1467
1468 const onHslChannelChange = React.useCallback(
1469 (channel: "h" | "s" | "l", max: number) =>
1470 (event: React.ChangeEvent<InputElement>) => {
1471 const value = Number.parseInt(event.target.value, 10);
1472 if (!Number.isNaN(value) && value >= 0 && value <= max) {
1473 const newHsl = { ...hsl, [channel]: value };
1474 const newColor = hslToRgb(newHsl, color?.a ?? 1);
1475 onColorChange(newColor);
1476 }
1477 },
1478 [hsl, color, onColorChange],
1479 );
1480
1481 const onAlphaChange = React.useCallback(
1482 (event: React.ChangeEvent<InputElement>) => {
1483 const value = Number.parseInt(event.target.value, 10);
1484 if (!Number.isNaN(value) && value >= 0 && value <= 100) {
1485 onColorChange({ ...color, a: value / 100 });
1486 }
1487 },
1488 [color, onColorChange],
1489 );
1490
1491 return (
1492 <div
1493 data-slot="color-picker-input-wrapper"
1494 className={cn("flex items-center", className)}
1495 >
1496 <InputGroupItem
1497 aria-label="Hue degree (0-360)"
1498 position="first"
1499 {...inputProps}
1500 placeholder="0"
1501 inputMode="numeric"
1502 pattern="[0-9]*"
1503 min="0"
1504 max="360"
1505 className="w-14"
1506 value={hsl.h}
1507 onChange={onHslChannelChange("h", 360)}
1508 disabled={context.disabled}
1509 />
1510 <InputGroupItem
1511 aria-label="Saturation percentage (0-100)"
1512 position="middle"
1513 {...inputProps}
1514 placeholder="0"
1515 inputMode="numeric"
1516 pattern="[0-9]*"
1517 min="0"
1518 max="100"
1519 className="w-14"
1520 value={hsl.s}
1521 onChange={onHslChannelChange("s", 100)}
1522 disabled={context.disabled}
1523 />
1524 <InputGroupItem
1525 aria-label="Lightness percentage (0-100)"
1526 position={withoutAlpha ? "last" : "middle"}
1527 {...inputProps}
1528 placeholder="0"
1529 inputMode="numeric"
1530 pattern="[0-9]*"
1531 min="0"
1532 max="100"
1533 className="w-14"
1534 value={hsl.l}
1535 onChange={onHslChannelChange("l", 100)}
1536 disabled={context.disabled}
1537 />
1538 {!withoutAlpha && (
1539 <InputGroupItem
1540 aria-label="Alpha transparency percentage"
1541 position="last"
1542 {...inputProps}
1543 placeholder="100"
1544 inputMode="numeric"
1545 pattern="[0-9]*"
1546 min="0"
1547 max="100"
1548 className="w-14"
1549 value={alphaValue}
1550 onChange={onAlphaChange}
1551 disabled={context.disabled}
1552 />
1553 )}
1554 </div>
1555 );
1556}
1557
1558interface HsbInputProps extends Omit<FormatInputProps, "color"> {
1559 hsv: HSVColorValue;
1560}
1561
1562function HsbInput(props: HsbInputProps) {
1563 const {
1564 hsv,
1565 onColorChange,
1566 context,
1567 withoutAlpha,
1568 className,
1569 ...inputProps
1570 } = props;
1571
1572 const alphaValue = Math.round((hsv?.a ?? 1) * 100);
1573
1574 const onHsvChannelChange = React.useCallback(
1575 (channel: "h" | "s" | "v", max: number) =>
1576 (event: React.ChangeEvent<InputElement>) => {
1577 const value = Number.parseInt(event.target.value, 10);
1578 if (!Number.isNaN(value) && value >= 0 && value <= max) {
1579 const newHsv = { ...hsv, [channel]: value };
1580 const newColor = hsvToRgb(newHsv);
1581 onColorChange(newColor);
1582 }
1583 },
1584 [hsv, onColorChange],
1585 );
1586
1587 const onAlphaChange = React.useCallback(
1588 (event: React.ChangeEvent<InputElement>) => {
1589 const value = Number.parseInt(event.target.value, 10);
1590 if (!Number.isNaN(value) && value >= 0 && value <= 100) {
1591 const currentColor = hsvToRgb(hsv);
1592 onColorChange({ ...currentColor, a: value / 100 });
1593 }
1594 },
1595 [hsv, onColorChange],
1596 );
1597
1598 return (
1599 <div
1600 data-slot="color-picker-input-wrapper"
1601 className={cn("flex items-center", className)}
1602 >
1603 <InputGroupItem
1604 aria-label="Hue degree (0-360)"
1605 position="first"
1606 {...inputProps}
1607 placeholder="0"
1608 inputMode="numeric"
1609 pattern="[0-9]*"
1610 min="0"
1611 max="360"
1612 className="w-14"
1613 value={hsv?.h ?? 0}
1614 onChange={onHsvChannelChange("h", 360)}
1615 disabled={context.disabled}
1616 />
1617 <InputGroupItem
1618 aria-label="Saturation percentage (0-100)"
1619 position="middle"
1620 {...inputProps}
1621 placeholder="0"
1622 inputMode="numeric"
1623 pattern="[0-9]*"
1624 min="0"
1625 max="100"
1626 className="w-14"
1627 value={hsv?.s ?? 0}
1628 onChange={onHsvChannelChange("s", 100)}
1629 disabled={context.disabled}
1630 />
1631 <InputGroupItem
1632 aria-label="Brightness percentage (0-100)"
1633 position={withoutAlpha ? "last" : "middle"}
1634 {...inputProps}
1635 placeholder="0"
1636 inputMode="numeric"
1637 pattern="[0-9]*"
1638 min="0"
1639 max="100"
1640 className="w-14"
1641 value={hsv?.v ?? 0}
1642 onChange={onHsvChannelChange("v", 100)}
1643 disabled={context.disabled}
1644 />
1645 {!withoutAlpha && (
1646 <InputGroupItem
1647 aria-label="Alpha transparency percentage"
1648 position="last"
1649 {...inputProps}
1650 placeholder="100"
1651 inputMode="numeric"
1652 pattern="[0-9]*"
1653 min="0"
1654 max="100"
1655 className="w-14"
1656 value={alphaValue}
1657 onChange={onAlphaChange}
1658 disabled={context.disabled}
1659 />
1660 )}
1661 </div>
1662 );
1663}
1664
1665export {
1666 ColorPicker,
1667 ColorPickerAlphaSlider,
1668 ColorPickerArea,
1669 ColorPickerContent,
1670 ColorPickerEyeDropper,
1671 ColorPickerFormatSelect,
1672 ColorPickerHueSlider,
1673 ColorPickerInput,
1674 type ColorPickerProps,
1675 ColorPickerSwatch,
1676 ColorPickerTrigger,
1677 useStore as useColorPicker,
1678};
1679