@tamagui/animate-presence 1.0.0-beta.202 → 1.0.0-beta.208
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.
- package/dist/cjs/AnimatePresence.js +42 -37
- package/dist/cjs/AnimatePresence.js.map +2 -2
- package/dist/cjs/AnimatePresenceContext.js.map +2 -2
- package/dist/cjs/PresenceChild.js +27 -23
- package/dist/cjs/PresenceChild.js.map +2 -2
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/esm/AnimatePresence.js +42 -40
- package/dist/esm/AnimatePresence.js.map +2 -2
- package/dist/esm/AnimatePresenceContext.js.map +2 -2
- package/dist/esm/PresenceChild.js +27 -23
- package/dist/esm/PresenceChild.js.map +2 -2
- package/dist/esm/index.js +1 -1
- package/dist/esm/index.js.map +1 -1
- package/dist/jsx/AnimatePresence.js +25 -27
- package/dist/jsx/PresenceChild.js +27 -23
- package/dist/jsx/index.js +1 -1
- package/package.json +4 -4
|
@@ -25,22 +25,22 @@ __export(AnimatePresence_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(AnimatePresence_exports);
|
|
27
27
|
var import_core = require("@tamagui/core");
|
|
28
|
-
var
|
|
29
|
-
var import_react = require("react");
|
|
28
|
+
var import_react = __toESM(require("react"));
|
|
30
29
|
var import_LayoutGroupContext = require("./LayoutGroupContext");
|
|
31
30
|
var import_PresenceChild = require("./PresenceChild");
|
|
31
|
+
import_react.default["keep"];
|
|
32
32
|
const getChildKey = /* @__PURE__ */ __name((child) => child.key || "", "getChildKey");
|
|
33
33
|
const isDev = process.env.NODE_ENV !== "production";
|
|
34
34
|
function updateChildLookup(children, allChildren) {
|
|
35
35
|
const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
|
|
36
|
-
|
|
36
|
+
children.forEach((child) => {
|
|
37
37
|
const key = getChildKey(child);
|
|
38
38
|
if (isDev && seenChildren && seenChildren.has(key)) {
|
|
39
39
|
console.warn(`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`);
|
|
40
40
|
seenChildren.add(key);
|
|
41
41
|
}
|
|
42
42
|
allChildren.set(key, child);
|
|
43
|
-
}
|
|
43
|
+
});
|
|
44
44
|
}
|
|
45
45
|
__name(updateChildLookup, "updateChildLookup");
|
|
46
46
|
function onlyElements(children) {
|
|
@@ -54,7 +54,8 @@ function onlyElements(children) {
|
|
|
54
54
|
__name(onlyElements, "onlyElements");
|
|
55
55
|
const AnimatePresence = /* @__PURE__ */ __name(({
|
|
56
56
|
children,
|
|
57
|
-
|
|
57
|
+
enterVariant,
|
|
58
|
+
exitVariant,
|
|
58
59
|
initial = true,
|
|
59
60
|
onExitComplete,
|
|
60
61
|
exitBeforeEnter,
|
|
@@ -64,7 +65,10 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
64
65
|
const forceRenderLayoutGroup = (0, import_react.useContext)(import_LayoutGroupContext.LayoutGroupContext).forceRender;
|
|
65
66
|
if (forceRenderLayoutGroup)
|
|
66
67
|
forceRender = forceRenderLayoutGroup;
|
|
67
|
-
const isMounted = (0,
|
|
68
|
+
const [isMounted, setIsMounted] = (0, import_react.useState)(false);
|
|
69
|
+
(0, import_react.useEffect)(() => {
|
|
70
|
+
setIsMounted(true);
|
|
71
|
+
}, []);
|
|
68
72
|
const filteredChildren = onlyElements(children);
|
|
69
73
|
let childrenToRender = filteredChildren;
|
|
70
74
|
const exiting = /* @__PURE__ */ new Set();
|
|
@@ -82,9 +86,11 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
82
86
|
exiting.clear();
|
|
83
87
|
});
|
|
84
88
|
if (isInitialRender.current) {
|
|
85
|
-
return /* @__PURE__ */
|
|
89
|
+
return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, childrenToRender.map((child) => /* @__PURE__ */ import_react.default.createElement(import_PresenceChild.PresenceChild, {
|
|
86
90
|
key: getChildKey(child),
|
|
87
|
-
|
|
91
|
+
isEntering: true,
|
|
92
|
+
exitVariant,
|
|
93
|
+
enterVariant,
|
|
88
94
|
initial: initial ? void 0 : false,
|
|
89
95
|
presenceAffectsLayout
|
|
90
96
|
}, child)));
|
|
@@ -102,48 +108,47 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
102
108
|
if (exitBeforeEnter && exiting.size) {
|
|
103
109
|
childrenToRender = [];
|
|
104
110
|
}
|
|
105
|
-
|
|
106
|
-
if (targetKeys.indexOf(key) !== -1)
|
|
107
|
-
|
|
108
|
-
}
|
|
111
|
+
exiting.forEach((key) => {
|
|
112
|
+
if (targetKeys.indexOf(key) !== -1)
|
|
113
|
+
return;
|
|
109
114
|
const child = allChildren.get(key);
|
|
110
|
-
if (!child)
|
|
111
|
-
|
|
112
|
-
}
|
|
115
|
+
if (!child)
|
|
116
|
+
return;
|
|
113
117
|
const insertionIndex = presentKeys.indexOf(key);
|
|
114
|
-
|
|
115
|
-
allChildren.delete(key);
|
|
116
|
-
exiting.delete(key);
|
|
117
|
-
const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
|
|
118
|
-
presentChildren.current.splice(removeIndex, 1);
|
|
119
|
-
if (!exiting.size) {
|
|
120
|
-
presentChildren.current = filteredChildren;
|
|
121
|
-
if (isMounted.current === false) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
forceRender();
|
|
125
|
-
onExitComplete == null ? void 0 : onExitComplete();
|
|
126
|
-
}
|
|
127
|
-
}, "onExit");
|
|
128
|
-
childrenToRender.splice(insertionIndex, 0, /* @__PURE__ */ React.createElement(import_PresenceChild.PresenceChild, {
|
|
118
|
+
childrenToRender.splice(insertionIndex, 0, /* @__PURE__ */ import_react.default.createElement(import_PresenceChild.PresenceChild, {
|
|
129
119
|
key: getChildKey(child),
|
|
130
|
-
|
|
131
|
-
onExitComplete:
|
|
132
|
-
|
|
120
|
+
isEntering: false,
|
|
121
|
+
onExitComplete: () => {
|
|
122
|
+
allChildren.delete(key);
|
|
123
|
+
exiting.delete(key);
|
|
124
|
+
const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
|
|
125
|
+
presentChildren.current.splice(removeIndex, 1);
|
|
126
|
+
if (!exiting.size) {
|
|
127
|
+
presentChildren.current = filteredChildren;
|
|
128
|
+
if (isMounted === false)
|
|
129
|
+
return;
|
|
130
|
+
forceRender();
|
|
131
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
exitVariant,
|
|
135
|
+
enterVariant,
|
|
133
136
|
presenceAffectsLayout
|
|
134
137
|
}, child));
|
|
135
|
-
}
|
|
138
|
+
});
|
|
136
139
|
childrenToRender = childrenToRender.map((child) => {
|
|
137
140
|
const key = child.key;
|
|
138
|
-
return exiting.has(key) ? child : /* @__PURE__ */
|
|
141
|
+
return exiting.has(key) ? child : /* @__PURE__ */ import_react.default.createElement(import_PresenceChild.PresenceChild, {
|
|
139
142
|
key: getChildKey(child),
|
|
140
|
-
|
|
143
|
+
isEntering: void 0,
|
|
144
|
+
exitVariant,
|
|
145
|
+
enterVariant,
|
|
141
146
|
presenceAffectsLayout
|
|
142
147
|
}, child);
|
|
143
148
|
});
|
|
144
149
|
if (process.env.NODE_ENV !== "production" && exitBeforeEnter && childrenToRender.length > 1) {
|
|
145
150
|
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.`);
|
|
146
151
|
}
|
|
147
|
-
return /* @__PURE__ */
|
|
152
|
+
return /* @__PURE__ */ import_react.default.createElement(import_react.default.Fragment, null, exiting.size ? childrenToRender : childrenToRender.map((child) => (0, import_react.cloneElement)(child)));
|
|
148
153
|
}, "AnimatePresence");
|
|
149
154
|
//# sourceMappingURL=AnimatePresence.js.map
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
-
"sourcesContent": ["import {\n useForceUpdate,\n useIsMounted,\n useIsomorphicLayoutEffect,\n useUnmountEffect,\n} from '@tamagui/core'\nimport * as React from 'react'\nimport {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\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 custom?: any\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 || ''\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 for (const child of children) {\n const key = getChildKey(child)\n if (isDev && seenChildren && seenChildren.has(key)) {\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n seenChildren.add(key)\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 animated 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 animated components with `exit` props, it will only unmount the child\n * once all animated 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<AnimatePresenceProps> = ({\n children,\n custom,\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 = useIsMounted()\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 isPresent\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 for (const key of exiting) {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) {\n continue\n }\n\n const child = allChildren.get(key)\n if (!child) {\n continue\n }\n\n const insertionIndex = presentKeys.indexOf(key)\n\n const onExit = () => {\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 if (isMounted.current === false) {\n return\n }\n forceRender()\n onExitComplete?.()\n }\n }\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={onExit}\n custom={custom}\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 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"],
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,
|
|
4
|
+
"sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\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 useEffect(() => {\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\n// import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from '@tamagui/core'\n// import React, {\n// Children,\n// ReactElement,\n// ReactNode,\n// cloneElement,\n// isValidElement,\n// useContext,\n// useEffect,\n// useRef,\n// useState,\n// } from 'react'\n\n// import { LayoutGroupContext } from './LayoutGroupContext'\n// import { PresenceChild } from './PresenceChild'\n\n// React['keep']\n\n// export 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\n// type ComponentKey = string | number\n\n// const getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n// const isDev = process.env.NODE_ENV !== 'production'\n\n// function updateChildLookup(\n// children: ReactElement<any>[],\n// allChildren: Map<ComponentKey, ReactElement<any>>\n// ) {\n// const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n// for (const child of children) {\n// const key = getChildKey(child)\n// if (isDev && seenChildren && seenChildren.has(key)) {\n// console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n// seenChildren.add(key)\n// }\n// allChildren.set(key, child)\n// }\n// }\n\n// function 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 animated 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 animated components with `exit` props, it will only unmount the child\n// * once all animated components have finished animating out. Likewise, any components using\n// * `useEntering` all need to call `safeToRemove`.\n// *\n// * @public\n// */\n// export const AnimatePresence: React.FunctionComponent<AnimatePresenceProps> = ({\n// children,\n// exitVariant,\n// enterVariant,\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// useEffect(() => {\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// console.log('AnimatePresence (iniital)', isInitialRender.current, {\n// isMounted,\n// childrenToRender,\n// })\n// return (\n// <>\n// {childrenToRender.map((child) => (\n// <PresenceChild\n// key={getChildKey(child)}\n// isEntering\n// initial={initial ? undefined : false}\n// presenceAffectsLayout={presenceAffectsLayout}\n// exitVariant={exitVariant}\n// enterVariant={enterVariant}\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// for (const key of exiting) {\n// // If this component is actually entering again, early return\n// if (targetKeys.indexOf(key) !== -1) {\n// continue\n// }\n\n// const child = allChildren.get(key)\n// if (!child) {\n// continue\n// }\n\n// const insertionIndex = presentKeys.indexOf(key)\n\n// const onExit = () => {\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// if (isMounted === false) {\n// return\n// }\n// forceRender()\n// onExitComplete?.()\n// }\n// }\n\n// childrenToRender.splice(\n// insertionIndex,\n// 0,\n// <PresenceChild\n// key={getChildKey(child)}\n// isEntering={false}\n// onExitComplete={onExit}\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// console.log(\n// 'AnimatePresence ()',\n// isInitialRender.current,\n// {\n// isMounted,\n// },\n// childrenToRender.map((x) => x.props.children.props.children.props.src)\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"],
|
|
5
|
+
"mappings": "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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresenceContext.tsx"],
|
|
4
|
-
"sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface AnimatePresenceContextProps {\n id: string\n
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA8B;
|
|
4
|
+
"sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface AnimatePresenceContextProps {\n id: string\n isEntering: boolean | undefined\n register: (id: string) => () => void\n onExitComplete?: (id: string) => void\n initial?: false | VariantLabels\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const AnimatePresenceContext = createContext<AnimatePresenceContextProps | null>(null)\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAA8B;AAcvB,MAAM,yBAAyB,gCAAkD,IAAI;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -31,41 +31,45 @@ var import_AnimatePresenceContext = require("./AnimatePresenceContext");
|
|
|
31
31
|
const PresenceChild = /* @__PURE__ */ __name(({
|
|
32
32
|
children,
|
|
33
33
|
initial,
|
|
34
|
-
|
|
34
|
+
isEntering,
|
|
35
35
|
onExitComplete,
|
|
36
|
-
|
|
36
|
+
exitVariant,
|
|
37
|
+
enterVariant,
|
|
37
38
|
presenceAffectsLayout
|
|
38
39
|
}) => {
|
|
39
40
|
const presenceChildren = (0, import_core.useConstant)(newChildrenMap);
|
|
40
41
|
const id = (0, import_utils.useId)();
|
|
41
|
-
const context = React.useMemo(() =>
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
const context = React.useMemo(() => {
|
|
43
|
+
return {
|
|
44
|
+
id,
|
|
45
|
+
initial,
|
|
46
|
+
isEntering,
|
|
47
|
+
exitVariant,
|
|
48
|
+
enterVariant,
|
|
49
|
+
onExitComplete: (childId) => {
|
|
50
|
+
console.log("on exist complete", childId);
|
|
51
|
+
presenceChildren.set(childId, true);
|
|
52
|
+
for (const isComplete of presenceChildren.values()) {
|
|
53
|
+
if (!isComplete) {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
51
56
|
}
|
|
57
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
58
|
+
},
|
|
59
|
+
register: (childId) => {
|
|
60
|
+
presenceChildren.set(childId, false);
|
|
61
|
+
return () => presenceChildren.delete(childId);
|
|
52
62
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
register: (childId) => {
|
|
56
|
-
presenceChildren.set(childId, false);
|
|
57
|
-
return () => presenceChildren.delete(childId);
|
|
58
|
-
}
|
|
59
|
-
}), presenceAffectsLayout ? void 0 : [isPresent]);
|
|
63
|
+
};
|
|
64
|
+
}, presenceAffectsLayout ? void 0 : [isEntering, onExitComplete, exitVariant, enterVariant]);
|
|
60
65
|
React.useMemo(() => {
|
|
61
66
|
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
62
|
-
}, [
|
|
67
|
+
}, [isEntering]);
|
|
63
68
|
React.useEffect(() => {
|
|
64
|
-
if (
|
|
65
|
-
console.log("on exit complete");
|
|
69
|
+
if (isEntering === false && !presenceChildren.size) {
|
|
66
70
|
onExitComplete == null ? void 0 : onExitComplete();
|
|
67
71
|
}
|
|
68
|
-
}, [
|
|
72
|
+
}, [isEntering]);
|
|
69
73
|
return /* @__PURE__ */ React.createElement(import_AnimatePresenceContext.AnimatePresenceContext.Provider, {
|
|
70
74
|
value: context
|
|
71
75
|
}, children);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
-
"sourcesContent": ["import { useId } from '@react-aria/utils'\nimport { useConstant } 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
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AACtB,kBAA4B;AAC5B,YAAuB;AAEvB,oCAAoE;
|
|
4
|
+
"sourcesContent": ["import { useId } from '@react-aria/utils'\nimport { useConstant } 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 console.log('on exist complete', childId)\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;AAAA;AAAA;AAAA;AAAA;AAAA,mBAAsB;AACtB,kBAA4B;AAC5B,YAAuB;AAEvB,oCAAoE;AAa7D,MAAM,gBAAgB,wBAAC;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,MACwB;AACxB,QAAM,mBAAmB,6BAAY,cAAc;AACnD,QAAM,KAAK,wBAAM;AAEjB,QAAM,UAAU,MAAM,QACpB,MAAmC;AACjC,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA,gBAAgB,CAAC,YAAoB;AACnC,gBAAQ,IAAI,qBAAqB,OAAO;AACxC,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,qDAAuB,UAAvB;AAAA,IAAgC,OAAO;AAAA,KAAU,QAAS;AAE/D,GA9D6B;AAgE7B,0BAAgD;AAC9C,SAAO,oBAAI,IAAI;AACjB;AAFS;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -22,6 +22,6 @@ __export(src_exports, {
|
|
|
22
22
|
});
|
|
23
23
|
module.exports = __toCommonJS(src_exports);
|
|
24
24
|
__reExport(src_exports, require("./AnimatePresence"), module.exports);
|
|
25
|
-
__reExport(src_exports, require("./
|
|
25
|
+
__reExport(src_exports, require("./useEntering"), module.exports);
|
|
26
26
|
var import_AnimatePresenceContext = require("./AnimatePresenceContext");
|
|
27
27
|
//# sourceMappingURL=index.js.map
|
package/dist/cjs/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './AnimatePresence'\nexport * from './
|
|
4
|
+
"sourcesContent": ["export * from './AnimatePresence'\nexport * from './useEntering'\nexport { AnimatePresenceContext } from './AnimatePresenceContext'\n"],
|
|
5
5
|
"mappings": ";;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAc,8BAAd;AACA,wBAAc,0BADd;AAEA,oCAAuC;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,33 +1,30 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
useIsMounted,
|
|
6
|
-
useIsomorphicLayoutEffect,
|
|
7
|
-
useUnmountEffect
|
|
8
|
-
} from "@tamagui/core";
|
|
9
|
-
import * as React from "react";
|
|
10
|
-
import {
|
|
3
|
+
import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from "@tamagui/core";
|
|
4
|
+
import React, {
|
|
11
5
|
Children,
|
|
12
6
|
cloneElement,
|
|
13
7
|
isValidElement,
|
|
14
8
|
useContext,
|
|
15
|
-
|
|
9
|
+
useEffect,
|
|
10
|
+
useRef,
|
|
11
|
+
useState
|
|
16
12
|
} from "react";
|
|
17
13
|
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
18
14
|
import { PresenceChild } from "./PresenceChild";
|
|
15
|
+
React["keep"];
|
|
19
16
|
const getChildKey = /* @__PURE__ */ __name((child) => child.key || "", "getChildKey");
|
|
20
17
|
const isDev = process.env.NODE_ENV !== "production";
|
|
21
18
|
function updateChildLookup(children, allChildren) {
|
|
22
19
|
const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
|
|
23
|
-
|
|
20
|
+
children.forEach((child) => {
|
|
24
21
|
const key = getChildKey(child);
|
|
25
22
|
if (isDev && seenChildren && seenChildren.has(key)) {
|
|
26
23
|
console.warn(`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`);
|
|
27
24
|
seenChildren.add(key);
|
|
28
25
|
}
|
|
29
26
|
allChildren.set(key, child);
|
|
30
|
-
}
|
|
27
|
+
});
|
|
31
28
|
}
|
|
32
29
|
__name(updateChildLookup, "updateChildLookup");
|
|
33
30
|
function onlyElements(children) {
|
|
@@ -41,7 +38,8 @@ function onlyElements(children) {
|
|
|
41
38
|
__name(onlyElements, "onlyElements");
|
|
42
39
|
const AnimatePresence = /* @__PURE__ */ __name(({
|
|
43
40
|
children,
|
|
44
|
-
|
|
41
|
+
enterVariant,
|
|
42
|
+
exitVariant,
|
|
45
43
|
initial = true,
|
|
46
44
|
onExitComplete,
|
|
47
45
|
exitBeforeEnter,
|
|
@@ -51,7 +49,10 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
51
49
|
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
52
50
|
if (forceRenderLayoutGroup)
|
|
53
51
|
forceRender = forceRenderLayoutGroup;
|
|
54
|
-
const isMounted =
|
|
52
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
setIsMounted(true);
|
|
55
|
+
}, []);
|
|
55
56
|
const filteredChildren = onlyElements(children);
|
|
56
57
|
let childrenToRender = filteredChildren;
|
|
57
58
|
const exiting = /* @__PURE__ */ new Set();
|
|
@@ -71,7 +72,9 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
71
72
|
if (isInitialRender.current) {
|
|
72
73
|
return /* @__PURE__ */ React.createElement(React.Fragment, null, childrenToRender.map((child) => /* @__PURE__ */ React.createElement(PresenceChild, {
|
|
73
74
|
key: getChildKey(child),
|
|
74
|
-
|
|
75
|
+
isEntering: true,
|
|
76
|
+
exitVariant,
|
|
77
|
+
enterVariant,
|
|
75
78
|
initial: initial ? void 0 : false,
|
|
76
79
|
presenceAffectsLayout
|
|
77
80
|
}, child)));
|
|
@@ -89,42 +92,41 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
89
92
|
if (exitBeforeEnter && exiting.size) {
|
|
90
93
|
childrenToRender = [];
|
|
91
94
|
}
|
|
92
|
-
|
|
93
|
-
if (targetKeys.indexOf(key) !== -1)
|
|
94
|
-
|
|
95
|
-
}
|
|
95
|
+
exiting.forEach((key) => {
|
|
96
|
+
if (targetKeys.indexOf(key) !== -1)
|
|
97
|
+
return;
|
|
96
98
|
const child = allChildren.get(key);
|
|
97
|
-
if (!child)
|
|
98
|
-
|
|
99
|
-
}
|
|
99
|
+
if (!child)
|
|
100
|
+
return;
|
|
100
101
|
const insertionIndex = presentKeys.indexOf(key);
|
|
101
|
-
const onExit = /* @__PURE__ */ __name(() => {
|
|
102
|
-
allChildren.delete(key);
|
|
103
|
-
exiting.delete(key);
|
|
104
|
-
const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
|
|
105
|
-
presentChildren.current.splice(removeIndex, 1);
|
|
106
|
-
if (!exiting.size) {
|
|
107
|
-
presentChildren.current = filteredChildren;
|
|
108
|
-
if (isMounted.current === false) {
|
|
109
|
-
return;
|
|
110
|
-
}
|
|
111
|
-
forceRender();
|
|
112
|
-
onExitComplete == null ? void 0 : onExitComplete();
|
|
113
|
-
}
|
|
114
|
-
}, "onExit");
|
|
115
102
|
childrenToRender.splice(insertionIndex, 0, /* @__PURE__ */ React.createElement(PresenceChild, {
|
|
116
103
|
key: getChildKey(child),
|
|
117
|
-
|
|
118
|
-
onExitComplete:
|
|
119
|
-
|
|
104
|
+
isEntering: false,
|
|
105
|
+
onExitComplete: () => {
|
|
106
|
+
allChildren.delete(key);
|
|
107
|
+
exiting.delete(key);
|
|
108
|
+
const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
|
|
109
|
+
presentChildren.current.splice(removeIndex, 1);
|
|
110
|
+
if (!exiting.size) {
|
|
111
|
+
presentChildren.current = filteredChildren;
|
|
112
|
+
if (isMounted === false)
|
|
113
|
+
return;
|
|
114
|
+
forceRender();
|
|
115
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
116
|
+
}
|
|
117
|
+
},
|
|
118
|
+
exitVariant,
|
|
119
|
+
enterVariant,
|
|
120
120
|
presenceAffectsLayout
|
|
121
121
|
}, child));
|
|
122
|
-
}
|
|
122
|
+
});
|
|
123
123
|
childrenToRender = childrenToRender.map((child) => {
|
|
124
124
|
const key = child.key;
|
|
125
125
|
return exiting.has(key) ? child : /* @__PURE__ */ React.createElement(PresenceChild, {
|
|
126
126
|
key: getChildKey(child),
|
|
127
|
-
|
|
127
|
+
isEntering: void 0,
|
|
128
|
+
exitVariant,
|
|
129
|
+
enterVariant,
|
|
128
130
|
presenceAffectsLayout
|
|
129
131
|
}, child);
|
|
130
132
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
-
"sourcesContent": ["import {\n useForceUpdate,\n useIsMounted,\n useIsomorphicLayoutEffect,\n useUnmountEffect,\n} from '@tamagui/core'\nimport * as React from 'react'\nimport {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\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 custom?: any\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 || ''\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 for (const child of children) {\n const key = getChildKey(child)\n if (isDev && seenChildren && seenChildren.has(key)) {\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n seenChildren.add(key)\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 animated 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 animated components with `exit` props, it will only unmount the child\n * once all animated 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<AnimatePresenceProps> = ({\n children,\n custom,\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 = useIsMounted()\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 isPresent\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 for (const key of exiting) {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) {\n continue\n }\n\n const child = allChildren.get(key)\n if (!child) {\n continue\n }\n\n const insertionIndex = presentKeys.indexOf(key)\n\n const onExit = () => {\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 if (isMounted.current === false) {\n return\n }\n forceRender()\n onExitComplete?.()\n }\n }\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={onExit}\n custom={custom}\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 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"],
|
|
5
|
-
"mappings": ";;AAAA;
|
|
4
|
+
"sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\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 useEffect(() => {\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\n// import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from '@tamagui/core'\n// import React, {\n// Children,\n// ReactElement,\n// ReactNode,\n// cloneElement,\n// isValidElement,\n// useContext,\n// useEffect,\n// useRef,\n// useState,\n// } from 'react'\n\n// import { LayoutGroupContext } from './LayoutGroupContext'\n// import { PresenceChild } from './PresenceChild'\n\n// React['keep']\n\n// export 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\n// type ComponentKey = string | number\n\n// const getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n// const isDev = process.env.NODE_ENV !== 'production'\n\n// function updateChildLookup(\n// children: ReactElement<any>[],\n// allChildren: Map<ComponentKey, ReactElement<any>>\n// ) {\n// const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n// for (const child of children) {\n// const key = getChildKey(child)\n// if (isDev && seenChildren && seenChildren.has(key)) {\n// console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n// seenChildren.add(key)\n// }\n// allChildren.set(key, child)\n// }\n// }\n\n// function 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 animated 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 animated components with `exit` props, it will only unmount the child\n// * once all animated components have finished animating out. Likewise, any components using\n// * `useEntering` all need to call `safeToRemove`.\n// *\n// * @public\n// */\n// export const AnimatePresence: React.FunctionComponent<AnimatePresenceProps> = ({\n// children,\n// exitVariant,\n// enterVariant,\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// useEffect(() => {\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// console.log('AnimatePresence (iniital)', isInitialRender.current, {\n// isMounted,\n// childrenToRender,\n// })\n// return (\n// <>\n// {childrenToRender.map((child) => (\n// <PresenceChild\n// key={getChildKey(child)}\n// isEntering\n// initial={initial ? undefined : false}\n// presenceAffectsLayout={presenceAffectsLayout}\n// exitVariant={exitVariant}\n// enterVariant={enterVariant}\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// for (const key of exiting) {\n// // If this component is actually entering again, early return\n// if (targetKeys.indexOf(key) !== -1) {\n// continue\n// }\n\n// const child = allChildren.get(key)\n// if (!child) {\n// continue\n// }\n\n// const insertionIndex = presentKeys.indexOf(key)\n\n// const onExit = () => {\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// if (isMounted === false) {\n// return\n// }\n// forceRender()\n// onExitComplete?.()\n// }\n// }\n\n// childrenToRender.splice(\n// insertionIndex,\n// 0,\n// <PresenceChild\n// key={getChildKey(child)}\n// isEntering={false}\n// onExitComplete={onExit}\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// console.log(\n// 'AnimatePresence ()',\n// isInitialRender.current,\n// {\n// isMounted,\n// },\n// childrenToRender.map((x) => x.props.children.props.children.props.src)\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"],
|
|
5
|
+
"mappings": "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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresenceContext.tsx"],
|
|
4
|
-
"sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface AnimatePresenceContextProps {\n id: string\n
|
|
5
|
-
"mappings": "AAAA;
|
|
4
|
+
"sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface AnimatePresenceContextProps {\n id: string\n isEntering: boolean | undefined\n register: (id: string) => () => void\n onExitComplete?: (id: string) => void\n initial?: false | VariantLabels\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const AnimatePresenceContext = createContext<AnimatePresenceContextProps | null>(null)\n"],
|
|
5
|
+
"mappings": "AAAA;AAcO,MAAM,yBAAyB,cAAkD,IAAI;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -7,41 +7,45 @@ import { AnimatePresenceContext } from "./AnimatePresenceContext";
|
|
|
7
7
|
const PresenceChild = /* @__PURE__ */ __name(({
|
|
8
8
|
children,
|
|
9
9
|
initial,
|
|
10
|
-
|
|
10
|
+
isEntering,
|
|
11
11
|
onExitComplete,
|
|
12
|
-
|
|
12
|
+
exitVariant,
|
|
13
|
+
enterVariant,
|
|
13
14
|
presenceAffectsLayout
|
|
14
15
|
}) => {
|
|
15
16
|
const presenceChildren = useConstant(newChildrenMap);
|
|
16
17
|
const id = useId();
|
|
17
|
-
const context = React.useMemo(() =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const context = React.useMemo(() => {
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
initial,
|
|
22
|
+
isEntering,
|
|
23
|
+
exitVariant,
|
|
24
|
+
enterVariant,
|
|
25
|
+
onExitComplete: (childId) => {
|
|
26
|
+
console.log("on exist complete", childId);
|
|
27
|
+
presenceChildren.set(childId, true);
|
|
28
|
+
for (const isComplete of presenceChildren.values()) {
|
|
29
|
+
if (!isComplete) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
27
32
|
}
|
|
33
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
34
|
+
},
|
|
35
|
+
register: (childId) => {
|
|
36
|
+
presenceChildren.set(childId, false);
|
|
37
|
+
return () => presenceChildren.delete(childId);
|
|
28
38
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
register: (childId) => {
|
|
32
|
-
presenceChildren.set(childId, false);
|
|
33
|
-
return () => presenceChildren.delete(childId);
|
|
34
|
-
}
|
|
35
|
-
}), presenceAffectsLayout ? void 0 : [isPresent]);
|
|
39
|
+
};
|
|
40
|
+
}, presenceAffectsLayout ? void 0 : [isEntering, onExitComplete, exitVariant, enterVariant]);
|
|
36
41
|
React.useMemo(() => {
|
|
37
42
|
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
38
|
-
}, [
|
|
43
|
+
}, [isEntering]);
|
|
39
44
|
React.useEffect(() => {
|
|
40
|
-
if (
|
|
41
|
-
console.log("on exit complete");
|
|
45
|
+
if (isEntering === false && !presenceChildren.size) {
|
|
42
46
|
onExitComplete == null ? void 0 : onExitComplete();
|
|
43
47
|
}
|
|
44
|
-
}, [
|
|
48
|
+
}, [isEntering]);
|
|
45
49
|
return /* @__PURE__ */ React.createElement(AnimatePresenceContext.Provider, {
|
|
46
50
|
value: context
|
|
47
51
|
}, children);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
-
"sourcesContent": ["import { useId } from '@react-aria/utils'\nimport { useConstant } 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
|
|
5
|
-
"mappings": ";;AAAA;AACA;AACA;AAEA;
|
|
4
|
+
"sourcesContent": ["import { useId } from '@react-aria/utils'\nimport { useConstant } 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 console.log('on exist complete', childId)\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;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,gBAAQ,IAAI,qBAAqB,OAAO;AACxC,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,GA9D6B;AAgE7B,0BAAgD;AAC9C,SAAO,oBAAI,IAAI;AACjB;AAFS;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/esm/index.js
CHANGED
package/dist/esm/index.js.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/index.ts"],
|
|
4
|
-
"sourcesContent": ["export * from './AnimatePresence'\nexport * from './
|
|
4
|
+
"sourcesContent": ["export * from './AnimatePresence'\nexport * from './useEntering'\nexport { AnimatePresenceContext } from './AnimatePresenceContext'\n"],
|
|
5
5
|
"mappings": "AAAA;AACA;AACA;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
2
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
useIsMounted,
|
|
6
|
-
useIsomorphicLayoutEffect,
|
|
7
|
-
useUnmountEffect
|
|
8
|
-
} from "@tamagui/core";
|
|
9
|
-
import {
|
|
3
|
+
import { useForceUpdate, useIsomorphicLayoutEffect, useUnmountEffect } from "@tamagui/core";
|
|
4
|
+
import React, {
|
|
10
5
|
Children,
|
|
11
6
|
cloneElement,
|
|
12
7
|
isValidElement,
|
|
13
8
|
useContext,
|
|
14
|
-
|
|
9
|
+
useEffect,
|
|
10
|
+
useRef,
|
|
11
|
+
useState
|
|
15
12
|
} from "react";
|
|
16
13
|
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
17
14
|
import { PresenceChild } from "./PresenceChild";
|
|
15
|
+
React["keep"];
|
|
18
16
|
const getChildKey = /* @__PURE__ */ __name((child) => child.key || "", "getChildKey");
|
|
19
17
|
const isDev = process.env.NODE_ENV !== "production";
|
|
20
18
|
function updateChildLookup(children, allChildren) {
|
|
21
19
|
const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
|
|
22
|
-
|
|
20
|
+
children.forEach((child) => {
|
|
23
21
|
const key = getChildKey(child);
|
|
24
22
|
if (isDev && seenChildren && seenChildren.has(key)) {
|
|
25
23
|
console.warn(`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`);
|
|
26
24
|
seenChildren.add(key);
|
|
27
25
|
}
|
|
28
26
|
allChildren.set(key, child);
|
|
29
|
-
}
|
|
27
|
+
});
|
|
30
28
|
}
|
|
31
29
|
__name(updateChildLookup, "updateChildLookup");
|
|
32
30
|
function onlyElements(children) {
|
|
@@ -40,7 +38,8 @@ function onlyElements(children) {
|
|
|
40
38
|
__name(onlyElements, "onlyElements");
|
|
41
39
|
const AnimatePresence = /* @__PURE__ */ __name(({
|
|
42
40
|
children,
|
|
43
|
-
|
|
41
|
+
enterVariant,
|
|
42
|
+
exitVariant,
|
|
44
43
|
initial = true,
|
|
45
44
|
onExitComplete,
|
|
46
45
|
exitBeforeEnter,
|
|
@@ -50,7 +49,10 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
50
49
|
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
51
50
|
if (forceRenderLayoutGroup)
|
|
52
51
|
forceRender = forceRenderLayoutGroup;
|
|
53
|
-
const isMounted =
|
|
52
|
+
const [isMounted, setIsMounted] = useState(false);
|
|
53
|
+
useEffect(() => {
|
|
54
|
+
setIsMounted(true);
|
|
55
|
+
}, []);
|
|
54
56
|
const filteredChildren = onlyElements(children);
|
|
55
57
|
let childrenToRender = filteredChildren;
|
|
56
58
|
const exiting = /* @__PURE__ */ new Set();
|
|
@@ -68,7 +70,7 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
68
70
|
exiting.clear();
|
|
69
71
|
});
|
|
70
72
|
if (isInitialRender.current) {
|
|
71
|
-
return <>{childrenToRender.map((child) => <PresenceChild key={getChildKey(child)}
|
|
73
|
+
return <>{childrenToRender.map((child) => <PresenceChild key={getChildKey(child)} isEntering exitVariant={exitVariant} enterVariant={enterVariant} initial={initial ? void 0 : false} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>)}</>;
|
|
72
74
|
}
|
|
73
75
|
childrenToRender = [...childrenToRender];
|
|
74
76
|
const presentKeys = presentChildren.current.map(getChildKey);
|
|
@@ -83,34 +85,30 @@ const AnimatePresence = /* @__PURE__ */ __name(({
|
|
|
83
85
|
if (exitBeforeEnter && exiting.size) {
|
|
84
86
|
childrenToRender = [];
|
|
85
87
|
}
|
|
86
|
-
|
|
87
|
-
if (targetKeys.indexOf(key) !== -1)
|
|
88
|
-
|
|
89
|
-
}
|
|
88
|
+
exiting.forEach((key) => {
|
|
89
|
+
if (targetKeys.indexOf(key) !== -1)
|
|
90
|
+
return;
|
|
90
91
|
const child = allChildren.get(key);
|
|
91
|
-
if (!child)
|
|
92
|
-
|
|
93
|
-
}
|
|
92
|
+
if (!child)
|
|
93
|
+
return;
|
|
94
94
|
const insertionIndex = presentKeys.indexOf(key);
|
|
95
|
-
|
|
95
|
+
childrenToRender.splice(insertionIndex, 0, <PresenceChild key={getChildKey(child)} isEntering={false} onExitComplete={() => {
|
|
96
96
|
allChildren.delete(key);
|
|
97
97
|
exiting.delete(key);
|
|
98
98
|
const removeIndex = presentChildren.current.findIndex((presentChild) => presentChild.key === key);
|
|
99
99
|
presentChildren.current.splice(removeIndex, 1);
|
|
100
100
|
if (!exiting.size) {
|
|
101
101
|
presentChildren.current = filteredChildren;
|
|
102
|
-
if (isMounted
|
|
102
|
+
if (isMounted === false)
|
|
103
103
|
return;
|
|
104
|
-
}
|
|
105
104
|
forceRender();
|
|
106
105
|
onExitComplete == null ? void 0 : onExitComplete();
|
|
107
106
|
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
}
|
|
107
|
+
}} exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>);
|
|
108
|
+
});
|
|
111
109
|
childrenToRender = childrenToRender.map((child) => {
|
|
112
110
|
const key = child.key;
|
|
113
|
-
return exiting.has(key) ? child : <PresenceChild key={getChildKey(child)}
|
|
111
|
+
return exiting.has(key) ? child : <PresenceChild key={getChildKey(child)} isEntering={void 0} exitVariant={exitVariant} enterVariant={enterVariant} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>;
|
|
114
112
|
});
|
|
115
113
|
if (process.env.NODE_ENV !== "production" && exitBeforeEnter && childrenToRender.length > 1) {
|
|
116
114
|
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.`);
|
|
@@ -7,41 +7,45 @@ import { AnimatePresenceContext } from "./AnimatePresenceContext";
|
|
|
7
7
|
const PresenceChild = /* @__PURE__ */ __name(({
|
|
8
8
|
children,
|
|
9
9
|
initial,
|
|
10
|
-
|
|
10
|
+
isEntering,
|
|
11
11
|
onExitComplete,
|
|
12
|
-
|
|
12
|
+
exitVariant,
|
|
13
|
+
enterVariant,
|
|
13
14
|
presenceAffectsLayout
|
|
14
15
|
}) => {
|
|
15
16
|
const presenceChildren = useConstant(newChildrenMap);
|
|
16
17
|
const id = useId();
|
|
17
|
-
const context = React.useMemo(() =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
const context = React.useMemo(() => {
|
|
19
|
+
return {
|
|
20
|
+
id,
|
|
21
|
+
initial,
|
|
22
|
+
isEntering,
|
|
23
|
+
exitVariant,
|
|
24
|
+
enterVariant,
|
|
25
|
+
onExitComplete: (childId) => {
|
|
26
|
+
console.log("on exist complete", childId);
|
|
27
|
+
presenceChildren.set(childId, true);
|
|
28
|
+
for (const isComplete of presenceChildren.values()) {
|
|
29
|
+
if (!isComplete) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
27
32
|
}
|
|
33
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
34
|
+
},
|
|
35
|
+
register: (childId) => {
|
|
36
|
+
presenceChildren.set(childId, false);
|
|
37
|
+
return () => presenceChildren.delete(childId);
|
|
28
38
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
register: (childId) => {
|
|
32
|
-
presenceChildren.set(childId, false);
|
|
33
|
-
return () => presenceChildren.delete(childId);
|
|
34
|
-
}
|
|
35
|
-
}), presenceAffectsLayout ? void 0 : [isPresent]);
|
|
39
|
+
};
|
|
40
|
+
}, presenceAffectsLayout ? void 0 : [isEntering, onExitComplete, exitVariant, enterVariant]);
|
|
36
41
|
React.useMemo(() => {
|
|
37
42
|
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
38
|
-
}, [
|
|
43
|
+
}, [isEntering]);
|
|
39
44
|
React.useEffect(() => {
|
|
40
|
-
if (
|
|
41
|
-
console.log("on exit complete");
|
|
45
|
+
if (isEntering === false && !presenceChildren.size) {
|
|
42
46
|
onExitComplete == null ? void 0 : onExitComplete();
|
|
43
47
|
}
|
|
44
|
-
}, [
|
|
48
|
+
}, [isEntering]);
|
|
45
49
|
return <AnimatePresenceContext.Provider value={context}>{children}</AnimatePresenceContext.Provider>;
|
|
46
50
|
}, "PresenceChild");
|
|
47
51
|
function newChildrenMap() {
|
package/dist/jsx/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/animate-presence",
|
|
3
|
-
"version": "1.0.0-beta.
|
|
3
|
+
"version": "1.0.0-beta.208+f5ae07b4",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"typings": "types",
|
|
@@ -12,10 +12,10 @@
|
|
|
12
12
|
"dist"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@tamagui/core": "^1.0.0-beta.
|
|
15
|
+
"@tamagui/core": "^1.0.0-beta.208+f5ae07b4"
|
|
16
16
|
},
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@tamagui/build": "^1.0.0-beta.
|
|
18
|
+
"@tamagui/build": "^1.0.0-beta.208+f5ae07b4"
|
|
19
19
|
},
|
|
20
20
|
"scripts": {
|
|
21
21
|
"build": "tamagui-build",
|
|
@@ -26,5 +26,5 @@
|
|
|
26
26
|
"publishConfig": {
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
|
-
"gitHead": "
|
|
29
|
+
"gitHead": "f5ae07b474577e5546139d5a187dde4e1d08df4b"
|
|
30
30
|
}
|