@wordpress/components
Version:
UI components for WordPress.
8 lines (7 loc) • 3.85 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../src/modal/use-modal-exit-animation.ts"],
"sourcesContent": ["/**\n * WordPress dependencies\n */\nimport { useReducedMotion } from '@wordpress/compose';\nimport { useCallback, useRef, useState } from '@wordpress/element';\nimport warning from '@wordpress/warning';\n\n/**\n * Internal dependencies\n */\nimport { CONFIG } from '../utils';\n\n// Animation duration (ms) extracted to JS in order to be used on a setTimeout.\nconst FRAME_ANIMATION_DURATION = CONFIG.transitionDuration;\nconst FRAME_ANIMATION_DURATION_NUMBER = Number.parseInt(CONFIG.transitionDuration);\nconst EXIT_ANIMATION_NAME = 'components-modal__disappear-animation';\nexport function useModalExitAnimation() {\n const frameRef = useRef(null);\n const [isAnimatingOut, setIsAnimatingOut] = useState(false);\n const isReducedMotion = useReducedMotion();\n const closeModal = useCallback(() => new Promise(closeModalResolve => {\n // Grab a \"stable\" reference of the frame element, since\n // the value held by the react ref might change at runtime.\n const frameEl = frameRef.current;\n if (isReducedMotion) {\n closeModalResolve();\n return;\n }\n if (!frameEl) {\n globalThis.SCRIPT_DEBUG === true ? warning(\"wp.components.Modal: the Modal component can't be closed with an exit animation because of a missing reference to the modal frame element.\") : void 0;\n closeModalResolve();\n return;\n }\n let handleAnimationEnd;\n const startAnimation = () => new Promise(animationResolve => {\n handleAnimationEnd = e => {\n if (e.animationName === EXIT_ANIMATION_NAME) {\n animationResolve();\n }\n };\n frameEl.addEventListener('animationend', handleAnimationEnd);\n setIsAnimatingOut(true);\n });\n const animationTimeout = () => new Promise(timeoutResolve => {\n setTimeout(() => timeoutResolve(),\n // Allow an extra 20% of the animation duration for the\n // animationend event to fire, in case the animation frame is\n // slightly delayes by some other events in the event loop.\n FRAME_ANIMATION_DURATION_NUMBER * 1.2);\n });\n Promise.race([startAnimation(), animationTimeout()]).then(() => {\n if (handleAnimationEnd) {\n frameEl.removeEventListener('animationend', handleAnimationEnd);\n }\n setIsAnimatingOut(false);\n closeModalResolve();\n });\n }), [isReducedMotion]);\n return {\n overlayClassname: isAnimatingOut ? 'is-animating-out' : undefined,\n frameRef,\n frameStyle: {\n '--modal-frame-animation-duration': `${FRAME_ANIMATION_DURATION}`\n },\n closeModal\n };\n}"],
"mappings": ";AAGA,SAAS,wBAAwB;AACjC,SAAS,aAAa,QAAQ,gBAAgB;AAC9C,OAAO,aAAa;AAKpB,SAAS,cAAc;AAGvB,IAAM,2BAA2B,OAAO;AACxC,IAAM,kCAAkC,OAAO,SAAS,OAAO,kBAAkB;AACjF,IAAM,sBAAsB;AACrB,SAAS,wBAAwB;AACtC,QAAM,WAAW,OAAO,IAAI;AAC5B,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAS,KAAK;AAC1D,QAAM,kBAAkB,iBAAiB;AACzC,QAAM,aAAa,YAAY,MAAM,IAAI,QAAQ,uBAAqB;AAGpE,UAAM,UAAU,SAAS;AACzB,QAAI,iBAAiB;AACnB,wBAAkB;AAClB;AAAA,IACF;AACA,QAAI,CAAC,SAAS;AACZ,iBAAW,iBAAiB,OAAO,QAAQ,4IAA4I,IAAI;AAC3L,wBAAkB;AAClB;AAAA,IACF;AACA,QAAI;AACJ,UAAM,iBAAiB,MAAM,IAAI,QAAQ,sBAAoB;AAC3D,2BAAqB,OAAK;AACxB,YAAI,EAAE,kBAAkB,qBAAqB;AAC3C,2BAAiB;AAAA,QACnB;AAAA,MACF;AACA,cAAQ,iBAAiB,gBAAgB,kBAAkB;AAC3D,wBAAkB,IAAI;AAAA,IACxB,CAAC;AACD,UAAM,mBAAmB,MAAM,IAAI,QAAQ,oBAAkB;AAC3D;AAAA,QAAW,MAAM,eAAe;AAAA;AAAA;AAAA;AAAA,QAIhC,kCAAkC;AAAA,MAAG;AAAA,IACvC,CAAC;AACD,YAAQ,KAAK,CAAC,eAAe,GAAG,iBAAiB,CAAC,CAAC,EAAE,KAAK,MAAM;AAC9D,UAAI,oBAAoB;AACtB,gBAAQ,oBAAoB,gBAA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