@develemit/web-hooks 0.0.0-stage → 0.2.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.
Files changed (73) hide show
  1. package/CHANGELOG.md +24 -0
  2. package/LICENSE +21 -0
  3. package/README.md +249 -2
  4. package/dist/auto-grow.cjs +60 -0
  5. package/dist/auto-grow.d.cts +14 -0
  6. package/dist/auto-grow.d.ts +14 -0
  7. package/dist/auto-grow.js +9 -0
  8. package/dist/chunk-3CACI4OQ.js +68 -0
  9. package/dist/chunk-3UGRXQIA.js +39 -0
  10. package/dist/chunk-5CA5RIMH.js +34 -0
  11. package/dist/chunk-5KIH2XLV.js +136 -0
  12. package/dist/chunk-6KCRVSLS.js +80 -0
  13. package/dist/chunk-DNU57XEJ.js +82 -0
  14. package/dist/chunk-IWQDAJEP.js +69 -0
  15. package/dist/chunk-LZKAGDER.js +22 -0
  16. package/dist/chunk-NKLEB5TR.js +98 -0
  17. package/dist/chunk-OZ4PU4E3.js +115 -0
  18. package/dist/chunk-S4YKMGQE.js +23 -0
  19. package/dist/chunk-UPVNSGIK.js +72 -0
  20. package/dist/chunk-UZADAINO.js +61 -0
  21. package/dist/clipboard.cjs +94 -0
  22. package/dist/clipboard.d.cts +17 -0
  23. package/dist/clipboard.d.ts +17 -0
  24. package/dist/clipboard.js +9 -0
  25. package/dist/debounce.cjs +96 -0
  26. package/dist/debounce.d.cts +21 -0
  27. package/dist/debounce.d.ts +21 -0
  28. package/dist/debounce.js +11 -0
  29. package/dist/drawer-swipe.cjs +149 -0
  30. package/dist/drawer-swipe.d.cts +26 -0
  31. package/dist/drawer-swipe.d.ts +26 -0
  32. package/dist/drawer-swipe.js +25 -0
  33. package/dist/escape.cjs +48 -0
  34. package/dist/escape.d.cts +3 -0
  35. package/dist/escape.d.ts +3 -0
  36. package/dist/escape.js +7 -0
  37. package/dist/focus-trap.cjs +110 -0
  38. package/dist/focus-trap.d.cts +17 -0
  39. package/dist/focus-trap.d.ts +17 -0
  40. package/dist/focus-trap.js +13 -0
  41. package/dist/hotkey.cjs +127 -0
  42. package/dist/hotkey.d.cts +31 -0
  43. package/dist/hotkey.d.ts +31 -0
  44. package/dist/hotkey.js +15 -0
  45. package/dist/index.cjs +918 -0
  46. package/dist/index.d.cts +14 -0
  47. package/dist/index.d.ts +14 -0
  48. package/dist/index.js +115 -0
  49. package/dist/media-query.cjs +90 -0
  50. package/dist/media-query.d.cts +12 -0
  51. package/dist/media-query.d.ts +12 -0
  52. package/dist/media-query.js +15 -0
  53. package/dist/outside-press.cjs +66 -0
  54. package/dist/outside-press.d.cts +15 -0
  55. package/dist/outside-press.d.ts +15 -0
  56. package/dist/outside-press.js +11 -0
  57. package/dist/scroll-lock.cjs +107 -0
  58. package/dist/scroll-lock.d.cts +8 -0
  59. package/dist/scroll-lock.d.ts +8 -0
  60. package/dist/scroll-lock.js +11 -0
  61. package/dist/storage-state.cjs +161 -0
  62. package/dist/storage-state.d.cts +14 -0
  63. package/dist/storage-state.d.ts +14 -0
  64. package/dist/storage-state.js +7 -0
  65. package/dist/unsaved-guard.cjs +48 -0
  66. package/dist/unsaved-guard.d.cts +5 -0
  67. package/dist/unsaved-guard.d.ts +5 -0
  68. package/dist/unsaved-guard.js +9 -0
  69. package/dist/visibility-polling.cjs +98 -0
  70. package/dist/visibility-polling.d.cts +14 -0
  71. package/dist/visibility-polling.d.ts +14 -0
  72. package/dist/visibility-polling.js +9 -0
  73. package/package.json +126 -3
@@ -0,0 +1,136 @@
1
+ "use client";
2
+
3
+ // src/storage-state/use-storage-state.ts
4
+ import { useCallback, useEffect, useRef, useSyncExternalStore } from "react";
5
+
6
+ // src/storage-state/storage-store.ts
7
+ function getStorage(kind) {
8
+ try {
9
+ if (typeof window === "undefined") return null;
10
+ return kind === "session" ? window.sessionStorage : window.localStorage;
11
+ } catch {
12
+ return null;
13
+ }
14
+ }
15
+ function readRaw(storage, key) {
16
+ try {
17
+ return storage ? storage.getItem(key) : null;
18
+ } catch {
19
+ return null;
20
+ }
21
+ }
22
+ function writeRaw(storage, key, value) {
23
+ try {
24
+ if (!storage) return false;
25
+ storage.setItem(key, value);
26
+ return true;
27
+ } catch {
28
+ return false;
29
+ }
30
+ }
31
+ function removeRaw(storage, key) {
32
+ try {
33
+ if (!storage) return false;
34
+ storage.removeItem(key);
35
+ return true;
36
+ } catch {
37
+ return false;
38
+ }
39
+ }
40
+ function parseOr(raw, parse, fallback) {
41
+ if (raw === null) return fallback;
42
+ try {
43
+ return parse(raw);
44
+ } catch {
45
+ return fallback;
46
+ }
47
+ }
48
+ var slot = (kind, key) => `${kind}\0${key}`;
49
+ var memory = /* @__PURE__ */ new Map();
50
+ var listeners = /* @__PURE__ */ new Map();
51
+ function readCurrent(kind, key) {
52
+ const held = memory.get(slot(kind, key));
53
+ if (held !== void 0) return held;
54
+ return readRaw(getStorage(kind), key);
55
+ }
56
+ function notify(id) {
57
+ for (const listener of [...listeners.get(id) ?? []]) listener();
58
+ }
59
+ function writeCurrent(kind, key, raw) {
60
+ const id = slot(kind, key);
61
+ if (writeRaw(getStorage(kind), key, raw)) memory.delete(id);
62
+ else memory.set(id, raw);
63
+ notify(id);
64
+ }
65
+ function removeCurrent(kind, key) {
66
+ const id = slot(kind, key);
67
+ if (removeRaw(getStorage(kind), key)) memory.delete(id);
68
+ else memory.set(id, null);
69
+ notify(id);
70
+ }
71
+ function onStorageEvent(event) {
72
+ const kinds = [];
73
+ if (!event.storageArea || event.storageArea === getStorage("local")) kinds.push("local");
74
+ if (!event.storageArea || event.storageArea === getStorage("session")) kinds.push("session");
75
+ for (const id of [...listeners.keys()]) {
76
+ const [kind, key] = id.split("\0");
77
+ if (!kinds.includes(kind)) continue;
78
+ if (event.key !== null && event.key !== key) continue;
79
+ memory.delete(id);
80
+ notify(id);
81
+ }
82
+ }
83
+ function subscribeToKey(kind, key, listener) {
84
+ const id = slot(kind, key);
85
+ const wasEmpty = listeners.size === 0;
86
+ let set = listeners.get(id);
87
+ if (!set) listeners.set(id, set = /* @__PURE__ */ new Set());
88
+ set.add(listener);
89
+ if (wasEmpty) window.addEventListener("storage", onStorageEvent);
90
+ return () => {
91
+ const current = listeners.get(id);
92
+ current?.delete(listener);
93
+ if (current?.size === 0) listeners.delete(id);
94
+ if (listeners.size === 0) window.removeEventListener("storage", onStorageEvent);
95
+ };
96
+ }
97
+
98
+ // src/storage-state/use-storage-state.ts
99
+ function useStorageState(key, defaultValue, options = {}) {
100
+ const kind = options.storage ?? "local";
101
+ const serialize = options.serialize ?? JSON.stringify;
102
+ const parse = options.parse ?? JSON.parse;
103
+ const latest = useRef({ serialize, parse, defaultValue });
104
+ useEffect(() => {
105
+ latest.current = { serialize, parse, defaultValue };
106
+ });
107
+ const cache = useRef(null);
108
+ const subscribe = useCallback((onChange) => subscribeToKey(kind, key, onChange), [kind, key]);
109
+ const getSnapshot = useCallback(() => {
110
+ const raw = readCurrent(kind, key);
111
+ const hit = cache.current;
112
+ if (hit && hit.kind === kind && hit.key === key && hit.raw === raw && hit.parse === parse) return hit.value;
113
+ const value2 = parseOr(raw, parse, defaultValue);
114
+ cache.current = { kind, key, raw, parse, value: value2 };
115
+ return value2;
116
+ }, [kind, key, parse, defaultValue]);
117
+ const getServerSnapshot = useCallback(() => defaultValue, [defaultValue]);
118
+ const value = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
119
+ const set = useCallback(
120
+ (next) => {
121
+ const { serialize: encode, parse: decode, defaultValue: fallback } = latest.current;
122
+ try {
123
+ const resolved = typeof next === "function" ? next(parseOr(readCurrent(kind, key), decode, fallback)) : next;
124
+ writeCurrent(kind, key, encode(resolved));
125
+ } catch {
126
+ }
127
+ },
128
+ [kind, key]
129
+ );
130
+ const remove = useCallback(() => removeCurrent(kind, key), [kind, key]);
131
+ return [value, set, remove];
132
+ }
133
+
134
+ export {
135
+ useStorageState
136
+ };
@@ -0,0 +1,80 @@
1
+ "use client";
2
+
3
+ // src/scroll-lock/use-scroll-lock.ts
4
+ import { useEffect } from "react";
5
+
6
+ // src/scroll-lock/lock-styles.ts
7
+ var LOCKED_PROPERTIES = [
8
+ "position",
9
+ "top",
10
+ "left",
11
+ "right",
12
+ "width",
13
+ "overflow",
14
+ "paddingRight"
15
+ ];
16
+ function computeLockStyles({ scrollY, scrollbarWidth, existingPaddingRight }) {
17
+ const styles = {
18
+ position: "fixed",
19
+ top: `-${scrollY}px`,
20
+ left: "0",
21
+ right: "0",
22
+ width: "100%",
23
+ overflow: "hidden"
24
+ };
25
+ if (scrollbarWidth > 0) styles.paddingRight = `${existingPaddingRight + scrollbarWidth}px`;
26
+ return styles;
27
+ }
28
+
29
+ // src/scroll-lock/scroll-lock.ts
30
+ var count = 0;
31
+ var saved = null;
32
+ function applyFirstLock() {
33
+ const { body, documentElement } = document;
34
+ const inline = {};
35
+ for (const property of LOCKED_PROPERTIES) inline[property] = body.style[property];
36
+ saved = { scrollY: window.scrollY, inline };
37
+ const styles = computeLockStyles({
38
+ scrollY: saved.scrollY,
39
+ scrollbarWidth: Math.max(0, window.innerWidth - documentElement.clientWidth),
40
+ existingPaddingRight: parseFloat(getComputedStyle(body).paddingRight) || 0
41
+ });
42
+ Object.assign(body.style, styles);
43
+ }
44
+ function restoreLastLock() {
45
+ if (!saved) return;
46
+ const { scrollY, inline } = saved;
47
+ saved = null;
48
+ Object.assign(document.body.style, inline);
49
+ window.scrollTo(0, scrollY);
50
+ }
51
+ function lockScroll() {
52
+ if (count === 0) applyFirstLock();
53
+ count += 1;
54
+ let released = false;
55
+ return () => {
56
+ if (released) return;
57
+ released = true;
58
+ count -= 1;
59
+ if (count === 0) restoreLastLock();
60
+ };
61
+ }
62
+ function unlockScroll() {
63
+ if (count === 0) return;
64
+ count -= 1;
65
+ if (count === 0) restoreLastLock();
66
+ }
67
+
68
+ // src/scroll-lock/use-scroll-lock.ts
69
+ function useScrollLock(active = true) {
70
+ useEffect(() => {
71
+ if (!active) return;
72
+ return lockScroll();
73
+ }, [active]);
74
+ }
75
+
76
+ export {
77
+ lockScroll,
78
+ unlockScroll,
79
+ useScrollLock
80
+ };
@@ -0,0 +1,82 @@
1
+ "use client";
2
+
3
+ // src/focus-trap/focusables.ts
4
+ var FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), input:not([disabled]):not([type="hidden"]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])';
5
+ function focusableWithin(container) {
6
+ return Array.from(
7
+ container.querySelectorAll(FOCUSABLE_SELECTOR)
8
+ ).filter((el) => !el.closest("[hidden], [inert]"));
9
+ }
10
+ function nextFocusTarget(items, current, shiftKey, container) {
11
+ const first = items[0];
12
+ const last = items[items.length - 1];
13
+ if (!first || !last) return null;
14
+ if (!current || !container.contains(current)) return shiftKey ? last : first;
15
+ if (shiftKey && (current === first || current === container)) return last;
16
+ if (!shiftKey && current === last) return first;
17
+ return null;
18
+ }
19
+
20
+ // src/focus-trap/use-focus-trap.ts
21
+ import { useEffect, useRef } from "react";
22
+ var activeTraps = [];
23
+ function useFocusTrap(containerRef, {
24
+ active,
25
+ initialFocusRef,
26
+ returnFocusRef,
27
+ restoreFocus = true
28
+ }) {
29
+ const openerRef = useRef(null);
30
+ if (active && !openerRef.current && typeof document !== "undefined") {
31
+ openerRef.current = document.activeElement;
32
+ }
33
+ useEffect(() => {
34
+ if (!active) openerRef.current = null;
35
+ }, [active]);
36
+ useEffect(() => {
37
+ const container = containerRef.current;
38
+ if (!active || !container) return;
39
+ const token = {};
40
+ activeTraps.push(token);
41
+ if (!container.contains(document.activeElement)) {
42
+ const target = initialFocusRef?.current ?? focusableWithin(container)[0] ?? container;
43
+ if (target === container && !container.hasAttribute("tabindex")) {
44
+ container.setAttribute("tabindex", "-1");
45
+ }
46
+ target.focus();
47
+ }
48
+ function onKeyDown(event) {
49
+ if (event.key !== "Tab" || !container) return;
50
+ if (activeTraps[activeTraps.length - 1] !== token) return;
51
+ const items = focusableWithin(container);
52
+ if (items.length === 0) {
53
+ event.preventDefault();
54
+ return;
55
+ }
56
+ const target = nextFocusTarget(
57
+ items,
58
+ document.activeElement,
59
+ event.shiftKey,
60
+ container
61
+ );
62
+ if (target) {
63
+ event.preventDefault();
64
+ target.focus();
65
+ }
66
+ }
67
+ document.addEventListener("keydown", onKeyDown);
68
+ return () => {
69
+ document.removeEventListener("keydown", onKeyDown);
70
+ activeTraps.splice(activeTraps.indexOf(token), 1);
71
+ const opener = returnFocusRef?.current ?? openerRef.current;
72
+ if (restoreFocus && opener?.isConnected) opener.focus();
73
+ };
74
+ }, [active, containerRef, initialFocusRef, returnFocusRef, restoreFocus]);
75
+ }
76
+
77
+ export {
78
+ FOCUSABLE_SELECTOR,
79
+ focusableWithin,
80
+ nextFocusTarget,
81
+ useFocusTrap
82
+ };
@@ -0,0 +1,69 @@
1
+ "use client";
2
+
3
+ // src/debounce/debouncer.ts
4
+ function createDebouncer(getFn, getMs) {
5
+ let timer;
6
+ let pending;
7
+ function clear() {
8
+ if (timer !== void 0) clearTimeout(timer);
9
+ timer = void 0;
10
+ pending = void 0;
11
+ }
12
+ function run() {
13
+ const args = pending;
14
+ clear();
15
+ if (args) getFn()(...args);
16
+ }
17
+ return {
18
+ call(...args) {
19
+ if (timer !== void 0) clearTimeout(timer);
20
+ pending = args;
21
+ timer = setTimeout(run, getMs());
22
+ },
23
+ cancel: clear,
24
+ flush() {
25
+ if (pending) run();
26
+ },
27
+ isPending: () => pending !== void 0
28
+ };
29
+ }
30
+
31
+ // src/debounce/use-debounced-value.ts
32
+ import { useEffect, useState } from "react";
33
+ function useDebouncedValue(value, ms) {
34
+ const [debounced, setDebounced] = useState(value);
35
+ useEffect(() => {
36
+ const timer = setTimeout(() => setDebounced(value), ms);
37
+ return () => clearTimeout(timer);
38
+ }, [value, ms]);
39
+ return debounced;
40
+ }
41
+
42
+ // src/debounce/use-debounced-callback.ts
43
+ import { useEffect as useEffect2, useRef, useState as useState2 } from "react";
44
+ function useDebouncedCallback(fn, ms) {
45
+ const fnRef = useRef(fn);
46
+ const msRef = useRef(ms);
47
+ useEffect2(() => {
48
+ fnRef.current = fn;
49
+ msRef.current = ms;
50
+ });
51
+ const [debounced] = useState2(() => {
52
+ const debouncer = createDebouncer(
53
+ () => fnRef.current,
54
+ () => msRef.current
55
+ );
56
+ return Object.assign((...args) => debouncer.call(...args), {
57
+ cancel: debouncer.cancel,
58
+ flush: debouncer.flush
59
+ });
60
+ });
61
+ useEffect2(() => () => debounced.cancel(), [debounced]);
62
+ return debounced;
63
+ }
64
+
65
+ export {
66
+ createDebouncer,
67
+ useDebouncedValue,
68
+ useDebouncedCallback
69
+ };
@@ -0,0 +1,22 @@
1
+ "use client";
2
+
3
+ // src/unsaved-guard/handle-before-unload.ts
4
+ function handleBeforeUnload(event) {
5
+ event.preventDefault();
6
+ event.returnValue = "";
7
+ }
8
+
9
+ // src/unsaved-guard/use-unsaved-changes-guard.ts
10
+ import { useEffect } from "react";
11
+ function useUnsavedChangesGuard(dirty) {
12
+ useEffect(() => {
13
+ if (!dirty) return;
14
+ window.addEventListener("beforeunload", handleBeforeUnload);
15
+ return () => window.removeEventListener("beforeunload", handleBeforeUnload);
16
+ }, [dirty]);
17
+ }
18
+
19
+ export {
20
+ handleBeforeUnload,
21
+ useUnsavedChangesGuard
22
+ };
@@ -0,0 +1,98 @@
1
+ "use client";
2
+
3
+ // src/hotkey/parse-hotkey.ts
4
+ var KEY_ALIASES = {
5
+ esc: "escape",
6
+ space: " ",
7
+ plus: "+",
8
+ up: "arrowup",
9
+ down: "arrowdown",
10
+ left: "arrowleft",
11
+ right: "arrowright"
12
+ };
13
+ function parseHotkey(combo) {
14
+ const parsed = { key: "", mod: false, ctrl: false, meta: false, alt: false, shift: false };
15
+ const parts = combo.trim().toLowerCase().split(/\+(?=.)/);
16
+ for (const part of parts) {
17
+ const token = part.trim();
18
+ if (token === "mod" || token === "ctrl" || token === "meta" || token === "alt" || token === "shift") {
19
+ parsed[token] = true;
20
+ } else {
21
+ parsed.key = KEY_ALIASES[token] ?? token;
22
+ }
23
+ }
24
+ if (!parsed.key) throw new Error(`Invalid hotkey "${combo}": no key`);
25
+ return parsed;
26
+ }
27
+
28
+ // src/hotkey/match-hotkey.ts
29
+ var CODE_FOR_SYMBOL = {
30
+ "/": "Slash",
31
+ ".": "Period",
32
+ ",": "Comma",
33
+ ";": "Semicolon",
34
+ "'": "Quote",
35
+ "[": "BracketLeft",
36
+ "]": "BracketRight",
37
+ "\\": "Backslash",
38
+ "-": "Minus",
39
+ "=": "Equal",
40
+ "`": "Backquote"
41
+ };
42
+ function physicalCode(key) {
43
+ if (/^[a-z]$/.test(key)) return `Key${key.toUpperCase()}`;
44
+ if (/^[0-9]$/.test(key)) return `Digit${key}`;
45
+ return CODE_FOR_SYMBOL[key] ?? null;
46
+ }
47
+ function keyMatches(event, parsed) {
48
+ if (event.key.toLowerCase() === parsed.key) return true;
49
+ return parsed.shift && physicalCode(parsed.key) === event.code;
50
+ }
51
+ function matchHotkey(event, combo, isApple) {
52
+ const parsed = typeof combo === "string" ? parseHotkey(combo) : combo;
53
+ const wantMeta = parsed.meta || parsed.mod && isApple;
54
+ const wantCtrl = parsed.ctrl || parsed.mod && !isApple;
55
+ return event.metaKey === wantMeta && event.ctrlKey === wantCtrl && event.altKey === parsed.alt && event.shiftKey === parsed.shift && keyMatches(event, parsed);
56
+ }
57
+ function detectApple(nav) {
58
+ if (!nav) return false;
59
+ const platform = nav.userAgentData?.platform || nav.platform || "";
60
+ return /mac|iphone|ipad|ipod/i.test(platform);
61
+ }
62
+ function isEditableTarget(target) {
63
+ const el = target;
64
+ if (!el || typeof el.tagName !== "string") return false;
65
+ const tag = el.tagName.toLowerCase();
66
+ return tag === "input" || tag === "textarea" || tag === "select" || el.isContentEditable === true;
67
+ }
68
+
69
+ // src/hotkey/use-hotkey.ts
70
+ import { useEffect, useRef } from "react";
71
+ function useHotkey(combo, onHotkey, options = {}) {
72
+ const { enabled = true, allowInInputs = false, preventDefault = true } = options;
73
+ const handlerRef = useRef(onHotkey);
74
+ useEffect(() => {
75
+ handlerRef.current = onHotkey;
76
+ });
77
+ useEffect(() => {
78
+ if (!enabled) return;
79
+ const parsed = parseHotkey(combo);
80
+ function onKeyDown(event) {
81
+ if (event.isComposing || event.keyCode === 229) return;
82
+ if (!allowInInputs && isEditableTarget(event.target)) return;
83
+ if (!matchHotkey(event, parsed, detectApple(navigator))) return;
84
+ if (preventDefault) event.preventDefault();
85
+ handlerRef.current(event);
86
+ }
87
+ document.addEventListener("keydown", onKeyDown);
88
+ return () => document.removeEventListener("keydown", onKeyDown);
89
+ }, [combo, enabled, allowInInputs, preventDefault]);
90
+ }
91
+
92
+ export {
93
+ parseHotkey,
94
+ matchHotkey,
95
+ detectApple,
96
+ isEditableTarget,
97
+ useHotkey
98
+ };
@@ -0,0 +1,115 @@
1
+ "use client";
2
+
3
+ // src/drawer-swipe/swipe-intent.ts
4
+ var BACK_ZONE_PX = 60;
5
+ var FORWARD_ZONE_PX = 30;
6
+ var OPEN_DISTANCE_PX = 80;
7
+ var CLOSE_DISTANCE_PX = 60;
8
+ var VERTICAL_CANCEL_PX = 40;
9
+ var MAX_SWIPE_DURATION_MS = 450;
10
+ var HORIZONTAL_DOMINANCE = 1.5;
11
+ function isInOpenZone(x, viewportWidth) {
12
+ return x > BACK_ZONE_PX && x < viewportWidth - FORWARD_ZONE_PX;
13
+ }
14
+ function resolveSwipeIntent(start, end, drawerOpen, viewportWidth) {
15
+ const dx = end.x - start.x;
16
+ const dy = end.y - start.y;
17
+ if (end.time - start.time > MAX_SWIPE_DURATION_MS) return null;
18
+ if (Math.abs(dx) < Math.abs(dy) * HORIZONTAL_DOMINANCE) return null;
19
+ if (drawerOpen) {
20
+ return dx <= -CLOSE_DISTANCE_PX ? "close" : null;
21
+ }
22
+ if (dx < OPEN_DISTANCE_PX) return null;
23
+ return isInOpenZone(start.x, viewportWidth) ? "open" : null;
24
+ }
25
+ function isInsideHorizontalScroller(target) {
26
+ let node = target instanceof Element ? target : null;
27
+ while (node && node !== document.body) {
28
+ const { overflowX } = window.getComputedStyle(node);
29
+ if ((overflowX === "auto" || overflowX === "scroll") && node.scrollWidth > node.clientWidth) {
30
+ return true;
31
+ }
32
+ node = node.parentElement;
33
+ }
34
+ return false;
35
+ }
36
+
37
+ // src/drawer-swipe/use-drawer-swipe.ts
38
+ import { useEffect, useRef } from "react";
39
+ function isWideViewport(maxViewportWidth) {
40
+ return window.matchMedia(`(min-width: ${maxViewportWidth}px)`).matches;
41
+ }
42
+ function useDrawerSwipe({
43
+ drawerOpen,
44
+ onOpen,
45
+ onClose,
46
+ enabled = true,
47
+ maxViewportWidth = 768
48
+ }) {
49
+ const start = useRef(null);
50
+ const handlers = useRef({ drawerOpen, onOpen, onClose, maxViewportWidth });
51
+ handlers.current = { drawerOpen, onOpen, onClose, maxViewportWidth };
52
+ useEffect(() => {
53
+ if (!enabled) return;
54
+ function handleStart(event) {
55
+ start.current = null;
56
+ if (event.touches.length !== 1) return;
57
+ if (isWideViewport(handlers.current.maxViewportWidth)) return;
58
+ if (!handlers.current.drawerOpen && isInsideHorizontalScroller(event.target)) {
59
+ return;
60
+ }
61
+ const touch = event.touches[0];
62
+ if (!touch) return;
63
+ start.current = { x: touch.clientX, y: touch.clientY, time: Date.now() };
64
+ }
65
+ function handleMove(event) {
66
+ const origin = start.current;
67
+ const touch = event.touches[0];
68
+ if (!origin || !touch) return;
69
+ if (Math.abs(touch.clientY - origin.y) > VERTICAL_CANCEL_PX) {
70
+ start.current = null;
71
+ }
72
+ }
73
+ function handleEnd(event) {
74
+ const origin = start.current;
75
+ start.current = null;
76
+ const touch = event.changedTouches[0];
77
+ if (!origin || !touch) return;
78
+ const { drawerOpen: drawerOpen2, onOpen: onOpen2, onClose: onClose2 } = handlers.current;
79
+ const intent = resolveSwipeIntent(
80
+ origin,
81
+ { x: touch.clientX, y: touch.clientY, time: Date.now() },
82
+ drawerOpen2,
83
+ window.innerWidth
84
+ );
85
+ if (intent === "open") onOpen2();
86
+ if (intent === "close") onClose2();
87
+ }
88
+ function handleCancel() {
89
+ start.current = null;
90
+ }
91
+ document.addEventListener("touchstart", handleStart, { passive: true });
92
+ document.addEventListener("touchmove", handleMove, { passive: true });
93
+ document.addEventListener("touchend", handleEnd, { passive: true });
94
+ document.addEventListener("touchcancel", handleCancel, { passive: true });
95
+ return () => {
96
+ document.removeEventListener("touchstart", handleStart);
97
+ document.removeEventListener("touchmove", handleMove);
98
+ document.removeEventListener("touchend", handleEnd);
99
+ document.removeEventListener("touchcancel", handleCancel);
100
+ };
101
+ }, [enabled]);
102
+ }
103
+
104
+ export {
105
+ BACK_ZONE_PX,
106
+ FORWARD_ZONE_PX,
107
+ OPEN_DISTANCE_PX,
108
+ CLOSE_DISTANCE_PX,
109
+ VERTICAL_CANCEL_PX,
110
+ MAX_SWIPE_DURATION_MS,
111
+ isInOpenZone,
112
+ resolveSwipeIntent,
113
+ isInsideHorizontalScroller,
114
+ useDrawerSwipe
115
+ };
@@ -0,0 +1,23 @@
1
+ "use client";
2
+
3
+ // src/escape/use-escape.ts
4
+ import { useEffect, useRef } from "react";
5
+ function useEscape(onEscape, active = true) {
6
+ const handlerRef = useRef(onEscape);
7
+ useEffect(() => {
8
+ handlerRef.current = onEscape;
9
+ });
10
+ useEffect(() => {
11
+ if (!active) return;
12
+ function onKeyDown(event) {
13
+ if (event.isComposing || event.keyCode === 229) return;
14
+ if (event.key === "Escape") handlerRef.current();
15
+ }
16
+ document.addEventListener("keydown", onKeyDown);
17
+ return () => document.removeEventListener("keydown", onKeyDown);
18
+ }, [active]);
19
+ }
20
+
21
+ export {
22
+ useEscape
23
+ };
@@ -0,0 +1,72 @@
1
+ "use client";
2
+
3
+ // src/visibility-polling/schedule.ts
4
+ function nextDelay(intervalMs, jitter, random) {
5
+ const fraction = Math.min(1, Math.max(0, jitter));
6
+ return intervalMs + random() * fraction * intervalMs;
7
+ }
8
+
9
+ // src/visibility-polling/use-visibility-polling.ts
10
+ import { useEffect, useRef } from "react";
11
+ var isHidden = () => document.visibilityState === "hidden";
12
+ function useVisibilityPolling(fn, intervalMs, { enabled = true, immediate = true, jitter = 0, runOnVisible = true } = {}) {
13
+ const fnRef = useRef(fn);
14
+ const optionsRef = useRef({ immediate, jitter, runOnVisible });
15
+ useEffect(() => {
16
+ fnRef.current = fn;
17
+ optionsRef.current = { immediate, jitter, runOnVisible };
18
+ });
19
+ const runningRef = useRef(false);
20
+ const scheduleRef = useRef(null);
21
+ useEffect(() => {
22
+ if (!enabled) return;
23
+ let active = true;
24
+ let timer = null;
25
+ function clearTimer() {
26
+ if (timer !== null) clearTimeout(timer);
27
+ timer = null;
28
+ }
29
+ function schedule() {
30
+ clearTimer();
31
+ if (!active || isHidden()) return;
32
+ timer = setTimeout(run, nextDelay(intervalMs, optionsRef.current.jitter, Math.random));
33
+ }
34
+ async function run() {
35
+ timer = null;
36
+ if (!active || runningRef.current || isHidden()) return;
37
+ runningRef.current = true;
38
+ try {
39
+ await fnRef.current();
40
+ } catch {
41
+ } finally {
42
+ runningRef.current = false;
43
+ }
44
+ scheduleRef.current?.();
45
+ }
46
+ function onVisibilityChange() {
47
+ if (isHidden()) {
48
+ clearTimer();
49
+ } else if (optionsRef.current.runOnVisible) {
50
+ clearTimer();
51
+ void run();
52
+ } else if (!runningRef.current) {
53
+ schedule();
54
+ }
55
+ }
56
+ scheduleRef.current = schedule;
57
+ if (optionsRef.current.immediate && !isHidden()) void run();
58
+ else schedule();
59
+ document.addEventListener("visibilitychange", onVisibilityChange);
60
+ return () => {
61
+ active = false;
62
+ scheduleRef.current = null;
63
+ clearTimer();
64
+ document.removeEventListener("visibilitychange", onVisibilityChange);
65
+ };
66
+ }, [enabled, intervalMs]);
67
+ }
68
+
69
+ export {
70
+ nextDelay,
71
+ useVisibilityPolling
72
+ };