import React, { useState, ReactNode, useRef, useEffect } from 'react'; import { animated, useSpring } from '@react-spring/web'; interface DropdownProps { title: ReactNode; open: boolean; toggleOpen: () => void; className: string; children: ReactNode; } // Would be way too messy to add the center the title in the other Dropdown const SectionDropdown: React.FC = ({ title, open, className, children, toggleOpen }) => { 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 + 0); } else { // Fallback for browsers that don't support borderBoxSize const height = entry.contentRect.height; setContentHeight(height + 0); } } }); resizeObserver.observe(contentRef.current); } return () => { if (resizeObserver) { resizeObserver.disconnect(); } }; }, []); const springProps = useSpring({ height: open ? contentHeight : 0, opacity: open ? 1 : 0, config: { tension: 300, friction: 30 } }); return (
{children}
); }; export default SectionDropdown;