import { Square, RectangleVertical, RectangleHorizontal, ChevronDown } from 'lucide-react'; import { useState, useEffect, useRef } from 'react'; const dimensions = [ { id: 'landscape', label: '3:2', width: 1500, height: 1000, icon: RectangleHorizontal }, { id: 'square', label: '1:1', width: 1000, height: 1000, icon: Square }, { id: 'portrait', label: '4:5', width: 1000, height: 1250, icon: RectangleVertical } ]; const DimensionSelector = ({ currentDimension = dimensions[0], onDimensionChange }) => { const [isOpen, setIsOpen] = useState(false); const dropdownRef = useRef(null); // Handle both click and hover for better mobile and desktop experience const handleToggle = () => setIsOpen(!isOpen); // We don't want to close on mouse leave immediately const timeoutRef = useRef(null); const handleMouseEnter = () => { if (timeoutRef.current) { clearTimeout(timeoutRef.current); timeoutRef.current = null; } setIsOpen(true); }; const handleMouseLeave = () => { timeoutRef.current = setTimeout(() => { setIsOpen(false); }, 100); }; // Close dropdown when clicking outside useEffect(() => { const handleClickOutside = (event) => { if (dropdownRef.current && !dropdownRef.current.contains(event.target)) { setIsOpen(false); } }; document.addEventListener('mousedown', handleClickOutside); return () => { document.removeEventListener('mousedown', handleClickOutside); if (timeoutRef.current) clearTimeout(timeoutRef.current); }; }, []); // Find current dimension const current = dimensions.find(d => d.id === currentDimension.id) || dimensions[0]; const Icon = current.icon; return (
{/* Current selection */} {/* Dropdown */} {isOpen && ( <> {/* Invisible bridge element that extends to the dropdown */}
{dimensions.map((dim) => { const Icon = dim.icon; return ( ); })}
)}
); }; export default DimensionSelector;