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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/dist/cjs/AnimatePresence.js +81 -54
  2. package/dist/cjs/AnimatePresence.js.map +3 -3
  3. package/dist/cjs/LayoutGroupContext.js +1 -0
  4. package/dist/cjs/LayoutGroupContext.js.map +1 -1
  5. package/dist/cjs/PresenceChild.js +43 -38
  6. package/dist/cjs/PresenceChild.js.map +3 -3
  7. package/dist/cjs/{AnimatePresenceContext.js → PresenceContext.js} +8 -7
  8. package/dist/cjs/PresenceContext.js.map +7 -0
  9. package/dist/cjs/index.js +2 -13
  10. package/dist/cjs/index.js.map +2 -2
  11. package/dist/cjs/types.js +1 -0
  12. package/dist/cjs/types.js.map +2 -2
  13. package/dist/cjs/usePresence.js +52 -0
  14. package/dist/cjs/usePresence.js.map +7 -0
  15. package/dist/esm/AnimatePresence.js +79 -57
  16. package/dist/esm/AnimatePresence.js.map +2 -2
  17. package/dist/esm/LayoutGroupContext.js +0 -0
  18. package/dist/esm/LayoutGroupContext.js.map +1 -1
  19. package/dist/esm/PresenceChild.js +38 -38
  20. package/dist/esm/PresenceChild.js.map +3 -3
  21. package/dist/esm/PresenceContext.js +6 -0
  22. package/dist/esm/PresenceContext.js.map +7 -0
  23. package/dist/esm/index.js +1 -5
  24. package/dist/esm/index.js.map +2 -2
  25. package/dist/esm/types.js +0 -0
  26. package/dist/esm/types.js.map +0 -0
  27. package/dist/esm/usePresence.js +26 -0
  28. package/dist/esm/usePresence.js.map +7 -0
  29. package/dist/jsx/AnimatePresence.js +56 -38
  30. package/dist/jsx/AnimatePresence.js.map +7 -0
  31. package/dist/jsx/LayoutGroupContext.js +1 -0
  32. package/dist/jsx/LayoutGroupContext.js.map +7 -0
  33. package/dist/jsx/PresenceChild.js +35 -36
  34. package/dist/jsx/PresenceChild.js.map +7 -0
  35. package/dist/jsx/PresenceContext.js +6 -0
  36. package/dist/jsx/PresenceContext.js.map +7 -0
  37. package/dist/jsx/index.js +2 -5
  38. package/dist/jsx/index.js.map +7 -0
  39. package/dist/jsx/types.js +1 -0
  40. package/dist/jsx/types.js.map +7 -0
  41. package/dist/jsx/usePresence.js +26 -0
  42. package/dist/jsx/usePresence.js.map +7 -0
  43. package/package.json +10 -5
  44. package/src/AnimatePresence.tsx +275 -0
  45. package/src/LayoutGroupContext.tsx +8 -0
  46. package/src/PresenceChild.tsx +81 -0
  47. package/src/index.ts +2 -0
  48. package/src/types.ts +81 -0
  49. package/types/AnimatePresence.d.ts +34 -8
  50. package/types/PresenceChild.d.ts +4 -3
  51. package/types/{AnimatePresenceContext.d.ts → PresenceContext.d.ts} +5 -4
  52. package/types/index.d.ts +1 -2
  53. package/types/types.d.ts +63 -0
  54. package/types/usePresence.d.ts +14 -0
  55. package/dist/cjs/AnimatePresenceContext.js.map +0 -7
  56. package/dist/cjs/useEntering.js +0 -57
  57. package/dist/cjs/useEntering.js.map +0 -7
  58. package/dist/esm/AnimatePresenceContext.js +0 -6
  59. package/dist/esm/AnimatePresenceContext.js.map +0 -7
  60. package/dist/esm/useEntering.js +0 -33
  61. package/dist/esm/useEntering.js.map +0 -7
  62. package/dist/jsx/AnimatePresenceContext.js +0 -5
  63. package/dist/jsx/useEntering.js +0 -32
  64. package/types/AnimatePresence.d.ts.map +0 -1
  65. package/types/AnimatePresenceContext.d.ts.map +0 -1
  66. package/types/LayoutGroupContext.d.ts.map +0 -1
  67. package/types/PresenceChild.d.ts.map +0 -1
  68. package/types/index.d.ts.map +0 -1
  69. package/types/types.d.ts.map +0 -1
  70. package/types/useEntering.d.ts +0 -6
  71. package/types/useEntering.d.ts.map +0 -1
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/AnimatePresence.tsx"],
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,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,KAAK;AACd,YAAI,QAAQ,IAAI,aAAa,eAAe;AAE1C,kBAAQ,KAAK,+DAA+D;AAAA,QAC9E;AACA,iBAAS;AAAA,UACP,MAAM,aAAa,OAAO;AAAA,YACxB,KAAK;AAAA,UACP,CAAC;AAAA,QACH;AAAA,MACF,OAAO;AACL,iBAAS,KAAK,KAAK;AAAA,MACrB;AAAA,IACF;AAAA,EACF,CAAC;AAED,SAAO;AACT;AAmCO,MAAM,kBAET,CAAC;AAAA,EACH;AAAA,EACA;AAAA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createContext({});
3
3
  export {
4
4
  LayoutGroupContext
5
5
  };
6
+ //# sourceMappingURL=LayoutGroupContext.js.map
@@ -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
+ }
@@ -1,55 +1,54 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
3
- import { useConstant, useId } from "@tamagui/core";
1
+ import { PresenceContext } from "@tamagui/use-presence";
4
2
  import * as React from "react";
5
- import { AnimatePresenceContext } from "./AnimatePresenceContext";
6
- const PresenceChild = /* @__PURE__ */ __name(({
3
+ import { useId } from "react";
4
+ const PresenceChild = ({
7
5
  children,
8
6
  initial,
9
- isEntering,
7
+ isPresent,
10
8
  onExitComplete,
11
9
  exitVariant,
12
10
  enterVariant,
13
11
  presenceAffectsLayout
14
12
  }) => {
15
- const presenceChildren = useConstant(newChildrenMap);
16
- const id = useId();
17
- const context = React.useMemo(() => {
18
- return {
19
- id,
20
- initial,
21
- isEntering,
22
- exitVariant,
23
- enterVariant,
24
- onExitComplete: (childId) => {
25
- presenceChildren.set(childId, true);
26
- for (const isComplete of presenceChildren.values()) {
27
- if (!isComplete) {
28
- return;
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
29
  }
30
+ onExitComplete?.();
31
+ },
32
+ register: (id2) => {
33
+ presenceChildren.set(id2, false);
34
+ return () => presenceChildren.delete(id2);
30
35
  }
31
- onExitComplete == null ? void 0 : onExitComplete();
32
- },
33
- register: (childId) => {
34
- presenceChildren.set(childId, false);
35
- return () => presenceChildren.delete(childId);
36
- }
37
- };
38
- }, presenceAffectsLayout ? void 0 : [isEntering, onExitComplete, exitVariant, enterVariant]);
36
+ };
37
+ },
38
+ presenceAffectsLayout ? void 0 : [isPresent, exitVariant, enterVariant]
39
+ );
39
40
  React.useMemo(() => {
40
41
  presenceChildren.forEach((_, key) => presenceChildren.set(key, false));
41
- }, [isEntering]);
42
+ }, [isPresent]);
42
43
  React.useEffect(() => {
43
- if (isEntering === false && !presenceChildren.size) {
44
- onExitComplete == null ? void 0 : onExitComplete();
45
- }
46
- }, [isEntering]);
47
- return <AnimatePresenceContext.Provider value={context}>{children}</AnimatePresenceContext.Provider>;
48
- }, "PresenceChild");
44
+ !isPresent && !presenceChildren.size && onExitComplete?.();
45
+ }, [isPresent]);
46
+ return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>;
47
+ };
49
48
  function newChildrenMap() {
50
49
  return /* @__PURE__ */ new Map();
51
50
  }
52
- __name(newChildrenMap, "newChildrenMap");
53
51
  export {
54
52
  PresenceChild
55
53
  };
54
+ //# sourceMappingURL=PresenceChild.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/PresenceChild.tsx"],
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
+ "names": ["id"]
7
+ }
@@ -0,0 +1,6 @@
1
+ import { createContext } from "react";
2
+ const PresenceContext = createContext(null);
3
+ export {
4
+ PresenceContext
5
+ };
6
+ //# sourceMappingURL=PresenceContext.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/PresenceContext.tsx"],
4
+ "sourcesContent": ["import { createContext } from 'react'\n\nimport { VariantLabels } from './types'\n\nexport interface PresenceContextProps {\n id: string\n isPresent: boolean\n register: (id: string) => () => void\n onExitComplete?: (id: string) => void\n initial?: false | VariantLabels\n custom?: any\n exitVariant?: string | null\n enterVariant?: string | null\n}\n\nexport const PresenceContext = createContext<PresenceContextProps | null>(null)\n"],
5
+ "mappings": "AAAA,SAAS,qBAAqB;AAevB,MAAM,kBAAkB,cAA2C,IAAI;",
6
+ "names": []
7
+ }
package/dist/jsx/index.js CHANGED
@@ -1,6 +1,3 @@
1
1
  export * from "./AnimatePresence";
2
- export * from "./useEntering";
3
- import { AnimatePresenceContext } from "./AnimatePresenceContext";
4
- export {
5
- AnimatePresenceContext
6
- };
2
+ export * from "@tamagui/use-presence";
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/index.ts"],
4
+ "sourcesContent": ["export * from './AnimatePresence'\nexport * from '@tamagui/use-presence'\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;",
6
+ "names": []
7
+ }
package/dist/jsx/types.js CHANGED
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
@@ -0,0 +1,26 @@
1
+ import { useId } from "@tamagui/core";
2
+ import { useContext, useEffect } from "react";
3
+ import { PresenceContext } from "./PresenceContext";
4
+ function usePresence() {
5
+ const context = useContext(PresenceContext);
6
+ if (context === null) {
7
+ return [true, null];
8
+ }
9
+ const { isPresent: isPresent2, onExitComplete, register } = context;
10
+ const id = useId() || "";
11
+ useEffect(() => register(id), [id, register]);
12
+ const safeToRemove = () => onExitComplete?.(id);
13
+ return !isPresent2 && onExitComplete ? [false, safeToRemove] : [true];
14
+ }
15
+ function useIsPresent() {
16
+ return isPresent(useContext(PresenceContext));
17
+ }
18
+ function isPresent(context) {
19
+ return context === null ? true : context.isPresent;
20
+ }
21
+ export {
22
+ isPresent,
23
+ useIsPresent,
24
+ usePresence
25
+ };
26
+ //# sourceMappingURL=usePresence.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/usePresence.ts"],
4
+ "sourcesContent": ["import { useId } from '@tamagui/core'\nimport { useContext, useEffect } from 'react'\n\nimport { PresenceContext, PresenceContextProps } from './PresenceContext'\n\nexport type SafeToRemove = () => void\ntype AlwaysPresent = [true, null]\ntype Present = [true]\ntype NotPresent = [false, SafeToRemove]\n\nexport function usePresence(): AlwaysPresent | Present | NotPresent {\n const context = useContext(PresenceContext)\n\n if (context === null) {\n return [true, null]\n }\n\n const { isPresent, onExitComplete, register } = context\n const id = useId() || ''\n useEffect(() => register(id), [id, register])\n\n const safeToRemove = () => onExitComplete?.(id)\n\n return !isPresent && onExitComplete ? [false, safeToRemove] : [true]\n}\n\n/**\n * Similar to `usePresence`, except `useIsPresent` simply returns whether or not the component is present.\n * There is no `safeToRemove` function. ```\n */\nexport function useIsPresent() {\n return isPresent(useContext(PresenceContext))\n}\n\nexport function isPresent(context: PresenceContextProps | null) {\n return context === null ? true : context.isPresent\n}\n"],
5
+ "mappings": "AAAA,SAAS,aAAa;AACtB,SAAS,YAAY,iBAAiB;AAEtC,SAAS,uBAA6C;AAO/C,SAAS,cAAoD;AAClE,QAAM,UAAU,WAAW,eAAe;AAE1C,MAAI,YAAY,MAAM;AACpB,WAAO,CAAC,MAAM,IAAI;AAAA,EACpB;AAEA,QAAM,EAAE,WAAAA,YAAW,gBAAgB,SAAS,IAAI;AAChD,QAAM,KAAK,MAAM,KAAK;AACtB,YAAU,MAAM,SAAS,EAAE,GAAG,CAAC,IAAI,QAAQ,CAAC;AAE5C,QAAM,eAAe,MAAM,iBAAiB,EAAE;AAE9C,SAAO,CAACA,cAAa,iBAAiB,CAAC,OAAO,YAAY,IAAI,CAAC,IAAI;AACrE;AAMO,SAAS,eAAe;AAC7B,SAAO,UAAU,WAAW,eAAe,CAAC;AAC9C;AAEO,SAAS,UAAU,SAAsC;AAC9D,SAAO,YAAY,OAAO,OAAO,QAAQ;AAC3C;",
6
+ "names": ["isPresent"]
7
+ }
package/package.json CHANGED
@@ -1,27 +1,32 @@
1
1
  {
2
2
  "name": "@tamagui/animate-presence",
3
- "version": "1.0.1-beta.21",
3
+ "version": "1.0.1-beta.211",
4
4
  "source": "src/index.ts",
5
+ "sideEffects": false,
5
6
  "license": "MIT",
6
- "typings": "types",
7
+ "types": "./types/index.d.ts",
7
8
  "main": "dist/cjs",
8
9
  "module": "dist/esm",
9
10
  "module:jsx": "dist/jsx",
10
11
  "files": [
12
+ "src",
11
13
  "types",
12
14
  "dist"
13
15
  ],
14
16
  "dependencies": {
15
- "@tamagui/core": "^1.0.1-beta.21"
17
+ "@tamagui/core": "^1.0.1-beta.211",
18
+ "@tamagui/use-presence": "^1.0.1-beta.211"
16
19
  },
17
20
  "devDependencies": {
18
- "@tamagui/build": "^1.0.1-beta.21"
21
+ "@tamagui/build": "^1.0.1-beta.211"
19
22
  },
20
23
  "scripts": {
21
24
  "build": "tamagui-build",
22
25
  "watch": "tamagui-build --watch",
23
26
  "clean": "tamagui-build clean",
24
- "clean:build": "tamagui-build clean:build"
27
+ "clean:build": "tamagui-build clean:build",
28
+ "lint": "eslint",
29
+ "lint:fix": "yarn lint --fix src"
25
30
  },
26
31
  "publishConfig": {
27
32
  "access": "public"
@@ -0,0 +1,275 @@
1
+ import { useForceUpdate, useIsomorphicLayoutEffect } from '@tamagui/core'
2
+ import React, {
3
+ Children,
4
+ ReactElement,
5
+ ReactNode,
6
+ cloneElement,
7
+ isValidElement,
8
+ useContext,
9
+ useEffect,
10
+ useRef,
11
+ } from 'react'
12
+
13
+ import { LayoutGroupContext } from './LayoutGroupContext'
14
+ import { PresenceChild } from './PresenceChild'
15
+ import { AnimatePresenceProps } from './types'
16
+
17
+ React['keep']
18
+
19
+ type ComponentKey = string | number
20
+
21
+ const getChildKey = (child: ReactElement<any>): ComponentKey => child.key || ''
22
+
23
+ const isDev = process.env.NODE_ENV !== 'production'
24
+
25
+ function updateChildLookup(
26
+ children: ReactElement<any>[],
27
+ allChildren: Map<ComponentKey, ReactElement<any>>
28
+ ) {
29
+ const seenChildren = isDev ? new Set<ComponentKey>() : null
30
+
31
+ children.forEach((child) => {
32
+ const key = getChildKey(child)
33
+
34
+ if (isDev && seenChildren && seenChildren.has(key)) {
35
+ // eslint-disable-next-line no-console
36
+ console.warn(`Children of AnimatePresence require unique keys. "${key}" is a duplicate.`)
37
+
38
+ seenChildren.add(key)
39
+ }
40
+
41
+ allChildren.set(key, child)
42
+ })
43
+ }
44
+
45
+ function onlyElements(children: ReactNode): ReactElement<any>[] {
46
+ const filtered: ReactElement<any>[] = []
47
+
48
+ // We use forEach here instead of map as map mutates the component key by preprending `.$`
49
+ Children.forEach(children, (child, index) => {
50
+ if (isValidElement(child)) {
51
+ if (!child.key) {
52
+ if (process.env.NODE_ENV === 'development') {
53
+ // eslint-disable-next-line no-console
54
+ console.warn(`No key given to AnimatePresence child, assigning index as key`)
55
+ }
56
+ filtered.push(
57
+ React.cloneElement(child, {
58
+ key: index,
59
+ })
60
+ )
61
+ } else {
62
+ filtered.push(child)
63
+ }
64
+ }
65
+ })
66
+
67
+ return filtered
68
+ }
69
+
70
+ /**
71
+ * `AnimatePresence` enables the animation of components that have been removed from the tree.
72
+ *
73
+ * When adding/removing more than a single child, every child **must** be given a unique `key` prop.
74
+ *
75
+ * Any `motion` components that have an `exit` property defined will animate out when removed from
76
+ * the tree.
77
+ *
78
+ * ```tsx
79
+ * import { motion, AnimatePresence } from 'framer-motion'
80
+ *
81
+ * export const Items = ({ items }) => (
82
+ * <AnimatePresence>
83
+ * {items.map(item => (
84
+ * <motion.div
85
+ * key={item.id}
86
+ * initial={{ opacity: 0 }}
87
+ * animate={{ opacity: 1 }}
88
+ * exit={{ opacity: 0 }}
89
+ * />
90
+ * ))}
91
+ * </AnimatePresence>
92
+ * )
93
+ * ```
94
+ *
95
+ * You can sequence exit animations throughout a tree using variants.
96
+ *
97
+ * If a child contains multiple `motion` components with `exit` props, it will only unmount the child
98
+ * once all `motion` components have finished animating out. Likewise, any components using
99
+ * `usePresence` all need to call `safeToRemove`.
100
+ *
101
+ * @public
102
+ */
103
+ export const AnimatePresence: React.FunctionComponent<
104
+ React.PropsWithChildren<AnimatePresenceProps>
105
+ > = ({
106
+ children,
107
+ enterVariant,
108
+ exitVariant,
109
+ initial = true,
110
+ onExitComplete,
111
+ exitBeforeEnter,
112
+ presenceAffectsLayout = true,
113
+ }) => {
114
+ // We want to force a re-render once all exiting animations have finished. We
115
+ // either use a local forceRender function, or one from a parent context if it exists.
116
+ let forceRender = useForceUpdate()
117
+ const forceRenderLayoutGroup = useContext(LayoutGroupContext).forceRender
118
+ if (forceRenderLayoutGroup) forceRender = forceRenderLayoutGroup
119
+
120
+ const isMounted = useRef(false)
121
+
122
+ // Filter out any children that aren't ReactElements. We can only track ReactElements with a props.key
123
+ const filteredChildren = onlyElements(children)
124
+ let childrenToRender = filteredChildren
125
+
126
+ const exiting = new Set<ComponentKey>()
127
+
128
+ // Keep a living record of the children we're actually rendering so we
129
+ // can diff to figure out which are entering and exiting
130
+ const presentChildren = useRef(childrenToRender)
131
+
132
+ // A lookup table to quickly reference components by key
133
+ const allChildren = useRef(new Map<ComponentKey, ReactElement<any>>()).current
134
+
135
+ // If this is the initial component render, just deal with logic surrounding whether
136
+ // we play onMount animations or not.
137
+ const isInitialRender = useRef(true)
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
+
150
+ useIsomorphicLayoutEffect(() => {
151
+ isInitialRender.current = false
152
+ updateChildLookup(filteredChildren, allChildren)
153
+ presentChildren.current = childrenToRender
154
+ })
155
+
156
+ if (isInitialRender.current) {
157
+ return (
158
+ <>
159
+ {childrenToRender.map((child) => (
160
+ <PresenceChild
161
+ key={getChildKey(child)}
162
+ isPresent
163
+ exitVariant={exitVariant}
164
+ enterVariant={enterVariant}
165
+ initial={initial ? undefined : false}
166
+ presenceAffectsLayout={presenceAffectsLayout}
167
+ >
168
+ {child}
169
+ </PresenceChild>
170
+ ))}
171
+ </>
172
+ )
173
+ }
174
+
175
+ // If this is a subsequent render, deal with entering and exiting children
176
+ childrenToRender = [...childrenToRender]
177
+
178
+ // Diff the keys of the currently-present and target children to update our
179
+ // exiting list.
180
+ const presentKeys = presentChildren.current.map(getChildKey)
181
+ const targetKeys = filteredChildren.map(getChildKey)
182
+
183
+ // Diff the present children with our target children and mark those that are exiting
184
+ const numPresent = presentKeys.length
185
+ for (let i = 0; i < numPresent; i++) {
186
+ const key = presentKeys[i]
187
+ if (targetKeys.indexOf(key) === -1) {
188
+ exiting.add(key)
189
+ }
190
+ }
191
+
192
+ // If we currently have exiting children, and we're deferring rendering incoming children
193
+ // until after all current children have exiting, empty the childrenToRender array
194
+ if (exitBeforeEnter && exiting.size) {
195
+ childrenToRender = []
196
+ }
197
+
198
+ // Loop through all currently exiting components and clone them to overwrite `animate`
199
+ // with any `exit` prop they might have defined.
200
+ exiting.forEach((key) => {
201
+ // If this component is actually entering again, early return
202
+ if (targetKeys.indexOf(key) !== -1) return
203
+
204
+ const child = allChildren.get(key)
205
+ if (!child) return
206
+
207
+ const insertionIndex = presentKeys.indexOf(key)
208
+
209
+ childrenToRender.splice(
210
+ insertionIndex,
211
+ 0,
212
+ <PresenceChild
213
+ key={getChildKey(child)}
214
+ isPresent={false}
215
+ onExitComplete={() => {
216
+ allChildren.delete(key)
217
+ exiting.delete(key)
218
+
219
+ // Remove this child from the present children
220
+ const removeIndex = presentChildren.current.findIndex(
221
+ (presentChild) => presentChild.key === key
222
+ )
223
+ presentChildren.current.splice(removeIndex, 1)
224
+
225
+ // Defer re-rendering until all exiting children have indeed left
226
+ if (!exiting.size) {
227
+ presentChildren.current = filteredChildren
228
+
229
+ if (isMounted.current === false) return
230
+
231
+ forceRender()
232
+ onExitComplete?.()
233
+ }
234
+ }}
235
+ exitVariant={exitVariant}
236
+ enterVariant={enterVariant}
237
+ presenceAffectsLayout={presenceAffectsLayout}
238
+ >
239
+ {child}
240
+ </PresenceChild>
241
+ )
242
+ })
243
+
244
+ // Add `MotionContext` even to children that don't need it to ensure we're rendering
245
+ // the same tree between renders
246
+ childrenToRender = childrenToRender.map((child) => {
247
+ const key = child.key as string | number
248
+ return exiting.has(key) ? (
249
+ child
250
+ ) : (
251
+ <PresenceChild
252
+ key={getChildKey(child)}
253
+ isPresent
254
+ exitVariant={exitVariant}
255
+ enterVariant={enterVariant}
256
+ presenceAffectsLayout={presenceAffectsLayout}
257
+ >
258
+ {child}
259
+ </PresenceChild>
260
+ )
261
+ })
262
+
263
+ if (process.env.NODE_ENV !== 'production' && exitBeforeEnter && childrenToRender.length > 1) {
264
+ // eslint-disable-next-line no-console
265
+ console.warn(
266
+ `You're attempting to animate multiple children within AnimatePresence, but its exitBeforeEnter prop is set to true. This will lead to odd visual behaviour.`
267
+ )
268
+ }
269
+
270
+ return (
271
+ <>{exiting.size ? childrenToRender : childrenToRender.map((child) => cloneElement(child))}</>
272
+ )
273
+ }
274
+
275
+ AnimatePresence.displayName = 'AnimatePresence'
@@ -0,0 +1,8 @@
1
+ import { createContext } from 'react'
2
+
3
+ export interface LayoutGroupContextProps {
4
+ id?: string
5
+ forceRender?: VoidFunction
6
+ }
7
+
8
+ export const LayoutGroupContext = createContext<LayoutGroupContextProps>({})
@@ -0,0 +1,81 @@
1
+ import { PresenceContext, PresenceContextProps } from '@tamagui/use-presence'
2
+ import * as React from 'react'
3
+ import { useId } from 'react'
4
+
5
+ import { VariantLabels } from './types'
6
+
7
+ interface PresenceChildProps {
8
+ children: React.ReactElement<any>
9
+ isPresent: boolean
10
+ onExitComplete?: () => void
11
+ initial?: false | VariantLabels
12
+ custom?: any
13
+ presenceAffectsLayout: boolean
14
+ exitVariant?: string | null
15
+ enterVariant?: string | null
16
+ }
17
+
18
+ export const PresenceChild = ({
19
+ children,
20
+ initial,
21
+ isPresent,
22
+ onExitComplete,
23
+ exitVariant,
24
+ enterVariant,
25
+ presenceAffectsLayout,
26
+ }: PresenceChildProps) => {
27
+ const presenceChildren = React.useMemo(newChildrenMap, [])
28
+ const id = useId() || ''
29
+
30
+ const context = React.useMemo(
31
+ (): PresenceContextProps => {
32
+ return {
33
+ id,
34
+ initial,
35
+ isPresent,
36
+ exitVariant,
37
+ enterVariant,
38
+ onExitComplete: (id: string) => {
39
+ presenceChildren.set(id, true)
40
+ for (const isComplete of presenceChildren.values()) {
41
+ if (!isComplete) {
42
+ return // can stop searching when any is incomplete
43
+ }
44
+ }
45
+ onExitComplete?.()
46
+ },
47
+ register: (id: string) => {
48
+ presenceChildren.set(id, false)
49
+ return () => presenceChildren.delete(id)
50
+ },
51
+ }
52
+ },
53
+ /**
54
+ * If the presence of a child affects the layout of the components around it,
55
+ * we want to make a new context value to ensure they get re-rendered
56
+ * so they can detect that layout change.
57
+ */
58
+ // eslint-disable-next-line react-hooks/exhaustive-deps
59
+ presenceAffectsLayout ? undefined : [isPresent, exitVariant, enterVariant]
60
+ )
61
+
62
+ React.useMemo(() => {
63
+ presenceChildren.forEach((_, key) => presenceChildren.set(key, false))
64
+ // eslint-disable-next-line react-hooks/exhaustive-deps
65
+ }, [isPresent])
66
+
67
+ /**
68
+ * If there's no animated components to fire exit animations, we want to remove this
69
+ * component immediately.
70
+ */
71
+ React.useEffect(() => {
72
+ !isPresent && !presenceChildren.size && onExitComplete?.()
73
+ // eslint-disable-next-line react-hooks/exhaustive-deps
74
+ }, [isPresent])
75
+
76
+ return <PresenceContext.Provider value={context}>{children}</PresenceContext.Provider>
77
+ }
78
+
79
+ function newChildrenMap(): Map<string, boolean> {
80
+ return new Map()
81
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './AnimatePresence'
2
+ export * from '@tamagui/use-presence'