@radix-ui/react-presence 0.1.2 → 0.1.3-rc.11

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/index.d.ts CHANGED
@@ -1,9 +1,9 @@
1
1
  import * as React from "react";
2
2
  export interface PresenceProps {
3
- present: boolean;
4
3
  children: React.ReactElement | ((props: {
5
4
  present: boolean;
6
5
  }) => React.ReactElement);
6
+ present: boolean;
7
7
  }
8
8
  export const Presence: React.FC<PresenceProps>;
9
9
 
@@ -1 +1 @@
1
- {"mappings":"A;ACKA;IACE,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,KAAK,MAAM,YAAY,CAAC,CAAC;CACtF;AAED,OAAA,MAAM,UAAU,MAAM,EAAE,CAAC,aAAa,CAarC,CAAC","sources":["./packages/react/presence/src/packages/react/presence/src/useStateMachine.tsx","./packages/react/presence/src/packages/react/presence/src/Presence.tsx","./packages/react/presence/src/packages/react/presence/src/index.ts"],"sourcesContent":[null,null,null],"names":[],"version":3,"file":"index.d.ts.map"}
1
+ {"mappings":";ACMA;IACE,QAAQ,EAAE,MAAM,YAAY,GAAG,CAAC,CAAC,KAAK,EAAE;QAAE,OAAO,EAAE,OAAO,CAAA;KAAE,KAAK,MAAM,YAAY,CAAC,CAAC;IACrF,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,OAAA,MAAM,UAAU,MAAM,EAAE,CAAC,aAAa,CAarC,CAAC","sources":["packages/react/presence/src/packages/react/presence/src/useStateMachine.tsx","packages/react/presence/src/packages/react/presence/src/Presence.tsx","packages/react/presence/src/packages/react/presence/src/index.ts","packages/react/presence/src/index.ts"],"sourcesContent":[null,null,null,"export * from './Presence';\n"],"names":[],"version":3,"file":"index.d.ts.map"}
package/dist/index.js CHANGED
@@ -1,2 +1,161 @@
1
- var e,n,t=require("@radix-ui/react-use-layout-effect").useLayoutEffect,r=require("@radix-ui/react-compose-refs").useComposedRefs,u=(e={},n=require("react"),Object.keys(n).forEach((function(t){"default"!==t&&"__esModule"!==t&&Object.defineProperty(e,t,{enumerable:!0,get:function(){return n[t]}})})),e);const o=e=>{const{present:n,children:o}=e,i=function(e){const[n,r]=u.useState(),o=u.useRef({}),i=u.useRef(e),c=u.useRef("none"),a=e?"mounted":"unmounted",[d,m]=function(e,n){return u.useReducer(((e,t)=>{const r=n[e][t];return null!=r?r:e}),e)}(a,{mounted:{UNMOUNT:"unmounted",ANIMATION_OUT:"unmountSuspended"},unmountSuspended:{MOUNT:"mounted",ANIMATION_END:"unmounted"},unmounted:{MOUNT:"mounted"}});return u.useEffect((()=>{const e=s(o.current);c.current="mounted"===d?e:"none"}),[d]),t((()=>{const n=o.current,t=i.current;if(t!==e){const r=c.current,u=s(n);if(e)m("MOUNT");else if("none"===u||"none"===(null==n?void 0:n.display))m("UNMOUNT");else{const e=r!==u;m(t&&e?"ANIMATION_OUT":"UNMOUNT")}i.current=e}}),[e,m]),t((()=>{if(n){const e=e=>{const t=s(o.current).includes(e.animationName);e.target===n&&t&&m("ANIMATION_END")},t=e=>{e.target===n&&(c.current=s(o.current))};return n.addEventListener("animationstart",t),n.addEventListener("animationcancel",e),n.addEventListener("animationend",e),()=>{n.removeEventListener("animationstart",t),n.removeEventListener("animationcancel",e),n.removeEventListener("animationend",e)}}m("ANIMATION_END")}),[n,m]),{isPresent:["mounted","unmountSuspended"].includes(d),ref:u.useCallback((e=>{e&&(o.current=getComputedStyle(e)),r(e)}),[])}}(n),c="function"==typeof o?o({present:i.isPresent}):u.Children.only(o),a=r(i.ref,c.ref);return"function"==typeof o||i.isPresent?/*#__PURE__*/u.cloneElement(c,{ref:a}):null};function s(e){return(null==e?void 0:e.animationName)||"none"}exports.Presence=o,o.displayName="Presence";
1
+ var $fnLeV$react = require("react");
2
+ var $fnLeV$reactdom = require("react-dom");
3
+ var $fnLeV$radixuireactcomposerefs = require("@radix-ui/react-compose-refs");
4
+ var $fnLeV$radixuireactuselayouteffect = require("@radix-ui/react-use-layout-effect");
5
+
6
+ function $parcel$exportWildcard(dest, source) {
7
+ Object.keys(source).forEach(function(key) {
8
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
9
+ return;
10
+ }
11
+
12
+ Object.defineProperty(dest, key, {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return source[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ return dest;
21
+ }
22
+ function $parcel$export(e, n, v, s) {
23
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
24
+ }
25
+ var $a2fa0214bb2735a1$exports = {};
26
+
27
+ $parcel$export($a2fa0214bb2735a1$exports, "Presence", () => $a2fa0214bb2735a1$export$99c2b779aa4e8b8b);
28
+
29
+
30
+
31
+
32
+
33
+ function $8f63844556d0d3cd$export$3e6543de14f8614f(initialState, machine) {
34
+ return $fnLeV$react.useReducer((state, event)=>{
35
+ const nextState = machine[state][event];
36
+ return nextState !== null && nextState !== void 0 ? nextState : state;
37
+ }, initialState);
38
+ }
39
+
40
+
41
+ const $a2fa0214bb2735a1$export$99c2b779aa4e8b8b = (props)=>{
42
+ const { present: present , children: children } = props;
43
+ const presence = $a2fa0214bb2735a1$var$usePresence(present);
44
+ const child = typeof children === 'function' ? children({
45
+ present: presence.isPresent
46
+ }) : $fnLeV$react.Children.only(children);
47
+ const ref = $fnLeV$radixuireactcomposerefs.useComposedRefs(presence.ref, child.ref);
48
+ const forceMount = typeof children === 'function';
49
+ return forceMount || presence.isPresent ? /*#__PURE__*/ $fnLeV$react.cloneElement(child, {
50
+ ref: ref
51
+ }) : null;
52
+ };
53
+ $a2fa0214bb2735a1$export$99c2b779aa4e8b8b.displayName = 'Presence';
54
+ /* -------------------------------------------------------------------------------------------------
55
+ * usePresence
56
+ * -----------------------------------------------------------------------------------------------*/ function $a2fa0214bb2735a1$var$usePresence(present) {
57
+ const [node1, setNode] = $fnLeV$react.useState();
58
+ const stylesRef = $fnLeV$react.useRef({});
59
+ const prevPresentRef = $fnLeV$react.useRef(present);
60
+ const prevAnimationNameRef = $fnLeV$react.useRef('none');
61
+ const initialState = present ? 'mounted' : 'unmounted';
62
+ const [state, send] = $8f63844556d0d3cd$export$3e6543de14f8614f(initialState, {
63
+ mounted: {
64
+ UNMOUNT: 'unmounted',
65
+ ANIMATION_OUT: 'unmountSuspended'
66
+ },
67
+ unmountSuspended: {
68
+ MOUNT: 'mounted',
69
+ ANIMATION_END: 'unmounted'
70
+ },
71
+ unmounted: {
72
+ MOUNT: 'mounted'
73
+ }
74
+ });
75
+ $fnLeV$react.useEffect(()=>{
76
+ const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
77
+ prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';
78
+ }, [
79
+ state
80
+ ]);
81
+ $fnLeV$radixuireactuselayouteffect.useLayoutEffect(()=>{
82
+ const styles = stylesRef.current;
83
+ const wasPresent = prevPresentRef.current;
84
+ const hasPresentChanged = wasPresent !== present;
85
+ if (hasPresentChanged) {
86
+ const prevAnimationName = prevAnimationNameRef.current;
87
+ const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(styles);
88
+ if (present) send('MOUNT');
89
+ else if (currentAnimationName === 'none' || (styles === null || styles === void 0 ? void 0 : styles.display) === 'none') // If there is no exit animation or the element is hidden, animations won't run
90
+ // so we unmount instantly
91
+ send('UNMOUNT');
92
+ else {
93
+ /**
94
+ * When `present` changes to `false`, we check changes to animation-name to
95
+ * determine whether an animation has started. We chose this approach (reading
96
+ * computed styles) because there is no `animationrun` event and `animationstart`
97
+ * fires after `animation-delay` has expired which would be too late.
98
+ */ const isAnimating = prevAnimationName !== currentAnimationName;
99
+ if (wasPresent && isAnimating) send('ANIMATION_OUT');
100
+ else send('UNMOUNT');
101
+ }
102
+ prevPresentRef.current = present;
103
+ }
104
+ }, [
105
+ present,
106
+ send
107
+ ]);
108
+ $fnLeV$radixuireactuselayouteffect.useLayoutEffect(()=>{
109
+ if (node1) {
110
+ /**
111
+ * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
112
+ * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
113
+ * make sure we only trigger ANIMATION_END for the currently active animation.
114
+ */ const handleAnimationEnd = (event)=>{
115
+ const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
116
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
117
+ if (event.target === node1 && isCurrentAnimation) // With React 18 concurrency this update is applied
118
+ // a frame after the animation ends, creating a flash of visible content.
119
+ // By manually flushing we ensure they sync within a frame, removing the flash.
120
+ $fnLeV$reactdom.flushSync(()=>send('ANIMATION_END')
121
+ );
122
+ };
123
+ const handleAnimationStart = (event)=>{
124
+ if (event.target === node1) // if animation occurred, store its name as the previous animation.
125
+ prevAnimationNameRef.current = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
126
+ };
127
+ node1.addEventListener('animationstart', handleAnimationStart);
128
+ node1.addEventListener('animationcancel', handleAnimationEnd);
129
+ node1.addEventListener('animationend', handleAnimationEnd);
130
+ return ()=>{
131
+ node1.removeEventListener('animationstart', handleAnimationStart);
132
+ node1.removeEventListener('animationcancel', handleAnimationEnd);
133
+ node1.removeEventListener('animationend', handleAnimationEnd);
134
+ };
135
+ } else // Transition to the unmounted state if the node is removed prematurely.
136
+ // We avoid doing so during cleanup as the node may change but still exist.
137
+ send('ANIMATION_END');
138
+ }, [
139
+ node1,
140
+ send
141
+ ]);
142
+ return {
143
+ isPresent: [
144
+ 'mounted',
145
+ 'unmountSuspended'
146
+ ].includes(state),
147
+ ref: $fnLeV$react.useCallback((node)=>{
148
+ if (node) stylesRef.current = getComputedStyle(node);
149
+ setNode(node);
150
+ }, [])
151
+ };
152
+ }
153
+ /* -----------------------------------------------------------------------------------------------*/ function $a2fa0214bb2735a1$var$getAnimationName(styles) {
154
+ return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
155
+ }
156
+
157
+
158
+ $parcel$exportWildcard(module.exports, $a2fa0214bb2735a1$exports);
159
+
160
+
2
161
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"mappings":"IAYgCA,EAAMC,8HAAND,KAAMC,mBACpCC,OAAOC,KAAKF,GAAQG,SAAQ,SAASC,GACvB,YAARA,GAA6B,eAARA,GAIzBH,OAAOI,eAAeN,EAAMK,EAAK,CAC/BE,YAAY,EACZC,IAAK,WACH,OAAOP,EAAOI,SAKbL,GChBT,MAAMS,EAAqCC,IACzC,MAAMC,QAAEA,EAAFC,SAAWA,GAAaF,EACxBG,EAmBR,SAAqBF,GACnB,MAAOG,EAAMC,GAAWC,EAAMC,WACxBC,EAAYF,EAAMG,OAA4B,IAC9CC,EAAiBJ,EAAMG,OAAOR,GAC9BU,EAAuBL,EAAMG,OAAe,QAC5CG,EAAeX,EAAU,UAAY,aACpCY,EAAOC,GC1BT,SACLF,EACAG,GAEA,OAAOT,EAAMU,YAAW,CAACH,EAAwBI,KAC/C,MAAMC,EAAaH,EAAQF,GAAeI,GAC1C,OAAOC,MAAAA,EAAAA,EAAaL,IACnBD,GDmBmBO,CAAgBP,EAAc,CAClDQ,QAAS,CACPC,QAAS,YACTC,cAAe,oBAEjBC,iBAAkB,CAChBC,MAAO,UACPC,cAAe,aAEjBC,UAAW,CACTF,MAAO,aA+EX,OA3EAlB,EAAMqB,WAAU,KACd,MAAMC,EAAuBC,EAAiBrB,EAAUsB,SACxDnB,EAAqBmB,QAAoB,YAAVjB,EAAsBe,EAAuB,SAC3E,CAACf,IAEJkB,GAAgB,KACd,MAAMC,EAASxB,EAAUsB,QACnBG,EAAavB,EAAeoB,QAGlC,GAF0BG,IAAehC,EAElB,CACrB,MAAMiC,EAAoBvB,EAAqBmB,QACzCF,EAAuBC,EAAiBG,GAE9C,GAAI/B,EACFa,EAAK,cACA,GAA6B,SAAzBc,GAAuD,UAApBI,MAAA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/presence/src/useStateMachine.tsx"],"sourcesContent":["function $parcel$interopDefault(a) {\n return a && a.__esModule ? a.default : a;\n}\n\nfunction $parcel$defineInteropFlag(a) {\n Object.defineProperty(a, '__esModule', {value: true});\n}\n\nfunction $parcel$reexport(e, n, v) {\n Object.defineProperty(e, n, {get: v, enumerable: true});\n}\n\nfunction $parcel$exportWildcard(dest, source) {\n Object.keys(source).forEach(function(key) {\n if (key === 'default' || key === '__esModule') {\n return;\n }\n\n Object.defineProperty(dest, key, {\n enumerable: true,\n get: function get() {\n return source[key];\n },\n });\n });\n\n return dest;\n}\n\nfunction $parcel$missingModule(name) {\n var err = new Error(\"Cannot find module '\" + name + \"'\");\n err.code = 'MODULE_NOT_FOUND';\n throw err;\n}\n\nvar $parcel$global =\n typeof globalThis !== 'undefined'\n ? globalThis\n : typeof self !== 'undefined'\n ? self\n : typeof window !== 'undefined'\n ? window\n : typeof global !== 'undefined'\n ? global\n : {};\n","import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { useStateMachine } from './useStateMachine';\n\ninterface PresenceProps {\n present: boolean;\n children: React.ReactElement | ((props: { present: boolean }) => React.ReactElement);\n}\n\nconst Presence: React.FC<PresenceProps> = (props) => {\n const { present, children } = props;\n const presence = usePresence(present);\n\n const child = (\n typeof children === 'function'\n ? children({ present: presence.isPresent })\n : React.Children.only(children)\n ) as React.ReactElement;\n\n const ref = useComposedRefs(presence.ref, (child as any).ref);\n const forceMount = typeof children === 'function';\n return forceMount || presence.isPresent ? React.cloneElement(child, { ref }) : null;\n};\n\nPresence.displayName = 'Presence';\n\n/* -------------------------------------------------------------------------------------------------\n * usePresence\n * -----------------------------------------------------------------------------------------------*/\n\nfunction usePresence(present: boolean) {\n const [node, setNode] = React.useState<HTMLElement>();\n const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);\n const prevPresentRef = React.useRef(present);\n const prevAnimationNameRef = React.useRef<string>('none');\n const initialState = present ? 'mounted' : 'unmounted';\n const [state, send] = useStateMachine(initialState, {\n mounted: {\n UNMOUNT: 'unmounted',\n ANIMATION_OUT: 'unmountSuspended',\n },\n unmountSuspended: {\n MOUNT: 'mounted',\n ANIMATION_END: 'unmounted',\n },\n unmounted: {\n MOUNT: 'mounted',\n },\n });\n\n React.useEffect(() => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';\n }, [state]);\n\n useLayoutEffect(() => {\n const styles = stylesRef.current;\n const wasPresent = prevPresentRef.current;\n const hasPresentChanged = wasPresent !== present;\n\n if (hasPresentChanged) {\n const prevAnimationName = prevAnimationNameRef.current;\n const currentAnimationName = getAnimationName(styles);\n\n if (present) {\n send('MOUNT');\n } else if (currentAnimationName === 'none' || styles?.display === 'none') {\n // If there is no exit animation or the element is hidden, animations won't run\n // so we unmount instantly\n send('UNMOUNT');\n } else {\n /**\n * When `present` changes to `false`, we check changes to animation-name to\n * determine whether an animation has started. We chose this approach (reading\n * computed styles) because there is no `animationrun` event and `animationstart`\n * fires after `animation-delay` has expired which would be too late.\n */\n const isAnimating = prevAnimationName !== currentAnimationName;\n\n if (wasPresent && isAnimating) {\n send('ANIMATION_OUT');\n } else {\n send('UNMOUNT');\n }\n }\n\n prevPresentRef.current = present;\n }\n }, [present, send]);\n\n useLayoutEffect(() => {\n if (node) {\n /**\n * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`\n * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we\n * make sure we only trigger ANIMATION_END for the currently active animation.\n */\n const handleAnimationEnd = (event: AnimationEvent) => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n const isCurrentAnimation = currentAnimationName.includes(event.animationName);\n if (event.target === node && isCurrentAnimation) {\n send('ANIMATION_END');\n }\n };\n const handleAnimationStart = (event: AnimationEvent) => {\n if (event.target === node) {\n // if animation occurred, store its name as the previous animation.\n prevAnimationNameRef.current = getAnimationName(stylesRef.current);\n }\n };\n node.addEventListener('animationstart', handleAnimationStart);\n node.addEventListener('animationcancel', handleAnimationEnd);\n node.addEventListener('animationend', handleAnimationEnd);\n return () => {\n node.removeEventListener('animationstart', handleAnimationStart);\n node.removeEventListener('animationcancel', handleAnimationEnd);\n node.removeEventListener('animationend', handleAnimationEnd);\n };\n } else {\n // Transition to the unmounted state if the node is removed prematurely.\n // We avoid doing so during cleanup as the node may change but still exist.\n send('ANIMATION_END');\n }\n }, [node, send]);\n\n return {\n isPresent: ['mounted', 'unmountSuspended'].includes(state),\n ref: React.useCallback((node: HTMLElement) => {\n if (node) stylesRef.current = getComputedStyle(node);\n setNode(node);\n }, []),\n };\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getAnimationName(styles?: CSSStyleDeclaration) {\n return styles?.animationName || 'none';\n}\n\nexport { Presence };\nexport type { PresenceProps };\n","import * as React from 'react';\n\ntype Machine<S> = { [k: string]: { [k: string]: S } };\ntype MachineState<T> = keyof T;\ntype MachineEvent<T> = keyof UnionToIntersection<T[keyof T]>;\n\n// 🤯 https://fettblog.eu/typescript-union-to-intersection/\ntype UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any\n ? R\n : never;\n\nexport function useStateMachine<M>(\n initialState: MachineState<M>,\n machine: M & Machine<MachineState<M>>\n) {\n return React.useReducer((state: MachineState<M>, event: MachineEvent<M>): MachineState<M> => {\n const nextState = (machine[state] as any)[event];\n return nextState ?? state;\n }, initialState);\n}\n"],"names":["dest","source","Object","keys","forEach","key","defineProperty","enumerable","get","Presence","props","present","children","presence","node","setNode","React","useState","stylesRef","useRef","prevPresentRef","prevAnimationNameRef","initialState","state","send","machine","useReducer","event","nextState","$dbcc0409145c648e23373d561744ca9$export$useStateMachine","mounted","UNMOUNT","ANIMATION_OUT","unmountSuspended","MOUNT","ANIMATION_END","unmounted","useEffect","currentAnimationName","getAnimationName","current","useLayoutEffect","styles","wasPresent","prevAnimationName","display","isAnimating","handleAnimationEnd","isCurrentAnimation","includes","animationName","target","handleAnimationStart","addEventListener","removeEventListener","isPresent","ref","useCallback","getComputedStyle","usePresence","child","Children","only","useComposedRefs","cloneElement","displayName"],"version":3,"file":"index.js.map"}
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;ACAA;;;;ACAA;AAWO,SAASI,yCAAT,CACLsB,YADK,EAELgC,OAFK,EAGL;IACA,OAAO1D,uBAAA,CAAiB,CAAC2B,KAAD,EAAyBoB,KAAzB,GAAqE;QAC3F,MAAMa,SAAS,GAAIF,OAAO,CAAC/B,KAAD,CAAR,CAAwBoB,KAAxB,CAAlB,AAAA;QACA,OAAOa,SAAP,KAAA,IAAA,IAAOA,SAAP,KAAA,KAAA,CAAA,GAAOA,SAAP,GAAoBjC,KAApB,CAAA;KAFK,EAGJD,YAHI,CAAP,CAGC;CACF;;;ADRD,MAAMrB,yCAAiC,GAAIC,CAAAA,KAAD,GAAW;IACnD,MAAM,EAZR,SAYUC,OAAF,CAAA,EAZR,UAYmBC,QAAAA,CAAAA,EAAX,GAAwBF,KAA9B,AAAM;IACN,MAAMG,QAAQ,GAAGC,iCAAW,CAACH,OAAD,CAA5B,AAAA;IAEA,MAAMI,KAAK,GACT,OAAOH,QAAP,KAAoB,UAApB,GACIA,QAAQ,CAAC;QAAED,OAAO,EAAEE,QAAQ,CAACG,SAAlBL;KAAH,CADZ,GAEIP,qBAAA,CAAec,IAAf,CAAoBN,QAApB,CAHN,AAEe;IAIf,MAAMO,GAAG,GAAGb,8CAAe,CAACO,QAAQ,CAACM,GAAV,EAAgBJ,KAAD,CAAeI,GAA9B,CAA3B,AAAA;IACA,MAAMC,UAAU,GAAG,OAAOR,QAAP,KAAoB,UAAvC,AAAA;IACA,OAAOQ,UAAU,IAAIP,QAAQ,CAACG,SAAvB,GAAA,aAAmCZ,CAAAA,yBAAA,CAAmBW,KAAnB,EAA0B;QAvBtE,KAuBwEI,GAAAA;KAA5B,CAAnC,GAAwE,IAA/E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AM,KAAA,KAAA,CAAN,GAAA,KAAA,CAAA,GAAAA,MAAM,CAAEU,aAAR,CAAA,IAAyB,MAAhC,CAAA;CACD;;AD/ID","sources":["packages/react/presence/src/index.ts","packages/react/presence/src/Presence.tsx","packages/react/presence/src/useStateMachine.tsx"],"sourcesContent":["export * from './Presence';\n","import * as React from 'react';\nimport * as ReactDOM from 'react-dom';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { useStateMachine } from './useStateMachine';\n\ninterface PresenceProps {\n children: React.ReactElement | ((props: { present: boolean }) => React.ReactElement);\n present: boolean;\n}\n\nconst Presence: React.FC<PresenceProps> = (props) => {\n const { present, children } = props;\n const presence = usePresence(present);\n\n const child = (\n typeof children === 'function'\n ? children({ present: presence.isPresent })\n : React.Children.only(children)\n ) as React.ReactElement;\n\n const ref = useComposedRefs(presence.ref, (child as any).ref);\n const forceMount = typeof children === 'function';\n return forceMount || presence.isPresent ? React.cloneElement(child, { ref }) : null;\n};\n\nPresence.displayName = 'Presence';\n\n/* -------------------------------------------------------------------------------------------------\n * usePresence\n * -----------------------------------------------------------------------------------------------*/\n\nfunction usePresence(present: boolean) {\n const [node, setNode] = React.useState<HTMLElement>();\n const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);\n const prevPresentRef = React.useRef(present);\n const prevAnimationNameRef = React.useRef<string>('none');\n const initialState = present ? 'mounted' : 'unmounted';\n const [state, send] = useStateMachine(initialState, {\n mounted: {\n UNMOUNT: 'unmounted',\n ANIMATION_OUT: 'unmountSuspended',\n },\n unmountSuspended: {\n MOUNT: 'mounted',\n ANIMATION_END: 'unmounted',\n },\n unmounted: {\n MOUNT: 'mounted',\n },\n });\n\n React.useEffect(() => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';\n }, [state]);\n\n useLayoutEffect(() => {\n const styles = stylesRef.current;\n const wasPresent = prevPresentRef.current;\n const hasPresentChanged = wasPresent !== present;\n\n if (hasPresentChanged) {\n const prevAnimationName = prevAnimationNameRef.current;\n const currentAnimationName = getAnimationName(styles);\n\n if (present) {\n send('MOUNT');\n } else if (currentAnimationName === 'none' || styles?.display === 'none') {\n // If there is no exit animation or the element is hidden, animations won't run\n // so we unmount instantly\n send('UNMOUNT');\n } else {\n /**\n * When `present` changes to `false`, we check changes to animation-name to\n * determine whether an animation has started. We chose this approach (reading\n * computed styles) because there is no `animationrun` event and `animationstart`\n * fires after `animation-delay` has expired which would be too late.\n */\n const isAnimating = prevAnimationName !== currentAnimationName;\n\n if (wasPresent && isAnimating) {\n send('ANIMATION_OUT');\n } else {\n send('UNMOUNT');\n }\n }\n\n prevPresentRef.current = present;\n }\n }, [present, send]);\n\n useLayoutEffect(() => {\n if (node) {\n /**\n * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`\n * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we\n * make sure we only trigger ANIMATION_END for the currently active animation.\n */\n const handleAnimationEnd = (event: AnimationEvent) => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n const isCurrentAnimation = currentAnimationName.includes(event.animationName);\n if (event.target === node && isCurrentAnimation) {\n // With React 18 concurrency this update is applied\n // a frame after the animation ends, creating a flash of visible content.\n // By manually flushing we ensure they sync within a frame, removing the flash.\n ReactDOM.flushSync(() => send('ANIMATION_END'));\n }\n };\n const handleAnimationStart = (event: AnimationEvent) => {\n if (event.target === node) {\n // if animation occurred, store its name as the previous animation.\n prevAnimationNameRef.current = getAnimationName(stylesRef.current);\n }\n };\n node.addEventListener('animationstart', handleAnimationStart);\n node.addEventListener('animationcancel', handleAnimationEnd);\n node.addEventListener('animationend', handleAnimationEnd);\n return () => {\n node.removeEventListener('animationstart', handleAnimationStart);\n node.removeEventListener('animationcancel', handleAnimationEnd);\n node.removeEventListener('animationend', handleAnimationEnd);\n };\n } else {\n // Transition to the unmounted state if the node is removed prematurely.\n // We avoid doing so during cleanup as the node may change but still exist.\n send('ANIMATION_END');\n }\n }, [node, send]);\n\n return {\n isPresent: ['mounted', 'unmountSuspended'].includes(state),\n ref: React.useCallback((node: HTMLElement) => {\n if (node) stylesRef.current = getComputedStyle(node);\n setNode(node);\n }, []),\n };\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getAnimationName(styles?: CSSStyleDeclaration) {\n return styles?.animationName || 'none';\n}\n\nexport { Presence };\nexport type { PresenceProps };\n","import * as React from 'react';\n\ntype Machine<S> = { [k: string]: { [k: string]: S } };\ntype MachineState<T> = keyof T;\ntype MachineEvent<T> = keyof UnionToIntersection<T[keyof T]>;\n\n// 🤯 https://fettblog.eu/typescript-union-to-intersection/\ntype UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any\n ? R\n : never;\n\nexport function useStateMachine<M>(\n initialState: MachineState<M>,\n machine: M & Machine<MachineState<M>>\n) {\n return React.useReducer((state: MachineState<M>, event: MachineEvent<M>): MachineState<M> => {\n const nextState = (machine[state] as any)[event];\n return nextState ?? state;\n }, initialState);\n}\n"],"names":["React","ReactDOM","useComposedRefs","useLayoutEffect","useStateMachine","Presence","props","present","children","presence","usePresence","child","isPresent","Children","only","ref","forceMount","cloneElement","displayName","node","setNode","useState","stylesRef","useRef","prevPresentRef","prevAnimationNameRef","initialState","state","send","mounted","UNMOUNT","ANIMATION_OUT","unmountSuspended","MOUNT","ANIMATION_END","unmounted","useEffect","currentAnimationName","getAnimationName","current","styles","wasPresent","hasPresentChanged","prevAnimationName","display","isAnimating","handleAnimationEnd","event","isCurrentAnimation","includes","animationName","target","flushSync","handleAnimationStart","addEventListener","removeEventListener","useCallback","getComputedStyle","machine","useReducer","nextState"],"version":3,"file":"index.js.map"}
@@ -1,2 +1,145 @@
1
- import{useLayoutEffect as e}from"@radix-ui/react-use-layout-effect";import{useComposedRefs as n}from"@radix-ui/react-compose-refs";import*as t from"react";export const Presence=u=>{const{present:o,children:i}=u,s=function(n){const[u,o]=t.useState(),i=t.useRef({}),s=t.useRef(n),c=t.useRef("none"),a=n?"mounted":"unmounted",[d,m]=function(e,n){return t.useReducer(((e,t)=>{const r=n[e][t];return null!=r?r:e}),e)}(a,{mounted:{UNMOUNT:"unmounted",ANIMATION_OUT:"unmountSuspended"},unmountSuspended:{MOUNT:"mounted",ANIMATION_END:"unmounted"},unmounted:{MOUNT:"mounted"}});return t.useEffect((()=>{const e=r(i.current);c.current="mounted"===d?e:"none"}),[d]),e((()=>{const e=i.current,t=s.current;if(t!==n){const u=c.current,o=r(e);if(n)m("MOUNT");else if("none"===o||"none"===(null==e?void 0:e.display))m("UNMOUNT");else{const e=u!==o;m(t&&e?"ANIMATION_OUT":"UNMOUNT")}s.current=n}}),[n,m]),e((()=>{if(u){const e=e=>{const n=r(i.current).includes(e.animationName);e.target===u&&n&&m("ANIMATION_END")},n=e=>{e.target===u&&(c.current=r(i.current))};return u.addEventListener("animationstart",n),u.addEventListener("animationcancel",e),u.addEventListener("animationend",e),()=>{u.removeEventListener("animationstart",n),u.removeEventListener("animationcancel",e),u.removeEventListener("animationend",e)}}m("ANIMATION_END")}),[u,m]),{isPresent:["mounted","unmountSuspended"].includes(d),ref:t.useCallback((e=>{e&&(i.current=getComputedStyle(e)),o(e)}),[])}}(o),c="function"==typeof i?i({present:s.isPresent}):t.Children.only(i),a=n(s.ref,c.ref);return"function"==typeof i||s.isPresent?/*#__PURE__*/t.cloneElement(c,{ref:a}):null};function r(e){return(null==e?void 0:e.animationName)||"none"}Presence.displayName="Presence";
1
+ import {Children as $iqq3r$Children, cloneElement as $iqq3r$cloneElement, useState as $iqq3r$useState, useRef as $iqq3r$useRef, useEffect as $iqq3r$useEffect, useCallback as $iqq3r$useCallback, useReducer as $iqq3r$useReducer} from "react";
2
+ import {flushSync as $iqq3r$flushSync} from "react-dom";
3
+ import {useComposedRefs as $iqq3r$useComposedRefs} from "@radix-ui/react-compose-refs";
4
+ import {useLayoutEffect as $iqq3r$useLayoutEffect} from "@radix-ui/react-use-layout-effect";
5
+
6
+ function $parcel$export(e, n, v, s) {
7
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
+ }
9
+ var $921a889cee6df7e8$exports = {};
10
+
11
+ $parcel$export($921a889cee6df7e8$exports, "Presence", () => $921a889cee6df7e8$export$99c2b779aa4e8b8b);
12
+
13
+
14
+
15
+
16
+
17
+ function $fe963b355347cc68$export$3e6543de14f8614f(initialState, machine) {
18
+ return $iqq3r$useReducer((state, event)=>{
19
+ const nextState = machine[state][event];
20
+ return nextState !== null && nextState !== void 0 ? nextState : state;
21
+ }, initialState);
22
+ }
23
+
24
+
25
+ const $921a889cee6df7e8$export$99c2b779aa4e8b8b = (props)=>{
26
+ const { present: present , children: children } = props;
27
+ const presence = $921a889cee6df7e8$var$usePresence(present);
28
+ const child = typeof children === 'function' ? children({
29
+ present: presence.isPresent
30
+ }) : $iqq3r$Children.only(children);
31
+ const ref = $iqq3r$useComposedRefs(presence.ref, child.ref);
32
+ const forceMount = typeof children === 'function';
33
+ return forceMount || presence.isPresent ? /*#__PURE__*/ $iqq3r$cloneElement(child, {
34
+ ref: ref
35
+ }) : null;
36
+ };
37
+ $921a889cee6df7e8$export$99c2b779aa4e8b8b.displayName = 'Presence';
38
+ /* -------------------------------------------------------------------------------------------------
39
+ * usePresence
40
+ * -----------------------------------------------------------------------------------------------*/ function $921a889cee6df7e8$var$usePresence(present) {
41
+ const [node1, setNode] = $iqq3r$useState();
42
+ const stylesRef = $iqq3r$useRef({});
43
+ const prevPresentRef = $iqq3r$useRef(present);
44
+ const prevAnimationNameRef = $iqq3r$useRef('none');
45
+ const initialState = present ? 'mounted' : 'unmounted';
46
+ const [state, send] = $fe963b355347cc68$export$3e6543de14f8614f(initialState, {
47
+ mounted: {
48
+ UNMOUNT: 'unmounted',
49
+ ANIMATION_OUT: 'unmountSuspended'
50
+ },
51
+ unmountSuspended: {
52
+ MOUNT: 'mounted',
53
+ ANIMATION_END: 'unmounted'
54
+ },
55
+ unmounted: {
56
+ MOUNT: 'mounted'
57
+ }
58
+ });
59
+ $iqq3r$useEffect(()=>{
60
+ const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
61
+ prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';
62
+ }, [
63
+ state
64
+ ]);
65
+ $iqq3r$useLayoutEffect(()=>{
66
+ const styles = stylesRef.current;
67
+ const wasPresent = prevPresentRef.current;
68
+ const hasPresentChanged = wasPresent !== present;
69
+ if (hasPresentChanged) {
70
+ const prevAnimationName = prevAnimationNameRef.current;
71
+ const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(styles);
72
+ if (present) send('MOUNT');
73
+ else if (currentAnimationName === 'none' || (styles === null || styles === void 0 ? void 0 : styles.display) === 'none') // If there is no exit animation or the element is hidden, animations won't run
74
+ // so we unmount instantly
75
+ send('UNMOUNT');
76
+ else {
77
+ /**
78
+ * When `present` changes to `false`, we check changes to animation-name to
79
+ * determine whether an animation has started. We chose this approach (reading
80
+ * computed styles) because there is no `animationrun` event and `animationstart`
81
+ * fires after `animation-delay` has expired which would be too late.
82
+ */ const isAnimating = prevAnimationName !== currentAnimationName;
83
+ if (wasPresent && isAnimating) send('ANIMATION_OUT');
84
+ else send('UNMOUNT');
85
+ }
86
+ prevPresentRef.current = present;
87
+ }
88
+ }, [
89
+ present,
90
+ send
91
+ ]);
92
+ $iqq3r$useLayoutEffect(()=>{
93
+ if (node1) {
94
+ /**
95
+ * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
96
+ * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
97
+ * make sure we only trigger ANIMATION_END for the currently active animation.
98
+ */ const handleAnimationEnd = (event)=>{
99
+ const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
100
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
101
+ if (event.target === node1 && isCurrentAnimation) // With React 18 concurrency this update is applied
102
+ // a frame after the animation ends, creating a flash of visible content.
103
+ // By manually flushing we ensure they sync within a frame, removing the flash.
104
+ $iqq3r$flushSync(()=>send('ANIMATION_END')
105
+ );
106
+ };
107
+ const handleAnimationStart = (event)=>{
108
+ if (event.target === node1) // if animation occurred, store its name as the previous animation.
109
+ prevAnimationNameRef.current = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
110
+ };
111
+ node1.addEventListener('animationstart', handleAnimationStart);
112
+ node1.addEventListener('animationcancel', handleAnimationEnd);
113
+ node1.addEventListener('animationend', handleAnimationEnd);
114
+ return ()=>{
115
+ node1.removeEventListener('animationstart', handleAnimationStart);
116
+ node1.removeEventListener('animationcancel', handleAnimationEnd);
117
+ node1.removeEventListener('animationend', handleAnimationEnd);
118
+ };
119
+ } else // Transition to the unmounted state if the node is removed prematurely.
120
+ // We avoid doing so during cleanup as the node may change but still exist.
121
+ send('ANIMATION_END');
122
+ }, [
123
+ node1,
124
+ send
125
+ ]);
126
+ return {
127
+ isPresent: [
128
+ 'mounted',
129
+ 'unmountSuspended'
130
+ ].includes(state),
131
+ ref: $iqq3r$useCallback((node)=>{
132
+ if (node) stylesRef.current = getComputedStyle(node);
133
+ setNode(node);
134
+ }, [])
135
+ };
136
+ }
137
+ /* -----------------------------------------------------------------------------------------------*/ function $921a889cee6df7e8$var$getAnimationName(styles) {
138
+ return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
139
+ }
140
+
141
+
142
+
143
+
144
+ export {$921a889cee6df7e8$export$99c2b779aa4e8b8b as Presence};
2
145
  //# sourceMappingURL=index.module.js.map
@@ -1 +1 @@
1
- {"mappings":"kKAUA,MAAMA,SAAqCC,IACzC,MAAMC,QAAEA,EAAFC,SAAWA,GAAaF,EACxBG,EAmBR,SAAqBF,GACnB,MAAOG,EAAMC,GAAWC,EAAMC,WACxBC,EAAYF,EAAMG,OAA4B,IAC9CC,EAAiBJ,EAAMG,OAAOR,GAC9BU,EAAuBL,EAAMG,OAAe,QAC5CG,EAAeX,EAAU,UAAY,aACpCY,E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5CjC,EAAKwC,oBAAoB,eAAgBP,IAK3CvB,EAAK,mBAEN,CAACV,EAAMU,IAEH,CACL+B,UAAW,CAAC,UAAW,oBAAoBN,SAAS1B,GACpDiC,IAAKxC,EAAMyC,aAAa3C,IAClBA,IAAMI,EAAUsB,QAAUkB,iBAAiB5C,IAC/CC,EAAQD,KACP,KAvHY6C,CAAYhD,GAEvBiD,EACgB,mBAAbhD,EACHA,EAAS,CAAED,QAASE,EAAS0C,YAC7BvC,EAAM6C,SAASC,KAAKlD,GAGpB4C,EAAMO,EAAgBlD,EAAS2C,IAAMI,EAAcJ,KAEzD,MADuC,mBAAb5C,GACLC,EAAS0C,uBAAYvC,EAAMgD,aAAaJ,EAAO,CAAEJ,IAAAA,IAAS,MAmHjF,SAASjB,EAAiBG,GACxB,OAAOA,MAAAA,OAAA,EAAAA,EAAQQ,gBAAiB,OAjHlCzC,SAASwD,YAAc","sources":["./packages/react/presence/src/Presence.tsx","./packages/react/presence/src/useStateMachine.tsx"],"sourcesContent":["import * as React from 'react';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { useStateMachine } from './useStateMachine';\n\ninterface PresenceProps {\n present: boolean;\n children: React.ReactElement | ((props: { present: boolean }) => React.ReactElement);\n}\n\nconst Presence: React.FC<PresenceProps> = (props) => {\n const { present, children } = props;\n const presence = usePresence(present);\n\n const child = (\n typeof children === 'function'\n ? children({ present: presence.isPresent })\n : React.Children.only(children)\n ) as React.ReactElement;\n\n const ref = useComposedRefs(presence.ref, (child as any).ref);\n const forceMount = typeof children === 'function';\n return forceMount || presence.isPresent ? React.cloneElement(child, { ref }) : null;\n};\n\nPresence.displayName = 'Presence';\n\n/* -------------------------------------------------------------------------------------------------\n * usePresence\n * -----------------------------------------------------------------------------------------------*/\n\nfunction usePresence(present: boolean) {\n const [node, setNode] = React.useState<HTMLElement>();\n const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);\n const prevPresentRef = React.useRef(present);\n const prevAnimationNameRef = React.useRef<string>('none');\n const initialState = present ? 'mounted' : 'unmounted';\n const [state, send] = useStateMachine(initialState, {\n mounted: {\n UNMOUNT: 'unmounted',\n ANIMATION_OUT: 'unmountSuspended',\n },\n unmountSuspended: {\n MOUNT: 'mounted',\n ANIMATION_END: 'unmounted',\n },\n unmounted: {\n MOUNT: 'mounted',\n },\n });\n\n React.useEffect(() => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';\n }, [state]);\n\n useLayoutEffect(() => {\n const styles = stylesRef.current;\n const wasPresent = prevPresentRef.current;\n const hasPresentChanged = wasPresent !== present;\n\n if (hasPresentChanged) {\n const prevAnimationName = prevAnimationNameRef.current;\n const currentAnimationName = getAnimationName(styles);\n\n if (present) {\n send('MOUNT');\n } else if (currentAnimationName === 'none' || styles?.display === 'none') {\n // If there is no exit animation or the element is hidden, animations won't run\n // so we unmount instantly\n send('UNMOUNT');\n } else {\n /**\n * When `present` changes to `false`, we check changes to animation-name to\n * determine whether an animation has started. We chose this approach (reading\n * computed styles) because there is no `animationrun` event and `animationstart`\n * fires after `animation-delay` has expired which would be too late.\n */\n const isAnimating = prevAnimationName !== currentAnimationName;\n\n if (wasPresent && isAnimating) {\n send('ANIMATION_OUT');\n } else {\n send('UNMOUNT');\n }\n }\n\n prevPresentRef.current = present;\n }\n }, [present, send]);\n\n useLayoutEffect(() => {\n if (node) {\n /**\n * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`\n * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we\n * make sure we only trigger ANIMATION_END for the currently active animation.\n */\n const handleAnimationEnd = (event: AnimationEvent) => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n const isCurrentAnimation = currentAnimationName.includes(event.animationName);\n if (event.target === node && isCurrentAnimation) {\n send('ANIMATION_END');\n }\n };\n const handleAnimationStart = (event: AnimationEvent) => {\n if (event.target === node) {\n // if animation occurred, store its name as the previous animation.\n prevAnimationNameRef.current = getAnimationName(stylesRef.current);\n }\n };\n node.addEventListener('animationstart', handleAnimationStart);\n node.addEventListener('animationcancel', handleAnimationEnd);\n node.addEventListener('animationend', handleAnimationEnd);\n return () => {\n node.removeEventListener('animationstart', handleAnimationStart);\n node.removeEventListener('animationcancel', handleAnimationEnd);\n node.removeEventListener('animationend', handleAnimationEnd);\n };\n } else {\n // Transition to the unmounted state if the node is removed prematurely.\n // We avoid doing so during cleanup as the node may change but still exist.\n send('ANIMATION_END');\n }\n }, [node, send]);\n\n return {\n isPresent: ['mounted', 'unmountSuspended'].includes(state),\n ref: React.useCallback((node: HTMLElement) => {\n if (node) stylesRef.current = getComputedStyle(node);\n setNode(node);\n }, []),\n };\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getAnimationName(styles?: CSSStyleDeclaration) {\n return styles?.animationName || 'none';\n}\n\nexport { Presence };\nexport type { PresenceProps };\n","import * as React from 'react';\n\ntype Machine<S> = { [k: string]: { [k: string]: S } };\ntype MachineState<T> = keyof T;\ntype MachineEvent<T> = keyof UnionToIntersection<T[keyof T]>;\n\n// 🤯 https://fettblog.eu/typescript-union-to-intersection/\ntype UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any\n ? R\n : never;\n\nexport function useStateMachine<M>(\n initialState: MachineState<M>,\n machine: M & Machine<MachineState<M>>\n) {\n return React.useReducer((state: MachineState<M>, event: MachineEvent<M>): MachineState<M> => {\n const nextState = (machine[state] as any)[event];\n return nextState ?? state;\n }, initialState);\n}\n"],"names":["Presence","props","present","children","presence","node","setNode","React","useState","stylesRef","useRef","prevPresentRef","prevAnimationNameRef","initialState","state","send","machine","useReducer","event","nextState","$d88c0747af12355025ed3195fb48c68a$export$useStateMachine","mounted","UNMOUNT","ANIMATION_OUT","unmountSuspended","MOUNT","ANIMATION_END","unmounted","useEffect","currentAnimationName","getAnimationName","current","useLayoutEffect","styles","wasPresent","prevAnimationName","display","isAnimating","handleAnimationEnd","isCurrentAnimation","includes","animationName","target","handleAnimationStart","addEventListener","removeEventListener","isPresent","ref","useCallback","getComputedStyle","usePresence","child","Children","only","useComposedRefs","cloneElement","displayName"],"version":3,"file":"index.module.js.map"}
1
+ {"mappings":";;;;;;;;;;;ACAA;;;;ACAA;AAWO,SAASI,yCAAT,CACLsB,YADK,EAELgC,OAFK,EAGL;IACA,OAAO1D,iBAAA,CAAiB,CAAC2B,KAAD,EAAyBoB,KAAzB,GAAqE;QAC3F,MAAMa,SAAS,GAAIF,OAAO,CAAC/B,KAAD,CAAR,CAAwBoB,KAAxB,CAAlB,AAAA;QACA,OAAOa,SAAP,KAAA,IAAA,IAAOA,SAAP,KAAA,KAAA,CAAA,GAAOA,SAAP,GAAoBjC,KAApB,CAAA;KAFK,EAGJD,YAHI,CAAP,CAGC;CACF;;;ADRD,MAAMrB,yCAAiC,GAAIC,CAAAA,KAAD,GAAW;IACnD,MAAM,EAZR,SAYUC,OAAF,CAAA,EAZR,UAYmBC,QAAAA,CAAAA,EAAX,GAAwBF,KAA9B,AAAM;IACN,MAAMG,QAAQ,GAAGC,iCAAW,CAACH,OAAD,CAA5B,AAAA;IAEA,MAAMI,KAAK,GACT,OAAOH,QAAP,KAAoB,UAApB,GACIA,QAAQ,CAAC;QAAED,OAAO,EAAEE,QAAQ,CAACG,SAAlBL;KAAH,CADZ,GAEIP,eAAA,CAAec,IAAf,CAAoBN,QAApB,CAHN,AAEe;IAIf,MAAMO,GAAG,GAAGb,sBAAe,CAACO,QAAQ,CAACM,GAAV,EAAgBJ,KAAD,CAAeI,GAA9B,CAA3B,AAAA;IACA,MAAMC,UAAU,GAAG,OAAOR,QAAP,KAAoB,UAAvC,AAAA;IACA,OAAOQ,UAAU,IAAIP,QAAQ,CAACG,SAAvB,GAAA,aAAmCZ,CAAAA,mBAAA,CAAmBW,KAAnB,EAA0B;QAvBtE,KAuBwEI,GAAAA;KAA5B,CAAnC,GAAwE,IAA/E,CAAoE;CA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CAAN,GAAA,KAAA,CAAA,GAAAA,MAAM,CAAEU,aAAR,CAAA,IAAyB,MAAhC,CAAA;CACD;;AD/ID","sources":["packages/react/presence/src/index.ts","packages/react/presence/src/Presence.tsx","packages/react/presence/src/useStateMachine.tsx"],"sourcesContent":["export * from './Presence';\n","import * as React from 'react';\nimport * as ReactDOM from 'react-dom';\nimport { useComposedRefs } from '@radix-ui/react-compose-refs';\nimport { useLayoutEffect } from '@radix-ui/react-use-layout-effect';\nimport { useStateMachine } from './useStateMachine';\n\ninterface PresenceProps {\n children: React.ReactElement | ((props: { present: boolean }) => React.ReactElement);\n present: boolean;\n}\n\nconst Presence: React.FC<PresenceProps> = (props) => {\n const { present, children } = props;\n const presence = usePresence(present);\n\n const child = (\n typeof children === 'function'\n ? children({ present: presence.isPresent })\n : React.Children.only(children)\n ) as React.ReactElement;\n\n const ref = useComposedRefs(presence.ref, (child as any).ref);\n const forceMount = typeof children === 'function';\n return forceMount || presence.isPresent ? React.cloneElement(child, { ref }) : null;\n};\n\nPresence.displayName = 'Presence';\n\n/* -------------------------------------------------------------------------------------------------\n * usePresence\n * -----------------------------------------------------------------------------------------------*/\n\nfunction usePresence(present: boolean) {\n const [node, setNode] = React.useState<HTMLElement>();\n const stylesRef = React.useRef<CSSStyleDeclaration>({} as any);\n const prevPresentRef = React.useRef(present);\n const prevAnimationNameRef = React.useRef<string>('none');\n const initialState = present ? 'mounted' : 'unmounted';\n const [state, send] = useStateMachine(initialState, {\n mounted: {\n UNMOUNT: 'unmounted',\n ANIMATION_OUT: 'unmountSuspended',\n },\n unmountSuspended: {\n MOUNT: 'mounted',\n ANIMATION_END: 'unmounted',\n },\n unmounted: {\n MOUNT: 'mounted',\n },\n });\n\n React.useEffect(() => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';\n }, [state]);\n\n useLayoutEffect(() => {\n const styles = stylesRef.current;\n const wasPresent = prevPresentRef.current;\n const hasPresentChanged = wasPresent !== present;\n\n if (hasPresentChanged) {\n const prevAnimationName = prevAnimationNameRef.current;\n const currentAnimationName = getAnimationName(styles);\n\n if (present) {\n send('MOUNT');\n } else if (currentAnimationName === 'none' || styles?.display === 'none') {\n // If there is no exit animation or the element is hidden, animations won't run\n // so we unmount instantly\n send('UNMOUNT');\n } else {\n /**\n * When `present` changes to `false`, we check changes to animation-name to\n * determine whether an animation has started. We chose this approach (reading\n * computed styles) because there is no `animationrun` event and `animationstart`\n * fires after `animation-delay` has expired which would be too late.\n */\n const isAnimating = prevAnimationName !== currentAnimationName;\n\n if (wasPresent && isAnimating) {\n send('ANIMATION_OUT');\n } else {\n send('UNMOUNT');\n }\n }\n\n prevPresentRef.current = present;\n }\n }, [present, send]);\n\n useLayoutEffect(() => {\n if (node) {\n /**\n * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`\n * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we\n * make sure we only trigger ANIMATION_END for the currently active animation.\n */\n const handleAnimationEnd = (event: AnimationEvent) => {\n const currentAnimationName = getAnimationName(stylesRef.current);\n const isCurrentAnimation = currentAnimationName.includes(event.animationName);\n if (event.target === node && isCurrentAnimation) {\n // With React 18 concurrency this update is applied\n // a frame after the animation ends, creating a flash of visible content.\n // By manually flushing we ensure they sync within a frame, removing the flash.\n ReactDOM.flushSync(() => send('ANIMATION_END'));\n }\n };\n const handleAnimationStart = (event: AnimationEvent) => {\n if (event.target === node) {\n // if animation occurred, store its name as the previous animation.\n prevAnimationNameRef.current = getAnimationName(stylesRef.current);\n }\n };\n node.addEventListener('animationstart', handleAnimationStart);\n node.addEventListener('animationcancel', handleAnimationEnd);\n node.addEventListener('animationend', handleAnimationEnd);\n return () => {\n node.removeEventListener('animationstart', handleAnimationStart);\n node.removeEventListener('animationcancel', handleAnimationEnd);\n node.removeEventListener('animationend', handleAnimationEnd);\n };\n } else {\n // Transition to the unmounted state if the node is removed prematurely.\n // We avoid doing so during cleanup as the node may change but still exist.\n send('ANIMATION_END');\n }\n }, [node, send]);\n\n return {\n isPresent: ['mounted', 'unmountSuspended'].includes(state),\n ref: React.useCallback((node: HTMLElement) => {\n if (node) stylesRef.current = getComputedStyle(node);\n setNode(node);\n }, []),\n };\n}\n\n/* -----------------------------------------------------------------------------------------------*/\n\nfunction getAnimationName(styles?: CSSStyleDeclaration) {\n return styles?.animationName || 'none';\n}\n\nexport { Presence };\nexport type { PresenceProps };\n","import * as React from 'react';\n\ntype Machine<S> = { [k: string]: { [k: string]: S } };\ntype MachineState<T> = keyof T;\ntype MachineEvent<T> = keyof UnionToIntersection<T[keyof T]>;\n\n// 🤯 https://fettblog.eu/typescript-union-to-intersection/\ntype UnionToIntersection<T> = (T extends any ? (x: T) => any : never) extends (x: infer R) => any\n ? R\n : never;\n\nexport function useStateMachine<M>(\n initialState: MachineState<M>,\n machine: M & Machine<MachineState<M>>\n) {\n return React.useReducer((state: MachineState<M>, event: MachineEvent<M>): MachineState<M> => {\n const nextState = (machine[state] as any)[event];\n return nextState ?? state;\n }, initialState);\n}\n"],"names":["React","ReactDOM","useComposedRefs","useLayoutEffect","useStateMachine","Presence","props","present","children","presence","usePresence","child","isPresent","Children","only","ref","forceMount","cloneElement","displayName","node","setNode","useState","stylesRef","useRef","prevPresentRef","prevAnimationNameRef","initialState","state","send","mounted","UNMOUNT","ANIMATION_OUT","unmountSuspended","MOUNT","ANIMATION_END","unmounted","useEffect","currentAnimationName","getAnimationName","current","styles","wasPresent","hasPresentChanged","prevAnimationName","display","isAnimating","handleAnimationEnd","event","isCurrentAnimation","includes","animationName","target","flushSync","handleAnimationStart","addEventListener","removeEventListener","useCallback","getComputedStyle","machine","useReducer","nextState"],"version":3,"file":"index.module.js.map"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-presence",
3
- "version": "0.1.2",
3
+ "version": "0.1.3-rc.11",
4
4
  "license": "MIT",
5
5
  "source": "src/index.ts",
6
6
  "main": "dist/index.js",
@@ -17,11 +17,12 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@babel/runtime": "^7.13.10",
20
- "@radix-ui/react-compose-refs": "0.1.0",
21
- "@radix-ui/react-use-layout-effect": "0.1.0"
20
+ "@radix-ui/react-compose-refs": "0.1.1-rc.11",
21
+ "@radix-ui/react-use-layout-effect": "0.1.1-rc.11"
22
22
  },
23
23
  "peerDependencies": {
24
- "react": ">=16.8"
24
+ "react": "^16.8 || ^17.0 || ^18.0",
25
+ "react-dom": "^16.8 || ^17.0 || ^18.0"
25
26
  },
26
27
  "homepage": "https://radix-ui.com/primitives",
27
28
  "repository": {
@@ -30,5 +31,6 @@
30
31
  },
31
32
  "bugs": {
32
33
  "url": "https://github.com/radix-ui/primitives/issues"
33
- }
34
+ },
35
+ "stableVersion": "0.1.2"
34
36
  }