UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

208 lines (207 loc) 6.3 kB
"use strict"; "use client"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useHeightAnimation = useHeightAnimation; var _react = require("react"); var _HeightAnimationInstance = _interopRequireDefault(require("./HeightAnimationInstance.js")); var _useIsomorphicLayoutEffect = require("../../shared/helpers/useIsomorphicLayoutEffect.js"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; } function useHeightAnimation(targetRef, { open = true, animate = true, children = null, compensateForGap, onInit = null, onOpen = null, onAnimationStart = null, onAnimationEnd = null } = {}) { const instRef = (0, _react.useRef)(null); const isInitialRenderRef = (0, _react.useRef)(typeof globalThis !== 'undefined' ? globalThis.readjustTime !== -1 : true); const [isOpen, setIsOpen] = (0, _react.useState)(open); const [isVisible, setIsVisible] = (0, _react.useState)(open); const [isAnimating, setIsAnimating] = (0, _react.useState)(false); const [isVisibleParallax, setParallax] = (0, _react.useState)(open); const eventsRef = (0, _react.useRef)({ onInit, onAnimationEnd, onAnimationStart, onOpen }); eventsRef.current.onInit = onInit; eventsRef.current.onAnimationEnd = onAnimationEnd; eventsRef.current.onAnimationStart = onAnimationStart; eventsRef.current.onOpen = onOpen; (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { instRef.current = new _HeightAnimationInstance.default(); eventsRef.current.onInit?.(instRef.current); return () => { instRef.current?.remove(); instRef.current = null; }; }, []); (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { instRef.current.setOptions({ animate }); if (typeof global !== 'undefined' && globalThis.IS_TEST) { instRef.current.setOptions({ animate: false }); } }, [animate]); const handleCompensateForGap = (0, _react.useCallback)(() => { if (compensateForGap) { let gap = compensateForGap; const { elem } = instRef.current; if (compensateForGap === 'auto') { gap = window.getComputedStyle(elem.parentElement).getPropertyValue('row-gap'); } elem.style.marginTop = `calc(${gap} * -1)`; const inner = elem.querySelector('.dnb-height-animation__compensate-for-gap'); inner.style.marginTop = gap; } }, [compensateForGap]); (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { instRef.current.onStart(state => { switch (state) { case 'opening': handleCompensateForGap(); setIsVisible(true); setParallax(true); setIsAnimating(true); break; case 'closing': setParallax(false); setIsAnimating(true); break; case 'adjusting': setIsAnimating(true); break; } if (!isInitialRenderRef.current) { eventsRef.current.onAnimationStart?.(state); } }); instRef.current.onEnd(state => { switch (state) { case 'opened': setIsVisible(true); setIsOpen(true); setIsAnimating(false); eventsRef.current.onOpen?.(true); break; case 'closed': setIsVisible(false); setIsOpen(false); setParallax(false); setIsAnimating(false); eventsRef.current.onOpen?.(false); break; case 'adjusted': setIsAnimating(false); break; } if (!isInitialRenderRef.current) { eventsRef.current.onAnimationEnd?.(state); } }); }, [compensateForGap, handleCompensateForGap]); useOpenClose({ open, instRef, isInitialRenderRef, targetRef }); useAdjust({ children, instRef, isInitialRenderRef, targetRef }); const firstPaintStyle = open && !isVisible && !isAnimating && instRef.current?.firstPaintStyle || {}; const isInDOM = open || isVisible; return { open, isOpen, isInDOM, isVisible, isVisibleParallax, isAnimating, firstPaintStyle }; } function useOpenClose({ open, instRef, isInitialRenderRef, targetRef }) { const isTest = typeof process !== 'undefined' && process.env.NODE_ENV === 'test' && typeof globalThis.IS_TEST === 'undefined'; (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { instRef.current.setElement(targetRef.current); if (!targetRef.current || instRef.current.state === 'init' && isInitialRenderRef.current) { if (open) { instRef.current.setAsOpen(); } else { instRef.current.setAsClosed(); } } else { if (open) { instRef.current.open(); } else { instRef.current.close(); } } if (isTest) { const event = new CustomEvent('transitionend'); targetRef.current?.dispatchEvent(event); } }, [open, targetRef, instRef, isInitialRenderRef, isTest]); (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { const run = () => { isInitialRenderRef.current = false; }; if (globalThis.bypassTime === -1 || isTest) { run(); } else { window.requestAnimationFrame?.(run); } }, [isInitialRenderRef, isTest]); } function useAdjust({ children, instRef, isInitialRenderRef, targetRef }) { const fromHeight = (0, _react.useRef)(0); const [timer] = (0, _react.useState)(() => Date.now()); const shouldAdjust = () => { switch (instRef.current?.state) { case 'opened': case 'adjusted': case 'adjusting': return !isInitialRenderRef.current && Date.now() - timer > (globalThis.readjustTime || 100); } return false; }; (0, _react.useMemo)(() => { if (shouldAdjust()) { fromHeight.current = instRef.current?.getHeight(); } }, [children]); (0, _useIsomorphicLayoutEffect.useIsomorphicLayoutEffect)(() => { if (shouldAdjust()) { instRef.current.setElement(targetRef.current); instRef.current.elem.style.height = ''; const toHeight = instRef.current.getHeight(); instRef.current.adjustTo(fromHeight.current, toHeight); } }, [children]); } //# sourceMappingURL=useHeightAnimation.js.map