@pelatform/ui.hook 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/README.md ADDED
@@ -0,0 +1,313 @@
1
+ # @pelatform/ui.hook
2
+
3
+ [![Version](https://img.shields.io/npm/v/@pelatform/ui.hook.svg)](https://www.npmjs.com/package/@pelatform/ui.hook)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A collection of production-ready React hooks for the Pelatform UI Library. This package provides 15 reusable hooks for analytics, responsive design, form handling, navigation, DOM management, and more.
7
+
8
+ ## Installation
9
+
10
+ ```bash
11
+ # Using bun
12
+ bun add @pelatform/ui.hook
13
+
14
+ # Using npm
15
+ npm install @pelatform/ui.hook
16
+ ```
17
+
18
+ ## Available Hooks
19
+
20
+ ### Analytics & Tracking
21
+
22
+ #### `useAnalytics`
23
+
24
+ Track CRUD operations and user interactions with Google Analytics integration.
25
+
26
+ ```typescript
27
+ import { useAnalytics } from "@pelatform/ui.hook";
28
+
29
+ const { trackCreate, trackUpdate, trackDelete } = useAnalytics();
30
+
31
+ trackCreate("workspace", "projects", "project", "proj-123");
32
+ trackUpdate("user", "profile", "settings", "user-456");
33
+ trackDelete("content", "posts", "post", "post-789", false);
34
+ ```
35
+
36
+ ### Responsive Design
37
+
38
+ #### `useMediaQuery`
39
+
40
+ Track CSS media query matches in real-time.
41
+
42
+ ```typescript
43
+ import { useMediaQuery } from "@pelatform/ui.hook";
44
+
45
+ const isDarkMode = useMediaQuery("(prefers-color-scheme: dark)");
46
+ const isLargeScreen = useMediaQuery("(min-width: 1024px)");
47
+ ```
48
+
49
+ #### `useIsMobile`
50
+
51
+ Detect if viewport is mobile-sized with customizable breakpoint.
52
+
53
+ ```typescript
54
+ import { useIsMobile } from "@pelatform/ui.hook";
55
+
56
+ const isMobile = useIsMobile(); // Default breakpoint: 1024px
57
+ const isSmallScreen = useIsMobile(768); // Custom breakpoint
58
+ ```
59
+
60
+ #### `useViewport`
61
+
62
+ Track viewport dimensions with real-time updates.
63
+
64
+ ```typescript
65
+ import { useViewport } from "@pelatform/ui.hook";
66
+
67
+ const [height, width] = useViewport();
68
+ ```
69
+
70
+ ### Form & Input Management
71
+
72
+ #### `useFileUpload`
73
+
74
+ Comprehensive file upload with drag & drop support, validation, and preview generation.
75
+
76
+ ```typescript
77
+ import { useFileUpload } from "@pelatform/ui.hook";
78
+
79
+ const [
80
+ { files, isDragging, errors },
81
+ { addFiles, removeFile, clearFiles, handleDrop, getInputProps },
82
+ ] = useFileUpload({
83
+ maxFiles: 5,
84
+ maxSize: 5242880, // 5MB
85
+ accept: "image/*",
86
+ multiple: true,
87
+ onFilesChange: (files) => console.log("Files changed:", files),
88
+ });
89
+ ```
90
+
91
+ #### `useSliderInput`
92
+
93
+ Manage dual slider input with synchronized number inputs and validation.
94
+
95
+ ```typescript
96
+ import { useSliderInput } from "@pelatform/ui.hook";
97
+
98
+ const { sliderValues, inputValues, handleSliderChange, handleInputChange } =
99
+ useSliderInput({
100
+ minValue: 0,
101
+ maxValue: 1000,
102
+ initialValue: [100, 500],
103
+ });
104
+ ```
105
+
106
+ #### `useCopyToClipboard`
107
+
108
+ Copy text to clipboard with automatic state management and callbacks.
109
+
110
+ ```typescript
111
+ import { useCopyToClipboard } from "@pelatform/ui.hook";
112
+
113
+ const { copied, copy } = useCopyToClipboard({
114
+ timeout: 2000,
115
+ onCopy: () => console.log("Copied!"),
116
+ });
117
+
118
+ <button onClick={() => copy("Text to copy")}>
119
+ {copied ? "Copied!" : "Copy"}
120
+ </button>;
121
+ ```
122
+
123
+ ### Navigation & Scrolling
124
+
125
+ #### `useMenu`
126
+
127
+ Comprehensive menu navigation state management with active detection and breadcrumb generation.
128
+
129
+ ```typescript
130
+ import { useMenu } from "@pelatform/ui.hook";
131
+ import { usePathname } from "next/navigation";
132
+
133
+ const pathname = usePathname();
134
+ const { isActive, hasActiveChild, getCurrentItem, getBreadcrumb } =
135
+ useMenu(pathname);
136
+
137
+ const breadcrumb = getBreadcrumb(menuItems);
138
+ const currentItem = getCurrentItem(menuItems);
139
+ ```
140
+
141
+ #### `useScrollPosition`
142
+
143
+ Track scroll position of window or specific elements.
144
+
145
+ ```typescript
146
+ import { useScrollPosition } from "@pelatform/ui.hook";
147
+
148
+ const scrollY = useScrollPosition();
149
+ const elementScroll = useScrollPosition({ targetRef: myRef });
150
+ ```
151
+
152
+ ### DOM Management
153
+
154
+ #### `useBodyClasses`
155
+
156
+ Dynamically add/remove CSS classes from the document body element.
157
+
158
+ ```typescript
159
+ import { useBodyClasses } from "@pelatform/ui.hook";
160
+
161
+ useBodyClasses("dark-theme overflow-hidden");
162
+ ```
163
+
164
+ #### `useMutationObserver`
165
+
166
+ Observe DOM mutations on a referenced element.
167
+
168
+ ```typescript
169
+ import { useMutationObserver } from "@pelatform/ui.hook";
170
+
171
+ const ref = useRef<HTMLDivElement>(null);
172
+
173
+ useMutationObserver(ref, (mutations) => {
174
+ mutations.forEach((mutation) => {
175
+ console.log("DOM changed:", mutation);
176
+ });
177
+ });
178
+ ```
179
+
180
+ ### Security
181
+
182
+ #### `useRecaptchaV2`
183
+
184
+ Integrate Google reCAPTCHA v2 with automatic script loading and widget management.
185
+
186
+ ```typescript
187
+ import { useRecaptchaV2 } from "@pelatform/ui.hook";
188
+
189
+ const { containerRef, getToken, resetCaptcha, initializeRecaptcha } =
190
+ useRecaptchaV2(siteKey);
191
+
192
+ useEffect(() => {
193
+ initializeRecaptcha();
194
+ }, []);
195
+
196
+ const handleSubmit = async () => {
197
+ const token = getToken();
198
+ // Send token to server for verification
199
+ };
200
+ ```
201
+
202
+ ### Utilities
203
+
204
+ #### `useMounted`
205
+
206
+ Detect when a component has mounted on the client (essential for SSR).
207
+
208
+ ```typescript
209
+ import { useMounted } from "@pelatform/ui.hook";
210
+
211
+ const mounted = useMounted();
212
+
213
+ if (!mounted) return null; // Prevent hydration mismatches
214
+ ```
215
+
216
+ #### `useHydrated`
217
+
218
+ SSR-safe hydration state detection using `useSyncExternalStore`.
219
+
220
+ ```typescript
221
+ import { useHydrated } from "@pelatform/ui.hook";
222
+
223
+ const hydrated = useHydrated();
224
+
225
+ if (!hydrated) return null; // Returns false on server, true after client hydration
226
+ ```
227
+
228
+ **Features:** Prevents hydration mismatches, minimal overhead, stable value between server and client renders
229
+
230
+ #### `useRemoveGAParams`
231
+
232
+ Automatically remove Google Analytics `_gl` parameter from URL after initialization.
233
+
234
+ ```typescript
235
+ import { useRemoveGAParams } from "@pelatform/ui.hook";
236
+
237
+ useRemoveGAParams(); // Cleans URL after GA processes linker attribution
238
+ ```
239
+
240
+ ## Hook Categories
241
+
242
+ ### By Use Case
243
+
244
+ - **Analytics & Tracking**: `useAnalytics`
245
+ - **Responsive Design**: `useMediaQuery`, `useIsMobile`, `useViewport`
246
+ - **Form Management**: `useFileUpload`, `useSliderInput`, `useCopyToClipboard`
247
+ - **Navigation**: `useMenu`, `useScrollPosition`
248
+ - **DOM Interaction**: `useMutationObserver`, `useBodyClasses`
249
+ - **Security**: `useRecaptchaV2`
250
+ - **SSR Safety**: `useMounted`, `useHydrated`, `useRemoveGAParams`
251
+
252
+ ### By Complexity
253
+
254
+ **Simple** (Stateless/Single-purpose):
255
+
256
+ - `useMounted`
257
+ - `useHydrated`
258
+ - `useBodyClasses`
259
+ - `useRemoveGAParams`
260
+ - `useMediaQuery`
261
+ - `useIsMobile`
262
+ - `useViewport`
263
+ - `useScrollPosition`
264
+
265
+ **Moderate** (State Management):
266
+
267
+ - `useCopyToClipboard`
268
+ - `useSliderInput`
269
+ - `useMenu`
270
+ - `useMutationObserver`
271
+
272
+ **Advanced** (Complex State/Side Effects):
273
+
274
+ - `useAnalytics`
275
+ - `useFileUpload`
276
+ - `useRecaptchaV2`
277
+
278
+ ## TypeScript Support
279
+
280
+ All hooks are written in TypeScript and include comprehensive type definitions. Import types as needed:
281
+
282
+ ```typescript
283
+ import type { FileUploadOptions, FileWithPreview } from "@pelatform/ui.hook";
284
+ import type { MenuItem, MenuConfig } from "@pelatform/ui.hook";
285
+ ```
286
+
287
+ ## Features
288
+
289
+ - Full TypeScript support with comprehensive types
290
+ - SSR-safe implementations for Next.js and other frameworks
291
+ - Automatic cleanup and memory management
292
+ - Performance optimized with passive listeners and memoization
293
+ - Zero side effects (tree-shakeable)
294
+ - Extensive validation and error handling
295
+ - Browser API compatibility checks
296
+
297
+ ## Dependencies
298
+
299
+ This package has zero runtime dependencies. It requires:
300
+
301
+ - React 19.2+ (peer dependency)
302
+ - Modern browser with Web APIs support
303
+
304
+ ## Links
305
+
306
+ - [npm Package](https://www.npmjs.com/package/@pelatform/ui.hook)
307
+ - [Contributing Guide](../../../CONTRIBUTING.md)
308
+ - [Code of Conduct](../../../CODE_OF_CONDUCT.md)
309
+ - [License](../../../LICENSE)
310
+
311
+ ## License
312
+
313
+ MIT © [Pelatform Inc.](../../../LICENSE)