Blame view

天文台pc/tianwentai-ui/node_modules/framer-motion/dist/es/utils/use-composed-ref.mjs 1.91 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
53
54
55
56
57
58
59
60
61
  import * as React from 'react';
  
  /**
   * Taken from https://github.com/radix-ui/primitives/blob/main/packages/react/compose-refs/src/compose-refs.tsx
   */
  /**
   * Set a given ref to a given value
   * This utility takes care of different types of refs: callback refs and RefObject(s)
   */
  function setRef(ref, value) {
      if (typeof ref === "function") {
          return ref(value);
      }
      else if (ref !== null && ref !== undefined) {
          ref.current = value;
      }
  }
  /**
   * A utility to compose multiple refs together
   * Accepts callback refs and RefObject(s)
   */
  function composeRefs(...refs) {
      return (node) => {
          let hasCleanup = false;
          const cleanups = refs.map((ref) => {
              const cleanup = setRef(ref, node);
              if (!hasCleanup && typeof cleanup === "function") {
                  hasCleanup = true;
              }
              return cleanup;
          });
          // React <19 will log an error to the console if a callback ref returns a
          // value. We don't use ref cleanups internally so this will only happen if a
          // user's ref callback returns a value, which we only expect if they are
          // using the cleanup functionality added in React 19.
          if (hasCleanup) {
              return () => {
                  for (let i = 0; i < cleanups.length; i++) {
                      const cleanup = cleanups[i];
                      if (typeof cleanup === "function") {
                          cleanup();
                      }
                      else {
                          setRef(refs[i], null);
                      }
                  }
              };
          }
      };
  }
  /**
   * A custom hook that composes multiple refs
   * Accepts callback refs and RefObject(s)
   */
  function useComposedRefs(...refs) {
      // eslint-disable-next-line react-hooks/exhaustive-deps
      return React.useCallback(composeRefs(...refs), refs);
  }
  
  export { useComposedRefs };
  //# sourceMappingURL=use-composed-ref.mjs.map