@usefy/usefy 0.0.16 โ 0.0.18
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 +66 -18
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@
|
|
|
47
47
|
- **๐ Zero Dependencies** โ Pure React implementation with no external dependencies
|
|
48
48
|
- **๐ฆ Tree Shakeable** โ Import only the hooks you need to optimize bundle size
|
|
49
49
|
- **๐ท TypeScript First** โ Complete type safety with full autocomplete support
|
|
50
|
-
- **โก SSR
|
|
50
|
+
- **โก SSR Compatible** โ Works seamlessly with Next.js, Remix, and other SSR frameworks
|
|
51
51
|
- **๐งช Well Tested** โ High test coverage ensures reliability and stability
|
|
52
52
|
- **๐ Well Documented** โ Detailed documentation with practical examples
|
|
53
53
|
|
|
@@ -112,6 +112,8 @@ All packages require React 18 or 19:
|
|
|
112
112
|
| [@usefy/use-session-storage](https://www.npmjs.com/package/@usefy/use-session-storage) | sessionStorage persistence for tab lifetime | [](https://www.npmjs.com/package/@usefy/use-session-storage) |  |
|
|
113
113
|
| [@usefy/use-click-any-where](https://www.npmjs.com/package/@usefy/use-click-any-where) | Document-wide click event detection | [](https://www.npmjs.com/package/@usefy/use-click-any-where) |  |
|
|
114
114
|
| [@usefy/use-copy-to-clipboard](https://www.npmjs.com/package/@usefy/use-copy-to-clipboard) | Clipboard copy with fallback support | [](https://www.npmjs.com/package/@usefy/use-copy-to-clipboard) |  |
|
|
115
|
+
| [@usefy/use-event-listener](https://www.npmjs.com/package/@usefy/use-event-listener) | DOM event listener with auto cleanup | [](https://www.npmjs.com/package/@usefy/use-event-listener) |  |
|
|
116
|
+
| [@usefy/use-on-click-outside](https://www.npmjs.com/package/@usefy/use-on-click-outside) | Outside click detection for modals/dropdowns | [](https://www.npmjs.com/package/@usefy/use-on-click-outside) |  |
|
|
115
117
|
|
|
116
118
|
---
|
|
117
119
|
|
|
@@ -126,6 +128,8 @@ import {
|
|
|
126
128
|
useDebounce,
|
|
127
129
|
useLocalStorage,
|
|
128
130
|
useCopyToClipboard,
|
|
131
|
+
useEventListener,
|
|
132
|
+
useOnClickOutside,
|
|
129
133
|
} from "@usefy/usefy";
|
|
130
134
|
|
|
131
135
|
function App() {
|
|
@@ -324,6 +328,65 @@ Data persists during tab lifetime, isolated per tab.
|
|
|
324
328
|
|
|
325
329
|
### ๐ฑ๏ธ Events
|
|
326
330
|
|
|
331
|
+
<details>
|
|
332
|
+
<summary><strong>useEventListener</strong> โ DOM event listener with auto cleanup</summary>
|
|
333
|
+
|
|
334
|
+
```tsx
|
|
335
|
+
// Window resize event (default target)
|
|
336
|
+
useEventListener("resize", (e) => {
|
|
337
|
+
console.log("Window resized:", window.innerWidth);
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
// Document keydown event
|
|
341
|
+
useEventListener(
|
|
342
|
+
"keydown",
|
|
343
|
+
(e) => {
|
|
344
|
+
if (e.key === "Escape") closeModal();
|
|
345
|
+
},
|
|
346
|
+
document
|
|
347
|
+
);
|
|
348
|
+
|
|
349
|
+
// Element with ref
|
|
350
|
+
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
351
|
+
useEventListener("click", handleClick, buttonRef);
|
|
352
|
+
|
|
353
|
+
// With options
|
|
354
|
+
useEventListener("scroll", handleScroll, window, {
|
|
355
|
+
passive: true,
|
|
356
|
+
capture: false,
|
|
357
|
+
enabled: isTracking,
|
|
358
|
+
});
|
|
359
|
+
```
|
|
360
|
+
|
|
361
|
+
Supports window, document, HTMLElement, and RefObject targets with full TypeScript type inference.
|
|
362
|
+
|
|
363
|
+
</details>
|
|
364
|
+
|
|
365
|
+
<details>
|
|
366
|
+
<summary><strong>useOnClickOutside</strong> โ Outside click detection</summary>
|
|
367
|
+
|
|
368
|
+
```tsx
|
|
369
|
+
// Basic usage - close modal on outside click
|
|
370
|
+
const modalRef = useRef<HTMLDivElement>(null);
|
|
371
|
+
useOnClickOutside(modalRef, () => onClose(), { enabled: isOpen });
|
|
372
|
+
|
|
373
|
+
// Multiple refs - button and dropdown menu
|
|
374
|
+
const buttonRef = useRef<HTMLButtonElement>(null);
|
|
375
|
+
const menuRef = useRef<HTMLDivElement>(null);
|
|
376
|
+
useOnClickOutside([buttonRef, menuRef], () => setIsOpen(false), {
|
|
377
|
+
enabled: isOpen,
|
|
378
|
+
});
|
|
379
|
+
|
|
380
|
+
// With exclude refs
|
|
381
|
+
useOnClickOutside(modalRef, onClose, {
|
|
382
|
+
excludeRefs: [toastRef], // Clicks on toast won't close modal
|
|
383
|
+
});
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
Perfect for modals, dropdowns, popovers, tooltips, and context menus with mouse + touch support.
|
|
387
|
+
|
|
388
|
+
</details>
|
|
389
|
+
|
|
327
390
|
<details>
|
|
328
391
|
<summary><strong>useClickAnyWhere</strong> โ Global click detection</summary>
|
|
329
392
|
|
|
@@ -377,6 +440,8 @@ All packages are comprehensively tested using Vitest to ensure reliability and s
|
|
|
377
440
|
| use-click-any-where | 92% | 88% | 100% | 92% |
|
|
378
441
|
| use-debounce | 91% | 90% | 67% | 93% |
|
|
379
442
|
| use-copy-to-clipboard | 88% | 79% | 86% | 88% |
|
|
443
|
+
| use-event-listener | 96% | 91% | 100% | 96% |
|
|
444
|
+
| use-on-click-outside | 97% | 93% | 100% | 97% |
|
|
380
445
|
|
|
381
446
|
---
|
|
382
447
|
|
|
@@ -392,23 +457,6 @@ All packages are comprehensively tested using Vitest to ensure reliability and s
|
|
|
392
457
|
|
|
393
458
|
---
|
|
394
459
|
|
|
395
|
-
## SSR Compatibility
|
|
396
|
-
|
|
397
|
-
All hooks are SSR-safe and will not throw errors on the server.
|
|
398
|
-
|
|
399
|
-
| Hook | SSR Behavior |
|
|
400
|
-
| -------------------------------------------- | ------------------------------------------------ |
|
|
401
|
-
| `useToggle`, `useCounter` | โ
Fully compatible (pure React state) |
|
|
402
|
-
| `useDebounce`, `useThrottle` | โ
Fully compatible (uses `setTimeout`) |
|
|
403
|
-
| `useDebounceCallback`, `useThrottleCallback` | โ
Fully compatible |
|
|
404
|
-
| `useLocalStorage`, `useSessionStorage` | โ
Safe โ returns `initialValue` on server |
|
|
405
|
-
| `useClickAnyWhere` | โ
Safe โ event listener only attached on client |
|
|
406
|
-
| `useCopyToClipboard` | โ
Safe โ copy function only works on client |
|
|
407
|
-
|
|
408
|
-
> **Note**: `useLocalStorage` and `useSessionStorage` include proper `typeof window` checks and will gracefully fall back to `initialValue` during server-side rendering.
|
|
409
|
-
|
|
410
|
-
---
|
|
411
|
-
|
|
412
460
|
## Related Links
|
|
413
461
|
|
|
414
462
|
- ๐ฆ [npm Organization](https://www.npmjs.com/org/usefy)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usefy/usefy",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.18",
|
|
4
4
|
"description": "A collection of useful React hooks",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -17,18 +17,18 @@
|
|
|
17
17
|
],
|
|
18
18
|
"sideEffects": false,
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@usefy/use-click-any-where": "0.0.
|
|
21
|
-
"@usefy/use-copy-to-clipboard": "0.0.
|
|
22
|
-
"@usefy/use-counter": "0.0.
|
|
23
|
-
"@usefy/use-
|
|
24
|
-
"@usefy/use-
|
|
25
|
-
"@usefy/use-debounce-callback": "0.0.
|
|
26
|
-
"@usefy/use-throttle
|
|
27
|
-
"@usefy/use-throttle": "0.0.
|
|
28
|
-
"@usefy/use-local-storage": "0.0.
|
|
29
|
-
"@usefy/use-session-storage": "0.0.
|
|
30
|
-
"@usefy/use-on-click-outside": "0.0.
|
|
31
|
-
"@usefy/use-event-listener": "0.0.
|
|
20
|
+
"@usefy/use-click-any-where": "0.0.18",
|
|
21
|
+
"@usefy/use-copy-to-clipboard": "0.0.18",
|
|
22
|
+
"@usefy/use-counter": "0.0.18",
|
|
23
|
+
"@usefy/use-toggle": "0.0.18",
|
|
24
|
+
"@usefy/use-debounce": "0.0.18",
|
|
25
|
+
"@usefy/use-debounce-callback": "0.0.18",
|
|
26
|
+
"@usefy/use-throttle": "0.0.18",
|
|
27
|
+
"@usefy/use-throttle-callback": "0.0.18",
|
|
28
|
+
"@usefy/use-local-storage": "0.0.18",
|
|
29
|
+
"@usefy/use-session-storage": "0.0.18",
|
|
30
|
+
"@usefy/use-on-click-outside": "0.0.18",
|
|
31
|
+
"@usefy/use-event-listener": "0.0.18"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
34
|
"react": "^18.0.0 || ^19.0.0"
|