@uxf/core-react 14.0.0 → 14.1.0
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/README.md +5 -3
- package/clickable/use-button-props.js +108 -47
- package/global-context/index.test.js +23 -2
- package/hooks/use-body-scroll-lock.js +56 -27
- package/hooks/use-composed-refs.test.js +14 -2
- package/hooks/use-debounce.js +21 -8
- package/hooks/use-debounce.test.js +15 -3
- package/hooks/use-focus-return.js +55 -24
- package/hooks/use-focus-trap.js +87 -52
- package/hooks/use-increment.js +47 -5
- package/hooks/use-input-focus.js +65 -19
- package/hooks/use-is-hydrated.js +7 -1
- package/hooks/use-is-hydrated.test.js +13 -1
- package/hooks/use-is-mounted.js +15 -1
- package/hooks/use-key.js +53 -23
- package/hooks/use-key.test.js +88 -8
- package/hooks/use-media-query.js +77 -18
- package/hooks/use-media-query.test.js +29 -8
- package/hooks/use-min-window-width.js +39 -19
- package/hooks/use-mouse-drag-to-scroll.js +116 -61
- package/hooks/use-mouse-drag-to-scroll.test.js +56 -10
- package/hooks/use-on-mount.js +20 -3
- package/hooks/use-on-unmount.js +22 -3
- package/hooks/use-on-update.js +20 -8
- package/hooks/use-on-update.test.js +17 -2
- package/hooks/use-pagination.js +30 -14
- package/hooks/use-previous.js +14 -3
- package/hooks/use-raf-state.js +35 -8
- package/hooks/use-toggle.js +42 -4
- package/hooks/use-window-scroll.d.ts +7 -2
- package/hooks/use-window-scroll.js +36 -13
- package/hooks/use-window-scroll.test.js +141 -41
- package/hooks/use-window-size.d.ts +7 -2
- package/hooks/use-window-size.js +34 -13
- package/hooks/use-window-size.test.js +147 -31
- package/package.json +4 -4
- package/swipeable/use-swipeable.test.js +31 -5
- package/translations/index.js +1 -1
|
@@ -33,14 +33,29 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
36
37
|
const react_1 = require("@testing-library/react");
|
|
37
38
|
const react_2 = __importStar(require("react"));
|
|
38
39
|
const use_on_update_1 = require("./use-on-update");
|
|
39
40
|
function Probe(props) {
|
|
41
|
+
const $ = (0, compiler_runtime_1.c)(4);
|
|
40
42
|
const effect = props.effect;
|
|
41
43
|
const value = props.value;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
let t0;
|
|
45
|
+
let t1;
|
|
46
|
+
if ($[0] !== effect || $[1] !== value) {
|
|
47
|
+
t0 = () => effect(value);
|
|
48
|
+
t1 = [effect, value];
|
|
49
|
+
$[0] = effect;
|
|
50
|
+
$[1] = value;
|
|
51
|
+
$[2] = t0;
|
|
52
|
+
$[3] = t1;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
t0 = $[2];
|
|
56
|
+
t1 = $[3];
|
|
57
|
+
}
|
|
58
|
+
(0, use_on_update_1.useOnUpdate)(t0, t1);
|
|
44
59
|
return null;
|
|
45
60
|
}
|
|
46
61
|
test("does not run on mount", () => {
|
package/hooks/use-pagination.js
CHANGED
|
@@ -1,20 +1,36 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.usePagination = void 0;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const pagination_1 = require("@uxf/core/utils/pagination");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
[
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
6
|
+
const usePagination = config => {
|
|
7
|
+
const $ = (0, compiler_runtime_1.c)(9);
|
|
8
|
+
const { boundaryCount, siblingCount, count, page, hideNextButton, hidePrevButton, showFirstButton, showLastButton } = config;
|
|
9
|
+
let t0;
|
|
10
|
+
if ($[0] !== boundaryCount || $[1] !== count || $[2] !== hideNextButton || $[3] !== hidePrevButton || $[4] !== page || $[5] !== showFirstButton || $[6] !== showLastButton || $[7] !== siblingCount) {
|
|
11
|
+
t0 = (0, pagination_1.paginationGenerate)({
|
|
12
|
+
boundaryCount,
|
|
13
|
+
siblingCount,
|
|
14
|
+
count,
|
|
15
|
+
page,
|
|
16
|
+
hideNextButton,
|
|
17
|
+
hidePrevButton,
|
|
18
|
+
showFirstButton,
|
|
19
|
+
showLastButton
|
|
20
|
+
});
|
|
21
|
+
$[0] = boundaryCount;
|
|
22
|
+
$[1] = count;
|
|
23
|
+
$[2] = hideNextButton;
|
|
24
|
+
$[3] = hidePrevButton;
|
|
25
|
+
$[4] = page;
|
|
26
|
+
$[5] = showFirstButton;
|
|
27
|
+
$[6] = showLastButton;
|
|
28
|
+
$[7] = siblingCount;
|
|
29
|
+
$[8] = t0;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
t0 = $[8];
|
|
33
|
+
}
|
|
34
|
+
return t0;
|
|
19
35
|
};
|
|
20
36
|
exports.usePagination = usePagination;
|
package/hooks/use-previous.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.usePrevious = usePrevious;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
6
|
/**
|
|
6
7
|
* @deprecated Returns a ref whose only use is reading `.current` during render, which React 19 asks not to do.
|
|
@@ -8,9 +9,19 @@ const react_1 = require("react");
|
|
|
8
9
|
* (https://react.dev/reference/react/useState#storing-information-from-previous-renders).
|
|
9
10
|
*/
|
|
10
11
|
function usePrevious(current) {
|
|
12
|
+
const $ = (0, compiler_runtime_1.c)(2);
|
|
11
13
|
const storedValue = (0, react_1.useRef)(current);
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
let t0;
|
|
15
|
+
if ($[0] !== current) {
|
|
16
|
+
t0 = () => {
|
|
17
|
+
storedValue.current = current;
|
|
18
|
+
};
|
|
19
|
+
$[0] = current;
|
|
20
|
+
$[1] = t0;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
t0 = $[1];
|
|
24
|
+
}
|
|
25
|
+
(0, react_1.useEffect)(t0);
|
|
15
26
|
return storedValue;
|
|
16
27
|
}
|
package/hooks/use-raf-state.js
CHANGED
|
@@ -1,17 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useRafState = useRafState;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
6
|
const use_on_unmount_1 = require("./use-on-unmount");
|
|
6
7
|
function useRafState(initialState) {
|
|
8
|
+
const $ = (0, compiler_runtime_1.c)(4);
|
|
7
9
|
const frame = (0, react_1.useRef)(0);
|
|
8
10
|
const [state, setState] = (0, react_1.useState)(initialState);
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
11
|
+
let t0;
|
|
12
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
13
|
+
t0 = value => {
|
|
14
|
+
cancelAnimationFrame(frame.current);
|
|
15
|
+
frame.current = requestAnimationFrame(() => {
|
|
16
|
+
setState(value);
|
|
17
|
+
});
|
|
18
|
+
};
|
|
19
|
+
$[0] = t0;
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
t0 = $[0];
|
|
23
|
+
}
|
|
24
|
+
const setRafState = t0;
|
|
25
|
+
let t1;
|
|
26
|
+
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
|
|
27
|
+
t1 = () => cancelAnimationFrame(frame.current);
|
|
28
|
+
$[1] = t1;
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
t1 = $[1];
|
|
32
|
+
}
|
|
33
|
+
(0, use_on_unmount_1.useOnUnmount)(t1);
|
|
34
|
+
let t2;
|
|
35
|
+
if ($[2] !== state) {
|
|
36
|
+
t2 = [state, setRafState];
|
|
37
|
+
$[2] = state;
|
|
38
|
+
$[3] = t2;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
t2 = $[3];
|
|
42
|
+
}
|
|
43
|
+
return t2;
|
|
17
44
|
}
|
package/hooks/use-toggle.js
CHANGED
|
@@ -1,11 +1,49 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useToggle = useToggle;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
6
|
function useToggle(defaultValue) {
|
|
7
|
+
const $ = (0, compiler_runtime_1.c)(5);
|
|
6
8
|
const [isOn, setIsOn] = (0, react_1.useState)(defaultValue);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
let t0;
|
|
10
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
11
|
+
t0 = () => setIsOn(_temp);
|
|
12
|
+
$[0] = t0;
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
t0 = $[0];
|
|
16
|
+
}
|
|
17
|
+
const toggleHandler = t0;
|
|
18
|
+
let t1;
|
|
19
|
+
if ($[1] === Symbol.for("react.memo_cache_sentinel")) {
|
|
20
|
+
t1 = () => setIsOn(true);
|
|
21
|
+
$[1] = t1;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
t1 = $[1];
|
|
25
|
+
}
|
|
26
|
+
const setTrueHandler = t1;
|
|
27
|
+
let t2;
|
|
28
|
+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
|
|
29
|
+
t2 = () => setIsOn(false);
|
|
30
|
+
$[2] = t2;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
t2 = $[2];
|
|
34
|
+
}
|
|
35
|
+
const setFalseHandler = t2;
|
|
36
|
+
let t3;
|
|
37
|
+
if ($[3] !== isOn) {
|
|
38
|
+
t3 = [isOn, toggleHandler, setTrueHandler, setFalseHandler];
|
|
39
|
+
$[3] = isOn;
|
|
40
|
+
$[4] = t3;
|
|
41
|
+
}
|
|
42
|
+
else {
|
|
43
|
+
t3 = $[4];
|
|
44
|
+
}
|
|
45
|
+
return t3;
|
|
46
|
+
}
|
|
47
|
+
function _temp(prev) {
|
|
48
|
+
return !prev;
|
|
11
49
|
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface WindowScroll {
|
|
2
2
|
x: number;
|
|
3
3
|
y: number;
|
|
4
|
-
}
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The window's scroll position. `undefined` on the server and while hydrating, the current position from the first
|
|
7
|
+
* render of a component mounted later.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useWindowScroll(): WindowScroll | undefined;
|
|
@@ -2,18 +2,41 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useWindowScroll = useWindowScroll;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(0
|
|
9
|
-
|
|
5
|
+
let lastScroll;
|
|
6
|
+
// The same object while the position stays the same, as useSyncExternalStore requires.
|
|
7
|
+
function getSnapshot() {
|
|
8
|
+
if ((lastScroll === null || lastScroll === void 0 ? void 0 : lastScroll.x) !== window.scrollX || lastScroll.y !== window.scrollY) {
|
|
9
|
+
lastScroll = {
|
|
10
10
|
x: window.scrollX,
|
|
11
|
-
y: window.scrollY
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
y: window.scrollY
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
return lastScroll;
|
|
15
|
+
}
|
|
16
|
+
function getServerSnapshot() {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
// At most one update per animation frame, however often `scroll` fires.
|
|
20
|
+
function subscribe(onChange) {
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
let frame = 0;
|
|
23
|
+
window.addEventListener("scroll", () => {
|
|
24
|
+
cancelAnimationFrame(frame);
|
|
25
|
+
frame = requestAnimationFrame(onChange);
|
|
26
|
+
}, {
|
|
27
|
+
capture: false,
|
|
28
|
+
passive: true,
|
|
29
|
+
signal: controller.signal
|
|
30
|
+
});
|
|
31
|
+
return () => {
|
|
32
|
+
cancelAnimationFrame(frame);
|
|
33
|
+
controller.abort();
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* The window's scroll position. `undefined` on the server and while hydrating, the current position from the first
|
|
38
|
+
* render of a component mounted later.
|
|
39
|
+
*/
|
|
40
|
+
function useWindowScroll() {
|
|
41
|
+
return (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
|
|
19
42
|
}
|
|
@@ -1,51 +1,151 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
3
37
|
const react_1 = require("@testing-library/react");
|
|
38
|
+
const react_2 = __importStar(require("react"));
|
|
39
|
+
const client_1 = require("react-dom/client");
|
|
40
|
+
const server_1 = require("react-dom/server");
|
|
4
41
|
const use_window_scroll_1 = require("./use-window-scroll");
|
|
5
|
-
|
|
6
|
-
window
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (event === "scroll") {
|
|
10
|
-
window.scrollHandler = callback;
|
|
11
|
-
}
|
|
42
|
+
function setWindowScroll(x, y) {
|
|
43
|
+
Object.defineProperty(window, "scrollX", {
|
|
44
|
+
configurable: true,
|
|
45
|
+
value: x
|
|
12
46
|
});
|
|
13
|
-
window
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
state.x = newState.x;
|
|
22
|
-
state.y = newState.y;
|
|
23
|
-
});
|
|
24
|
-
return [state, setState];
|
|
25
|
-
}),
|
|
26
|
-
}));
|
|
27
|
-
afterAll(() => {
|
|
28
|
-
jest.clearAllMocks();
|
|
47
|
+
Object.defineProperty(window, "scrollY", {
|
|
48
|
+
configurable: true,
|
|
49
|
+
value: y
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
jest.useFakeTimers();
|
|
54
|
+
setWindowScroll(0, 120);
|
|
29
55
|
});
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
jest.useRealTimers();
|
|
58
|
+
});
|
|
59
|
+
test("returns the scroll position from the first render", () => {
|
|
60
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_window_scroll_1.useWindowScroll)());
|
|
61
|
+
expect(result.current).toEqual({
|
|
62
|
+
x: 0,
|
|
63
|
+
y: 120
|
|
34
64
|
});
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
expect(result.current).toEqual({ x: 100, y: 200 });
|
|
65
|
+
});
|
|
66
|
+
test("follows a scroll after the next animation frame", () => {
|
|
67
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_window_scroll_1.useWindowScroll)());
|
|
68
|
+
(0, react_1.act)(() => {
|
|
69
|
+
setWindowScroll(10, 400);
|
|
70
|
+
window.dispatchEvent(new Event("scroll"));
|
|
71
|
+
jest.advanceTimersByTime(50);
|
|
43
72
|
});
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
73
|
+
expect(result.current).toEqual({
|
|
74
|
+
x: 10,
|
|
75
|
+
y: 400
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
test("listens passively and stops on unmount", () => {
|
|
79
|
+
var _a, _b;
|
|
80
|
+
const addEventListener = jest.spyOn(window, "addEventListener");
|
|
81
|
+
const { unmount } = (0, react_1.renderHook)(() => (0, use_window_scroll_1.useWindowScroll)());
|
|
82
|
+
unmount();
|
|
83
|
+
const listenerOptions = (_a = addEventListener.mock.calls.find(([type]) => type === "scroll")) === null || _a === void 0 ? void 0 : _a[2];
|
|
84
|
+
expect(listenerOptions === null || listenerOptions === void 0 ? void 0 : listenerOptions.passive).toBe(true);
|
|
85
|
+
expect((_b = listenerOptions === null || listenerOptions === void 0 ? void 0 : listenerOptions.signal) === null || _b === void 0 ? void 0 : _b.aborted).toBe(true);
|
|
86
|
+
addEventListener.mockRestore();
|
|
87
|
+
});
|
|
88
|
+
test("renders without a position on the server", () => {
|
|
89
|
+
function Probe() {
|
|
90
|
+
const $ = (0, compiler_runtime_1.c)(4);
|
|
91
|
+
const value = (0, use_window_scroll_1.useWindowScroll)();
|
|
92
|
+
let t0;
|
|
93
|
+
if ($[0] !== value) {
|
|
94
|
+
t0 = value ? JSON.stringify(value) : "none";
|
|
95
|
+
$[0] = value;
|
|
96
|
+
$[1] = t0;
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
t0 = $[1];
|
|
100
|
+
}
|
|
101
|
+
let t1;
|
|
102
|
+
if ($[2] !== t0) {
|
|
103
|
+
t1 = react_2.default.createElement("span", null, t0);
|
|
104
|
+
$[2] = t0;
|
|
105
|
+
$[3] = t1;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
t1 = $[3];
|
|
109
|
+
}
|
|
110
|
+
return t1;
|
|
111
|
+
}
|
|
112
|
+
expect((0, server_1.renderToString)(react_2.default.createElement(Probe, null))).toBe("<span>none</span>");
|
|
113
|
+
});
|
|
114
|
+
test("hydrates without a mismatch and shows the client value after hydration", () => {
|
|
115
|
+
function Probe() {
|
|
116
|
+
const $ = (0, compiler_runtime_1.c)(4);
|
|
117
|
+
const value = (0, use_window_scroll_1.useWindowScroll)();
|
|
118
|
+
let t0;
|
|
119
|
+
if ($[0] !== value) {
|
|
120
|
+
t0 = value ? JSON.stringify(value) : "none";
|
|
121
|
+
$[0] = value;
|
|
122
|
+
$[1] = t0;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
t0 = $[1];
|
|
126
|
+
}
|
|
127
|
+
let t1;
|
|
128
|
+
if ($[2] !== t0) {
|
|
129
|
+
t1 = react_2.default.createElement("span", null, t0);
|
|
130
|
+
$[2] = t0;
|
|
131
|
+
$[3] = t1;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
t1 = $[3];
|
|
135
|
+
}
|
|
136
|
+
return t1;
|
|
137
|
+
}
|
|
138
|
+
const container = document.createElement("div");
|
|
139
|
+
container.innerHTML = (0, server_1.renderToString)(react_2.default.createElement(Probe, null));
|
|
140
|
+
document.body.append(container);
|
|
141
|
+
const onRecoverableError = jest.fn();
|
|
142
|
+
(0, react_1.act)(() => {
|
|
143
|
+
(0, client_1.hydrateRoot)(container, react_2.default.createElement(react_2.StrictMode, null,
|
|
144
|
+
react_2.default.createElement(Probe, null)), {
|
|
145
|
+
onRecoverableError
|
|
146
|
+
});
|
|
50
147
|
});
|
|
148
|
+
expect(onRecoverableError).not.toHaveBeenCalled();
|
|
149
|
+
expect(container.textContent).toBe('{"x":0,"y":120}');
|
|
150
|
+
container.remove();
|
|
51
151
|
});
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface WindowSize {
|
|
2
2
|
width: number;
|
|
3
3
|
height: number;
|
|
4
|
-
}
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* The window's inner size. `undefined` on the server and while hydrating, the current size from the first render of
|
|
7
|
+
* a component mounted later.
|
|
8
|
+
*/
|
|
9
|
+
export declare function useWindowSize(): WindowSize | undefined;
|
package/hooks/use-window-size.js
CHANGED
|
@@ -2,18 +2,39 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useWindowSize = useWindowSize;
|
|
4
4
|
const react_1 = require("react");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(0
|
|
9
|
-
|
|
5
|
+
let lastSize;
|
|
6
|
+
// The same object while the size stays the same, as useSyncExternalStore requires.
|
|
7
|
+
function getSnapshot() {
|
|
8
|
+
if ((lastSize === null || lastSize === void 0 ? void 0 : lastSize.width) !== window.innerWidth || lastSize.height !== window.innerHeight) {
|
|
9
|
+
lastSize = {
|
|
10
10
|
width: window.innerWidth,
|
|
11
|
-
height: window.innerHeight
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
11
|
+
height: window.innerHeight
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
return lastSize;
|
|
15
|
+
}
|
|
16
|
+
function getServerSnapshot() {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
// At most one update per animation frame, however often `resize` fires.
|
|
20
|
+
function subscribe(onChange) {
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
let frame = 0;
|
|
23
|
+
window.addEventListener("resize", () => {
|
|
24
|
+
cancelAnimationFrame(frame);
|
|
25
|
+
frame = requestAnimationFrame(onChange);
|
|
26
|
+
}, {
|
|
27
|
+
signal: controller.signal
|
|
28
|
+
});
|
|
29
|
+
return () => {
|
|
30
|
+
cancelAnimationFrame(frame);
|
|
31
|
+
controller.abort();
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The window's inner size. `undefined` on the server and while hydrating, the current size from the first render of
|
|
36
|
+
* a component mounted later.
|
|
37
|
+
*/
|
|
38
|
+
function useWindowSize() {
|
|
39
|
+
return (0, react_1.useSyncExternalStore)(subscribe, getSnapshot, getServerSnapshot);
|
|
19
40
|
}
|