| 1 | "use client"; |
| 2 | |
| 3 | import ColorPicker from "@/components/ui/color-picker"; |
| 4 | import { Input } from "@/components/ui/input"; |
| 5 | import { Label } from "@/components/ui/label"; |
| 6 | import { Palette } from "lucide-react"; |
| 7 | import { useState } from "react"; |
| 8 | import { useCommonValues } from "../hooks/useCommonValues"; |
| 9 | import { useUpdateAllSlides } from "../hooks/useUpdateAllSlides"; |
| 10 | |
| 11 | export function BackgroundSection() { |
| 12 | const { currentBgColor } = useCommonValues(); |
| 13 | const updateAllSlides = useUpdateAllSlides(); |
| 14 | const [hexInput, setHexInput] = useState(currentBgColor || ""); |
| 15 | |
| 16 | console.log(currentBgColor); |
| 17 | // To sync input if color is changed outside |
| 18 | // but simple usage is ok for now - set on color picker change also |
| 19 | const handleHexChange = (e: React.ChangeEvent<HTMLInputElement>) => { |
| 20 | const val = e.target.value; |
| 21 | setHexInput(val); |
| 22 | // only set if it looks like a valid hex |
| 23 | const validHex = /^#([0-9A-Fa-f]{6}|[0-9A-Fa-f]{3})$/; |
| 24 | if (validHex.test(val)) { |
| 25 | updateAllSlides({ bgColor: val }); |
| 26 | } |
| 27 | }; |
| 28 | |
| 29 | return ( |
| 30 | <div className="flex items-center justify-between gap-2"> |
| 31 | <Label |
| 32 | htmlFor="background-color" |
| 33 | className="flex items-center gap-2 text-sm font-semibold text-foreground" |
| 34 | > |
| 35 | <Palette className="h-4 w-4 text-muted-foreground" /> |
| 36 | Background Color |
| 37 | </Label> |
| 38 | <div className="flex items-center gap-3"> |
| 39 | <ColorPicker |
| 40 | value={currentBgColor} |
| 41 | onChange={(color) => { |
| 42 | updateAllSlides({ bgColor: color }); |
| 43 | setHexInput(color); |
| 44 | }} |
| 45 | /> |
| 46 | <Input |
| 47 | className="w-28 rounded border border-border bg-background px-2 py-1 text-xs focus:border-primary focus:outline-hidden" |
| 48 | value={hexInput} |
| 49 | onChange={handleHexChange} |
| 50 | placeholder="#RRGGBB" |
| 51 | maxLength={7} |
| 52 | spellCheck={false} |
| 53 | /> |
| 54 | </div> |
| 55 | </div> |
| 56 | ); |
| 57 | } |
| 58 |