{"version":3,"file":"usePatchAnimateHeight.js","names":["useEffect","useMemo","useRef","useState","usePatchAnimateHeight","containerRef","contentRef","duration","open","browserSupportsKeywordAnimation","CSS","supports","hasInitialized","previousOpenState","isAnimating","setIsAnimating","resizeObserverRef","container","current","content","setContainerHeight","height","style","handleTransitionEnd","transition","animate","scrollHeight","offsetHeight","onTransitionEnd","removeEventListener","addEventListener","ResizeObserver","observe","disconnect"],"sources":["../../../src/elements/AnimateHeight/usePatchAnimateHeight.ts"],"sourcesContent":["import { useEffect, useMemo, useRef, useState } from 'react'\n\nexport const usePatchAnimateHeight = ({\n containerRef,\n contentRef,\n duration,\n open,\n}: {\n containerRef: React.RefObject\n contentRef: React.RefObject\n duration: number\n open: boolean\n}): { browserSupportsKeywordAnimation: boolean } => {\n const browserSupportsKeywordAnimation = useMemo(\n () =>\n typeof CSS !== 'undefined' && CSS && CSS.supports\n ? CSS.supports('interpolate-size', 'allow-keywords')\n : false,\n [],\n )\n\n const hasInitialized = useRef(false)\n const previousOpenState = useRef(open)\n const [isAnimating, setIsAnimating] = useState(false)\n const resizeObserverRef = useRef(null)\n\n useEffect(() => {\n const container = containerRef.current\n const content = contentRef.current\n\n if (!container || !content || browserSupportsKeywordAnimation) {\n return\n }\n\n const setContainerHeight = (height: string) => {\n container.style.height = height\n }\n\n const handleTransitionEnd = () => {\n if (container) {\n container.style.transition = ''\n container.style.height = open ? 'auto' : '0px'\n setIsAnimating(false)\n }\n }\n\n const animate = () => {\n if (!hasInitialized.current && open) {\n // Skip animation on first render\n setContainerHeight('auto')\n setIsAnimating(false)\n return\n }\n\n hasInitialized.current = true\n\n if (previousOpenState.current !== open) {\n setContainerHeight(open ? '0px' : `${content.scrollHeight}px`)\n }\n\n // Trigger reflow\n container.offsetHeight // eslint-disable-line @typescript-eslint/no-unused-expressions\n\n setIsAnimating(true)\n container.style.transition = `height ${duration}ms ease`\n setContainerHeight(open ? `${content.scrollHeight}px` : '0px')\n\n const onTransitionEnd = () => {\n handleTransitionEnd()\n container.removeEventListener('transitionend', onTransitionEnd)\n }\n\n container.addEventListener('transitionend', onTransitionEnd)\n }\n\n animate()\n previousOpenState.current = open\n\n // Setup ResizeObserver\n resizeObserverRef.current = new ResizeObserver(() => {\n if (isAnimating) {\n container.style.height = open ? `${content.scrollHeight}px` : '0px'\n }\n })\n resizeObserverRef.current.observe(content)\n\n return () => {\n if (container) {\n container.style.transition = ''\n container.style.height = ''\n }\n resizeObserverRef.current?.disconnect()\n }\n }, [open, duration, containerRef, contentRef, browserSupportsKeywordAnimation])\n\n return { browserSupportsKeywordAnimation }\n}\n"],"mappings":"AAAA,SAASA,SAAS,EAAEC,OAAO,EAAEC,MAAM,EAAEC,QAAQ,QAAQ;AAErD,OAAO,MAAMC,qBAAA,GAAwBA,CAAC;EACpCC,YAAY;EACZC,UAAU;EACVC,QAAQ;EACRC;AAAI,CAML;EACC,MAAMC,+BAAA,GAAkCR,OAAA,CACtC,MACE,OAAOS,GAAA,KAAQ,eAAeA,GAAA,IAAOA,GAAA,CAAIC,QAAQ,GAC7CD,GAAA,CAAIC,QAAQ,CAAC,oBAAoB,oBACjC,OACN,EAAE;EAGJ,MAAMC,cAAA,GAAiBV,MAAA,CAAO;EAC9B,MAAMW,iBAAA,GAAoBX,MAAA,CAAOM,IAAA;EACjC,MAAM,CAACM,WAAA,EAAaC,cAAA,CAAe,GAAGZ,QAAA,CAAS;EAC/C,MAAMa,iBAAA,GAAoBd,MAAA,CAA8B;EAExDF,SAAA,CAAU;IACR,MAAMiB,SAAA,GAAYZ,YAAA,CAAaa,OAAO;IACtC,MAAMC,OAAA,GAAUb,UAAA,CAAWY,OAAO;IAElC,IAAI,CAACD,SAAA,IAAa,CAACE,OAAA,IAAWV,+BAAA,EAAiC;MAC7D;IACF;IAEA,MAAMW,kBAAA,GAAsBC,MAAA;MAC1BJ,SAAA,CAAUK,KAAK,CAACD,MAAM,GAAGA,MAAA;IAC3B;IAEA,MAAME,mBAAA,GAAsBA,CAAA;MAC1B,IAA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