Blame view

天文台pc/tianwentai-ui/node_modules/framer-motion/dist/es/motion/utils/use-motion-ref.mjs 1.79 KB
bc518174   王天杨   提交两个项目文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  "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