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

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,140 @@
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
+ (() => {
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 __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
10
+ get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
11
+ }) : x)(function(x) {
12
+ if (typeof require !== "undefined") return require.apply(this, arguments);
13
+ throw Error('Dynamic require of "' + x + '" is not supported');
14
+ });
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from))
18
+ if (!__hasOwnProp.call(to, key) && key !== except)
19
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
20
+ }
21
+ return to;
22
+ };
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
+ // If the importer is in node compatibility mode or this is not an ESM
25
+ // file that has been converted to a CommonJS file using a Babel-
26
+ // compatible transform (i.e. "__esModule" has not been set), then set
27
+ // "default" to the CommonJS "module.exports" for node compatibility.
28
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
+ mod
30
+ ));
5
31
 
6
- function $parcel$export(e, n, v, s) {
7
- Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8
- }
32
+ // packages/react/presence/src/Presence.tsx
33
+ var React2 = __toESM(__require("react"));
34
+ var ReactDOM = __toESM(__require("react-dom"));
35
+ var import_react_compose_refs = __require("@radix-ui/react-compose-refs");
36
+ var import_react_use_layout_effect = __require("@radix-ui/react-use-layout-effect");
9
37
 
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;
38
+ // packages/react/presence/src/useStateMachine.tsx
39
+ var React = __toESM(__require("react"));
40
+ function useStateMachine(initialState, machine) {
41
+ return React.useReducer((state, event) => {
42
+ const nextState = machine[state][event];
43
+ return nextState ?? state;
20
44
  }, initialState);
21
- }
22
-
45
+ }
23
46
 
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
- }
47
+ // packages/react/presence/src/Presence.tsx
48
+ var Presence = (props) => {
49
+ const { present, children } = props;
50
+ const presence = usePresence(present);
51
+ const child = typeof children === "function" ? children({ present: presence.isPresent }) : React2.Children.only(children);
52
+ const ref = (0, import_react_compose_refs.useComposedRefs)(presence.ref, child.ref);
53
+ const forceMount = typeof children === "function";
54
+ return forceMount || presence.isPresent ? React2.cloneElement(child, { ref }) : null;
55
+ };
56
+ Presence.displayName = "Presence";
57
+ function usePresence(present) {
58
+ const [node, setNode] = React2.useState();
59
+ const stylesRef = React2.useRef({});
60
+ const prevPresentRef = React2.useRef(present);
61
+ const prevAnimationNameRef = React2.useRef("none");
62
+ const initialState = present ? "mounted" : "unmounted";
63
+ const [state, send] = useStateMachine(initialState, {
64
+ mounted: {
65
+ UNMOUNT: "unmounted",
66
+ ANIMATION_OUT: "unmountSuspended"
67
+ },
68
+ unmountSuspended: {
69
+ MOUNT: "mounted",
70
+ ANIMATION_END: "unmounted"
71
+ },
72
+ unmounted: {
73
+ MOUNT: "mounted"
74
+ }
57
75
  });
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;
76
+ React2.useEffect(() => {
77
+ const currentAnimationName = getAnimationName(stylesRef.current);
78
+ prevAnimationNameRef.current = state === "mounted" ? currentAnimationName : "none";
79
+ }, [state]);
80
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
81
+ const styles = stylesRef.current;
82
+ const wasPresent = prevPresentRef.current;
83
+ const hasPresentChanged = wasPresent !== present;
84
+ if (hasPresentChanged) {
85
+ const prevAnimationName = prevAnimationNameRef.current;
86
+ const currentAnimationName = getAnimationName(styles);
87
+ if (present) {
88
+ send("MOUNT");
89
+ } else if (currentAnimationName === "none" || styles?.display === "none") {
90
+ send("UNMOUNT");
91
+ } else {
92
+ const isAnimating = prevAnimationName !== currentAnimationName;
93
+ if (wasPresent && isAnimating) {
94
+ send("ANIMATION_OUT");
95
+ } else {
96
+ send("UNMOUNT");
97
+ }
86
98
  }
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
- ]);
99
+ prevPresentRef.current = present;
100
+ }
101
+ }, [present, send]);
102
+ (0, import_react_use_layout_effect.useLayoutEffect)(() => {
103
+ if (node) {
104
+ const handleAnimationEnd = (event) => {
105
+ const currentAnimationName = getAnimationName(stylesRef.current);
106
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
107
+ if (event.target === node && isCurrentAnimation) {
108
+ ReactDOM.flushSync(() => send("ANIMATION_END"));
109
+ }
110
+ };
111
+ const handleAnimationStart = (event) => {
112
+ if (event.target === node) {
113
+ prevAnimationNameRef.current = getAnimationName(stylesRef.current);
114
+ }
115
+ };
116
+ node.addEventListener("animationstart", handleAnimationStart);
117
+ node.addEventListener("animationcancel", handleAnimationEnd);
118
+ node.addEventListener("animationend", handleAnimationEnd);
119
+ return () => {
120
+ node.removeEventListener("animationstart", handleAnimationStart);
121
+ node.removeEventListener("animationcancel", handleAnimationEnd);
122
+ node.removeEventListener("animationend", handleAnimationEnd);
123
+ };
124
+ } else {
125
+ send("ANIMATION_END");
126
+ }
127
+ }, [node, send]);
125
128
  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
- }, [])
129
+ isPresent: ["mounted", "unmountSuspended"].includes(state),
130
+ ref: React2.useCallback((node2) => {
131
+ if (node2) stylesRef.current = getComputedStyle(node2);
132
+ setNode(node2);
133
+ }, [])
134
134
  };
135
- }
136
- /* -----------------------------------------------------------------------------------------------*/ function $a2fa0214bb2735a1$var$getAnimationName(styles) {
137
- return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
138
- }
139
-
140
-
141
-
142
-
135
+ }
136
+ function getAnimationName(styles) {
137
+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,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,uBAA6B,cAAe,M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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,111 @@
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
+ // packages/react/presence/src/Presence.tsx
2
+ import * as React2 from "react";
3
+ import * as ReactDOM from "react-dom";
4
+ import { useComposedRefs } from "@radix-ui/react-compose-refs";
5
+ import { useLayoutEffect } from "@radix-ui/react-use-layout-effect";
5
6
 
6
-
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);
7
+ // packages/react/presence/src/useStateMachine.tsx
8
+ import * as React from "react";
9
+ function useStateMachine(initialState, machine) {
10
+ return React.useReducer((state, event) => {
11
+ const nextState = machine[state][event];
12
+ return nextState ?? state;
13
+ }, initialState);
16
14
  }
17
15
 
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;
16
+ // packages/react/presence/src/Presence.tsx
17
+ var Presence = (props) => {
18
+ const { present, children } = props;
19
+ const presence = usePresence(present);
20
+ const child = typeof children === "function" ? children({ present: presence.isPresent }) : React2.Children.only(children);
21
+ const ref = useComposedRefs(presence.ref, child.ref);
22
+ const forceMount = typeof children === "function";
23
+ return forceMount || presence.isPresent ? React2.cloneElement(child, { ref }) : null;
30
24
  };
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'
25
+ Presence.displayName = "Presence";
26
+ function usePresence(present) {
27
+ const [node, setNode] = React2.useState();
28
+ const stylesRef = React2.useRef({});
29
+ const prevPresentRef = React2.useRef(present);
30
+ const prevAnimationNameRef = React2.useRef("none");
31
+ const initialState = present ? "mounted" : "unmounted";
32
+ const [state, send] = useStateMachine(initialState, {
33
+ mounted: {
34
+ UNMOUNT: "unmounted",
35
+ ANIMATION_OUT: "unmountSuspended"
36
+ },
37
+ unmountSuspended: {
38
+ MOUNT: "mounted",
39
+ ANIMATION_END: "unmounted"
40
+ },
41
+ unmounted: {
42
+ MOUNT: "mounted"
43
+ }
44
+ });
45
+ React2.useEffect(() => {
46
+ const currentAnimationName = getAnimationName(stylesRef.current);
47
+ prevAnimationNameRef.current = state === "mounted" ? currentAnimationName : "none";
48
+ }, [state]);
49
+ useLayoutEffect(() => {
50
+ const styles = stylesRef.current;
51
+ const wasPresent = prevPresentRef.current;
52
+ const hasPresentChanged = wasPresent !== present;
53
+ if (hasPresentChanged) {
54
+ const prevAnimationName = prevAnimationNameRef.current;
55
+ const currentAnimationName = getAnimationName(styles);
56
+ if (present) {
57
+ send("MOUNT");
58
+ } else if (currentAnimationName === "none" || styles?.display === "none") {
59
+ send("UNMOUNT");
60
+ } else {
61
+ const isAnimating = prevAnimationName !== currentAnimationName;
62
+ if (wasPresent && isAnimating) {
63
+ send("ANIMATION_OUT");
64
+ } else {
65
+ send("UNMOUNT");
51
66
  }
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;
67
+ }
68
+ prevPresentRef.current = present;
69
+ }
70
+ }, [present, send]);
71
+ useLayoutEffect(() => {
72
+ if (node) {
73
+ const handleAnimationEnd = (event) => {
74
+ const currentAnimationName = getAnimationName(stylesRef.current);
75
+ const isCurrentAnimation = currentAnimationName.includes(event.animationName);
76
+ if (event.target === node && isCurrentAnimation) {
77
+ ReactDOM.flushSync(() => send("ANIMATION_END"));
81
78
  }
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
- };
79
+ };
80
+ const handleAnimationStart = (event) => {
81
+ if (event.target === node) {
82
+ prevAnimationNameRef.current = getAnimationName(stylesRef.current);
83
+ }
84
+ };
85
+ node.addEventListener("animationstart", handleAnimationStart);
86
+ node.addEventListener("animationcancel", handleAnimationEnd);
87
+ node.addEventListener("animationend", handleAnimationEnd);
88
+ return () => {
89
+ node.removeEventListener("animationstart", handleAnimationStart);
90
+ node.removeEventListener("animationcancel", handleAnimationEnd);
91
+ node.removeEventListener("animationend", handleAnimationEnd);
92
+ };
93
+ } else {
94
+ send("ANIMATION_END");
95
+ }
96
+ }, [node, send]);
97
+ return {
98
+ isPresent: ["mounted", "unmountSuspended"].includes(state),
99
+ ref: React2.useCallback((node2) => {
100
+ if (node2) stylesRef.current = getComputedStyle(node2);
101
+ setNode(node2);
102
+ }, [])
103
+ };
130
104
  }
131
- /* -----------------------------------------------------------------------------------------------*/ function $921a889cee6df7e8$var$getAnimationName(styles) {
132
- return (styles === null || styles === void 0 ? void 0 : styles.animationName) || 'none';
105
+ function getAnimationName(styles) {
106
+ return styles?.animationName || "none";
133
107
  }
134
-
135
-
136
-
137
-
138
- export {$921a889cee6df7e8$export$99c2b779aa4e8b8b as Presence};
108
+ export {
109
+ Presence
110
+ };
139
111
  //# 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,OAAOQ,UAAU,IAAIP,QAAQ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;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;AAEA,SAAS,cAAc;AAMvB,SAAS,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B,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.1",
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.1",
32
+ "@radix-ui/react-use-layout-effect": "1.1.0-rc.1"
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"}