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