@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
package/hooks/use-focus-trap.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useFocusTrap = useFocusTrap;
|
|
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
|
const TABBABLE_NODES = /input|select|textarea|button|object/;
|
|
@@ -31,8 +32,7 @@ function getElementTabIndex(element) {
|
|
|
31
32
|
function focusable(element) {
|
|
32
33
|
const nodeName = element.nodeName.toLowerCase();
|
|
33
34
|
const isTabIndexNotNaN = !Number.isNaN(getElementTabIndex(element));
|
|
34
|
-
const res =
|
|
35
|
-
(element instanceof HTMLAnchorElement ? Boolean(element.href) || isTabIndexNotNaN : isTabIndexNotNaN);
|
|
35
|
+
const res = TABBABLE_NODES.test(nodeName) && !element.disabled || (element instanceof HTMLAnchorElement ? Boolean(element.href) || isTabIndexNotNaN : isTabIndexNotNaN);
|
|
36
36
|
return res && visible(element);
|
|
37
37
|
}
|
|
38
38
|
function tabbable(element) {
|
|
@@ -61,7 +61,7 @@ function scopeTab(node, event) {
|
|
|
61
61
|
}
|
|
62
62
|
}
|
|
63
63
|
function createAriaHider(containerNode, selector = "body > :not(script)") {
|
|
64
|
-
const rootNodes = Array.from(document.querySelectorAll(selector)).map(
|
|
64
|
+
const rootNodes = Array.from(document.querySelectorAll(selector)).map(node => {
|
|
65
65
|
if (node.contains(containerNode)) {
|
|
66
66
|
return;
|
|
67
67
|
}
|
|
@@ -69,10 +69,13 @@ function createAriaHider(containerNode, selector = "body > :not(script)") {
|
|
|
69
69
|
if (ariaHidden === null || ariaHidden === "false") {
|
|
70
70
|
node.setAttribute("aria-hidden", "true");
|
|
71
71
|
}
|
|
72
|
-
return {
|
|
72
|
+
return {
|
|
73
|
+
node,
|
|
74
|
+
ariaHidden
|
|
75
|
+
};
|
|
73
76
|
});
|
|
74
77
|
return () => {
|
|
75
|
-
rootNodes.forEach(
|
|
78
|
+
rootNodes.forEach(item => {
|
|
76
79
|
if (!item) {
|
|
77
80
|
return;
|
|
78
81
|
}
|
|
@@ -85,60 +88,92 @@ function createAriaHider(containerNode, selector = "body > :not(script)") {
|
|
|
85
88
|
});
|
|
86
89
|
};
|
|
87
90
|
}
|
|
88
|
-
function useFocusTrap(
|
|
91
|
+
function useFocusTrap(t0) {
|
|
92
|
+
const $ = (0, compiler_runtime_1.c)(6);
|
|
93
|
+
const active = t0 === undefined ? true : t0;
|
|
89
94
|
const ref = (0, react_1.useRef)(null);
|
|
90
95
|
const restoreAria = (0, react_1.useRef)(undefined);
|
|
91
96
|
const timer = (0, react_1.useRef)(undefined);
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
focusElement =
|
|
97
|
+
let t1;
|
|
98
|
+
if ($[0] !== active) {
|
|
99
|
+
t1 = node => {
|
|
100
|
+
if (!active) {
|
|
101
|
+
ref.current = null;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if (restoreAria.current) {
|
|
105
|
+
restoreAria.current();
|
|
106
|
+
}
|
|
107
|
+
if (node) {
|
|
108
|
+
const processNode = _node => {
|
|
109
|
+
restoreAria.current = createAriaHider(_node);
|
|
110
|
+
let focusElement = node.querySelector("[data-autofocus]");
|
|
111
|
+
if (!focusElement) {
|
|
112
|
+
const children = Array.from(node.querySelectorAll(FOCUS_SELECTOR));
|
|
113
|
+
focusElement = children.find(tabbable) || children.find(focusable) || null;
|
|
114
|
+
if (!focusElement && focusable(node)) {
|
|
115
|
+
focusElement = node;
|
|
116
|
+
}
|
|
109
117
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
118
|
+
if (focusElement) {
|
|
119
|
+
focusElement.focus();
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
timer.current = window.setTimeout(() => {
|
|
123
|
+
if (node.ownerDocument) {
|
|
124
|
+
processNode(node);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
ref.current = node;
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
ref.current = null;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
$[0] = active;
|
|
134
|
+
$[1] = t1;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
t1 = $[1];
|
|
138
|
+
}
|
|
139
|
+
const setRef = t1;
|
|
140
|
+
let t2;
|
|
141
|
+
let t3;
|
|
142
|
+
if ($[2] !== active) {
|
|
143
|
+
t2 = () => {
|
|
144
|
+
if (!active) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const handler = e => {
|
|
148
|
+
const node_0 = ref.current;
|
|
149
|
+
if (e.key === "Tab" && node_0) {
|
|
150
|
+
scopeTab(node_0, e);
|
|
113
151
|
}
|
|
114
152
|
};
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (node.ownerDocument) {
|
|
119
|
-
processNode(node);
|
|
120
|
-
}
|
|
153
|
+
const controller = new AbortController();
|
|
154
|
+
document.addEventListener("keydown", handler, {
|
|
155
|
+
signal: controller.signal
|
|
121
156
|
});
|
|
122
|
-
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
ref.current = null;
|
|
126
|
-
}
|
|
127
|
-
}, [active]);
|
|
128
|
-
(0, react_1.useEffect)(() => {
|
|
129
|
-
if (!active) {
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
const handler = (e) => {
|
|
133
|
-
const node = ref.current;
|
|
134
|
-
if (e.key === "Tab" && node) {
|
|
135
|
-
scopeTab(node, e);
|
|
136
|
-
}
|
|
157
|
+
return () => controller.abort();
|
|
137
158
|
};
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
159
|
+
t3 = [active];
|
|
160
|
+
$[2] = active;
|
|
161
|
+
$[3] = t2;
|
|
162
|
+
$[4] = t3;
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
t2 = $[3];
|
|
166
|
+
t3 = $[4];
|
|
167
|
+
}
|
|
168
|
+
(0, react_1.useEffect)(t2, t3);
|
|
169
|
+
let t4;
|
|
170
|
+
if ($[5] === Symbol.for("react.memo_cache_sentinel")) {
|
|
171
|
+
t4 = () => clearTimeout(timer.current);
|
|
172
|
+
$[5] = t4;
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
t4 = $[5];
|
|
176
|
+
}
|
|
177
|
+
(0, use_on_unmount_1.useOnUnmount)(t4);
|
|
143
178
|
return setRef;
|
|
144
179
|
}
|
package/hooks/use-increment.js
CHANGED
|
@@ -1,11 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useIncrement = useIncrement;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
|
-
function useIncrement(defaultValue,
|
|
6
|
+
function useIncrement(defaultValue, t0) {
|
|
7
|
+
const $ = (0, compiler_runtime_1.c)(11);
|
|
8
|
+
const step = t0 === undefined ? 1 : t0;
|
|
6
9
|
const [value, setValue] = (0, react_1.useState)(defaultValue);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
10
|
+
let t1;
|
|
11
|
+
if ($[0] !== step) {
|
|
12
|
+
t1 = () => setValue(prev => prev + step);
|
|
13
|
+
$[0] = step;
|
|
14
|
+
$[1] = t1;
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
t1 = $[1];
|
|
18
|
+
}
|
|
19
|
+
const incrementHandler = t1;
|
|
20
|
+
let t2;
|
|
21
|
+
if ($[2] !== step) {
|
|
22
|
+
t2 = () => setValue(prev_0 => prev_0 - step);
|
|
23
|
+
$[2] = step;
|
|
24
|
+
$[3] = t2;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
t2 = $[3];
|
|
28
|
+
}
|
|
29
|
+
const decrementHandler = t2;
|
|
30
|
+
let t3;
|
|
31
|
+
if ($[4] !== defaultValue) {
|
|
32
|
+
t3 = () => setValue(defaultValue);
|
|
33
|
+
$[4] = defaultValue;
|
|
34
|
+
$[5] = t3;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
t3 = $[5];
|
|
38
|
+
}
|
|
39
|
+
const resetHandler = t3;
|
|
40
|
+
let t4;
|
|
41
|
+
if ($[6] !== decrementHandler || $[7] !== incrementHandler || $[8] !== resetHandler || $[9] !== value) {
|
|
42
|
+
t4 = [value, incrementHandler, decrementHandler, resetHandler];
|
|
43
|
+
$[6] = decrementHandler;
|
|
44
|
+
$[7] = incrementHandler;
|
|
45
|
+
$[8] = resetHandler;
|
|
46
|
+
$[9] = value;
|
|
47
|
+
$[10] = t4;
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
t4 = $[10];
|
|
51
|
+
}
|
|
52
|
+
return t4;
|
|
11
53
|
}
|
package/hooks/use-input-focus.js
CHANGED
|
@@ -1,26 +1,72 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useInputFocus = useInputFocus;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
6
|
function useInputFocus(focusRef, onBlur, onFocus) {
|
|
7
|
+
const $ = (0, compiler_runtime_1.c)(11);
|
|
6
8
|
const [focused, setFocused] = (0, react_1.useState)(false);
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
node.
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
9
|
+
let t0;
|
|
10
|
+
if ($[0] !== focusRef) {
|
|
11
|
+
t0 = () => {
|
|
12
|
+
const node = focusRef.current;
|
|
13
|
+
if (node) {
|
|
14
|
+
node.focus();
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
$[0] = focusRef;
|
|
18
|
+
$[1] = t0;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
t0 = $[1];
|
|
22
|
+
}
|
|
23
|
+
const focus = t0;
|
|
24
|
+
let t1;
|
|
25
|
+
if ($[2] !== onFocus) {
|
|
26
|
+
t1 = e => {
|
|
27
|
+
setFocused(true);
|
|
28
|
+
if (onFocus) {
|
|
29
|
+
onFocus(e);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
$[2] = onFocus;
|
|
33
|
+
$[3] = t1;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
t1 = $[3];
|
|
37
|
+
}
|
|
38
|
+
const _onFocus = t1;
|
|
39
|
+
let t2;
|
|
40
|
+
if ($[4] !== onBlur) {
|
|
41
|
+
t2 = e_0 => {
|
|
42
|
+
setFocused(false);
|
|
43
|
+
if (onBlur) {
|
|
44
|
+
onBlur(e_0);
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
$[4] = onBlur;
|
|
48
|
+
$[5] = t2;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
t2 = $[5];
|
|
52
|
+
}
|
|
53
|
+
const _onBlur = t2;
|
|
54
|
+
let t3;
|
|
55
|
+
if ($[6] !== _onBlur || $[7] !== _onFocus || $[8] !== focus || $[9] !== focused) {
|
|
56
|
+
t3 = {
|
|
57
|
+
focused,
|
|
58
|
+
focus,
|
|
59
|
+
onBlur: _onBlur,
|
|
60
|
+
onFocus: _onFocus
|
|
61
|
+
};
|
|
62
|
+
$[6] = _onBlur;
|
|
63
|
+
$[7] = _onFocus;
|
|
64
|
+
$[8] = focus;
|
|
65
|
+
$[9] = focused;
|
|
66
|
+
$[10] = t3;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
t3 = $[10];
|
|
70
|
+
}
|
|
71
|
+
return t3;
|
|
26
72
|
}
|
package/hooks/use-is-hydrated.js
CHANGED
|
@@ -14,5 +14,11 @@ const subscribeToNothing = () => noop_1.noop;
|
|
|
14
14
|
* true. Code that reads refs or measures the DOM after mount still needs an effect.
|
|
15
15
|
*/
|
|
16
16
|
function useIsHydrated() {
|
|
17
|
-
return (0, react_1.useSyncExternalStore)(subscribeToNothing,
|
|
17
|
+
return (0, react_1.useSyncExternalStore)(subscribeToNothing, _temp, _temp2);
|
|
18
|
+
}
|
|
19
|
+
function _temp2() {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
function _temp() {
|
|
23
|
+
return true;
|
|
18
24
|
}
|
|
@@ -3,15 +3,27 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
6
7
|
const react_1 = require("@testing-library/react");
|
|
7
8
|
const react_2 = __importDefault(require("react"));
|
|
8
9
|
const client_1 = require("react-dom/client");
|
|
9
10
|
const server_1 = require("react-dom/server");
|
|
10
11
|
const use_is_hydrated_1 = require("./use-is-hydrated");
|
|
11
12
|
function Probe(props) {
|
|
13
|
+
const $ = (0, compiler_runtime_1.c)(2);
|
|
12
14
|
const isHydrated = (0, use_is_hydrated_1.useIsHydrated)();
|
|
13
15
|
props.onRender(isHydrated);
|
|
14
|
-
|
|
16
|
+
const t0 = isHydrated ? "client" : "server";
|
|
17
|
+
let t1;
|
|
18
|
+
if ($[0] !== t0) {
|
|
19
|
+
t1 = react_2.default.createElement("span", null, t0);
|
|
20
|
+
$[0] = t0;
|
|
21
|
+
$[1] = t1;
|
|
22
|
+
}
|
|
23
|
+
else {
|
|
24
|
+
t1 = $[1];
|
|
25
|
+
}
|
|
26
|
+
return t1;
|
|
15
27
|
}
|
|
16
28
|
test("is false while rendering on the server", () => {
|
|
17
29
|
expect((0, server_1.renderToString)(react_2.default.createElement(Probe, { onRender: jest.fn() }))).toContain("server");
|
package/hooks/use-is-mounted.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useIsMounted = useIsMounted;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
6
|
/**
|
|
6
7
|
* @deprecated Use `useIsHydrated` (`@uxf/core-react/hooks/use-is-hydrated`) to render something only in the browser:
|
|
@@ -8,7 +9,20 @@ const react_1 = require("react");
|
|
|
8
9
|
* flash of the fallback. Keep this only where the code needs effects to have run, i.e. refs attached.
|
|
9
10
|
*/
|
|
10
11
|
function useIsMounted() {
|
|
12
|
+
const $ = (0, compiler_runtime_1.c)(2);
|
|
11
13
|
const [isMounted, setIsMounted] = (0, react_1.useState)(false);
|
|
12
|
-
|
|
14
|
+
let t0;
|
|
15
|
+
let t1;
|
|
16
|
+
if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
|
|
17
|
+
t0 = () => setIsMounted(true);
|
|
18
|
+
t1 = [];
|
|
19
|
+
$[0] = t0;
|
|
20
|
+
$[1] = t1;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
t0 = $[0];
|
|
24
|
+
t1 = $[1];
|
|
25
|
+
}
|
|
26
|
+
(0, react_1.useEffect)(t0, t1);
|
|
13
27
|
return isMounted;
|
|
14
28
|
}
|
package/hooks/use-key.js
CHANGED
|
@@ -1,28 +1,58 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.useKey = useKey;
|
|
4
|
+
const compiler_runtime_1 = require("react/compiler-runtime");
|
|
4
5
|
const react_1 = require("react");
|
|
5
|
-
const createKeyPredicate =
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
6
|
+
const createKeyPredicate = keyFilter => typeof keyFilter === "function" ? keyFilter : typeof keyFilter === "string" ? e => e.key === keyFilter : () => false;
|
|
7
|
+
function useKey(keyFilter, callback, t0) {
|
|
8
|
+
const $ = (0, compiler_runtime_1.c)(10);
|
|
9
|
+
let t1;
|
|
10
|
+
if ($[0] !== t0) {
|
|
11
|
+
t1 = t0 === undefined ? {} : t0;
|
|
12
|
+
$[0] = t0;
|
|
13
|
+
$[1] = t1;
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
t1 = $[1];
|
|
17
|
+
}
|
|
18
|
+
const { disabled, targetRef, type: t2 } = t1;
|
|
19
|
+
const type = t2 === undefined ? "keydown" : t2;
|
|
20
|
+
let t3;
|
|
21
|
+
if ($[2] !== callback || $[3] !== keyFilter) {
|
|
22
|
+
t3 = e => {
|
|
23
|
+
if (createKeyPredicate(keyFilter)(e)) {
|
|
24
|
+
callback(e);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
$[2] = callback;
|
|
28
|
+
$[3] = keyFilter;
|
|
29
|
+
$[4] = t3;
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
t3 = $[4];
|
|
33
|
+
}
|
|
34
|
+
const onKey = (0, react_1.useEffectEvent)(t3);
|
|
35
|
+
let t4;
|
|
36
|
+
if ($[5] !== disabled || $[6] !== onKey || $[7] !== targetRef || $[8] !== type) {
|
|
37
|
+
t4 = () => {
|
|
38
|
+
if (disabled) {
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const thisNode = targetRef ? targetRef.current : document;
|
|
42
|
+
const controller = new AbortController();
|
|
43
|
+
thisNode === null || thisNode === void 0 ? void 0 : thisNode.addEventListener(type, e_0 => onKey(e_0), {
|
|
44
|
+
signal: controller.signal
|
|
45
|
+
});
|
|
46
|
+
return () => controller.abort();
|
|
47
|
+
};
|
|
48
|
+
$[5] = disabled;
|
|
49
|
+
$[6] = onKey;
|
|
50
|
+
$[7] = targetRef;
|
|
51
|
+
$[8] = type;
|
|
52
|
+
$[9] = t4;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
t4 = $[9];
|
|
56
|
+
}
|
|
57
|
+
(0, react_1.useEffect)(t4);
|
|
28
58
|
}
|
package/hooks/use-key.test.js
CHANGED
|
@@ -33,33 +33,113 @@ 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_key_1 = require("./use-key");
|
|
39
40
|
function Probe(props) {
|
|
41
|
+
const $ = (0, compiler_runtime_1.c)(8);
|
|
40
42
|
const ref = (0, react_2.useRef)(null);
|
|
41
43
|
const [isOpen, setIsOpen] = (0, react_2.useState)(false);
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
let t0;
|
|
45
|
+
if ($[0] !== props) {
|
|
46
|
+
t0 = () => props.onKey();
|
|
47
|
+
$[0] = props;
|
|
48
|
+
$[1] = t0;
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
t0 = $[1];
|
|
52
|
+
}
|
|
53
|
+
let t1;
|
|
54
|
+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
|
|
55
|
+
t1 = {
|
|
56
|
+
targetRef: ref
|
|
57
|
+
};
|
|
58
|
+
$[2] = t1;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
t1 = $[2];
|
|
62
|
+
}
|
|
63
|
+
(0, use_key_1.useKey)("Escape", t0, t1);
|
|
64
|
+
let t2;
|
|
65
|
+
if ($[3] === Symbol.for("react.memo_cache_sentinel")) {
|
|
66
|
+
t2 = react_2.default.createElement("button", { onClick: () => setIsOpen(true) }, "open");
|
|
67
|
+
$[3] = t2;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
t2 = $[3];
|
|
71
|
+
}
|
|
72
|
+
let t3;
|
|
73
|
+
if ($[4] !== isOpen) {
|
|
74
|
+
t3 = isOpen && react_2.default.createElement("div", { "data-testid": "t", ref: ref, tabIndex: -1 });
|
|
75
|
+
$[4] = isOpen;
|
|
76
|
+
$[5] = t3;
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
t3 = $[5];
|
|
80
|
+
}
|
|
81
|
+
let t4;
|
|
82
|
+
if ($[6] !== t3) {
|
|
83
|
+
t4 = react_2.default.createElement(react_2.default.Fragment, null,
|
|
84
|
+
t2,
|
|
85
|
+
t3);
|
|
86
|
+
$[6] = t3;
|
|
87
|
+
$[7] = t4;
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
t4 = $[7];
|
|
91
|
+
}
|
|
92
|
+
return t4;
|
|
46
93
|
}
|
|
47
94
|
test("listens on a target that appears after mount", () => {
|
|
48
95
|
const onKey = jest.fn();
|
|
49
96
|
const { getByText, getByTestId } = (0, react_1.render)(react_2.default.createElement(Probe, { onKey: onKey }));
|
|
50
97
|
(0, react_1.act)(() => getByText("open").click());
|
|
51
|
-
react_1.fireEvent.keyDown(getByTestId("t"), {
|
|
98
|
+
react_1.fireEvent.keyDown(getByTestId("t"), {
|
|
99
|
+
key: "Escape"
|
|
100
|
+
});
|
|
52
101
|
expect(onKey).toHaveBeenCalledTimes(1);
|
|
53
102
|
});
|
|
54
103
|
function Toggle(props) {
|
|
104
|
+
const $ = (0, compiler_runtime_1.c)(5);
|
|
55
105
|
const ref = (0, react_2.useRef)(null);
|
|
56
|
-
|
|
57
|
-
|
|
106
|
+
let t0;
|
|
107
|
+
if ($[0] !== props) {
|
|
108
|
+
t0 = () => props.onKey();
|
|
109
|
+
$[0] = props;
|
|
110
|
+
$[1] = t0;
|
|
111
|
+
}
|
|
112
|
+
else {
|
|
113
|
+
t0 = $[1];
|
|
114
|
+
}
|
|
115
|
+
let t1;
|
|
116
|
+
if ($[2] === Symbol.for("react.memo_cache_sentinel")) {
|
|
117
|
+
t1 = {
|
|
118
|
+
targetRef: ref
|
|
119
|
+
};
|
|
120
|
+
$[2] = t1;
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
t1 = $[2];
|
|
124
|
+
}
|
|
125
|
+
(0, use_key_1.useKey)("Escape", t0, t1);
|
|
126
|
+
let t2;
|
|
127
|
+
if ($[3] !== props.isA) {
|
|
128
|
+
t2 = props.isA ? react_2.default.createElement("div", { "data-testid": "a", ref: ref }) : react_2.default.createElement("span", { "data-testid": "b", ref: ref });
|
|
129
|
+
$[3] = props.isA;
|
|
130
|
+
$[4] = t2;
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
t2 = $[4];
|
|
134
|
+
}
|
|
135
|
+
return t2;
|
|
58
136
|
}
|
|
59
137
|
test("follows the target when the element is replaced", () => {
|
|
60
138
|
const onKey = jest.fn();
|
|
61
139
|
const { rerender, getByTestId } = (0, react_1.render)(react_2.default.createElement(Toggle, { isA: true, onKey: onKey }));
|
|
62
140
|
rerender(react_2.default.createElement(Toggle, { isA: false, onKey: onKey }));
|
|
63
|
-
react_1.fireEvent.keyDown(getByTestId("b"), {
|
|
141
|
+
react_1.fireEvent.keyDown(getByTestId("b"), {
|
|
142
|
+
key: "Escape"
|
|
143
|
+
});
|
|
64
144
|
expect(onKey).toHaveBeenCalledTimes(1);
|
|
65
145
|
});
|