@tamagui/animate-presence 1.2.9 → 1.2.10
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/esm/AnimatePresence.mjs +184 -0
- package/dist/esm/AnimatePresence.mjs.map +7 -0
- package/dist/esm/LayoutGroupContext.mjs +6 -0
- package/dist/esm/LayoutGroupContext.mjs.map +7 -0
- package/dist/esm/PresenceChild.mjs +61 -0
- package/dist/esm/PresenceChild.mjs.map +7 -0
- package/dist/esm/index.mjs +3 -0
- package/dist/esm/index.mjs.map +7 -0
- package/dist/esm/types.mjs +1 -0
- package/dist/esm/types.mjs.map +7 -0
- package/dist/jsx/AnimatePresence.mjs +171 -0
- package/dist/jsx/AnimatePresence.mjs.map +7 -0
- package/dist/jsx/LayoutGroupContext.mjs +6 -0
- package/dist/jsx/LayoutGroupContext.mjs.map +7 -0
- package/dist/jsx/PresenceChild.mjs +60 -0
- package/dist/jsx/PresenceChild.mjs.map +7 -0
- package/dist/jsx/index.mjs +3 -0
- package/dist/jsx/index.mjs.map +7 -0
- package/dist/jsx/types.mjs +1 -0
- package/dist/jsx/types.mjs.map +7 -0
- package/package.json +4 -4
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
+
import {
|
|
3
|
+
useDidFinishSSR,
|
|
4
|
+
useForceUpdate,
|
|
5
|
+
useIsomorphicLayoutEffect
|
|
6
|
+
} from "@tamagui/core";
|
|
7
|
+
import React, {
|
|
8
|
+
Children,
|
|
9
|
+
cloneElement,
|
|
10
|
+
isValidElement,
|
|
11
|
+
useContext,
|
|
12
|
+
useEffect,
|
|
13
|
+
useRef
|
|
14
|
+
} from "react";
|
|
15
|
+
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
16
|
+
import { PresenceChild } from "./PresenceChild";
|
|
17
|
+
const getChildKey = (child) => child.key || "";
|
|
18
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
19
|
+
function updateChildLookup(children, allChildren) {
|
|
20
|
+
const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
|
|
21
|
+
children.forEach((child) => {
|
|
22
|
+
const key = getChildKey(child);
|
|
23
|
+
if (isDev && seenChildren && seenChildren.has(key)) {
|
|
24
|
+
console.warn(
|
|
25
|
+
`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`
|
|
26
|
+
);
|
|
27
|
+
seenChildren.add(key);
|
|
28
|
+
}
|
|
29
|
+
allChildren.set(key, child);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
function onlyElements(children) {
|
|
33
|
+
const filtered = [];
|
|
34
|
+
Children.forEach(children, (child, index) => {
|
|
35
|
+
if (isValidElement(child)) {
|
|
36
|
+
if (!child.key) {
|
|
37
|
+
if (process.env.NODE_ENV === "development") {
|
|
38
|
+
console.warn(
|
|
39
|
+
"No key given to AnimatePresence child, assigning index as key"
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
filtered.push(
|
|
43
|
+
React.cloneElement(child, {
|
|
44
|
+
key: index
|
|
45
|
+
})
|
|
46
|
+
);
|
|
47
|
+
} else {
|
|
48
|
+
filtered.push(child);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return filtered;
|
|
53
|
+
}
|
|
54
|
+
const AnimatePresence = ({
|
|
55
|
+
children,
|
|
56
|
+
enterVariant,
|
|
57
|
+
exitVariant,
|
|
58
|
+
initial = true,
|
|
59
|
+
onExitComplete,
|
|
60
|
+
exitBeforeEnter,
|
|
61
|
+
presenceAffectsLayout = true
|
|
62
|
+
}) => {
|
|
63
|
+
let forceRender = useForceUpdate();
|
|
64
|
+
const isClientMounted = useDidFinishSSR();
|
|
65
|
+
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
66
|
+
if (forceRenderLayoutGroup)
|
|
67
|
+
forceRender = forceRenderLayoutGroup;
|
|
68
|
+
const isMounted = useRef(false);
|
|
69
|
+
const filteredChildren = onlyElements(children);
|
|
70
|
+
let childrenToRender = filteredChildren;
|
|
71
|
+
const exiting = /* @__PURE__ */ new Set();
|
|
72
|
+
const presentChildren = useRef(childrenToRender);
|
|
73
|
+
const allChildren = useRef(/* @__PURE__ */ new Map()).current;
|
|
74
|
+
const isInitialRender = useRef(true);
|
|
75
|
+
useEffect(() => {
|
|
76
|
+
isMounted.current = true;
|
|
77
|
+
return () => {
|
|
78
|
+
isMounted.current = false;
|
|
79
|
+
isInitialRender.current = true;
|
|
80
|
+
allChildren.clear();
|
|
81
|
+
exiting.clear();
|
|
82
|
+
};
|
|
83
|
+
}, []);
|
|
84
|
+
useIsomorphicLayoutEffect(() => {
|
|
85
|
+
isInitialRender.current = false;
|
|
86
|
+
updateChildLookup(filteredChildren, allChildren);
|
|
87
|
+
presentChildren.current = childrenToRender;
|
|
88
|
+
});
|
|
89
|
+
const hasWarned = process.env.NODE_ENV === "development" ? useRef(false) : null;
|
|
90
|
+
if (isInitialRender.current) {
|
|
91
|
+
return /* @__PURE__ */ jsx(Fragment, { children: childrenToRender.map((child) => /* @__PURE__ */ jsx(
|
|
92
|
+
PresenceChild,
|
|
93
|
+
{
|
|
94
|
+
isPresent: Boolean(isClientMounted ? true : isMounted.current),
|
|
95
|
+
exitVariant,
|
|
96
|
+
enterVariant,
|
|
97
|
+
initial: initial ? void 0 : false,
|
|
98
|
+
presenceAffectsLayout,
|
|
99
|
+
children: child
|
|
100
|
+
},
|
|
101
|
+
getChildKey(child)
|
|
102
|
+
)) });
|
|
103
|
+
}
|
|
104
|
+
childrenToRender = [...childrenToRender];
|
|
105
|
+
const presentKeys = presentChildren.current.map(getChildKey);
|
|
106
|
+
const targetKeys = filteredChildren.map(getChildKey);
|
|
107
|
+
const numPresent = presentKeys.length;
|
|
108
|
+
for (let i = 0; i < numPresent; i++) {
|
|
109
|
+
const key = presentKeys[i];
|
|
110
|
+
if (targetKeys.indexOf(key) === -1) {
|
|
111
|
+
exiting.add(key);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (exitBeforeEnter && exiting.size) {
|
|
115
|
+
childrenToRender = [];
|
|
116
|
+
}
|
|
117
|
+
exiting.forEach((key) => {
|
|
118
|
+
if (targetKeys.indexOf(key) !== -1)
|
|
119
|
+
return;
|
|
120
|
+
const child = allChildren.get(key);
|
|
121
|
+
if (!child)
|
|
122
|
+
return;
|
|
123
|
+
const insertionIndex = presentKeys.indexOf(key);
|
|
124
|
+
childrenToRender.splice(
|
|
125
|
+
insertionIndex,
|
|
126
|
+
0,
|
|
127
|
+
/* @__PURE__ */ jsx(
|
|
128
|
+
PresenceChild,
|
|
129
|
+
{
|
|
130
|
+
isPresent: false,
|
|
131
|
+
onExitComplete: () => {
|
|
132
|
+
allChildren.delete(key);
|
|
133
|
+
exiting.delete(key);
|
|
134
|
+
const removeIndex = presentChildren.current.findIndex(
|
|
135
|
+
(presentChild) => presentChild.key === key
|
|
136
|
+
);
|
|
137
|
+
presentChildren.current.splice(removeIndex, 1);
|
|
138
|
+
if (!exiting.size) {
|
|
139
|
+
presentChildren.current = filteredChildren;
|
|
140
|
+
if (isMounted.current === false)
|
|
141
|
+
return;
|
|
142
|
+
forceRender();
|
|
143
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
exitVariant,
|
|
147
|
+
enterVariant,
|
|
148
|
+
presenceAffectsLayout,
|
|
149
|
+
children: child
|
|
150
|
+
},
|
|
151
|
+
getChildKey(child)
|
|
152
|
+
)
|
|
153
|
+
);
|
|
154
|
+
});
|
|
155
|
+
childrenToRender = childrenToRender.map((child) => {
|
|
156
|
+
const key = child.key;
|
|
157
|
+
return exiting.has(key) ? child : /* @__PURE__ */ jsx(
|
|
158
|
+
PresenceChild,
|
|
159
|
+
{
|
|
160
|
+
isPresent: true,
|
|
161
|
+
exitVariant,
|
|
162
|
+
enterVariant,
|
|
163
|
+
presenceAffectsLayout,
|
|
164
|
+
children: child
|
|
165
|
+
},
|
|
166
|
+
getChildKey(child)
|
|
167
|
+
);
|
|
168
|
+
});
|
|
169
|
+
if (process.env.NODE_ENV === "development") {
|
|
170
|
+
const shouldWarn = exitBeforeEnter && childrenToRender.length > 1;
|
|
171
|
+
if (shouldWarn && hasWarned && !hasWarned.current) {
|
|
172
|
+
hasWarned.current = true;
|
|
173
|
+
console.log(
|
|
174
|
+
`You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This can lead to odd visual behaviour.`
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return /* @__PURE__ */ jsx(Fragment, { children: exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child)) });
|
|
179
|
+
};
|
|
180
|
+
AnimatePresence.displayName = "AnimatePresence";
|
|
181
|
+
export {
|
|
182
|
+
AnimatePresence
|
|
183
|
+
};
|
|
184
|
+
//# sourceMappingURL=AnimatePresence.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
+
"sourcesContent": ["import {\n useDidFinishSSR,\n useForceUpdate,\n useIsomorphicLayoutEffect,\n} 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\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(\n `Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`,\n )\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(\n 'No key given to AnimatePresence child, assigning index as key',\n )\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 isClientMounted = useDidFinishSSR()\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 const hasWarned = process.env.NODE_ENV === 'development' ? useRef(false) : null\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent={Boolean(isClientMounted ? true : isMounted.current)}\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 === 'development') {\n const shouldWarn = exitBeforeEnter && childrenToRender.length > 1\n if (shouldWarn && hasWarned && !hasWarned.current) {\n hasWarned.current = true\n // eslint-disable-next-line no-console\n console.log(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This can lead to odd visual behaviour.`,\n )\n }\n }\n\n return (\n <>\n {exiting.size\n ? childrenToRender\n : childrenToRender.map((child) => cloneElement(child))}\n </>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
|
|
5
|
+
"mappings": "AAsKM,mBAEI,WAFJ;AAtKN;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,O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,KAAK,CAAC,GACzD;AAEJ;AAEA,gBAAgB,cAAc;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/LayoutGroupContext.tsx"],
|
|
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,SAAS,qBAAqB;AAOvB,MAAM,qBAAqB,cAAuC,CAAC,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { jsx } from "react/jsx-runtime";
|
|
2
|
+
import { PresenceContext } from "@tamagui/use-presence";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { useId } from "react";
|
|
5
|
+
const PresenceChild = ({
|
|
6
|
+
children,
|
|
7
|
+
initial,
|
|
8
|
+
isPresent,
|
|
9
|
+
onExitComplete,
|
|
10
|
+
exitVariant,
|
|
11
|
+
enterVariant,
|
|
12
|
+
presenceAffectsLayout
|
|
13
|
+
}) => {
|
|
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
|
+
}
|
|
30
|
+
}
|
|
31
|
+
onExitComplete == null ? void 0 : onExitComplete();
|
|
32
|
+
},
|
|
33
|
+
register: (id2) => {
|
|
34
|
+
presenceChildren.set(id2, false);
|
|
35
|
+
return () => presenceChildren.delete(id2);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
},
|
|
39
|
+
/**
|
|
40
|
+
* If the presence of a child affects the layout of the components around it,
|
|
41
|
+
* we want to make a new context value to ensure they get re-rendered
|
|
42
|
+
* so they can detect that layout change.
|
|
43
|
+
*/
|
|
44
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
45
|
+
presenceAffectsLayout ? void 0 : [isPresent, exitVariant, enterVariant]
|
|
46
|
+
);
|
|
47
|
+
React.useMemo(() => {
|
|
48
|
+
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
49
|
+
}, [isPresent]);
|
|
50
|
+
React.useEffect(() => {
|
|
51
|
+
!(isPresent || presenceChildren.size) && (onExitComplete == null ? void 0 : onExitComplete());
|
|
52
|
+
}, [isPresent]);
|
|
53
|
+
return /* @__PURE__ */ jsx(PresenceContext.Provider, { value: context, children });
|
|
54
|
+
};
|
|
55
|
+
function newChildrenMap() {
|
|
56
|
+
return /* @__PURE__ */ new Map();
|
|
57
|
+
}
|
|
58
|
+
export {
|
|
59
|
+
PresenceChild
|
|
60
|
+
};
|
|
61
|
+
//# sourceMappingURL=PresenceChild.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
+
"sourcesContent": ["import type { PresenceContextProps } from '@tamagui/core'\nimport { PresenceContext } 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 (\n <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n )\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
|
|
5
|
+
"mappings": "AA6EI;AA5EJ,SAAS,uBAAuB;AAChC,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;AAAA;AAAA;AAAA;AAAA;AAAA;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,MAAE,aAAa,iBAAiB,UAAS;AAAA,EAE3C,GAAG,CAAC,SAAS,CAAC;AAEd,SACE,oBAAC,gBAAgB,UAAhB,EAAyB,OAAO,SAAU,UAAS;AAExD;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
|
|
6
|
+
"names": ["id"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.mjs.map
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useDidFinishSSR,
|
|
3
|
+
useForceUpdate,
|
|
4
|
+
useIsomorphicLayoutEffect
|
|
5
|
+
} from "@tamagui/core";
|
|
6
|
+
import React, {
|
|
7
|
+
Children,
|
|
8
|
+
cloneElement,
|
|
9
|
+
isValidElement,
|
|
10
|
+
useContext,
|
|
11
|
+
useEffect,
|
|
12
|
+
useRef
|
|
13
|
+
} from "react";
|
|
14
|
+
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
15
|
+
import { PresenceChild } from "./PresenceChild";
|
|
16
|
+
const getChildKey = (child) => child.key || "";
|
|
17
|
+
const isDev = process.env.NODE_ENV !== "production";
|
|
18
|
+
function updateChildLookup(children, allChildren) {
|
|
19
|
+
const seenChildren = isDev ? /* @__PURE__ */ new Set() : null;
|
|
20
|
+
children.forEach((child) => {
|
|
21
|
+
const key = getChildKey(child);
|
|
22
|
+
if (isDev && seenChildren && seenChildren.has(key)) {
|
|
23
|
+
console.warn(
|
|
24
|
+
`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`
|
|
25
|
+
);
|
|
26
|
+
seenChildren.add(key);
|
|
27
|
+
}
|
|
28
|
+
allChildren.set(key, child);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function onlyElements(children) {
|
|
32
|
+
const filtered = [];
|
|
33
|
+
Children.forEach(children, (child, index) => {
|
|
34
|
+
if (isValidElement(child)) {
|
|
35
|
+
if (!child.key) {
|
|
36
|
+
if (process.env.NODE_ENV === "development") {
|
|
37
|
+
console.warn(
|
|
38
|
+
"No key given to AnimatePresence child, assigning index as key"
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
filtered.push(
|
|
42
|
+
React.cloneElement(child, {
|
|
43
|
+
key: index
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
} else {
|
|
47
|
+
filtered.push(child);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return filtered;
|
|
52
|
+
}
|
|
53
|
+
const AnimatePresence = ({
|
|
54
|
+
children,
|
|
55
|
+
enterVariant,
|
|
56
|
+
exitVariant,
|
|
57
|
+
initial = true,
|
|
58
|
+
onExitComplete,
|
|
59
|
+
exitBeforeEnter,
|
|
60
|
+
presenceAffectsLayout = true
|
|
61
|
+
}) => {
|
|
62
|
+
let forceRender = useForceUpdate();
|
|
63
|
+
const isClientMounted = useDidFinishSSR();
|
|
64
|
+
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
65
|
+
if (forceRenderLayoutGroup)
|
|
66
|
+
forceRender = forceRenderLayoutGroup;
|
|
67
|
+
const isMounted = useRef(false);
|
|
68
|
+
const filteredChildren = onlyElements(children);
|
|
69
|
+
let childrenToRender = filteredChildren;
|
|
70
|
+
const exiting = /* @__PURE__ */ new Set();
|
|
71
|
+
const presentChildren = useRef(childrenToRender);
|
|
72
|
+
const allChildren = useRef(/* @__PURE__ */ new Map()).current;
|
|
73
|
+
const isInitialRender = useRef(true);
|
|
74
|
+
useEffect(() => {
|
|
75
|
+
isMounted.current = true;
|
|
76
|
+
return () => {
|
|
77
|
+
isMounted.current = false;
|
|
78
|
+
isInitialRender.current = true;
|
|
79
|
+
allChildren.clear();
|
|
80
|
+
exiting.clear();
|
|
81
|
+
};
|
|
82
|
+
}, []);
|
|
83
|
+
useIsomorphicLayoutEffect(() => {
|
|
84
|
+
isInitialRender.current = false;
|
|
85
|
+
updateChildLookup(filteredChildren, allChildren);
|
|
86
|
+
presentChildren.current = childrenToRender;
|
|
87
|
+
});
|
|
88
|
+
const hasWarned = process.env.NODE_ENV === "development" ? useRef(false) : null;
|
|
89
|
+
if (isInitialRender.current) {
|
|
90
|
+
return <>{childrenToRender.map((child) => <PresenceChild
|
|
91
|
+
key={getChildKey(child)}
|
|
92
|
+
isPresent={Boolean(isClientMounted ? true : isMounted.current)}
|
|
93
|
+
exitVariant={exitVariant}
|
|
94
|
+
enterVariant={enterVariant}
|
|
95
|
+
initial={initial ? void 0 : false}
|
|
96
|
+
presenceAffectsLayout={presenceAffectsLayout}
|
|
97
|
+
>{child}</PresenceChild>)}</>;
|
|
98
|
+
}
|
|
99
|
+
childrenToRender = [...childrenToRender];
|
|
100
|
+
const presentKeys = presentChildren.current.map(getChildKey);
|
|
101
|
+
const targetKeys = filteredChildren.map(getChildKey);
|
|
102
|
+
const numPresent = presentKeys.length;
|
|
103
|
+
for (let i = 0; i < numPresent; i++) {
|
|
104
|
+
const key = presentKeys[i];
|
|
105
|
+
if (targetKeys.indexOf(key) === -1) {
|
|
106
|
+
exiting.add(key);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
if (exitBeforeEnter && exiting.size) {
|
|
110
|
+
childrenToRender = [];
|
|
111
|
+
}
|
|
112
|
+
exiting.forEach((key) => {
|
|
113
|
+
if (targetKeys.indexOf(key) !== -1)
|
|
114
|
+
return;
|
|
115
|
+
const child = allChildren.get(key);
|
|
116
|
+
if (!child)
|
|
117
|
+
return;
|
|
118
|
+
const insertionIndex = presentKeys.indexOf(key);
|
|
119
|
+
childrenToRender.splice(
|
|
120
|
+
insertionIndex,
|
|
121
|
+
0,
|
|
122
|
+
<PresenceChild
|
|
123
|
+
key={getChildKey(child)}
|
|
124
|
+
isPresent={false}
|
|
125
|
+
onExitComplete={() => {
|
|
126
|
+
allChildren.delete(key);
|
|
127
|
+
exiting.delete(key);
|
|
128
|
+
const removeIndex = presentChildren.current.findIndex(
|
|
129
|
+
(presentChild) => presentChild.key === key
|
|
130
|
+
);
|
|
131
|
+
presentChildren.current.splice(removeIndex, 1);
|
|
132
|
+
if (!exiting.size) {
|
|
133
|
+
presentChildren.current = filteredChildren;
|
|
134
|
+
if (isMounted.current === false)
|
|
135
|
+
return;
|
|
136
|
+
forceRender();
|
|
137
|
+
onExitComplete?.();
|
|
138
|
+
}
|
|
139
|
+
}}
|
|
140
|
+
exitVariant={exitVariant}
|
|
141
|
+
enterVariant={enterVariant}
|
|
142
|
+
presenceAffectsLayout={presenceAffectsLayout}
|
|
143
|
+
>{child}</PresenceChild>
|
|
144
|
+
);
|
|
145
|
+
});
|
|
146
|
+
childrenToRender = childrenToRender.map((child) => {
|
|
147
|
+
const key = child.key;
|
|
148
|
+
return exiting.has(key) ? child : <PresenceChild
|
|
149
|
+
key={getChildKey(child)}
|
|
150
|
+
isPresent
|
|
151
|
+
exitVariant={exitVariant}
|
|
152
|
+
enterVariant={enterVariant}
|
|
153
|
+
presenceAffectsLayout={presenceAffectsLayout}
|
|
154
|
+
>{child}</PresenceChild>;
|
|
155
|
+
});
|
|
156
|
+
if (process.env.NODE_ENV === "development") {
|
|
157
|
+
const shouldWarn = exitBeforeEnter && childrenToRender.length > 1;
|
|
158
|
+
if (shouldWarn && hasWarned && !hasWarned.current) {
|
|
159
|
+
hasWarned.current = true;
|
|
160
|
+
console.log(
|
|
161
|
+
`You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This can lead to odd visual behaviour.`
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>;
|
|
166
|
+
};
|
|
167
|
+
AnimatePresence.displayName = "AnimatePresence";
|
|
168
|
+
export {
|
|
169
|
+
AnimatePresence
|
|
170
|
+
};
|
|
171
|
+
//# sourceMappingURL=AnimatePresence.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
+
"sourcesContent": ["import {\n useDidFinishSSR,\n useForceUpdate,\n useIsomorphicLayoutEffect,\n} 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\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(\n `Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`,\n )\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(\n 'No key given to AnimatePresence child, assigning index as key',\n )\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 isClientMounted = useDidFinishSSR()\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 const hasWarned = process.env.NODE_ENV === 'development' ? useRef(false) : null\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent={Boolean(isClientMounted ? true : isMounted.current)}\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 === 'development') {\n const shouldWarn = exitBeforeEnter && childrenToRender.length > 1\n if (shouldWarn && hasWarned && !hasWarned.current) {\n hasWarned.current = true\n // eslint-disable-next-line no-console\n console.log(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This can lead to odd visual behaviour.`,\n )\n }\n }\n\n return (\n <>\n {exiting.size\n ? childrenToRender\n : childrenToRender.map((child) => cloneElement(child))}\n </>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
|
|
5
|
+
"mappings": "AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,OAAO;AAAA,EACL;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0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
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/LayoutGroupContext.tsx"],
|
|
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,SAAS,qBAAqB;AAOvB,MAAM,qBAAqB,cAAuC,CAAC,CAAC;",
|
|
6
|
+
"names": []
|
|
7
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { PresenceContext } from "@tamagui/use-presence";
|
|
2
|
+
import * as React from "react";
|
|
3
|
+
import { useId } from "react";
|
|
4
|
+
const PresenceChild = ({
|
|
5
|
+
children,
|
|
6
|
+
initial,
|
|
7
|
+
isPresent,
|
|
8
|
+
onExitComplete,
|
|
9
|
+
exitVariant,
|
|
10
|
+
enterVariant,
|
|
11
|
+
presenceAffectsLayout
|
|
12
|
+
}) => {
|
|
13
|
+
const presenceChildren = React.useMemo(newChildrenMap, []);
|
|
14
|
+
const id = useId() || "";
|
|
15
|
+
const context = React.useMemo(
|
|
16
|
+
() => {
|
|
17
|
+
return {
|
|
18
|
+
id,
|
|
19
|
+
initial,
|
|
20
|
+
isPresent,
|
|
21
|
+
exitVariant,
|
|
22
|
+
enterVariant,
|
|
23
|
+
onExitComplete: (id2) => {
|
|
24
|
+
presenceChildren.set(id2, true);
|
|
25
|
+
for (const isComplete of presenceChildren.values()) {
|
|
26
|
+
if (!isComplete) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
onExitComplete?.();
|
|
31
|
+
},
|
|
32
|
+
register: (id2) => {
|
|
33
|
+
presenceChildren.set(id2, false);
|
|
34
|
+
return () => presenceChildren.delete(id2);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
/**
|
|
39
|
+
* If the presence of a child affects the layout of the components around it,
|
|
40
|
+
* we want to make a new context value to ensure they get re-rendered
|
|
41
|
+
* so they can detect that layout change.
|
|
42
|
+
*/
|
|
43
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
44
|
+
presenceAffectsLayout ? void 0 : [isPresent, exitVariant, enterVariant]
|
|
45
|
+
);
|
|
46
|
+
React.useMemo(() => {
|
|
47
|
+
presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
|
|
48
|
+
}, [isPresent]);
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
!(isPresent || presenceChildren.size) && onExitComplete?.();
|
|
51
|
+
}, [isPresent]);
|
|
52
|
+
return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>;
|
|
53
|
+
};
|
|
54
|
+
function newChildrenMap() {
|
|
55
|
+
return /* @__PURE__ */ new Map();
|
|
56
|
+
}
|
|
57
|
+
export {
|
|
58
|
+
PresenceChild
|
|
59
|
+
};
|
|
60
|
+
//# sourceMappingURL=PresenceChild.mjs.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
+
"sourcesContent": ["import type { PresenceContextProps } from '@tamagui/core'\nimport { PresenceContext } 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 (\n <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n )\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
|
|
5
|
+
"mappings": "AACA,SAAS,uBAAuB;AAChC,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,2BAAiB;AAAA,QACnB;AAAA,QACA,UAAU,CAACA,QAAe;AACxB,2BAAiB,IAAIA,KAAI,KAAK;AAC9B,iBAAO,MAAM,iBAAiB,OAAOA,GAAE;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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,MAAE,aAAa,iBAAiB,SAAS,iBAAiB;AAAA,EAE5D,GAAG,CAAC,SAAS,CAAC;AAEd,SACE,CAAC,gBAAgB,SAAS,OAAO,UAAU,SAAS,EAAnD,gBAAgB;AAErB;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
|
|
6
|
+
"names": ["id"]
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.mjs.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/animate-presence",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,11 +14,11 @@
|
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tamagui/core": "^1.2.
|
|
18
|
-
"@tamagui/use-presence": "^1.2.
|
|
17
|
+
"@tamagui/core": "^1.2.10",
|
|
18
|
+
"@tamagui/use-presence": "^1.2.10"
|
|
19
19
|
},
|
|
20
20
|
"devDependencies": {
|
|
21
|
-
"@tamagui/build": "^1.2.
|
|
21
|
+
"@tamagui/build": "^1.2.10"
|
|
22
22
|
},
|
|
23
23
|
"scripts": {
|
|
24
24
|
"build": "tamagui-build",
|