@wordpress/components
Version:
UI components for WordPress.
74 lines (73 loc) • 3.15 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
// \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i];
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ((k) => from[k]).bind(null, key),
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
value: mod,
enumerable: true
}) : target, mod));
let _wordpress_compose = require("@wordpress/compose");
let _wordpress_element = require("@wordpress/element");
let _wordpress_warning = require("@wordpress/warning");
_wordpress_warning = __toESM(_wordpress_warning);
// packages/components/src/modal/use-modal-exit-animation.ts
const EXIT_ANIMATION_NAME = "components-modal__disappear-animation";
function useModalExitAnimation() {
const frameRef = (0, _wordpress_element.useRef)(null);
const [isAnimatingOut, setIsAnimatingOut] = (0, _wordpress_element.useState)(false);
const isReducedMotion = (0, _wordpress_compose.useReducedMotion)();
const closeModal = (0, _wordpress_element.useCallback)(() => new Promise((closeModalResolve) => {
const frameEl = frameRef.current;
if (isReducedMotion) {
closeModalResolve();
return;
}
if (!frameEl) {
(0, _wordpress_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.");
closeModalResolve();
return;
}
const animationDuration = Number.parseFloat(frameEl.ownerDocument.defaultView?.getComputedStyle(frameEl).animationDuration || "0") * 1e3;
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(), animationDuration * 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,
closeModal
};
}
exports.useModalExitAnimation = useModalExitAnimation;
//# sourceMappingURL=use-modal-exit-animation.cjs.map