|
| 1 | +import { useRef, useState, useEffect } from 'react'; |
| 2 | +import { useAppStore } from '@/stores/appStore'; |
| 3 | + |
| 4 | +const PRESET_BACKGROUNDS = [ |
| 5 | + { id: 'transparent', label: 'None', value: null, type: 'transparent' }, |
| 6 | + { id: 'white', label: 'White', value: '#ffffff', type: 'color' }, |
| 7 | + { id: 'black', label: 'Black', value: '#000000', type: 'color' }, |
| 8 | + { id: 'mist', label: 'Mist', value: '#e8e8e8', type: 'color' }, |
| 9 | + { id: 'sky', label: 'Sky', value: '#87ceeb', type: 'color' }, |
| 10 | + { id: 'honeydew', label: 'Honeydew', value: '#f0fff0', type: 'color' }, |
| 11 | + { id: 'ivory', label: 'Ivory', value: '#fffff0', type: 'color' }, |
| 12 | + { id: 'lavender', label: 'Lavender', value: '#e6e6fa', type: 'color' }, |
| 13 | + { id: 'cyan', label: 'Cyan', value: '#e0ffff', type: 'color' }, |
| 14 | + { id: 'mint', label: 'Mint', value: '#98ff98', type: 'color' }, |
| 15 | + { id: 'peach', label: 'Peach', value: '#ffdab9', type: 'color' }, |
| 16 | + { id: 'rose', label: 'Rose', value: '#ffe4e1', type: 'color' }, |
| 17 | +] as const; |
| 18 | + |
| 19 | +export default function BackgroundSelector() { |
| 20 | + const fileInputRef = useRef<HTMLInputElement>(null); |
| 21 | + |
| 22 | + const backgroundType = useAppStore((state) => state.backgroundType); |
| 23 | + const backgroundColor = useAppStore((state) => state.backgroundColor); |
| 24 | + const setBackgroundType = useAppStore((state) => state.setBackgroundType); |
| 25 | + const setBackgroundColor = useAppStore((state) => state.setBackgroundColor); |
| 26 | + const setBackgroundImage = useAppStore((state) => state.setBackgroundImage); |
| 27 | + const setHasUnsavedEdits = useAppStore((state) => state.setHasUnsavedEdits); |
| 28 | + |
| 29 | + // Initialize customColor from current backgroundColor if type is 'color' |
| 30 | + const [customColor, setCustomColor] = useState( |
| 31 | + backgroundType === 'color' ? backgroundColor : '#ffffff' |
| 32 | + ); |
| 33 | + |
| 34 | + // Sync customColor when backgroundColor changes externally |
| 35 | + useEffect(() => { |
| 36 | + if (backgroundType === 'color') { |
| 37 | + setCustomColor(backgroundColor); |
| 38 | + } |
| 39 | + }, [backgroundColor, backgroundType]); |
| 40 | + |
| 41 | + function handlePresetClick(preset: (typeof PRESET_BACKGROUNDS)[number]) { |
| 42 | + if (preset.type === 'transparent') { |
| 43 | + setBackgroundType('transparent'); |
| 44 | + } else { |
| 45 | + setBackgroundType('color'); |
| 46 | + setBackgroundColor(preset.value!); |
| 47 | + } |
| 48 | + setHasUnsavedEdits(true); |
| 49 | + } |
| 50 | + |
| 51 | + function handleCustomColorChange(e: React.ChangeEvent<HTMLInputElement>) { |
| 52 | + const color = e.target.value; |
| 53 | + setCustomColor(color); |
| 54 | + setBackgroundType('color'); |
| 55 | + setBackgroundColor(color); |
| 56 | + setHasUnsavedEdits(true); |
| 57 | + } |
| 58 | + |
| 59 | + function handleUploadClick() { |
| 60 | + fileInputRef.current?.click(); |
| 61 | + } |
| 62 | + |
| 63 | + function handleFileSelect(e: React.ChangeEvent<HTMLInputElement>) { |
| 64 | + const file = e.target.files?.[0]; |
| 65 | + if (!file) return; |
| 66 | + |
| 67 | + const img = new Image(); |
| 68 | + const objectUrl = URL.createObjectURL(file); |
| 69 | + img.src = objectUrl; |
| 70 | + |
| 71 | + img.onload = () => { |
| 72 | + URL.revokeObjectURL(objectUrl); |
| 73 | + setBackgroundType('image'); |
| 74 | + setBackgroundImage(img); |
| 75 | + setHasUnsavedEdits(true); |
| 76 | + }; |
| 77 | + |
| 78 | + img.onerror = () => { |
| 79 | + URL.revokeObjectURL(objectUrl); |
| 80 | + console.error('Failed to load background image'); |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + function isSelected(preset: (typeof PRESET_BACKGROUNDS)[number]): boolean { |
| 85 | + if (preset.type === 'transparent') { |
| 86 | + return backgroundType === 'transparent'; |
| 87 | + } |
| 88 | + return backgroundType === 'color' && backgroundColor === preset.value; |
| 89 | + } |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="space-y-4"> |
| 93 | + {/* Section Header */} |
| 94 | + <h3 className="text-sm font-semibold text-gray-700">Background</h3> |
| 95 | + |
| 96 | + {/* Preset Swatches Grid */} |
| 97 | + <div className="grid grid-cols-6 gap-2"> |
| 98 | + {PRESET_BACKGROUNDS.map((preset) => ( |
| 99 | + <button |
| 100 | + key={preset.id} |
| 101 | + onClick={() => handlePresetClick(preset)} |
| 102 | + className={` |
| 103 | + w-10 h-10 rounded-lg border-2 transition-all |
| 104 | + ${ |
| 105 | + isSelected(preset) |
| 106 | + ? 'border-blue-500 ring-2 ring-blue-500/30' |
| 107 | + : 'border-gray-200 hover:border-gray-300' |
| 108 | + } |
| 109 | + `} |
| 110 | + style={ |
| 111 | + preset.type === 'transparent' |
| 112 | + ? { |
| 113 | + backgroundColor: '#fff', |
| 114 | + backgroundImage: ` |
| 115 | + linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%), |
| 116 | + linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%) |
| 117 | + `, |
| 118 | + backgroundSize: '8px 8px', |
| 119 | + backgroundPosition: '0 0, 4px 4px', |
| 120 | + } |
| 121 | + : { backgroundColor: preset.value || undefined } |
| 122 | + } |
| 123 | + aria-label={`${preset.label} background`} |
| 124 | + title={preset.label} |
| 125 | + /> |
| 126 | + ))} |
| 127 | + </div> |
| 128 | + |
| 129 | + {/* Custom Color Row */} |
| 130 | + <div className="flex items-center gap-3"> |
| 131 | + <label htmlFor="custom-color" className="text-sm text-gray-600"> |
| 132 | + Custom Color |
| 133 | + </label> |
| 134 | + <input |
| 135 | + type="color" |
| 136 | + id="custom-color" |
| 137 | + value={customColor} |
| 138 | + onChange={handleCustomColorChange} |
| 139 | + className="w-10 h-10 rounded-lg cursor-pointer border border-gray-200" |
| 140 | + aria-label="Select custom background color" |
| 141 | + /> |
| 142 | + </div> |
| 143 | + |
| 144 | + {/* Photo Upload */} |
| 145 | + <div> |
| 146 | + <button |
| 147 | + onClick={handleUploadClick} |
| 148 | + className="w-full py-2 px-4 border-2 border-dashed border-gray-300 rounded-lg |
| 149 | + text-sm text-gray-600 hover:border-blue-500 hover:text-blue-500 |
| 150 | + transition-colors flex items-center justify-center gap-2" |
| 151 | + > |
| 152 | + <svg |
| 153 | + className="w-4 h-4" |
| 154 | + fill="none" |
| 155 | + stroke="currentColor" |
| 156 | + viewBox="0 0 24 24" |
| 157 | + xmlns="http://www.w3.org/2000/svg" |
| 158 | + > |
| 159 | + <path |
| 160 | + strokeLinecap="round" |
| 161 | + strokeLinejoin="round" |
| 162 | + strokeWidth={2} |
| 163 | + d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" |
| 164 | + /> |
| 165 | + </svg> |
| 166 | + Upload Photo Background |
| 167 | + </button> |
| 168 | + <input |
| 169 | + type="file" |
| 170 | + ref={fileInputRef} |
| 171 | + accept="image/*" |
| 172 | + onChange={handleFileSelect} |
| 173 | + className="hidden" |
| 174 | + /> |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + ); |
| 178 | +} |
0 commit comments