@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,164 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { CSSProperties } from 'react';
|
|
3
|
+
import { flushSync, useEffectEvent, useRef, useState } from 'octane';
|
|
4
|
+
import type { Octane } from 'octane/jsx-runtime';
|
|
5
|
+
import { useDidUpdate } from '../use-did-update/use-did-update';
|
|
6
|
+
import { mergeRefs } from '../use-merged-ref/use-merged-ref';
|
|
7
|
+
|
|
8
|
+
function getAutoWidthDuration(width: number | string) {
|
|
9
|
+
if (!width || typeof width === 'string') {
|
|
10
|
+
return 0;
|
|
11
|
+
}
|
|
12
|
+
const constant = width / 36;
|
|
13
|
+
return Math.round((4 + 15 * constant ** 0.25 + constant / 5) * 10);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function getElementWidth(elementRef: React.RefObject<HTMLElement | null>) {
|
|
17
|
+
return elementRef.current ? elementRef.current.scrollWidth : 'auto';
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface UseHorizontalCollapseInput {
|
|
21
|
+
/** Expanded state */
|
|
22
|
+
expanded: boolean;
|
|
23
|
+
|
|
24
|
+
/** Transition duration in milliseconds, by default calculated based on content width */
|
|
25
|
+
transitionDuration?: number;
|
|
26
|
+
|
|
27
|
+
/** Transition timing function, `ease` by default */
|
|
28
|
+
transitionTimingFunction?: string;
|
|
29
|
+
|
|
30
|
+
/** Called when transition ends */
|
|
31
|
+
onTransitionEnd?: () => void;
|
|
32
|
+
|
|
33
|
+
/** Called when transition starts */
|
|
34
|
+
onTransitionStart?: () => void;
|
|
35
|
+
|
|
36
|
+
/** If true, collapsed content is kept in the DOM and hidden with `display: none` styles */
|
|
37
|
+
keepMounted?: boolean;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface GetHorizontalCollapsePropsInput {
|
|
41
|
+
style?: CSSProperties;
|
|
42
|
+
ref?: Octane.Ref<HTMLDivElement>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface GetHorizontalCollapsePropsReturnValue {
|
|
46
|
+
'aria-hidden': boolean;
|
|
47
|
+
inert: boolean;
|
|
48
|
+
ref: React.RefCallback<HTMLDivElement>;
|
|
49
|
+
onTransitionEnd: (event: TransitionEvent & { currentTarget: Element & EventTarget }) => void;
|
|
50
|
+
style: React.CSSProperties;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export type UseHorizontalCollapseState = 'entering' | 'entered' | 'exiting' | 'exited';
|
|
54
|
+
|
|
55
|
+
export interface UseHorizontalCollapseReturnValue {
|
|
56
|
+
/** Current transition state */
|
|
57
|
+
state: UseHorizontalCollapseState;
|
|
58
|
+
|
|
59
|
+
/** Props to pass down to the collapsible element */
|
|
60
|
+
getCollapseProps: (
|
|
61
|
+
input?: GetHorizontalCollapsePropsInput,
|
|
62
|
+
) => GetHorizontalCollapsePropsReturnValue;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function useHorizontalCollapse({
|
|
66
|
+
transitionDuration,
|
|
67
|
+
transitionTimingFunction = 'ease',
|
|
68
|
+
onTransitionEnd,
|
|
69
|
+
onTransitionStart,
|
|
70
|
+
expanded,
|
|
71
|
+
keepMounted,
|
|
72
|
+
}: UseHorizontalCollapseInput): UseHorizontalCollapseReturnValue {
|
|
73
|
+
const collapsedStyles = {
|
|
74
|
+
width: 0,
|
|
75
|
+
overflow: 'hidden',
|
|
76
|
+
...(keepMounted ? {} : { display: 'none' }),
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const onTransitionStartEvent = useEffectEvent(() => onTransitionStart?.());
|
|
80
|
+
|
|
81
|
+
const elementRef = useRef<HTMLElement | null>(null);
|
|
82
|
+
const [styles, setStylesRaw] = useState<CSSProperties>(expanded ? {} : collapsedStyles);
|
|
83
|
+
const [state, setState] = useState<UseHorizontalCollapseState>(expanded ? 'entered' : 'exited');
|
|
84
|
+
const setStyles = (newStyles: React.SetStateAction<CSSProperties>) => {
|
|
85
|
+
flushSync(() => setStylesRaw(newStyles));
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const mergeStyles = (newStyles: CSSProperties) => {
|
|
89
|
+
setStyles((oldStyles) => ({ ...oldStyles, ...newStyles }));
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const getTransitionStyles = (width: number | string) => {
|
|
93
|
+
const duration = transitionDuration ?? getAutoWidthDuration(width);
|
|
94
|
+
return {
|
|
95
|
+
transition: `width ${duration}ms ${transitionTimingFunction}, opacity ${duration}ms ${transitionTimingFunction}`,
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
useDidUpdate(() => {
|
|
100
|
+
const shouldTransition = transitionDuration !== 0;
|
|
101
|
+
|
|
102
|
+
if (shouldTransition) {
|
|
103
|
+
onTransitionStartEvent();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
if (expanded) {
|
|
107
|
+
window.requestAnimationFrame(() => {
|
|
108
|
+
flushSync(() => setState('entering'));
|
|
109
|
+
mergeStyles({ willChange: 'width', display: 'block', overflow: 'hidden' });
|
|
110
|
+
window.requestAnimationFrame(() => {
|
|
111
|
+
const width = getElementWidth(elementRef);
|
|
112
|
+
mergeStyles({ ...getTransitionStyles(width), width });
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
} else {
|
|
116
|
+
window.requestAnimationFrame(() => {
|
|
117
|
+
flushSync(() => setState('exiting'));
|
|
118
|
+
const width = getElementWidth(elementRef);
|
|
119
|
+
mergeStyles({ ...getTransitionStyles(width), willChange: 'width', width });
|
|
120
|
+
window.requestAnimationFrame(() => mergeStyles({ width: 0, overflow: 'hidden' }));
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}, [expanded]);
|
|
124
|
+
|
|
125
|
+
const handleTransitionEnd = (event: TransitionEvent): void => {
|
|
126
|
+
if (event.target !== elementRef.current || event.propertyName !== 'width') {
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
if (expanded) {
|
|
131
|
+
const width = getElementWidth(elementRef);
|
|
132
|
+
|
|
133
|
+
if (width === styles.width) {
|
|
134
|
+
setStyles({});
|
|
135
|
+
} else {
|
|
136
|
+
mergeStyles({ width });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
setState('entered');
|
|
140
|
+
onTransitionEnd?.();
|
|
141
|
+
} else if (styles.width === 0) {
|
|
142
|
+
setStyles(collapsedStyles);
|
|
143
|
+
setState('exited');
|
|
144
|
+
onTransitionEnd?.();
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return {
|
|
149
|
+
state,
|
|
150
|
+
getCollapseProps: (input) => ({
|
|
151
|
+
'aria-hidden': !expanded,
|
|
152
|
+
inert: !expanded,
|
|
153
|
+
ref: mergeRefs(elementRef, input?.ref),
|
|
154
|
+
onTransitionEnd: handleTransitionEnd,
|
|
155
|
+
style: { boxSizing: 'border-box', ...input?.style, ...styles },
|
|
156
|
+
}),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export namespace useHorizontalCollapse {
|
|
161
|
+
export type Input = UseHorizontalCollapseInput;
|
|
162
|
+
export type ReturnValue = UseHorizontalCollapseReturnValue;
|
|
163
|
+
export type State = UseHorizontalCollapseState;
|
|
164
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useMediaQuery } from '../use-media-query/use-media-query';
|
|
2
|
+
import type { UseMediaQueryOptions } from '../use-media-query/use-media-query';
|
|
3
|
+
|
|
4
|
+
export type UseColorSchemeValue = 'dark' | 'light';
|
|
5
|
+
|
|
6
|
+
export function useColorScheme(
|
|
7
|
+
initialValue?: UseColorSchemeValue,
|
|
8
|
+
options?: UseMediaQueryOptions,
|
|
9
|
+
): UseColorSchemeValue {
|
|
10
|
+
return useMediaQuery('(prefers-color-scheme: dark)', initialValue === 'dark', options)
|
|
11
|
+
? 'dark'
|
|
12
|
+
: 'light';
|
|
13
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useCallback, useState } from 'octane';
|
|
2
|
+
import { clamp } from '../utils';
|
|
3
|
+
|
|
4
|
+
const DEFAULT_OPTIONS = {
|
|
5
|
+
min: -Infinity,
|
|
6
|
+
max: Infinity,
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export interface UseCounterOptions {
|
|
10
|
+
min?: number;
|
|
11
|
+
max?: number;
|
|
12
|
+
step?: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UseCounterHandlers {
|
|
16
|
+
increment: () => void;
|
|
17
|
+
decrement: () => void;
|
|
18
|
+
set: (value: number) => void;
|
|
19
|
+
reset: () => void;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type UseCounterReturnValue = [number, UseCounterHandlers];
|
|
23
|
+
|
|
24
|
+
function withoutSlot<T>(value: T | symbol): T | undefined {
|
|
25
|
+
return typeof value === 'symbol' ? undefined : value;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function useCounter(initialValue = 0, options?: UseCounterOptions): UseCounterReturnValue {
|
|
29
|
+
const normalizedInitialValue = withoutSlot(initialValue) ?? 0;
|
|
30
|
+
const normalizedOptions = withoutSlot(options);
|
|
31
|
+
const { min, max, step: _step = 1 } = { ...DEFAULT_OPTIONS, ...normalizedOptions };
|
|
32
|
+
const step = Math.abs(_step);
|
|
33
|
+
const [count, setCount] = useState<number>(clamp(normalizedInitialValue, min, max));
|
|
34
|
+
|
|
35
|
+
const increment = useCallback(
|
|
36
|
+
() => setCount((current) => clamp(current + step, min, max)),
|
|
37
|
+
[min, max, step],
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
const decrement = useCallback(
|
|
41
|
+
() => setCount((current) => clamp(current - step, min, max)),
|
|
42
|
+
[min, max, step],
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const set = useCallback((value: number) => setCount(clamp(value, min, max)), [min, max]);
|
|
46
|
+
|
|
47
|
+
const reset = useCallback(
|
|
48
|
+
() => setCount(clamp(normalizedInitialValue, min, max)),
|
|
49
|
+
[normalizedInitialValue, min, max],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
return [count, { increment, decrement, set, reset }];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export namespace useCounter {
|
|
56
|
+
export type Options = UseCounterOptions;
|
|
57
|
+
export type Handlers = UseCounterHandlers;
|
|
58
|
+
export type ReturnValue = UseCounterReturnValue;
|
|
59
|
+
}
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'octane';
|
|
2
|
+
import { useCallbackRef } from '../utils';
|
|
3
|
+
|
|
4
|
+
export interface UseDebouncedCallbackOptions {
|
|
5
|
+
delay: number;
|
|
6
|
+
flushOnUnmount?: boolean;
|
|
7
|
+
leading?: boolean;
|
|
8
|
+
maxWait?: number;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type UseDebouncedCallbackReturnValue<T extends (...args: any[]) => any> = ((
|
|
12
|
+
...args: Parameters<T>
|
|
13
|
+
) => void) & { flush: () => void; cancel: () => void; isPending: () => boolean };
|
|
14
|
+
|
|
15
|
+
export function useDebouncedCallback<T extends (...args: any[]) => any>(
|
|
16
|
+
callback: T,
|
|
17
|
+
options: number | UseDebouncedCallbackOptions,
|
|
18
|
+
) {
|
|
19
|
+
const { delay, flushOnUnmount, leading, maxWait } =
|
|
20
|
+
typeof options === 'number'
|
|
21
|
+
? {
|
|
22
|
+
delay: options,
|
|
23
|
+
flushOnUnmount: false,
|
|
24
|
+
leading: false,
|
|
25
|
+
maxWait: undefined as number | undefined,
|
|
26
|
+
}
|
|
27
|
+
: options;
|
|
28
|
+
|
|
29
|
+
const handleCallback = useCallbackRef(callback);
|
|
30
|
+
const debounceTimerRef = useRef(0);
|
|
31
|
+
const maxWaitTimerRef = useRef(0);
|
|
32
|
+
const latestArgsRef = useRef<Parameters<T> | null>(null);
|
|
33
|
+
|
|
34
|
+
const lastCallback = useMemo(() => {
|
|
35
|
+
const currentCallback = Object.assign(
|
|
36
|
+
(...args: Parameters<T>) => {
|
|
37
|
+
window.clearTimeout(debounceTimerRef.current);
|
|
38
|
+
latestArgsRef.current = args;
|
|
39
|
+
|
|
40
|
+
const isFirstCall = currentCallback._isFirstCall;
|
|
41
|
+
currentCallback._isFirstCall = false;
|
|
42
|
+
|
|
43
|
+
function clearTimeoutAndLeadingRef() {
|
|
44
|
+
window.clearTimeout(debounceTimerRef.current);
|
|
45
|
+
window.clearTimeout(maxWaitTimerRef.current);
|
|
46
|
+
debounceTimerRef.current = 0;
|
|
47
|
+
maxWaitTimerRef.current = 0;
|
|
48
|
+
currentCallback._isFirstCall = true;
|
|
49
|
+
currentCallback._hasPendingCallback = false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function startMaxWaitTimer() {
|
|
53
|
+
if (maxWait !== undefined && maxWaitTimerRef.current === 0) {
|
|
54
|
+
maxWaitTimerRef.current = window.setTimeout(() => {
|
|
55
|
+
if (debounceTimerRef.current !== 0) {
|
|
56
|
+
const latestArgs = latestArgsRef.current!;
|
|
57
|
+
clearTimeoutAndLeadingRef();
|
|
58
|
+
handleCallback(...latestArgs);
|
|
59
|
+
}
|
|
60
|
+
}, maxWait);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (leading && isFirstCall) {
|
|
65
|
+
handleCallback(...args);
|
|
66
|
+
|
|
67
|
+
const resetLeadingState = () => {
|
|
68
|
+
clearTimeoutAndLeadingRef();
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const flush = () => {
|
|
72
|
+
if (debounceTimerRef.current !== 0) {
|
|
73
|
+
clearTimeoutAndLeadingRef();
|
|
74
|
+
handleCallback(...args);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const cancel = () => {
|
|
79
|
+
clearTimeoutAndLeadingRef();
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
currentCallback.flush = flush;
|
|
83
|
+
currentCallback.cancel = cancel;
|
|
84
|
+
debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);
|
|
85
|
+
startMaxWaitTimer();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (leading && !isFirstCall) {
|
|
90
|
+
currentCallback._hasPendingCallback = true;
|
|
91
|
+
const flush = () => {
|
|
92
|
+
if (debounceTimerRef.current !== 0) {
|
|
93
|
+
clearTimeoutAndLeadingRef();
|
|
94
|
+
handleCallback(...args);
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const cancel = () => {
|
|
99
|
+
clearTimeoutAndLeadingRef();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
currentCallback.flush = flush;
|
|
103
|
+
currentCallback.cancel = cancel;
|
|
104
|
+
|
|
105
|
+
const resetLeadingState = () => {
|
|
106
|
+
clearTimeoutAndLeadingRef();
|
|
107
|
+
};
|
|
108
|
+
debounceTimerRef.current = window.setTimeout(resetLeadingState, delay);
|
|
109
|
+
startMaxWaitTimer();
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
currentCallback._hasPendingCallback = true;
|
|
114
|
+
|
|
115
|
+
const flush = () => {
|
|
116
|
+
if (debounceTimerRef.current !== 0) {
|
|
117
|
+
clearTimeoutAndLeadingRef();
|
|
118
|
+
handleCallback(...args);
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
const cancel = () => {
|
|
123
|
+
clearTimeoutAndLeadingRef();
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
currentCallback.flush = flush;
|
|
127
|
+
currentCallback.cancel = cancel;
|
|
128
|
+
debounceTimerRef.current = window.setTimeout(flush, delay);
|
|
129
|
+
startMaxWaitTimer();
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
flush: () => {},
|
|
133
|
+
cancel: () => {},
|
|
134
|
+
isPending: () => currentCallback._hasPendingCallback,
|
|
135
|
+
_isFirstCall: true,
|
|
136
|
+
_hasPendingCallback: false,
|
|
137
|
+
},
|
|
138
|
+
);
|
|
139
|
+
return currentCallback;
|
|
140
|
+
}, [handleCallback, delay, leading, maxWait]);
|
|
141
|
+
|
|
142
|
+
useEffect(
|
|
143
|
+
() => () => {
|
|
144
|
+
if (flushOnUnmount) {
|
|
145
|
+
lastCallback.flush();
|
|
146
|
+
} else {
|
|
147
|
+
lastCallback.cancel();
|
|
148
|
+
}
|
|
149
|
+
},
|
|
150
|
+
[lastCallback, flushOnUnmount],
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
return lastCallback;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export namespace useDebouncedCallback {
|
|
157
|
+
export type Options = UseDebouncedCallbackOptions;
|
|
158
|
+
export type ReturnValue<T extends (...args: any[]) => any> = UseDebouncedCallbackReturnValue<T>;
|
|
159
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { SetStateAction } from 'react';
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from 'octane';
|
|
3
|
+
|
|
4
|
+
export interface UseDebouncedStateOptions {
|
|
5
|
+
leading?: boolean;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type UseDebouncedStateReturnValue<T> = [T, (newValue: SetStateAction<T>) => void];
|
|
9
|
+
|
|
10
|
+
export function useDebouncedState<T = any>(
|
|
11
|
+
defaultValue: T,
|
|
12
|
+
wait: number,
|
|
13
|
+
options: UseDebouncedStateOptions = { leading: false },
|
|
14
|
+
): UseDebouncedStateReturnValue<T> {
|
|
15
|
+
const [value, setValue] = useState(defaultValue);
|
|
16
|
+
const timeoutRef = useRef<number | null>(null);
|
|
17
|
+
const leadingRef = useRef(true);
|
|
18
|
+
|
|
19
|
+
const clearTimeout = () => window.clearTimeout(timeoutRef.current!);
|
|
20
|
+
useEffect(() => clearTimeout, []);
|
|
21
|
+
|
|
22
|
+
const debouncedSetValue = useCallback(
|
|
23
|
+
(newValue: SetStateAction<T>) => {
|
|
24
|
+
clearTimeout();
|
|
25
|
+
if (leadingRef.current && options.leading) {
|
|
26
|
+
setValue(newValue);
|
|
27
|
+
} else {
|
|
28
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
29
|
+
leadingRef.current = true;
|
|
30
|
+
setValue(newValue);
|
|
31
|
+
}, wait);
|
|
32
|
+
}
|
|
33
|
+
leadingRef.current = false;
|
|
34
|
+
},
|
|
35
|
+
[options.leading, wait],
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return [value, debouncedSetValue] as const;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export namespace useDebouncedState {
|
|
42
|
+
export type Options = UseDebouncedStateOptions;
|
|
43
|
+
export type ReturnValue<T> = UseDebouncedStateReturnValue<T>;
|
|
44
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export interface UseDebouncedValueOptions {
|
|
4
|
+
leading?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface UseDebouncedValueHandlers {
|
|
8
|
+
cancel: () => void;
|
|
9
|
+
flush: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type UseDebouncedValueReturnValue<T> = [T, () => void, UseDebouncedValueHandlers];
|
|
13
|
+
|
|
14
|
+
export function useDebouncedValue<T = any>(
|
|
15
|
+
value: T,
|
|
16
|
+
wait: number,
|
|
17
|
+
options: UseDebouncedValueOptions = { leading: false },
|
|
18
|
+
): UseDebouncedValueReturnValue<T> {
|
|
19
|
+
const [_value, setValue] = useState(value);
|
|
20
|
+
const mountedRef = useRef(false);
|
|
21
|
+
const timeoutRef = useRef<number | null>(null);
|
|
22
|
+
const cooldownRef = useRef(false);
|
|
23
|
+
|
|
24
|
+
const latestValueRef = useRef(value);
|
|
25
|
+
latestValueRef.current = value;
|
|
26
|
+
|
|
27
|
+
const cancel = useCallback(() => {
|
|
28
|
+
window.clearTimeout(timeoutRef.current!);
|
|
29
|
+
timeoutRef.current = null;
|
|
30
|
+
cooldownRef.current = false;
|
|
31
|
+
}, []);
|
|
32
|
+
|
|
33
|
+
const flush = useCallback(() => {
|
|
34
|
+
if (timeoutRef.current) {
|
|
35
|
+
cancel();
|
|
36
|
+
cooldownRef.current = false;
|
|
37
|
+
setValue(latestValueRef.current);
|
|
38
|
+
}
|
|
39
|
+
}, []);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (mountedRef.current) {
|
|
43
|
+
if (!cooldownRef.current && options.leading) {
|
|
44
|
+
cooldownRef.current = true;
|
|
45
|
+
setValue(value);
|
|
46
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
47
|
+
cooldownRef.current = false;
|
|
48
|
+
}, wait);
|
|
49
|
+
} else {
|
|
50
|
+
cancel();
|
|
51
|
+
timeoutRef.current = window.setTimeout(() => {
|
|
52
|
+
cooldownRef.current = false;
|
|
53
|
+
setValue(value);
|
|
54
|
+
}, wait);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}, [value, options.leading, wait]);
|
|
58
|
+
|
|
59
|
+
useEffect(() => {
|
|
60
|
+
mountedRef.current = true;
|
|
61
|
+
return cancel;
|
|
62
|
+
}, []);
|
|
63
|
+
|
|
64
|
+
return [_value, cancel, { cancel, flush }];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export namespace useDebouncedValue {
|
|
68
|
+
export type Handlers = UseDebouncedValueHandlers;
|
|
69
|
+
export type Options = UseDebouncedValueOptions;
|
|
70
|
+
export type ReturnValue<T> = UseDebouncedValueReturnValue<T>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { DependencyList } from 'react';
|
|
2
|
+
import { useEffect, useRef } from 'octane';
|
|
3
|
+
|
|
4
|
+
export function useDidUpdate(fn: () => void | (() => void), dependencies?: DependencyList) {
|
|
5
|
+
const normalizedDependencies = typeof dependencies === 'symbol' ? undefined : dependencies;
|
|
6
|
+
const mounted = useRef(false);
|
|
7
|
+
|
|
8
|
+
useEffect(
|
|
9
|
+
() => () => {
|
|
10
|
+
mounted.current = false;
|
|
11
|
+
},
|
|
12
|
+
[],
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
useEffect(
|
|
16
|
+
() => {
|
|
17
|
+
if (mounted.current) {
|
|
18
|
+
return fn();
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
mounted.current = true;
|
|
22
|
+
return undefined;
|
|
23
|
+
},
|
|
24
|
+
normalizedDependencies ? [...normalizedDependencies] : normalizedDependencies,
|
|
25
|
+
);
|
|
26
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { useCallback, useState } from 'octane';
|
|
2
|
+
import { withoutSlot } from '../internal';
|
|
3
|
+
|
|
4
|
+
export interface UseDisclosureOptions {
|
|
5
|
+
onOpen?: () => void;
|
|
6
|
+
onClose?: () => void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface UseDisclosureHandlers {
|
|
10
|
+
set: (value: boolean) => void;
|
|
11
|
+
open: () => void;
|
|
12
|
+
close: () => void;
|
|
13
|
+
toggle: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export type UseDisclosureReturnValue = [boolean, UseDisclosureHandlers];
|
|
17
|
+
|
|
18
|
+
export function useDisclosure(
|
|
19
|
+
initialState = false,
|
|
20
|
+
options: UseDisclosureOptions = {},
|
|
21
|
+
): UseDisclosureReturnValue {
|
|
22
|
+
const normalizedInitialState = withoutSlot(initialState) ?? false;
|
|
23
|
+
const normalizedOptions = withoutSlot(options) ?? {};
|
|
24
|
+
const [opened, setOpened] = useState(normalizedInitialState);
|
|
25
|
+
|
|
26
|
+
const open = useCallback(() => {
|
|
27
|
+
setOpened((isOpened) => {
|
|
28
|
+
if (!isOpened) {
|
|
29
|
+
normalizedOptions.onOpen?.();
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
return isOpened;
|
|
33
|
+
});
|
|
34
|
+
}, [normalizedOptions.onOpen]);
|
|
35
|
+
|
|
36
|
+
const close = useCallback(() => {
|
|
37
|
+
setOpened((isOpened) => {
|
|
38
|
+
if (isOpened) {
|
|
39
|
+
normalizedOptions.onClose?.();
|
|
40
|
+
return false;
|
|
41
|
+
}
|
|
42
|
+
return isOpened;
|
|
43
|
+
});
|
|
44
|
+
}, [normalizedOptions.onClose]);
|
|
45
|
+
|
|
46
|
+
const toggle = useCallback(() => {
|
|
47
|
+
opened ? close() : open();
|
|
48
|
+
}, [close, open, opened]);
|
|
49
|
+
|
|
50
|
+
return [opened, { open, close, toggle, set: setOpened }];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export namespace useDisclosure {
|
|
54
|
+
export type Options = UseDisclosureOptions;
|
|
55
|
+
export type Handlers = UseDisclosureHandlers;
|
|
56
|
+
export type ReturnValue = UseDisclosureReturnValue;
|
|
57
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';
|
|
2
|
+
|
|
3
|
+
export function useDocumentTitle(title: string) {
|
|
4
|
+
useIsomorphicEffect(() => {
|
|
5
|
+
if (typeof title === 'string' && title.trim().length > 0) {
|
|
6
|
+
document.title = title.trim();
|
|
7
|
+
}
|
|
8
|
+
}, [title]);
|
|
9
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { useEffect, useState } from 'octane';
|
|
2
|
+
|
|
3
|
+
export function useDocumentVisibility(): DocumentVisibilityState {
|
|
4
|
+
const [documentVisibility, setDocumentVisibility] = useState<DocumentVisibilityState>('visible');
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
setDocumentVisibility(document.visibilityState);
|
|
8
|
+
const listener = () => setDocumentVisibility(document.visibilityState);
|
|
9
|
+
document.addEventListener('visibilitychange', listener);
|
|
10
|
+
return () => document.removeEventListener('visibilitychange', listener);
|
|
11
|
+
}, []);
|
|
12
|
+
|
|
13
|
+
return documentVisibility;
|
|
14
|
+
}
|