@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.
Files changed (38) hide show
  1. package/README.md +5 -3
  2. package/clickable/use-button-props.js +108 -47
  3. package/global-context/index.test.js +23 -2
  4. package/hooks/use-body-scroll-lock.js +56 -27
  5. package/hooks/use-composed-refs.test.js +14 -2
  6. package/hooks/use-debounce.js +21 -8
  7. package/hooks/use-debounce.test.js +15 -3
  8. package/hooks/use-focus-return.js +55 -24
  9. package/hooks/use-focus-trap.js +87 -52
  10. package/hooks/use-increment.js +47 -5
  11. package/hooks/use-input-focus.js +65 -19
  12. package/hooks/use-is-hydrated.js +7 -1
  13. package/hooks/use-is-hydrated.test.js +13 -1
  14. package/hooks/use-is-mounted.js +15 -1
  15. package/hooks/use-key.js +53 -23
  16. package/hooks/use-key.test.js +88 -8
  17. package/hooks/use-media-query.js +77 -18
  18. package/hooks/use-media-query.test.js +29 -8
  19. package/hooks/use-min-window-width.js +39 -19
  20. package/hooks/use-mouse-drag-to-scroll.js +116 -61
  21. package/hooks/use-mouse-drag-to-scroll.test.js +56 -10
  22. package/hooks/use-on-mount.js +20 -3
  23. package/hooks/use-on-unmount.js +22 -3
  24. package/hooks/use-on-update.js +20 -8
  25. package/hooks/use-on-update.test.js +17 -2
  26. package/hooks/use-pagination.js +30 -14
  27. package/hooks/use-previous.js +14 -3
  28. package/hooks/use-raf-state.js +35 -8
  29. package/hooks/use-toggle.js +42 -4
  30. package/hooks/use-window-scroll.d.ts +7 -2
  31. package/hooks/use-window-scroll.js +36 -13
  32. package/hooks/use-window-scroll.test.js +141 -41
  33. package/hooks/use-window-size.d.ts +7 -2
  34. package/hooks/use-window-size.js +34 -13
  35. package/hooks/use-window-size.test.js +147 -31
  36. package/package.json +4 -4
  37. package/swipeable/use-swipeable.test.js +31 -5
  38. package/translations/index.js +1 -1
package/README.md CHANGED
@@ -217,21 +217,23 @@ useOnUpdate(
217
217
 
218
218
  ### useWindowScroll
219
219
 
220
- Returns x and y scroll coordinates of window on scroll.
220
+ Returns the window's scroll position (`x`, `y`), updated at most once per animation frame. `undefined` on the
221
+ server and during hydration, the current position from the first render of a component mounted later.
221
222
 
222
223
  ```tsx
223
224
  import { useWindowScroll } from "@uxf/core-react/hooks/use-window-scroll";
224
225
 
225
226
  const windowScroll = useWindowScroll();
226
227
 
227
- const example = windowScroll && windowScroll.y > 100 ? "scroled" : "on top";
228
+ const example = windowScroll && windowScroll.y > 100 ? "scrolled" : "on top";
228
229
  ```
229
230
 
230
231
  ---
231
232
 
232
233
  ### useWindowSize
233
234
 
234
- Returns window width and height.
235
+ Returns the window's inner `width` and `height`, updated at most once per animation frame. `undefined` on the
236
+ server and during hydration, the current size from the first render of a component mounted later.
235
237
 
236
238
  ```tsx
237
239
  import { useWindowSize } from "@uxf/core-react/hooks/use-window-size";
@@ -1,60 +1,121 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useButtonProps = useButtonProps;
4
+ const compiler_runtime_1 = require("react/compiler-runtime");
4
5
  const cx_1 = require("@uxf/core/utils/cx");
5
- const react_1 = require("react");
6
6
  const add_busy_and_disabled_classnames_1 = require("./add-busy-and-disabled-classnames");
7
7
  const add_is_hoverable_classname_1 = require("./add-is-hoverable-classname");
8
- function useButtonProps(props, options = {}) {
9
- const { ["aria-busy"]: ariaBusy, ["aria-disabled"]: ariaDisabled, autoFocus, analyticsCallback, className, disabled, formAction, formEncType, formMethod, formNoValidate, formTarget, loading, name, onClick, role, tabIndex, type, value, ...restProps } = props;
10
- const isBusy = loading ? loading : Boolean(ariaBusy);
11
- const isDisabled = disabled ? disabled : Boolean(ariaDisabled);
12
- const isBusyOrDisabled = isBusy || isDisabled;
13
- const _className = (0, add_busy_and_disabled_classnames_1.addBusyAndDisabledClassnames)(className, { isBusy, isDisabled }, options);
14
- const _onClick = (0, react_1.useCallback)((e) => {
15
- if (isBusyOrDisabled) {
16
- e.preventDefault();
17
- }
18
- else {
19
- if (analyticsCallback) {
20
- analyticsCallback();
8
+ function useButtonProps(props, t0) {
9
+ const $ = (0, compiler_runtime_1.c)(16);
10
+ let restProps;
11
+ let role;
12
+ let t1;
13
+ let t2;
14
+ let tabIndex;
15
+ if ($[0] !== props || $[1] !== t0) {
16
+ t2 = Symbol.for("react.early_return_sentinel");
17
+ bb0: {
18
+ const options = t0 === undefined ? {} : t0;
19
+ const { "aria-busy": ariaBusy, "aria-disabled": ariaDisabled, autoFocus, analyticsCallback, className, disabled, formAction, formEncType, formMethod, formNoValidate, formTarget, loading, name, onClick, role: t3, tabIndex: t4, type, value, ...t5 } = props;
20
+ role = t3;
21
+ tabIndex = t4;
22
+ restProps = t5;
23
+ const isBusy = loading ? loading : Boolean(ariaBusy);
24
+ const isDisabled = disabled ? disabled : Boolean(ariaDisabled);
25
+ const isBusyOrDisabled = isBusy || isDisabled;
26
+ const _className = (0, add_busy_and_disabled_classnames_1.addBusyAndDisabledClassnames)(className, {
27
+ isBusy,
28
+ isDisabled
29
+ }, options);
30
+ let t6;
31
+ if ($[7] !== analyticsCallback || $[8] !== isBusyOrDisabled || $[9] !== onClick) {
32
+ t6 = e => {
33
+ if (isBusyOrDisabled) {
34
+ e.preventDefault();
35
+ }
36
+ else {
37
+ if (analyticsCallback) {
38
+ analyticsCallback();
39
+ }
40
+ if (onClick) {
41
+ onClick(e);
42
+ }
43
+ }
44
+ };
45
+ $[7] = analyticsCallback;
46
+ $[8] = isBusyOrDisabled;
47
+ $[9] = onClick;
48
+ $[10] = t6;
21
49
  }
22
- if (onClick) {
23
- onClick(e);
50
+ else {
51
+ t6 = $[10];
24
52
  }
53
+ const _onClick = t6;
54
+ if (onClick || type === "reset" || type === "submit") {
55
+ let submitProps = {};
56
+ if (type === "submit") {
57
+ submitProps = {
58
+ formAction,
59
+ formEncType,
60
+ formMethod,
61
+ formNoValidate,
62
+ formTarget,
63
+ name,
64
+ value
65
+ };
66
+ }
67
+ t2 = {
68
+ "aria-busy": isBusy,
69
+ "aria-disabled": ariaDisabled,
70
+ autoFocus,
71
+ className: (0, cx_1.cx)((0, add_is_hoverable_classname_1.addIsHoverableClassname)(options), ..._className),
72
+ disabled,
73
+ onClick: _onClick,
74
+ role,
75
+ tabIndex: isBusyOrDisabled ? -1 : tabIndex,
76
+ type,
77
+ ...submitProps,
78
+ ...restProps
79
+ };
80
+ break bb0;
81
+ }
82
+ t1 = (0, cx_1.cx)(..._className);
25
83
  }
26
- }, [analyticsCallback, isBusyOrDisabled, onClick]);
27
- if (onClick || type === "reset" || type === "submit") {
28
- let submitProps = {};
29
- if (type === "submit") {
30
- submitProps = {
31
- formAction,
32
- formEncType,
33
- formMethod,
34
- formNoValidate,
35
- formTarget,
36
- name,
37
- value,
38
- };
39
- }
40
- return {
41
- "aria-busy": isBusy,
42
- "aria-disabled": ariaDisabled,
43
- autoFocus,
44
- className: (0, cx_1.cx)((0, add_is_hoverable_classname_1.addIsHoverableClassname)(options), ..._className),
45
- disabled,
46
- onClick: _onClick,
84
+ $[0] = props;
85
+ $[1] = t0;
86
+ $[2] = restProps;
87
+ $[3] = role;
88
+ $[4] = t1;
89
+ $[5] = t2;
90
+ $[6] = tabIndex;
91
+ }
92
+ else {
93
+ restProps = $[2];
94
+ role = $[3];
95
+ t1 = $[4];
96
+ t2 = $[5];
97
+ tabIndex = $[6];
98
+ }
99
+ if (t2 !== Symbol.for("react.early_return_sentinel")) {
100
+ return t2;
101
+ }
102
+ const t3 = tabIndex !== null && tabIndex !== void 0 ? tabIndex : -1;
103
+ let t4;
104
+ if ($[11] !== restProps || $[12] !== role || $[13] !== t1 || $[14] !== t3) {
105
+ t4 = {
106
+ className: t1,
47
107
  role,
48
- tabIndex: isBusyOrDisabled ? -1 : tabIndex,
49
- type,
50
- ...submitProps,
51
- ...restProps,
108
+ tabIndex: t3,
109
+ ...restProps
52
110
  };
111
+ $[11] = restProps;
112
+ $[12] = role;
113
+ $[13] = t1;
114
+ $[14] = t3;
115
+ $[15] = t4;
116
+ }
117
+ else {
118
+ t4 = $[15];
53
119
  }
54
- return {
55
- className: (0, cx_1.cx)(..._className),
56
- role,
57
- tabIndex: tabIndex !== null && tabIndex !== void 0 ? tabIndex : -1,
58
- ...restProps,
59
- };
120
+ return t4;
60
121
  }
@@ -33,6 +33,7 @@ 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 index_1 = require("./index");
@@ -74,11 +75,31 @@ describe("createGlobalContext", () => {
74
75
  it("returns the provided value through Provider/useContext", () => {
75
76
  const ctx = (0, index_1.createGlobalContext)("test/provider-consumer", "default");
76
77
  function Consumer() {
78
+ const $ = (0, compiler_runtime_1.c)(2);
77
79
  const value = (0, react_2.useContext)(ctx);
78
- return react_2.default.createElement("span", null, value);
80
+ let t0;
81
+ if ($[0] !== value) {
82
+ t0 = react_2.default.createElement("span", null, value);
83
+ $[0] = value;
84
+ $[1] = t0;
85
+ }
86
+ else {
87
+ t0 = $[1];
88
+ }
89
+ return t0;
79
90
  }
80
91
  function Wrapper(props) {
81
- return react_2.default.createElement(ctx.Provider, { value: "provided" }, props.children);
92
+ const $ = (0, compiler_runtime_1.c)(2);
93
+ let t0;
94
+ if ($[0] !== props.children) {
95
+ t0 = react_2.default.createElement(ctx.Provider, { value: "provided" }, props.children);
96
+ $[0] = props.children;
97
+ $[1] = t0;
98
+ }
99
+ else {
100
+ t0 = $[1];
101
+ }
102
+ return t0;
82
103
  }
83
104
  (0, react_1.render)(react_2.default.createElement(Wrapper, null,
84
105
  react_2.default.createElement(Consumer, null)));
@@ -1,36 +1,65 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useBodyScrollLock = useBodyScrollLock;
4
+ const compiler_runtime_1 = require("react/compiler-runtime");
4
5
  const body_scroll_lock_1 = require("@uxf/core/utils/body-scroll-lock");
5
6
  const is_not_nil_1 = require("@uxf/core/utils/is-not-nil");
6
7
  const react_1 = require("react");
7
- function useBodyScrollLock(containerRef, isOpen, { allowTouchMove, willClearAllOnClose, willReserveScrollBarGap } = {}) {
8
- (0, react_1.useEffect)(() => {
9
- const node = containerRef.current;
10
- if (isOpen && node) {
11
- (0, body_scroll_lock_1.disableBodyScroll)(node, {
12
- allowTouchMove: allowTouchMove ||
13
- ((element) => {
14
- while (element !== document.body) {
15
- if (element.getAttribute("data-body-scroll-lock-ignore") !== null) {
16
- return true;
17
- }
18
- if ((0, is_not_nil_1.isNotNil)(element.parentElement)) {
19
- element = element.parentElement;
20
- }
21
- }
22
- return false;
23
- }),
24
- willReserveScrollBarGap,
25
- });
26
- }
27
- return () => {
28
- if (node) {
29
- (0, body_scroll_lock_1.enableBodyScroll)(node);
30
- }
31
- if (willClearAllOnClose) {
32
- (0, body_scroll_lock_1.clearAllBodyScrollLocks)();
8
+ function useBodyScrollLock(containerRef, isOpen, t0) {
9
+ const $ = (0, compiler_runtime_1.c)(9);
10
+ let t1;
11
+ if ($[0] !== t0) {
12
+ t1 = t0 === undefined ? {} : t0;
13
+ $[0] = t0;
14
+ $[1] = t1;
15
+ }
16
+ else {
17
+ t1 = $[1];
18
+ }
19
+ const { allowTouchMove, willClearAllOnClose, willReserveScrollBarGap } = t1;
20
+ let t2;
21
+ let t3;
22
+ if ($[2] !== allowTouchMove || $[3] !== containerRef || $[4] !== isOpen || $[5] !== willClearAllOnClose || $[6] !== willReserveScrollBarGap) {
23
+ t2 = () => {
24
+ const node = containerRef.current;
25
+ if (isOpen && node) {
26
+ (0, body_scroll_lock_1.disableBodyScroll)(node, {
27
+ allowTouchMove: allowTouchMove || _temp,
28
+ willReserveScrollBarGap
29
+ });
33
30
  }
31
+ return () => {
32
+ if (node) {
33
+ (0, body_scroll_lock_1.enableBodyScroll)(node);
34
+ }
35
+ if (willClearAllOnClose) {
36
+ (0, body_scroll_lock_1.clearAllBodyScrollLocks)();
37
+ }
38
+ };
34
39
  };
35
- }, [allowTouchMove, willClearAllOnClose, containerRef, isOpen, willReserveScrollBarGap]);
40
+ t3 = [allowTouchMove, willClearAllOnClose, containerRef, isOpen, willReserveScrollBarGap];
41
+ $[2] = allowTouchMove;
42
+ $[3] = containerRef;
43
+ $[4] = isOpen;
44
+ $[5] = willClearAllOnClose;
45
+ $[6] = willReserveScrollBarGap;
46
+ $[7] = t2;
47
+ $[8] = t3;
48
+ }
49
+ else {
50
+ t2 = $[7];
51
+ t3 = $[8];
52
+ }
53
+ (0, react_1.useEffect)(t2, t3);
54
+ }
55
+ function _temp(element) {
56
+ while (element !== document.body) {
57
+ if (element.getAttribute("data-body-scroll-lock-ignore") !== null) {
58
+ return true;
59
+ }
60
+ if ((0, is_not_nil_1.isNotNil)(element.parentElement)) {
61
+ element = element.parentElement;
62
+ }
63
+ }
64
+ return false;
36
65
  }
@@ -33,17 +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_composed_refs_1 = require("./use-composed-refs");
39
40
  function Probe(props) {
41
+ const $ = (0, compiler_runtime_1.c)(3);
40
42
  const ref = (0, use_composed_refs_1.useComposedRefs)(props.outerRef, props.innerRef);
41
- return react_2.default.createElement("span", { ref: ref }, props.label);
43
+ let t0;
44
+ if ($[0] !== props.label || $[1] !== ref) {
45
+ t0 = react_2.default.createElement("span", { ref: ref }, props.label);
46
+ $[0] = props.label;
47
+ $[1] = ref;
48
+ $[2] = t0;
49
+ }
50
+ else {
51
+ t0 = $[2];
52
+ }
53
+ return t0;
42
54
  }
43
55
  test("keeps the same composed ref across re-renders, so a cleanup ref attaches once", () => {
44
56
  var _a;
45
57
  const calls = [];
46
- const cleanupRef = (node) => {
58
+ const cleanupRef = node => {
47
59
  calls.push(node ? "attach" : "null");
48
60
  return () => {
49
61
  calls.push("cleanup");
@@ -1,17 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useDebounce = useDebounce;
4
+ const compiler_runtime_1 = require("react/compiler-runtime");
4
5
  const react_1 = require("react");
5
6
  function useDebounce(value, delay) {
7
+ const $ = (0, compiler_runtime_1.c)(4);
6
8
  const [debouncedValue, setDebouncedValue] = (0, react_1.useState)(value);
7
- // A plain effect, not useOnUpdate: after a reconnect (an <Activity> revealed again) a pending value must still
8
- // arrive. On mount the timer sets the value the state already holds, so nothing re-renders, except that under
9
- // StrictMode a value built during render (an inline object) is a new one.
10
- (0, react_1.useEffect)(() => {
11
- const timer = setTimeout(() => setDebouncedValue(value), delay !== null && delay !== void 0 ? delay : 500);
12
- return () => {
13
- clearTimeout(timer);
9
+ let t0;
10
+ let t1;
11
+ if ($[0] !== delay || $[1] !== value) {
12
+ t0 = () => {
13
+ const timer = setTimeout(() => setDebouncedValue(value), delay !== null && delay !== void 0 ? delay : 500);
14
+ return () => {
15
+ clearTimeout(timer);
16
+ };
14
17
  };
15
- }, [value, delay]);
18
+ t1 = [value, delay];
19
+ $[0] = delay;
20
+ $[1] = value;
21
+ $[2] = t0;
22
+ $[3] = t1;
23
+ }
24
+ else {
25
+ t0 = $[2];
26
+ t1 = $[3];
27
+ }
28
+ (0, react_1.useEffect)(t0, t1);
16
29
  return debouncedValue;
17
30
  }
@@ -33,16 +33,28 @@ 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_debounce_1 = require("./use-debounce");
39
40
  function Probe(props) {
41
+ const $ = (0, compiler_runtime_1.c)(2);
40
42
  const debounced = (0, use_debounce_1.useDebounce)(props.value, 100);
41
- return react_2.default.createElement("span", { "data-testid": "debounced" }, debounced);
43
+ let t0;
44
+ if ($[0] !== debounced) {
45
+ t0 = react_2.default.createElement("span", { "data-testid": "debounced" }, debounced);
46
+ $[0] = debounced;
47
+ $[1] = t0;
48
+ }
49
+ else {
50
+ t0 = $[1];
51
+ }
52
+ return t0;
42
53
  }
43
54
  function Host(props) {
44
- return (react_2.default.createElement(react_2.Activity, { mode: props.isVisible ? "visible" : "hidden" },
45
- react_2.default.createElement(Probe, { value: props.value })));
55
+ "use no memo";
56
+ return react_2.default.createElement(react_2.Activity, { mode: props.isVisible ? "visible" : "hidden" },
57
+ react_2.default.createElement(Probe, { value: props.value }));
46
58
  }
47
59
  beforeEach(() => jest.useFakeTimers());
48
60
  afterEach(() => jest.useRealTimers());
@@ -1,35 +1,66 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.useFocusReturn = useFocusReturn;
4
+ const compiler_runtime_1 = require("react/compiler-runtime");
4
5
  const react_1 = require("react");
5
6
  const use_on_update_1 = require("./use-on-update");
6
- function useFocusReturn(opened, transitionDuration = 0, shouldReturnFocus = true) {
7
+ function useFocusReturn(opened, t0, t1) {
8
+ const $ = (0, compiler_runtime_1.c)(6);
9
+ const transitionDuration = t0 === undefined ? 0 : t0;
10
+ const shouldReturnFocus = t1 === undefined ? true : t1;
7
11
  const lastActiveElement = (0, react_1.useRef)(undefined);
8
- const returnFocus = () => {
9
- const node = lastActiveElement.current;
10
- if (node && "focus" in node && typeof node.focus === "function") {
11
- node.focus();
12
- }
13
- };
14
- (0, use_on_update_1.useOnUpdate)(() => {
15
- let timeout = -1;
16
- const clearFocusTimeout = (e) => {
17
- if (e.key === "Tab") {
18
- clearTimeout(timeout);
12
+ let t2;
13
+ if ($[0] === Symbol.for("react.memo_cache_sentinel")) {
14
+ t2 = () => {
15
+ const node = lastActiveElement.current;
16
+ if (node && "focus" in node && typeof node.focus === "function") {
17
+ node.focus();
19
18
  }
20
19
  };
21
- const controller = new AbortController();
22
- document.addEventListener("keydown", clearFocusTimeout, { signal: controller.signal });
23
- if (opened) {
24
- lastActiveElement.current = document.activeElement;
25
- }
26
- else if (shouldReturnFocus) {
27
- timeout = window.setTimeout(returnFocus, transitionDuration + 10);
28
- }
29
- return () => {
30
- clearTimeout(timeout);
31
- controller.abort();
20
+ $[0] = t2;
21
+ }
22
+ else {
23
+ t2 = $[0];
24
+ }
25
+ const returnFocus = t2;
26
+ let t3;
27
+ let t4;
28
+ if ($[1] !== opened || $[2] !== shouldReturnFocus || $[3] !== transitionDuration) {
29
+ t3 = () => {
30
+ let timeout = -1;
31
+ const clearFocusTimeout = e => {
32
+ if (e.key === "Tab") {
33
+ clearTimeout(timeout);
34
+ }
35
+ };
36
+ const controller = new AbortController();
37
+ document.addEventListener("keydown", clearFocusTimeout, {
38
+ signal: controller.signal
39
+ });
40
+ if (opened) {
41
+ lastActiveElement.current = document.activeElement;
42
+ }
43
+ else {
44
+ if (shouldReturnFocus) {
45
+ timeout = window.setTimeout(returnFocus, transitionDuration + 10);
46
+ }
47
+ }
48
+ return () => {
49
+ clearTimeout(timeout);
50
+ controller.abort();
51
+ };
32
52
  };
33
- }, [opened, shouldReturnFocus, transitionDuration]);
53
+ t4 = [opened, shouldReturnFocus, transitionDuration];
54
+ $[1] = opened;
55
+ $[2] = shouldReturnFocus;
56
+ $[3] = transitionDuration;
57
+ $[4] = t3;
58
+ $[5] = t4;
59
+ }
60
+ else {
61
+ t3 = $[4];
62
+ t4 = $[5];
63
+ }
64
+ (0, use_on_update_1.useOnUpdate)(t3, t4);
34
65
  return returnFocus;
35
66
  }