import { injectAnimatePresence } from "./presence.mjs"; import { onMounted, onUnmounted, onUpdated } from "vue"; //#region src/components/animate-presence/use-presence.ts /** * Lets a component run its own exit when an `AnimatePresence` removes it, * for components that animate out without a `motion` element's `exit`. * * Removal waits for the promise `onExit` returns. Vue runs the component's * `onUnmounted` hooks before that, so teardown the exit needs (an engine, * listeners) should wait for the same promise. `onEnter` runs if the * element comes back while its exit is running. * * Outside an `AnimatePresence` it does nothing. */ function usePresence(handler) { const presence = injectAnimatePresence({}); if (!presence.registerHandler) return; let element; const read = () => { element = handler.element() ?? element; }; const registered = { ...handler, element: () => element }; onMounted(() => { read(); presence.registerHandler(registered); }); onUpdated(read); onUnmounted(() => presence.unregisterHandler(registered)); } //#endregion export { usePresence };