@octanejs/mantine-hooks 0.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/LICENSE +21 -0
- package/README.md +29 -0
- package/package.json +45 -0
- package/src/index.ts +259 -0
- package/src/internal.ts +6 -0
- package/src/use-click-outside/use-click-outside.ts +57 -0
- package/src/use-clipboard/use-clipboard.ts +69 -0
- package/src/use-collapse/use-collapse.ts +162 -0
- package/src/use-collapse/use-horizontal-collapse.ts +164 -0
- package/src/use-color-scheme/use-color-scheme.ts +13 -0
- package/src/use-counter/use-counter.ts +59 -0
- package/src/use-debounced-callback/use-debounced-callback.ts +159 -0
- package/src/use-debounced-state/use-debounced-state.ts +44 -0
- package/src/use-debounced-value/use-debounced-value.ts +71 -0
- package/src/use-did-update/use-did-update.ts +26 -0
- package/src/use-disclosure/use-disclosure.ts +57 -0
- package/src/use-document-title/use-document-title.ts +9 -0
- package/src/use-document-visibility/use-document-visibility.ts +14 -0
- package/src/use-drag/use-drag.ts +463 -0
- package/src/use-event-listener/use-event-listener.ts +47 -0
- package/src/use-eye-dropper/use-eye-dropper.ts +41 -0
- package/src/use-favicon/use-favicon.ts +41 -0
- package/src/use-fetch/use-fetch.ts +85 -0
- package/src/use-file-dialog/use-file-dialog.ts +140 -0
- package/src/use-floating-window/use-floating-window.ts +431 -0
- package/src/use-focus-return/use-focus-return.ts +65 -0
- package/src/use-focus-trap/scope-tab.ts +36 -0
- package/src/use-focus-trap/tabbable.ts +65 -0
- package/src/use-focus-trap/use-focus-trap.ts +84 -0
- package/src/use-focus-within/use-focus-within.ts +90 -0
- package/src/use-force-update/use-force-update.ts +8 -0
- package/src/use-fullscreen/use-fullscreen.ts +153 -0
- package/src/use-hash/use-hash.ts +42 -0
- package/src/use-headroom/use-headroom.ts +160 -0
- package/src/use-hotkeys/parse-hotkey.ts +130 -0
- package/src/use-hotkeys/use-hotkeys.ts +63 -0
- package/src/use-hover/use-hover.ts +57 -0
- package/src/use-id/use-id.ts +23 -0
- package/src/use-idle/use-idle.ts +54 -0
- package/src/use-in-viewport/use-in-viewport.ts +34 -0
- package/src/use-input-state/use-input-state.ts +41 -0
- package/src/use-intersection/use-intersection.ts +41 -0
- package/src/use-interval/use-interval.ts +84 -0
- package/src/use-is-first-render/use-is-first-render.ts +12 -0
- package/src/use-isomorphic-effect/use-isomorphic-effect.ts +5 -0
- package/src/use-list-state/use-list-state.ts +157 -0
- package/src/use-local-storage/create-storage.ts +200 -0
- package/src/use-local-storage/use-local-storage.ts +19 -0
- package/src/use-logger/use-logger.ts +16 -0
- package/src/use-long-press/use-long-press.ts +188 -0
- package/src/use-map/use-map.ts +28 -0
- package/src/use-mask/use-mask.ts +1049 -0
- package/src/use-media-query/use-media-query.ts +57 -0
- package/src/use-merged-ref/use-merged-ref.ts +53 -0
- package/src/use-mounted/use-mounted.ts +7 -0
- package/src/use-mouse/use-mouse.ts +91 -0
- package/src/use-move/use-move.ts +165 -0
- package/src/use-mutation-observer/use-mutation-observer.ts +61 -0
- package/src/use-network/use-network.ts +70 -0
- package/src/use-orientation/use-orientation.ts +87 -0
- package/src/use-os/use-os.ts +93 -0
- package/src/use-page-leave/use-page-leave.ts +10 -0
- package/src/use-pagination/use-pagination.ts +149 -0
- package/src/use-previous/use-previous.ts +11 -0
- package/src/use-queue/use-queue.ts +71 -0
- package/src/use-radial-move/use-radial-move.ts +161 -0
- package/src/use-reduced-motion/use-reduced-motion.ts +6 -0
- package/src/use-resize-observer/use-resize-observer.ts +95 -0
- package/src/use-roving-index/use-roving-index.ts +386 -0
- package/src/use-scroll-direction/use-scroll-direction.ts +41 -0
- package/src/use-scroll-into-view/use-scroll-into-view.ts +294 -0
- package/src/use-scroll-spy/use-scroll-spy.ts +155 -0
- package/src/use-scroller/use-scroller.ts +225 -0
- package/src/use-selection/use-selection.ts +120 -0
- package/src/use-session-storage/use-session-storage.ts +19 -0
- package/src/use-set/use-set.ts +88 -0
- package/src/use-set-state/use-set-state.ts +29 -0
- package/src/use-shallow-effect/use-shallow-effect.ts +40 -0
- package/src/use-splitter/use-splitter.ts +1242 -0
- package/src/use-state-history/use-state-history.ts +70 -0
- package/src/use-text-selection/use-text-selection.ts +20 -0
- package/src/use-throttled-callback/use-throttled-callback.ts +60 -0
- package/src/use-throttled-state/use-throttled-state.ts +12 -0
- package/src/use-throttled-value/use-throttled-value.ts +23 -0
- package/src/use-timeout/use-timeout.ts +56 -0
- package/src/use-toggle/use-toggle.ts +23 -0
- package/src/use-uncontrolled/use-uncontrolled.ts +53 -0
- package/src/use-validated-state/use-validated-state.ts +50 -0
- package/src/use-viewport-size/use-viewport-size.ts +23 -0
- package/src/use-window-event/use-window-event.ts +16 -0
- package/src/use-window-scroll/use-window-scroll.ts +49 -0
- package/src/utils/clamp/clamp.ts +15 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/lower-first/lower-first.ts +3 -0
- package/src/utils/random-id/random-id.ts +3 -0
- package/src/utils/range/range.ts +10 -0
- package/src/utils/shallow-equal/shallow-equal.ts +34 -0
- package/src/utils/upper-first/upper-first.ts +3 -0
- package/src/utils/use-callback-ref/use-callback-ref.ts +11 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { useCallback, useMemo, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export interface UseListStateHandlers<T> {
|
|
4
|
+
setState: React.Dispatch<React.SetStateAction<T[]>>;
|
|
5
|
+
append: (...items: T[]) => void;
|
|
6
|
+
prepend: (...items: T[]) => void;
|
|
7
|
+
insert: (index: number, ...items: T[]) => void;
|
|
8
|
+
pop: () => void;
|
|
9
|
+
shift: () => void;
|
|
10
|
+
apply: (fn: (item: T, index?: number) => T) => void;
|
|
11
|
+
applyWhere: (
|
|
12
|
+
condition: (item: T, index: number) => boolean,
|
|
13
|
+
fn: (item: T, index?: number) => T,
|
|
14
|
+
) => void;
|
|
15
|
+
remove: (...indices: number[]) => void;
|
|
16
|
+
reorder: ({ from, to }: { from: number; to: number }) => void;
|
|
17
|
+
swap: ({ from, to }: { from: number; to: number }) => void;
|
|
18
|
+
setItem: (index: number, item: T) => void;
|
|
19
|
+
setItemProp: <K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) => void;
|
|
20
|
+
filter: (fn: (item: T, i: number) => boolean) => void;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export type UseListStateReturnValue<T> = [T[], UseListStateHandlers<T>];
|
|
24
|
+
|
|
25
|
+
export function useListState<T>(initialValue: T[] | (() => T[]) = []): UseListStateReturnValue<T> {
|
|
26
|
+
const [state, setState] = useState(initialValue);
|
|
27
|
+
|
|
28
|
+
const append = useCallback((...items: T[]) => setState((current) => [...current, ...items]), []);
|
|
29
|
+
|
|
30
|
+
const prepend = useCallback((...items: T[]) => setState((current) => [...items, ...current]), []);
|
|
31
|
+
|
|
32
|
+
const insert = useCallback(
|
|
33
|
+
(index: number, ...items: T[]) =>
|
|
34
|
+
setState((current) => [...current.slice(0, index), ...items, ...current.slice(index)]),
|
|
35
|
+
[],
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const apply = useCallback(
|
|
39
|
+
(fn: (item: T, index?: number) => T) =>
|
|
40
|
+
setState((current) => current.map((item, index) => fn(item, index))),
|
|
41
|
+
[],
|
|
42
|
+
);
|
|
43
|
+
|
|
44
|
+
const remove = useCallback(
|
|
45
|
+
(...indices: number[]) =>
|
|
46
|
+
setState((current) => current.filter((_, index) => !indices.includes(index))),
|
|
47
|
+
[],
|
|
48
|
+
);
|
|
49
|
+
|
|
50
|
+
const pop = useCallback(
|
|
51
|
+
() =>
|
|
52
|
+
setState((current) => {
|
|
53
|
+
const cloned = [...current];
|
|
54
|
+
cloned.pop();
|
|
55
|
+
return cloned;
|
|
56
|
+
}),
|
|
57
|
+
[],
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
const shift = useCallback(
|
|
61
|
+
() =>
|
|
62
|
+
setState((current) => {
|
|
63
|
+
const cloned = [...current];
|
|
64
|
+
cloned.shift();
|
|
65
|
+
return cloned;
|
|
66
|
+
}),
|
|
67
|
+
[],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const reorder = useCallback(
|
|
71
|
+
({ from, to }: { from: number; to: number }) =>
|
|
72
|
+
setState((current) => {
|
|
73
|
+
const cloned = [...current];
|
|
74
|
+
const item = current[from];
|
|
75
|
+
|
|
76
|
+
cloned.splice(from, 1);
|
|
77
|
+
cloned.splice(to, 0, item);
|
|
78
|
+
|
|
79
|
+
return cloned;
|
|
80
|
+
}),
|
|
81
|
+
[],
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
const swap = useCallback(
|
|
85
|
+
({ from, to }: { from: number; to: number }) =>
|
|
86
|
+
setState((current) => {
|
|
87
|
+
const cloned = [...current];
|
|
88
|
+
const fromItem = cloned[from];
|
|
89
|
+
const toItem = cloned[to];
|
|
90
|
+
|
|
91
|
+
cloned.splice(to, 1, fromItem);
|
|
92
|
+
cloned.splice(from, 1, toItem);
|
|
93
|
+
|
|
94
|
+
return cloned;
|
|
95
|
+
}),
|
|
96
|
+
[],
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
const setItem = useCallback(
|
|
100
|
+
(index: number, item: T) =>
|
|
101
|
+
setState((current) => {
|
|
102
|
+
const cloned = [...current];
|
|
103
|
+
cloned[index] = item;
|
|
104
|
+
return cloned;
|
|
105
|
+
}),
|
|
106
|
+
[],
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const setItemProp = useCallback(
|
|
110
|
+
<K extends keyof T, U extends T[K]>(index: number, prop: K, value: U) =>
|
|
111
|
+
setState((current) => {
|
|
112
|
+
const cloned = [...current];
|
|
113
|
+
cloned[index] = { ...cloned[index], [prop]: value };
|
|
114
|
+
return cloned;
|
|
115
|
+
}),
|
|
116
|
+
[],
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const applyWhere = useCallback(
|
|
120
|
+
(condition: (item: T, index: number) => boolean, fn: (item: T, index?: number) => T) =>
|
|
121
|
+
setState((current) =>
|
|
122
|
+
current.map((item, index) => (condition(item, index) ? fn(item, index) : item)),
|
|
123
|
+
),
|
|
124
|
+
[],
|
|
125
|
+
);
|
|
126
|
+
|
|
127
|
+
const filter = useCallback((fn: (item: T, i: number) => boolean) => {
|
|
128
|
+
setState((current) => current.filter(fn));
|
|
129
|
+
}, []);
|
|
130
|
+
|
|
131
|
+
const handlers = useMemo(
|
|
132
|
+
() => ({
|
|
133
|
+
setState,
|
|
134
|
+
append,
|
|
135
|
+
prepend,
|
|
136
|
+
insert,
|
|
137
|
+
pop,
|
|
138
|
+
shift,
|
|
139
|
+
apply,
|
|
140
|
+
applyWhere,
|
|
141
|
+
remove,
|
|
142
|
+
reorder,
|
|
143
|
+
swap,
|
|
144
|
+
setItem,
|
|
145
|
+
setItemProp,
|
|
146
|
+
filter,
|
|
147
|
+
}),
|
|
148
|
+
[],
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
return [state, handlers];
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export namespace useListState {
|
|
155
|
+
export type ReturnValue<T> = UseListStateReturnValue<T>;
|
|
156
|
+
export type Handlers<T> = UseListStateHandlers<T>;
|
|
157
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
/* oxlint-disable no-console */
|
|
2
|
+
import { useCallback, useEffect, useState } from 'octane';
|
|
3
|
+
import { useWindowEvent } from '../use-window-event/use-window-event';
|
|
4
|
+
|
|
5
|
+
export type StorageType = 'localStorage' | 'sessionStorage';
|
|
6
|
+
|
|
7
|
+
export interface UseStorageOptions<T> {
|
|
8
|
+
/** Storage key */
|
|
9
|
+
key: string;
|
|
10
|
+
|
|
11
|
+
/** Default value that will be set if value is not found in storage */
|
|
12
|
+
defaultValue?: T;
|
|
13
|
+
|
|
14
|
+
/** If set to true, value will be updated in useEffect after mount. Default value is true. */
|
|
15
|
+
getInitialValueInEffect?: boolean;
|
|
16
|
+
|
|
17
|
+
/** Determines whether the value must be synced between browser tabs, `true` by default */
|
|
18
|
+
sync?: boolean;
|
|
19
|
+
|
|
20
|
+
/** Function to serialize value into string to be save in storage */
|
|
21
|
+
serialize?: (value: T) => string;
|
|
22
|
+
|
|
23
|
+
/** Function to deserialize string value from storage to value */
|
|
24
|
+
deserialize?: (value: string | undefined) => T;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function serializeJSON<T>(value: T, hookName: string = 'use-local-storage') {
|
|
28
|
+
try {
|
|
29
|
+
return JSON.stringify(value);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error(`@mantine/hooks ${hookName}: Failed to serialize the value`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function deserializeJSON(value: string | undefined) {
|
|
36
|
+
try {
|
|
37
|
+
return value && JSON.parse(value);
|
|
38
|
+
} catch {
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function createStorageHandler(type: StorageType) {
|
|
44
|
+
const getItem = (key: string) => {
|
|
45
|
+
try {
|
|
46
|
+
return window[type].getItem(key);
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.warn('use-local-storage: Failed to get value from storage, localStorage is blocked');
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const setItem = (key: string, value: string) => {
|
|
54
|
+
try {
|
|
55
|
+
window[type].setItem(key, value);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.warn('use-local-storage: Failed to set value to storage, localStorage is blocked');
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
const removeItem = (key: string) => {
|
|
62
|
+
try {
|
|
63
|
+
window[type].removeItem(key);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
console.warn(
|
|
66
|
+
'use-local-storage: Failed to remove value from storage, localStorage is blocked',
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return { getItem, setItem, removeItem };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export type UseStorageReturnValue<T> = [
|
|
75
|
+
T, // current value
|
|
76
|
+
(val: T | ((prevState: T) => T)) => void, // callback to set value in storage
|
|
77
|
+
() => void, // callback to remove value from storage
|
|
78
|
+
];
|
|
79
|
+
|
|
80
|
+
export function createStorage<T>(type: StorageType, hookName: string) {
|
|
81
|
+
const eventName = type === 'localStorage' ? 'mantine-local-storage' : 'mantine-session-storage';
|
|
82
|
+
const { getItem, setItem, removeItem } = createStorageHandler(type);
|
|
83
|
+
|
|
84
|
+
return function useStorage({
|
|
85
|
+
key,
|
|
86
|
+
defaultValue,
|
|
87
|
+
getInitialValueInEffect = true,
|
|
88
|
+
sync = true,
|
|
89
|
+
deserialize = deserializeJSON,
|
|
90
|
+
serialize = (value: T) => serializeJSON(value, hookName),
|
|
91
|
+
}: UseStorageOptions<T>): UseStorageReturnValue<T> {
|
|
92
|
+
const readStorageValue = useCallback(
|
|
93
|
+
(skipStorage?: boolean): T => {
|
|
94
|
+
let storageBlockedOrSkipped;
|
|
95
|
+
|
|
96
|
+
try {
|
|
97
|
+
storageBlockedOrSkipped =
|
|
98
|
+
typeof window === 'undefined' ||
|
|
99
|
+
!(type in window) ||
|
|
100
|
+
window[type] === null ||
|
|
101
|
+
!!skipStorage;
|
|
102
|
+
} catch (_e) {
|
|
103
|
+
storageBlockedOrSkipped = true;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (storageBlockedOrSkipped) {
|
|
107
|
+
return defaultValue as T;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const storageValue = getItem(key);
|
|
111
|
+
return storageValue !== null ? deserialize(storageValue) : (defaultValue as T);
|
|
112
|
+
},
|
|
113
|
+
[key, defaultValue],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const [value, setValue] = useState<T>(readStorageValue(getInitialValueInEffect));
|
|
117
|
+
|
|
118
|
+
const setStorageValue = useCallback(
|
|
119
|
+
(val: T | ((prevState: T) => T)) => {
|
|
120
|
+
if (val instanceof Function) {
|
|
121
|
+
setValue((current) => {
|
|
122
|
+
const result = val(current);
|
|
123
|
+
setItem(key, serialize(result));
|
|
124
|
+
// Defer dispatching this event to avoid the handler being called during render.
|
|
125
|
+
queueMicrotask(() => {
|
|
126
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: result } }));
|
|
127
|
+
});
|
|
128
|
+
return result;
|
|
129
|
+
});
|
|
130
|
+
} else {
|
|
131
|
+
setItem(key, serialize(val));
|
|
132
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: val } }));
|
|
133
|
+
setValue(val);
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
[key],
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const removeStorageValue = useCallback(() => {
|
|
140
|
+
removeItem(key);
|
|
141
|
+
setValue(defaultValue as T);
|
|
142
|
+
window.dispatchEvent(new CustomEvent(eventName, { detail: { key, value: defaultValue } }));
|
|
143
|
+
}, [key, defaultValue]);
|
|
144
|
+
|
|
145
|
+
useWindowEvent('storage', (event) => {
|
|
146
|
+
if (sync) {
|
|
147
|
+
if (event.storageArea === window[type] && event.key === key) {
|
|
148
|
+
setValue(deserialize(event.newValue ?? undefined));
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
useWindowEvent(eventName, (event) => {
|
|
154
|
+
if (sync) {
|
|
155
|
+
if (event.detail.key === key) {
|
|
156
|
+
setValue(event.detail.value);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
useEffect(() => {
|
|
162
|
+
if (defaultValue !== undefined && value === undefined) {
|
|
163
|
+
setStorageValue(defaultValue);
|
|
164
|
+
}
|
|
165
|
+
}, [defaultValue, value, setStorageValue]);
|
|
166
|
+
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
const val = readStorageValue();
|
|
169
|
+
val !== undefined && setStorageValue(val);
|
|
170
|
+
}, [key]);
|
|
171
|
+
|
|
172
|
+
return [value === undefined ? (defaultValue as T) : value, setStorageValue, removeStorageValue];
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function readValue(type: StorageType) {
|
|
177
|
+
const { getItem } = createStorageHandler(type);
|
|
178
|
+
|
|
179
|
+
return function read<T>({
|
|
180
|
+
key,
|
|
181
|
+
defaultValue,
|
|
182
|
+
deserialize = deserializeJSON,
|
|
183
|
+
}: UseStorageOptions<T>) {
|
|
184
|
+
let storageBlockedOrSkipped;
|
|
185
|
+
|
|
186
|
+
try {
|
|
187
|
+
storageBlockedOrSkipped =
|
|
188
|
+
typeof window === 'undefined' || !(type in window) || window[type] === null;
|
|
189
|
+
} catch (_e) {
|
|
190
|
+
storageBlockedOrSkipped = true;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (storageBlockedOrSkipped) {
|
|
194
|
+
return defaultValue as T;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const storageValue = getItem(key);
|
|
198
|
+
return storageValue !== null ? deserialize(storageValue) : (defaultValue as T);
|
|
199
|
+
};
|
|
200
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { createStorage, readValue } from './create-storage';
|
|
2
|
+
import type { UseStorageOptions, UseStorageReturnValue } from './create-storage';
|
|
3
|
+
|
|
4
|
+
export function useLocalStorage<T = string>(
|
|
5
|
+
props: UseStorageOptions<T> & { defaultValue: T },
|
|
6
|
+
): UseStorageReturnValue<T>;
|
|
7
|
+
export function useLocalStorage<T = string>(
|
|
8
|
+
props: UseStorageOptions<T>,
|
|
9
|
+
): UseStorageReturnValue<T | undefined>;
|
|
10
|
+
export function useLocalStorage<T = string>(props: UseStorageOptions<T>) {
|
|
11
|
+
return createStorage<T>('localStorage', 'use-local-storage')(props);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ReadStorageValue {
|
|
15
|
+
<T>(options: UseStorageOptions<T> & { defaultValue: T }): T;
|
|
16
|
+
<T>(options: UseStorageOptions<T>): T | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export const readLocalStorageValue: ReadStorageValue = readValue('localStorage');
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* oxlint-disable no-console */
|
|
2
|
+
import { useEffect } from 'octane';
|
|
3
|
+
import { useDidUpdate } from '../use-did-update/use-did-update';
|
|
4
|
+
|
|
5
|
+
export function useLogger(componentName: string, props: any[]) {
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
console.log(`${componentName} mounted`, ...props);
|
|
8
|
+
return () => console.log(`${componentName} unmounted`);
|
|
9
|
+
}, []);
|
|
10
|
+
|
|
11
|
+
useDidUpdate(() => {
|
|
12
|
+
console.log(`${componentName} updated`, ...props);
|
|
13
|
+
}, props);
|
|
14
|
+
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import { useEffect, useMemo, useRef } from 'octane';
|
|
3
|
+
|
|
4
|
+
export type UseLongPressEvent = 'mouse' | 'touch';
|
|
5
|
+
|
|
6
|
+
export interface UseLongPressOptions {
|
|
7
|
+
/** Time in milliseconds to trigger the long press, default is 400ms */
|
|
8
|
+
threshold?: number;
|
|
9
|
+
|
|
10
|
+
/** Input types that can trigger the long press, `['mouse', 'touch']` by default */
|
|
11
|
+
events?: UseLongPressEvent[];
|
|
12
|
+
|
|
13
|
+
/** If set, the long press is canceled when the pointer moves further than the given distance in px from the start position. `true` uses a 10px threshold, a number sets a custom threshold. `false` by default */
|
|
14
|
+
cancelOnMove?: boolean | number;
|
|
15
|
+
|
|
16
|
+
/** Callback triggered when the long press starts */
|
|
17
|
+
onStart?: (event: React.MouseEvent | React.TouchEvent) => void;
|
|
18
|
+
|
|
19
|
+
/** Callback triggered when the long press finishes */
|
|
20
|
+
onFinish?: (event: React.MouseEvent | React.TouchEvent) => void;
|
|
21
|
+
|
|
22
|
+
/** Callback triggered when the long press is canceled */
|
|
23
|
+
onCancel?: (event: React.MouseEvent | React.TouchEvent) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UseLongPressReturnValue {
|
|
27
|
+
onMouseDown?: (event: React.MouseEvent) => void;
|
|
28
|
+
onMouseUp?: (event: React.MouseEvent) => void;
|
|
29
|
+
onMouseLeave?: (event: React.MouseEvent) => void;
|
|
30
|
+
onMouseMove?: (event: React.MouseEvent) => void;
|
|
31
|
+
onTouchStart?: (event: React.TouchEvent) => void;
|
|
32
|
+
onTouchEnd?: (event: React.TouchEvent) => void;
|
|
33
|
+
onTouchCancel?: (event: React.TouchEvent) => void;
|
|
34
|
+
onTouchMove?: (event: React.TouchEvent) => void;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const DEFAULT_EVENTS: UseLongPressEvent[] = ['mouse', 'touch'];
|
|
38
|
+
const DEFAULT_MOVE_THRESHOLD = 10;
|
|
39
|
+
|
|
40
|
+
export function useLongPress(
|
|
41
|
+
onLongPress: (event: React.MouseEvent | React.TouchEvent) => void,
|
|
42
|
+
options: UseLongPressOptions = {},
|
|
43
|
+
): UseLongPressReturnValue {
|
|
44
|
+
const {
|
|
45
|
+
threshold = 400,
|
|
46
|
+
events = DEFAULT_EVENTS,
|
|
47
|
+
cancelOnMove = false,
|
|
48
|
+
onStart,
|
|
49
|
+
onFinish,
|
|
50
|
+
onCancel,
|
|
51
|
+
} = options;
|
|
52
|
+
const isLongPressActive = useRef(false);
|
|
53
|
+
const isPressed = useRef(false);
|
|
54
|
+
const timeout = useRef<number>(-1);
|
|
55
|
+
const startPosition = useRef<{ x: number; y: number } | null>(null);
|
|
56
|
+
|
|
57
|
+
useEffect(() => () => window.clearTimeout(timeout.current), []);
|
|
58
|
+
|
|
59
|
+
const eventsKey = events.join(',');
|
|
60
|
+
|
|
61
|
+
return useMemo(() => {
|
|
62
|
+
if (typeof onLongPress !== 'function') {
|
|
63
|
+
return {} as UseLongPressReturnValue;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const moveEnabled = cancelOnMove !== false;
|
|
67
|
+
const moveThreshold =
|
|
68
|
+
cancelOnMove === true ? DEFAULT_MOVE_THRESHOLD : cancelOnMove === false ? 0 : cancelOnMove;
|
|
69
|
+
|
|
70
|
+
const start = (event: React.MouseEvent | React.TouchEvent) => {
|
|
71
|
+
if (!isMouseEvent(event) && !isTouchEvent(event)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (onStart) {
|
|
76
|
+
onStart(event);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
startPosition.current = getEventPosition(event);
|
|
80
|
+
isPressed.current = true;
|
|
81
|
+
timeout.current = window.setTimeout(() => {
|
|
82
|
+
onLongPress(event);
|
|
83
|
+
isLongPressActive.current = true;
|
|
84
|
+
}, threshold);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const cancel = (event: React.MouseEvent | React.TouchEvent) => {
|
|
88
|
+
if (!isMouseEvent(event) && !isTouchEvent(event)) {
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (isLongPressActive.current) {
|
|
93
|
+
if (onFinish) {
|
|
94
|
+
onFinish(event);
|
|
95
|
+
}
|
|
96
|
+
} else if (isPressed.current) {
|
|
97
|
+
if (onCancel) {
|
|
98
|
+
onCancel(event);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
isLongPressActive.current = false;
|
|
103
|
+
isPressed.current = false;
|
|
104
|
+
startPosition.current = null;
|
|
105
|
+
|
|
106
|
+
if (timeout.current !== -1) {
|
|
107
|
+
window.clearTimeout(timeout.current);
|
|
108
|
+
timeout.current = -1;
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const move = (event: React.MouseEvent | React.TouchEvent) => {
|
|
113
|
+
if (!moveEnabled || !isPressed.current || isLongPressActive.current) {
|
|
114
|
+
return;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const position = getEventPosition(event);
|
|
118
|
+
if (!position || !startPosition.current) {
|
|
119
|
+
return;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const dx = position.x - startPosition.current.x;
|
|
123
|
+
const dy = position.y - startPosition.current.y;
|
|
124
|
+
|
|
125
|
+
if (Math.sqrt(dx * dx + dy * dy) > moveThreshold) {
|
|
126
|
+
cancel(event);
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const handlers: UseLongPressReturnValue = {};
|
|
131
|
+
|
|
132
|
+
if (events.includes('mouse')) {
|
|
133
|
+
handlers.onMouseDown = start;
|
|
134
|
+
handlers.onMouseUp = cancel;
|
|
135
|
+
handlers.onMouseLeave = cancel;
|
|
136
|
+
if (moveEnabled) {
|
|
137
|
+
handlers.onMouseMove = move;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (events.includes('touch')) {
|
|
142
|
+
handlers.onTouchStart = start;
|
|
143
|
+
handlers.onTouchEnd = cancel;
|
|
144
|
+
handlers.onTouchCancel = cancel;
|
|
145
|
+
if (moveEnabled) {
|
|
146
|
+
handlers.onTouchMove = move;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return handlers;
|
|
151
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
152
|
+
}, [onLongPress, threshold, onCancel, onFinish, onStart, cancelOnMove, eventsKey]);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function getEventPosition(
|
|
156
|
+
event: React.MouseEvent | React.TouchEvent,
|
|
157
|
+
): { x: number; y: number } | null {
|
|
158
|
+
const nativeEvent = getNativeEvent(event);
|
|
159
|
+
if (isTouchEvent(event)) {
|
|
160
|
+
const touch = nativeEvent.touches?.[0] ?? nativeEvent.changedTouches?.[0];
|
|
161
|
+
return touch ? { x: touch.clientX, y: touch.clientY } : null;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return { x: nativeEvent.clientX, y: nativeEvent.clientY };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function isTouchEvent(event: React.MouseEvent | React.TouchEvent): event is React.TouchEvent {
|
|
168
|
+
const nativeEvent = getNativeEvent(event);
|
|
169
|
+
return 'touches' in nativeEvent || 'changedTouches' in nativeEvent;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function isMouseEvent(event: React.MouseEvent | React.TouchEvent): event is React.MouseEvent {
|
|
173
|
+
const nativeEvent = getNativeEvent(event);
|
|
174
|
+
return (
|
|
175
|
+
typeof nativeEvent.clientX === 'number' &&
|
|
176
|
+
typeof nativeEvent.clientY === 'number' &&
|
|
177
|
+
!('touches' in nativeEvent)
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function getNativeEvent(event: React.MouseEvent | React.TouchEvent): any {
|
|
182
|
+
return (event as any).nativeEvent ?? event;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export namespace useLongPress {
|
|
186
|
+
export type Options = UseLongPressOptions;
|
|
187
|
+
export type ReturnValue = UseLongPressReturnValue;
|
|
188
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { useRef, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export function useMap<T, V>(initialState?: [T, V][]): Map<T, V> {
|
|
4
|
+
const normalizedInitialState = typeof initialState === 'symbol' ? undefined : initialState;
|
|
5
|
+
const [map, setMap] = useState(() => new Map<T, V>(normalizedInitialState));
|
|
6
|
+
const mapRef = useRef(map);
|
|
7
|
+
mapRef.current = map;
|
|
8
|
+
|
|
9
|
+
map.set = (...args) => {
|
|
10
|
+
Map.prototype.set.apply(mapRef.current, args);
|
|
11
|
+
setMap(new Map(mapRef.current));
|
|
12
|
+
return mapRef.current;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
map.clear = (...args) => {
|
|
16
|
+
Map.prototype.clear.apply(mapRef.current, args);
|
|
17
|
+
setMap(new Map(mapRef.current));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
map.delete = (...args) => {
|
|
21
|
+
const res = Map.prototype.delete.apply(mapRef.current, args);
|
|
22
|
+
setMap(new Map(mapRef.current));
|
|
23
|
+
|
|
24
|
+
return res;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return map;
|
|
28
|
+
}
|