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

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.js CHANGED
@@ -1,141 +1,145 @@
1
1
  "use strict";
2
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
- ));
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
30
 
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");
31
+ // packages/react/presence/src/index.ts
32
+ var src_exports = {};
33
+ __export(src_exports, {
34
+ Presence: () => Presence
35
+ });
36
+ module.exports = __toCommonJS(src_exports);
38
37
 
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;
45
- }, initialState);
46
- }
38
+ // packages/react/presence/src/Presence.tsx
39
+ var React2 = __toESM(require("react"));
40
+ var ReactDOM = __toESM(require("react-dom"));
41
+ var import_react_compose_refs = require("@radix-ui/react-compose-refs");
42
+ var import_react_use_layout_effect = require("@radix-ui/react-use-layout-effect");
47
43
 
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
- }
76
- });
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");
44
+ // packages/react/presence/src/useStateMachine.tsx
45
+ var React = __toESM(require("react"));
46
+ function useStateMachine(initialState, machine) {
47
+ return React.useReducer((state, event) => {
48
+ const nextState = machine[state][event];
49
+ return nextState ?? state;
50
+ }, initialState);
51
+ }
52
+
53
+ // packages/react/presence/src/Presence.tsx
54
+ var Presence = (props) => {
55
+ const { present, children } = props;
56
+ const presence = usePresence(present);
57
+ const child = typeof children === "function" ? children({ present: presence.isPresent }) : React2.Children.only(children);
58
+ const ref = (0, import_react_compose_refs.useComposedRefs)(presence.ref, child.ref);
59
+ const forceMount = typeof children === "function";
60
+ return forceMount || presence.isPresent ? React2.cloneElement(child, { ref }) : null;
61
+ };
62
+ Presence.displayName = "Presence";
63
+ function usePresence(present) {
64
+ const [node, setNode] = React2.useState();
65
+ const stylesRef = React2.useRef({});
66
+ const prevPresentRef = React2.useRef(present);
67
+ const prevAnimationNameRef = React2.useRef("none");
68
+ const initialState = present ? "mounted" : "unmounted";
69
+ const [state, send] = useStateMachine(initialState, {
70
+ mounted: {
71
+ UNMOUNT: "unmounted",
72
+ ANIMATION_OUT: "unmountSuspended"
73
+ },
74
+ unmountSuspended: {
75
+ MOUNT: "mounted",
76
+ ANIMATION_END: "unmounted"
77
+ },
78
+ unmounted: {
79
+ MOUNT: "mounted"
80
+ }
81
+ });
82
+ React2.useEffect(() => {
83
+ const currentAnimationName = getAnimationName(stylesRef.current);
84
+ prevAnimationNameRef.current = state === "mounted" ? currentAnimationName : "none";
85
+ }, [state]);
86
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
87
+ const styles = stylesRef.current;
88
+ const wasPresent = prevPresentRef.current;
89
+ const hasPresentChanged = wasPresent !== present;
90
+ if (hasPresentChanged) {
91
+ const prevAnimationName = prevAnimationNameRef.current;
92
+ const currentAnimationName = getAnimationName(styles);
93
+ if (present) {
94
+ send("MOUNT");
95
+ } else if (currentAnimationName === "none" || styles?.display === "none") {
96
+ send("UNMOUNT");
97
+ } else {
98
+ const isAnimating = prevAnimationName !== currentAnimationName;
99
+ if (wasPresent && isAnimating) {
100
+ send("ANIMATION_OUT");
92
101
  } else {
93
- const isAnimating = prevAnimationName !== currentAnimationName;
94
- if (wasPresent && isAnimating) {
95
- send("ANIMATION_OUT");
96
- } else {
97
- send("UNMOUNT");
98
- }
102
+ send("UNMOUNT");
99
103
  }
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
104
  }
128
- }, [node, send]);
129
- return {
130
- isPresent: ["mounted", "unmountSuspended"].includes(state),
131
- ref: React2.useCallback((node2) => {
132
- if (node2) stylesRef.current = getComputedStyle(node2);
133
- setNode(node2);
134
- }, [])
135
- };
136
- }
137
- function getAnimationName(styles) {
138
- return styles?.animationName || "none";
139
- }
140
- })();
105
+ prevPresentRef.current = present;
106
+ }
107
+ }, [present, send]);
108
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
109
+ if (node) {
110
+ const handleAnimationEnd = (event) => {
111
+ const currentAnimationName = getAnimationName(stylesRef.current);
112
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
113
+ if (event.target === node && isCurrentAnimation) {
114
+ ReactDOM.flushSync(() => send("ANIMATION_END"));
115
+ }
116
+ };
117
+ const handleAnimationStart = (event) => {
118
+ if (event.target === node) {
119
+ prevAnimationNameRef.current = getAnimationName(stylesRef.current);
120
+ }
121
+ };
122
+ node.addEventListener("animationstart", handleAnimationStart);
123
+ node.addEventListener("animationcancel", handleAnimationEnd);
124
+ node.addEventListener("animationend", handleAnimationEnd);
125
+ return () => {
126
+ node.removeEventListener("animationstart", handleAnimationStart);
127
+ node.removeEventListener("animationcancel", handleAnimationEnd);
128
+ node.removeEventListener("animationend", handleAnimationEnd);
129
+ };
130
+ } else {
131
+ send("ANIMATION_END");
132
+ }
133
+ }, [node, send]);
134
+ return {
135
+ isPresent: ["mounted", "unmountSuspended"].includes(state),
136
+ ref: React2.useCallback((node2) => {
137
+ if (node2) stylesRef.current = getComputedStyle(node2);
138
+ setNode(node2);
139
+ }, [])
140
+ };
141
+ }
142
+ function getAnimationName(styles) {
143
+ return styles?.animationName || "none";
144
+ }
141
145
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
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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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;",
3
+ "sources": ["../src/index.ts", "../src/Presence.tsx", "../src/useStateMachine.tsx"],
4
+ "sourcesContent": ["'use client';\nexport { 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// \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;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,SAAuB;AACvB,eAA0B;AAC1B,gCAAgC;AAChC,qCAAgC;;;ACHhC,YAAuB;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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name": "@radix-ui/react-presence",
3
- "version": "1.1.0-rc.2",
3
+ "version": "1.1.0-rc.3",
4
4
  "license": "MIT",
5
5
  "exports": {
6
6
  ".": {
@@ -28,8 +28,8 @@
28
28
  "version": "yarn version"
29
29
  },
30
30
  "dependencies": {
31
- "@radix-ui/react-compose-refs": "1.1.0-rc.2",
32
- "@radix-ui/react-use-layout-effect": "1.1.0-rc.2"
31
+ "@radix-ui/react-compose-refs": "1.1.0-rc.3",
32
+ "@radix-ui/react-use-layout-effect": "1.1.0-rc.3"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "@types/react": "*",