@tamagui/animate-presence 1.88.13 → 1.89.0-1706308641099
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/LICENSE +21 -0
- package/dist/esm/AnimatePresence.mjs +93 -0
- package/dist/esm/LayoutGroupContext.mjs +3 -0
- package/dist/esm/PresenceChild.mjs +53 -0
- package/dist/esm/index.mjs +4 -0
- package/dist/esm/types.mjs +0 -0
- package/package.json +7 -7
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { useForceUpdate } from "@tamagui/use-force-update";
|
|
2
|
+
import { Children, cloneElement, isValidElement, useContext, useRef } from "react";
|
|
3
|
+
import { LayoutGroupContext } from "./LayoutGroupContext.mjs";
|
|
4
|
+
import { PresenceChild } from "./PresenceChild.mjs";
|
|
5
|
+
import { Fragment, jsx } from "react/jsx-runtime";
|
|
6
|
+
const getChildKey = child => child.key || "";
|
|
7
|
+
function updateChildLookup(children, allChildren) {
|
|
8
|
+
children.forEach(child => {
|
|
9
|
+
const key = getChildKey(child);
|
|
10
|
+
allChildren.set(key, child);
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function onlyElements(children) {
|
|
14
|
+
const filtered = [];
|
|
15
|
+
return Children.forEach(children, child => {
|
|
16
|
+
isValidElement(child) && filtered.push(child);
|
|
17
|
+
}), filtered;
|
|
18
|
+
}
|
|
19
|
+
const AnimatePresence = ({
|
|
20
|
+
children,
|
|
21
|
+
enterVariant,
|
|
22
|
+
exitVariant,
|
|
23
|
+
enterExitVariant,
|
|
24
|
+
initial = !0,
|
|
25
|
+
onExitComplete,
|
|
26
|
+
exitBeforeEnter,
|
|
27
|
+
presenceAffectsLayout = !0,
|
|
28
|
+
custom
|
|
29
|
+
}) => {
|
|
30
|
+
let forceRender = useContext(LayoutGroupContext).forceRender ?? useForceUpdate();
|
|
31
|
+
const filteredChildren = onlyElements(children),
|
|
32
|
+
presentChildren = useRef(filteredChildren),
|
|
33
|
+
allChildren = useRef( /* @__PURE__ */new Map()).current,
|
|
34
|
+
exiting = useRef( /* @__PURE__ */new Set()).current;
|
|
35
|
+
updateChildLookup(filteredChildren, allChildren);
|
|
36
|
+
const isInitialRender = useRef(!0);
|
|
37
|
+
if (isInitialRender.current) return isInitialRender.current = !1, /* @__PURE__ */jsx(Fragment, {
|
|
38
|
+
children: filteredChildren.map(child => /* @__PURE__ */jsx(PresenceChild, {
|
|
39
|
+
isPresent: !0,
|
|
40
|
+
enterExitVariant,
|
|
41
|
+
exitVariant,
|
|
42
|
+
enterVariant,
|
|
43
|
+
initial: initial ? void 0 : !1,
|
|
44
|
+
presenceAffectsLayout,
|
|
45
|
+
custom,
|
|
46
|
+
children: child
|
|
47
|
+
}, getChildKey(child)))
|
|
48
|
+
});
|
|
49
|
+
let childrenToRender = [...filteredChildren];
|
|
50
|
+
const presentKeys = presentChildren.current.map(getChildKey),
|
|
51
|
+
targetKeys = filteredChildren.map(getChildKey),
|
|
52
|
+
numPresent = presentKeys.length;
|
|
53
|
+
for (let i = 0; i < numPresent; i++) {
|
|
54
|
+
const key = presentKeys[i];
|
|
55
|
+
targetKeys.indexOf(key) === -1 ? exiting.add(key) : exiting.delete(key);
|
|
56
|
+
}
|
|
57
|
+
return exitBeforeEnter && exiting.size && (childrenToRender = []), exiting.forEach(key => {
|
|
58
|
+
if (targetKeys.indexOf(key) !== -1) return;
|
|
59
|
+
const child = allChildren.get(key);
|
|
60
|
+
if (!child) return;
|
|
61
|
+
const insertionIndex = presentKeys.indexOf(key),
|
|
62
|
+
exitingComponent = /* @__PURE__ */jsx(PresenceChild, {
|
|
63
|
+
isPresent: !1,
|
|
64
|
+
onExitComplete: () => {
|
|
65
|
+
allChildren.delete(key), exiting.delete(key);
|
|
66
|
+
const removeIndex = presentChildren.current.findIndex(presentChild => presentChild.key === key);
|
|
67
|
+
presentChildren.current.splice(removeIndex, 1), exiting.size || (presentChildren.current = filteredChildren, forceRender(), onExitComplete?.());
|
|
68
|
+
},
|
|
69
|
+
presenceAffectsLayout,
|
|
70
|
+
enterExitVariant,
|
|
71
|
+
enterVariant,
|
|
72
|
+
exitVariant,
|
|
73
|
+
custom,
|
|
74
|
+
children: child
|
|
75
|
+
}, getChildKey(child));
|
|
76
|
+
childrenToRender.splice(insertionIndex, 0, exitingComponent);
|
|
77
|
+
}), childrenToRender = childrenToRender.map(child => {
|
|
78
|
+
const key = child.key;
|
|
79
|
+
return exiting.has(key) ? child : /* @__PURE__ */jsx(PresenceChild, {
|
|
80
|
+
isPresent: !0,
|
|
81
|
+
exitVariant,
|
|
82
|
+
enterVariant,
|
|
83
|
+
enterExitVariant,
|
|
84
|
+
presenceAffectsLayout,
|
|
85
|
+
custom,
|
|
86
|
+
children: child
|
|
87
|
+
}, getChildKey(child));
|
|
88
|
+
}), presentChildren.current = childrenToRender, /* @__PURE__ */jsx(Fragment, {
|
|
89
|
+
children: exiting.size ? childrenToRender : childrenToRender.map(child => cloneElement(child))
|
|
90
|
+
});
|
|
91
|
+
};
|
|
92
|
+
AnimatePresence.displayName = "AnimatePresence";
|
|
93
|
+
export { AnimatePresence };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useConstant } from "@tamagui/use-constant";
|
|
2
|
+
import { PresenceContext } from "@tamagui/use-presence";
|
|
3
|
+
import * as React from "react";
|
|
4
|
+
import { useId } from "react";
|
|
5
|
+
import { jsx } from "react/jsx-runtime";
|
|
6
|
+
const PresenceChild = ({
|
|
7
|
+
children,
|
|
8
|
+
initial,
|
|
9
|
+
isPresent,
|
|
10
|
+
onExitComplete,
|
|
11
|
+
exitVariant,
|
|
12
|
+
enterVariant,
|
|
13
|
+
enterExitVariant,
|
|
14
|
+
presenceAffectsLayout,
|
|
15
|
+
custom
|
|
16
|
+
}) => {
|
|
17
|
+
const presenceChildren = useConstant(newChildrenMap),
|
|
18
|
+
id = useId() || "",
|
|
19
|
+
context = React.useMemo(() => ({
|
|
20
|
+
id,
|
|
21
|
+
initial,
|
|
22
|
+
isPresent,
|
|
23
|
+
custom,
|
|
24
|
+
exitVariant,
|
|
25
|
+
enterVariant,
|
|
26
|
+
enterExitVariant,
|
|
27
|
+
onExitComplete: id2 => {
|
|
28
|
+
presenceChildren.set(id2, !0);
|
|
29
|
+
for (const isComplete of presenceChildren.values()) if (!isComplete) return;
|
|
30
|
+
onExitComplete?.();
|
|
31
|
+
},
|
|
32
|
+
register: id2 => (presenceChildren.set(id2, !1), () => presenceChildren.delete(id2))
|
|
33
|
+
}),
|
|
34
|
+
/**
|
|
35
|
+
* If the presence of a child affects the layout of the components around it,
|
|
36
|
+
* we want to make a new context value to ensure they get re-rendered
|
|
37
|
+
* so they can detect that layout change.
|
|
38
|
+
*/
|
|
39
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
40
|
+
presenceAffectsLayout ? void 0 : [isPresent, exitVariant, enterVariant]);
|
|
41
|
+
return React.useMemo(() => {
|
|
42
|
+
presenceChildren.forEach((_, key) => presenceChildren.set(key, !1));
|
|
43
|
+
}, [isPresent]), React.useEffect(() => {
|
|
44
|
+
!isPresent && !presenceChildren.size && onExitComplete?.();
|
|
45
|
+
}, [isPresent]), /* @__PURE__ */jsx(PresenceContext.Provider, {
|
|
46
|
+
value: context,
|
|
47
|
+
children
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
function newChildrenMap() {
|
|
51
|
+
return /* @__PURE__ */new Map();
|
|
52
|
+
}
|
|
53
|
+
export { PresenceChild };
|
|
File without changes
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/animate-presence",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.89.0-1706308641099",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -13,14 +13,14 @@
|
|
|
13
13
|
"dist"
|
|
14
14
|
],
|
|
15
15
|
"dependencies": {
|
|
16
|
-
"@tamagui/helpers": "1.
|
|
17
|
-
"@tamagui/use-constant": "1.
|
|
18
|
-
"@tamagui/use-force-update": "1.
|
|
19
|
-
"@tamagui/use-presence": "1.
|
|
20
|
-
"@tamagui/web": "1.
|
|
16
|
+
"@tamagui/helpers": "1.89.0-1706308641099",
|
|
17
|
+
"@tamagui/use-constant": "1.89.0-1706308641099",
|
|
18
|
+
"@tamagui/use-force-update": "1.89.0-1706308641099",
|
|
19
|
+
"@tamagui/use-presence": "1.89.0-1706308641099",
|
|
20
|
+
"@tamagui/web": "1.89.0-1706308641099"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@tamagui/build": "1.
|
|
23
|
+
"@tamagui/build": "1.89.0-1706308641099"
|
|
24
24
|
},
|
|
25
25
|
"scripts": {
|
|
26
26
|
"build": "tamagui-build",
|