@develemit/web-hooks 0.1.0 → 0.3.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 (101) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/README.md +311 -0
  3. package/dist/auto-grow.cjs +60 -0
  4. package/dist/auto-grow.d.cts +14 -0
  5. package/dist/auto-grow.d.ts +14 -0
  6. package/dist/auto-grow.js +9 -0
  7. package/dist/chunk-4PVVTQ5V.js +74 -0
  8. package/dist/chunk-5CA5RIMH.js +34 -0
  9. package/dist/chunk-5KIH2XLV.js +136 -0
  10. package/dist/chunk-5XRVPZHD.js +125 -0
  11. package/dist/chunk-6KCRVSLS.js +80 -0
  12. package/dist/chunk-DNU57XEJ.js +82 -0
  13. package/dist/chunk-DQBKU7HI.js +46 -0
  14. package/dist/chunk-EXEJI4FK.js +43 -0
  15. package/dist/chunk-FE3BQD2I.js +44 -0
  16. package/dist/chunk-HTVNN2NM.js +29 -0
  17. package/dist/chunk-IWQDAJEP.js +69 -0
  18. package/dist/chunk-JG3GPTMY.js +101 -0
  19. package/dist/chunk-LZKAGDER.js +22 -0
  20. package/dist/chunk-NKLEB5TR.js +98 -0
  21. package/dist/chunk-S4YKMGQE.js +23 -0
  22. package/dist/chunk-TNEJ34T5.js +86 -0
  23. package/dist/chunk-UJ366T3Y.js +146 -0
  24. package/dist/chunk-UPVNSGIK.js +72 -0
  25. package/dist/chunk-WVM2LTWQ.js +42 -0
  26. package/dist/chunk-WXL22EWK.js +21 -0
  27. package/dist/chunk-YFMMSLMB.js +41 -0
  28. package/dist/chunk-Z3X27PCA.js +108 -0
  29. package/dist/clipboard.cjs +18 -0
  30. package/dist/clipboard.d.cts +10 -1
  31. package/dist/clipboard.d.ts +10 -1
  32. package/dist/clipboard.js +8 -57
  33. package/dist/debounce.cjs +96 -0
  34. package/dist/debounce.d.cts +21 -0
  35. package/dist/debounce.d.ts +21 -0
  36. package/dist/debounce.js +11 -0
  37. package/dist/drawer-swipe.cjs +16 -4
  38. package/dist/drawer-swipe.d.cts +7 -3
  39. package/dist/drawer-swipe.d.ts +7 -3
  40. package/dist/drawer-swipe.js +16 -101
  41. package/dist/escape.cjs +1 -0
  42. package/dist/escape.js +3 -17
  43. package/dist/focus-trap.cjs +3 -1
  44. package/dist/focus-trap.js +6 -72
  45. package/dist/hotkey.cjs +127 -0
  46. package/dist/hotkey.d.cts +31 -0
  47. package/dist/hotkey.d.ts +31 -0
  48. package/dist/hotkey.js +15 -0
  49. package/dist/index.cjs +1207 -7
  50. package/dist/index.d.cts +20 -60
  51. package/dist/index.d.ts +20 -60
  52. package/dist/index.js +167 -247
  53. package/dist/keyboard-inset.cjs +113 -0
  54. package/dist/keyboard-inset.d.cts +25 -0
  55. package/dist/keyboard-inset.d.ts +25 -0
  56. package/dist/keyboard-inset.js +11 -0
  57. package/dist/media-query.cjs +90 -0
  58. package/dist/media-query.d.cts +12 -0
  59. package/dist/media-query.d.ts +12 -0
  60. package/dist/media-query.js +17 -0
  61. package/dist/online-status.cjs +46 -0
  62. package/dist/online-status.d.cts +3 -0
  63. package/dist/online-status.d.ts +3 -0
  64. package/dist/online-status.js +7 -0
  65. package/dist/outside-press.cjs +73 -0
  66. package/dist/outside-press.d.cts +17 -0
  67. package/dist/outside-press.d.ts +17 -0
  68. package/dist/outside-press.js +11 -0
  69. package/dist/push.cjs +201 -0
  70. package/dist/push.d.cts +37 -0
  71. package/dist/push.d.ts +37 -0
  72. package/dist/push.js +14 -0
  73. package/dist/pwa.cjs +198 -0
  74. package/dist/pwa.d.cts +35 -0
  75. package/dist/pwa.d.ts +35 -0
  76. package/dist/pwa.js +22 -0
  77. package/dist/scroll-lock.cjs +107 -0
  78. package/dist/scroll-lock.d.cts +8 -0
  79. package/dist/scroll-lock.d.ts +8 -0
  80. package/dist/scroll-lock.js +11 -0
  81. package/dist/service-worker.cjs +128 -0
  82. package/dist/service-worker.d.cts +23 -0
  83. package/dist/service-worker.d.ts +23 -0
  84. package/dist/service-worker.js +11 -0
  85. package/dist/share.cjs +139 -0
  86. package/dist/share.d.cts +23 -0
  87. package/dist/share.d.ts +23 -0
  88. package/dist/share.js +16 -0
  89. package/dist/storage-state.cjs +161 -0
  90. package/dist/storage-state.d.cts +14 -0
  91. package/dist/storage-state.d.ts +14 -0
  92. package/dist/storage-state.js +7 -0
  93. package/dist/unsaved-guard.cjs +48 -0
  94. package/dist/unsaved-guard.d.cts +5 -0
  95. package/dist/unsaved-guard.d.ts +5 -0
  96. package/dist/unsaved-guard.js +9 -0
  97. package/dist/visibility-polling.cjs +98 -0
  98. package/dist/visibility-polling.d.cts +14 -0
  99. package/dist/visibility-polling.d.ts +14 -0
  100. package/dist/visibility-polling.js +9 -0
  101. package/package.json +85 -2
package/dist/escape.js CHANGED
@@ -1,21 +1,7 @@
1
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.key === "Escape") handlerRef.current();
14
- }
15
- document.addEventListener("keydown", onKeyDown);
16
- return () => document.removeEventListener("keydown", onKeyDown);
17
- }, [active]);
18
- }
2
+ import {
3
+ useEscape
4
+ } from "./chunk-S4YKMGQE.js";
19
5
  export {
20
6
  useEscape
21
7
  };
@@ -58,6 +58,9 @@ function useFocusTrap(containerRef, {
58
58
  if (active && !openerRef.current && typeof document !== "undefined") {
59
59
  openerRef.current = document.activeElement;
60
60
  }
61
+ (0, import_react.useEffect)(() => {
62
+ if (!active) openerRef.current = null;
63
+ }, [active]);
61
64
  (0, import_react.useEffect)(() => {
62
65
  const container = containerRef.current;
63
66
  if (!active || !container) return;
@@ -94,7 +97,6 @@ function useFocusTrap(containerRef, {
94
97
  document.removeEventListener("keydown", onKeyDown);
95
98
  activeTraps.splice(activeTraps.indexOf(token), 1);
96
99
  const opener = returnFocusRef?.current ?? openerRef.current;
97
- openerRef.current = null;
98
100
  if (restoreFocus && opener?.isConnected) opener.focus();
99
101
  };
100
102
  }, [active, containerRef, initialFocusRef, returnFocusRef, restoreFocus]);
@@ -1,76 +1,10 @@
1
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
- const container = containerRef.current;
35
- if (!active || !container) return;
36
- const token = {};
37
- activeTraps.push(token);
38
- if (!container.contains(document.activeElement)) {
39
- const target = initialFocusRef?.current ?? focusableWithin(container)[0] ?? container;
40
- if (target === container && !container.hasAttribute("tabindex")) {
41
- container.setAttribute("tabindex", "-1");
42
- }
43
- target.focus();
44
- }
45
- function onKeyDown(event) {
46
- if (event.key !== "Tab" || !container) return;
47
- if (activeTraps[activeTraps.length - 1] !== token) return;
48
- const items = focusableWithin(container);
49
- if (items.length === 0) {
50
- event.preventDefault();
51
- return;
52
- }
53
- const target = nextFocusTarget(
54
- items,
55
- document.activeElement,
56
- event.shiftKey,
57
- container
58
- );
59
- if (target) {
60
- event.preventDefault();
61
- target.focus();
62
- }
63
- }
64
- document.addEventListener("keydown", onKeyDown);
65
- return () => {
66
- document.removeEventListener("keydown", onKeyDown);
67
- activeTraps.splice(activeTraps.indexOf(token), 1);
68
- const opener = returnFocusRef?.current ?? openerRef.current;
69
- openerRef.current = null;
70
- if (restoreFocus && opener?.isConnected) opener.focus();
71
- };
72
- }, [active, containerRef, initialFocusRef, returnFocusRef, restoreFocus]);
73
- }
2
+ import {
3
+ FOCUSABLE_SELECTOR,
4
+ focusableWithin,
5
+ nextFocusTarget,
6
+ useFocusTrap
7
+ } from "./chunk-DNU57XEJ.js";
74
8
  export {
75
9
  FOCUSABLE_SELECTOR,
76
10
  focusableWithin,
@@ -0,0 +1,127 @@
1
+ "use client";
2
+ "use strict";
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
+
21
+ // src/hotkey/index.ts
22
+ var hotkey_exports = {};
23
+ __export(hotkey_exports, {
24
+ detectApple: () => detectApple,
25
+ isEditableTarget: () => isEditableTarget,
26
+ matchHotkey: () => matchHotkey,
27
+ parseHotkey: () => parseHotkey,
28
+ useHotkey: () => useHotkey
29
+ });
30
+ module.exports = __toCommonJS(hotkey_exports);
31
+
32
+ // src/hotkey/parse-hotkey.ts
33
+ var KEY_ALIASES = {
34
+ esc: "escape",
35
+ space: " ",
36
+ plus: "+",
37
+ up: "arrowup",
38
+ down: "arrowdown",
39
+ left: "arrowleft",
40
+ right: "arrowright"
41
+ };
42
+ function parseHotkey(combo) {
43
+ const parsed = { key: "", mod: false, ctrl: false, meta: false, alt: false, shift: false };
44
+ const parts = combo.trim().toLowerCase().split(/\+(?=.)/);
45
+ for (const part of parts) {
46
+ const token = part.trim();
47
+ if (token === "mod" || token === "ctrl" || token === "meta" || token === "alt" || token === "shift") {
48
+ parsed[token] = true;
49
+ } else {
50
+ parsed.key = KEY_ALIASES[token] ?? token;
51
+ }
52
+ }
53
+ if (!parsed.key) throw new Error(`Invalid hotkey "${combo}": no key`);
54
+ return parsed;
55
+ }
56
+
57
+ // src/hotkey/match-hotkey.ts
58
+ var CODE_FOR_SYMBOL = {
59
+ "/": "Slash",
60
+ ".": "Period",
61
+ ",": "Comma",
62
+ ";": "Semicolon",
63
+ "'": "Quote",
64
+ "[": "BracketLeft",
65
+ "]": "BracketRight",
66
+ "\\": "Backslash",
67
+ "-": "Minus",
68
+ "=": "Equal",
69
+ "`": "Backquote"
70
+ };
71
+ function physicalCode(key) {
72
+ if (/^[a-z]$/.test(key)) return `Key${key.toUpperCase()}`;
73
+ if (/^[0-9]$/.test(key)) return `Digit${key}`;
74
+ return CODE_FOR_SYMBOL[key] ?? null;
75
+ }
76
+ function keyMatches(event, parsed) {
77
+ if (event.key.toLowerCase() === parsed.key) return true;
78
+ return parsed.shift && physicalCode(parsed.key) === event.code;
79
+ }
80
+ function matchHotkey(event, combo, isApple) {
81
+ const parsed = typeof combo === "string" ? parseHotkey(combo) : combo;
82
+ const wantMeta = parsed.meta || parsed.mod && isApple;
83
+ const wantCtrl = parsed.ctrl || parsed.mod && !isApple;
84
+ return event.metaKey === wantMeta && event.ctrlKey === wantCtrl && event.altKey === parsed.alt && event.shiftKey === parsed.shift && keyMatches(event, parsed);
85
+ }
86
+ function detectApple(nav) {
87
+ if (!nav) return false;
88
+ const platform = nav.userAgentData?.platform || nav.platform || "";
89
+ return /mac|iphone|ipad|ipod/i.test(platform);
90
+ }
91
+ function isEditableTarget(target) {
92
+ const el = target;
93
+ if (!el || typeof el.tagName !== "string") return false;
94
+ const tag = el.tagName.toLowerCase();
95
+ return tag === "input" || tag === "textarea" || tag === "select" || el.isContentEditable === true;
96
+ }
97
+
98
+ // src/hotkey/use-hotkey.ts
99
+ var import_react = require("react");
100
+ function useHotkey(combo, onHotkey, options = {}) {
101
+ const { enabled = true, allowInInputs = false, preventDefault = true } = options;
102
+ const handlerRef = (0, import_react.useRef)(onHotkey);
103
+ (0, import_react.useEffect)(() => {
104
+ handlerRef.current = onHotkey;
105
+ });
106
+ (0, import_react.useEffect)(() => {
107
+ if (!enabled) return;
108
+ const parsed = parseHotkey(combo);
109
+ function onKeyDown(event) {
110
+ if (event.isComposing || event.keyCode === 229) return;
111
+ if (!allowInInputs && isEditableTarget(event.target)) return;
112
+ if (!matchHotkey(event, parsed, detectApple(navigator))) return;
113
+ if (preventDefault) event.preventDefault();
114
+ handlerRef.current(event);
115
+ }
116
+ document.addEventListener("keydown", onKeyDown);
117
+ return () => document.removeEventListener("keydown", onKeyDown);
118
+ }, [combo, enabled, allowInInputs, preventDefault]);
119
+ }
120
+ // Annotate the CommonJS export names for ESM import in node:
121
+ 0 && (module.exports = {
122
+ detectApple,
123
+ isEditableTarget,
124
+ matchHotkey,
125
+ parseHotkey,
126
+ useHotkey
127
+ });
@@ -0,0 +1,31 @@
1
+ interface ParsedHotkey {
2
+ key: string;
3
+ mod: boolean;
4
+ ctrl: boolean;
5
+ meta: boolean;
6
+ alt: boolean;
7
+ shift: boolean;
8
+ }
9
+ declare function parseHotkey(combo: string): ParsedHotkey;
10
+
11
+ type KeyEventLike = Pick<KeyboardEvent, 'key' | 'code' | 'ctrlKey' | 'metaKey' | 'altKey' | 'shiftKey'>;
12
+ /** `isApple` decides what `mod` means: Cmd on Apple devices, Ctrl elsewhere. */
13
+ declare function matchHotkey(event: KeyEventLike, combo: string | ParsedHotkey, isApple: boolean): boolean;
14
+ interface NavigatorLike {
15
+ platform?: string;
16
+ userAgentData?: {
17
+ platform?: string;
18
+ };
19
+ }
20
+ /** iPad in desktop mode reports MacIntel, so it counts as Apple. */
21
+ declare function detectApple(nav: NavigatorLike | undefined): boolean;
22
+ declare function isEditableTarget(target: EventTarget | null): boolean;
23
+
24
+ interface UseHotkeyOptions {
25
+ enabled?: boolean;
26
+ allowInInputs?: boolean;
27
+ preventDefault?: boolean;
28
+ }
29
+ declare function useHotkey(combo: string, onHotkey: (event: KeyboardEvent) => void, options?: UseHotkeyOptions): void;
30
+
31
+ export { type ParsedHotkey, type UseHotkeyOptions, detectApple, isEditableTarget, matchHotkey, parseHotkey, useHotkey };
@@ -0,0 +1,31 @@
1
+ interface ParsedHotkey {
2
+ key: string;
3
+ mod: boolean;
4
+ ctrl: boolean;
5
+ meta: boolean;
6
+ alt: boolean;
7
+ shift: boolean;
8
+ }
9
+ declare function parseHotkey(combo: string): ParsedHotkey;
10
+
11
+ type KeyEventLike = Pick<KeyboardEvent, 'key' | 'code' | 'ctrlKey' | 'metaKey' | 'altKey' | 'shiftKey'>;
12
+ /** `isApple` decides what `mod` means: Cmd on Apple devices, Ctrl elsewhere. */
13
+ declare function matchHotkey(event: KeyEventLike, combo: string | ParsedHotkey, isApple: boolean): boolean;
14
+ interface NavigatorLike {
15
+ platform?: string;
16
+ userAgentData?: {
17
+ platform?: string;
18
+ };
19
+ }
20
+ /** iPad in desktop mode reports MacIntel, so it counts as Apple. */
21
+ declare function detectApple(nav: NavigatorLike | undefined): boolean;
22
+ declare function isEditableTarget(target: EventTarget | null): boolean;
23
+
24
+ interface UseHotkeyOptions {
25
+ enabled?: boolean;
26
+ allowInInputs?: boolean;
27
+ preventDefault?: boolean;
28
+ }
29
+ declare function useHotkey(combo: string, onHotkey: (event: KeyboardEvent) => void, options?: UseHotkeyOptions): void;
30
+
31
+ export { type ParsedHotkey, type UseHotkeyOptions, detectApple, isEditableTarget, matchHotkey, parseHotkey, useHotkey };
package/dist/hotkey.js ADDED
@@ -0,0 +1,15 @@
1
+ "use client";
2
+ import {
3
+ detectApple,
4
+ isEditableTarget,
5
+ matchHotkey,
6
+ parseHotkey,
7
+ useHotkey
8
+ } from "./chunk-NKLEB5TR.js";
9
+ export {
10
+ detectApple,
11
+ isEditableTarget,
12
+ matchHotkey,
13
+ parseHotkey,
14
+ useHotkey
15
+ };