Blame view

天文台pc/tianwentai-ui/node_modules/framer-motion/dist/es/components/AnimatePresence/use-presence.mjs 2.17 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
62
63
64
65
66
67
68
69
70
71
72
  "use client";
  import { useContext, useId, useEffect, useCallback } from 'react';
  import { PresenceContext } from '../../context/PresenceContext.mjs';
  
  /**
   * When a component is the child of `AnimatePresence`, it can use `usePresence`
   * to access information about whether it's still present in the React tree.
   *
   * ```jsx
   * import { usePresence } from "framer-motion"
   *
   * export const Component = () => {
   *   const [isPresent, safeToRemove] = usePresence()
   *
   *   useEffect(() => {
   *     !isPresent && setTimeout(safeToRemove, 1000)
   *   }, [isPresent])
   *
   *   return <div />
   * }
   * ```
   *
   * If `isPresent` is `false`, it means that a component has been removed from the tree,
   * but `AnimatePresence` won't really remove it until `safeToRemove` has been called.
   *
   * @public
   */
  function usePresence(subscribe = true) {
      const context = useContext(PresenceContext);
      if (context === null)
          return [true, null];
      const { isPresent, onExitComplete, register } = context;
      // It's safe to call the following hooks conditionally (after an early return) because the context will always
      // either be null or non-null for the lifespan of the component.
      const id = useId();
      useEffect(() => {
          if (subscribe) {
              return register(id);
          }
      }, [subscribe]);
      const safeToRemove = useCallback(() => subscribe && onExitComplete && onExitComplete(id), [id, onExitComplete, subscribe]);
      return !isPresent && onExitComplete ? [false, safeToRemove] : [true];
  }
  /**
   * Similar to `usePresence`, except `useIsPresent` simply returns whether or not the component is present.
   * There is no `safeToRemove` function.
   *
   * ```jsx
   * import { useIsPresent } from "framer-motion"
   *
   * export const Component = () => {
   *   const isPresent = useIsPresent()
   *
   *   useEffect(() => {
   *     !isPresent && console.log("I've been removed!")
   *   }, [isPresent])
   *
   *   return <div />
   * }
   * ```
   *
   * @public
   */
  function useIsPresent() {
      return isPresent(useContext(PresenceContext));
  }
  function isPresent(context) {
      return context === null ? true : context.isPresent;
  }
  
  export { isPresent, useIsPresent, usePresence };
  //# sourceMappingURL=use-presence.mjs.map