@rescui/collapse 0.8.4-RUI-297-Add-react-compiler-to-the-packages-build-f15074a13.11 → 0.8.5

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.
@@ -1,192 +1,117 @@
1
1
  "use client";
2
- import { c } from 'react-compiler-runtime';
3
- import { useReducer, useRef, useLayoutEffect } from 'react';
2
+ import { useState, useCallback, useEffect, useLayoutEffect } from 'react';
3
+ const useIsomorphicLayoutEffect = typeof window === 'undefined' ? useEffect : useLayoutEffect;
4
+
5
+ const getCollapseTransition = element => element.getAnimations().find(animation => animation.transitionProperty === 'grid-template-rows');
6
+
4
7
  var COLLAPSE_STATES;
5
8
 
6
9
  (function (COLLAPSE_STATES) {
7
10
  COLLAPSE_STATES["OPENING"] = "OPENING";
8
11
  COLLAPSE_STATES["OPEN"] = "OPEN";
9
- COLLAPSE_STATES["CLOSING_PREPARE"] = "CLOSING_PREPARE";
10
12
  COLLAPSE_STATES["CLOSING"] = "CLOSING";
11
13
  COLLAPSE_STATES["CLOSE"] = "CLOSE";
12
14
  })(COLLAPSE_STATES || (COLLAPSE_STATES = {}));
13
15
 
14
- function forceLayoutRecalculation(element) {
15
- 'use no memo'; // eslint-disable-next-line no-unused-expressions
16
-
17
- element.offsetHeight;
18
- }
19
-
20
- function getInitialState(_ref) {
21
- let {
22
- shouldBeOpened,
23
- cancelAnimation
24
- } = _ref;
25
-
26
- if (!shouldBeOpened) {
27
- return COLLAPSE_STATES.CLOSE;
28
- }
29
-
30
- return cancelAnimation ? COLLAPSE_STATES.OPEN : COLLAPSE_STATES.OPENING;
31
- }
32
-
33
- function getStateForTarget(state, shouldBeOpened, cancelAnimation) {
34
- if (cancelAnimation) {
35
- return shouldBeOpened ? COLLAPSE_STATES.OPEN : COLLAPSE_STATES.CLOSE;
36
- }
37
-
16
+ function getStateForTarget(state, shouldBeOpened) {
38
17
  if (shouldBeOpened) {
39
- // Closing has not started visually yet, so there is nothing to animate back.
40
- if (state === COLLAPSE_STATES.CLOSING_PREPARE) {
41
- return COLLAPSE_STATES.OPEN;
42
- }
43
-
44
18
  return state === COLLAPSE_STATES.OPEN || state === COLLAPSE_STATES.OPENING ? state : COLLAPSE_STATES.OPENING;
45
19
  }
46
20
 
47
- if (state === COLLAPSE_STATES.CLOSE || state === COLLAPSE_STATES.CLOSING_PREPARE || state === COLLAPSE_STATES.CLOSING) {
21
+ if (state === COLLAPSE_STATES.CLOSE || state === COLLAPSE_STATES.CLOSING) {
48
22
  return state;
49
23
  }
50
24
 
51
- return state === COLLAPSE_STATES.OPEN ? COLLAPSE_STATES.CLOSING_PREPARE : COLLAPSE_STATES.CLOSING;
25
+ return COLLAPSE_STATES.CLOSING;
52
26
  }
53
27
 
54
- function collapseReducer(state, action) {
55
- switch (action.type) {
56
- case 'TARGET_CHANGED':
57
- return getStateForTarget(state, action.shouldBeOpened, action.cancelAnimation);
58
-
59
- case 'CLOSING_READY':
60
- return state === COLLAPSE_STATES.CLOSING_PREPARE ? COLLAPSE_STATES.CLOSING : state;
28
+ function useCollapseState(_ref) {
29
+ let {
30
+ shouldBeOpened,
31
+ contentRef
32
+ } = _ref;
33
+ const [state, setState] = useState(shouldBeOpened ? COLLAPSE_STATES.OPEN : COLLAPSE_STATES.CLOSE);
34
+ const completeTransition = useCallback(expectedState => {
35
+ setState(currentState => {
36
+ if (currentState !== expectedState) {
37
+ return currentState;
38
+ }
61
39
 
62
- case 'TRANSITION_END':
63
- if (action.shouldBeOpened && state === COLLAPSE_STATES.OPENING) {
40
+ if (expectedState === COLLAPSE_STATES.OPENING) {
64
41
  return COLLAPSE_STATES.OPEN;
65
42
  }
66
43
 
67
- if (!action.shouldBeOpened && state === COLLAPSE_STATES.CLOSING) {
44
+ if (expectedState === COLLAPSE_STATES.CLOSING) {
68
45
  return COLLAPSE_STATES.CLOSE;
69
46
  }
70
47
 
71
- return state;
48
+ return currentState;
49
+ });
50
+ }, []); // Track the grid track transition that currently belongs to OPENING/CLOSING:
51
+ // - normally, complete the phase after the track reaches its target;
52
+ // - a direction change can replace the transition, so follow the latest one;
53
+ // - if the phase changes, cleanup invalidates its waiter, so an old
54
+ // transition cannot complete a newer phase;
55
+ // - if no transition is created (for example, its duration is 0s), complete
56
+ // the phase immediately.
57
+
58
+ useIsomorphicLayoutEffect(() => {
59
+ if (state !== COLLAPSE_STATES.OPENING && state !== COLLAPSE_STATES.CLOSING) {
60
+ return undefined;
61
+ }
72
62
 
73
- default:
74
- return state;
75
- }
76
- }
63
+ const contentElement = contentRef.current;
77
64
 
78
- function useCollapseState(t0) {
79
- const $ = c(16);
80
- const {
81
- shouldBeOpened,
82
- cancelAnimation
83
- } = t0;
84
- let t1;
85
-
86
- if ($[0] !== cancelAnimation || $[1] !== shouldBeOpened) {
87
- t1 = {
88
- shouldBeOpened,
89
- cancelAnimation
90
- };
91
- $[0] = cancelAnimation;
92
- $[1] = shouldBeOpened;
93
- $[2] = t1;
94
- } else {
95
- t1 = $[2];
96
- }
65
+ if (!contentElement) {
66
+ return undefined;
67
+ }
97
68
 
98
- const [state, dispatch] = useReducer(collapseReducer, t1, getInitialState);
99
- const contentRef = useRef(null);
100
- let t2;
101
- let t3;
102
-
103
- if ($[3] !== cancelAnimation || $[4] !== shouldBeOpened) {
104
- t2 = () => {
105
- dispatch({
106
- type: "TARGET_CHANGED",
107
- shouldBeOpened,
108
- cancelAnimation
109
- });
69
+ let isCurrentPhase = true;
70
+
71
+ const completeCurrentPhase = () => {
72
+ if (isCurrentPhase) {
73
+ completeTransition(state);
74
+ }
110
75
  };
111
76
 
112
- t3 = [shouldBeOpened, cancelAnimation];
113
- $[3] = cancelAnimation;
114
- $[4] = shouldBeOpened;
115
- $[5] = t2;
116
- $[6] = t3;
117
- } else {
118
- t2 = $[5];
119
- t3 = $[6];
120
- }
77
+ const waitForCurrentTransition = previousTransition => {
78
+ if (!isCurrentPhase) {
79
+ return Promise.resolve();
80
+ }
121
81
 
122
- useLayoutEffect(t2, t3);
123
- let t4;
124
- let t5;
82
+ const transition = getCollapseTransition(contentElement);
125
83
 
126
- if ($[7] !== state) {
127
- t4 = () => {
128
- if (state !== COLLAPSE_STATES.CLOSING_PREPARE) {
129
- return;
84
+ if (!transition || transition === previousTransition) {
85
+ return Promise.resolve();
130
86
  }
131
87
 
132
- const frame = requestAnimationFrame(() => {
133
- if (contentRef.current) {
134
- forceLayoutRecalculation(contentRef.current);
88
+ return transition.finished.then(() => {
89
+ if (!isCurrentPhase) {
90
+ return Promise.resolve();
135
91
  }
136
92
 
137
- dispatch({
138
- type: "CLOSING_READY"
139
- });
140
- });
141
- return () => cancelAnimationFrame(frame);
142
- };
143
-
144
- t5 = [state];
145
- $[7] = state;
146
- $[8] = t4;
147
- $[9] = t5;
148
- } else {
149
- t4 = $[8];
150
- t5 = $[9];
151
- }
93
+ const replacementTransition = getCollapseTransition(contentElement);
152
94
 
153
- useLayoutEffect(t4, t5);
154
- let t6;
95
+ if (replacementTransition && replacementTransition !== transition) {
96
+ return waitForCurrentTransition(transition);
97
+ }
155
98
 
156
- if ($[10] !== shouldBeOpened) {
157
- t6 = () => {
158
- dispatch({
159
- type: "TRANSITION_END",
160
- shouldBeOpened
161
- });
99
+ return Promise.resolve();
100
+ }, () => waitForCurrentTransition(transition));
162
101
  };
163
102
 
164
- $[10] = shouldBeOpened;
165
- $[11] = t6;
166
- } else {
167
- t6 = $[11];
168
- }
169
-
170
- const onAnimationEnd = t6;
171
- const t7 = state === COLLAPSE_STATES.CLOSING_PREPARE;
172
- let t8;
173
-
174
- if ($[12] !== onAnimationEnd || $[13] !== state || $[14] !== t7) {
175
- t8 = {
176
- contentRef,
177
- isStartedClosing: t7,
178
- onAnimationEnd,
179
- state
103
+ waitForCurrentTransition().then(completeCurrentPhase);
104
+ return () => {
105
+ isCurrentPhase = false;
180
106
  };
181
- $[12] = onAnimationEnd;
182
- $[13] = state;
183
- $[14] = t7;
184
- $[15] = t8;
185
- } else {
186
- t8 = $[15];
107
+ }, [completeTransition, contentRef, state]);
108
+ const stateForTarget = getStateForTarget(state, shouldBeOpened); // React retries this component before rendering its children when the phase changes.
109
+
110
+ if (stateForTarget !== state) {
111
+ setState(stateForTarget);
187
112
  }
188
113
 
189
- return t8;
114
+ return state;
190
115
  }
191
116
 
192
117
  export { COLLAPSE_STATES, useCollapseState };
@@ -1,3 +1,4 @@
1
1
  import React from 'react';
2
- export declare function useFallbackId(): number;
2
+ declare function useFallbackId(): number;
3
3
  export declare const useSafeId: typeof React.useId | typeof useFallbackId;
4
+ export {};
@@ -7,4 +7,4 @@ function useFallbackId() {
7
7
  }
8
8
 
9
9
  const useSafeId = React.useId ?? useFallbackId;
10
- export { useFallbackId, useSafeId };
10
+ export { useSafeId };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rescui/collapse",
3
- "version": "0.8.4-RUI-297-Add-react-compiler-to-the-packages-build-f15074a13.11+f15074a13",
3
+ "version": "0.8.5",
4
4
  "description": "",
5
5
  "license": "Apache-2.0",
6
6
  "author": "JetBrains",
@@ -17,26 +17,24 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "@babel/runtime-corejs3": "^7.26.0",
20
- "@rescui/use-resize-observer": "0.3.4-RUI-297-Add-react-compiler-to-the-packages-build-f15074a13.11+f15074a13",
21
- "classnames": "^2.2.6",
22
- "react-compiler-runtime": "^1.0.0"
20
+ "classnames": "^2.2.6"
23
21
  },
24
22
  "peerDependencies": {
25
23
  "@rescui/ui-contexts": "^0.6.0",
26
24
  "prop-types": "^15",
27
- "react": ">=17.0.0 <20"
25
+ "react": ">=16.8.0 <20"
28
26
  },
29
27
  "devDependencies": {
30
- "@rescui/colors": "0.3.3-RUI-297-Add-react-compiler-to-the-packages-build-f15074a13.15+f15074a13",
31
- "@rescui/scripts": "0.5.2",
32
- "@rescui/typography": "0.27.3-RUI-297-Add-react-compiler-to-the-packages-build-f15074a13.15+f15074a13",
33
- "@rescui/visual-regression": "0.1.4",
28
+ "@rescui/colors": "^0.3.3",
29
+ "@rescui/scripts": "^0.5.2",
30
+ "@rescui/typography": "^0.27.4",
31
+ "@rescui/visual-regression": "^0.1.4",
34
32
  "@types/classnames": "^2.2.11"
35
33
  },
36
34
  "scripts": {
37
- "build": "rescui-scripts build --react-compiler-target 17 && yarn run build-pcss-api",
35
+ "build": "rescui-scripts build && yarn run build-pcss-api",
38
36
  "build-pcss-api": "rescui-scripts build-pcss-api --file public-api.p.css"
39
37
  },
40
38
  "nx": {},
41
- "gitHead": "f15074a13a93f4cdfe7b9ac295f0a493e4a0ca0f"
39
+ "gitHead": "3711cbfc0c797014395421b008aa5331d6d47c6e"
42
40
  }