@carbon/react
Version:
React components for the Carbon Design System
61 lines (59 loc) • 1.82 kB
JavaScript
/**
* Copyright IBM Corp. 2016, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
require("../_virtual/_rolldown/runtime.js");
const require_usePrefix = require("./usePrefix.js");
const require_useIsomorphicEffect = require("./useIsomorphicEffect.js");
let react = require("react");
//#region src/internal/usePresence.ts
/**
* Copyright IBM Corp. 2025, 2026
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/
const usePresence = (ref, isOpen) => {
const prefix = require_usePrefix.usePrefix();
const [exitState, setExitState] = (0, react.useState)(isOpen ? "idle" : "finished");
const isExiting = exitState === "active";
require_useIsomorphicEffect.default(() => {
setExitState((prev) => {
if (!isOpen && prev === "idle") return "active";
if (isOpen && prev !== "idle") return "idle";
return prev;
});
}, [isOpen]);
require_useIsomorphicEffect.default(() => {
if (!ref.current || !isExiting) return;
if (!("getAnimations" in ref.current)) {
setExitState("finished");
return;
}
const animations = ref.current.getAnimations({ subtree: true }).filter((animation) => animation instanceof CSSAnimation && animation.animationName.startsWith(`${prefix}--presence`));
if (!animations.length) {
setExitState("finished");
return;
}
let cancelled = false;
Promise.all(animations.map((animation) => animation.finished)).finally(() => {
if (cancelled) return;
setExitState("finished");
});
return () => {
cancelled = true;
};
}, [
ref,
isExiting,
prefix
]);
return {
isPresent: isOpen || exitState !== "finished",
isExiting
};
};
//#endregion
exports.usePresence = usePresence;