@tamagui/animate-presence 1.0.1-beta.193 → 1.0.1-beta.194
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 +10 -6
- package/dist/cjs/AnimatePresence.js.map +2 -2
- package/dist/cjs/PresenceChild.js +5 -5
- package/dist/cjs/PresenceChild.js.map +2 -2
- package/dist/cjs/index.js +1 -13
- package/dist/cjs/index.js.map +2 -2
- package/dist/esm/AnimatePresence.js +12 -12
- package/dist/esm/AnimatePresence.js.map +2 -2
- package/dist/esm/PresenceChild.js +3 -3
- package/dist/esm/PresenceChild.js.map +2 -2
- package/dist/esm/index.js +1 -5
- package/dist/esm/index.js.map +2 -2
- package/dist/jsx/AnimatePresence.js +12 -12
- package/dist/jsx/AnimatePresence.js.map +2 -2
- package/dist/jsx/PresenceChild.js +3 -3
- package/dist/jsx/PresenceChild.js.map +2 -2
- package/dist/jsx/index.js +1 -5
- package/dist/jsx/index.js.map +2 -2
- package/package.json +4 -3
- package/src/AnimatePresence.tsx +14 -13
- package/src/PresenceChild.tsx +3 -3
- package/src/index.ts +1 -2
- package/types/index.d.ts +1 -2
- package/src/PresenceContext.tsx +0 -16
- package/src/usePresence.ts +0 -37
|
@@ -79,23 +79,27 @@ const AnimatePresence = ({
|
|
|
79
79
|
const forceRenderLayoutGroup = (0, import_react.useContext)(import_LayoutGroupContext.LayoutGroupContext).forceRender;
|
|
80
80
|
if (forceRenderLayoutGroup)
|
|
81
81
|
forceRender = forceRenderLayoutGroup;
|
|
82
|
-
const isMounted = (0,
|
|
82
|
+
const isMounted = (0, import_react.useRef)(false);
|
|
83
83
|
const filteredChildren = onlyElements(children);
|
|
84
84
|
let childrenToRender = filteredChildren;
|
|
85
85
|
const exiting = /* @__PURE__ */ new Set();
|
|
86
86
|
const presentChildren = (0, import_react.useRef)(childrenToRender);
|
|
87
87
|
const allChildren = (0, import_react.useRef)(/* @__PURE__ */ new Map()).current;
|
|
88
88
|
const isInitialRender = (0, import_react.useRef)(true);
|
|
89
|
+
(0, import_react.useEffect)(() => {
|
|
90
|
+
isMounted.current = true;
|
|
91
|
+
return () => {
|
|
92
|
+
isMounted.current = false;
|
|
93
|
+
isInitialRender.current = true;
|
|
94
|
+
allChildren.clear();
|
|
95
|
+
exiting.clear();
|
|
96
|
+
};
|
|
97
|
+
}, []);
|
|
89
98
|
(0, import_core.useIsomorphicLayoutEffect)(() => {
|
|
90
99
|
isInitialRender.current = false;
|
|
91
100
|
updateChildLookup(filteredChildren, allChildren);
|
|
92
101
|
presentChildren.current = childrenToRender;
|
|
93
102
|
});
|
|
94
|
-
(0, import_core.useUnmountEffect)(() => {
|
|
95
|
-
isInitialRender.current = true;
|
|
96
|
-
allChildren.clear();
|
|
97
|
-
exiting.clear();
|
|
98
|
-
});
|
|
99
103
|
if (isInitialRender.current) {
|
|
100
104
|
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
|
|
101
105
|
children: childrenToRender.map((child) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_PresenceChild.PresenceChild, {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;
|
|
4
|
+
"sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\nimport { AnimatePresenceProps } from './types'\n\nReact['keep']\n\ntype ComponentKey = string | number\n\nconst getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n\nconst isDev = process.env.NODE_ENV !== 'production'\n\nfunction updateChildLookup(\n children: ReactElement<any>[],\n allChildren: Map<ComponentKey, ReactElement<any>>\n) {\n const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n children.forEach((child) => {\n const key = getChildKey(child)\n\n if (isDev && seenChildren && seenChildren.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n\n seenChildren.add(key)\n }\n\n allChildren.set(key, child)\n })\n}\n\nfunction onlyElements(children: ReactNode): ReactElement<any>[] {\n const filtered: ReactElement<any>[] = []\n\n // We use forEach here instead of map as map mutates the component key by preprending `.$`\n Children.forEach(children, (child, index) => {\n if (isValidElement(child)) {\n if (!child.key) {\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(`No key given to AnimatePresence child, assigning index as key`)\n }\n filtered.push(\n React.cloneElement(child, {\n key: index,\n })\n )\n } else {\n filtered.push(child)\n }\n }\n })\n\n return filtered\n}\n\n/**\n * `AnimatePresence` enables the animation of components that have been removed from the tree.\n *\n * When adding/removing more than a single child, every child **must** be given a unique `key` prop.\n *\n * Any `motion` components that have an `exit` property defined will animate out when removed from\n * the tree.\n *\n * ```tsx\n * import { motion, AnimatePresence } from 'framer-motion'\n *\n * export const Items = ({ items }) => (\n * <AnimatePresence>\n * {items.map(item => (\n * <motion.div\n * key={item.id}\n * initial={{ opacity: 0 }}\n * animate={{ opacity: 1 }}\n * exit={{ opacity: 0 }}\n * />\n * ))}\n * </AnimatePresence>\n * )\n * ```\n *\n * You can sequence exit animations throughout a tree using variants.\n *\n * If a child contains multiple `motion` components with `exit` props, it will only unmount the child\n * once all `motion` components have finished animating out. Likewise, any components using\n * `usePresence` all need to call `safeToRemove`.\n *\n * @public\n */\nexport const AnimatePresence: React.FunctionComponent<\n React.PropsWithChildren<AnimatePresenceProps>\n> = ({\n children,\n enterVariant,\n exitVariant,\n initial = true,\n onExitComplete,\n exitBeforeEnter,\n presenceAffectsLayout = true,\n}) => {\n // We want to force a re-render once all exiting animations have finished. We\n // either use a local forceRender function, or one from a parent context if it exists.\n let forceRender = useForceUpdate()\n const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender\n if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup\n\n const isMounted = useRef(false)\n\n // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key\n const filteredChildren = onlyElements(children)\n let childrenToRender = filteredChildren\n\n const exiting = new Set<ComponentKey>()\n\n // Keep a living record of the children we're actually rendering so we\n // can diff to figure out which are entering and exiting\n const presentChildren = useRef(childrenToRender)\n\n // A lookup table to quickly reference components by key\n const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current\n\n // If this is the initial component render, just deal with logic surrounding whether\n // we play onMount animations or not.\n const isInitialRender = useRef(true)\n\n useEffect(() => {\n isMounted.current = true\n return () => {\n isMounted.current = false\n isInitialRender.current = true\n allChildren.clear()\n exiting.clear()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n isInitialRender.current = false\n updateChildLookup(filteredChildren, allChildren)\n presentChildren.current = childrenToRender\n })\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n initial={initial ? undefined : false}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n ))}\n </>\n )\n }\n\n // If this is a subsequent render, deal with entering and exiting children\n childrenToRender = [...childrenToRender]\n\n // Diff the keys of the currently-present and target children to update our\n // exiting list.\n const presentKeys = presentChildren.current.map(getChildKey)\n const targetKeys = filteredChildren.map(getChildKey)\n\n // Diff the present children with our target children and mark those that are exiting\n const numPresent = presentKeys.length\n for (let i = 0; i < numPresent; i++) {\n const key = presentKeys[i]\n if (targetKeys.indexOf(key) === -1) {\n exiting.add(key)\n }\n }\n\n // If we currently have exiting children, and we're deferring rendering incoming children\n // until after all current children have exiting, empty the childrenToRender array\n if (exitBeforeEnter && exiting.size) {\n childrenToRender = []\n }\n\n // Loop through all currently exiting components and clone them to overwrite `animate`\n // with any `exit` prop they might have defined.\n exiting.forEach((key) => {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) return\n\n const child = allChildren.get(key)\n if (!child) return\n\n const insertionIndex = presentKeys.indexOf(key)\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={() => {\n allChildren.delete(key)\n exiting.delete(key)\n\n // Remove this child from the present children\n const removeIndex = presentChildren.current.findIndex(\n (presentChild) => presentChild.key === key\n )\n presentChildren.current.splice(removeIndex, 1)\n\n // Defer re-rendering until all exiting children have indeed left\n if (!exiting.size) {\n presentChildren.current = filteredChildren\n\n if (isMounted.current === false) return\n\n forceRender()\n onExitComplete?.()\n }\n }}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n // Add `MotionContext` even to children that don't need it to ensure we're rendering\n // the same tree between renders\n childrenToRender = childrenToRender.map((child) => {\n const key = child.key as string | number\n return exiting.has(key) ? (\n child\n ) : (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {\n // eslint-disable-next-line no-console\n console.warn(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`\n )\n }\n\n return (\n <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AA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,YAAY,QAAQ,GAAG;AAE9C,qBAAiB;AAAA,MACf;AAAA,MACA;AAAA,MACA,4CAAC;AAAA,QAEC,WAAW;AAAA,QACX,gBAAgB,MAAM;AACpB,sBAAY,OAAO,GAAG;AACtB,kBAAQ,OAAO,GAAG;AAGlB,gBAAM,cAAc,gBAAgB,QAAQ;AAAA,YAC1C,CAAC,iBAAiB,aAAa,QAAQ;AAAA,UACzC;AACA,0BAAgB,QAAQ,OAAO,aAAa,CAAC;AAG7C,cAAI,CAAC,QAAQ,MAAM;AACjB,4BAAgB,UAAU;AAE1B,gBAAI,UAAU,YAAY;AAAO;AAEjC,wBAAY;AACZ;AAAA,UACF;AAAA,QACF;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QAEC;AAAA,SA1BI,YAAY,KAAK,CA2BxB;AAAA,IACF;AAAA,EACF,CAAC;AAID,qBAAmB,iBAAiB,IAAI,CAAC,UAAU;AACjD,UAAM,MAAM,MAAM;AAClB,WAAO,QAAQ,IAAI,GAAG,IACpB,QAEA,4CAAC;AAAA,MAEC,WAAS;AAAA,MACT;AAAA,MACA;AAAA,MACA;AAAA,MAEC;AAAA,OANI,YAAY,KAAK,CAOxB;AAAA,EAEJ,CAAC;AAED,MAAI,QAAQ,IAAI,aAAa,gBAAgB,mBAAmB,iBAAiB,SAAS,GAAG;AAE3F,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAEA,SACE;AAAA,IAAG,kBAAQ,OAAO,mBAAmB,iBAAiB,IAAI,CAAC,cAAU,2BAAa,KAAK,CAAC;AAAA,GAAE;AAE9F;AAEA,gBAAgB,cAAc;",
|
|
6
6
|
"names": ["React"]
|
|
7
7
|
}
|
|
@@ -28,9 +28,9 @@ __export(PresenceChild_exports, {
|
|
|
28
28
|
});
|
|
29
29
|
module.exports = __toCommonJS(PresenceChild_exports);
|
|
30
30
|
var import_jsx_runtime = require("react/jsx-runtime");
|
|
31
|
-
var
|
|
31
|
+
var import_use_presence = require("@tamagui/use-presence");
|
|
32
32
|
var React = __toESM(require("react"));
|
|
33
|
-
var
|
|
33
|
+
var import_react = require("react");
|
|
34
34
|
const PresenceChild = ({
|
|
35
35
|
children,
|
|
36
36
|
initial,
|
|
@@ -40,8 +40,8 @@ const PresenceChild = ({
|
|
|
40
40
|
enterVariant,
|
|
41
41
|
presenceAffectsLayout
|
|
42
42
|
}) => {
|
|
43
|
-
const presenceChildren = (
|
|
44
|
-
const id = (0,
|
|
43
|
+
const presenceChildren = React.useMemo(newChildrenMap, []);
|
|
44
|
+
const id = (0, import_react.useId)() || "";
|
|
45
45
|
const context = React.useMemo(
|
|
46
46
|
() => {
|
|
47
47
|
return {
|
|
@@ -73,7 +73,7 @@ const PresenceChild = ({
|
|
|
73
73
|
React.useEffect(() => {
|
|
74
74
|
!isPresent && !presenceChildren.size && (onExitComplete == null ? void 0 : onExitComplete());
|
|
75
75
|
}, [isPresent]);
|
|
76
|
-
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
|
|
76
|
+
return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_use_presence.PresenceContext.Provider, {
|
|
77
77
|
value: context,
|
|
78
78
|
children
|
|
79
79
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA2ES;AA3ET,
|
|
4
|
+
"sourcesContent": ["import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'\nimport * as React from 'react'\nimport { useId } from 'react'\n\nimport { VariantLabels } from './types'\n\ninterface PresenceChildProps {\n children: React.ReactElement<any>\n isPresent: boolean\n onExitComplete?: () => void\n initial?: false | VariantLabels\n custom?: any\n presenceAffectsLayout: boolean\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceChild = ({\n children,\n initial,\n isPresent,\n onExitComplete,\n exitVariant,\n enterVariant,\n presenceAffectsLayout,\n}: PresenceChildProps) => {\n const presenceChildren = React.useMemo(newChildrenMap, [])\n const id = useId() || ''\n\n const context = React.useMemo(\n (): PresenceContextProps => {\n return {\n id,\n initial,\n isPresent,\n exitVariant,\n enterVariant,\n onExitComplete: (id: string) => {\n presenceChildren.set(id, true)\n for (const isComplete of presenceChildren.values()) {\n if (!isComplete) {\n return // can stop searching when any is incomplete\n }\n }\n onExitComplete?.()\n },\n register: (id: string) => {\n presenceChildren.set(id, false)\n return () => presenceChildren.delete(id)\n },\n }\n },\n /**\n * If the presence of a child affects the layout of the components around it,\n * we want to make a new context value to ensure they get re-rendered\n * so they can detect that layout change.\n */\n // eslint-disable-next-line react-hooks/exhaustive-deps\n presenceAffectsLayout ? undefined : [isPresent, exitVariant, enterVariant]\n )\n\n React.useMemo(() => {\n presenceChildren.forEach((_, key) => presenceChildren.set(key, false))\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n /**\n * If there's no animated components to fire exit animations, we want to remove this\n * component immediately.\n */\n React.useEffect(() => {\n !isPresent && !presenceChildren.size && onExitComplete?.()\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA2ES;AA3ET,0BAAsD;AACtD,YAAuB;AACvB,mBAAsB;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,SAAK,oBAAM,KAAK;AAEtB,QAAM,UAAU,MAAM;AAAA,IACpB,MAA4B;AAC1B,aAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,CAACA,QAAe;AAC9B,2BAAiB,IAAIA,KAAI,IAAI;AAC7B,qBAAW,cAAc,iBAAiB,OAAO,GAAG;AAClD,gBAAI,CAAC,YAAY;AACf;AAAA,YACF;AAAA,UACF;AACA;AAAA,QACF;AAAA,QACA,UAAU,CAACA,QAAe;AACxB,2BAAiB,IAAIA,KAAI,KAAK;AAC9B,iBAAO,MAAM,iBAAiB,OAAOA,GAAE;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA,IAOA,wBAAwB,SAAY,CAAC,WAAW,aAAa,YAAY;AAAA,EAC3E;AAEA,QAAM,QAAQ,MAAM;AAClB,qBAAiB,QAAQ,CAAC,GAAG,QAAQ,iBAAiB,IAAI,KAAK,KAAK,CAAC;AAAA,EAEvE,GAAG,CAAC,SAAS,CAAC;AAMd,QAAM,UAAU,MAAM;AACpB,KAAC,aAAa,CAAC,iBAAiB,SAAQ;AAAA,EAE1C,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO,4CAAC,oCAAgB,UAAhB;AAAA,IAAyB,OAAO;AAAA,IAAU;AAAA,GAAS;AAC7D;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
|
|
6
6
|
"names": ["id"]
|
|
7
7
|
}
|
package/dist/cjs/index.js
CHANGED
|
@@ -3,10 +3,6 @@ var __defProp = Object.defineProperty;
|
|
|
3
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
6
|
var __copyProps = (to, from, except, desc) => {
|
|
11
7
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
8
|
for (let key of __getOwnPropNames(from))
|
|
@@ -18,15 +14,7 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
14
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
15
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
16
|
var src_exports = {};
|
|
21
|
-
__export(src_exports, {
|
|
22
|
-
PresenceContext: () => import_PresenceContext.PresenceContext
|
|
23
|
-
});
|
|
24
17
|
module.exports = __toCommonJS(src_exports);
|
|
25
18
|
__reExport(src_exports, require("./AnimatePresence"), module.exports);
|
|
26
|
-
__reExport(src_exports, require("
|
|
27
|
-
var import_PresenceContext = require("./PresenceContext");
|
|
28
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
29
|
-
0 && (module.exports = {
|
|
30
|
-
PresenceContext
|
|
31
|
-
});
|
|
19
|
+
__reExport(src_exports, require("@tamagui/use-presence"), module.exports);
|
|
32
20
|
//# 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 '
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["export * from './AnimatePresence'\nexport * from '@tamagui/use-presence'\n"],
|
|
5
|
+
"mappings": ";;;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,8BAAd;AACA,wBAAc,kCADd;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { Fragment, jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
3
|
-
useForceUpdate,
|
|
4
|
-
useIsMounted,
|
|
5
|
-
useIsomorphicLayoutEffect,
|
|
6
|
-
useUnmountEffect
|
|
7
|
-
} from "@tamagui/core";
|
|
2
|
+
import { useForceUpdate, useIsomorphicLayoutEffect } from "@tamagui/core";
|
|
8
3
|
import React, {
|
|
9
4
|
Children,
|
|
10
5
|
cloneElement,
|
|
11
6
|
isValidElement,
|
|
12
7
|
useContext,
|
|
8
|
+
useEffect,
|
|
13
9
|
useRef
|
|
14
10
|
} from "react";
|
|
15
11
|
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
@@ -61,23 +57,27 @@ const AnimatePresence = ({
|
|
|
61
57
|
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
62
58
|
if (forceRenderLayoutGroup)
|
|
63
59
|
forceRender = forceRenderLayoutGroup;
|
|
64
|
-
const isMounted =
|
|
60
|
+
const isMounted = useRef(false);
|
|
65
61
|
const filteredChildren = onlyElements(children);
|
|
66
62
|
let childrenToRender = filteredChildren;
|
|
67
63
|
const exiting = /* @__PURE__ */ new Set();
|
|
68
64
|
const presentChildren = useRef(childrenToRender);
|
|
69
65
|
const allChildren = useRef(/* @__PURE__ */ new Map()).current;
|
|
70
66
|
const isInitialRender = useRef(true);
|
|
67
|
+
useEffect(() => {
|
|
68
|
+
isMounted.current = true;
|
|
69
|
+
return () => {
|
|
70
|
+
isMounted.current = false;
|
|
71
|
+
isInitialRender.current = true;
|
|
72
|
+
allChildren.clear();
|
|
73
|
+
exiting.clear();
|
|
74
|
+
};
|
|
75
|
+
}, []);
|
|
71
76
|
useIsomorphicLayoutEffect(() => {
|
|
72
77
|
isInitialRender.current = false;
|
|
73
78
|
updateChildLookup(filteredChildren, allChildren);
|
|
74
79
|
presentChildren.current = childrenToRender;
|
|
75
80
|
});
|
|
76
|
-
useUnmountEffect(() => {
|
|
77
|
-
isInitialRender.current = true;
|
|
78
|
-
allChildren.clear();
|
|
79
|
-
exiting.clear();
|
|
80
|
-
});
|
|
81
81
|
if (isInitialRender.current) {
|
|
82
82
|
return /* @__PURE__ */ jsx(Fragment, {
|
|
83
83
|
children: childrenToRender.map((child) => /* @__PURE__ */ jsx(PresenceChild, {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "
|
|
4
|
+
"sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\nimport { AnimatePresenceProps } from './types'\n\nReact['keep']\n\ntype ComponentKey = string | number\n\nconst getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n\nconst isDev = process.env.NODE_ENV !== 'production'\n\nfunction updateChildLookup(\n children: ReactElement<any>[],\n allChildren: Map<ComponentKey, ReactElement<any>>\n) {\n const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n children.forEach((child) => {\n const key = getChildKey(child)\n\n if (isDev && seenChildren && seenChildren.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n\n seenChildren.add(key)\n }\n\n allChildren.set(key, child)\n })\n}\n\nfunction onlyElements(children: ReactNode): ReactElement<any>[] {\n const filtered: ReactElement<any>[] = []\n\n // We use forEach here instead of map as map mutates the component key by preprending `.$`\n Children.forEach(children, (child, index) => {\n if (isValidElement(child)) {\n if (!child.key) {\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(`No key given to AnimatePresence child, assigning index as key`)\n }\n filtered.push(\n React.cloneElement(child, {\n key: index,\n })\n )\n } else {\n filtered.push(child)\n }\n }\n })\n\n return filtered\n}\n\n/**\n * `AnimatePresence` enables the animation of components that have been removed from the tree.\n *\n * When adding/removing more than a single child, every child **must** be given a unique `key` prop.\n *\n * Any `motion` components that have an `exit` property defined will animate out when removed from\n * the tree.\n *\n * ```tsx\n * import { motion, AnimatePresence } from 'framer-motion'\n *\n * export const Items = ({ items }) => (\n * <AnimatePresence>\n * {items.map(item => (\n * <motion.div\n * key={item.id}\n * initial={{ opacity: 0 }}\n * animate={{ opacity: 1 }}\n * exit={{ opacity: 0 }}\n * />\n * ))}\n * </AnimatePresence>\n * )\n * ```\n *\n * You can sequence exit animations throughout a tree using variants.\n *\n * If a child contains multiple `motion` components with `exit` props, it will only unmount the child\n * once all `motion` components have finished animating out. Likewise, any components using\n * `usePresence` all need to call `safeToRemove`.\n *\n * @public\n */\nexport const AnimatePresence: React.FunctionComponent<\n React.PropsWithChildren<AnimatePresenceProps>\n> = ({\n children,\n enterVariant,\n exitVariant,\n initial = true,\n onExitComplete,\n exitBeforeEnter,\n presenceAffectsLayout = true,\n}) => {\n // We want to force a re-render once all exiting animations have finished. We\n // either use a local forceRender function, or one from a parent context if it exists.\n let forceRender = useForceUpdate()\n const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender\n if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup\n\n const isMounted = useRef(false)\n\n // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key\n const filteredChildren = onlyElements(children)\n let childrenToRender = filteredChildren\n\n const exiting = new Set<ComponentKey>()\n\n // Keep a living record of the children we're actually rendering so we\n // can diff to figure out which are entering and exiting\n const presentChildren = useRef(childrenToRender)\n\n // A lookup table to quickly reference components by key\n const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current\n\n // If this is the initial component render, just deal with logic surrounding whether\n // we play onMount animations or not.\n const isInitialRender = useRef(true)\n\n useEffect(() => {\n isMounted.current = true\n return () => {\n isMounted.current = false\n isInitialRender.current = true\n allChildren.clear()\n exiting.clear()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n isInitialRender.current = false\n updateChildLookup(filteredChildren, allChildren)\n presentChildren.current = childrenToRender\n })\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n initial={initial ? undefined : false}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n ))}\n </>\n )\n }\n\n // If this is a subsequent render, deal with entering and exiting children\n childrenToRender = [...childrenToRender]\n\n // Diff the keys of the currently-present and target children to update our\n // exiting list.\n const presentKeys = presentChildren.current.map(getChildKey)\n const targetKeys = filteredChildren.map(getChildKey)\n\n // Diff the present children with our target children and mark those that are exiting\n const numPresent = presentKeys.length\n for (let i = 0; i < numPresent; i++) {\n const key = presentKeys[i]\n if (targetKeys.indexOf(key) === -1) {\n exiting.add(key)\n }\n }\n\n // If we currently have exiting children, and we're deferring rendering incoming children\n // until after all current children have exiting, empty the childrenToRender array\n if (exitBeforeEnter && exiting.size) {\n childrenToRender = []\n }\n\n // Loop through all currently exiting components and clone them to overwrite `animate`\n // with any `exit` prop they might have defined.\n exiting.forEach((key) => {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) return\n\n const child = allChildren.get(key)\n if (!child) return\n\n const insertionIndex = presentKeys.indexOf(key)\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={() => {\n allChildren.delete(key)\n exiting.delete(key)\n\n // Remove this child from the present children\n const removeIndex = presentChildren.current.findIndex(\n (presentChild) => presentChild.key === key\n )\n presentChildren.current.splice(removeIndex, 1)\n\n // Defer re-rendering until all exiting children have indeed left\n if (!exiting.size) {\n presentChildren.current = filteredChildren\n\n if (isMounted.current === false) return\n\n forceRender()\n onExitComplete?.()\n }\n }}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n // Add `MotionContext` even to children that don't need it to ensure we're rendering\n // the same tree between renders\n childrenToRender = childrenToRender.map((child) => {\n const key = child.key as string | number\n return exiting.has(key) ? (\n child\n ) : (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {\n // eslint-disable-next-line no-console\n console.warn(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`\n )\n }\n\n return (\n <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
|
|
5
|
+
"mappings": "AA6JM,mBAEI,WAFJ;AA7JN,SAAS,gBAAgB,iCAAiC;AAC1D,OAAO;AAAA,EACL;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0BAA0B;AACnC,SAAS,qBAAqB;AAG9B,MAAM;AAIN,MAAM,cAAc,CAAC,UAA2C,MAAM,OAAO;AAE7E,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,SAAS,kBACP,UACA,aACA;AACA,QAAM,eAAe,QAAQ,oBAAI,IAAkB,IAAI;AAEvD,WAAS,QAAQ,CAAC,UAAU;AAC1B,UAAM,MAAM,YAAY,KAAK;AAE7B,QAAI,SAAS,gBAAgB,aAAa,IAAI,GAAG,GAAG;AAElD,cAAQ,KAAK,qDAAqD,sBAAsB;AAExF,mBAAa,IAAI,GAAG;AAAA,IACtB;AAEA,gBAAY,IAAI,KAAK,KAAK;AAAA,EAC5B,CAAC;AACH;AAEA,SAAS,aAAa,UAA0C;AAC9D,QAAM,WAAgC,CAAC;AAGvC,WAAS,QAAQ,UAAU,CAAC,OAAO,UAAU;AAC3C,QAAI,eAAe,KAAK,GAAG;AACzB,UAAI,CAAC,MAAM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;AAAA,MACA;AAAA,MACA;AAAA,MAEC;AAAA,OANI,YAAY,KAAK,CAOxB;AAAA,EAEJ,CAAC;AAED,MAAI,QAAQ,IAAI,aAAa,gBAAgB,mBAAmB,iBAAiB,SAAS,GAAG;AAE3F,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAEA,SACE;AAAA,IAAG,kBAAQ,OAAO,mBAAmB,iBAAiB,IAAI,CAAC,UAAU,aAAa,KAAK,CAAC;AAAA,GAAE;AAE9F;AAEA,gBAAgB,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
|
-
import {
|
|
2
|
+
import { PresenceContext } from "@tamagui/use-presence";
|
|
3
3
|
import * as React from "react";
|
|
4
|
-
import {
|
|
4
|
+
import { useId } from "react";
|
|
5
5
|
const PresenceChild = ({
|
|
6
6
|
children,
|
|
7
7
|
initial,
|
|
@@ -11,7 +11,7 @@ const PresenceChild = ({
|
|
|
11
11
|
enterVariant,
|
|
12
12
|
presenceAffectsLayout
|
|
13
13
|
}) => {
|
|
14
|
-
const presenceChildren =
|
|
14
|
+
const presenceChildren = React.useMemo(newChildrenMap, []);
|
|
15
15
|
const id = useId() || "";
|
|
16
16
|
const context = React.useMemo(
|
|
17
17
|
() => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AA2ES;AA3ET,SAAS,
|
|
4
|
+
"sourcesContent": ["import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'\nimport * as React from 'react'\nimport { useId } from 'react'\n\nimport { VariantLabels } from './types'\n\ninterface PresenceChildProps {\n children: React.ReactElement<any>\n isPresent: boolean\n onExitComplete?: () => void\n initial?: false | VariantLabels\n custom?: any\n presenceAffectsLayout: boolean\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceChild = ({\n children,\n initial,\n isPresent,\n onExitComplete,\n exitVariant,\n enterVariant,\n presenceAffectsLayout,\n}: PresenceChildProps) => {\n const presenceChildren = React.useMemo(newChildrenMap, [])\n const id = useId() || ''\n\n const context = React.useMemo(\n (): PresenceContextProps => {\n return {\n id,\n initial,\n isPresent,\n exitVariant,\n enterVariant,\n onExitComplete: (id: string) => {\n presenceChildren.set(id, true)\n for (const isComplete of presenceChildren.values()) {\n if (!isComplete) {\n return // can stop searching when any is incomplete\n }\n }\n onExitComplete?.()\n },\n register: (id: string) => {\n presenceChildren.set(id, false)\n return () => presenceChildren.delete(id)\n },\n }\n },\n /**\n * If the presence of a child affects the layout of the components around it,\n * we want to make a new context value to ensure they get re-rendered\n * so they can detect that layout change.\n */\n // eslint-disable-next-line react-hooks/exhaustive-deps\n presenceAffectsLayout ? undefined : [isPresent, exitVariant, enterVariant]\n )\n\n React.useMemo(() => {\n presenceChildren.forEach((_, key) => presenceChildren.set(key, false))\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n /**\n * If there's no animated components to fire exit animations, we want to remove this\n * component immediately.\n */\n React.useEffect(() => {\n !isPresent && !presenceChildren.size && onExitComplete?.()\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
|
|
5
|
+
"mappings": "AA2ES;AA3ET,SAAS,uBAA6C;AACtD,YAAY,WAAW;AACvB,SAAS,aAAa;AAef,MAAM,gBAAgB,CAAC;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA0B;AACxB,QAAM,mBAAmB,MAAM,QAAQ,gBAAgB,CAAC,CAAC;AACzD,QAAM,KAAK,MAAM,KAAK;AAEtB,QAAM,UAAU,MAAM;AAAA,IACpB,MAA4B;AAC1B,aAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,CAACA,QAAe;AAC9B,2BAAiB,IAAIA,KAAI,IAAI;AAC7B,qBAAW,cAAc,iBAAiB,OAAO,GAAG;AAClD,gBAAI,CAAC,YAAY;AACf;AAAA,YACF;AAAA,UACF;AACA;AAAA,QACF;AAAA,QACA,UAAU,CAACA,QAAe;AACxB,2BAAiB,IAAIA,KAAI,KAAK;AAC9B,iBAAO,MAAM,iBAAiB,OAAOA,GAAE;AAAA,QACzC;AAAA,MACF;AAAA,IACF;AAAA,IAOA,wBAAwB,SAAY,CAAC,WAAW,aAAa,YAAY;AAAA,EAC3E;AAEA,QAAM,QAAQ,MAAM;AAClB,qBAAiB,QAAQ,CAAC,GAAG,QAAQ,iBAAiB,IAAI,KAAK,KAAK,CAAC;AAAA,EAEvE,GAAG,CAAC,SAAS,CAAC;AAMd,QAAM,UAAU,MAAM;AACpB,KAAC,aAAa,CAAC,iBAAiB,SAAQ;AAAA,EAE1C,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO,oBAAC,gBAAgB,UAAhB;AAAA,IAAyB,OAAO;AAAA,IAAU;AAAA,GAAS;AAC7D;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
|
|
6
6
|
"names": ["id"]
|
|
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 '
|
|
5
|
-
"mappings": "AAAA,cAAc;AACd,cAAc;
|
|
4
|
+
"sourcesContent": ["export * from './AnimatePresence'\nexport * from '@tamagui/use-presence'\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useForceUpdate,
|
|
3
|
-
useIsMounted,
|
|
4
|
-
useIsomorphicLayoutEffect,
|
|
5
|
-
useUnmountEffect
|
|
6
|
-
} from "@tamagui/core";
|
|
1
|
+
import { useForceUpdate, useIsomorphicLayoutEffect } from "@tamagui/core";
|
|
7
2
|
import React, {
|
|
8
3
|
Children,
|
|
9
4
|
cloneElement,
|
|
10
5
|
isValidElement,
|
|
11
6
|
useContext,
|
|
7
|
+
useEffect,
|
|
12
8
|
useRef
|
|
13
9
|
} from "react";
|
|
14
10
|
import { LayoutGroupContext } from "./LayoutGroupContext";
|
|
@@ -60,23 +56,27 @@ const AnimatePresence = ({
|
|
|
60
56
|
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender;
|
|
61
57
|
if (forceRenderLayoutGroup)
|
|
62
58
|
forceRender = forceRenderLayoutGroup;
|
|
63
|
-
const isMounted =
|
|
59
|
+
const isMounted = useRef(false);
|
|
64
60
|
const filteredChildren = onlyElements(children);
|
|
65
61
|
let childrenToRender = filteredChildren;
|
|
66
62
|
const exiting = /* @__PURE__ */ new Set();
|
|
67
63
|
const presentChildren = useRef(childrenToRender);
|
|
68
64
|
const allChildren = useRef(/* @__PURE__ */ new Map()).current;
|
|
69
65
|
const isInitialRender = useRef(true);
|
|
66
|
+
useEffect(() => {
|
|
67
|
+
isMounted.current = true;
|
|
68
|
+
return () => {
|
|
69
|
+
isMounted.current = false;
|
|
70
|
+
isInitialRender.current = true;
|
|
71
|
+
allChildren.clear();
|
|
72
|
+
exiting.clear();
|
|
73
|
+
};
|
|
74
|
+
}, []);
|
|
70
75
|
useIsomorphicLayoutEffect(() => {
|
|
71
76
|
isInitialRender.current = false;
|
|
72
77
|
updateChildLookup(filteredChildren, allChildren);
|
|
73
78
|
presentChildren.current = childrenToRender;
|
|
74
79
|
});
|
|
75
|
-
useUnmountEffect(() => {
|
|
76
|
-
isInitialRender.current = true;
|
|
77
|
-
allChildren.clear();
|
|
78
|
-
exiting.clear();
|
|
79
|
-
});
|
|
80
80
|
if (isInitialRender.current) {
|
|
81
81
|
return <>{childrenToRender.map((child) => <PresenceChild key={getChildKey(child)} isPresent exitVariant={exitVariant} enterVariant={enterVariant} initial={initial ? void 0 : false} presenceAffectsLayout={presenceAffectsLayout}>{child}</PresenceChild>)}</>;
|
|
82
82
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/AnimatePresence.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA
|
|
4
|
+
"sourcesContent": ["import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport React, {\n Children,\n ReactElement,\n ReactNode,\n cloneElement,\n isValidElement,\n useContext,\n useEffect,\n useRef,\n} from 'react'\n\nimport { LayoutGroupContext } from './LayoutGroupContext'\nimport { PresenceChild } from './PresenceChild'\nimport { AnimatePresenceProps } from './types'\n\nReact['keep']\n\ntype ComponentKey = string | number\n\nconst getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''\n\nconst isDev = process.env.NODE_ENV !== 'production'\n\nfunction updateChildLookup(\n children: ReactElement<any>[],\n allChildren: Map<ComponentKey, ReactElement<any>>\n) {\n const seenChildren = isDev ? new Set<ComponentKey>() : null\n\n children.forEach((child) => {\n const key = getChildKey(child)\n\n if (isDev && seenChildren && seenChildren.has(key)) {\n // eslint-disable-next-line no-console\n console.warn(`Children of AnimatePresence require unique keys. \"${key}\" is a duplicate.`)\n\n seenChildren.add(key)\n }\n\n allChildren.set(key, child)\n })\n}\n\nfunction onlyElements(children: ReactNode): ReactElement<any>[] {\n const filtered: ReactElement<any>[] = []\n\n // We use forEach here instead of map as map mutates the component key by preprending `.$`\n Children.forEach(children, (child, index) => {\n if (isValidElement(child)) {\n if (!child.key) {\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(`No key given to AnimatePresence child, assigning index as key`)\n }\n filtered.push(\n React.cloneElement(child, {\n key: index,\n })\n )\n } else {\n filtered.push(child)\n }\n }\n })\n\n return filtered\n}\n\n/**\n * `AnimatePresence` enables the animation of components that have been removed from the tree.\n *\n * When adding/removing more than a single child, every child **must** be given a unique `key` prop.\n *\n * Any `motion` components that have an `exit` property defined will animate out when removed from\n * the tree.\n *\n * ```tsx\n * import { motion, AnimatePresence } from 'framer-motion'\n *\n * export const Items = ({ items }) => (\n * <AnimatePresence>\n * {items.map(item => (\n * <motion.div\n * key={item.id}\n * initial={{ opacity: 0 }}\n * animate={{ opacity: 1 }}\n * exit={{ opacity: 0 }}\n * />\n * ))}\n * </AnimatePresence>\n * )\n * ```\n *\n * You can sequence exit animations throughout a tree using variants.\n *\n * If a child contains multiple `motion` components with `exit` props, it will only unmount the child\n * once all `motion` components have finished animating out. Likewise, any components using\n * `usePresence` all need to call `safeToRemove`.\n *\n * @public\n */\nexport const AnimatePresence: React.FunctionComponent<\n React.PropsWithChildren<AnimatePresenceProps>\n> = ({\n children,\n enterVariant,\n exitVariant,\n initial = true,\n onExitComplete,\n exitBeforeEnter,\n presenceAffectsLayout = true,\n}) => {\n // We want to force a re-render once all exiting animations have finished. We\n // either use a local forceRender function, or one from a parent context if it exists.\n let forceRender = useForceUpdate()\n const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender\n if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup\n\n const isMounted = useRef(false)\n\n // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key\n const filteredChildren = onlyElements(children)\n let childrenToRender = filteredChildren\n\n const exiting = new Set<ComponentKey>()\n\n // Keep a living record of the children we're actually rendering so we\n // can diff to figure out which are entering and exiting\n const presentChildren = useRef(childrenToRender)\n\n // A lookup table to quickly reference components by key\n const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current\n\n // If this is the initial component render, just deal with logic surrounding whether\n // we play onMount animations or not.\n const isInitialRender = useRef(true)\n\n useEffect(() => {\n isMounted.current = true\n return () => {\n isMounted.current = false\n isInitialRender.current = true\n allChildren.clear()\n exiting.clear()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n isInitialRender.current = false\n updateChildLookup(filteredChildren, allChildren)\n presentChildren.current = childrenToRender\n })\n\n if (isInitialRender.current) {\n return (\n <>\n {childrenToRender.map((child) => (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n initial={initial ? undefined : false}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n ))}\n </>\n )\n }\n\n // If this is a subsequent render, deal with entering and exiting children\n childrenToRender = [...childrenToRender]\n\n // Diff the keys of the currently-present and target children to update our\n // exiting list.\n const presentKeys = presentChildren.current.map(getChildKey)\n const targetKeys = filteredChildren.map(getChildKey)\n\n // Diff the present children with our target children and mark those that are exiting\n const numPresent = presentKeys.length\n for (let i = 0; i < numPresent; i++) {\n const key = presentKeys[i]\n if (targetKeys.indexOf(key) === -1) {\n exiting.add(key)\n }\n }\n\n // If we currently have exiting children, and we're deferring rendering incoming children\n // until after all current children have exiting, empty the childrenToRender array\n if (exitBeforeEnter && exiting.size) {\n childrenToRender = []\n }\n\n // Loop through all currently exiting components and clone them to overwrite `animate`\n // with any `exit` prop they might have defined.\n exiting.forEach((key) => {\n // If this component is actually entering again, early return\n if (targetKeys.indexOf(key) !== -1) return\n\n const child = allChildren.get(key)\n if (!child) return\n\n const insertionIndex = presentKeys.indexOf(key)\n\n childrenToRender.splice(\n insertionIndex,\n 0,\n <PresenceChild\n key={getChildKey(child)}\n isPresent={false}\n onExitComplete={() => {\n allChildren.delete(key)\n exiting.delete(key)\n\n // Remove this child from the present children\n const removeIndex = presentChildren.current.findIndex(\n (presentChild) => presentChild.key === key\n )\n presentChildren.current.splice(removeIndex, 1)\n\n // Defer re-rendering until all exiting children have indeed left\n if (!exiting.size) {\n presentChildren.current = filteredChildren\n\n if (isMounted.current === false) return\n\n forceRender()\n onExitComplete?.()\n }\n }}\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n // Add `MotionContext` even to children that don't need it to ensure we're rendering\n // the same tree between renders\n childrenToRender = childrenToRender.map((child) => {\n const key = child.key as string | number\n return exiting.has(key) ? (\n child\n ) : (\n <PresenceChild\n key={getChildKey(child)}\n isPresent\n exitVariant={exitVariant}\n enterVariant={enterVariant}\n presenceAffectsLayout={presenceAffectsLayout}\n >\n {child}\n </PresenceChild>\n )\n })\n\n if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {\n // eslint-disable-next-line no-console\n console.warn(\n `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`\n )\n }\n\n return (\n <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>\n )\n}\n\nAnimatePresence.displayName = 'AnimatePresence'\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,gBAAgB,iCAAiC;AAC1D,OAAO;AAAA,EACL;AAAA,EAGA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAEP,SAAS,0BAA0B;AACnC,SAAS,qBAAqB;AAG9B,MAAM;AAIN,MAAM,cAAc,CAAC,UAA2C,MAAM,OAAO;AAE7E,MAAM,QAAQ,QAAQ,IAAI,aAAa;AAEvC,SAAS,kBACP,UACA,aACA;AACA,QAAM,eAAe,QAAQ,oBAAI,IAAkB,IAAI;AAEvD,WAAS,QAAQ,CAAC,UAAU;AAC1B,UAAM,MAAM,YAAY,KAAK;AAE7B,QAAI,SAAS,gBAAgB,aAAa,IAAI,GAAG,GAAG;AAElD,cAAQ,KAAK,qDAAqD,sBAAsB;AAExF,mBAAa,IAAI,GAAG;AAAA,IACtB;AAEA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,IA6BH;AAAA,EACF,CAAC;AAID,qBAAmB,iBAAiB,IAAI,CAAC,UAAU;AACjD,UAAM,MAAM,MAAM;AAClB,WAAO,QAAQ,IAAI,GAAG,IACpB,QAEA,CAAC,cACC,KAAK,YAAY,KAAK,GACtB,UACA,aAAa,aACb,cAAc,cACd,uBAAuB,wBAEtB,MACH,EARC;AAAA,EAUL,CAAC;AAED,MAAI,QAAQ,IAAI,aAAa,gBAAgB,mBAAmB,iBAAiB,SAAS,GAAG;AAE3F,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAEA,SACE,GAAG,QAAQ,OAAO,mBAAmB,iBAAiB,IAAI,CAAC,UAAU,aAAa,KAAK,CAAC,EAAE;AAE9F;AAEA,gBAAgB,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PresenceContext } from "@tamagui/use-presence";
|
|
2
2
|
import * as React from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { useId } from "react";
|
|
4
4
|
const PresenceChild = ({
|
|
5
5
|
children,
|
|
6
6
|
initial,
|
|
@@ -10,7 +10,7 @@ const PresenceChild = ({
|
|
|
10
10
|
enterVariant,
|
|
11
11
|
presenceAffectsLayout
|
|
12
12
|
}) => {
|
|
13
|
-
const presenceChildren =
|
|
13
|
+
const presenceChildren = React.useMemo(newChildrenMap, []);
|
|
14
14
|
const id = useId() || "";
|
|
15
15
|
const context = React.useMemo(
|
|
16
16
|
() => {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../../src/PresenceChild.tsx"],
|
|
4
|
-
"sourcesContent": ["import {
|
|
5
|
-
"mappings": "AAAA,SAAS,
|
|
4
|
+
"sourcesContent": ["import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'\nimport * as React from 'react'\nimport { useId } from 'react'\n\nimport { VariantLabels } from './types'\n\ninterface PresenceChildProps {\n children: React.ReactElement<any>\n isPresent: boolean\n onExitComplete?: () => void\n initial?: false | VariantLabels\n custom?: any\n presenceAffectsLayout: boolean\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceChild = ({\n children,\n initial,\n isPresent,\n onExitComplete,\n exitVariant,\n enterVariant,\n presenceAffectsLayout,\n}: PresenceChildProps) => {\n const presenceChildren = React.useMemo(newChildrenMap, [])\n const id = useId() || ''\n\n const context = React.useMemo(\n (): PresenceContextProps => {\n return {\n id,\n initial,\n isPresent,\n exitVariant,\n enterVariant,\n onExitComplete: (id: string) => {\n presenceChildren.set(id, true)\n for (const isComplete of presenceChildren.values()) {\n if (!isComplete) {\n return // can stop searching when any is incomplete\n }\n }\n onExitComplete?.()\n },\n register: (id: string) => {\n presenceChildren.set(id, false)\n return () => presenceChildren.delete(id)\n },\n }\n },\n /**\n * If the presence of a child affects the layout of the components around it,\n * we want to make a new context value to ensure they get re-rendered\n * so they can detect that layout change.\n */\n // eslint-disable-next-line react-hooks/exhaustive-deps\n presenceAffectsLayout ? undefined : [isPresent, exitVariant, enterVariant]\n )\n\n React.useMemo(() => {\n presenceChildren.forEach((_, key) => presenceChildren.set(key, false))\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n /**\n * If there's no animated components to fire exit animations, we want to remove this\n * component immediately.\n */\n React.useEffect(() => {\n !isPresent && !presenceChildren.size && onExitComplete?.()\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isPresent])\n\n return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>\n}\n\nfunction newChildrenMap(): Map<string, boolean> {\n return new Map()\n}\n"],
|
|
5
|
+
"mappings": "AAAA,SAAS,uBAA6C;AACtD,YAAY,WAAW;AACvB,SAAS,aAAa;AAef,MAAM,gBAAgB,CAAC;AAAA,EAC5B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,MAA0B;AACxB,QAAM,mBAAmB,MAAM,QAAQ,gBAAgB,CAAC,CAAC;AACzD,QAAM,KAAK,MAAM,KAAK;AAEtB,QAAM,UAAU,MAAM;AAAA,IACpB,MAA4B;AAC1B,aAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA,gBAAgB,CAACA,QAAe;AAC9B,2BAAiB,IAAIA,KAAI,IAAI;AAC7B,qBAAW,cAAc,iBAAiB,OAAO,GAAG;AAClD,gBAAI,CAAC,YAAY;AACf;AAAA,YACF;AAAA,UACF;AACA,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,IAOA,wBAAwB,SAAY,CAAC,WAAW,aAAa,YAAY;AAAA,EAC3E;AAEA,QAAM,QAAQ,MAAM;AAClB,qBAAiB,QAAQ,CAAC,GAAG,QAAQ,iBAAiB,IAAI,KAAK,KAAK,CAAC;AAAA,EAEvE,GAAG,CAAC,SAAS,CAAC;AAMd,QAAM,UAAU,MAAM;AACpB,KAAC,aAAa,CAAC,iBAAiB,QAAQ,iBAAiB;AAAA,EAE3D,GAAG,CAAC,SAAS,CAAC;AAEd,SAAO,CAAC,gBAAgB,SAAS,OAAO,UAAU,SAAS,EAAnD,gBAAgB;AAC1B;AAEA,SAAS,iBAAuC;AAC9C,SAAO,oBAAI,IAAI;AACjB;",
|
|
6
6
|
"names": ["id"]
|
|
7
7
|
}
|
package/dist/jsx/index.js
CHANGED
package/dist/jsx/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 '
|
|
5
|
-
"mappings": "AAAA,cAAc;AACd,cAAc;
|
|
4
|
+
"sourcesContent": ["export * from './AnimatePresence'\nexport * from '@tamagui/use-presence'\n"],
|
|
5
|
+
"mappings": "AAAA,cAAc;AACd,cAAc;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/animate-presence",
|
|
3
|
-
"version": "1.0.1-beta.
|
|
3
|
+
"version": "1.0.1-beta.194",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"license": "MIT",
|
|
@@ -14,10 +14,11 @@
|
|
|
14
14
|
"dist"
|
|
15
15
|
],
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tamagui/core": "^1.0.1-beta.
|
|
17
|
+
"@tamagui/core": "^1.0.1-beta.194",
|
|
18
|
+
"@tamagui/use-presence": "^1.0.1-beta.194"
|
|
18
19
|
},
|
|
19
20
|
"devDependencies": {
|
|
20
|
-
"@tamagui/build": "^1.0.1-beta.
|
|
21
|
+
"@tamagui/build": "^1.0.1-beta.194"
|
|
21
22
|
},
|
|
22
23
|
"scripts": {
|
|
23
24
|
"build": "tamagui-build",
|
package/src/AnimatePresence.tsx
CHANGED
|
@@ -1,9 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
useForceUpdate,
|
|
3
|
-
useIsMounted,
|
|
4
|
-
useIsomorphicLayoutEffect,
|
|
5
|
-
useUnmountEffect,
|
|
6
|
-
} from '@tamagui/core'
|
|
1
|
+
import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'
|
|
7
2
|
import React, {
|
|
8
3
|
Children,
|
|
9
4
|
ReactElement,
|
|
@@ -11,6 +6,7 @@ import React, {
|
|
|
11
6
|
cloneElement,
|
|
12
7
|
isValidElement,
|
|
13
8
|
useContext,
|
|
9
|
+
useEffect,
|
|
14
10
|
useRef,
|
|
15
11
|
} from 'react'
|
|
16
12
|
|
|
@@ -121,7 +117,7 @@ export const AnimatePresence: React.FunctionComponent<
|
|
|
121
117
|
const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender
|
|
122
118
|
if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup
|
|
123
119
|
|
|
124
|
-
const isMounted =
|
|
120
|
+
const isMounted = useRef(false)
|
|
125
121
|
|
|
126
122
|
// Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key
|
|
127
123
|
const filteredChildren = onlyElements(children)
|
|
@@ -140,18 +136,23 @@ export const AnimatePresence: React.FunctionComponent<
|
|
|
140
136
|
// we play onMount animations or not.
|
|
141
137
|
const isInitialRender = useRef(true)
|
|
142
138
|
|
|
139
|
+
useEffect(() => {
|
|
140
|
+
isMounted.current = true
|
|
141
|
+
return () => {
|
|
142
|
+
isMounted.current = false
|
|
143
|
+
isInitialRender.current = true
|
|
144
|
+
allChildren.clear()
|
|
145
|
+
exiting.clear()
|
|
146
|
+
}
|
|
147
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
148
|
+
}, [])
|
|
149
|
+
|
|
143
150
|
useIsomorphicLayoutEffect(() => {
|
|
144
151
|
isInitialRender.current = false
|
|
145
152
|
updateChildLookup(filteredChildren, allChildren)
|
|
146
153
|
presentChildren.current = childrenToRender
|
|
147
154
|
})
|
|
148
155
|
|
|
149
|
-
useUnmountEffect(() => {
|
|
150
|
-
isInitialRender.current = true
|
|
151
|
-
allChildren.clear()
|
|
152
|
-
exiting.clear()
|
|
153
|
-
})
|
|
154
|
-
|
|
155
156
|
if (isInitialRender.current) {
|
|
156
157
|
return (
|
|
157
158
|
<>
|
package/src/PresenceChild.tsx
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'
|
|
2
2
|
import * as React from 'react'
|
|
3
|
+
import { useId } from 'react'
|
|
3
4
|
|
|
4
|
-
import { PresenceContext, PresenceContextProps } from './PresenceContext'
|
|
5
5
|
import { VariantLabels } from './types'
|
|
6
6
|
|
|
7
7
|
interface PresenceChildProps {
|
|
@@ -24,7 +24,7 @@ export const PresenceChild = ({
|
|
|
24
24
|
enterVariant,
|
|
25
25
|
presenceAffectsLayout,
|
|
26
26
|
}: PresenceChildProps) => {
|
|
27
|
-
const presenceChildren =
|
|
27
|
+
const presenceChildren = React.useMemo(newChildrenMap, [])
|
|
28
28
|
const id = useId() || ''
|
|
29
29
|
|
|
30
30
|
const context = React.useMemo(
|
package/src/index.ts
CHANGED
package/types/index.d.ts
CHANGED
package/src/PresenceContext.tsx
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { createContext } from 'react'
|
|
2
|
-
|
|
3
|
-
import { VariantLabels } from './types'
|
|
4
|
-
|
|
5
|
-
export interface PresenceContextProps {
|
|
6
|
-
id: string
|
|
7
|
-
isPresent: boolean
|
|
8
|
-
register: (id: string) => () => void
|
|
9
|
-
onExitComplete?: (id: string) => void
|
|
10
|
-
initial?: false | VariantLabels
|
|
11
|
-
custom?: any
|
|
12
|
-
exitVariant?: string | null
|
|
13
|
-
enterVariant?: string | null
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const PresenceContext = createContext<PresenceContextProps | null>(null)
|
package/src/usePresence.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import { useId } from '@tamagui/core'
|
|
2
|
-
import { useContext, useEffect } from 'react'
|
|
3
|
-
|
|
4
|
-
import { PresenceContext, PresenceContextProps } from './PresenceContext'
|
|
5
|
-
|
|
6
|
-
export type SafeToRemove = () => void
|
|
7
|
-
type AlwaysPresent = [true, null]
|
|
8
|
-
type Present = [true]
|
|
9
|
-
type NotPresent = [false, SafeToRemove]
|
|
10
|
-
|
|
11
|
-
export function usePresence(): AlwaysPresent | Present | NotPresent {
|
|
12
|
-
const context = useContext(PresenceContext)
|
|
13
|
-
|
|
14
|
-
if (context === null) {
|
|
15
|
-
return [true, null]
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
const { isPresent, onExitComplete, register } = context
|
|
19
|
-
const id = useId() || ''
|
|
20
|
-
useEffect(() => register(id), [id, register])
|
|
21
|
-
|
|
22
|
-
const safeToRemove = () => onExitComplete?.(id)
|
|
23
|
-
|
|
24
|
-
return !isPresent && onExitComplete ? [false, safeToRemove] : [true]
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Similar to `usePresence`, except `useIsPresent` simply returns whether or not the component is present.
|
|
29
|
-
* There is no `safeToRemove` function. ```
|
|
30
|
-
*/
|
|
31
|
-
export function useIsPresent() {
|
|
32
|
-
return isPresent(useContext(PresenceContext))
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function isPresent(context: PresenceContextProps | null) {
|
|
36
|
-
return context === null ? true : context.isPresent
|
|
37
|
-
}
|