UNPKG

@wordpress/components

Version:
99 lines (97 loc) 4.22 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/components/src/modal/use-modal-exit-animation.ts var use_modal_exit_animation_exports = {}; __export(use_modal_exit_animation_exports, { useModalExitAnimation: () => useModalExitAnimation }); module.exports = __toCommonJS(use_modal_exit_animation_exports); var import_compose = require("@wordpress/compose"); var import_element = require("@wordpress/element"); var import_utils = require("../utils"); var import_warning = __toESM(require("@wordpress/warning")); var FRAME_ANIMATION_DURATION = import_utils.CONFIG.transitionDuration; var FRAME_ANIMATION_DURATION_NUMBER = Number.parseInt(import_utils.CONFIG.transitionDuration); var EXIT_ANIMATION_NAME = "components-modal__disappear-animation"; function useModalExitAnimation() { const frameRef = (0, import_element.useRef)(); const [isAnimatingOut, setIsAnimatingOut] = (0, import_element.useState)(false); const isReducedMotion = (0, import_compose.useReducedMotion)(); const closeModal = (0, import_element.useCallback)(() => new Promise((closeModalResolve) => { const frameEl = frameRef.current; if (isReducedMotion) { closeModalResolve(); return; } if (!frameEl) { globalThis.SCRIPT_DEBUG === true ? (0, import_warning.default)("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; closeModalResolve(); return; } let handleAnimationEnd; const startAnimation = () => new Promise((animationResolve) => { handleAnimationEnd = (e) => { if (e.animationName === EXIT_ANIMATION_NAME) { animationResolve(); } }; frameEl.addEventListener("animationend", handleAnimationEnd); setIsAnimatingOut(true); }); const animationTimeout = () => new Promise((timeoutResolve) => { setTimeout( () => timeoutResolve(), // Allow an extra 20% of the animation duration for the // animationend event to fire, in case the animation frame is // slightly delayes by some other events in the event loop. FRAME_ANIMATION_DURATION_NUMBER * 1.2 ); }); Promise.race([startAnimation(), animationTimeout()]).then(() => { if (handleAnimationEnd) { frameEl.removeEventListener("animationend", handleAnimationEnd); } setIsAnimatingOut(false); closeModalResolve(); }); }), [isReducedMotion]); return { overlayClassname: isAnimatingOut ? "is-animating-out" : void 0, frameRef, frameStyle: { "--modal-frame-animation-duration": `${FRAME_ANIMATION_DURATION}` }, closeModal }; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { useModalExitAnimation }); //# sourceMappingURL=use-modal-exit-animation.js.map