Files
SnowLuma/.pnpm-store/v11/files/c0/879cbd5382613ff9eaed396f89bdbb71f314a8b03743918c6bb328a0beed8a365e0a9ddde86b7902b08dae78e9e990e184694ee1213cd1789d0ce760e30469
TanTan 2a69b23b47
Dev Build / cleanup (push) Canceled after 0s
Dev Build / typecheck (push) Canceled after 0s
Dev Build / build (linux-arm64, ubuntu-24.04-arm, linux-arm64) (push) Canceled after 0s
Dev Build / build (linux-x64, ubuntu-latest, linux-x64) (push) Canceled after 0s
Dev Build / build (win-x64, windows-latest, win32-x64) (push) Canceled after 0s
Dev Build / dispatch-dev-docker (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

53 lines
1.8 KiB
Plaintext

"use client";
import { useRef, useInsertionEffect, useCallback } from 'react';
/**
* Creates a ref function that, when called, hydrates the provided
* external ref and VisualElement.
*/
function useMotionRef(visualState, visualElement, externalRef) {
/**
* Store externalRef in a ref to avoid including it in the useCallback
* dependency array. Including externalRef in dependencies causes issues
* with libraries like Radix UI that create new callback refs on each render
* when using asChild - this would cause the callback to be recreated,
* triggering element remounts and breaking AnimatePresence exit animations.
*/
const externalRefContainer = useRef(externalRef);
useInsertionEffect(() => {
externalRefContainer.current = externalRef;
});
// Store cleanup function returned by callback refs (React 19 feature)
const refCleanup = useRef(null);
return useCallback((instance) => {
if (instance) {
visualState.onMount?.(instance);
}
const ref = externalRefContainer.current;
if (typeof ref === "function") {
if (instance) {
const cleanup = ref(instance);
if (typeof cleanup === "function") {
refCleanup.current = cleanup;
}
}
else if (refCleanup.current) {
refCleanup.current();
refCleanup.current = null;
}
else {
ref(instance);
}
}
else if (ref) {
ref.current = instance;
}
if (visualElement) {
instance ? visualElement.mount(instance) : visualElement.unmount();
}
}, [visualElement]);
}
export { useMotionRef };
//# sourceMappingURL=use-motion-ref.mjs.map