@allem-sdk/hooks 0.1.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/LICENSE +21 -0
- package/README.md +77 -0
- package/dist/index.d.mts +31 -0
- package/dist/index.d.ts +31 -0
- package/dist/index.js +175 -0
- package/dist/index.mjs +142 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ahmed Allem
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/kingofmit/allem-sdk/main/.github/AllemSDK.png" alt="Allem SDK" />
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://github.com/kingofmit/allem-sdk/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue.svg" alt="License" /></a>
|
|
7
|
+
<img src="https://img.shields.io/badge/react-18+-61dafb" alt="React 18+" />
|
|
8
|
+
<img src="https://img.shields.io/badge/typescript-strict-blue" alt="TypeScript" />
|
|
9
|
+
<img src="https://img.shields.io/badge/SSR-safe-brightgreen" alt="SSR Safe" />
|
|
10
|
+
</p>
|
|
11
|
+
|
|
12
|
+
# @allem-sdk/hooks
|
|
13
|
+
|
|
14
|
+
Essential React hooks for modern applications. SSR-safe, zero dependencies, works with Next.js, Vite, and Remix.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
npm install @allem-sdk/hooks
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Hooks
|
|
23
|
+
|
|
24
|
+
| Hook | Description |
|
|
25
|
+
|------|-------------|
|
|
26
|
+
| `useDebounce` | Debounce a value by a given delay |
|
|
27
|
+
| `useLocalStorage` | Persist state in localStorage with SSR safety |
|
|
28
|
+
| `useMediaQuery` | Reactive CSS media query matching |
|
|
29
|
+
| `useClickOutside` | Detect clicks outside a ref element |
|
|
30
|
+
| `useToggle` | Boolean toggle state |
|
|
31
|
+
| `useCopyToClipboard` | Copy text to clipboard with status |
|
|
32
|
+
| `useIntersectionObserver` | Observe element visibility via IntersectionObserver |
|
|
33
|
+
| `useWindowSize` | Reactive window dimensions |
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
```tsx
|
|
38
|
+
import { useDebounce, useLocalStorage, useMediaQuery } from "@allem-sdk/hooks";
|
|
39
|
+
|
|
40
|
+
function Search() {
|
|
41
|
+
const [query, setQuery] = useLocalStorage("search", "");
|
|
42
|
+
const debouncedQuery = useDebounce(query, 300);
|
|
43
|
+
const isMobile = useMediaQuery("(max-width: 768px)");
|
|
44
|
+
|
|
45
|
+
return <input value={query} onChange={(e) => setQuery(e.target.value)} />;
|
|
46
|
+
}
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
import { useClickOutside, useToggle } from "@allem-sdk/hooks";
|
|
51
|
+
|
|
52
|
+
function Dropdown() {
|
|
53
|
+
const [isOpen, toggle] = useToggle(false);
|
|
54
|
+
const ref = useClickOutside<HTMLDivElement>(() => toggle(false));
|
|
55
|
+
|
|
56
|
+
return (
|
|
57
|
+
<div ref={ref}>
|
|
58
|
+
<button onClick={() => toggle()}>Menu</button>
|
|
59
|
+
{isOpen && <ul>...</ul>}
|
|
60
|
+
</div>
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Part of [Allem SDK](https://github.com/kingofmit/allem-sdk)
|
|
66
|
+
|
|
67
|
+
This package can be used standalone or as part of the full SDK. Install `allem-sdk` to get all packages in one install.
|
|
68
|
+
|
|
69
|
+
## Support
|
|
70
|
+
|
|
71
|
+
If you find Allem SDK useful, consider supporting its development:
|
|
72
|
+
|
|
73
|
+
<a href="https://buymeacoffee.com/kingofmit" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" height="50" /></a>
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
[MIT](https://github.com/kingofmit/allem-sdk/blob/main/LICENSE) - [Ahmed Allem](https://kingallem.com)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
declare function useDebounce<T>(value: T, delay?: number): T;
|
|
4
|
+
|
|
5
|
+
declare function useLocalStorage<T>(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void, () => void];
|
|
6
|
+
|
|
7
|
+
declare function useMediaQuery(query: string): boolean;
|
|
8
|
+
|
|
9
|
+
declare function useClickOutside<T extends HTMLElement>(ref: RefObject<T | null>, handler: (event: MouseEvent | TouchEvent) => void): void;
|
|
10
|
+
|
|
11
|
+
declare function useToggle(initialValue?: boolean): [boolean, () => void, (value: boolean) => void];
|
|
12
|
+
|
|
13
|
+
declare function useCopyToClipboard(): [
|
|
14
|
+
boolean,
|
|
15
|
+
(text: string) => Promise<void>
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
interface UseIntersectionObserverOptions {
|
|
19
|
+
threshold?: number | number[];
|
|
20
|
+
root?: Element | null;
|
|
21
|
+
rootMargin?: string;
|
|
22
|
+
}
|
|
23
|
+
declare function useIntersectionObserver<T extends HTMLElement>(ref: RefObject<T | null>, options?: UseIntersectionObserverOptions): boolean;
|
|
24
|
+
|
|
25
|
+
interface WindowSize {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
declare function useWindowSize(): WindowSize;
|
|
30
|
+
|
|
31
|
+
export { useClickOutside, useCopyToClipboard, useDebounce, useIntersectionObserver, useLocalStorage, useMediaQuery, useToggle, useWindowSize };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
declare function useDebounce<T>(value: T, delay?: number): T;
|
|
4
|
+
|
|
5
|
+
declare function useLocalStorage<T>(key: string, initialValue: T): [T, (value: T | ((prev: T) => T)) => void, () => void];
|
|
6
|
+
|
|
7
|
+
declare function useMediaQuery(query: string): boolean;
|
|
8
|
+
|
|
9
|
+
declare function useClickOutside<T extends HTMLElement>(ref: RefObject<T | null>, handler: (event: MouseEvent | TouchEvent) => void): void;
|
|
10
|
+
|
|
11
|
+
declare function useToggle(initialValue?: boolean): [boolean, () => void, (value: boolean) => void];
|
|
12
|
+
|
|
13
|
+
declare function useCopyToClipboard(): [
|
|
14
|
+
boolean,
|
|
15
|
+
(text: string) => Promise<void>
|
|
16
|
+
];
|
|
17
|
+
|
|
18
|
+
interface UseIntersectionObserverOptions {
|
|
19
|
+
threshold?: number | number[];
|
|
20
|
+
root?: Element | null;
|
|
21
|
+
rootMargin?: string;
|
|
22
|
+
}
|
|
23
|
+
declare function useIntersectionObserver<T extends HTMLElement>(ref: RefObject<T | null>, options?: UseIntersectionObserverOptions): boolean;
|
|
24
|
+
|
|
25
|
+
interface WindowSize {
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
}
|
|
29
|
+
declare function useWindowSize(): WindowSize;
|
|
30
|
+
|
|
31
|
+
export { useClickOutside, useCopyToClipboard, useDebounce, useIntersectionObserver, useLocalStorage, useMediaQuery, useToggle, useWindowSize };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
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/index.ts
|
|
22
|
+
var index_exports = {};
|
|
23
|
+
__export(index_exports, {
|
|
24
|
+
useClickOutside: () => useClickOutside,
|
|
25
|
+
useCopyToClipboard: () => useCopyToClipboard,
|
|
26
|
+
useDebounce: () => useDebounce,
|
|
27
|
+
useIntersectionObserver: () => useIntersectionObserver,
|
|
28
|
+
useLocalStorage: () => useLocalStorage,
|
|
29
|
+
useMediaQuery: () => useMediaQuery,
|
|
30
|
+
useToggle: () => useToggle,
|
|
31
|
+
useWindowSize: () => useWindowSize
|
|
32
|
+
});
|
|
33
|
+
module.exports = __toCommonJS(index_exports);
|
|
34
|
+
|
|
35
|
+
// src/hooks/useDebounce.ts
|
|
36
|
+
var import_react = require("react");
|
|
37
|
+
function useDebounce(value, delay = 300) {
|
|
38
|
+
const [debouncedValue, setDebouncedValue] = (0, import_react.useState)(value);
|
|
39
|
+
(0, import_react.useEffect)(() => {
|
|
40
|
+
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
|
41
|
+
return () => clearTimeout(timer);
|
|
42
|
+
}, [value, delay]);
|
|
43
|
+
return debouncedValue;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/hooks/useLocalStorage.ts
|
|
47
|
+
var import_react2 = require("react");
|
|
48
|
+
function useLocalStorage(key, initialValue) {
|
|
49
|
+
const [storedValue, setStoredValue] = (0, import_react2.useState)(() => {
|
|
50
|
+
if (typeof window === "undefined") return initialValue;
|
|
51
|
+
try {
|
|
52
|
+
const item = window.localStorage.getItem(key);
|
|
53
|
+
return item ? JSON.parse(item) : initialValue;
|
|
54
|
+
} catch {
|
|
55
|
+
return initialValue;
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const setValue = (0, import_react2.useCallback)(
|
|
59
|
+
(value) => {
|
|
60
|
+
setStoredValue((prev) => {
|
|
61
|
+
const next = value instanceof Function ? value(prev) : value;
|
|
62
|
+
if (typeof window !== "undefined") {
|
|
63
|
+
window.localStorage.setItem(key, JSON.stringify(next));
|
|
64
|
+
}
|
|
65
|
+
return next;
|
|
66
|
+
});
|
|
67
|
+
},
|
|
68
|
+
[key]
|
|
69
|
+
);
|
|
70
|
+
const removeValue = (0, import_react2.useCallback)(() => {
|
|
71
|
+
setStoredValue(initialValue);
|
|
72
|
+
if (typeof window !== "undefined") {
|
|
73
|
+
window.localStorage.removeItem(key);
|
|
74
|
+
}
|
|
75
|
+
}, [key, initialValue]);
|
|
76
|
+
return [storedValue, setValue, removeValue];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/hooks/useMediaQuery.ts
|
|
80
|
+
var import_react3 = require("react");
|
|
81
|
+
function useMediaQuery(query) {
|
|
82
|
+
const [matches, setMatches] = (0, import_react3.useState)(() => {
|
|
83
|
+
if (typeof window === "undefined") return false;
|
|
84
|
+
return window.matchMedia(query).matches;
|
|
85
|
+
});
|
|
86
|
+
(0, import_react3.useEffect)(() => {
|
|
87
|
+
if (typeof window === "undefined") return;
|
|
88
|
+
const mediaQuery = window.matchMedia(query);
|
|
89
|
+
const handler = (event) => setMatches(event.matches);
|
|
90
|
+
setMatches(mediaQuery.matches);
|
|
91
|
+
mediaQuery.addEventListener("change", handler);
|
|
92
|
+
return () => mediaQuery.removeEventListener("change", handler);
|
|
93
|
+
}, [query]);
|
|
94
|
+
return matches;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// src/hooks/useClickOutside.ts
|
|
98
|
+
var import_react4 = require("react");
|
|
99
|
+
function useClickOutside(ref, handler) {
|
|
100
|
+
(0, import_react4.useEffect)(() => {
|
|
101
|
+
const listener = (event) => {
|
|
102
|
+
if (!ref.current || ref.current.contains(event.target)) return;
|
|
103
|
+
handler(event);
|
|
104
|
+
};
|
|
105
|
+
document.addEventListener("mousedown", listener);
|
|
106
|
+
document.addEventListener("touchstart", listener);
|
|
107
|
+
return () => {
|
|
108
|
+
document.removeEventListener("mousedown", listener);
|
|
109
|
+
document.removeEventListener("touchstart", listener);
|
|
110
|
+
};
|
|
111
|
+
}, [ref, handler]);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// src/hooks/useToggle.ts
|
|
115
|
+
var import_react5 = require("react");
|
|
116
|
+
function useToggle(initialValue = false) {
|
|
117
|
+
const [value, setValue] = (0, import_react5.useState)(initialValue);
|
|
118
|
+
const toggle = (0, import_react5.useCallback)(() => setValue((prev) => !prev), []);
|
|
119
|
+
const set = (0, import_react5.useCallback)((v) => setValue(v), []);
|
|
120
|
+
return [value, toggle, set];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// src/hooks/useCopyToClipboard.ts
|
|
124
|
+
var import_react6 = require("react");
|
|
125
|
+
function useCopyToClipboard() {
|
|
126
|
+
const [copied, setCopied] = (0, import_react6.useState)(false);
|
|
127
|
+
const copy = (0, import_react6.useCallback)(async (text) => {
|
|
128
|
+
if (typeof navigator === "undefined") return;
|
|
129
|
+
await navigator.clipboard.writeText(text);
|
|
130
|
+
setCopied(true);
|
|
131
|
+
setTimeout(() => setCopied(false), 2e3);
|
|
132
|
+
}, []);
|
|
133
|
+
return [copied, copy];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// src/hooks/useIntersectionObserver.ts
|
|
137
|
+
var import_react7 = require("react");
|
|
138
|
+
function useIntersectionObserver(ref, options = {}) {
|
|
139
|
+
const [isIntersecting, setIsIntersecting] = (0, import_react7.useState)(false);
|
|
140
|
+
const { threshold = 0, root = null, rootMargin = "0px" } = options;
|
|
141
|
+
(0, import_react7.useEffect)(() => {
|
|
142
|
+
if (!ref.current || typeof IntersectionObserver === "undefined") return;
|
|
143
|
+
const observer = new IntersectionObserver(
|
|
144
|
+
([entry]) => setIsIntersecting(entry.isIntersecting),
|
|
145
|
+
{ threshold, root, rootMargin }
|
|
146
|
+
);
|
|
147
|
+
observer.observe(ref.current);
|
|
148
|
+
return () => observer.disconnect();
|
|
149
|
+
}, [ref, threshold, root, rootMargin]);
|
|
150
|
+
return isIntersecting;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// src/hooks/useWindowSize.ts
|
|
154
|
+
var import_react8 = require("react");
|
|
155
|
+
function useWindowSize() {
|
|
156
|
+
const [size, setSize] = (0, import_react8.useState)({ width: 0, height: 0 });
|
|
157
|
+
(0, import_react8.useEffect)(() => {
|
|
158
|
+
const handler = () => setSize({ width: window.innerWidth, height: window.innerHeight });
|
|
159
|
+
handler();
|
|
160
|
+
window.addEventListener("resize", handler);
|
|
161
|
+
return () => window.removeEventListener("resize", handler);
|
|
162
|
+
}, []);
|
|
163
|
+
return size;
|
|
164
|
+
}
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
useClickOutside,
|
|
168
|
+
useCopyToClipboard,
|
|
169
|
+
useDebounce,
|
|
170
|
+
useIntersectionObserver,
|
|
171
|
+
useLocalStorage,
|
|
172
|
+
useMediaQuery,
|
|
173
|
+
useToggle,
|
|
174
|
+
useWindowSize
|
|
175
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
// src/hooks/useDebounce.ts
|
|
4
|
+
import { useState, useEffect } from "react";
|
|
5
|
+
function useDebounce(value, delay = 300) {
|
|
6
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const timer = setTimeout(() => setDebouncedValue(value), delay);
|
|
9
|
+
return () => clearTimeout(timer);
|
|
10
|
+
}, [value, delay]);
|
|
11
|
+
return debouncedValue;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/hooks/useLocalStorage.ts
|
|
15
|
+
import { useState as useState2, useCallback } from "react";
|
|
16
|
+
function useLocalStorage(key, initialValue) {
|
|
17
|
+
const [storedValue, setStoredValue] = useState2(() => {
|
|
18
|
+
if (typeof window === "undefined") return initialValue;
|
|
19
|
+
try {
|
|
20
|
+
const item = window.localStorage.getItem(key);
|
|
21
|
+
return item ? JSON.parse(item) : initialValue;
|
|
22
|
+
} catch {
|
|
23
|
+
return initialValue;
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
const setValue = useCallback(
|
|
27
|
+
(value) => {
|
|
28
|
+
setStoredValue((prev) => {
|
|
29
|
+
const next = value instanceof Function ? value(prev) : value;
|
|
30
|
+
if (typeof window !== "undefined") {
|
|
31
|
+
window.localStorage.setItem(key, JSON.stringify(next));
|
|
32
|
+
}
|
|
33
|
+
return next;
|
|
34
|
+
});
|
|
35
|
+
},
|
|
36
|
+
[key]
|
|
37
|
+
);
|
|
38
|
+
const removeValue = useCallback(() => {
|
|
39
|
+
setStoredValue(initialValue);
|
|
40
|
+
if (typeof window !== "undefined") {
|
|
41
|
+
window.localStorage.removeItem(key);
|
|
42
|
+
}
|
|
43
|
+
}, [key, initialValue]);
|
|
44
|
+
return [storedValue, setValue, removeValue];
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/hooks/useMediaQuery.ts
|
|
48
|
+
import { useState as useState3, useEffect as useEffect2 } from "react";
|
|
49
|
+
function useMediaQuery(query) {
|
|
50
|
+
const [matches, setMatches] = useState3(() => {
|
|
51
|
+
if (typeof window === "undefined") return false;
|
|
52
|
+
return window.matchMedia(query).matches;
|
|
53
|
+
});
|
|
54
|
+
useEffect2(() => {
|
|
55
|
+
if (typeof window === "undefined") return;
|
|
56
|
+
const mediaQuery = window.matchMedia(query);
|
|
57
|
+
const handler = (event) => setMatches(event.matches);
|
|
58
|
+
setMatches(mediaQuery.matches);
|
|
59
|
+
mediaQuery.addEventListener("change", handler);
|
|
60
|
+
return () => mediaQuery.removeEventListener("change", handler);
|
|
61
|
+
}, [query]);
|
|
62
|
+
return matches;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// src/hooks/useClickOutside.ts
|
|
66
|
+
import { useEffect as useEffect3 } from "react";
|
|
67
|
+
function useClickOutside(ref, handler) {
|
|
68
|
+
useEffect3(() => {
|
|
69
|
+
const listener = (event) => {
|
|
70
|
+
if (!ref.current || ref.current.contains(event.target)) return;
|
|
71
|
+
handler(event);
|
|
72
|
+
};
|
|
73
|
+
document.addEventListener("mousedown", listener);
|
|
74
|
+
document.addEventListener("touchstart", listener);
|
|
75
|
+
return () => {
|
|
76
|
+
document.removeEventListener("mousedown", listener);
|
|
77
|
+
document.removeEventListener("touchstart", listener);
|
|
78
|
+
};
|
|
79
|
+
}, [ref, handler]);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// src/hooks/useToggle.ts
|
|
83
|
+
import { useState as useState4, useCallback as useCallback2 } from "react";
|
|
84
|
+
function useToggle(initialValue = false) {
|
|
85
|
+
const [value, setValue] = useState4(initialValue);
|
|
86
|
+
const toggle = useCallback2(() => setValue((prev) => !prev), []);
|
|
87
|
+
const set = useCallback2((v) => setValue(v), []);
|
|
88
|
+
return [value, toggle, set];
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/hooks/useCopyToClipboard.ts
|
|
92
|
+
import { useState as useState5, useCallback as useCallback3 } from "react";
|
|
93
|
+
function useCopyToClipboard() {
|
|
94
|
+
const [copied, setCopied] = useState5(false);
|
|
95
|
+
const copy = useCallback3(async (text) => {
|
|
96
|
+
if (typeof navigator === "undefined") return;
|
|
97
|
+
await navigator.clipboard.writeText(text);
|
|
98
|
+
setCopied(true);
|
|
99
|
+
setTimeout(() => setCopied(false), 2e3);
|
|
100
|
+
}, []);
|
|
101
|
+
return [copied, copy];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
// src/hooks/useIntersectionObserver.ts
|
|
105
|
+
import { useState as useState6, useEffect as useEffect4 } from "react";
|
|
106
|
+
function useIntersectionObserver(ref, options = {}) {
|
|
107
|
+
const [isIntersecting, setIsIntersecting] = useState6(false);
|
|
108
|
+
const { threshold = 0, root = null, rootMargin = "0px" } = options;
|
|
109
|
+
useEffect4(() => {
|
|
110
|
+
if (!ref.current || typeof IntersectionObserver === "undefined") return;
|
|
111
|
+
const observer = new IntersectionObserver(
|
|
112
|
+
([entry]) => setIsIntersecting(entry.isIntersecting),
|
|
113
|
+
{ threshold, root, rootMargin }
|
|
114
|
+
);
|
|
115
|
+
observer.observe(ref.current);
|
|
116
|
+
return () => observer.disconnect();
|
|
117
|
+
}, [ref, threshold, root, rootMargin]);
|
|
118
|
+
return isIntersecting;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/hooks/useWindowSize.ts
|
|
122
|
+
import { useState as useState7, useEffect as useEffect5 } from "react";
|
|
123
|
+
function useWindowSize() {
|
|
124
|
+
const [size, setSize] = useState7({ width: 0, height: 0 });
|
|
125
|
+
useEffect5(() => {
|
|
126
|
+
const handler = () => setSize({ width: window.innerWidth, height: window.innerHeight });
|
|
127
|
+
handler();
|
|
128
|
+
window.addEventListener("resize", handler);
|
|
129
|
+
return () => window.removeEventListener("resize", handler);
|
|
130
|
+
}, []);
|
|
131
|
+
return size;
|
|
132
|
+
}
|
|
133
|
+
export {
|
|
134
|
+
useClickOutside,
|
|
135
|
+
useCopyToClipboard,
|
|
136
|
+
useDebounce,
|
|
137
|
+
useIntersectionObserver,
|
|
138
|
+
useLocalStorage,
|
|
139
|
+
useMediaQuery,
|
|
140
|
+
useToggle,
|
|
141
|
+
useWindowSize
|
|
142
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@allem-sdk/hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A collection of essential React hooks for modern applications",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Ahmed Allem",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"import": "./dist/index.mjs",
|
|
14
|
+
"require": "./dist/index.js"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"sideEffects": false,
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@testing-library/react": "^16.0.0",
|
|
23
|
+
"@types/react": "^19.0.0",
|
|
24
|
+
"jsdom": "^25.0.0",
|
|
25
|
+
"react": "^19.0.0",
|
|
26
|
+
"react-dom": "^19.0.0",
|
|
27
|
+
"tsup": "^8.4.0",
|
|
28
|
+
"typescript": "^5.8.0",
|
|
29
|
+
"vitest": "^3.0.0"
|
|
30
|
+
},
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"react": ">=18.0.0"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/kingofmit/allem-sdk.git",
|
|
37
|
+
"directory": "packages/hooks"
|
|
38
|
+
},
|
|
39
|
+
"keywords": [
|
|
40
|
+
"allem-sdk",
|
|
41
|
+
"react",
|
|
42
|
+
"hooks",
|
|
43
|
+
"useDebounce",
|
|
44
|
+
"useMediaQuery",
|
|
45
|
+
"useLocalStorage"
|
|
46
|
+
],
|
|
47
|
+
"publishConfig": {
|
|
48
|
+
"access": "public"
|
|
49
|
+
},
|
|
50
|
+
"scripts": {
|
|
51
|
+
"build": "tsup",
|
|
52
|
+
"dev": "tsup --watch",
|
|
53
|
+
"clean": "rm -rf dist",
|
|
54
|
+
"test": "vitest run",
|
|
55
|
+
"test:watch": "vitest"
|
|
56
|
+
}
|
|
57
|
+
}
|