@vitus-labs/hooks 2.0.0-alpha.9 → 2.0.0-beta.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/README.md +265 -40
- package/lib/index.d.ts +295 -1
- package/lib/index.js +569 -3
- package/lib/vitus-labs-hooks.native.js +435 -0
- package/package.json +20 -9
package/lib/index.js
CHANGED
|
@@ -1,6 +1,279 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { context, get, throttle } from "@vitus-labs/core";
|
|
2
|
+
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
|
|
4
|
+
//#region src/useBreakpoint.ts
|
|
5
|
+
/**
|
|
6
|
+
* Returns the name of the currently active breakpoint from the
|
|
7
|
+
* unistyle/core theme context (e.g. `"xs"`, `"md"`, `"lg"`).
|
|
8
|
+
*
|
|
9
|
+
* Reads `theme.breakpoints` from the nearest `Provider` and
|
|
10
|
+
* subscribes to viewport changes via `matchMedia`.
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` when no Provider or breakpoints are available.
|
|
13
|
+
*/
|
|
14
|
+
const useBreakpoint = () => {
|
|
15
|
+
const breakpoints = useContext(context)?.theme?.breakpoints;
|
|
16
|
+
const sorted = useMemo(() => {
|
|
17
|
+
if (!breakpoints) return [];
|
|
18
|
+
return Object.entries(breakpoints).sort(([, a], [, b]) => a - b);
|
|
19
|
+
}, [breakpoints]);
|
|
20
|
+
const [current, setCurrent] = useState(() => {
|
|
21
|
+
if (sorted.length === 0) return void 0;
|
|
22
|
+
if (typeof window === "undefined") return sorted[0]?.[0];
|
|
23
|
+
const width = window.innerWidth;
|
|
24
|
+
let match = sorted[0]?.[0];
|
|
25
|
+
for (const [name, min] of sorted) if (width >= min) match = name;
|
|
26
|
+
return match;
|
|
27
|
+
});
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
if (sorted.length === 0) return void 0;
|
|
30
|
+
const mqls = [];
|
|
31
|
+
const update = () => {
|
|
32
|
+
const width = window.innerWidth;
|
|
33
|
+
let match = sorted[0]?.[0];
|
|
34
|
+
for (const [name, min] of sorted) if (width >= min) match = name;
|
|
35
|
+
setCurrent(match);
|
|
36
|
+
};
|
|
37
|
+
for (const [, min] of sorted) {
|
|
38
|
+
const mql = window.matchMedia(`(min-width: ${min}px)`);
|
|
39
|
+
const handler = () => update();
|
|
40
|
+
mql.addEventListener("change", handler);
|
|
41
|
+
mqls.push({
|
|
42
|
+
mql,
|
|
43
|
+
handler
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
return () => {
|
|
47
|
+
for (const { mql, handler } of mqls) mql.removeEventListener("change", handler);
|
|
48
|
+
};
|
|
49
|
+
}, [sorted]);
|
|
50
|
+
return current;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
//#endregion
|
|
54
|
+
//#region src/useClickOutside.ts
|
|
55
|
+
/**
|
|
56
|
+
* Calls `handler` when a click (mousedown or touchstart) occurs
|
|
57
|
+
* outside the element referenced by `ref`.
|
|
58
|
+
*/
|
|
59
|
+
const useClickOutside = (ref, handler) => {
|
|
60
|
+
const handlerRef = useRef(handler);
|
|
61
|
+
handlerRef.current = handler;
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
const listener = (event) => {
|
|
64
|
+
const el = ref.current;
|
|
65
|
+
const target = event.target;
|
|
66
|
+
if (!el || !target || el.contains(target)) return;
|
|
67
|
+
handlerRef.current(event);
|
|
68
|
+
};
|
|
69
|
+
document.addEventListener("mousedown", listener);
|
|
70
|
+
document.addEventListener("touchstart", listener);
|
|
71
|
+
return () => {
|
|
72
|
+
document.removeEventListener("mousedown", listener);
|
|
73
|
+
document.removeEventListener("touchstart", listener);
|
|
74
|
+
};
|
|
75
|
+
}, [ref]);
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/useMediaQuery.ts
|
|
80
|
+
/**
|
|
81
|
+
* Subscribes to a CSS media query and returns whether it currently matches.
|
|
82
|
+
* Uses `window.matchMedia` with an event listener for live updates.
|
|
83
|
+
*/
|
|
84
|
+
const useMediaQuery = (query) => {
|
|
85
|
+
const [matches, setMatches] = useState(useCallback(() => typeof window !== "undefined" ? window.matchMedia(query).matches : false, [query]));
|
|
86
|
+
useEffect(() => {
|
|
87
|
+
const mql = window.matchMedia(query);
|
|
88
|
+
setMatches(mql.matches);
|
|
89
|
+
const handler = (e) => setMatches(e.matches);
|
|
90
|
+
mql.addEventListener("change", handler);
|
|
91
|
+
return () => mql.removeEventListener("change", handler);
|
|
92
|
+
}, [query]);
|
|
93
|
+
return matches;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
//#endregion
|
|
97
|
+
//#region src/useColorScheme.ts
|
|
98
|
+
/**
|
|
99
|
+
* Returns the user's preferred color scheme (`"light"` or `"dark"`).
|
|
100
|
+
* Reacts to OS-level preference changes in real time.
|
|
101
|
+
* Pairs with rocketstyle's `mode` system.
|
|
102
|
+
*/
|
|
103
|
+
const useColorScheme = () => {
|
|
104
|
+
return useMediaQuery("(prefers-color-scheme: dark)") ? "dark" : "light";
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/useControllableState.ts
|
|
109
|
+
/**
|
|
110
|
+
* Unified controlled/uncontrolled state pattern.
|
|
111
|
+
* When `value` is provided the component is controlled; otherwise
|
|
112
|
+
* internal state is used with `defaultValue` as the initial value.
|
|
113
|
+
* The `onChange` callback fires in both modes.
|
|
114
|
+
*/
|
|
115
|
+
const useControllableState = ({ value, defaultValue, onChange }) => {
|
|
116
|
+
const [internal, setInternal] = useState(defaultValue);
|
|
117
|
+
const onChangeRef = useRef(onChange);
|
|
118
|
+
onChangeRef.current = onChange;
|
|
119
|
+
const isControlled = value !== void 0;
|
|
120
|
+
const current = isControlled ? value : internal;
|
|
121
|
+
return [current, useCallback((next) => {
|
|
122
|
+
const nextValue = typeof next === "function" ? next(current) : next;
|
|
123
|
+
if (!isControlled) setInternal(nextValue);
|
|
124
|
+
onChangeRef.current?.(nextValue);
|
|
125
|
+
}, [current, isControlled])];
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
//#endregion
|
|
129
|
+
//#region src/useDebouncedCallback.ts
|
|
130
|
+
/**
|
|
131
|
+
* Returns a stable debounced version of the callback.
|
|
132
|
+
* The returned function has `.cancel()` and `.flush()` methods.
|
|
133
|
+
* Always calls the latest callback (no stale closures).
|
|
134
|
+
* Cleans up on unmount.
|
|
135
|
+
*/
|
|
136
|
+
const useDebouncedCallback = (callback, delay) => {
|
|
137
|
+
const callbackRef = useRef(callback);
|
|
138
|
+
callbackRef.current = callback;
|
|
139
|
+
const timerRef = useRef(null);
|
|
140
|
+
const lastArgsRef = useRef(null);
|
|
141
|
+
const cancel = useCallback(() => {
|
|
142
|
+
if (timerRef.current != null) {
|
|
143
|
+
clearTimeout(timerRef.current);
|
|
144
|
+
timerRef.current = null;
|
|
145
|
+
}
|
|
146
|
+
lastArgsRef.current = null;
|
|
147
|
+
}, []);
|
|
148
|
+
const flush = useCallback(() => {
|
|
149
|
+
if (timerRef.current != null && lastArgsRef.current != null) {
|
|
150
|
+
clearTimeout(timerRef.current);
|
|
151
|
+
timerRef.current = null;
|
|
152
|
+
callbackRef.current(...lastArgsRef.current);
|
|
153
|
+
lastArgsRef.current = null;
|
|
154
|
+
}
|
|
155
|
+
}, []);
|
|
156
|
+
useEffect(() => cancel, [cancel]);
|
|
157
|
+
const debounced = useCallback((...args) => {
|
|
158
|
+
lastArgsRef.current = args;
|
|
159
|
+
if (timerRef.current != null) clearTimeout(timerRef.current);
|
|
160
|
+
timerRef.current = setTimeout(() => {
|
|
161
|
+
timerRef.current = null;
|
|
162
|
+
callbackRef.current(...args);
|
|
163
|
+
lastArgsRef.current = null;
|
|
164
|
+
}, delay);
|
|
165
|
+
}, [delay]);
|
|
166
|
+
return Object.assign(debounced, {
|
|
167
|
+
cancel,
|
|
168
|
+
flush
|
|
169
|
+
});
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
//#endregion
|
|
173
|
+
//#region src/useDebouncedValue.ts
|
|
174
|
+
/**
|
|
175
|
+
* Returns a debounced version of the value that only updates
|
|
176
|
+
* after `delay` ms of inactivity.
|
|
177
|
+
*
|
|
178
|
+
* @example
|
|
179
|
+
* ```ts
|
|
180
|
+
* const [search, setSearch] = useState('')
|
|
181
|
+
* const debouncedSearch = useDebouncedValue(search, 300)
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
const useDebouncedValue = (value, delay) => {
|
|
185
|
+
const [debounced, setDebounced] = useState(value);
|
|
186
|
+
useEffect(() => {
|
|
187
|
+
const id = setTimeout(() => setDebounced(value), delay);
|
|
188
|
+
return () => clearTimeout(id);
|
|
189
|
+
}, [value, delay]);
|
|
190
|
+
return debounced;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
//#endregion
|
|
194
|
+
//#region src/useElementSize.ts
|
|
195
|
+
/**
|
|
196
|
+
* Tracks an element's `width` and `height` via `ResizeObserver`.
|
|
197
|
+
* Returns `[ref, { width, height }]` — pass `ref` as a callback ref.
|
|
198
|
+
*/
|
|
199
|
+
const useElementSize = () => {
|
|
200
|
+
const [size, setSize] = useState({
|
|
201
|
+
width: 0,
|
|
202
|
+
height: 0
|
|
203
|
+
});
|
|
204
|
+
const observerRef = useRef(null);
|
|
205
|
+
return [useCallback((node) => {
|
|
206
|
+
if (observerRef.current) {
|
|
207
|
+
observerRef.current.disconnect();
|
|
208
|
+
observerRef.current = null;
|
|
209
|
+
}
|
|
210
|
+
if (!node || typeof ResizeObserver === "undefined") return;
|
|
211
|
+
const observer = new ResizeObserver((entries) => {
|
|
212
|
+
const entry = entries[0];
|
|
213
|
+
if (!entry) return;
|
|
214
|
+
const { width, height } = entry.contentRect;
|
|
215
|
+
setSize((prev) => prev.width === width && prev.height === height ? prev : {
|
|
216
|
+
width,
|
|
217
|
+
height
|
|
218
|
+
});
|
|
219
|
+
});
|
|
220
|
+
observer.observe(node);
|
|
221
|
+
observerRef.current = observer;
|
|
222
|
+
const rect = node.getBoundingClientRect();
|
|
223
|
+
setSize((prev) => prev.width === rect.width && prev.height === rect.height ? prev : {
|
|
224
|
+
width: rect.width,
|
|
225
|
+
height: rect.height
|
|
226
|
+
});
|
|
227
|
+
}, []), size];
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
//#endregion
|
|
231
|
+
//#region src/useFocus.ts
|
|
232
|
+
/**
|
|
233
|
+
* Simple focus-state hook that returns a boolean plus stable
|
|
234
|
+
* `onFocus`/`onBlur` handlers ready to spread onto an element.
|
|
235
|
+
*/
|
|
236
|
+
const useFocus = (initial = false) => {
|
|
237
|
+
const [focused, setFocused] = useState(initial);
|
|
238
|
+
return {
|
|
239
|
+
focused,
|
|
240
|
+
onFocus: useCallback(() => setFocused(true), []),
|
|
241
|
+
onBlur: useCallback(() => setFocused(false), [])
|
|
242
|
+
};
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
//#endregion
|
|
246
|
+
//#region src/useFocusTrap.ts
|
|
247
|
+
const FOCUSABLE = "a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex=\"-1\"])";
|
|
248
|
+
const wrapFocus = (e, target) => {
|
|
249
|
+
if (!target) return;
|
|
250
|
+
e.preventDefault();
|
|
251
|
+
target.focus();
|
|
252
|
+
};
|
|
253
|
+
/**
|
|
254
|
+
* Traps keyboard focus within the referenced container.
|
|
255
|
+
* Tab and Shift+Tab cycle through focusable elements inside.
|
|
256
|
+
* Useful for modals, dialogs, and dropdown menus.
|
|
257
|
+
*/
|
|
258
|
+
const useFocusTrap = (ref, enabled = true) => {
|
|
259
|
+
useEffect(() => {
|
|
260
|
+
if (!enabled) return void 0;
|
|
261
|
+
const handler = (e) => {
|
|
262
|
+
if (e.key !== "Tab" || !ref.current) return;
|
|
263
|
+
const focusable = ref.current.querySelectorAll(FOCUSABLE);
|
|
264
|
+
if (focusable.length === 0) return;
|
|
265
|
+
const first = focusable[0];
|
|
266
|
+
const last = focusable[focusable.length - 1];
|
|
267
|
+
const active = document.activeElement;
|
|
268
|
+
if (e.shiftKey && active === first) wrapFocus(e, last);
|
|
269
|
+
else if (!e.shiftKey && active === last) wrapFocus(e, first);
|
|
270
|
+
};
|
|
271
|
+
document.addEventListener("keydown", handler);
|
|
272
|
+
return () => document.removeEventListener("keydown", handler);
|
|
273
|
+
}, [ref, enabled]);
|
|
274
|
+
};
|
|
275
|
+
|
|
276
|
+
//#endregion
|
|
4
277
|
//#region src/useHover.ts
|
|
5
278
|
/**
|
|
6
279
|
* Simple hover-state hook that returns a boolean plus stable
|
|
@@ -15,6 +288,299 @@ const useHover = (initial = false) => {
|
|
|
15
288
|
};
|
|
16
289
|
};
|
|
17
290
|
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/useIntersection.ts
|
|
293
|
+
/**
|
|
294
|
+
* Observes an element's intersection with the viewport (or a root element).
|
|
295
|
+
* Returns `[ref, entry]` — pass `ref` as a callback ref.
|
|
296
|
+
*/
|
|
297
|
+
const useIntersection = (options = {}) => {
|
|
298
|
+
const { threshold = 0, rootMargin = "0px", root = null } = options;
|
|
299
|
+
const [entry, setEntry] = useState(null);
|
|
300
|
+
const observerRef = useRef(null);
|
|
301
|
+
return [useCallback((node) => {
|
|
302
|
+
if (observerRef.current) {
|
|
303
|
+
observerRef.current.disconnect();
|
|
304
|
+
observerRef.current = null;
|
|
305
|
+
}
|
|
306
|
+
if (!node || typeof IntersectionObserver === "undefined") return;
|
|
307
|
+
const observer = new IntersectionObserver((entries) => {
|
|
308
|
+
setEntry(entries[0] ?? null);
|
|
309
|
+
}, {
|
|
310
|
+
threshold,
|
|
311
|
+
rootMargin,
|
|
312
|
+
root
|
|
313
|
+
});
|
|
314
|
+
observer.observe(node);
|
|
315
|
+
observerRef.current = observer;
|
|
316
|
+
}, [
|
|
317
|
+
threshold,
|
|
318
|
+
rootMargin,
|
|
319
|
+
root
|
|
320
|
+
]), entry];
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
//#endregion
|
|
324
|
+
//#region src/useInterval.ts
|
|
325
|
+
/**
|
|
326
|
+
* Declarative `setInterval` with auto-cleanup.
|
|
327
|
+
* Pass `null` as `delay` to pause the interval.
|
|
328
|
+
* Always calls the latest callback (no stale closures).
|
|
329
|
+
*/
|
|
330
|
+
const useInterval = (callback, delay) => {
|
|
331
|
+
const callbackRef = useRef(callback);
|
|
332
|
+
callbackRef.current = callback;
|
|
333
|
+
useEffect(() => {
|
|
334
|
+
if (delay === null) return void 0;
|
|
335
|
+
const id = setInterval(() => callbackRef.current(), delay);
|
|
336
|
+
return () => clearInterval(id);
|
|
337
|
+
}, [delay]);
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
//#endregion
|
|
341
|
+
//#region src/useIsomorphicLayoutEffect.ts
|
|
342
|
+
/**
|
|
343
|
+
* `useLayoutEffect` on the client, `useEffect` on the server.
|
|
344
|
+
* Avoids the React SSR warning about useLayoutEffect.
|
|
345
|
+
*/
|
|
346
|
+
const useIsomorphicLayoutEffect = typeof window !== "undefined" ? useLayoutEffect : useEffect;
|
|
347
|
+
|
|
348
|
+
//#endregion
|
|
349
|
+
//#region src/useKeyboard.ts
|
|
350
|
+
/**
|
|
351
|
+
* Listens for a specific keyboard key and calls the handler.
|
|
352
|
+
* Matches `event.key` (e.g. `"Escape"`, `"Enter"`, `"a"`).
|
|
353
|
+
* Always calls the latest handler (no stale closures).
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* useKeyboard('Escape', () => setOpen(false))
|
|
358
|
+
* ```
|
|
359
|
+
*/
|
|
360
|
+
const useKeyboard = (key, handler) => {
|
|
361
|
+
const handlerRef = useRef(handler);
|
|
362
|
+
handlerRef.current = handler;
|
|
363
|
+
useEffect(() => {
|
|
364
|
+
const listener = (e) => {
|
|
365
|
+
if (e.key === key) handlerRef.current(e);
|
|
366
|
+
};
|
|
367
|
+
window.addEventListener("keydown", listener);
|
|
368
|
+
return () => window.removeEventListener("keydown", listener);
|
|
369
|
+
}, [key]);
|
|
370
|
+
};
|
|
371
|
+
|
|
372
|
+
//#endregion
|
|
373
|
+
//#region src/useLatest.ts
|
|
374
|
+
/**
|
|
375
|
+
* Returns a ref that always holds the latest value.
|
|
376
|
+
* Useful to avoid stale closures in callbacks and effects.
|
|
377
|
+
*/
|
|
378
|
+
const useLatest = (value) => {
|
|
379
|
+
const ref = useRef(value);
|
|
380
|
+
ref.current = value;
|
|
381
|
+
return ref;
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
//#endregion
|
|
385
|
+
//#region src/useMergedRef.ts
|
|
386
|
+
/**
|
|
387
|
+
* Merges multiple refs (callback or object) into a single stable callback ref.
|
|
388
|
+
* Handles null, callback refs, and object refs with `.current`.
|
|
389
|
+
*/
|
|
390
|
+
const useMergedRef = (...refs) => {
|
|
391
|
+
return useCallback((node) => {
|
|
392
|
+
for (const ref of refs) {
|
|
393
|
+
if (!ref) continue;
|
|
394
|
+
if (typeof ref === "function") ref(node);
|
|
395
|
+
else ref.current = node;
|
|
396
|
+
}
|
|
397
|
+
}, refs);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
//#endregion
|
|
401
|
+
//#region src/usePrevious.ts
|
|
402
|
+
/**
|
|
403
|
+
* Returns the value from the previous render.
|
|
404
|
+
* Returns `undefined` on the first render.
|
|
405
|
+
*/
|
|
406
|
+
const usePrevious = (value) => {
|
|
407
|
+
const ref = useRef(void 0);
|
|
408
|
+
useEffect(() => {
|
|
409
|
+
ref.current = value;
|
|
410
|
+
});
|
|
411
|
+
return ref.current;
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
//#endregion
|
|
415
|
+
//#region src/useReducedMotion.ts
|
|
416
|
+
/**
|
|
417
|
+
* Returns `true` when the user prefers reduced motion.
|
|
418
|
+
* Use to disable or simplify animations for accessibility.
|
|
419
|
+
*/
|
|
420
|
+
const useReducedMotion = () => useMediaQuery("(prefers-reduced-motion: reduce)");
|
|
421
|
+
|
|
422
|
+
//#endregion
|
|
423
|
+
//#region src/useRootSize.ts
|
|
424
|
+
/**
|
|
425
|
+
* Returns `rootSize` from the theme context along with
|
|
426
|
+
* `pxToRem` and `remToPx` conversion utilities.
|
|
427
|
+
*
|
|
428
|
+
* Defaults to `16` when no Provider is mounted.
|
|
429
|
+
*/
|
|
430
|
+
const useRootSize = () => {
|
|
431
|
+
const rootSize = useContext(context)?.theme?.rootSize ?? 16;
|
|
432
|
+
return useMemo(() => ({
|
|
433
|
+
rootSize,
|
|
434
|
+
pxToRem: (px) => `${px / rootSize}rem`,
|
|
435
|
+
remToPx: (rem) => rem * rootSize
|
|
436
|
+
}), [rootSize]);
|
|
437
|
+
};
|
|
438
|
+
|
|
439
|
+
//#endregion
|
|
440
|
+
//#region src/useScrollLock.ts
|
|
441
|
+
let lockCount = 0;
|
|
442
|
+
let originalOverflow;
|
|
443
|
+
/**
|
|
444
|
+
* Locks page scroll by setting `overflow: hidden` on `document.body`.
|
|
445
|
+
* Uses a reference counter so concurrent locks don't clobber each other.
|
|
446
|
+
*/
|
|
447
|
+
const useScrollLock = (enabled) => {
|
|
448
|
+
useEffect(() => {
|
|
449
|
+
if (!enabled) return void 0;
|
|
450
|
+
if (lockCount === 0) originalOverflow = document.body.style.overflow;
|
|
451
|
+
lockCount++;
|
|
452
|
+
document.body.style.overflow = "hidden";
|
|
453
|
+
return () => {
|
|
454
|
+
lockCount--;
|
|
455
|
+
if (lockCount === 0) {
|
|
456
|
+
document.body.style.overflow = originalOverflow ?? "";
|
|
457
|
+
originalOverflow = void 0;
|
|
458
|
+
}
|
|
459
|
+
};
|
|
460
|
+
}, [enabled]);
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
//#endregion
|
|
464
|
+
//#region src/useSpacing.ts
|
|
465
|
+
/**
|
|
466
|
+
* Returns a `spacing(n)` function that computes spacing values
|
|
467
|
+
* based on `rootSize` from the theme.
|
|
468
|
+
*
|
|
469
|
+
* @param base - Base spacing unit in px (defaults to `rootSize / 2`, i.e. 8px)
|
|
470
|
+
*
|
|
471
|
+
* @example
|
|
472
|
+
* ```ts
|
|
473
|
+
* const spacing = useSpacing()
|
|
474
|
+
* spacing(1) // "8px"
|
|
475
|
+
* spacing(2) // "16px"
|
|
476
|
+
* spacing(0.5) // "4px"
|
|
477
|
+
* ```
|
|
478
|
+
*/
|
|
479
|
+
const useSpacing = (base) => {
|
|
480
|
+
const { rootSize } = useRootSize();
|
|
481
|
+
const unit = base ?? rootSize / 2;
|
|
482
|
+
return useMemo(() => (multiplier) => `${unit * multiplier}px`, [unit]);
|
|
483
|
+
};
|
|
484
|
+
|
|
485
|
+
//#endregion
|
|
486
|
+
//#region src/useThemeValue.ts
|
|
487
|
+
/**
|
|
488
|
+
* Deep-reads a value from the current theme by dot-separated path.
|
|
489
|
+
*
|
|
490
|
+
* @example
|
|
491
|
+
* ```ts
|
|
492
|
+
* const primary = useThemeValue<string>('colors.primary')
|
|
493
|
+
* const columns = useThemeValue<number>('grid.columns')
|
|
494
|
+
* ```
|
|
495
|
+
*/
|
|
496
|
+
const useThemeValue = (path) => {
|
|
497
|
+
const theme = useContext(context)?.theme;
|
|
498
|
+
if (!theme) return void 0;
|
|
499
|
+
return get(theme, path);
|
|
500
|
+
};
|
|
501
|
+
|
|
502
|
+
//#endregion
|
|
503
|
+
//#region src/useThrottledCallback.ts
|
|
504
|
+
/**
|
|
505
|
+
* Returns a stable throttled version of the callback.
|
|
506
|
+
* Uses `throttle` from `@vitus-labs/core`.
|
|
507
|
+
* Always calls the latest callback (no stale closures).
|
|
508
|
+
* Cleans up on unmount.
|
|
509
|
+
*/
|
|
510
|
+
const useThrottledCallback = (callback, delay) => {
|
|
511
|
+
const callbackRef = useRef(callback);
|
|
512
|
+
callbackRef.current = callback;
|
|
513
|
+
const throttled = useMemo(() => throttle((...args) => callbackRef.current(...args), delay), [delay]);
|
|
514
|
+
useEffect(() => () => throttled.cancel(), [throttled]);
|
|
515
|
+
return throttled;
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
//#endregion
|
|
519
|
+
//#region src/useTimeout.ts
|
|
520
|
+
/**
|
|
521
|
+
* Declarative `setTimeout` with auto-cleanup.
|
|
522
|
+
* Pass `null` as `delay` to disable. Returns `reset` and `clear` controls.
|
|
523
|
+
* Always calls the latest callback (no stale closures).
|
|
524
|
+
*/
|
|
525
|
+
const useTimeout = (callback, delay) => {
|
|
526
|
+
const callbackRef = useRef(callback);
|
|
527
|
+
callbackRef.current = callback;
|
|
528
|
+
const timerRef = useRef(null);
|
|
529
|
+
const clear = useCallback(() => {
|
|
530
|
+
if (timerRef.current != null) {
|
|
531
|
+
clearTimeout(timerRef.current);
|
|
532
|
+
timerRef.current = null;
|
|
533
|
+
}
|
|
534
|
+
}, []);
|
|
535
|
+
const reset = useCallback(() => {
|
|
536
|
+
clear();
|
|
537
|
+
if (delay !== null) timerRef.current = setTimeout(() => {
|
|
538
|
+
timerRef.current = null;
|
|
539
|
+
callbackRef.current();
|
|
540
|
+
}, delay);
|
|
541
|
+
}, [delay, clear]);
|
|
542
|
+
useEffect(() => {
|
|
543
|
+
reset();
|
|
544
|
+
return clear;
|
|
545
|
+
}, [reset, clear]);
|
|
546
|
+
return {
|
|
547
|
+
reset,
|
|
548
|
+
clear
|
|
549
|
+
};
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
//#endregion
|
|
553
|
+
//#region src/useToggle.ts
|
|
554
|
+
/**
|
|
555
|
+
* Boolean state with `toggle`, `setTrue`, and `setFalse` helpers.
|
|
556
|
+
* Returns `[value, toggle, setTrue, setFalse]`.
|
|
557
|
+
*/
|
|
558
|
+
const useToggle = (initial = false) => {
|
|
559
|
+
const [value, setValue] = useState(initial);
|
|
560
|
+
return [
|
|
561
|
+
value,
|
|
562
|
+
useCallback(() => setValue((v) => !v), []),
|
|
563
|
+
useCallback(() => setValue(true), []),
|
|
564
|
+
useCallback(() => setValue(false), [])
|
|
565
|
+
];
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
//#endregion
|
|
569
|
+
//#region src/useUpdateEffect.ts
|
|
570
|
+
/**
|
|
571
|
+
* Like `useEffect` but skips the initial mount — only fires on updates.
|
|
572
|
+
*/
|
|
573
|
+
const useUpdateEffect = (effect, deps) => {
|
|
574
|
+
const mounted = useRef(false);
|
|
575
|
+
useEffect(() => {
|
|
576
|
+
if (!mounted.current) {
|
|
577
|
+
mounted.current = true;
|
|
578
|
+
return;
|
|
579
|
+
}
|
|
580
|
+
return effect();
|
|
581
|
+
}, deps);
|
|
582
|
+
};
|
|
583
|
+
|
|
18
584
|
//#endregion
|
|
19
585
|
//#region src/useWindowResize.ts
|
|
20
586
|
/**
|
|
@@ -53,5 +619,5 @@ const useWindowResize = ({ throttleDelay = 200, onChange } = {}, { width = 0, he
|
|
|
53
619
|
};
|
|
54
620
|
|
|
55
621
|
//#endregion
|
|
56
|
-
export { useHover, useWindowResize };
|
|
622
|
+
export { useBreakpoint, useClickOutside, useColorScheme, useControllableState, useDebouncedCallback, useDebouncedValue, useElementSize, useFocus, useFocusTrap, useHover, useIntersection, useInterval, useIsomorphicLayoutEffect, useKeyboard, useLatest, useMediaQuery, useMergedRef, usePrevious, useReducedMotion, useRootSize, useScrollLock, useSpacing, useThemeValue, useThrottledCallback, useTimeout, useToggle, useUpdateEffect, useWindowResize };
|
|
57
623
|
//# sourceMappingURL=index.js.map
|