"use client"; import { useEffect, useRef, useState } from "react"; import { CollapsibleProps } from "./types"; // Collapsible with smooth height/opacity transition export const Collapse = (props: CollapsibleProps) => { const { open, children } = props; const contentRef = useRef(null); const [maxHeight, setMaxHeight] = useState(0); useEffect(() => { const el = contentRef.current; /* istanbul ignore next -- defensive guard for ref not yet attached */ if (!el) return; const updateHeight = () => { setMaxHeight(open ? el.scrollHeight : 0); }; updateHeight(); let resizeObserver: ResizeObserver | undefined; if (open) { resizeObserver = new ResizeObserver(() => updateHeight()); resizeObserver.observe(el); } return () => { if (resizeObserver) resizeObserver.disconnect(); }; }, [open, children]); return (
{children}
); }; Collapse.displayName = "Collapse"; export type { CollapsibleProps };