@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,140 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'octane';
|
|
2
|
+
import { useIsomorphicEffect } from '../use-isomorphic-effect/use-isomorphic-effect';
|
|
3
|
+
|
|
4
|
+
export interface UseFileDialogOptions {
|
|
5
|
+
/** Determines whether multiple files are allowed, `true` by default */
|
|
6
|
+
multiple?: boolean;
|
|
7
|
+
|
|
8
|
+
/** `accept` attribute of the file input, '*' by default */
|
|
9
|
+
accept?: string;
|
|
10
|
+
|
|
11
|
+
/** `capture` attribute of the file input */
|
|
12
|
+
capture?: string;
|
|
13
|
+
|
|
14
|
+
/** Determines whether the user can pick a directory instead of file, `false` by default */
|
|
15
|
+
directory?: boolean;
|
|
16
|
+
|
|
17
|
+
/** Determines whether the file input state should be reset when the file dialog is opened, `false` by default */
|
|
18
|
+
resetOnOpen?: boolean;
|
|
19
|
+
|
|
20
|
+
/** Initial selected files */
|
|
21
|
+
initialFiles?: FileList | File[];
|
|
22
|
+
|
|
23
|
+
/** Called when files are selected */
|
|
24
|
+
onChange?: (files: FileList | null) => void;
|
|
25
|
+
|
|
26
|
+
/** Called when file dialog is canceled */
|
|
27
|
+
onCancel?: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultOptions: UseFileDialogOptions = {
|
|
31
|
+
multiple: true,
|
|
32
|
+
accept: '*',
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
function getInitialFilesList(files: UseFileDialogOptions['initialFiles']): FileList | null {
|
|
36
|
+
if (!files) {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (files instanceof FileList) {
|
|
41
|
+
return files;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const result = new DataTransfer();
|
|
45
|
+
for (const file of files) {
|
|
46
|
+
result.items.add(file);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return result.files;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function createInput(options: UseFileDialogOptions) {
|
|
53
|
+
if (typeof document === 'undefined') {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const input = document.createElement('input');
|
|
58
|
+
input.type = 'file';
|
|
59
|
+
|
|
60
|
+
if (options.accept) {
|
|
61
|
+
input.accept = options.accept;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (options.multiple) {
|
|
65
|
+
input.multiple = options.multiple;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (options.capture) {
|
|
69
|
+
input.capture = options.capture;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (options.directory) {
|
|
73
|
+
input.webkitdirectory = options.directory;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
input.style.display = 'none';
|
|
77
|
+
return input;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface UseFileDialogReturnValue {
|
|
81
|
+
files: FileList | null;
|
|
82
|
+
open: () => void;
|
|
83
|
+
reset: () => void;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function useFileDialog(input: UseFileDialogOptions = {}): UseFileDialogReturnValue {
|
|
87
|
+
const options: UseFileDialogOptions = { ...defaultOptions, ...input };
|
|
88
|
+
const [files, setFiles] = useState<FileList | null>(getInitialFilesList(options.initialFiles));
|
|
89
|
+
const inputRef = useRef<HTMLInputElement | null>(null);
|
|
90
|
+
|
|
91
|
+
const handleChange = useCallback(
|
|
92
|
+
(event: Event) => {
|
|
93
|
+
const target = event.target as HTMLInputElement;
|
|
94
|
+
if (target?.files) {
|
|
95
|
+
setFiles(target.files);
|
|
96
|
+
options.onChange?.(target.files);
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
[options.onChange],
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
const createAndSetupInput = useCallback(() => {
|
|
103
|
+
inputRef.current?.remove();
|
|
104
|
+
inputRef.current = createInput(options);
|
|
105
|
+
|
|
106
|
+
if (inputRef.current) {
|
|
107
|
+
inputRef.current.addEventListener('change', handleChange, { once: true });
|
|
108
|
+
if (options.onCancel) {
|
|
109
|
+
inputRef.current.addEventListener('cancel', options.onCancel, { once: true });
|
|
110
|
+
}
|
|
111
|
+
document.body.appendChild(inputRef.current);
|
|
112
|
+
}
|
|
113
|
+
}, [options, handleChange]);
|
|
114
|
+
|
|
115
|
+
useIsomorphicEffect(() => {
|
|
116
|
+
createAndSetupInput();
|
|
117
|
+
return () => inputRef.current?.remove();
|
|
118
|
+
}, []);
|
|
119
|
+
|
|
120
|
+
const reset = useCallback(() => {
|
|
121
|
+
setFiles(null);
|
|
122
|
+
options.onChange?.(null);
|
|
123
|
+
}, [options.onChange]);
|
|
124
|
+
|
|
125
|
+
const open = useCallback(() => {
|
|
126
|
+
if (options.resetOnOpen) {
|
|
127
|
+
reset();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
createAndSetupInput();
|
|
131
|
+
inputRef.current?.click();
|
|
132
|
+
}, [options.resetOnOpen, reset, createAndSetupInput]);
|
|
133
|
+
|
|
134
|
+
return { files, open, reset };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export namespace useFileDialog {
|
|
138
|
+
export type Options = UseFileDialogOptions;
|
|
139
|
+
export type ReturnValue = UseFileDialogReturnValue;
|
|
140
|
+
}
|
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
// Required to disable for webkit-user-select, although deprecated, it is still required for Safari support
|
|
2
|
+
import type { RefCallback } from 'react';
|
|
3
|
+
import { useCallback, useEffect, useRef, useState } from 'octane';
|
|
4
|
+
|
|
5
|
+
function useRefValue<T>(value: T) {
|
|
6
|
+
const ref = useRef(value);
|
|
7
|
+
ref.current = value;
|
|
8
|
+
return ref;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
interface FloatingWindowPositionConfig {
|
|
12
|
+
top?: number;
|
|
13
|
+
left?: number;
|
|
14
|
+
right?: number;
|
|
15
|
+
bottom?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
interface FloatingWindowPosition {
|
|
19
|
+
/** Element offset from the left side of the viewport */
|
|
20
|
+
x: number;
|
|
21
|
+
|
|
22
|
+
/** Element offset from the top side of the viewport */
|
|
23
|
+
y: number;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface UseFloatingWindowOptions {
|
|
27
|
+
/** If `false`, the element can not be dragged. */
|
|
28
|
+
enabled?: boolean;
|
|
29
|
+
|
|
30
|
+
/** If `true`, the element can only move within the current viewport boundaries. */
|
|
31
|
+
constrainToViewport?: boolean;
|
|
32
|
+
|
|
33
|
+
/** The offset from the viewport edges when constraining the element. Requires `constrainToViewport: true`. */
|
|
34
|
+
constrainOffset?: number;
|
|
35
|
+
|
|
36
|
+
/** Selector of an element that should be used to drag floating window. If not specified, the entire root element is used as a drag target. */
|
|
37
|
+
dragHandleSelector?: string;
|
|
38
|
+
|
|
39
|
+
/** Selector of an element within `dragHandleSelector` that should be excluded from the drag event. */
|
|
40
|
+
excludeDragHandleSelector?: string;
|
|
41
|
+
|
|
42
|
+
/** If set, restricts movement to the specified axis */
|
|
43
|
+
axis?: 'x' | 'y';
|
|
44
|
+
|
|
45
|
+
/** Initial position. If not set, calculated from element styles. */
|
|
46
|
+
initialPosition?: FloatingWindowPositionConfig;
|
|
47
|
+
|
|
48
|
+
/** Called when the element position changes */
|
|
49
|
+
onPositionChange?: (pos: FloatingWindowPosition) => void;
|
|
50
|
+
|
|
51
|
+
/** Called when the drag starts */
|
|
52
|
+
onDragStart?: () => void;
|
|
53
|
+
|
|
54
|
+
/** Called when the drag stops */
|
|
55
|
+
onDragEnd?: () => void;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export type SetFloatingWindowPosition = (position: FloatingWindowPositionConfig) => void;
|
|
59
|
+
|
|
60
|
+
export interface UseFloatingWindowReturnValue<T extends HTMLElement> {
|
|
61
|
+
/** Ref to the element that should be draggable */
|
|
62
|
+
ref: RefCallback<T | null>;
|
|
63
|
+
|
|
64
|
+
/** Function to set the position of the element */
|
|
65
|
+
setPosition: SetFloatingWindowPosition;
|
|
66
|
+
|
|
67
|
+
/** `true` if the element is currently being dragged */
|
|
68
|
+
isDragging: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function useFloatingWindow<T extends HTMLElement>(
|
|
72
|
+
options: UseFloatingWindowOptions = {},
|
|
73
|
+
): UseFloatingWindowReturnValue<T> {
|
|
74
|
+
const [element, setElement] = useState<T | null>(null);
|
|
75
|
+
const ref = useRef<T | null>(null);
|
|
76
|
+
const pos = useRef({ x: 0, y: 0 });
|
|
77
|
+
const offset = useRef({ x: 0, y: 0 });
|
|
78
|
+
const [isDragging, setIsDragging] = useState(false);
|
|
79
|
+
const isDraggingRef = useRef(false);
|
|
80
|
+
const initialized = useRef(false);
|
|
81
|
+
const previousUserSelect = useRef('');
|
|
82
|
+
const previousWebkitUserSelect = useRef('');
|
|
83
|
+
const enabledRef = useRefValue(options.enabled);
|
|
84
|
+
const onPositionChangeRef = useRefValue(options.onPositionChange);
|
|
85
|
+
const onDragStartRef = useRefValue(options.onDragStart);
|
|
86
|
+
const onDragEndRef = useRefValue(options.onDragEnd);
|
|
87
|
+
|
|
88
|
+
const setDragging = useCallback((value: boolean) => {
|
|
89
|
+
setIsDragging(value);
|
|
90
|
+
isDraggingRef.current = value;
|
|
91
|
+
}, []);
|
|
92
|
+
|
|
93
|
+
const assignRef = useCallback((node: T | null) => {
|
|
94
|
+
if (node) {
|
|
95
|
+
ref.current = node;
|
|
96
|
+
setElement(node);
|
|
97
|
+
} else {
|
|
98
|
+
ref.current = null;
|
|
99
|
+
setElement(null);
|
|
100
|
+
}
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
const el = ref.current;
|
|
105
|
+
if (!initialized.current && el) {
|
|
106
|
+
initialized.current = true;
|
|
107
|
+
pos.current = calculateInitialPosition(el, options);
|
|
108
|
+
el.style.left = `${pos.current.x}px`;
|
|
109
|
+
el.style.top = `${pos.current.y}px`;
|
|
110
|
+
el.style.right = 'unset';
|
|
111
|
+
el.style.bottom = 'unset';
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return () => {
|
|
115
|
+
initialized.current = false;
|
|
116
|
+
};
|
|
117
|
+
}, [
|
|
118
|
+
element,
|
|
119
|
+
options.constrainOffset,
|
|
120
|
+
options.initialPosition?.top,
|
|
121
|
+
options.initialPosition?.left,
|
|
122
|
+
options.initialPosition?.right,
|
|
123
|
+
options.initialPosition?.bottom,
|
|
124
|
+
options.constrainToViewport,
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
const el = element;
|
|
129
|
+
if (!el) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const controller = new AbortController();
|
|
134
|
+
const signal = controller.signal;
|
|
135
|
+
|
|
136
|
+
const onStart = (e: MouseEvent | TouchEvent) => {
|
|
137
|
+
if (enabledRef.current === false) {
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
const point = 'touches' in e ? e.touches[0] : e;
|
|
142
|
+
|
|
143
|
+
if ('button' in e && e.button !== 0) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
if (!getHandle(el, e.target, options)) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
setDragging(true);
|
|
152
|
+
previousUserSelect.current = document.body.style.userSelect;
|
|
153
|
+
previousWebkitUserSelect.current = document.body.style.webkitUserSelect;
|
|
154
|
+
document.body.style.userSelect = 'none';
|
|
155
|
+
document.body.style.webkitUserSelect = 'none';
|
|
156
|
+
|
|
157
|
+
const rect = el.getBoundingClientRect();
|
|
158
|
+
|
|
159
|
+
offset.current = {
|
|
160
|
+
x: point.clientX - rect.left,
|
|
161
|
+
y: point.clientY - rect.top,
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
onDragStartRef.current?.();
|
|
165
|
+
|
|
166
|
+
document.addEventListener('mousemove', onMove, { signal });
|
|
167
|
+
document.addEventListener('mouseup', onEnd, { signal });
|
|
168
|
+
document.addEventListener('touchmove', onMove, { signal, passive: false });
|
|
169
|
+
document.addEventListener('touchend', onEnd, { signal });
|
|
170
|
+
document.addEventListener('touchcancel', onEnd, { signal });
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const onMove = (e: MouseEvent | TouchEvent) => {
|
|
174
|
+
if (!isDraggingRef.current) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
const point = 'touches' in e ? e.touches[0] : e;
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
|
|
181
|
+
let x = point.clientX - offset.current.x;
|
|
182
|
+
let y = point.clientY - offset.current.y;
|
|
183
|
+
|
|
184
|
+
const constrained = getConstrainedPosition(el, { x, y }, options);
|
|
185
|
+
if (options.axis === 'x') {
|
|
186
|
+
x = constrained.x;
|
|
187
|
+
y = pos.current.y;
|
|
188
|
+
} else if (options.axis === 'y') {
|
|
189
|
+
x = pos.current.x;
|
|
190
|
+
y = constrained.y;
|
|
191
|
+
} else {
|
|
192
|
+
x = constrained.x;
|
|
193
|
+
y = constrained.y;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
pos.current = { x, y };
|
|
197
|
+
|
|
198
|
+
if (ref.current) {
|
|
199
|
+
ref.current.style.left = `${x}px`;
|
|
200
|
+
ref.current.style.top = `${y}px`;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
onPositionChangeRef.current?.({ x, y });
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
const onEnd = () => {
|
|
207
|
+
if (isDraggingRef.current) {
|
|
208
|
+
setDragging(false);
|
|
209
|
+
document.body.style.userSelect = previousUserSelect.current;
|
|
210
|
+
document.body.style.webkitUserSelect = previousWebkitUserSelect.current;
|
|
211
|
+
onDragEndRef.current?.();
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
el.addEventListener('mousedown', onStart, { signal });
|
|
216
|
+
el.addEventListener('touchstart', onStart, { signal, passive: false });
|
|
217
|
+
|
|
218
|
+
return () => {
|
|
219
|
+
controller.abort();
|
|
220
|
+
if (isDraggingRef.current) {
|
|
221
|
+
isDraggingRef.current = false;
|
|
222
|
+
document.body.style.userSelect = previousUserSelect.current;
|
|
223
|
+
document.body.style.webkitUserSelect = previousWebkitUserSelect.current;
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
}, [
|
|
227
|
+
options.constrainToViewport,
|
|
228
|
+
options.constrainOffset,
|
|
229
|
+
options.dragHandleSelector,
|
|
230
|
+
options.axis,
|
|
231
|
+
options.initialPosition?.top,
|
|
232
|
+
options.initialPosition?.left,
|
|
233
|
+
options.initialPosition?.right,
|
|
234
|
+
options.initialPosition?.bottom,
|
|
235
|
+
element,
|
|
236
|
+
]);
|
|
237
|
+
|
|
238
|
+
useEffect(() => {
|
|
239
|
+
const el = ref.current;
|
|
240
|
+
if (!el) {
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const observer = new ResizeObserver(() => {
|
|
245
|
+
// Re-clamp current position if element size changes
|
|
246
|
+
const constrained = getConstrainedPosition(el, pos.current, options);
|
|
247
|
+
pos.current = constrained;
|
|
248
|
+
el.style.left = `${constrained.x}px`;
|
|
249
|
+
el.style.top = `${constrained.y}px`;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
observer.observe(el);
|
|
253
|
+
|
|
254
|
+
return () => {
|
|
255
|
+
observer.disconnect();
|
|
256
|
+
};
|
|
257
|
+
}, [element, options.constrainToViewport, options.constrainOffset]);
|
|
258
|
+
|
|
259
|
+
const setPosition = useCallback(
|
|
260
|
+
(position: FloatingWindowPositionConfig) => {
|
|
261
|
+
const el = ref.current;
|
|
262
|
+
if (!el) {
|
|
263
|
+
return;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const offset = options.constrainOffset ?? 0;
|
|
267
|
+
const rect = el.getBoundingClientRect();
|
|
268
|
+
|
|
269
|
+
let x: number | undefined;
|
|
270
|
+
let y: number | undefined;
|
|
271
|
+
|
|
272
|
+
if (position.left != null) {
|
|
273
|
+
x = position.left;
|
|
274
|
+
} else if (position.right != null) {
|
|
275
|
+
x = window.innerWidth - rect.width - position.right;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (position.top != null) {
|
|
279
|
+
y = position.top;
|
|
280
|
+
} else if (position.bottom != null) {
|
|
281
|
+
y = window.innerHeight - rect.height - position.bottom;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
x = x ?? pos.current.x;
|
|
285
|
+
y = y ?? pos.current.y;
|
|
286
|
+
|
|
287
|
+
if (options.constrainToViewport) {
|
|
288
|
+
const clamped = clampToViewport(x, y, el, offset);
|
|
289
|
+
x = clamped.x;
|
|
290
|
+
y = clamped.y;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
pos.current = { x, y };
|
|
294
|
+
el.style.left = `${x}px`;
|
|
295
|
+
el.style.top = `${y}px`;
|
|
296
|
+
onPositionChangeRef.current?.({ x, y });
|
|
297
|
+
},
|
|
298
|
+
[options.constrainToViewport, options.constrainOffset],
|
|
299
|
+
);
|
|
300
|
+
|
|
301
|
+
return {
|
|
302
|
+
ref: assignRef,
|
|
303
|
+
setPosition,
|
|
304
|
+
isDragging,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
// -------------------------------------------------------
|
|
309
|
+
// Helper functions
|
|
310
|
+
// -------------------------------------------------------
|
|
311
|
+
|
|
312
|
+
function px(v: string) {
|
|
313
|
+
return v.endsWith('px') ? parseFloat(v) : undefined;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function calculateInitialPosition(
|
|
317
|
+
el: HTMLElement,
|
|
318
|
+
options: UseFloatingWindowOptions,
|
|
319
|
+
): { x: number; y: number } {
|
|
320
|
+
const rect = el.getBoundingClientRect();
|
|
321
|
+
const offset = options.constrainOffset ?? 0;
|
|
322
|
+
const winW = window.innerWidth;
|
|
323
|
+
const winH = window.innerHeight;
|
|
324
|
+
const style = window.getComputedStyle(el);
|
|
325
|
+
const top = options.initialPosition?.top;
|
|
326
|
+
const left = options.initialPosition?.left;
|
|
327
|
+
const right = options.initialPosition?.right;
|
|
328
|
+
const bottom = options.initialPosition?.bottom;
|
|
329
|
+
|
|
330
|
+
let x = offset;
|
|
331
|
+
let y = offset;
|
|
332
|
+
|
|
333
|
+
if (left != null) {
|
|
334
|
+
x = left;
|
|
335
|
+
} else if (right != null) {
|
|
336
|
+
x = winW - rect.width - right;
|
|
337
|
+
} else {
|
|
338
|
+
const styleLeft = px(style.left);
|
|
339
|
+
const styleRight = px(style.right);
|
|
340
|
+
x = styleLeft ?? (styleRight == null ? offset : winW - rect.width - styleRight);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (top != null) {
|
|
344
|
+
y = top;
|
|
345
|
+
} else if (bottom != null) {
|
|
346
|
+
y = winH - rect.height - bottom;
|
|
347
|
+
} else {
|
|
348
|
+
const styleTop = px(style.top);
|
|
349
|
+
const styleBottom = px(style.bottom);
|
|
350
|
+
y = styleTop ?? (styleBottom == null ? offset : winH - rect.height - styleBottom);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return options.constrainToViewport
|
|
354
|
+
? clampToViewport(x, y, el, options.constrainOffset)
|
|
355
|
+
: { x, y };
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
function getConstrainedPosition(
|
|
359
|
+
el: HTMLElement,
|
|
360
|
+
pos: FloatingWindowPosition,
|
|
361
|
+
options: UseFloatingWindowOptions,
|
|
362
|
+
) {
|
|
363
|
+
if (!options.constrainToViewport || !el) {
|
|
364
|
+
return pos;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const rect = el.getBoundingClientRect();
|
|
368
|
+
const offset = options.constrainOffset ?? 0;
|
|
369
|
+
const maxX = window.innerWidth - rect.width - offset;
|
|
370
|
+
const maxY = window.innerHeight - rect.height - offset;
|
|
371
|
+
|
|
372
|
+
return {
|
|
373
|
+
x: Math.min(Math.max(offset, pos.x), maxX),
|
|
374
|
+
y: Math.min(Math.max(offset, pos.y), maxY),
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function matchesExcludeSelector(target: Node, excludeSelector?: string): boolean {
|
|
379
|
+
if (!excludeSelector) {
|
|
380
|
+
return false;
|
|
381
|
+
}
|
|
382
|
+
if (!(target instanceof Element)) {
|
|
383
|
+
return false;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return Boolean(target.closest(excludeSelector));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function getHandle(
|
|
390
|
+
el: HTMLElement,
|
|
391
|
+
target: EventTarget | null,
|
|
392
|
+
options: UseFloatingWindowOptions,
|
|
393
|
+
): boolean {
|
|
394
|
+
if (!(target instanceof Node)) {
|
|
395
|
+
return false;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
// If no drag handle selector, allow dragging from entire element
|
|
399
|
+
if (!options.dragHandleSelector) {
|
|
400
|
+
return !matchesExcludeSelector(target, options.excludeDragHandleSelector);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
const handles = Array.from(el.querySelectorAll(options.dragHandleSelector));
|
|
404
|
+
return handles.some(
|
|
405
|
+
(handle) =>
|
|
406
|
+
handle.contains(target) && !matchesExcludeSelector(target, options.excludeDragHandleSelector),
|
|
407
|
+
);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
function clampToViewport(
|
|
411
|
+
x: number,
|
|
412
|
+
y: number,
|
|
413
|
+
el: HTMLElement,
|
|
414
|
+
offset: number = 0,
|
|
415
|
+
): { x: number; y: number } {
|
|
416
|
+
const rect = el.getBoundingClientRect();
|
|
417
|
+
const maxX = window.innerWidth - rect.width - offset;
|
|
418
|
+
const maxY = window.innerHeight - rect.height - offset;
|
|
419
|
+
|
|
420
|
+
return {
|
|
421
|
+
x: Math.min(Math.max(offset, x), maxX),
|
|
422
|
+
y: Math.min(Math.max(offset, y), maxY),
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
export namespace useFloatingWindow {
|
|
427
|
+
export type Options = UseFloatingWindowOptions;
|
|
428
|
+
export type Position = FloatingWindowPosition;
|
|
429
|
+
export type SetPosition = SetFloatingWindowPosition;
|
|
430
|
+
export type ReturnValue<T extends HTMLElement> = UseFloatingWindowReturnValue<T>;
|
|
431
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useRef } from 'octane';
|
|
2
|
+
import { useDidUpdate } from '../use-did-update/use-did-update';
|
|
3
|
+
|
|
4
|
+
export interface UseFocusReturnInput {
|
|
5
|
+
opened: boolean;
|
|
6
|
+
shouldReturnFocus?: boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type UseFocusReturnReturnValue = () => void;
|
|
10
|
+
|
|
11
|
+
export function useFocusReturn({
|
|
12
|
+
opened,
|
|
13
|
+
shouldReturnFocus = true,
|
|
14
|
+
}: UseFocusReturnInput): UseFocusReturnReturnValue {
|
|
15
|
+
const lastActiveElement = useRef<HTMLElement | null>(null);
|
|
16
|
+
const returnFocus = () => {
|
|
17
|
+
if (
|
|
18
|
+
lastActiveElement.current &&
|
|
19
|
+
'focus' in lastActiveElement.current &&
|
|
20
|
+
typeof lastActiveElement.current.focus === 'function'
|
|
21
|
+
) {
|
|
22
|
+
lastActiveElement.current?.focus({ preventScroll: true });
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
useDidUpdate(() => {
|
|
27
|
+
let timeout = -1;
|
|
28
|
+
|
|
29
|
+
const clearFocusTimeout = (event: KeyboardEvent) => {
|
|
30
|
+
if (event.key === 'Tab') {
|
|
31
|
+
window.clearTimeout(timeout);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
document.addEventListener('keydown', clearFocusTimeout);
|
|
36
|
+
|
|
37
|
+
if (opened) {
|
|
38
|
+
lastActiveElement.current = document.activeElement as HTMLElement;
|
|
39
|
+
} else if (shouldReturnFocus) {
|
|
40
|
+
const activeElementAtClose = document.activeElement;
|
|
41
|
+
timeout = window.setTimeout(() => {
|
|
42
|
+
const currentActiveElement = document.activeElement;
|
|
43
|
+
if (
|
|
44
|
+
currentActiveElement === null ||
|
|
45
|
+
currentActiveElement === document.body ||
|
|
46
|
+
currentActiveElement === activeElementAtClose
|
|
47
|
+
) {
|
|
48
|
+
returnFocus();
|
|
49
|
+
}
|
|
50
|
+
}, 10);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return () => {
|
|
54
|
+
window.clearTimeout(timeout);
|
|
55
|
+
document.removeEventListener('keydown', clearFocusTimeout);
|
|
56
|
+
};
|
|
57
|
+
}, [opened, shouldReturnFocus]);
|
|
58
|
+
|
|
59
|
+
return returnFocus;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export namespace useFocusReturn {
|
|
63
|
+
export type Input = UseFocusReturnInput;
|
|
64
|
+
export type ReturnValue = UseFocusReturnReturnValue;
|
|
65
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { findTabbableDescendants } from './tabbable';
|
|
2
|
+
|
|
3
|
+
export function scopeTab(node: HTMLElement, event: KeyboardEvent) {
|
|
4
|
+
const tabbable = findTabbableDescendants(node);
|
|
5
|
+
if (!tabbable.length) {
|
|
6
|
+
event.preventDefault();
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const finalTabbable = tabbable[event.shiftKey ? 0 : tabbable.length - 1];
|
|
10
|
+
const root = node.getRootNode() as unknown as DocumentOrShadowRoot;
|
|
11
|
+
let leavingFinalTabbable = finalTabbable === root.activeElement || node === root.activeElement;
|
|
12
|
+
|
|
13
|
+
const activeElement = root.activeElement as Element;
|
|
14
|
+
const activeElementIsRadio =
|
|
15
|
+
activeElement.tagName === 'INPUT' && activeElement.getAttribute('type') === 'radio';
|
|
16
|
+
if (activeElementIsRadio) {
|
|
17
|
+
const activeRadioGroup = tabbable.filter(
|
|
18
|
+
(element) =>
|
|
19
|
+
element.getAttribute('type') === 'radio' &&
|
|
20
|
+
element.getAttribute('name') === activeElement.getAttribute('name'),
|
|
21
|
+
);
|
|
22
|
+
leavingFinalTabbable = activeRadioGroup.includes(finalTabbable);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (!leavingFinalTabbable) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
|
|
31
|
+
const target = tabbable[event.shiftKey ? tabbable.length - 1 : 0];
|
|
32
|
+
|
|
33
|
+
if (target) {
|
|
34
|
+
target.focus();
|
|
35
|
+
}
|
|
36
|
+
}
|