@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
@@ -0,0 +1,5 @@
1
+ declare function handleBeforeUnload(event: BeforeUnloadEvent): void;
2
+
3
+ declare function useUnsavedChangesGuard(dirty: boolean): void;
4
+
5
+ export { handleBeforeUnload, useUnsavedChangesGuard };
@@ -0,0 +1,9 @@
1
+ "use client";
2
+ import {
3
+ handleBeforeUnload,
4
+ useUnsavedChangesGuard
5
+ } from "./chunk-LZKAGDER.js";
6
+ export {
7
+ handleBeforeUnload,
8
+ useUnsavedChangesGuard
9
+ };
@@ -0,0 +1,98 @@
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/visibility-polling/index.ts
22
+ var visibility_polling_exports = {};
23
+ __export(visibility_polling_exports, {
24
+ nextDelay: () => nextDelay,
25
+ useVisibilityPolling: () => useVisibilityPolling
26
+ });
27
+ module.exports = __toCommonJS(visibility_polling_exports);
28
+
29
+ // src/visibility-polling/schedule.ts
30
+ function nextDelay(intervalMs, jitter, random) {
31
+ const fraction = Math.min(1, Math.max(0, jitter));
32
+ return intervalMs + random() * fraction * intervalMs;
33
+ }
34
+
35
+ // src/visibility-polling/use-visibility-polling.ts
36
+ var import_react = require("react");
37
+ var isHidden = () => document.visibilityState === "hidden";
38
+ function useVisibilityPolling(fn, intervalMs, { enabled = true, immediate = true, jitter = 0, runOnVisible = true } = {}) {
39
+ const fnRef = (0, import_react.useRef)(fn);
40
+ const optionsRef = (0, import_react.useRef)({ immediate, jitter, runOnVisible });
41
+ (0, import_react.useEffect)(() => {
42
+ fnRef.current = fn;
43
+ optionsRef.current = { immediate, jitter, runOnVisible };
44
+ });
45
+ const runningRef = (0, import_react.useRef)(false);
46
+ const scheduleRef = (0, import_react.useRef)(null);
47
+ (0, import_react.useEffect)(() => {
48
+ if (!enabled) return;
49
+ let active = true;
50
+ let timer = null;
51
+ function clearTimer() {
52
+ if (timer !== null) clearTimeout(timer);
53
+ timer = null;
54
+ }
55
+ function schedule() {
56
+ clearTimer();
57
+ if (!active || isHidden()) return;
58
+ timer = setTimeout(run, nextDelay(intervalMs, optionsRef.current.jitter, Math.random));
59
+ }
60
+ async function run() {
61
+ timer = null;
62
+ if (!active || runningRef.current || isHidden()) return;
63
+ runningRef.current = true;
64
+ try {
65
+ await fnRef.current();
66
+ } catch {
67
+ } finally {
68
+ runningRef.current = false;
69
+ }
70
+ scheduleRef.current?.();
71
+ }
72
+ function onVisibilityChange() {
73
+ if (isHidden()) {
74
+ clearTimer();
75
+ } else if (optionsRef.current.runOnVisible) {
76
+ clearTimer();
77
+ void run();
78
+ } else if (!runningRef.current) {
79
+ schedule();
80
+ }
81
+ }
82
+ scheduleRef.current = schedule;
83
+ if (optionsRef.current.immediate && !isHidden()) void run();
84
+ else schedule();
85
+ document.addEventListener("visibilitychange", onVisibilityChange);
86
+ return () => {
87
+ active = false;
88
+ scheduleRef.current = null;
89
+ clearTimer();
90
+ document.removeEventListener("visibilitychange", onVisibilityChange);
91
+ };
92
+ }, [enabled, intervalMs]);
93
+ }
94
+ // Annotate the CommonJS export names for ESM import in node:
95
+ 0 && (module.exports = {
96
+ nextDelay,
97
+ useVisibilityPolling
98
+ });
@@ -0,0 +1,14 @@
1
+ declare function nextDelay(intervalMs: number, jitter: number, random: () => number): number;
2
+
3
+ interface UseVisibilityPollingOptions {
4
+ enabled?: boolean;
5
+ /** Run once on mount (when visible) before the first interval. */
6
+ immediate?: boolean;
7
+ /** 0–1 fraction of the interval added at random to each delay. */
8
+ jitter?: number;
9
+ /** Run once as soon as the page becomes visible again. */
10
+ runOnVisible?: boolean;
11
+ }
12
+ declare function useVisibilityPolling(fn: () => unknown, intervalMs: number, { enabled, immediate, jitter, runOnVisible }?: UseVisibilityPollingOptions): void;
13
+
14
+ export { type UseVisibilityPollingOptions, nextDelay, useVisibilityPolling };
@@ -0,0 +1,14 @@
1
+ declare function nextDelay(intervalMs: number, jitter: number, random: () => number): number;
2
+
3
+ interface UseVisibilityPollingOptions {
4
+ enabled?: boolean;
5
+ /** Run once on mount (when visible) before the first interval. */
6
+ immediate?: boolean;
7
+ /** 0–1 fraction of the interval added at random to each delay. */
8
+ jitter?: number;
9
+ /** Run once as soon as the page becomes visible again. */
10
+ runOnVisible?: boolean;
11
+ }
12
+ declare function useVisibilityPolling(fn: () => unknown, intervalMs: number, { enabled, immediate, jitter, runOnVisible }?: UseVisibilityPollingOptions): void;
13
+
14
+ export { type UseVisibilityPollingOptions, nextDelay, useVisibilityPolling };
@@ -0,0 +1,9 @@
1
+ "use client";
2
+ import {
3
+ nextDelay,
4
+ useVisibilityPolling
5
+ } from "./chunk-UPVNSGIK.js";
6
+ export {
7
+ nextDelay,
8
+ useVisibilityPolling
9
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@develemit/web-hooks",
3
- "version": "0.1.0",
3
+ "version": "0.3.0",
4
4
  "description": "Shared React behavior hooks for develemit web apps: drawer swipe, focus trap, escape key and clipboard.",
5
5
  "license": "MIT",
6
6
  "author": "Emit Dutcher",
@@ -34,6 +34,81 @@
34
34
  "types": "./dist/clipboard.d.ts",
35
35
  "import": "./dist/clipboard.js",
36
36
  "require": "./dist/clipboard.cjs"
37
+ },
38
+ "./scroll-lock": {
39
+ "types": "./dist/scroll-lock.d.ts",
40
+ "import": "./dist/scroll-lock.js",
41
+ "require": "./dist/scroll-lock.cjs"
42
+ },
43
+ "./media-query": {
44
+ "types": "./dist/media-query.d.ts",
45
+ "import": "./dist/media-query.js",
46
+ "require": "./dist/media-query.cjs"
47
+ },
48
+ "./outside-press": {
49
+ "types": "./dist/outside-press.d.ts",
50
+ "import": "./dist/outside-press.js",
51
+ "require": "./dist/outside-press.cjs"
52
+ },
53
+ "./visibility-polling": {
54
+ "types": "./dist/visibility-polling.d.ts",
55
+ "import": "./dist/visibility-polling.js",
56
+ "require": "./dist/visibility-polling.cjs"
57
+ },
58
+ "./debounce": {
59
+ "types": "./dist/debounce.d.ts",
60
+ "import": "./dist/debounce.js",
61
+ "require": "./dist/debounce.cjs"
62
+ },
63
+ "./storage-state": {
64
+ "types": "./dist/storage-state.d.ts",
65
+ "import": "./dist/storage-state.js",
66
+ "require": "./dist/storage-state.cjs"
67
+ },
68
+ "./unsaved-guard": {
69
+ "types": "./dist/unsaved-guard.d.ts",
70
+ "import": "./dist/unsaved-guard.js",
71
+ "require": "./dist/unsaved-guard.cjs"
72
+ },
73
+ "./hotkey": {
74
+ "types": "./dist/hotkey.d.ts",
75
+ "import": "./dist/hotkey.js",
76
+ "require": "./dist/hotkey.cjs"
77
+ },
78
+ "./auto-grow": {
79
+ "types": "./dist/auto-grow.d.ts",
80
+ "import": "./dist/auto-grow.js",
81
+ "require": "./dist/auto-grow.cjs"
82
+ },
83
+ "./pwa": {
84
+ "types": "./dist/pwa.d.ts",
85
+ "import": "./dist/pwa.js",
86
+ "require": "./dist/pwa.cjs"
87
+ },
88
+ "./online-status": {
89
+ "types": "./dist/online-status.d.ts",
90
+ "import": "./dist/online-status.js",
91
+ "require": "./dist/online-status.cjs"
92
+ },
93
+ "./service-worker": {
94
+ "types": "./dist/service-worker.d.ts",
95
+ "import": "./dist/service-worker.js",
96
+ "require": "./dist/service-worker.cjs"
97
+ },
98
+ "./share": {
99
+ "types": "./dist/share.d.ts",
100
+ "import": "./dist/share.js",
101
+ "require": "./dist/share.cjs"
102
+ },
103
+ "./keyboard-inset": {
104
+ "types": "./dist/keyboard-inset.d.ts",
105
+ "import": "./dist/keyboard-inset.js",
106
+ "require": "./dist/keyboard-inset.cjs"
107
+ },
108
+ "./push": {
109
+ "types": "./dist/push.d.ts",
110
+ "import": "./dist/push.js",
111
+ "require": "./dist/push.cjs"
37
112
  }
38
113
  },
39
114
  "main": "./dist/index.cjs",
@@ -54,11 +129,17 @@
54
129
  "@eslint/js": "^9.0.0",
55
130
  "@testing-library/react": "^16.0.0",
56
131
  "@types/react": "^19.0.0",
132
+ "@types/react-18": "npm:@types/react@^18.3.31",
57
133
  "@types/react-dom": "^19.0.0",
134
+ "@vitest/browser": "3.2.7",
58
135
  "eslint": "^9.0.0",
59
136
  "jsdom": "^26.0.0",
137
+ "playwright": "1.64.0",
60
138
  "react": "^19.0.0",
139
+ "react-18": "npm:react@^18.3.1",
61
140
  "react-dom": "^19.0.0",
141
+ "react-dom-18": "npm:react-dom@^18.3.1",
142
+ "testing-library-react-18": "npm:@testing-library/react@16.2.0",
62
143
  "tsup": "^8.5.1",
63
144
  "typescript": "^5.9.0",
64
145
  "typescript-eslint": "^8.0.0",
@@ -71,6 +152,8 @@
71
152
  "build": "rm -rf dist && tsup",
72
153
  "check": "pnpm typecheck && pnpm lint && pnpm test && pnpm build",
73
154
  "release": "pnpm check && pnpm publish --access public",
74
- "release:dry": "pnpm publish --access public --dry-run"
155
+ "release:dry": "pnpm publish --access public --dry-run",
156
+ "test:unit": "vitest run --project jsdom-react19 --project jsdom-react18",
157
+ "test:browser": "vitest run --project browser-webkit"
75
158
  }
76
159
  }