@tamagui/animate-presence 1.0.1-beta.21 → 1.0.1-beta.210

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/cjs/AnimatePresence.js +81 -54
  2. package/dist/cjs/AnimatePresence.js.map +3 -3
  3. package/dist/cjs/LayoutGroupContext.js +1 -0
  4. package/dist/cjs/LayoutGroupContext.js.map +1 -1
  5. package/dist/cjs/PresenceChild.js +43 -38
  6. package/dist/cjs/PresenceChild.js.map +3 -3
  7. package/dist/cjs/{AnimatePresenceContext.js → PresenceContext.js} +8 -7
  8. package/dist/cjs/PresenceContext.js.map +7 -0
  9. package/dist/cjs/index.js +2 -13
  10. package/dist/cjs/index.js.map +2 -2
  11. package/dist/cjs/types.js +1 -0
  12. package/dist/cjs/types.js.map +2 -2
  13. package/dist/cjs/usePresence.js +52 -0
  14. package/dist/cjs/usePresence.js.map +7 -0
  15. package/dist/esm/AnimatePresence.js +79 -57
  16. package/dist/esm/AnimatePresence.js.map +2 -2
  17. package/dist/esm/LayoutGroupContext.js +0 -0
  18. package/dist/esm/LayoutGroupContext.js.map +1 -1
  19. package/dist/esm/PresenceChild.js +38 -38
  20. package/dist/esm/PresenceChild.js.map +3 -3
  21. package/dist/esm/PresenceContext.js +6 -0
  22. package/dist/esm/PresenceContext.js.map +7 -0
  23. package/dist/esm/index.js +1 -5
  24. package/dist/esm/index.js.map +2 -2
  25. package/dist/esm/types.js +0 -0
  26. package/dist/esm/types.js.map +0 -0
  27. package/dist/esm/usePresence.js +26 -0
  28. package/dist/esm/usePresence.js.map +7 -0
  29. package/dist/jsx/AnimatePresence.js +56 -38
  30. package/dist/jsx/AnimatePresence.js.map +7 -0
  31. package/dist/jsx/LayoutGroupContext.js +1 -0
  32. package/dist/jsx/LayoutGroupContext.js.map +7 -0
  33. package/dist/jsx/PresenceChild.js +35 -36
  34. package/dist/jsx/PresenceChild.js.map +7 -0
  35. package/dist/jsx/PresenceContext.js +6 -0
  36. package/dist/jsx/PresenceContext.js.map +7 -0
  37. package/dist/jsx/index.js +2 -5
  38. package/dist/jsx/index.js.map +7 -0
  39. package/dist/jsx/types.js +1 -0
  40. package/dist/jsx/types.js.map +7 -0
  41. package/dist/jsx/usePresence.js +26 -0
  42. package/dist/jsx/usePresence.js.map +7 -0
  43. package/package.json +10 -5
  44. package/src/AnimatePresence.tsx +275 -0
  45. package/src/LayoutGroupContext.tsx +8 -0
  46. package/src/PresenceChild.tsx +81 -0
  47. package/src/index.ts +2 -0
  48. package/src/types.ts +81 -0
  49. package/types/AnimatePresence.d.ts +34 -8
  50. package/types/PresenceChild.d.ts +4 -3
  51. package/types/{AnimatePresenceContext.d.ts → PresenceContext.d.ts} +5 -4
  52. package/types/index.d.ts +1 -2
  53. package/types/types.d.ts +63 -0
  54. package/types/usePresence.d.ts +14 -0
  55. package/dist/cjs/AnimatePresenceContext.js.map +0 -7
  56. package/dist/cjs/useEntering.js +0 -57
  57. package/dist/cjs/useEntering.js.map +0 -7
  58. package/dist/esm/AnimatePresenceContext.js +0 -6
  59. package/dist/esm/AnimatePresenceContext.js.map +0 -7
  60. package/dist/esm/useEntering.js +0 -33
  61. package/dist/esm/useEntering.js.map +0 -7
  62. package/dist/jsx/AnimatePresenceContext.js +0 -5
  63. package/dist/jsx/useEntering.js +0 -32
  64. package/types/AnimatePresence.d.ts.map +0 -1
  65. package/types/AnimatePresenceContext.d.ts.map +0 -1
  66. package/types/LayoutGroupContext.d.ts.map +0 -1
  67. package/types/PresenceChild.d.ts.map +0 -1
  68. package/types/index.d.ts.map +0 -1
  69. package/types/types.d.ts.map +0 -1
  70. package/types/useEntering.d.ts +0 -6
  71. package/types/useEntering.d.ts.map +0 -1
@@ -1,18 +1,17 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from "@tamagui/core";
1
+ import { Fragment, jsx } from "react/jsx-runtime";
2
+ import { useForceUpdate, useIsomorphicLayoutEffect } from "@tamagui/core";
4
3
  import React, {
5
4
  Children,
6
5
  cloneElement,
7
6
  isValidElement,
8
7
  useContext,
9
- useRef,
10
- useState
8
+ useEffect,
9
+ useRef
11
10
  } from "react";
12
11
  import { LayoutGroupContext } from "./LayoutGroupContext";
13
12
  import { PresenceChild } from "./PresenceChild";
14
13
  React["keep"];
15
- const getChildKey = /* @__PURE__ */ __name((child) => child.key || "", "getChildKey");
14
+ const getChildKey = (child) => child.key || "";
16
15
  const isDev = process.env.NODE_ENV !== "production";
17
16
  function updateChildLookup(children, allChildren) {
18
17
  const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
@@ -25,17 +24,27 @@ function updateChildLookup(children, allChildren) {
25
24
  allChildren.set(key, child);
26
25
  });
27
26
  }
28
- __name(updateChildLookup, "updateChildLookup");
29
27
  function onlyElements(children) {
30
28
  const filtered = [];
31
- Children.forEach(children, (child) => {
32
- if (isValidElement(child))
33
- filtered.push(child);
29
+ Children.forEach(children, (child, index) => {
30
+ if (isValidElement(child)) {
31
+ if (!child.key) {
32
+ if (process.env.NODE_ENV === "development") {
33
+ console.warn(`No key given to AnimatePresence child, assigning index as key`);
34
+ }
35
+ filtered.push(
36
+ React.cloneElement(child, {
37
+ key: index
38
+ })
39
+ );
40
+ } else {
41
+ filtered.push(child);
42
+ }
43
+ }
34
44
  });
35
45
  return filtered;
36
46
  }
37
- __name(onlyElements, "onlyElements");
38
- const AnimatePresence = /* @__PURE__ */ __name(({
47
+ const AnimatePresence = ({
39
48
  children,
40
49
  enterVariant,
41
50
  exitVariant,
@@ -48,35 +57,38 @@ const AnimatePresence = /* @__PURE__ */ __name(({
48
57
  const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
49
58
  if (forceRenderLayoutGroup)
50
59
  forceRender = forceRenderLayoutGroup;
51
- const [isMounted, setIsMounted] = useState(false);
52
- useIsomorphicLayoutEffect(() => {
53
- setIsMounted(true);
54
- }, []);
60
+ const isMounted = useRef(false);
55
61
  const filteredChildren = onlyElements(children);
56
62
  let childrenToRender = filteredChildren;
57
63
  const exiting = /* @__PURE__ */ new Set();
58
64
  const presentChildren = useRef(childrenToRender);
59
65
  const allChildren = useRef(/* @__PURE__ */ new Map()).current;
60
66
  const isInitialRender = useRef(true);
67
+ useEffect(() => {
68
+ isMounted.current = true;
69
+ return () => {
70
+ isMounted.current = false;
71
+ isInitialRender.current = true;
72
+ allChildren.clear();
73
+ exiting.clear();
74
+ };
75
+ }, []);
61
76
  useIsomorphicLayoutEffect(() => {
62
77
  isInitialRender.current = false;
63
78
  updateChildLookup(filteredChildren, allChildren);
64
79
  presentChildren.current = childrenToRender;
65
80
  });
66
- useUnmountEffect(() => {
67
- isInitialRender.current = true;
68
- allChildren.clear();
69
- exiting.clear();
70
- });
71
81
  if (isInitialRender.current) {
72
- return /* @__PURE__ */ React.createElement(React.Fragment, null, childrenToRender.map((child) => /* @__PURE__ */ React.createElement(PresenceChild, {
73
- key: getChildKey(child),
74
- isEntering: true,
75
- exitVariant,
76
- enterVariant,
77
- initial: initial ? void 0 : false,
78
- presenceAffectsLayout
79
- }, child)));
82
+ return /* @__PURE__ */ jsx(Fragment, {
83
+ children: childrenToRender.map((child) => /* @__PURE__ */ jsx(PresenceChild, {
84
+ isPresent: true,
85
+ exitVariant,
86
+ enterVariant,
87
+ initial: initial ? void 0 : false,
88
+ presenceAffectsLayout,
89
+ children: child
90
+ }, getChildKey(child)))
91
+ });
80
92
  }
81
93
  childrenToRender = [...childrenToRender];
82
94
  const presentKeys = presentChildren.current.map(getChildKey);
@@ -98,42 +110,52 @@ const AnimatePresence = /* @__PURE__ */ __name(({
98
110
  if (!child)
99
111
  return;
100
112
  const insertionIndex = presentKeys.indexOf(key);
101
- childrenToRender.splice(insertionIndex, 0, /* @__PURE__ */ React.createElement(PresenceChild, {
102
- key: getChildKey(child),
103
- isEntering: false,
104
- onExitComplete: () => {
105
- allChildren.delete(key);
106
- exiting.delete(key);
107
- const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
108
- presentChildren.current.splice(removeIndex, 1);
109
- if (!exiting.size) {
110
- presentChildren.current = filteredChildren;
111
- if (isMounted === false)
112
- return;
113
- forceRender();
114
- onExitComplete == null ? void 0 : onExitComplete();
115
- }
116
- },
117
- exitVariant,
118
- enterVariant,
119
- presenceAffectsLayout
120
- }, child));
113
+ childrenToRender.splice(
114
+ insertionIndex,
115
+ 0,
116
+ /* @__PURE__ */ jsx(PresenceChild, {
117
+ isPresent: false,
118
+ onExitComplete: () => {
119
+ allChildren.delete(key);
120
+ exiting.delete(key);
121
+ const removeIndex = presentChildren.current.findIndex(
122
+ (presentChild) => presentChild.key === key
123
+ );
124
+ presentChildren.current.splice(removeIndex, 1);
125
+ if (!exiting.size) {
126
+ presentChildren.current = filteredChildren;
127
+ if (isMounted.current === false)
128
+ return;
129
+ forceRender();
130
+ onExitComplete == null ? void 0 : onExitComplete();
131
+ }
132
+ },
133
+ exitVariant,
134
+ enterVariant,
135
+ presenceAffectsLayout,
136
+ children: child
137
+ }, getChildKey(child))
138
+ );
121
139
  });
122
140
  childrenToRender = childrenToRender.map((child) => {
123
141
  const key = child.key;
124
- return exiting.has(key) ? child : /* @__PURE__ */ React.createElement(PresenceChild, {
125
- key: getChildKey(child),
126
- isEntering: void 0,
142
+ return exiting.has(key) ? child : /* @__PURE__ */ jsx(PresenceChild, {
143
+ isPresent: true,
127
144
  exitVariant,
128
145
  enterVariant,
129
- presenceAffectsLayout
130
- }, child);
146
+ presenceAffectsLayout,
147
+ children: child
148
+ }, getChildKey(child));
131
149
  });
132
150
  if (process.env.NODE_ENV !== "production" && exitBeforeEnter && childrenToRender.length > 1) {
133
- console.warn(`You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`);
151
+ console.warn(
152
+ `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`
153
+ );
134
154
  }
135
- return /* @__PURE__ */ React.createElement(React.Fragment, null, exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child)));
136
- }, "AnimatePresence");
155
+ return /* @__PURE__ */ jsx(Fragment, {
156
+ children: exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))
157
+ });
158
+ };
137
159
  AnimatePresence.displayName = "AnimatePresence";
138
160
  export {
139
161
  AnimatePresence
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/AnimatePresence.tsx"],
4
- "sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useRef,\n useState,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\n\nReact['keep']\n\nexport interface AnimatePresenceProps {\n /**\n * By passing `initial={false}`, `AnimatePresence` will disable any initial animations on children\n * that are present when the component is first rendered.\n */\n initial?: boolean\n\n /**\n * When a component is removed, there's no longer a chance to update its props. So if a component's `exit`\n * prop is defined as a dynamic variant and you want to pass a new `custom` prop, you can do so via `AnimatePresence`.\n * This will ensure all leaving components animate using the latest data.\n */\n exitVariant?: string | null\n\n enterVariant?: string | null\n\n /**\n * Fires when all exiting nodes have completed animating out.\n */\n onExitComplete?: () => void\n\n /**\n * If set to `true`, `AnimatePresence` will only render one component at a time. The exiting component\n * will finish its exit animation before the entering component is rendered.\n */\n exitBeforeEnter?: boolean\n\n /**\n * Used in Framer to flag that sibling children *shouldn't* re-render as a result of a\n * child being removed.\n */\n presenceAffectsLayout?: boolean\n}\n\ntype ComponentKey = string | number\n\nconst getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n\nconst isDev = process.env.NODE_ENV !== 'production'\n\nfunction updateChildLookup(\n children: ReactElement<any>[],\n allChildren: Map<ComponentKey, ReactElement<any>>\n) {\n const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n children.forEach((child) => {\n const key = getChildKey(child)\n\n if (isDev && seenChildren && seenChildren.has(key)) {\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n\n seenChildren.add(key)\n }\n\n allChildren.set(key, child)\n })\n}\n\nfunction onlyElements(children: ReactNode): ReactElement<any>[] {\n const filtered: ReactElement<any>[] = []\n\n // We use forEach here instead of map as map mutates the component key by preprending `.$`\n Children.forEach(children, (child) => {\n if (isValidElement(child)) filtered.push(child)\n })\n\n return filtered\n}\n\n/**\n * `AnimatePresence` enables the animation of components that have been removed from the tree.\n *\n * When adding/removing more than a single child, every child **must** be given a unique `key` prop.\n *\n * Any `motion` components that have an `exit` property defined will animate out when removed from\n * the tree.\n *\n * ```jsx\n * import { motion, AnimatePresence } from 'framer-motion'\n *\n * export const Items = ({ items }) => (\n * <AnimatePresence>\n * {items.map(item => (\n * <motion.div\n * key={item.id}\n * initial={{ opacity: 0 }}\n * animate={{ opacity: 1 }}\n * exit={{ opacity: 0 }}\n * />\n * ))}\n * </AnimatePresence>\n * )\n * ```\n *\n * You can sequence exit animations throughout a tree using variants.\n *\n * If a child contains multiple `motion` components with `exit` props, it will only unmount the child\n * once all `motion` components have finished animating out. Likewise, any components using\n * `useEntering` all need to call `safeToRemove`.\n *\n * @public\n */\nexport const AnimatePresence: React.FunctionComponent<\n React.PropsWithChildren<AnimatePresenceProps>\n> = ({\n children,\n enterVariant,\n exitVariant,\n initial = true,\n onExitComplete,\n exitBeforeEnter,\n presenceAffectsLayout = true,\n}) => {\n // We want to force a re-render once all exiting animations have finished. We\n // either use a local forceRender function, or one from a parent context if it exists.\n let forceRender = useForceUpdate()\n const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender\n if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup\n\n const [isMounted, setIsMounted] = useState(false)\n\n useIsomorphicLayoutEffect(() => {\n setIsMounted(true)\n }, [])\n\n // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key\n const filteredChildren = onlyElements(children)\n let childrenToRender = filteredChildren\n\n const exiting = new Set<ComponentKey>()\n\n // Keep a living record of the children we're actually rendering so we\n // can diff to figure out which are entering and exiting\n const presentChildren = useRef(childrenToRender)\n\n // A lookup table to quickly reference components by key\n const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current\n\n // If this is the initial component render, just deal with logic surrounding whether\n // we play onMount animations or not.\n const isInitialRender = useRef(true)\n\n useIsomorphicLayoutEffect(() => {\n isInitialRender.current = false\n updateChildLookup(filteredChildren, allChildren)\n presentChildren.current = childrenToRender\n })\n\n useUnmountEffect(() => {\n isInitialRender.current = true\n allChildren.clear()\n exiting.clear()\n })\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isEntering\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n initial={initial ? undefined : false}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n ))}\n </>\n )\n }\n\n // If this is a subsequent render, deal with entering and exiting children\n childrenToRender = [...childrenToRender]\n\n // Diff the keys of the currently-present and target children to update our\n // exiting list.\n const presentKeys = presentChildren.current.map(getChildKey)\n const targetKeys = filteredChildren.map(getChildKey)\n\n // Diff the present children with our target children and mark those that are exiting\n const numPresent = presentKeys.length\n for (let i = 0; i < numPresent; i++) {\n const key = presentKeys[i]\n\n if (targetKeys.indexOf(key) === -1) {\n exiting.add(key)\n }\n }\n\n // If we currently have exiting children, and we're deferring rendering incoming children\n // until after all current children have exiting, empty the childrenToRender array\n if (exitBeforeEnter && exiting.size) {\n childrenToRender = []\n }\n\n // Loop through all currently exiting components and clone them to overwrite `animate`\n // with any `exit` prop they might have defined.\n exiting.forEach((key) => {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) return\n\n const child = allChildren.get(key)\n if (!child) return\n\n const insertionIndex = presentKeys.indexOf(key)\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isEntering={false}\n onExitComplete={() => {\n allChildren.delete(key)\n exiting.delete(key)\n\n // Remove this child from the present children\n const removeIndex = presentChildren.current.findIndex(\n (presentChild) => presentChild.key === key\n )\n presentChildren.current.splice(removeIndex, 1)\n\n // Defer re-rendering until all exiting children have indeed left\n if (!exiting.size) {\n presentChildren.current = filteredChildren\n\n if (isMounted === false) return\n\n forceRender()\n onExitComplete?.()\n }\n }}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n // Add `MotionContext` even to children that don't need it to ensure we're rendering\n // the same tree between renders\n childrenToRender = childrenToRender.map((child) => {\n const key = child.key as string | number\n return exiting.has(key) ? (\n child\n ) : (\n <PresenceChild\n key={getChildKey(child)}\n isEntering={undefined}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {\n console.warn(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`\n )\n }\n\n return (\n <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
5
- "mappings": ";;AAAA;AACA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAWA;AACA;AAEA,MAAM;AAsCN,MAAM,cAAc,wBAAC,UAA2C,MAAM,OAAO,IAAzD;AAEpB,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,2BACE,UACA,aACA;AACA,QAAM,eAAe,QAAQ,oBAAI,IAAkB,IAAI;AAEvD,WAAS,QAAQ,CAAC,UAAU;AAC1B,UAAM,MAAM,YAAY,KAAK;AAE7B,QAAI,SAAS,gBAAgB,aAAa,IAAI,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,YAAY,KAAK;AAAA,MACtB,YAAY;AAAA,MACZ;AAAA,MACA;AAAA,MACA;AAAA,OAEC,KACH;AAAA,EAEJ,CAAC;AAED,MAAI,QAAQ,IAAI,aAAa,gBAAgB,mBAAmB,iBAAiB,SAAS,GAAG;AAC3F,YAAQ,KACN,6JACF;AAAA,EACF;AAEA,SACE,0DAAG,QAAQ,OAAO,mBAAmB,iBAAiB,IAAI,CAAC,UAAU,aAAa,KAAK,CAAC,CAAE;AAE9F,GAvKI;AAyKJ,gBAAgB,cAAc;",
4
+ "sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\nimport { AnimatePresenceProps } from './types'\n\nReact['keep']\n\ntype ComponentKey = string | number\n\nconst getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n\nconst isDev = process.env.NODE_ENV !== 'production'\n\nfunction updateChildLookup(\n children: ReactElement<any>[],\n allChildren: Map<ComponentKey, ReactElement<any>>\n) {\n const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n children.forEach((child) => {\n const key = getChildKey(child)\n\n if (isDev && seenChildren && seenChildren.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n\n seenChildren.add(key)\n }\n\n allChildren.set(key, child)\n })\n}\n\nfunction onlyElements(children: ReactNode): ReactElement<any>[] {\n const filtered: ReactElement<any>[] = []\n\n // We use forEach here instead of map as map mutates the component key by preprending `.$`\n Children.forEach(children, (child, index) => {\n if (isValidElement(child)) {\n if (!child.key) {\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(`No key given to AnimatePresence child, assigning index as key`)\n }\n filtered.push(\n React.cloneElement(child, {\n key: index,\n })\n )\n } else {\n filtered.push(child)\n }\n }\n })\n\n return filtered\n}\n\n/**\n * `AnimatePresence` enables the animation of components that have been removed from the tree.\n *\n * When adding/removing more than a single child, every child **must** be given a unique `key` prop.\n *\n * Any `motion` components that have an `exit` property defined will animate out when removed from\n * the tree.\n *\n * ```tsx\n * import { motion, AnimatePresence } from 'framer-motion'\n *\n * export const Items = ({ items }) => (\n * <AnimatePresence>\n * {items.map(item => (\n * <motion.div\n * key={item.id}\n * initial={{ opacity: 0 }}\n * animate={{ opacity: 1 }}\n * exit={{ opacity: 0 }}\n * />\n * ))}\n * </AnimatePresence>\n * )\n * ```\n *\n * You can sequence exit animations throughout a tree using variants.\n *\n * If a child contains multiple `motion` components with `exit` props, it will only unmount the child\n * once all `motion` components have finished animating out. Likewise, any components using\n * `usePresence` all need to call `safeToRemove`.\n *\n * @public\n */\nexport const AnimatePresence: React.FunctionComponent<\n React.PropsWithChildren<AnimatePresenceProps>\n> = ({\n children,\n enterVariant,\n exitVariant,\n initial = true,\n onExitComplete,\n exitBeforeEnter,\n presenceAffectsLayout = true,\n}) => {\n // We want to force a re-render once all exiting animations have finished. We\n // either use a local forceRender function, or one from a parent context if it exists.\n let forceRender = useForceUpdate()\n const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender\n if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup\n\n const isMounted = useRef(false)\n\n // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key\n const filteredChildren = onlyElements(children)\n let childrenToRender = filteredChildren\n\n const exiting = new Set<ComponentKey>()\n\n // Keep a living record of the children we're actually rendering so we\n // can diff to figure out which are entering and exiting\n const presentChildren = useRef(childrenToRender)\n\n // A lookup table to quickly reference components by key\n const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current\n\n // If this is the initial component render, just deal with logic surrounding whether\n // we play onMount animations or not.\n const isInitialRender = useRef(true)\n\n useEffect(() => {\n isMounted.current = true\n return () => {\n isMounted.current = false\n isInitialRender.current = true\n allChildren.clear()\n exiting.clear()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n isInitialRender.current = false\n updateChildLookup(filteredChildren, allChildren)\n presentChildren.current = childrenToRender\n })\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n initial={initial ? undefined : false}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n ))}\n </>\n )\n }\n\n // If this is a subsequent render, deal with entering and exiting children\n childrenToRender = [...childrenToRender]\n\n // Diff the keys of the currently-present and target children to update our\n // exiting list.\n const presentKeys = presentChildren.current.map(getChildKey)\n const targetKeys = filteredChildren.map(getChildKey)\n\n // Diff the present children with our target children and mark those that are exiting\n const numPresent = presentKeys.length\n for (let i = 0; i < numPresent; i++) {\n const key = presentKeys[i]\n if (targetKeys.indexOf(key) === -1) {\n exiting.add(key)\n }\n }\n\n // If we currently have exiting children, and we're deferring rendering incoming children\n // until after all current children have exiting, empty the childrenToRender array\n if (exitBeforeEnter && exiting.size) {\n childrenToRender = []\n }\n\n // Loop through all currently exiting components and clone them to overwrite `animate`\n // with any `exit` prop they might have defined.\n exiting.forEach((key) => {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) return\n\n const child = allChildren.get(key)\n if (!child) return\n\n const insertionIndex = presentKeys.indexOf(key)\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={() => {\n allChildren.delete(key)\n exiting.delete(key)\n\n // Remove this child from the present children\n const removeIndex = presentChildren.current.findIndex(\n (presentChild) => presentChild.key === key\n )\n presentChildren.current.splice(removeIndex, 1)\n\n // Defer re-rendering until all exiting children have indeed left\n if (!exiting.size) {\n presentChildren.current = filteredChildren\n\n if (isMounted.current === false) return\n\n forceRender()\n onExitComplete?.()\n }\n }}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n // Add `MotionContext` even to children that don't need it to ensure we're rendering\n // the same tree between renders\n childrenToRender = childrenToRender.map((child) => {\n const key = child.key as string | number\n return exiting.has(key) ? (\n child\n ) : (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {\n // eslint-disable-next-line no-console\n console.warn(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`\n )\n }\n\n return (\n <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
5
+ "mappings": "AA6JM,mBAEI,WAFJ;AA7JN,SAAS,gBAAgB,iCAAiC;AAC1D,OAAO;AAAA,EACL;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0BAA0B;AACnC,SAAS,qBAAqB;AAG9B,MAAM;AAIN,MAAM,cAAc,CAAC,UAA2C,MAAM,OAAO;AAE7E,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,SAAS,kBACP,UACA,aACA;AACA,QAAM,eAAe,QAAQ,oBAAI,IAAkB,IAAI;AAEvD,WAAS,QAAQ,CAAC,UAAU;AAC1B,UAAM,MAAM,YAAY,KAAK;AAE7B,QAAI,SAAS,gBAAgB,aAAa,IAAI,GAAG,GAAG;AAElD,cAAQ,KAAK,qDAAqD,sBAAsB;AAExF,mBAAa,IAAI,GAAG;AAAA,IACtB;AAEA,gBAAY,IAAI,KAAK,KAAK;AAAA,EAC5B,CAAC;AACH;AAEA,SAAS,aAAa,UAA0C;AAC9D,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AI,CAAC,UAAU;AACjD,UAAM,MAAM,MAAM;AAClB,WAAO,QAAQ,IAAI,GAAG,IACpB,QAEA,oBAAC;AAAA,MAEC,WAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MAEC;AAAA,OANI,YAAY,KAAK,CAOxB;AAAA,EAEJ,CAAC;AAED,MAAI,QAAQ,IAAI,aAAa,gBAAgB,mBAAmB,iBAAiB,SAAS,GAAG;AAE3F,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAEA,SACE;AAAA,IAAG,kBAAQ,OAAO,mBAAmB,iBAAiB,IAAI,CAAC,UAAU,aAAa,KAAK,CAAC;AAAA,GAAE;AAE9F;AAEA,gBAAgB,cAAc;",
6
6
  "names": []
7
7
  }
File without changes
@@ -2,6 +2,6 @@
2
2
  "version": 3,
3
3
  "sources": ["../../src/LayoutGroupContext.tsx"],
4
4
  "sourcesContent": ["import { createContext } from 'react'\n\nexport interface LayoutGroupContextProps {\n id?: string\n forceRender?: VoidFunction\n}\n\nexport const LayoutGroupContext = createContext<LayoutGroupContextProps>({})\n"],
5
- "mappings": "AAAA;AAOO,MAAM,qBAAqB,cAAuC,CAAC,CAAC;",
5
+ "mappings": "AAAA,SAAS,qBAAqB;AAOvB,MAAM,qBAAqB,cAAuC,CAAC,CAAC;",
6
6
  "names": []
7
7
  }
@@ -1,57 +1,57 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useConstant, useId } from "@tamagui/core";
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { PresenceContext } from "@tamagui/use-presence";
4
3
  import * as React from "react";
5
- import { AnimatePresenceContext } from "./AnimatePresenceContext";
6
- const PresenceChild = /* @__PURE__ */ __name(({
4
+ import { useId } from "react";
5
+ const PresenceChild = ({
7
6
  children,
8
7
  initial,
9
- isEntering,
8
+ isPresent,
10
9
  onExitComplete,
11
10
  exitVariant,
12
11
  enterVariant,
13
12
  presenceAffectsLayout
14
13
  }) => {
15
- const presenceChildren = useConstant(newChildrenMap);
16
- const id = useId();
17
- const context = React.useMemo(() => {
18
- return {
19
- id,
20
- initial,
21
- isEntering,
22
- exitVariant,
23
- enterVariant,
24
- onExitComplete: (childId) => {
25
- presenceChildren.set(childId, true);
26
- for (const isComplete of presenceChildren.values()) {
27
- if (!isComplete) {
28
- return;
14
+ const presenceChildren = React.useMemo(newChildrenMap, []);
15
+ const id = useId() || "";
16
+ const context = React.useMemo(
17
+ () => {
18
+ return {
19
+ id,
20
+ initial,
21
+ isPresent,
22
+ exitVariant,
23
+ enterVariant,
24
+ onExitComplete: (id2) => {
25
+ presenceChildren.set(id2, true);
26
+ for (const isComplete of presenceChildren.values()) {
27
+ if (!isComplete) {
28
+ return;
29
+ }
29
30
  }
31
+ onExitComplete == null ? void 0 : onExitComplete();
32
+ },
33
+ register: (id2) => {
34
+ presenceChildren.set(id2, false);
35
+ return () => presenceChildren.delete(id2);
30
36
  }
31
- onExitComplete == null ? void 0 : onExitComplete();
32
- },
33
- register: (childId) => {
34
- presenceChildren.set(childId, false);
35
- return () => presenceChildren.delete(childId);
36
- }
37
- };
38
- }, presenceAffectsLayout ? void 0 : [isEntering, onExitComplete, exitVariant, enterVariant]);
37
+ };
38
+ },
39
+ presenceAffectsLayout ? void 0 : [isPresent, exitVariant, enterVariant]
40
+ );
39
41
  React.useMemo(() => {
40
42
  presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
41
- }, [isEntering]);
43
+ }, [isPresent]);
42
44
  React.useEffect(() => {
43
- if (isEntering === false && !presenceChildren.size) {
44
- onExitComplete == null ? void 0 : onExitComplete();
45
- }
46
- }, [isEntering]);
47
- return /* @__PURE__ */ React.createElement(AnimatePresenceContext.Provider, {
48
- value: context
49
- }, children);
50
- }, "PresenceChild");
45
+ !isPresent && !presenceChildren.size && (onExitComplete == null ? void 0 : onExitComplete());
46
+ }, [isPresent]);
47
+ return /* @__PURE__ */ jsx(PresenceContext.Provider, {
48
+ value: context,
49
+ children
50
+ });
51
+ };
51
52
  function newChildrenMap() {
52
53
  return /* @__PURE__ */ new Map();
53
54
  }
54
- __name(newChildrenMap, "newChildrenMap");
55
55
  export {
56
56
  PresenceChild
57
57
  };
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/PresenceChild.tsx"],
4
- "sourcesContent": ["import { useConstant, useId } from '@tamagui/core'\nimport * as React from 'react'\n\nimport { AnimatePresenceContext, AnimatePresenceContextProps } from './AnimatePresenceContext'\nimport { VariantLabels } from './types'\n\ninterface PresenceChildProps {\n children: React.ReactElement<any>\n isEntering: boolean | undefined\n onExitComplete?: () => void\n initial?: false | VariantLabels\n exitVariant?: string | null\n enterVariant?: string | null\n presenceAffectsLayout: boolean\n}\n\nexport const PresenceChild = ({\n children,\n initial,\n isEntering,\n onExitComplete,\n exitVariant,\n enterVariant,\n presenceAffectsLayout,\n}: PresenceChildProps) => {\n const presenceChildren = useConstant(newChildrenMap)\n const id = useId()\n\n const context = React.useMemo(\n (): AnimatePresenceContextProps => {\n return {\n id,\n initial,\n isEntering,\n exitVariant,\n enterVariant,\n onExitComplete: (childId: string) => {\n presenceChildren.set(childId, true)\n for (const isComplete of presenceChildren.values()) {\n if (!isComplete) {\n return // can stop searching when any is incomplete\n }\n }\n onExitComplete?.()\n },\n register: (childId: string) => {\n presenceChildren.set(childId, false)\n return () => presenceChildren.delete(childId)\n },\n }\n },\n /**\n * If the presence of a child affects the layout of the components around it,\n * we want to make a new context value to ensure they get re-rendered\n * so they can detect that layout change.\n */\n presenceAffectsLayout ? undefined : [isEntering, onExitComplete, exitVariant, enterVariant]\n )\n\n // cant this be a useEffect?\n React.useMemo(() => {\n presenceChildren.forEach((_, key) => presenceChildren.set(key, false))\n }, [isEntering])\n\n /**\n * If there's no animated components to fire exit animations, we want to remove this\n * component immediately.\n */\n React.useEffect(() => {\n if (isEntering === false && !presenceChildren.size) {\n onExitComplete?.()\n }\n }, [isEntering])\n\n return (\n <AnimatePresenceContext.Provider value={context}>{children}</AnimatePresenceContext.Provider>\n )\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
5
- "mappings": ";;AAAA;AACA;AAEA;AAaO,MAAM,gBAAgB,wBAAC;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,MACwB;AACxB,QAAM,mBAAmB,YAAY,cAAc;AACnD,QAAM,KAAK,MAAM;AAEjB,QAAM,UAAU,MAAM,QACpB,MAAmC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,gBAAgB,CAAC,YAAoB;AACnC,yBAAiB,IAAI,SAAS,IAAI;AAClC,mBAAW,cAAc,iBAAiB,OAAO,GAAG;AAClD,cAAI,CAAC,YAAY;AACf;AAAA,UACF;AAAA,QACF;AACA;AAAA,MACF;AAAA,MACA,UAAU,CAAC,YAAoB;AAC7B,yBAAiB,IAAI,SAAS,KAAK;AACnC,eAAO,MAAM,iBAAiB,OAAO,OAAO;AAAA,MAC9C;AAAA,IACF;AAAA,EACF,GAMA,wBAAwB,SAAY,CAAC,YAAY,gBAAgB,aAAa,YAAY,CAC5F;AAGA,QAAM,QAAQ,MAAM;AAClB,qBAAiB,QAAQ,CAAC,GAAG,QAAQ,iBAAiB,IAAI,KAAK,KAAK,CAAC;AAAA,EACvE,GAAG,CAAC,UAAU,CAAC;AAMf,QAAM,UAAU,MAAM;AACpB,QAAI,eAAe,SAAS,CAAC,iBAAiB,MAAM;AAClD;AAAA,IACF;AAAA,EACF,GAAG,CAAC,UAAU,CAAC;AAEf,SACE,oCAAC,uBAAuB,UAAvB;AAAA,IAAgC,OAAO;AAAA,KAAU,QAAS;AAE/D,GA7D6B;AA+D7B,0BAAgD;AAC9C,SAAO,oBAAI,IAAI;AACjB;AAFS;",
6
- "names": []
4
+ "sourcesContent": ["import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'\nimport * as React from 'react'\nimport { useId } from 'react'\n\nimport { VariantLabels } from './types'\n\ninterface PresenceChildProps {\n children: React.ReactElement<any>\n isPresent: boolean\n onExitComplete?: () => void\n initial?: false | VariantLabels\n custom?: any\n presenceAffectsLayout: boolean\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceChild = ({\n children,\n initial,\n isPresent,\n onExitComplete,\n exitVariant,\n enterVariant,\n presenceAffectsLayout,\n}: PresenceChildProps) => {\n const presenceChildren = React.useMemo(newChildrenMap, [])\n const id = useId() || ''\n\n const context = React.useMemo(\n (): PresenceContextProps => {\n return {\n id,\n initial,\n isPresent,\n exitVariant,\n enterVariant,\n onExitComplete: (id: string) => {\n presenceChildren.set(id, true)\n for (const isComplete of presenceChildren.values()) {\n if (!isComplete) {\n return // can stop searching when any is incomplete\n }\n }\n onExitComplete?.()\n },\n register: (id: string) => {\n presenceChildren.set(id, false)\n return () => presenceChildren.delete(id)\n },\n }\n },\n /**\n * If the presence of a child affects the layout of the components around it,\n * we want to make a new context value to ensure they get re-rendered\n * so they can detect that layout change.\n */\n // eslint-disable-next-line react-hooks/exhaustive-deps\n presenceAffectsLayout ? undefined : [isPresent, exitVariant, enterVariant]\n )\n\n React.useMemo(() => {\n presenceChildren.forEach((_, key) => presenceChildren.set(key, false))\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n /**\n * If there's no animated components to fire exit animations, we want to remove this\n * component immediately.\n */\n React.useEffect(() => {\n !isPresent && !presenceChildren.size && onExitComplete?.()\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
5
+ "mappings": "AA2ES;AA3ET,SAAS,uBAA6C;AACtD,YAAY,WAAW;AACvB,SAAS,aAAa;AAef,MAAM,gBAAgB,CAAC;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA0B;AACxB,QAAM,mBAAmB,MAAM,QAAQ,gBAAgB,CAAC,CAAC;AACzD,QAAM,KAAK,MAAM,KAAK;AAEtB,QAAM,UAAU,MAAM;AAAA,IACpB,MAA4B;AAC1B,aAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,CAACA,QAAe;AAC9B,2BAAiB,IAAIA,KAAI,IAAI;AAC7B,qBAAW,cAAc,iBAAiB,OAAO,GAAG;AAClD,gBAAI,CAAC,YAAY;AACf;AAAA,YACF;AAAA,UACF;AACA;AAAA,QACF;AAAA,QACA,UAAU,CAACA,QAAe;AACxB,2BAAiB,IAAIA,KAAI,KAAK;AAC9B,iBAAO,MAAM,iBAAiB,OAAOA,GAAE;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA,IAOA,wBAAwB,SAAY,CAAC,WAAW,aAAa,YAAY;AAAA,EAC3E;AAEA,QAAM,QAAQ,MAAM;AAClB,qBAAiB,QAAQ,CAAC,GAAG,QAAQ,iBAAiB,IAAI,KAAK,KAAK,CAAC;AAAA,EAEvE,GAAG,CAAC,SAAS,CAAC;AAMd,QAAM,UAAU,MAAM;AACpB,KAAC,aAAa,CAAC,iBAAiB,SAAQ;AAAA,EAE1C,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO,oBAAC,gBAAgB,UAAhB;AAAA,IAAyB,OAAO;AAAA,IAAU;AAAA,GAAS;AAC7D;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
6
+ "names": ["id"]
7
7
  }
@@ -0,0 +1,6 @@
1
+ import { createContext } from "react";
2
+ const PresenceContext = createContext(null);
3
+ export {
4
+ PresenceContext
5
+ };
6
+ //# sourceMappingURL=PresenceContext.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/PresenceContext.tsx"],
4
+ "sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface PresenceContextProps {\n id: string\n isPresent: boolean\n register: (id: string) => () => void\n onExitComplete?: (id: string) => void\n initial?: false | VariantLabels\n custom?: any\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceContext = createContext<PresenceContextProps | null>(null)\n"],
5
+ "mappings": "AAAA,SAAS,qBAAqB;AAevB,MAAM,kBAAkB,cAA2C,IAAI;",
6
+ "names": []
7
+ }
package/dist/esm/index.js CHANGED
@@ -1,7 +1,3 @@
1
1
  export * from "./AnimatePresence";
2
- export * from "./useEntering";
3
- import { AnimatePresenceContext } from "./AnimatePresenceContext";
4
- export {
5
- AnimatePresenceContext
6
- };
2
+ export * from "@tamagui/use-presence";
7
3
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.ts"],
4
- "sourcesContent": ["export * from './AnimatePresence'\nexport * from './useEntering'\nexport { AnimatePresenceContext } from './AnimatePresenceContext'\n"],
5
- "mappings": "AAAA;AACA;AACA;",
4
+ "sourcesContent": ["export * from './AnimatePresence'\nexport * from '@tamagui/use-presence'\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;",
6
6
  "names": []
7
7
  }
package/dist/esm/types.js CHANGED
File without changes
File without changes
@@ -0,0 +1,26 @@
1
+ import { useId } from "@tamagui/core";
2
+ import { useContext, useEffect } from "react";
3
+ import { PresenceContext } from "./PresenceContext";
4
+ function usePresence() {
5
+ const context = useContext(PresenceContext);
6
+ if (context === null) {
7
+ return [true, null];
8
+ }
9
+ const { isPresent: isPresent2, onExitComplete, register } = context;
10
+ const id = useId() || "";
11
+ useEffect(() => register(id), [id, register]);
12
+ const safeToRemove = () => onExitComplete == null ? void 0 : onExitComplete(id);
13
+ return !isPresent2 && onExitComplete ? [false, safeToRemove] : [true];
14
+ }
15
+ function useIsPresent() {
16
+ return isPresent(useContext(PresenceContext));
17
+ }
18
+ function isPresent(context) {
19
+ return context === null ? true : context.isPresent;
20
+ }
21
+ export {
22
+ isPresent,
23
+ useIsPresent,
24
+ usePresence
25
+ };
26
+ //# sourceMappingURL=usePresence.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/usePresence.ts"],
4
+ "sourcesContent": ["import { useId } from '@tamagui/core'\nimport { useContext, useEffect } from 'react'\n\nimport { PresenceContext, PresenceContextProps } from './PresenceContext'\n\nexport type SafeToRemove = () => void\ntype AlwaysPresent = [true, null]\ntype Present = [true]\ntype NotPresent = [false, SafeToRemove]\n\nexport function usePresence(): AlwaysPresent | Present | NotPresent {\n const context = useContext(PresenceContext)\n\n if (context === null) {\n return [true, null]\n }\n\n const { isPresent, onExitComplete, register } = context\n const id = useId() || ''\n useEffect(() => register(id), [id, register])\n\n const safeToRemove = () => onExitComplete?.(id)\n\n return !isPresent && onExitComplete ? [false, safeToRemove] : [true]\n}\n\n/**\n * Similar to `usePresence`, except `useIsPresent` simply returns whether or not the component is present.\n * There is no `safeToRemove` function. ```\n */\nexport function useIsPresent() {\n return isPresent(useContext(PresenceContext))\n}\n\nexport function isPresent(context: PresenceContextProps | null) {\n return context === null ? true : context.isPresent\n}\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AACtB,SAAS,YAAY,iBAAiB;AAEtC,SAAS,uBAA6C;AAO/C,SAAS,cAAoD;AAClE,QAAM,UAAU,WAAW,eAAe;AAE1C,MAAI,YAAY,MAAM;AACpB,WAAO,CAAC,MAAM,IAAI;AAAA,EACpB;AAEA,QAAM,EAAE,WAAAA,YAAW,gBAAgB,SAAS,IAAI;AAChD,QAAM,KAAK,MAAM,KAAK;AACtB,YAAU,MAAM,SAAS,EAAE,GAAG,CAAC,IAAI,QAAQ,CAAC;AAE5C,QAAM,eAAe,MAAM,iDAAiB;AAE5C,SAAO,CAACA,cAAa,iBAAiB,CAAC,OAAO,YAAY,IAAI,CAAC,IAAI;AACrE;AAMO,SAAS,eAAe;AAC7B,SAAO,UAAU,WAAW,eAAe,CAAC;AAC9C;AAEO,SAAS,UAAU,SAAsC;AAC9D,SAAO,YAAY,OAAO,OAAO,QAAQ;AAC3C;",
6
+ "names": ["isPresent"]
7
+ }
@@ -1,18 +1,16 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from "@tamagui/core";
1
+ import { useForceUpdate, useIsomorphicLayoutEffect } from "@tamagui/core";
4
2
  import React, {
5
3
  Children,
6
4
  cloneElement,
7
5
  isValidElement,
8
6
  useContext,
9
- useRef,
10
- useState
7
+ useEffect,
8
+ useRef
11
9
  } from "react";
12
10
  import { LayoutGroupContext } from "./LayoutGroupContext";
13
11
  import { PresenceChild } from "./PresenceChild";
14
12
  React["keep"];
15
- const getChildKey = /* @__PURE__ */ __name((child) => child.key || "", "getChildKey");
13
+ const getChildKey = (child) => child.key || "";
16
14
  const isDev = process.env.NODE_ENV !== "production";
17
15
  function updateChildLookup(children, allChildren) {
18
16
  const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
@@ -25,17 +23,27 @@ function updateChildLookup(children, allChildren) {
25
23
  allChildren.set(key, child);
26
24
  });
27
25
  }
28
- __name(updateChildLookup, "updateChildLookup");
29
26
  function onlyElements(children) {
30
27
  const filtered = [];
31
- Children.forEach(children, (child) => {
32
- if (isValidElement(child))
33
- filtered.push(child);
28
+ Children.forEach(children, (child, index) => {
29
+ if (isValidElement(child)) {
30
+ if (!child.key) {
31
+ if (process.env.NODE_ENV === "development") {
32
+ console.warn(`No key given to AnimatePresence child, assigning index as key`);
33
+ }
34
+ filtered.push(
35
+ React.cloneElement(child, {
36
+ key: index
37
+ })
38
+ );
39
+ } else {
40
+ filtered.push(child);
41
+ }
42
+ }
34
43
  });
35
44
  return filtered;
36
45
  }
37
- __name(onlyElements, "onlyElements");
38
- const AnimatePresence = /* @__PURE__ */ __name(({
46
+ const AnimatePresence = ({
39
47
  children,
40
48
  enterVariant,
41
49
  exitVariant,
@@ -48,28 +56,29 @@ const AnimatePresence = /* @__PURE__ */ __name(({
48
56
  const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
49
57
  if (forceRenderLayoutGroup)
50
58
  forceRender = forceRenderLayoutGroup;
51
- const [isMounted, setIsMounted] = useState(false);
52
- useIsomorphicLayoutEffect(() => {
53
- setIsMounted(true);
54
- }, []);
59
+ const isMounted = useRef(false);
55
60
  const filteredChildren = onlyElements(children);
56
61
  let childrenToRender = filteredChildren;
57
62
  const exiting = /* @__PURE__ */ new Set();
58
63
  const presentChildren = useRef(childrenToRender);
59
64
  const allChildren = useRef(/* @__PURE__ */ new Map()).current;
60
65
  const isInitialRender = useRef(true);
66
+ useEffect(() => {
67
+ isMounted.current = true;
68
+ return () => {
69
+ isMounted.current = false;
70
+ isInitialRender.current = true;
71
+ allChildren.clear();
72
+ exiting.clear();
73
+ };
74
+ }, []);
61
75
  useIsomorphicLayoutEffect(() => {
62
76
  isInitialRender.current = false;
63
77
  updateChildLookup(filteredChildren, allChildren);
64
78
  presentChildren.current = childrenToRender;
65
79
  });
66
- useUnmountEffect(() => {
67
- isInitialRender.current = true;
68
- allChildren.clear();
69
- exiting.clear();
70
- });
71
80
  if (isInitialRender.current) {
72
- return <>{childrenToRender.map((child) => <PresenceChild key={getChildKey(child)} isEntering exitVariant={exitVariant} enterVariant={enterVariant} initial={initial ? void 0 : false} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>)}</>;
81
+ return <>{childrenToRender.map((child) => <PresenceChild key={getChildKey(child)} isPresent exitVariant={exitVariant} enterVariant={enterVariant} initial={initial ? void 0 : false} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>)}</>;
73
82
  }
74
83
  childrenToRender = [...childrenToRender];
75
84
  const presentKeys = presentChildren.current.map(getChildKey);
@@ -91,30 +100,39 @@ const AnimatePresence = /* @__PURE__ */ __name(({
91
100
  if (!child)
92
101
  return;
93
102
  const insertionIndex = presentKeys.indexOf(key);
94
- childrenToRender.splice(insertionIndex, 0, <PresenceChild key={getChildKey(child)} isEntering={false} onExitComplete={() => {
95
- allChildren.delete(key);
96
- exiting.delete(key);
97
- const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
98
- presentChildren.current.splice(removeIndex, 1);
99
- if (!exiting.size) {
100
- presentChildren.current = filteredChildren;
101
- if (isMounted === false)
102
- return;
103
- forceRender();
104
- onExitComplete == null ? void 0 : onExitComplete();
105
- }
106
- }} exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>);
103
+ childrenToRender.splice(
104
+ insertionIndex,
105
+ 0,
106
+ <PresenceChild key={getChildKey(child)} isPresent={false} onExitComplete={() => {
107
+ allChildren.delete(key);
108
+ exiting.delete(key);
109
+ const removeIndex = presentChildren.current.findIndex(
110
+ (presentChild) => presentChild.key === key
111
+ );
112
+ presentChildren.current.splice(removeIndex, 1);
113
+ if (!exiting.size) {
114
+ presentChildren.current = filteredChildren;
115
+ if (isMounted.current === false)
116
+ return;
117
+ forceRender();
118
+ onExitComplete?.();
119
+ }
120
+ }} exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>
121
+ );
107
122
  });
108
123
  childrenToRender = childrenToRender.map((child) => {
109
124
  const key = child.key;
110
- return exiting.has(key) ? child : <PresenceChild key={getChildKey(child)} isEntering={void 0} exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>;
125
+ return exiting.has(key) ? child : <PresenceChild key={getChildKey(child)} isPresent exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>;
111
126
  });
112
127
  if (process.env.NODE_ENV !== "production" && exitBeforeEnter && childrenToRender.length > 1) {
113
- console.warn(`You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`);
128
+ console.warn(
129
+ `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`
130
+ );
114
131
  }
115
132
  return <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>;
116
- }, "AnimatePresence");
133
+ };
117
134
  AnimatePresence.displayName = "AnimatePresence";
118
135
  export {
119
136
  AnimatePresence
120
137
  };
138
+ //# sourceMappingURL=AnimatePresence.js.map