|
| 1 | +import { useState } from "react"; |
| 2 | +import { HexColorPicker } from "react-colorful"; |
| 3 | + |
| 4 | +import { useDebouncedState } from "@/hooks/useDebouncedState"; |
| 5 | +import { cn } from "@/lib/utils"; |
| 6 | + |
| 7 | +import { Button } from "./button"; |
| 8 | +import { |
| 9 | + Collapsible, |
| 10 | + CollapsibleContent, |
| 11 | + CollapsibleTrigger, |
| 12 | +} from "./collapsible"; |
| 13 | +import { Icon } from "./icon"; |
| 14 | +import { Input } from "./input"; |
| 15 | +import { BlockStack, InlineStack } from "./layout"; |
| 16 | +import { Popover, PopoverContent, PopoverTrigger } from "./popover"; |
| 17 | +import { Heading } from "./typography"; |
| 18 | + |
| 19 | +const PRESET_COLORS = [ |
| 20 | + "#FFF9C4", |
| 21 | + "#C8E6C9", |
| 22 | + "#BBDEFB", |
| 23 | + "#D1C4E9", |
| 24 | + "#FFE0B2", |
| 25 | + "#EF9A9A", |
| 26 | + "#FFCCBC", |
| 27 | + "#D7CCC8", |
| 28 | + "#F5F5F5", |
| 29 | + "#CFD8DC", |
| 30 | + "#B0BEC5", |
| 31 | + "transparent", |
| 32 | +]; |
| 33 | + |
| 34 | +interface ColorPickerProps { |
| 35 | + title?: string; |
| 36 | + color: string; |
| 37 | + debounceMs?: number; |
| 38 | + setColor: (color: string) => void; |
| 39 | + onClose?: () => void; |
| 40 | +} |
| 41 | + |
| 42 | +export const ColorPicker = ({ |
| 43 | + color, |
| 44 | + title, |
| 45 | + debounceMs = 300, |
| 46 | + setColor, |
| 47 | + onClose, |
| 48 | +}: ColorPickerProps) => { |
| 49 | + const [open, setOpen] = useState(false); |
| 50 | + const [localColor, setLocalColor] = useState(color); |
| 51 | + |
| 52 | + const { clearDebounce, updatePreviousState } = useDebouncedState( |
| 53 | + localColor, |
| 54 | + setColor, |
| 55 | + () => false, |
| 56 | + { debounceMs }, |
| 57 | + ); |
| 58 | + |
| 59 | + const handleOpenChange = (isOpen: boolean) => { |
| 60 | + setOpen(isOpen); |
| 61 | + if (!isOpen) { |
| 62 | + onClose?.(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + const handlePresetClick = (preset: string) => { |
| 67 | + clearDebounce(); |
| 68 | + setLocalColor(preset); |
| 69 | + setColor(preset); |
| 70 | + updatePreviousState(preset); |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <Popover open={open} onOpenChange={handleOpenChange}> |
| 75 | + <PopoverTrigger> |
| 76 | + <div |
| 77 | + className="aspect-square h-4 rounded-full border border-muted-foreground cursor-pointer" |
| 78 | + style={{ backgroundColor: color }} |
| 79 | + /> |
| 80 | + </PopoverTrigger> |
| 81 | + <PopoverContent className="w-fit"> |
| 82 | + <BlockStack gap="4" align="center"> |
| 83 | + <Heading level={3}>{title ?? "Pick a color"}</Heading> |
| 84 | + <InlineStack gap="2" className="grid grid-cols-6 px-2"> |
| 85 | + {PRESET_COLORS.map((preset) => ( |
| 86 | + <div |
| 87 | + key={preset} |
| 88 | + className={cn( |
| 89 | + "aspect-square w-6 rounded-sm border border-muted-foreground cursor-pointer relative overflow-hidden", |
| 90 | + { |
| 91 | + "ring-2 ring-offset-2 ring-black": preset === color, |
| 92 | + }, |
| 93 | + )} |
| 94 | + style={{ backgroundColor: preset }} |
| 95 | + onClick={() => handlePresetClick(preset)} |
| 96 | + > |
| 97 | + {preset === "transparent" && ( |
| 98 | + <div className="absolute inset-0 flex items-center justify-center"> |
| 99 | + <div className="h-px w-full bg-red-500 rotate-45 origin-center" /> |
| 100 | + </div> |
| 101 | + )} |
| 102 | + </div> |
| 103 | + ))} |
| 104 | + </InlineStack> |
| 105 | + <div className="relative w-full"> |
| 106 | + <Input |
| 107 | + value={localColor} |
| 108 | + onChange={(e) => setLocalColor(e.target.value)} |
| 109 | + /> |
| 110 | + <Collapsible> |
| 111 | + <InlineStack blockAlign="center" gap="1"> |
| 112 | + <CollapsibleTrigger asChild> |
| 113 | + <Button |
| 114 | + size="icon" |
| 115 | + variant="ghost" |
| 116 | + className="absolute right-2 top-0 hover:bg-transparent hover:text-black!" |
| 117 | + style={{ |
| 118 | + color: |
| 119 | + localColor === "transparent" ? "lightgray" : localColor, |
| 120 | + filter: "brightness(0.8)", |
| 121 | + }} |
| 122 | + > |
| 123 | + <Icon name="Palette" /> |
| 124 | + <span className="sr-only">Toggle</span> |
| 125 | + </Button> |
| 126 | + </CollapsibleTrigger> |
| 127 | + </InlineStack> |
| 128 | + <CollapsibleContent className="mt-2"> |
| 129 | + <HexColorPicker color={localColor} onChange={setLocalColor} /> |
| 130 | + </CollapsibleContent> |
| 131 | + </Collapsible> |
| 132 | + </div> |
| 133 | + </BlockStack> |
| 134 | + </PopoverContent> |
| 135 | + </Popover> |
| 136 | + ); |
| 137 | +}; |
0 commit comments