import React from "react"; import { HexColorPicker } from "react-colorful"; import { useEffect, useRef, useState } from "react"; import ColorInput from "./colorInput"; import { hexToRgb } from "../../util/colorUtil"; import Button from "../elements/button"; import useClickOutside from "../../customHooks/useClickOutside"; import PopOver from "../popOver/popOver"; type ColorPickerSectionProps = Readonly<{ hex: string; setHex: React.Dispatch>; }>; export type RGBType = { r: number; g: number; b: number }; export default function ColorPickerSection({ hex, setHex, }: ColorPickerSectionProps) { const [currentRGB, setCurrentRGB] = useState(); const [showPicker, setShowPicker] = useState(false); const [popoverStyle, setPopoverStyle] = useState({}); const buttonRef = useRef(null); const popoverRef = useRef(null); useEffect(() => { const rgbValue = hexToRgb(hex); setCurrentRGB(rgbValue); }, [hex]); const togglePicker = (value?: boolean) => { if (buttonRef.current) { const rect = buttonRef.current.getBoundingClientRect(); setPopoverStyle({ position: "fixed", top: rect.bottom - 80, right: rect.right - rect.width * 5, }); } setShowPicker((c) => value ?? !c); }; useClickOutside( popoverRef as React.RefObject, showPicker, () => setShowPicker(false) ); return (
{showPicker && ( // Popover for the color picker
)}
togglePicker()} className="relative hover:cursor-pointer w-20 h-20 rounded-xl" style={{ backgroundColor: hex }} />
); }