@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,70 @@
|
|
|
1
|
+
import { useCallback, useMemo, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export interface UseStateHistoryHandlers<T> {
|
|
4
|
+
set: (value: T) => void;
|
|
5
|
+
back: (steps?: number) => void;
|
|
6
|
+
forward: (steps?: number) => void;
|
|
7
|
+
reset: () => void;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface UseStateHistoryValue<T> {
|
|
11
|
+
history: T[];
|
|
12
|
+
current: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type UseStateHistoryReturnValue<T> = [
|
|
16
|
+
T,
|
|
17
|
+
UseStateHistoryHandlers<T>,
|
|
18
|
+
UseStateHistoryValue<T>,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export function useStateHistory<T>(initialValue: T): UseStateHistoryReturnValue<T> {
|
|
22
|
+
const [state, setState] = useState<UseStateHistoryValue<T>>({
|
|
23
|
+
history: [initialValue],
|
|
24
|
+
current: 0,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const set = useCallback(
|
|
28
|
+
(val: T) =>
|
|
29
|
+
setState((currentState) => {
|
|
30
|
+
const nextState = [...currentState.history.slice(0, currentState.current + 1), val];
|
|
31
|
+
return {
|
|
32
|
+
history: nextState,
|
|
33
|
+
current: nextState.length - 1,
|
|
34
|
+
};
|
|
35
|
+
}),
|
|
36
|
+
[],
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
const back = useCallback(
|
|
40
|
+
(steps = 1) =>
|
|
41
|
+
setState((currentState) => ({
|
|
42
|
+
history: currentState.history,
|
|
43
|
+
current: Math.max(0, currentState.current - steps),
|
|
44
|
+
})),
|
|
45
|
+
[],
|
|
46
|
+
);
|
|
47
|
+
|
|
48
|
+
const forward = useCallback(
|
|
49
|
+
(steps = 1) =>
|
|
50
|
+
setState((currentState) => ({
|
|
51
|
+
history: currentState.history,
|
|
52
|
+
current: Math.min(currentState.history.length - 1, currentState.current + steps),
|
|
53
|
+
})),
|
|
54
|
+
[],
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const reset = useCallback(() => {
|
|
58
|
+
setState({ history: [initialValue], current: 0 });
|
|
59
|
+
}, [initialValue]);
|
|
60
|
+
|
|
61
|
+
const handlers = useMemo(() => ({ back, forward, reset, set }), [back, forward, reset, set]);
|
|
62
|
+
|
|
63
|
+
return [state.history[state.current], handlers, state];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export namespace useStateHistory {
|
|
67
|
+
export type Handlers<T> = UseStateHistoryHandlers<T>;
|
|
68
|
+
export type Value<T> = UseStateHistoryValue<T>;
|
|
69
|
+
export type ReturnValue<T> = UseStateHistoryReturnValue<T>;
|
|
70
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect, useEffectEvent, useState } from 'octane';
|
|
2
|
+
import { useForceUpdate } from '../use-force-update/use-force-update';
|
|
3
|
+
|
|
4
|
+
export function useTextSelection(): Selection | null {
|
|
5
|
+
const forceUpdate = useForceUpdate();
|
|
6
|
+
const [selection, setSelection] = useState<Selection | null>(null);
|
|
7
|
+
|
|
8
|
+
const handleSelectionChange = useEffectEvent(() => {
|
|
9
|
+
setSelection(document.getSelection());
|
|
10
|
+
forceUpdate();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
setSelection(document.getSelection());
|
|
15
|
+
document.addEventListener('selectionchange', handleSelectionChange);
|
|
16
|
+
return () => document.removeEventListener('selectionchange', handleSelectionChange);
|
|
17
|
+
}, []);
|
|
18
|
+
|
|
19
|
+
return selection;
|
|
20
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'octane';
|
|
2
|
+
import { useCallbackRef } from '../utils';
|
|
3
|
+
|
|
4
|
+
export function useThrottledCallbackWithClearTimeout<T extends (...args: any[]) => any>(
|
|
5
|
+
callback: T,
|
|
6
|
+
wait: number,
|
|
7
|
+
) {
|
|
8
|
+
const handleCallback = useCallbackRef(callback);
|
|
9
|
+
const latestInArgsRef = useRef<Parameters<T> | null>(null);
|
|
10
|
+
const latestOutArgsRef = useRef<Parameters<T> | null>(null);
|
|
11
|
+
const active = useRef(true);
|
|
12
|
+
const waitRef = useRef(wait);
|
|
13
|
+
const timeoutRef = useRef<number>(-1);
|
|
14
|
+
|
|
15
|
+
const clearTimeout = () => window.clearTimeout(timeoutRef.current);
|
|
16
|
+
|
|
17
|
+
const callThrottledCallback = useCallback(
|
|
18
|
+
(...args: Parameters<T>) => {
|
|
19
|
+
handleCallback(...args);
|
|
20
|
+
latestInArgsRef.current = args;
|
|
21
|
+
latestOutArgsRef.current = args;
|
|
22
|
+
active.current = false;
|
|
23
|
+
},
|
|
24
|
+
[handleCallback],
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const timerCallback = useCallback(() => {
|
|
28
|
+
if (latestInArgsRef.current && latestInArgsRef.current !== latestOutArgsRef.current) {
|
|
29
|
+
callThrottledCallback(...latestInArgsRef.current);
|
|
30
|
+
|
|
31
|
+
timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);
|
|
32
|
+
} else {
|
|
33
|
+
active.current = true;
|
|
34
|
+
}
|
|
35
|
+
}, [callThrottledCallback]);
|
|
36
|
+
|
|
37
|
+
const throttled = useCallback(
|
|
38
|
+
(...args: Parameters<T>) => {
|
|
39
|
+
if (active.current) {
|
|
40
|
+
callThrottledCallback(...args);
|
|
41
|
+
timeoutRef.current = window.setTimeout(timerCallback, waitRef.current);
|
|
42
|
+
} else {
|
|
43
|
+
latestInArgsRef.current = args;
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
[callThrottledCallback, timerCallback],
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
useEffect(() => {
|
|
50
|
+
waitRef.current = wait;
|
|
51
|
+
}, [wait]);
|
|
52
|
+
|
|
53
|
+
return [throttled, clearTimeout] as const;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function useThrottledCallback<T extends (...args: any[]) => any>(callback: T, wait: number) {
|
|
57
|
+
const [throttled, clearTimeout] = useThrottledCallbackWithClearTimeout(callback, wait);
|
|
58
|
+
useEffect(() => clearTimeout, []);
|
|
59
|
+
return throttled;
|
|
60
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { useEffect, useState } from 'octane';
|
|
2
|
+
import { useThrottledCallbackWithClearTimeout } from '../use-throttled-callback/use-throttled-callback';
|
|
3
|
+
|
|
4
|
+
export function useThrottledState<T = any>(defaultValue: T, wait: number) {
|
|
5
|
+
const [value, setValue] = useState(defaultValue);
|
|
6
|
+
|
|
7
|
+
const [setThrottledValue, clearTimeout] = useThrottledCallbackWithClearTimeout(setValue, wait);
|
|
8
|
+
|
|
9
|
+
useEffect(() => clearTimeout, []);
|
|
10
|
+
|
|
11
|
+
return [value, setThrottledValue] as const;
|
|
12
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'octane';
|
|
2
|
+
import { useThrottledCallbackWithClearTimeout } from '../use-throttled-callback/use-throttled-callback';
|
|
3
|
+
|
|
4
|
+
export function useThrottledValue<T>(value: T, wait: number) {
|
|
5
|
+
const [throttledValue, setThrottledValue] = useState(value);
|
|
6
|
+
const valueRef = useRef(value);
|
|
7
|
+
|
|
8
|
+
const [throttledSetValue, clearTimeout] = useThrottledCallbackWithClearTimeout(
|
|
9
|
+
setThrottledValue,
|
|
10
|
+
wait,
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (value !== valueRef.current) {
|
|
15
|
+
valueRef.current = value;
|
|
16
|
+
throttledSetValue(value);
|
|
17
|
+
}
|
|
18
|
+
}, [throttledSetValue, value]);
|
|
19
|
+
|
|
20
|
+
useEffect(() => clearTimeout, []);
|
|
21
|
+
|
|
22
|
+
return throttledValue;
|
|
23
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef } from 'octane';
|
|
2
|
+
import { withoutSlot } from '../internal';
|
|
3
|
+
import { useCallbackRef } from '../utils';
|
|
4
|
+
|
|
5
|
+
export interface UseTimeoutOptions {
|
|
6
|
+
autoInvoke: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface UseTimeoutReturnValue {
|
|
10
|
+
start: (...args: any[]) => void;
|
|
11
|
+
clear: () => void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function useTimeout(
|
|
15
|
+
callback: (...args: any[]) => void,
|
|
16
|
+
delay: number,
|
|
17
|
+
options: UseTimeoutOptions = { autoInvoke: false },
|
|
18
|
+
): UseTimeoutReturnValue {
|
|
19
|
+
const normalizedOptions = withoutSlot(options) ?? { autoInvoke: false };
|
|
20
|
+
const timeoutRef = useRef<number | null>(null);
|
|
21
|
+
const handleCallback = useCallbackRef(callback);
|
|
22
|
+
|
|
23
|
+
const start = useCallback(
|
|
24
|
+
(...args: any[]) => {
|
|
25
|
+
if (!timeoutRef.current) {
|
|
26
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
27
|
+
handleCallback(...args);
|
|
28
|
+
timeoutRef.current = null;
|
|
29
|
+
}, delay);
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
[delay],
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
const clear = useCallback(() => {
|
|
36
|
+
if (timeoutRef.current) {
|
|
37
|
+
window.clearTimeout(timeoutRef.current);
|
|
38
|
+
timeoutRef.current = null;
|
|
39
|
+
}
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
useEffect(() => {
|
|
43
|
+
if (normalizedOptions.autoInvoke) {
|
|
44
|
+
start();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return clear;
|
|
48
|
+
}, [clear, start]);
|
|
49
|
+
|
|
50
|
+
return { start, clear };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export namespace useTimeout {
|
|
54
|
+
export type Options = UseTimeoutOptions;
|
|
55
|
+
export type ReturnValue = UseTimeoutReturnValue;
|
|
56
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useReducer } from 'octane';
|
|
2
|
+
import { withoutSlot } from '../internal';
|
|
3
|
+
|
|
4
|
+
type UseToggleAction<T> = (value?: React.SetStateAction<T>) => void;
|
|
5
|
+
export type UseToggleReturnValue<T> = [T, UseToggleAction<T>];
|
|
6
|
+
|
|
7
|
+
export function useToggle<T = boolean>(
|
|
8
|
+
options: readonly T[] = [false, true] as any,
|
|
9
|
+
): UseToggleReturnValue<T> {
|
|
10
|
+
const normalizedOptions = withoutSlot(options) ?? ([false, true] as any);
|
|
11
|
+
const [[option], toggle] = useReducer((state: T[], action: React.SetStateAction<T>) => {
|
|
12
|
+
const value = action instanceof Function ? action(state[0]) : action;
|
|
13
|
+
const index = Math.abs(state.indexOf(value));
|
|
14
|
+
|
|
15
|
+
return state.slice(index).concat(state.slice(0, index));
|
|
16
|
+
}, normalizedOptions as T[]);
|
|
17
|
+
|
|
18
|
+
return [option, toggle as UseToggleAction<T>];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export namespace useToggle {
|
|
22
|
+
export type ReturnValue<T> = UseToggleReturnValue<T>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export interface UseUncontrolledOptions<T> {
|
|
4
|
+
/** Value for controlled state */
|
|
5
|
+
value?: T;
|
|
6
|
+
|
|
7
|
+
/** Initial value for uncontrolled state */
|
|
8
|
+
defaultValue?: T;
|
|
9
|
+
|
|
10
|
+
/** Final value for uncontrolled state when value and defaultValue are not provided */
|
|
11
|
+
finalValue?: T;
|
|
12
|
+
|
|
13
|
+
/** Controlled state onChange handler */
|
|
14
|
+
onChange?: (value: T, ...payload: any[]) => void;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type UseUncontrolledReturnValue<T> = [
|
|
18
|
+
/** Current value */
|
|
19
|
+
T,
|
|
20
|
+
|
|
21
|
+
/** Handler to update the state, passes `value` and `payload` to `onChange` */
|
|
22
|
+
(value: T, ...payload: any[]) => void,
|
|
23
|
+
|
|
24
|
+
/** True if the state is controlled, false if uncontrolled */
|
|
25
|
+
boolean,
|
|
26
|
+
];
|
|
27
|
+
|
|
28
|
+
export function useUncontrolled<T>({
|
|
29
|
+
value,
|
|
30
|
+
defaultValue,
|
|
31
|
+
finalValue,
|
|
32
|
+
onChange = () => {},
|
|
33
|
+
}: UseUncontrolledOptions<T>): UseUncontrolledReturnValue<T> {
|
|
34
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(
|
|
35
|
+
defaultValue !== undefined ? defaultValue : finalValue,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
const handleUncontrolledChange = (val: T, ...payload: any[]) => {
|
|
39
|
+
setUncontrolledValue(val);
|
|
40
|
+
onChange?.(val, ...payload);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
if (value !== undefined) {
|
|
44
|
+
return [value as T, onChange, true];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return [uncontrolledValue as T, handleUncontrolledChange, false];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export namespace useUncontrolled {
|
|
51
|
+
export type Options<T> = UseUncontrolledOptions<T>;
|
|
52
|
+
export type ReturnValue<T> = UseUncontrolledReturnValue<T>;
|
|
53
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export interface UseValidatedStateValue<T> {
|
|
4
|
+
/** Current value */
|
|
5
|
+
value: T;
|
|
6
|
+
|
|
7
|
+
/** Last valid value */
|
|
8
|
+
lastValidValue: T | undefined;
|
|
9
|
+
|
|
10
|
+
/** True if the current value is valid, false otherwise */
|
|
11
|
+
valid: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export type UseValidatedStateReturnValue<T> = [
|
|
15
|
+
/** Current value */
|
|
16
|
+
UseValidatedStateValue<T>,
|
|
17
|
+
/** Handler to update the state, passes `value` and `payload` to `onChange` */
|
|
18
|
+
(value: T) => void,
|
|
19
|
+
];
|
|
20
|
+
|
|
21
|
+
export function useValidatedState<T>(
|
|
22
|
+
initialValue: T,
|
|
23
|
+
validate: (value: T) => boolean,
|
|
24
|
+
initialValidationState?: boolean,
|
|
25
|
+
): UseValidatedStateReturnValue<T> {
|
|
26
|
+
const [value, setValue] = useState<T>(initialValue);
|
|
27
|
+
const [lastValidValue, setLastValidValue] = useState<T | undefined>(
|
|
28
|
+
validate(initialValue) ? initialValue : undefined,
|
|
29
|
+
);
|
|
30
|
+
const [valid, setValid] = useState<boolean>(
|
|
31
|
+
typeof initialValidationState === 'boolean' ? initialValidationState : validate(initialValue),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const onChange = (val: T) => {
|
|
35
|
+
if (validate(val)) {
|
|
36
|
+
setLastValidValue(val);
|
|
37
|
+
setValid(true);
|
|
38
|
+
} else {
|
|
39
|
+
setValid(false);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setValue(val);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
return [{ value, lastValidValue, valid }, onChange] as const;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export namespace useValidatedState {
|
|
49
|
+
export type ReturnValue<T> = UseValidatedStateReturnValue<T>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'octane';
|
|
2
|
+
import { useWindowEvent } from '../use-window-event/use-window-event';
|
|
3
|
+
|
|
4
|
+
const eventListenerOptions = {
|
|
5
|
+
passive: true,
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export function useViewportSize() {
|
|
9
|
+
const [windowSize, setWindowSize] = useState({
|
|
10
|
+
width: 0,
|
|
11
|
+
height: 0,
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
const setSize = useCallback(() => {
|
|
15
|
+
setWindowSize({ width: window.innerWidth || 0, height: window.innerHeight || 0 });
|
|
16
|
+
}, []);
|
|
17
|
+
|
|
18
|
+
useWindowEvent('resize', setSize, eventListenerOptions);
|
|
19
|
+
useWindowEvent('orientationchange', setSize, eventListenerOptions);
|
|
20
|
+
useEffect(setSize, []);
|
|
21
|
+
|
|
22
|
+
return windowSize;
|
|
23
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { useEffect, useEffectEvent } from 'octane';
|
|
2
|
+
|
|
3
|
+
export function useWindowEvent<K extends string>(
|
|
4
|
+
type: K,
|
|
5
|
+
listener: K extends keyof WindowEventMap
|
|
6
|
+
? (this: Window, ev: WindowEventMap[K]) => void
|
|
7
|
+
: (this: Window, ev: CustomEvent) => void,
|
|
8
|
+
options?: boolean | AddEventListenerOptions,
|
|
9
|
+
) {
|
|
10
|
+
const stableListener = useEffectEvent(listener);
|
|
11
|
+
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
window.addEventListener(type as any, stableListener, options);
|
|
14
|
+
return () => window.removeEventListener(type as any, stableListener, options);
|
|
15
|
+
}, [type]);
|
|
16
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { useEffect, useState } from 'octane';
|
|
2
|
+
import { useWindowEvent } from '../use-window-event/use-window-event';
|
|
3
|
+
|
|
4
|
+
export interface UseWindowScrollPosition {
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type UseWindowScrollTo = (position: Partial<UseWindowScrollPosition>) => void;
|
|
10
|
+
export type UseWindowScrollReturnValue = [UseWindowScrollPosition, UseWindowScrollTo];
|
|
11
|
+
|
|
12
|
+
function getScrollPosition(): UseWindowScrollPosition {
|
|
13
|
+
return typeof window !== 'undefined' ? { x: window.scrollX, y: window.scrollY } : { x: 0, y: 0 };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function scrollTo({ x, y }: Partial<UseWindowScrollPosition>) {
|
|
17
|
+
if (typeof window !== 'undefined') {
|
|
18
|
+
const scrollOptions: ScrollToOptions = { behavior: 'smooth' };
|
|
19
|
+
|
|
20
|
+
if (typeof x === 'number') {
|
|
21
|
+
scrollOptions.left = x;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (typeof y === 'number') {
|
|
25
|
+
scrollOptions.top = y;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
window.scrollTo(scrollOptions);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function useWindowScroll(): UseWindowScrollReturnValue {
|
|
33
|
+
const [position, setPosition] = useState<UseWindowScrollPosition>({ x: 0, y: 0 });
|
|
34
|
+
|
|
35
|
+
useWindowEvent('scroll', () => setPosition(getScrollPosition()), { passive: true });
|
|
36
|
+
useWindowEvent('resize', () => setPosition(getScrollPosition()), { passive: true });
|
|
37
|
+
|
|
38
|
+
useEffect(() => {
|
|
39
|
+
setPosition(getScrollPosition());
|
|
40
|
+
}, []);
|
|
41
|
+
|
|
42
|
+
return [position, scrollTo] as const;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export namespace useWindowScroll {
|
|
46
|
+
export type Position = UseWindowScrollPosition;
|
|
47
|
+
export type ScrollTo = UseWindowScrollTo;
|
|
48
|
+
export type ReturnValue = UseWindowScrollReturnValue;
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export function clamp(value: number, min: number | undefined, max: number | undefined) {
|
|
2
|
+
if (min === undefined && max === undefined) {
|
|
3
|
+
return value;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (min !== undefined && max === undefined) {
|
|
7
|
+
return Math.max(value, min);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (min === undefined && max !== undefined) {
|
|
11
|
+
return Math.min(value, max);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return Math.min(Math.max(value, min!), max!);
|
|
15
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { clamp } from './clamp/clamp';
|
|
2
|
+
export { lowerFirst } from './lower-first/lower-first';
|
|
3
|
+
export { randomId } from './random-id/random-id';
|
|
4
|
+
export { range } from './range/range';
|
|
5
|
+
export { shallowEqual } from './shallow-equal/shallow-equal';
|
|
6
|
+
export { upperFirst } from './upper-first/upper-first';
|
|
7
|
+
export { useCallbackRef } from './use-callback-ref/use-callback-ref';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function range(start: number, end: number) {
|
|
2
|
+
const length = Math.abs(end - start) + 1;
|
|
3
|
+
const reversed = start > end;
|
|
4
|
+
|
|
5
|
+
if (!reversed) {
|
|
6
|
+
return Array.from({ length }, (_, index) => index + start);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
return Array.from({ length }, (_, index) => start - index);
|
|
10
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function shallowEqual(a: any, b: any) {
|
|
2
|
+
if (a === b) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
if (Number.isNaN(a) && Number.isNaN(b)) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (!(a instanceof Object) || !(b instanceof Object)) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const keys = Object.keys(a);
|
|
15
|
+
const { length } = keys;
|
|
16
|
+
|
|
17
|
+
if (length !== Object.keys(b).length) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < length; i += 1) {
|
|
22
|
+
const key = keys[i];
|
|
23
|
+
|
|
24
|
+
if (!(key in b)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (a[key] !== b[key] && !(Number.isNaN(a[key]) && Number.isNaN(b[key]))) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'octane';
|
|
2
|
+
|
|
3
|
+
export function useCallbackRef<T extends (...args: any[]) => any>(callback: T | undefined): T {
|
|
4
|
+
const callbackRef = useRef(callback);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
callbackRef.current = callback;
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
return useMemo(() => ((...args) => callbackRef.current?.(...args)) as T, []);
|
|
11
|
+
}
|