@radix-ui/react-presence 1.0.1 → 1.1.0-rc.2

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.mts CHANGED
@@ -1,10 +1,11 @@
1
- import * as React from "react";
2
- export interface PresenceProps {
1
+ import * as React from 'react';
2
+
3
+ interface PresenceProps {
3
4
  children: React.ReactElement | ((props: {
4
5
  present: boolean;
5
6
  }) => React.ReactElement);
6
7
  present: boolean;
7
8
  }
8
- export const Presence: React.FC<PresenceProps>;
9
+ declare const Presence: React.FC<PresenceProps>;
9
10
 
10
- //# sourceMappingURL=index.d.ts.map
11
+ export { Presence, type PresenceProps };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
- import * as React from "react";
2
- export interface PresenceProps {
1
+ import * as React from 'react';
2
+
3
+ interface PresenceProps {
3
4
  children: React.ReactElement | ((props: {
4
5
  present: boolean;
5
6
  }) => React.ReactElement);
6
7
  present: boolean;
7
8
  }
8
- export const Presence: React.FC<PresenceProps>;
9
+ declare const Presence: React.FC<PresenceProps>;
9
10
 
10
- //# sourceMappingURL=index.d.ts.map
11
+ export { Presence, type PresenceProps };
package/dist/index.js CHANGED
@@ -1,143 +1,141 @@
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");
1
+ "use strict";
2
+ "use client";
3
+ (() => {
4
+ var __create = Object.create;
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
11
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
12
+ }) : x)(function(x) {
13
+ if (typeof require !== "undefined") return require.apply(this, arguments);
14
+ throw Error('Dynamic require of "' + x + '" is not supported');
15
+ });
16
+ var __copyProps = (to, from, except, desc) => {
17
+ if (from && typeof from === "object" || typeof from === "function") {
18
+ for (let key of __getOwnPropNames(from))
19
+ if (!__hasOwnProp.call(to, key) && key !== except)
20
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
25
+ // If the importer is in node compatibility mode or this is not an ESM
26
+ // file that has been converted to a CommonJS file using a Babel-
27
+ // compatible transform (i.e. "__esModule" has not been set), then set
28
+ // "default" to the CommonJS "module.exports" for node compatibility.
29
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
30
+ mod
31
+ ));
5
32
 
6
- function $parcel$export(e, n, v, s) {
7
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
- }
33
+ // packages/react/presence/src/Presence.tsx
34
+ var React2 = __toESM(__require("react"));
35
+ var ReactDOM = __toESM(__require("react-dom"));
36
+ var import_react_compose_refs = __require("@radix-ui/react-compose-refs");
37
+ var import_react_use_layout_effect = __require("@radix-ui/react-use-layout-effect");
9
38
 
10
- $parcel$export(module.exports, "Presence", () => $a2fa0214bb2735a1$export$99c2b779aa4e8b8b);
11
-
12
-
13
-
14
-
15
-
16
- function $8f63844556d0d3cd$export$3e6543de14f8614f(initialState, machine) {
17
- return $fnLeV$react.useReducer((state, event)=>{
18
- const nextState = machine[state][event];
19
- return nextState !== null && nextState !== void 0 ? nextState : state;
39
+ // packages/react/presence/src/useStateMachine.tsx
40
+ var React = __toESM(__require("react"));
41
+ function useStateMachine(initialState, machine) {
42
+ return React.useReducer((state, event) => {
43
+ const nextState = machine[state][event];
44
+ return nextState ?? state;
20
45
  }, initialState);
21
- }
22
-
46
+ }
23
47
 
24
- const $a2fa0214bb2735a1$export$99c2b779aa4e8b8b = (props)=>{
25
- const { present: present , children: children } = props;
26
- const presence = $a2fa0214bb2735a1$var$usePresence(present);
27
- const child = typeof children === 'function' ? children({
28
- present: presence.isPresent
29
- }) : $fnLeV$react.Children.only(children);
30
- const ref = $fnLeV$radixuireactcomposerefs.useComposedRefs(presence.ref, child.ref);
31
- const forceMount = typeof children === 'function';
32
- return forceMount || presence.isPresent ? /*#__PURE__*/ $fnLeV$react.cloneElement(child, {
33
- ref: ref
34
- }) : null;
35
- };
36
- $a2fa0214bb2735a1$export$99c2b779aa4e8b8b.displayName = 'Presence';
37
- /* -------------------------------------------------------------------------------------------------
38
- * usePresence
39
- * -----------------------------------------------------------------------------------------------*/ function $a2fa0214bb2735a1$var$usePresence(present) {
40
- const [node1, setNode] = $fnLeV$react.useState();
41
- const stylesRef = $fnLeV$react.useRef({});
42
- const prevPresentRef = $fnLeV$react.useRef(present);
43
- const prevAnimationNameRef = $fnLeV$react.useRef('none');
44
- const initialState = present ? 'mounted' : 'unmounted';
45
- const [state, send] = $8f63844556d0d3cd$export$3e6543de14f8614f(initialState, {
46
- mounted: {
47
- UNMOUNT: 'unmounted',
48
- ANIMATION_OUT: 'unmountSuspended'
49
- },
50
- unmountSuspended: {
51
- MOUNT: 'mounted',
52
- ANIMATION_END: 'unmounted'
53
- },
54
- unmounted: {
55
- MOUNT: 'mounted'
56
- }
48
+ // packages/react/presence/src/Presence.tsx
49
+ var Presence = (props) => {
50
+ const { present, children } = props;
51
+ const presence = usePresence(present);
52
+ const child = typeof children === "function" ? children({ present: presence.isPresent }) : React2.Children.only(children);
53
+ const ref = (0, import_react_compose_refs.useComposedRefs)(presence.ref, child.ref);
54
+ const forceMount = typeof children === "function";
55
+ return forceMount || presence.isPresent ? React2.cloneElement(child, { ref }) : null;
56
+ };
57
+ Presence.displayName = "Presence";
58
+ function usePresence(present) {
59
+ const [node, setNode] = React2.useState();
60
+ const stylesRef = React2.useRef({});
61
+ const prevPresentRef = React2.useRef(present);
62
+ const prevAnimationNameRef = React2.useRef("none");
63
+ const initialState = present ? "mounted" : "unmounted";
64
+ const [state, send] = useStateMachine(initialState, {
65
+ mounted: {
66
+ UNMOUNT: "unmounted",
67
+ ANIMATION_OUT: "unmountSuspended"
68
+ },
69
+ unmountSuspended: {
70
+ MOUNT: "mounted",
71
+ ANIMATION_END: "unmounted"
72
+ },
73
+ unmounted: {
74
+ MOUNT: "mounted"
75
+ }
57
76
  });
58
- $fnLeV$react.useEffect(()=>{
59
- const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
60
- prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';
61
- }, [
62
- state
63
- ]);
64
- $fnLeV$radixuireactuselayouteffect.useLayoutEffect(()=>{
65
- const styles = stylesRef.current;
66
- const wasPresent = prevPresentRef.current;
67
- const hasPresentChanged = wasPresent !== present;
68
- if (hasPresentChanged) {
69
- const prevAnimationName = prevAnimationNameRef.current;
70
- const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(styles);
71
- if (present) send('MOUNT');
72
- 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
73
- // so we unmount instantly
74
- send('UNMOUNT');
75
- else {
76
- /**
77
- * When `present` changes to `false`, we check changes to animation-name to
78
- * determine whether an animation has started. We chose this approach (reading
79
- * computed styles) because there is no `animationrun` event and `animationstart`
80
- * fires after `animation-delay` has expired which would be too late.
81
- */ const isAnimating = prevAnimationName !== currentAnimationName;
82
- if (wasPresent && isAnimating) send('ANIMATION_OUT');
83
- else send('UNMOUNT');
84
- }
85
- prevPresentRef.current = present;
77
+ React2.useEffect(() => {
78
+ const currentAnimationName = getAnimationName(stylesRef.current);
79
+ prevAnimationNameRef.current = state === "mounted" ? currentAnimationName : "none";
80
+ }, [state]);
81
+ (0, import_react_use_layout_effect.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 = getAnimationName(styles);
88
+ if (present) {
89
+ send("MOUNT");
90
+ } else if (currentAnimationName === "none" || styles?.display === "none") {
91
+ send("UNMOUNT");
92
+ } else {
93
+ const isAnimating = prevAnimationName !== currentAnimationName;
94
+ if (wasPresent && isAnimating) {
95
+ send("ANIMATION_OUT");
96
+ } else {
97
+ send("UNMOUNT");
98
+ }
86
99
  }
87
- }, [
88
- present,
89
- send
90
- ]);
91
- $fnLeV$radixuireactuselayouteffect.useLayoutEffect(()=>{
92
- if (node1) {
93
- /**
94
- * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
95
- * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
96
- * make sure we only trigger ANIMATION_END for the currently active animation.
97
- */ const handleAnimationEnd = (event)=>{
98
- const currentAnimationName = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
99
- const isCurrentAnimation = currentAnimationName.includes(event.animationName);
100
- if (event.target === node1 && isCurrentAnimation) // With React 18 concurrency this update is applied
101
- // a frame after the animation ends, creating a flash of visible content.
102
- // By manually flushing we ensure they sync within a frame, removing the flash.
103
- $fnLeV$reactdom.flushSync(()=>send('ANIMATION_END')
104
- );
105
- };
106
- const handleAnimationStart = (event)=>{
107
- if (event.target === node1) // if animation occurred, store its name as the previous animation.
108
- prevAnimationNameRef.current = $a2fa0214bb2735a1$var$getAnimationName(stylesRef.current);
109
- };
110
- node1.addEventListener('animationstart', handleAnimationStart);
111
- node1.addEventListener('animationcancel', handleAnimationEnd);
112
- node1.addEventListener('animationend', handleAnimationEnd);
113
- return ()=>{
114
- node1.removeEventListener('animationstart', handleAnimationStart);
115
- node1.removeEventListener('animationcancel', handleAnimationEnd);
116
- node1.removeEventListener('animationend', handleAnimationEnd);
117
- };
118
- } else // Transition to the unmounted state if the node is removed prematurely.
119
- // We avoid doing so during cleanup as the node may change but still exist.
120
- send('ANIMATION_END');
121
- }, [
122
- node1,
123
- send
124
- ]);
100
+ prevPresentRef.current = present;
101
+ }
102
+ }, [present, send]);
103
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
104
+ if (node) {
105
+ const handleAnimationEnd = (event) => {
106
+ const currentAnimationName = getAnimationName(stylesRef.current);
107
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
108
+ if (event.target === node && isCurrentAnimation) {
109
+ ReactDOM.flushSync(() => send("ANIMATION_END"));
110
+ }
111
+ };
112
+ const handleAnimationStart = (event) => {
113
+ if (event.target === node) {
114
+ prevAnimationNameRef.current = getAnimationName(stylesRef.current);
115
+ }
116
+ };
117
+ node.addEventListener("animationstart", handleAnimationStart);
118
+ node.addEventListener("animationcancel", handleAnimationEnd);
119
+ node.addEventListener("animationend", handleAnimationEnd);
120
+ return () => {
121
+ node.removeEventListener("animationstart", handleAnimationStart);
122
+ node.removeEventListener("animationcancel", handleAnimationEnd);
123
+ node.removeEventListener("animationend", handleAnimationEnd);
124
+ };
125
+ } else {
126
+ send("ANIMATION_END");
127
+ }
128
+ }, [node, send]);
125
129
  return {
126
- isPresent: [
127
- 'mounted',
128
- 'unmountSuspended'
129
- ].includes(state),
130
- ref: $fnLeV$react.useCallback((node)=>{
131
- if (node) stylesRef.current = getComputedStyle(node);
132
- setNode(node);
133
- }, [])
130
+ isPresent: ["mounted", "unmountSuspended"].includes(state),
131
+ ref: React2.useCallback((node2) => {
132
+ if (node2) stylesRef.current = getComputedStyle(node2);
133
+ setNode(node2);
134
+ }, [])
134
135
  };
135
- }
136
- /* -----------------------------------------------------------------------------------------------*/ function $a2fa0214bb2735a1$var$getAnimationName(styles) {
137
- return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
138
- }
139
-
140
-
141
-
142
-
136
+ }
137
+ function getAnimationName(styles) {
138
+ return styles?.animationName || "none";
139
+ }
140
+ })();
143
141
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1,7 @@
1
- {"mappings":";;;;;;;;;;ACAA;;;;ACAA;AAWO,SAASK,yCAAT,CACLqB,YADK,EAELgC,OAFK,EAGL;IACA,OAAOzD,uBAAA,CAAiB,CAAC0B,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,MAAM1B,yCAAiC,GAAIM,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,GAEIN,qBAAA,CAAea,IAAf,CAAoBN,QAApB,CAHN,AAEe;IAIf,MAAMO,GAAG,GAAGZ,8CAAe,CAACM,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,aAAmCX,CAAAA,yBAAA,CAAmBU,KAAnB,EAA0B;QAvBtE,KAuBwEI,GAAAA;KAA5B,CAAnC,GAAwE,IAA/E,CAAoE;CAZtE,AAaC;AAEDf,yCAAQ,CAACkB,WAAT,GAAuB,UAA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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 { Presence } from './Presence';\nexport type { PresenceProps } 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":["Presence","React","ReactDOM","useComposedRefs","useLayoutEffect","useStateMachine","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
+ "version": 3,
3
+ "sources": ["../src/Presence.tsx", "../src/useStateMachine.tsx"],
4
+ "sourcesContent": ["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// \uD83E\uDD2F 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"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAAA,SAAuB;AACvB,iBAA0B;AAC1B,kCAAgC;AAChC,uCAAgC;;;ACHhC,cAAuB;AAWhB,WAAS,gBACd,cACA,SACA;AACA,WAAa,iBAAW,CAAC,OAAwB,UAA4C;AAC3F,YAAM,YAAa,QAAQ,KAAK,EAAU,KAAK;AAC/C,aAAO,aAAa;AAAA,IACtB,GAAG,YAAY;AAAA,EACjB;;;ADRA,MAAM,WAAoC,CAAC,UAAU;AACnD,UAAM,EAAE,SAAS,SAAS,IAAI;AAC9B,UAAM,WAAW,YAAY,OAAO;AAEpC,UAAM,QACJ,OAAO,aAAa,aAChB,SAAS,EAAE,SAAS,SAAS,UAAU,CAAC,IAClC,gBAAS,KAAK,QAAQ;AAGlC,UAAM,UAAM,2CAAgB,SAAS,KAAM,MAAc,GAAG;AAC5D,UAAM,aAAa,OAAO,aAAa;AACvC,WAAO,cAAc,SAAS,YAAkB,oBAAa,OAAO,EAAE,IAAI,CAAC,IAAI;AAAA,EACjF;AAEA,WAAS,cAAc;AAMvB,WAAS,YAAY,SAAkB;AACrC,UAAM,CAAC,MAAM,OAAO,IAAU,gBAAsB;AACpD,UAAM,YAAkB,cAA4B,CAAC,CAAQ;AAC7D,UAAM,iBAAuB,cAAO,OAAO;AAC3C,UAAM,uBA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AAuB,CAAC,UAA0B;AACtD,cAAI,MAAM,WAAW,MAAM;AAEzB,iCAAqB,UAAU,iBAAiB,UAAU,OAAO;AAAA,UACnE;AAAA,QACF;AACA,aAAK,iBAAiB,kBAAkB,oBAAoB;AAC5D,aAAK,iBAAiB,mBAAmB,kBAAkB;AAC3D,aAAK,iBAAiB,gBAAgB,kBAAkB;AACxD,eAAO,MAAM;AACX,eAAK,oBAAoB,kBAAkB,oBAAoB;AAC/D,eAAK,oBAAoB,mBAAmB,kBAAkB;AAC9D,eAAK,oBAAoB,gBAAgB,kBAAkB;AAAA,QAC7D;AAAA,MACF,OAAO;AAGL,aAAK,eAAe;AAAA,MACtB;AAAA,IACF,GAAG,CAAC,MAAM,IAAI,CAAC;AAEf,WAAO;AAAA,MACL,WAAW,CAAC,WAAW,kBAAkB,EAAE,SAAS,KAAK;AAAA,MACzD,KAAW,mBAAY,CAACC,UAAsB;AAC5C,YAAIA,MAAM,WAAU,UAAU,iBAAiBA,KAAI;AACnD,gBAAQA,KAAI;AAAA,MACd,GAAG,CAAC,CAAC;AAAA,IACP;AAAA,EACF;AAIA,WAAS,iBAAiB,QAA8B;AACtD,WAAO,QAAQ,iBAAiB;AAAA,EAClC;",
6
+ "names": ["React", "node"]
7
+ }
package/dist/index.mjs CHANGED
@@ -1,139 +1,113 @@
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";
1
+ "use client";
5
2
 
3
+ // packages/react/presence/src/Presence.tsx
4
+ import * as React2 from "react";
5
+ import * as ReactDOM from "react-dom";
6
+ import { useComposedRefs } from "@radix-ui/react-compose-refs";
7
+ import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
6
8
 
7
-
8
-
9
-
10
-
11
- function $fe963b355347cc68$export$3e6543de14f8614f(initialState, machine) {
12
- return $iqq3r$useReducer((state, event)=>{
13
- const nextState = machine[state][event];
14
- return nextState !== null && nextState !== void 0 ? nextState : state;
15
- }, initialState);
9
+ // packages/react/presence/src/useStateMachine.tsx
10
+ import * as React from "react";
11
+ function useStateMachine(initialState, machine) {
12
+ return React.useReducer((state, event) => {
13
+ const nextState = machine[state][event];
14
+ return nextState ?? state;
15
+ }, initialState);
16
16
  }
17
17
 
18
-
19
- const $921a889cee6df7e8$export$99c2b779aa4e8b8b = (props)=>{
20
- const { present: present , children: children } = props;
21
- const presence = $921a889cee6df7e8$var$usePresence(present);
22
- const child = typeof children === 'function' ? children({
23
- present: presence.isPresent
24
- }) : $iqq3r$Children.only(children);
25
- const ref = $iqq3r$useComposedRefs(presence.ref, child.ref);
26
- const forceMount = typeof children === 'function';
27
- return forceMount || presence.isPresent ? /*#__PURE__*/ $iqq3r$cloneElement(child, {
28
- ref: ref
29
- }) : null;
18
+ // packages/react/presence/src/Presence.tsx
19
+ var Presence = (props) => {
20
+ const { present, children } = props;
21
+ const presence = usePresence(present);
22
+ const child = typeof children === "function" ? children({ present: presence.isPresent }) : React2.Children.only(children);
23
+ const ref = useComposedRefs(presence.ref, child.ref);
24
+ const forceMount = typeof children === "function";
25
+ return forceMount || presence.isPresent ? React2.cloneElement(child, { ref }) : null;
30
26
  };
31
- $921a889cee6df7e8$export$99c2b779aa4e8b8b.displayName = 'Presence';
32
- /* -------------------------------------------------------------------------------------------------
33
- * usePresence
34
- * -----------------------------------------------------------------------------------------------*/ function $921a889cee6df7e8$var$usePresence(present) {
35
- const [node1, setNode] = $iqq3r$useState();
36
- const stylesRef = $iqq3r$useRef({});
37
- const prevPresentRef = $iqq3r$useRef(present);
38
- const prevAnimationNameRef = $iqq3r$useRef('none');
39
- const initialState = present ? 'mounted' : 'unmounted';
40
- const [state, send] = $fe963b355347cc68$export$3e6543de14f8614f(initialState, {
41
- mounted: {
42
- UNMOUNT: 'unmounted',
43
- ANIMATION_OUT: 'unmountSuspended'
44
- },
45
- unmountSuspended: {
46
- MOUNT: 'mounted',
47
- ANIMATION_END: 'unmounted'
48
- },
49
- unmounted: {
50
- MOUNT: 'mounted'
27
+ Presence.displayName = "Presence";
28
+ function usePresence(present) {
29
+ const [node, setNode] = React2.useState();
30
+ const stylesRef = React2.useRef({});
31
+ const prevPresentRef = React2.useRef(present);
32
+ const prevAnimationNameRef = React2.useRef("none");
33
+ const initialState = present ? "mounted" : "unmounted";
34
+ const [state, send] = useStateMachine(initialState, {
35
+ mounted: {
36
+ UNMOUNT: "unmounted",
37
+ ANIMATION_OUT: "unmountSuspended"
38
+ },
39
+ unmountSuspended: {
40
+ MOUNT: "mounted",
41
+ ANIMATION_END: "unmounted"
42
+ },
43
+ unmounted: {
44
+ MOUNT: "mounted"
45
+ }
46
+ });
47
+ React2.useEffect(() => {
48
+ const currentAnimationName = getAnimationName(stylesRef.current);
49
+ prevAnimationNameRef.current = state === "mounted" ? currentAnimationName : "none";
50
+ }, [state]);
51
+ useLayoutEffect(() => {
52
+ const styles = stylesRef.current;
53
+ const wasPresent = prevPresentRef.current;
54
+ const hasPresentChanged = wasPresent !== present;
55
+ if (hasPresentChanged) {
56
+ const prevAnimationName = prevAnimationNameRef.current;
57
+ const currentAnimationName = getAnimationName(styles);
58
+ if (present) {
59
+ send("MOUNT");
60
+ } else if (currentAnimationName === "none" || styles?.display === "none") {
61
+ send("UNMOUNT");
62
+ } else {
63
+ const isAnimating = prevAnimationName !== currentAnimationName;
64
+ if (wasPresent && isAnimating) {
65
+ send("ANIMATION_OUT");
66
+ } else {
67
+ send("UNMOUNT");
68
+ }
69
+ }
70
+ prevPresentRef.current = present;
71
+ }
72
+ }, [present, send]);
73
+ useLayoutEffect(() => {
74
+ if (node) {
75
+ const handleAnimationEnd = (event) => {
76
+ const currentAnimationName = getAnimationName(stylesRef.current);
77
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
78
+ if (event.target === node && isCurrentAnimation) {
79
+ ReactDOM.flushSync(() => send("ANIMATION_END"));
51
80
  }
52
- });
53
- $iqq3r$useEffect(()=>{
54
- const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
55
- prevAnimationNameRef.current = state === 'mounted' ? currentAnimationName : 'none';
56
- }, [
57
- state
58
- ]);
59
- $iqq3r$useLayoutEffect(()=>{
60
- const styles = stylesRef.current;
61
- const wasPresent = prevPresentRef.current;
62
- const hasPresentChanged = wasPresent !== present;
63
- if (hasPresentChanged) {
64
- const prevAnimationName = prevAnimationNameRef.current;
65
- const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(styles);
66
- if (present) send('MOUNT');
67
- 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
68
- // so we unmount instantly
69
- send('UNMOUNT');
70
- else {
71
- /**
72
- * When `present` changes to `false`, we check changes to animation-name to
73
- * determine whether an animation has started. We chose this approach (reading
74
- * computed styles) because there is no `animationrun` event and `animationstart`
75
- * fires after `animation-delay` has expired which would be too late.
76
- */ const isAnimating = prevAnimationName !== currentAnimationName;
77
- if (wasPresent && isAnimating) send('ANIMATION_OUT');
78
- else send('UNMOUNT');
79
- }
80
- prevPresentRef.current = present;
81
+ };
82
+ const handleAnimationStart = (event) => {
83
+ if (event.target === node) {
84
+ prevAnimationNameRef.current = getAnimationName(stylesRef.current);
81
85
  }
82
- }, [
83
- present,
84
- send
85
- ]);
86
- $iqq3r$useLayoutEffect(()=>{
87
- if (node1) {
88
- /**
89
- * Triggering an ANIMATION_OUT during an ANIMATION_IN will fire an `animationcancel`
90
- * event for ANIMATION_IN after we have entered `unmountSuspended` state. So, we
91
- * make sure we only trigger ANIMATION_END for the currently active animation.
92
- */ const handleAnimationEnd = (event)=>{
93
- const currentAnimationName = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
94
- const isCurrentAnimation = currentAnimationName.includes(event.animationName);
95
- if (event.target === node1 && isCurrentAnimation) // With React 18 concurrency this update is applied
96
- // a frame after the animation ends, creating a flash of visible content.
97
- // By manually flushing we ensure they sync within a frame, removing the flash.
98
- $iqq3r$flushSync(()=>send('ANIMATION_END')
99
- );
100
- };
101
- const handleAnimationStart = (event)=>{
102
- if (event.target === node1) // if animation occurred, store its name as the previous animation.
103
- prevAnimationNameRef.current = $921a889cee6df7e8$var$getAnimationName(stylesRef.current);
104
- };
105
- node1.addEventListener('animationstart', handleAnimationStart);
106
- node1.addEventListener('animationcancel', handleAnimationEnd);
107
- node1.addEventListener('animationend', handleAnimationEnd);
108
- return ()=>{
109
- node1.removeEventListener('animationstart', handleAnimationStart);
110
- node1.removeEventListener('animationcancel', handleAnimationEnd);
111
- node1.removeEventListener('animationend', handleAnimationEnd);
112
- };
113
- } else // Transition to the unmounted state if the node is removed prematurely.
114
- // We avoid doing so during cleanup as the node may change but still exist.
115
- send('ANIMATION_END');
116
- }, [
117
- node1,
118
- send
119
- ]);
120
- return {
121
- isPresent: [
122
- 'mounted',
123
- 'unmountSuspended'
124
- ].includes(state),
125
- ref: $iqq3r$useCallback((node)=>{
126
- if (node) stylesRef.current = getComputedStyle(node);
127
- setNode(node);
128
- }, [])
129
- };
86
+ };
87
+ node.addEventListener("animationstart", handleAnimationStart);
88
+ node.addEventListener("animationcancel", handleAnimationEnd);
89
+ node.addEventListener("animationend", handleAnimationEnd);
90
+ return () => {
91
+ node.removeEventListener("animationstart", handleAnimationStart);
92
+ node.removeEventListener("animationcancel", handleAnimationEnd);
93
+ node.removeEventListener("animationend", handleAnimationEnd);
94
+ };
95
+ } else {
96
+ send("ANIMATION_END");
97
+ }
98
+ }, [node, send]);
99
+ return {
100
+ isPresent: ["mounted", "unmountSuspended"].includes(state),
101
+ ref: React2.useCallback((node2) => {
102
+ if (node2) stylesRef.current = getComputedStyle(node2);
103
+ setNode(node2);
104
+ }, [])
105
+ };
130
106
  }
131
- /* -----------------------------------------------------------------------------------------------*/ function $921a889cee6df7e8$var$getAnimationName(styles) {
132
- return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
107
+ function getAnimationName(styles) {
108
+ return styles?.animationName || "none";
133
109
  }
134
-
135
-
136
-
137
-
138
- export {$921a889cee6df7e8$export$99c2b779aa4e8b8b as Presence};
110
+ export {
111
+ Presence
112
+ };
139
113
  //# sourceMappingURL=index.mjs.map
@@ -1 +1,7 @@
1
- {"mappings":";;;;;ACAA;;;;ACAA;AAWO,SAASK,yCAAT,CACLqB,YADK,EAELgC,OAFK,EAGL;IACA,OAAOzD,iBAAA,CAAiB,CAAC0B,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,MAAM1B,yCAAiC,GAAIM,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,GAEIN,eAAA,CAAea,IAAf,CAAoBN,QAApB,CAHN,AAEe;IAIf,MAAMO,GAAG,GAAGZ,sBAAe,CAACM,QAAQ,CAACM,GAAV,EAAgBJ,KAAD,CAAeI,GAA9B,CAA3B,AAAA;IACA,MAAMC,UAAU,GAAG,OAAOR,QAAP,KAAoB,UAAvC,AAAA;IACA,O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F,EAHE,CAGJ;KALH,CAAO;CAOR;AAED,oGAAA,CAEA,SAASkB,sCAAT,CAA0BE,MAA1B,EAAwD;IACtD,OAAO,AAAAA,CAAAA,MAAM,KAAA,IAAN,IAAAA,MAAM,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 { Presence } from './Presence';\nexport type { PresenceProps } 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":["Presence","React","ReactDOM","useComposedRefs","useLayoutEffect","useStateMachine","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.mjs.map"}
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/Presence.tsx", "../src/useStateMachine.tsx"],
4
+ "sourcesContent": ["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// \uD83E\uDD2F 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"],
5
+ "mappings": ";;;AAAA,YAAYA,YAAW;AACvB,YAAY,cAAc;AAC1B,SAAS,uBAAuB;AAChC,SAAS,uBAAuB;;;ACHhC,YAAY,WAAW;AAWhB,SAAS,gBACd,cACA,SACA;AACA,SAAa,iBAAW,CAAC,OAAwB,UAA4C;AAC3F,UAAM,YAAa,QAAQ,KAAK,EAAU,KAAK;AAC/C,WAAO,aAAa;AAAA,EACtB,GAAG,YAAY;AACjB;;;ADRA,IAAM,WAAoC,CAAC,UAAU;AACnD,QAAM,EAAE,SAAS,SAAS,IAAI;AAC9B,QAAM,WAAW,YAAY,OAAO;AAEpC,QAAM,QACJ,OAAO,aAAa,aAChB,SAAS,EAAE,SAAS,SAAS,UAAU,CAAC,IAClC,gBAAS,KAAK,QAAQ;AAGlC,QAAM,MAAM,gBAAgB,SAAS,KAAM,MAAc,GAAG;AAC5D,QAAM,aAAa,OAAO,aAAa;AACvC,SAAO,cAAc,SAAS,YAAkB,oBAAa,OAAO,EAAE,IAAI,CAAC,IAAI;AACjF;AAE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AAC/D,cAAM,qBAAqB,qBAAqB,SAAS,MAAM,aAAa;AAC5E,YAAI,MAAM,WAAW,QAAQ,oBAAoB;AAI/C,UAAS,mBAAU,MAAM,KAAK,eAAe,CAAC;AAAA,QAChD;AAAA,MACF;AACA,YAAM,uBAAuB,CAAC,UAA0B;AACtD,YAAI,MAAM,WAAW,MAAM;AAEzB,+BAAqB,UAAU,iBAAiB,UAAU,OAAO;AAAA,QACnE;AAAA,MACF;AACA,WAAK,iBAAiB,kBAAkB,oBAAoB;AAC5D,WAAK,iBAAiB,mBAAmB,kBAAkB;AAC3D,WAAK,iBAAiB,gBAAgB,kBAAkB;AACxD,aAAO,MAAM;AACX,aAAK,oBAAoB,kBAAkB,oBAAoB;AAC/D,aAAK,oBAAoB,mBAAmB,kBAAkB;AAC9D,aAAK,oBAAoB,gBAAgB,kBAAkB;AAAA,MAC7D;AAAA,IACF,OAAO;AAGL,WAAK,eAAe;AAAA,IACtB;AAAA,EACF,GAAG,CAAC,MAAM,IAAI,CAAC;AAEf,SAAO;AAAA,IACL,WAAW,CAAC,WAAW,kBAAkB,EAAE,SAAS,KAAK;AAAA,IACzD,KAAW,mBAAY,CAACC,UAAsB;AAC5C,UAAIA,MAAM,WAAU,UAAU,iBAAiBA,KAAI;AACnD,cAAQA,KAAI;AAAA,IACd,GAAG,CAAC,CAAC;AAAA,EACP;AACF;AAIA,SAAS,iBAAiB,QAA8B;AACtD,SAAO,QAAQ,iBAAiB;AAClC;",
6
+ "names": ["React", "node"]
7
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@radix-ui/react-presence",
3
- "version": "1.0.1",
3
+ "version": "1.1.0-rc.2",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": {
@@ -28,9 +28,8 @@
28
28
  "version": "yarn version"
29
29
  },
30
30
  "dependencies": {
31
- "@babel/runtime": "^7.13.10",
32
- "@radix-ui/react-compose-refs": "1.0.1",
33
- "@radix-ui/react-use-layout-effect": "1.0.1"
31
+ "@radix-ui/react-compose-refs": "1.1.0-rc.2",
32
+ "@radix-ui/react-use-layout-effect": "1.1.0-rc.2"
34
33
  },
35
34
  "peerDependencies": {
36
35
  "@types/react": "*",
@@ -53,5 +52,6 @@
53
52
  },
54
53
  "bugs": {
55
54
  "url": "https://github.com/radix-ui/primitives/issues"
56
- }
55
+ },
56
+ "stableVersion": "1.0.1"
57
57
  }
@@ -1 +0,0 @@
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 { Presence } from './Presence';\nexport type { PresenceProps } from './Presence';\n"],"names":[],"version":3,"file":"index.d.ts.map"}