@uxf/core-react 11.74.0 → 11.74.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/README.md +3 -3
- package/clickable/_use-simulated-button.d.ts +17 -0
- package/clickable/_use-simulated-button.js +61 -0
- package/clickable/add-busy-and-disabled-classnames.d.ts +9 -0
- package/clickable/add-busy-and-disabled-classnames.js +17 -0
- package/clickable/types.d.ts +3 -0
- package/clickable/types.js +2 -0
- package/clickable/use-anchor-props.d.ts +9 -0
- package/clickable/use-anchor-props.js +67 -0
- package/clickable/use-button-props.d.ts +7 -0
- package/clickable/use-button-props.js +60 -0
- package/clickable/use-clickable-props.d.ts +9 -0
- package/clickable/use-clickable-props.js +45 -0
- package/components/hide.d.ts +7 -0
- package/components/hide.js +13 -0
- package/components/show.d.ts +7 -0
- package/components/show.js +13 -0
- package/components/viewport-height-polyfill.d.ts +2 -0
- package/components/viewport-height-polyfill.js +49 -0
- package/hooks/use-body-scroll-lock.d.ts +6 -0
- package/hooks/use-body-scroll-lock.js +34 -0
- package/hooks/use-debounce.d.ts +1 -0
- package/hooks/use-debounce.js +15 -0
- package/hooks/use-focus-return.d.ts +1 -0
- package/hooks/use-focus-return.js +34 -0
- package/hooks/use-focus-trap.d.ts +2 -0
- package/hooks/use-focus-trap.js +145 -0
- package/hooks/use-increment.d.ts +2 -0
- package/hooks/use-increment.js +11 -0
- package/hooks/use-increment.test.d.ts +1 -0
- package/hooks/use-increment.test.js +61 -0
- package/hooks/use-input-focus.d.ts +7 -0
- package/hooks/use-input-focus.js +26 -0
- package/hooks/use-is-mounted.d.ts +1 -0
- package/hooks/use-is-mounted.js +9 -0
- package/hooks/use-isomorphic-layout-effect.d.ts +2 -0
- package/hooks/use-isomorphic-layout-effect.js +6 -0
- package/hooks/use-key.d.ts +10 -0
- package/hooks/use-key.js +27 -0
- package/hooks/use-latest.d.ts +1 -0
- package/hooks/use-latest.js +9 -0
- package/hooks/use-media-query.d.ts +1 -0
- package/hooks/use-media-query.js +18 -0
- package/hooks/use-min-window-width.d.ts +1 -0
- package/hooks/use-min-window-width.js +28 -0
- package/hooks/use-mouse-drag-to-scroll.d.ts +2 -0
- package/hooks/use-mouse-drag-to-scroll.js +38 -0
- package/hooks/use-on-mount.d.ts +2 -0
- package/hooks/use-on-mount.js +9 -0
- package/hooks/use-on-unmount.d.ts +2 -0
- package/hooks/use-on-unmount.js +14 -0
- package/hooks/use-on-update.d.ts +2 -0
- package/hooks/use-on-update.js +14 -0
- package/hooks/use-pagination.d.ts +2 -0
- package/hooks/use-pagination.js +20 -0
- package/hooks/use-pagination.test.d.ts +1 -0
- package/hooks/use-pagination.test.js +58 -0
- package/hooks/use-previous.d.ts +1 -0
- package/hooks/use-previous.js +11 -0
- package/hooks/use-raf-state.d.ts +2 -0
- package/hooks/use-raf-state.js +17 -0
- package/hooks/use-toggle.d.ts +2 -0
- package/hooks/use-toggle.js +11 -0
- package/hooks/use-toggle.test.d.ts +1 -0
- package/hooks/use-toggle.test.js +46 -0
- package/hooks/use-window-scroll.d.ts +4 -0
- package/hooks/use-window-scroll.js +21 -0
- package/hooks/use-window-scroll.test.d.ts +1 -0
- package/hooks/use-window-scroll.test.js +47 -0
- package/hooks/use-window-size.d.ts +4 -0
- package/hooks/use-window-size.js +18 -0
- package/hooks/use-window-size.test.d.ts +1 -0
- package/hooks/use-window-size.test.js +36 -0
- package/package.json +1 -1
- package/string/nl2br.d.ts +2 -0
- package/string/nl2br.js +37 -0
- package/swipeable/types.d.ts +152 -0
- package/swipeable/types.js +7 -0
- package/swipeable/use-swipeable.d.ts +3 -0
- package/swipeable/use-swipeable.js +314 -0
- package/translations/create-default-t.d.ts +2 -0
- package/translations/create-default-t.js +30 -0
- package/translations/create-dev-t.d.ts +2 -0
- package/translations/create-dev-t.js +23 -0
- package/translations/index.d.ts +2 -0
- package/translations/index.js +11 -0
- package/translations/replace-variables.d.ts +1 -0
- package/translations/replace-variables.js +6 -0
- package/translations/resolve-plural-key.d.ts +2 -0
- package/translations/resolve-plural-key.js +19 -0
- package/translations/types.d.ts +4 -0
- package/translations/types.js +2 -0
- package/utils/compose-refs.d.ts +2 -0
- package/utils/compose-refs.js +15 -0
- package/utils/compose-refs.test.d.ts +1 -0
- package/utils/compose-refs.test.js +41 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useFocusTrap = useFocusTrap;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const use_on_unmount_1 = require("./use-on-unmount");
|
|
6
|
+
const TABBABLE_NODES = /input|select|textarea|button|object/;
|
|
7
|
+
const FOCUS_SELECTOR = "a, input, select, textarea, button, object, [tabindex]";
|
|
8
|
+
function hidden(element) {
|
|
9
|
+
return element.style.display === "none";
|
|
10
|
+
}
|
|
11
|
+
function visible(element) {
|
|
12
|
+
let parentElement = element;
|
|
13
|
+
while (parentElement) {
|
|
14
|
+
if (parentElement === document.body) {
|
|
15
|
+
break;
|
|
16
|
+
}
|
|
17
|
+
if (hidden(parentElement)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
parentElement = parentElement.parentElement;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
24
|
+
function getElementTabIndex(element) {
|
|
25
|
+
const tabIndex = element.getAttribute("tabindex");
|
|
26
|
+
if (tabIndex === null) {
|
|
27
|
+
return NaN;
|
|
28
|
+
}
|
|
29
|
+
return parseInt(tabIndex, 10);
|
|
30
|
+
}
|
|
31
|
+
function focusable(element) {
|
|
32
|
+
const nodeName = element.nodeName.toLowerCase();
|
|
33
|
+
const isTabIndexNotNaN = !Number.isNaN(getElementTabIndex(element));
|
|
34
|
+
const res = (TABBABLE_NODES.test(nodeName) && !element.disabled) ||
|
|
35
|
+
(element instanceof HTMLAnchorElement ? Boolean(element.href) || isTabIndexNotNaN : isTabIndexNotNaN);
|
|
36
|
+
return res && visible(element);
|
|
37
|
+
}
|
|
38
|
+
function tabbable(element) {
|
|
39
|
+
const tabIndex = getElementTabIndex(element);
|
|
40
|
+
const isTabIndexNaN = Number.isNaN(tabIndex);
|
|
41
|
+
return (isTabIndexNaN || tabIndex >= 0) && focusable(element);
|
|
42
|
+
}
|
|
43
|
+
function findTabbableDescendants(element) {
|
|
44
|
+
return Array.from(element.querySelectorAll(FOCUS_SELECTOR)).filter(tabbable);
|
|
45
|
+
}
|
|
46
|
+
function scopeTab(node, event) {
|
|
47
|
+
const _tabbable = findTabbableDescendants(node);
|
|
48
|
+
if (!_tabbable.length) {
|
|
49
|
+
event.preventDefault();
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const finalTabbable = _tabbable[event.shiftKey ? 0 : _tabbable.length - 1];
|
|
53
|
+
const leavingFinalTabbable = finalTabbable === document.activeElement || node === document.activeElement;
|
|
54
|
+
if (!leavingFinalTabbable) {
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
event.preventDefault();
|
|
58
|
+
const target = _tabbable[event.shiftKey ? _tabbable.length - 1 : 0];
|
|
59
|
+
if (target) {
|
|
60
|
+
target.focus();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
function createAriaHider(containerNode, selector = "body > :not(script)") {
|
|
64
|
+
const rootNodes = Array.from(document.querySelectorAll(selector)).map((node) => {
|
|
65
|
+
if (node.contains(containerNode)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const ariaHidden = node.getAttribute("aria-hidden");
|
|
69
|
+
if (ariaHidden === null || ariaHidden === "false") {
|
|
70
|
+
node.setAttribute("aria-hidden", "true");
|
|
71
|
+
}
|
|
72
|
+
return { node, ariaHidden };
|
|
73
|
+
});
|
|
74
|
+
return () => {
|
|
75
|
+
rootNodes.forEach((item) => {
|
|
76
|
+
if (!item) {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (item.ariaHidden === null) {
|
|
80
|
+
item.node.removeAttribute("aria-hidden");
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
item.node.setAttribute("aria-hidden", item.ariaHidden);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function useFocusTrap(active = true) {
|
|
89
|
+
const ref = (0, react_1.useRef)();
|
|
90
|
+
const restoreAria = (0, react_1.useRef)();
|
|
91
|
+
const timer = (0, react_1.useRef)();
|
|
92
|
+
const setRef = (0, react_1.useCallback)((node) => {
|
|
93
|
+
if (!active) {
|
|
94
|
+
ref.current = null;
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
if (restoreAria.current) {
|
|
98
|
+
restoreAria.current();
|
|
99
|
+
}
|
|
100
|
+
if (node) {
|
|
101
|
+
const processNode = (_node) => {
|
|
102
|
+
restoreAria.current = createAriaHider(_node);
|
|
103
|
+
let focusElement = node.querySelector("[data-autofocus]");
|
|
104
|
+
if (!focusElement) {
|
|
105
|
+
const children = Array.from(node.querySelectorAll(FOCUS_SELECTOR));
|
|
106
|
+
focusElement = children.find(tabbable) || children.find(focusable) || null;
|
|
107
|
+
if (!focusElement && focusable(node)) {
|
|
108
|
+
focusElement = node;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (focusElement) {
|
|
112
|
+
focusElement.focus();
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
// Delay processing the HTML node by a frame. This ensures focus is assigned correctly.
|
|
116
|
+
timer.current = window.setTimeout(() => {
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
118
|
+
if (node.ownerDocument) {
|
|
119
|
+
processNode(node);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
ref.current = node;
|
|
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
|
+
}
|
|
137
|
+
};
|
|
138
|
+
document.addEventListener("keydown", handler);
|
|
139
|
+
return () => {
|
|
140
|
+
document.removeEventListener("keydown", handler);
|
|
141
|
+
};
|
|
142
|
+
}, [active]);
|
|
143
|
+
(0, use_on_unmount_1.useOnUnmount)(() => clearTimeout(timer.current));
|
|
144
|
+
return setRef;
|
|
145
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIncrement = useIncrement;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useIncrement(defaultValue, step = 1) {
|
|
6
|
+
const [value, setValue] = (0, react_1.useState)(defaultValue);
|
|
7
|
+
const incrementHandler = (0, react_1.useCallback)(() => setValue((prev) => prev + step), [step]);
|
|
8
|
+
const decrementHandler = (0, react_1.useCallback)(() => setValue((prev) => prev - step), [step]);
|
|
9
|
+
const resetHandler = (0, react_1.useCallback)(() => setValue(defaultValue), [defaultValue]);
|
|
10
|
+
return [value, incrementHandler, decrementHandler, resetHandler];
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("@testing-library/react");
|
|
4
|
+
const use_increment_1 = require("./use-increment"); // Adjust the import based on your file structure
|
|
5
|
+
describe("useIncrement Hook", () => {
|
|
6
|
+
it("should initialize with the default value", () => {
|
|
7
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(5));
|
|
8
|
+
const [value] = result.current;
|
|
9
|
+
expect(value).toBe(5);
|
|
10
|
+
});
|
|
11
|
+
it("should increment the value by the step value", () => {
|
|
12
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(5, 2));
|
|
13
|
+
const [value, incrementHandler] = result.current;
|
|
14
|
+
expect(value).toBe(5);
|
|
15
|
+
(0, react_1.act)(() => {
|
|
16
|
+
incrementHandler();
|
|
17
|
+
});
|
|
18
|
+
const [newValue] = result.current;
|
|
19
|
+
expect(newValue).toBe(7); // 5 + 2
|
|
20
|
+
});
|
|
21
|
+
it("should decrement the value by the step value", () => {
|
|
22
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(10, 3));
|
|
23
|
+
const [value, , decrementHandler] = result.current;
|
|
24
|
+
expect(value).toBe(10);
|
|
25
|
+
(0, react_1.act)(() => {
|
|
26
|
+
decrementHandler();
|
|
27
|
+
});
|
|
28
|
+
const [newValue] = result.current;
|
|
29
|
+
expect(newValue).toBe(7); // 10 - 3
|
|
30
|
+
});
|
|
31
|
+
it("should reset the value to the default value", () => {
|
|
32
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(20, 5));
|
|
33
|
+
const [value, incrementHandler, , resetHandler] = result.current;
|
|
34
|
+
expect(value).toBe(20);
|
|
35
|
+
(0, react_1.act)(() => {
|
|
36
|
+
incrementHandler();
|
|
37
|
+
});
|
|
38
|
+
const [newValue] = result.current;
|
|
39
|
+
expect(newValue).toBe(25); // 20 + 5
|
|
40
|
+
(0, react_1.act)(() => {
|
|
41
|
+
resetHandler();
|
|
42
|
+
});
|
|
43
|
+
const [resetValue] = result.current;
|
|
44
|
+
expect(resetValue).toBe(20); // Reset back to default
|
|
45
|
+
});
|
|
46
|
+
it("should use a step of 1 if no step is provided", () => {
|
|
47
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_increment_1.useIncrement)(10));
|
|
48
|
+
const [value, incrementHandler, decrementHandler] = result.current;
|
|
49
|
+
expect(value).toBe(10);
|
|
50
|
+
(0, react_1.act)(() => {
|
|
51
|
+
incrementHandler();
|
|
52
|
+
});
|
|
53
|
+
const [newValue] = result.current;
|
|
54
|
+
expect(newValue).toBe(11); // 10 + 1 (default step)
|
|
55
|
+
(0, react_1.act)(() => {
|
|
56
|
+
decrementHandler();
|
|
57
|
+
});
|
|
58
|
+
const [finalValue] = result.current;
|
|
59
|
+
expect(finalValue).toBe(10); // 11 - 1
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { FocusEventHandler, MouseEventHandler, RefObject } from "react";
|
|
2
|
+
export declare function useInputFocus<T extends HTMLInputElement | HTMLTextAreaElement | HTMLElement>(focusRef: RefObject<T | null>, onBlur?: FocusEventHandler<T>, onFocus?: FocusEventHandler<T>): {
|
|
3
|
+
focused: boolean;
|
|
4
|
+
focus: MouseEventHandler<HTMLElement>;
|
|
5
|
+
onBlur: FocusEventHandler<T>;
|
|
6
|
+
onFocus: FocusEventHandler<T>;
|
|
7
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useInputFocus = useInputFocus;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useInputFocus(focusRef, onBlur, onFocus) {
|
|
6
|
+
const [focused, setFocused] = (0, react_1.useState)(false);
|
|
7
|
+
const focus = (0, react_1.useCallback)(() => {
|
|
8
|
+
const node = focusRef.current;
|
|
9
|
+
if (node) {
|
|
10
|
+
node.focus();
|
|
11
|
+
}
|
|
12
|
+
}, [focusRef]);
|
|
13
|
+
const _onFocus = (0, react_1.useCallback)((e) => {
|
|
14
|
+
setFocused(true);
|
|
15
|
+
if (onFocus) {
|
|
16
|
+
onFocus(e);
|
|
17
|
+
}
|
|
18
|
+
}, [onFocus]);
|
|
19
|
+
const _onBlur = (0, react_1.useCallback)((e) => {
|
|
20
|
+
setFocused(false);
|
|
21
|
+
if (onBlur) {
|
|
22
|
+
onBlur(e);
|
|
23
|
+
}
|
|
24
|
+
}, [onBlur]);
|
|
25
|
+
return { focused, focus, onBlur: _onBlur, onFocus: _onFocus };
|
|
26
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useIsMounted(): boolean;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsMounted = useIsMounted;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useIsMounted() {
|
|
6
|
+
const [isMounted, setIsMounted] = (0, react_1.useState)(false);
|
|
7
|
+
(0, react_1.useEffect)(() => setIsMounted(true), []);
|
|
8
|
+
return isMounted;
|
|
9
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useIsomorphicLayoutEffect = void 0;
|
|
4
|
+
const is_browser_1 = require("@uxf/core/utils/is-browser");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
exports.useIsomorphicLayoutEffect = is_browser_1.isBrowser ? react_1.useLayoutEffect : react_1.useEffect;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { RefObject } from "react";
|
|
2
|
+
type KeyPredicate = (e: KeyboardEvent) => boolean;
|
|
3
|
+
export type KeyFilter = null | undefined | string | KeyPredicate;
|
|
4
|
+
export interface UseKeyOptions<T extends HTMLElement> {
|
|
5
|
+
disabled?: boolean;
|
|
6
|
+
targetRef?: RefObject<T | null>;
|
|
7
|
+
type: "keydown" | "keyup" | "keypress";
|
|
8
|
+
}
|
|
9
|
+
export declare function useKey<T extends HTMLElement>(keyFilter: KeyFilter, callback: (e: KeyboardEvent) => void, { disabled, targetRef, type }?: Partial<UseKeyOptions<T>>): void;
|
|
10
|
+
export {};
|
package/hooks/use-key.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useKey = useKey;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const createKeyPredicate = (keyFilter) => typeof keyFilter === "function"
|
|
6
|
+
? keyFilter
|
|
7
|
+
: typeof keyFilter === "string"
|
|
8
|
+
? (e) => e.key === keyFilter
|
|
9
|
+
: () => false;
|
|
10
|
+
function useKey(keyFilter, callback, { disabled, targetRef, type = "keydown" } = {}) {
|
|
11
|
+
(0, react_1.useEffect)(() => {
|
|
12
|
+
if (disabled) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const thisNode = (targetRef ? targetRef.current : document);
|
|
16
|
+
const predicate = createKeyPredicate(keyFilter);
|
|
17
|
+
const handler = (e) => {
|
|
18
|
+
if (predicate(e)) {
|
|
19
|
+
callback(e);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
thisNode === null || thisNode === void 0 ? void 0 : thisNode.addEventListener(type, handler);
|
|
23
|
+
return () => {
|
|
24
|
+
thisNode === null || thisNode === void 0 ? void 0 : thisNode.removeEventListener(type, handler);
|
|
25
|
+
};
|
|
26
|
+
}, [callback, disabled, keyFilter, targetRef, type]);
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useLatest<T>(current: T): import("react").MutableRefObject<T>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useLatest = useLatest;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useLatest(current) {
|
|
6
|
+
const storedValue = (0, react_1.useRef)(current);
|
|
7
|
+
storedValue.current = current;
|
|
8
|
+
return storedValue;
|
|
9
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useMediaQuery(mediaQueryString: string): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
"use client";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.useMediaQuery = useMediaQuery;
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
function useMediaQuery(mediaQueryString) {
|
|
7
|
+
const [isMatched, setIsMatched] = (0, react_1.useState)(false);
|
|
8
|
+
(0, react_1.useEffect)(() => {
|
|
9
|
+
const observer = new ResizeObserver(() => {
|
|
10
|
+
setIsMatched(window.matchMedia(mediaQueryString).matches);
|
|
11
|
+
});
|
|
12
|
+
observer.observe(document.body);
|
|
13
|
+
return () => {
|
|
14
|
+
observer.unobserve(document.body);
|
|
15
|
+
};
|
|
16
|
+
}, [mediaQueryString]);
|
|
17
|
+
return isMatched;
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useMinWindowWidth(minWidth: number, debounce?: number): boolean | undefined;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useMinWindowWidth = useMinWindowWidth;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const use_raf_state_1 = require("./use-raf-state");
|
|
6
|
+
function useMinWindowWidth(minWidth, debounce = 0) {
|
|
7
|
+
const [state, setState] = (0, use_raf_state_1.useRafState)();
|
|
8
|
+
(0, react_1.useEffect)(() => {
|
|
9
|
+
const stateHandler = () => setState(window.innerWidth >= minWidth);
|
|
10
|
+
let timer;
|
|
11
|
+
const handler = () => {
|
|
12
|
+
if (debounce > 0) {
|
|
13
|
+
window.clearTimeout(timer);
|
|
14
|
+
timer = window.setTimeout(stateHandler, debounce);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
stateHandler();
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
stateHandler();
|
|
21
|
+
window.addEventListener("resize", handler);
|
|
22
|
+
return () => {
|
|
23
|
+
window.removeEventListener("resize", handler);
|
|
24
|
+
window.clearTimeout(timer);
|
|
25
|
+
};
|
|
26
|
+
}, [debounce, minWidth, setState]);
|
|
27
|
+
return state;
|
|
28
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useMouseDragToScroll = useMouseDragToScroll;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useMouseDragToScroll(containerRef) {
|
|
6
|
+
const position = (0, react_1.useRef)({ left: 0, top: 0, x: 0, y: 0 });
|
|
7
|
+
const [style, setStyle] = (0, react_1.useState)();
|
|
8
|
+
(0, react_1.useEffect)(() => {
|
|
9
|
+
const node = containerRef.current;
|
|
10
|
+
if (!node) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
const mouseMoveHandler = (e) => {
|
|
14
|
+
const dx = e.clientX - position.current.x;
|
|
15
|
+
const dy = e.clientY - position.current.y;
|
|
16
|
+
node.scrollLeft = position.current.left - dx;
|
|
17
|
+
node.scrollTop = position.current.top - dy;
|
|
18
|
+
};
|
|
19
|
+
const mouseUpHandler = () => {
|
|
20
|
+
setStyle((prev) => ({ ...prev, cursor: "grab", userSelect: "unset" }));
|
|
21
|
+
document.removeEventListener("mousemove", mouseMoveHandler);
|
|
22
|
+
document.removeEventListener("mouseup", mouseUpHandler);
|
|
23
|
+
};
|
|
24
|
+
const mouseDownHandler = (e) => {
|
|
25
|
+
position.current = { left: node.scrollLeft, top: node.scrollTop, x: e.clientX, y: e.clientY };
|
|
26
|
+
setStyle((prev) => ({ ...prev, cursor: "grabbing", userSelect: "none" }));
|
|
27
|
+
document.addEventListener("mousemove", mouseMoveHandler);
|
|
28
|
+
document.addEventListener("mouseup", mouseUpHandler);
|
|
29
|
+
};
|
|
30
|
+
node.addEventListener("mousedown", mouseDownHandler);
|
|
31
|
+
return () => {
|
|
32
|
+
node.removeEventListener("mousedown", mouseDownHandler);
|
|
33
|
+
document.removeEventListener("mousedown", mouseMoveHandler);
|
|
34
|
+
document.removeEventListener("mousedown", mouseUpHandler);
|
|
35
|
+
};
|
|
36
|
+
}, [containerRef]);
|
|
37
|
+
return style;
|
|
38
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnMount = useOnMount;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useOnMount(effect) {
|
|
6
|
+
(0, react_1.useEffect)(() => {
|
|
7
|
+
return effect();
|
|
8
|
+
}, []); // eslint-disable-line react-hooks/exhaustive-deps
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnUnmount = useOnUnmount;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useOnUnmount(effect) {
|
|
6
|
+
const effectRef = (0, react_1.useRef)(effect);
|
|
7
|
+
// update the ref each render so if it changed the newest callback will be invoked
|
|
8
|
+
effectRef.current = effect;
|
|
9
|
+
(0, react_1.useEffect)(() => {
|
|
10
|
+
return () => {
|
|
11
|
+
effectRef.current();
|
|
12
|
+
};
|
|
13
|
+
}, []);
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useOnUpdate = useOnUpdate;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useOnUpdate(effect, deps) {
|
|
6
|
+
const firstRun = (0, react_1.useRef)(true);
|
|
7
|
+
(0, react_1.useEffect)(() => {
|
|
8
|
+
if (firstRun.current) {
|
|
9
|
+
firstRun.current = false;
|
|
10
|
+
return;
|
|
11
|
+
}
|
|
12
|
+
return effect();
|
|
13
|
+
}, deps); // eslint-disable-line react-hooks/exhaustive-deps
|
|
14
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePagination = void 0;
|
|
4
|
+
const pagination_1 = require("@uxf/core/utils/pagination");
|
|
5
|
+
const react_1 = require("react");
|
|
6
|
+
const usePagination = (config) => {
|
|
7
|
+
return (0, react_1.useMemo)(() => (0, pagination_1.paginationGenerate)(config),
|
|
8
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
9
|
+
[
|
|
10
|
+
config.boundaryCount,
|
|
11
|
+
config.siblingCount,
|
|
12
|
+
config.count,
|
|
13
|
+
config.page,
|
|
14
|
+
config.hideNextButton,
|
|
15
|
+
config.hidePrevButton,
|
|
16
|
+
config.showFirstButton,
|
|
17
|
+
config.showLastButton,
|
|
18
|
+
]);
|
|
19
|
+
};
|
|
20
|
+
exports.usePagination = usePagination;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const react_1 = require("@testing-library/react");
|
|
4
|
+
const use_pagination_1 = require("./use-pagination");
|
|
5
|
+
describe("usePagination Hook", () => {
|
|
6
|
+
it("should return correct pagination items based on config", () => {
|
|
7
|
+
const mockConfig = {
|
|
8
|
+
showFirstButton: true,
|
|
9
|
+
hidePrevButton: false,
|
|
10
|
+
boundaryCount: 2,
|
|
11
|
+
siblingCount: 1,
|
|
12
|
+
hideNextButton: false,
|
|
13
|
+
showLastButton: true,
|
|
14
|
+
page: 11,
|
|
15
|
+
count: 100,
|
|
16
|
+
};
|
|
17
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_pagination_1.usePagination)(mockConfig));
|
|
18
|
+
const paginationItems = result.current;
|
|
19
|
+
expect(paginationItems[0]).toBe("first");
|
|
20
|
+
expect(paginationItems[1]).toBe("previous");
|
|
21
|
+
expect(paginationItems[2]).toBe(1);
|
|
22
|
+
expect(paginationItems[3]).toBe(2);
|
|
23
|
+
expect(paginationItems[4]).toBe("start-ellipsis");
|
|
24
|
+
expect(paginationItems[5]).toBe(10);
|
|
25
|
+
expect(paginationItems[6]).toBe(11);
|
|
26
|
+
expect(paginationItems[7]).toBe(12);
|
|
27
|
+
expect(paginationItems[8]).toBe("end-ellipsis");
|
|
28
|
+
expect(paginationItems[9]).toBe(99);
|
|
29
|
+
expect(paginationItems[10]).toBe(100);
|
|
30
|
+
expect(paginationItems[11]).toBe("next");
|
|
31
|
+
expect(paginationItems[12]).toBe("last");
|
|
32
|
+
});
|
|
33
|
+
it("should return correct pagination items with hidden 'Previous' and 'Next' buttons", () => {
|
|
34
|
+
const mockConfigHidePrevNext = {
|
|
35
|
+
showFirstButton: true,
|
|
36
|
+
hidePrevButton: true,
|
|
37
|
+
boundaryCount: 1,
|
|
38
|
+
siblingCount: 2,
|
|
39
|
+
hideNextButton: true,
|
|
40
|
+
showLastButton: true,
|
|
41
|
+
page: 4,
|
|
42
|
+
count: 20,
|
|
43
|
+
};
|
|
44
|
+
const { result } = (0, react_1.renderHook)(() => (0, use_pagination_1.usePagination)(mockConfigHidePrevNext));
|
|
45
|
+
const paginationItems = result.current;
|
|
46
|
+
expect(paginationItems[0]).toBe("first");
|
|
47
|
+
expect(paginationItems[1]).toBe(1);
|
|
48
|
+
expect(paginationItems[2]).toBe(2);
|
|
49
|
+
expect(paginationItems[3]).toBe(3);
|
|
50
|
+
expect(paginationItems[4]).toBe(4);
|
|
51
|
+
expect(paginationItems[5]).toBe(5);
|
|
52
|
+
expect(paginationItems[6]).toBe(6);
|
|
53
|
+
expect(paginationItems[7]).toBe(7);
|
|
54
|
+
expect(paginationItems[8]).toBe("end-ellipsis");
|
|
55
|
+
expect(paginationItems[9]).toBe(20);
|
|
56
|
+
expect(paginationItems[10]).toBe("last");
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function usePrevious<T>(current: T): import("react").MutableRefObject<T>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePrevious = usePrevious;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function usePrevious(current) {
|
|
6
|
+
const storedValue = (0, react_1.useRef)(current);
|
|
7
|
+
(0, react_1.useEffect)(() => {
|
|
8
|
+
storedValue.current = current;
|
|
9
|
+
});
|
|
10
|
+
return storedValue;
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useRafState = useRafState;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
const use_on_unmount_1 = require("./use-on-unmount");
|
|
6
|
+
function useRafState(initialState) {
|
|
7
|
+
const frame = (0, react_1.useRef)(0);
|
|
8
|
+
const [state, setState] = (0, react_1.useState)(initialState);
|
|
9
|
+
const setRafState = (0, react_1.useCallback)((value) => {
|
|
10
|
+
cancelAnimationFrame(frame.current);
|
|
11
|
+
frame.current = requestAnimationFrame(() => {
|
|
12
|
+
setState(value);
|
|
13
|
+
});
|
|
14
|
+
}, []);
|
|
15
|
+
(0, use_on_unmount_1.useOnUnmount)(() => cancelAnimationFrame(frame.current));
|
|
16
|
+
return [state, setRafState];
|
|
17
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useToggle = useToggle;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useToggle(defaultValue) {
|
|
6
|
+
const [isOn, setIsOn] = (0, react_1.useState)(defaultValue);
|
|
7
|
+
const toggleHandler = (0, react_1.useCallback)(() => setIsOn((prev) => !prev), []);
|
|
8
|
+
const setTrueHandler = (0, react_1.useCallback)(() => setIsOn(true), []);
|
|
9
|
+
const setFalseHandler = (0, react_1.useCallback)(() => setIsOn(false), []);
|
|
10
|
+
return [isOn, toggleHandler, setTrueHandler, setFalseHandler];
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|