import React, { useState, ReactNode, useRef, useEffect } from 'react'; import { animated, useSpring } from '@react-spring/web'; interface DropdownProps { title: ReactNode; open?: boolean; className?: string; contentWrapperClassName?: string; bottomPadding?: number; children: ReactNode; } const Dropdown: React.FC = ({ title, open = false, className = "w-full text-left font-semibold flex justify-between items-center p-4", contentWrapperClassName = "px-6", bottomPadding = 12, children }) => { const [isOpen, setIsOpen] = useState(open); const contentRef = useRef(null); const [contentHeight, setContentHeight] = useState(0); useEffect(() => { let resizeObserver: ResizeObserver | null = null; if (contentRef.current) { resizeObserver = new ResizeObserver(entries => { for (let entry of entries) { if (entry.borderBoxSize && entry.borderBoxSize.length > 0) { const height = entry.borderBoxSize[0].blockSize; setContentHeight(height + bottomPadding); } else { // Fallback for browsers that don't support borderBoxSize const height = entry.contentRect.height; setContentHeight(height + bottomPadding); } } }); resizeObserver.observe(contentRef.current); } return () => { if (resizeObserver) { resizeObserver.disconnect(); } }; }, [bottomPadding]); const springProps = useSpring({ height: isOpen ? contentHeight : 0, opacity: isOpen ? 1 : 0, config: { tension: 300, friction: 30 } }); return ( <>
{children}
); }; export default Dropdown;