@foundrykit/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/.turbo/turbo-build.log +18 -0
- package/.turbo/turbo-lint.log +5 -0
- package/.turbo/turbo-type-check.log +5 -0
- package/README.md +451 -0
- package/components.json +20 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.js +91 -0
- package/dist/index.js.map +1 -0
- package/eslint.config.js +4 -0
- package/package.json +44 -0
- package/src/hooks/use-click-outside.ts +25 -0
- package/src/hooks/use-debounced-value.ts +17 -0
- package/src/hooks/use-local-storage.ts +34 -0
- package/src/hooks/use-media-query.ts +17 -0
- package/src/index.ts +8 -0
- package/src/lib/utils.ts +4 -0
- package/tsconfig.json +11 -0
- package/tsconfig.lint.json +8 -0
- package/tsup.config.ts +11 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @foundrykit/hooks@0.1.0 build /Users/isaacsneddon-jenkins/Desktop/projects/umi/projects/foundrykit/packages/hooks
|
|
4
|
+
> tsup
|
|
5
|
+
|
|
6
|
+
[34mCLI[39m Building entry: src/index.ts
|
|
7
|
+
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
8
|
+
[34mCLI[39m tsup v8.5.0
|
|
9
|
+
[34mCLI[39m Using tsup config: /Users/isaacsneddon-jenkins/Desktop/projects/umi/projects/foundrykit/packages/hooks/tsup.config.ts
|
|
10
|
+
[34mCLI[39m Target: es2022
|
|
11
|
+
[34mCLI[39m Cleaning output folder
|
|
12
|
+
[34mESM[39m Build start
|
|
13
|
+
[32mESM[39m [1mdist/index.js [22m[32m2.49 KB[39m
|
|
14
|
+
[32mESM[39m [1mdist/index.js.map [22m[32m4.72 KB[39m
|
|
15
|
+
[32mESM[39m ⚡️ Build success in 136ms
|
|
16
|
+
DTS Build start
|
|
17
|
+
DTS ⚡️ Build success in 3352ms
|
|
18
|
+
DTS dist/index.d.ts 546.00 B
|
package/README.md
ADDED
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
# @foundrykit/hooks
|
|
2
|
+
|
|
3
|
+
A collection of custom React hooks for common UI patterns and functionality. Provides reusable logic for state management, user interactions, and component behavior.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Custom hooks** - Reusable logic for common UI patterns
|
|
8
|
+
- **TypeScript support** - Full type safety with comprehensive definitions
|
|
9
|
+
- **Performance optimized** - Efficient implementations with proper cleanup
|
|
10
|
+
- **Accessibility focused** - Hooks that support accessibility best practices
|
|
11
|
+
- **Framework agnostic** - Works with any React setup
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @foundrykit/hooks
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Available Hooks
|
|
20
|
+
|
|
21
|
+
### State Management
|
|
22
|
+
- **useLocalStorage** - Persist state in localStorage
|
|
23
|
+
- **useDebouncedValue** - Debounce values for performance
|
|
24
|
+
- **useMediaQuery** - React to media query changes
|
|
25
|
+
- **useClickOutside** - Detect clicks outside elements
|
|
26
|
+
|
|
27
|
+
### Form Hooks
|
|
28
|
+
- **useForm** - Form state management
|
|
29
|
+
- **useField** - Individual field management
|
|
30
|
+
- **useValidation** - Form validation logic
|
|
31
|
+
|
|
32
|
+
### UI Hooks
|
|
33
|
+
- **useDialog** - Dialog/modal state management
|
|
34
|
+
- **usePopover** - Popover positioning and state
|
|
35
|
+
- **useTooltip** - Tooltip positioning and visibility
|
|
36
|
+
|
|
37
|
+
### Utility Hooks
|
|
38
|
+
- **usePrevious** - Access previous value
|
|
39
|
+
- **useInterval** - Set up intervals with cleanup
|
|
40
|
+
- **useTimeout** - Set up timeouts with cleanup
|
|
41
|
+
|
|
42
|
+
## Usage
|
|
43
|
+
|
|
44
|
+
### State Management Hooks
|
|
45
|
+
|
|
46
|
+
#### useLocalStorage
|
|
47
|
+
|
|
48
|
+
Persist state in localStorage with automatic serialization:
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { useLocalStorage } from '@foundrykit/hooks'
|
|
52
|
+
|
|
53
|
+
function UserPreferences() {
|
|
54
|
+
const [theme, setTheme] = useLocalStorage('theme', 'light')
|
|
55
|
+
const [language, setLanguage] = useLocalStorage('language', 'en')
|
|
56
|
+
|
|
57
|
+
return (
|
|
58
|
+
<div>
|
|
59
|
+
<select value={theme} onChange={(e) => setTheme(e.target.value)}>
|
|
60
|
+
<option value="light">Light</option>
|
|
61
|
+
<option value="dark">Dark</option>
|
|
62
|
+
</select>
|
|
63
|
+
|
|
64
|
+
<select value={language} onChange={(e) => setLanguage(e.target.value)}>
|
|
65
|
+
<option value="en">English</option>
|
|
66
|
+
<option value="es">Spanish</option>
|
|
67
|
+
</select>
|
|
68
|
+
</div>
|
|
69
|
+
)
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
#### useDebouncedValue
|
|
74
|
+
|
|
75
|
+
Debounce values to improve performance:
|
|
76
|
+
|
|
77
|
+
```tsx
|
|
78
|
+
import { useDebouncedValue } from '@foundrykit/hooks'
|
|
79
|
+
|
|
80
|
+
function SearchComponent() {
|
|
81
|
+
const [query, setQuery] = useState('')
|
|
82
|
+
const debouncedQuery = useDebouncedValue(query, 300)
|
|
83
|
+
|
|
84
|
+
// This effect only runs when debouncedQuery changes
|
|
85
|
+
useEffect(() => {
|
|
86
|
+
if (debouncedQuery) {
|
|
87
|
+
searchAPI(debouncedQuery)
|
|
88
|
+
}
|
|
89
|
+
}, [debouncedQuery])
|
|
90
|
+
|
|
91
|
+
return (
|
|
92
|
+
<input
|
|
93
|
+
value={query}
|
|
94
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
95
|
+
placeholder="Search..."
|
|
96
|
+
/>
|
|
97
|
+
)
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
#### useMediaQuery
|
|
102
|
+
|
|
103
|
+
React to media query changes:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
import { useMediaQuery } from '@foundrykit/hooks'
|
|
107
|
+
|
|
108
|
+
function ResponsiveComponent() {
|
|
109
|
+
const isMobile = useMediaQuery('(max-width: 768px)')
|
|
110
|
+
const isTablet = useMediaQuery('(min-width: 769px) and (max-width: 1024px)')
|
|
111
|
+
const isDesktop = useMediaQuery('(min-width: 1025px)')
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div>
|
|
115
|
+
{isMobile && <MobileLayout />}
|
|
116
|
+
{isTablet && <TabletLayout />}
|
|
117
|
+
{isDesktop && <DesktopLayout />}
|
|
118
|
+
</div>
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### UI Interaction Hooks
|
|
124
|
+
|
|
125
|
+
#### useClickOutside
|
|
126
|
+
|
|
127
|
+
Detect clicks outside an element:
|
|
128
|
+
|
|
129
|
+
```tsx
|
|
130
|
+
import { useClickOutside } from '@foundrykit/hooks'
|
|
131
|
+
|
|
132
|
+
function Dropdown({ isOpen, onClose, children }) {
|
|
133
|
+
const ref = useRef(null)
|
|
134
|
+
|
|
135
|
+
useClickOutside(ref, () => {
|
|
136
|
+
if (isOpen) {
|
|
137
|
+
onClose()
|
|
138
|
+
}
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
if (!isOpen) return null
|
|
142
|
+
|
|
143
|
+
return (
|
|
144
|
+
<div ref={ref} className="dropdown">
|
|
145
|
+
{children}
|
|
146
|
+
</div>
|
|
147
|
+
)
|
|
148
|
+
}
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
#### useDialog
|
|
152
|
+
|
|
153
|
+
Manage dialog/modal state:
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
import { useDialog } from '@foundrykit/hooks'
|
|
157
|
+
|
|
158
|
+
function ModalExample() {
|
|
159
|
+
const { isOpen, open, close, toggle } = useDialog()
|
|
160
|
+
|
|
161
|
+
return (
|
|
162
|
+
<div>
|
|
163
|
+
<button onClick={open}>Open Modal</button>
|
|
164
|
+
|
|
165
|
+
{isOpen && (
|
|
166
|
+
<div className="modal-overlay" onClick={close}>
|
|
167
|
+
<div className="modal-content" onClick={(e) => e.stopPropagation()}>
|
|
168
|
+
<h2>Modal Content</h2>
|
|
169
|
+
<button onClick={close}>Close</button>
|
|
170
|
+
</div>
|
|
171
|
+
</div>
|
|
172
|
+
)}
|
|
173
|
+
</div>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Form Hooks
|
|
179
|
+
|
|
180
|
+
#### useForm
|
|
181
|
+
|
|
182
|
+
Complete form state management:
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
import { useForm } from '@foundrykit/hooks'
|
|
186
|
+
|
|
187
|
+
function ContactForm() {
|
|
188
|
+
const { values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting } = useForm({
|
|
189
|
+
initialValues: {
|
|
190
|
+
name: '',
|
|
191
|
+
email: '',
|
|
192
|
+
message: ''
|
|
193
|
+
},
|
|
194
|
+
validate: (values) => {
|
|
195
|
+
const errors = {}
|
|
196
|
+
if (!values.name) errors.name = 'Name is required'
|
|
197
|
+
if (!values.email) errors.email = 'Email is required'
|
|
198
|
+
if (!values.message) errors.message = 'Message is required'
|
|
199
|
+
return errors
|
|
200
|
+
},
|
|
201
|
+
onSubmit: async (values) => {
|
|
202
|
+
await submitForm(values)
|
|
203
|
+
}
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
return (
|
|
207
|
+
<form onSubmit={handleSubmit}>
|
|
208
|
+
<input
|
|
209
|
+
name="name"
|
|
210
|
+
value={values.name}
|
|
211
|
+
onChange={handleChange}
|
|
212
|
+
onBlur={handleBlur}
|
|
213
|
+
className={touched.name && errors.name ? 'error' : ''}
|
|
214
|
+
/>
|
|
215
|
+
{touched.name && errors.name && <span>{errors.name}</span>}
|
|
216
|
+
|
|
217
|
+
<input
|
|
218
|
+
name="email"
|
|
219
|
+
value={values.email}
|
|
220
|
+
onChange={handleChange}
|
|
221
|
+
onBlur={handleBlur}
|
|
222
|
+
className={touched.email && errors.email ? 'error' : ''}
|
|
223
|
+
/>
|
|
224
|
+
{touched.email && errors.email && <span>{errors.email}</span>}
|
|
225
|
+
|
|
226
|
+
<textarea
|
|
227
|
+
name="message"
|
|
228
|
+
value={values.message}
|
|
229
|
+
onChange={handleChange}
|
|
230
|
+
onBlur={handleBlur}
|
|
231
|
+
className={touched.message && errors.message ? 'error' : ''}
|
|
232
|
+
/>
|
|
233
|
+
{touched.message && errors.message && <span>{errors.message}</span>}
|
|
234
|
+
|
|
235
|
+
<button type="submit" disabled={isSubmitting}>
|
|
236
|
+
{isSubmitting ? 'Sending...' : 'Send Message'}
|
|
237
|
+
</button>
|
|
238
|
+
</form>
|
|
239
|
+
)
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Utility Hooks
|
|
244
|
+
|
|
245
|
+
#### usePrevious
|
|
246
|
+
|
|
247
|
+
Access the previous value:
|
|
248
|
+
|
|
249
|
+
```tsx
|
|
250
|
+
import { usePrevious } from '@foundrykit/hooks'
|
|
251
|
+
|
|
252
|
+
function Counter() {
|
|
253
|
+
const [count, setCount] = useState(0)
|
|
254
|
+
const previousCount = usePrevious(count)
|
|
255
|
+
|
|
256
|
+
return (
|
|
257
|
+
<div>
|
|
258
|
+
<p>Current: {count}</p>
|
|
259
|
+
<p>Previous: {previousCount}</p>
|
|
260
|
+
<button onClick={() => setCount(count + 1)}>Increment</button>
|
|
261
|
+
</div>
|
|
262
|
+
)
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
#### useInterval
|
|
267
|
+
|
|
268
|
+
Set up intervals with automatic cleanup:
|
|
269
|
+
|
|
270
|
+
```tsx
|
|
271
|
+
import { useInterval } from '@foundrykit/hooks'
|
|
272
|
+
|
|
273
|
+
function Timer() {
|
|
274
|
+
const [seconds, setSeconds] = useState(0)
|
|
275
|
+
const [isRunning, setIsRunning] = useState(false)
|
|
276
|
+
|
|
277
|
+
useInterval(
|
|
278
|
+
() => setSeconds(seconds + 1),
|
|
279
|
+
isRunning ? 1000 : null
|
|
280
|
+
)
|
|
281
|
+
|
|
282
|
+
return (
|
|
283
|
+
<div>
|
|
284
|
+
<p>Seconds: {seconds}</p>
|
|
285
|
+
<button onClick={() => setIsRunning(!isRunning)}>
|
|
286
|
+
{isRunning ? 'Pause' : 'Start'}
|
|
287
|
+
</button>
|
|
288
|
+
</div>
|
|
289
|
+
)
|
|
290
|
+
}
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Advanced Usage
|
|
294
|
+
|
|
295
|
+
### Custom Hook Composition
|
|
296
|
+
|
|
297
|
+
Combine multiple hooks for complex functionality:
|
|
298
|
+
|
|
299
|
+
```tsx
|
|
300
|
+
import { useLocalStorage, useDebouncedValue, useMediaQuery } from '@foundrykit/hooks'
|
|
301
|
+
|
|
302
|
+
function useResponsiveSearch() {
|
|
303
|
+
const [query, setQuery] = useLocalStorage('search-query', '')
|
|
304
|
+
const debouncedQuery = useDebouncedValue(query, 300)
|
|
305
|
+
const isMobile = useMediaQuery('(max-width: 768px)')
|
|
306
|
+
|
|
307
|
+
const searchResults = useMemo(() => {
|
|
308
|
+
if (!debouncedQuery) return []
|
|
309
|
+
return performSearch(debouncedQuery, { limit: isMobile ? 5 : 10 })
|
|
310
|
+
}, [debouncedQuery, isMobile])
|
|
311
|
+
|
|
312
|
+
return {
|
|
313
|
+
query,
|
|
314
|
+
setQuery,
|
|
315
|
+
debouncedQuery,
|
|
316
|
+
searchResults,
|
|
317
|
+
isMobile
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
### Hook with Dependencies
|
|
323
|
+
|
|
324
|
+
```tsx
|
|
325
|
+
import { useClickOutside } from '@foundrykit/hooks'
|
|
326
|
+
|
|
327
|
+
function useDropdown(initialState = false) {
|
|
328
|
+
const [isOpen, setIsOpen] = useState(initialState)
|
|
329
|
+
const ref = useRef(null)
|
|
330
|
+
|
|
331
|
+
useClickOutside(ref, () => setIsOpen(false))
|
|
332
|
+
|
|
333
|
+
const open = useCallback(() => setIsOpen(true), [])
|
|
334
|
+
const close = useCallback(() => setIsOpen(false), [])
|
|
335
|
+
const toggle = useCallback(() => setIsOpen(!isOpen), [isOpen])
|
|
336
|
+
|
|
337
|
+
return {
|
|
338
|
+
isOpen,
|
|
339
|
+
ref,
|
|
340
|
+
open,
|
|
341
|
+
close,
|
|
342
|
+
toggle
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
```
|
|
346
|
+
|
|
347
|
+
### Performance Optimization
|
|
348
|
+
|
|
349
|
+
```tsx
|
|
350
|
+
import { useDebouncedValue } from '@foundrykit/hooks'
|
|
351
|
+
|
|
352
|
+
function OptimizedSearch() {
|
|
353
|
+
const [query, setQuery] = useState('')
|
|
354
|
+
const debouncedQuery = useDebouncedValue(query, 500)
|
|
355
|
+
|
|
356
|
+
// Memoize expensive operation
|
|
357
|
+
const searchResults = useMemo(() => {
|
|
358
|
+
if (!debouncedQuery) return []
|
|
359
|
+
return expensiveSearch(debouncedQuery)
|
|
360
|
+
}, [debouncedQuery])
|
|
361
|
+
|
|
362
|
+
return (
|
|
363
|
+
<div>
|
|
364
|
+
<input
|
|
365
|
+
value={query}
|
|
366
|
+
onChange={(e) => setQuery(e.target.value)}
|
|
367
|
+
placeholder="Search..."
|
|
368
|
+
/>
|
|
369
|
+
<SearchResults results={searchResults} />
|
|
370
|
+
</div>
|
|
371
|
+
)
|
|
372
|
+
}
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
## TypeScript Support
|
|
376
|
+
|
|
377
|
+
All hooks include comprehensive TypeScript definitions:
|
|
378
|
+
|
|
379
|
+
```tsx
|
|
380
|
+
import { useLocalStorage, useDebouncedValue } from '@foundrykit/hooks'
|
|
381
|
+
|
|
382
|
+
// Type-safe localStorage hook
|
|
383
|
+
const [user, setUser] = useLocalStorage<User>('user', null)
|
|
384
|
+
|
|
385
|
+
// Type-safe debounced value
|
|
386
|
+
const [searchTerm, setSearchTerm] = useState<string>('')
|
|
387
|
+
const debouncedSearchTerm = useDebouncedValue<string>(searchTerm, 300)
|
|
388
|
+
```
|
|
389
|
+
|
|
390
|
+
## Best Practices
|
|
391
|
+
|
|
392
|
+
### Hook Dependencies
|
|
393
|
+
|
|
394
|
+
```tsx
|
|
395
|
+
// ✅ Good - Stable callback
|
|
396
|
+
const handleClick = useCallback(() => {
|
|
397
|
+
console.log('clicked')
|
|
398
|
+
}, [])
|
|
399
|
+
|
|
400
|
+
// ❌ Avoid - Unstable callback
|
|
401
|
+
const handleClick = () => {
|
|
402
|
+
console.log('clicked')
|
|
403
|
+
}
|
|
404
|
+
```
|
|
405
|
+
|
|
406
|
+
### Cleanup
|
|
407
|
+
|
|
408
|
+
```tsx
|
|
409
|
+
// ✅ Good - Proper cleanup
|
|
410
|
+
useEffect(() => {
|
|
411
|
+
const timer = setTimeout(() => {
|
|
412
|
+
// do something
|
|
413
|
+
}, 1000)
|
|
414
|
+
|
|
415
|
+
return () => clearTimeout(timer)
|
|
416
|
+
}, [])
|
|
417
|
+
|
|
418
|
+
// ❌ Avoid - Missing cleanup
|
|
419
|
+
useEffect(() => {
|
|
420
|
+
setTimeout(() => {
|
|
421
|
+
// do something
|
|
422
|
+
}, 1000)
|
|
423
|
+
}, [])
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
### Performance
|
|
427
|
+
|
|
428
|
+
```tsx
|
|
429
|
+
// ✅ Good - Memoized expensive operations
|
|
430
|
+
const expensiveValue = useMemo(() => {
|
|
431
|
+
return computeExpensiveValue(data)
|
|
432
|
+
}, [data])
|
|
433
|
+
|
|
434
|
+
// ❌ Avoid - Recomputing on every render
|
|
435
|
+
const expensiveValue = computeExpensiveValue(data)
|
|
436
|
+
```
|
|
437
|
+
|
|
438
|
+
## Contributing
|
|
439
|
+
|
|
440
|
+
When adding new hooks:
|
|
441
|
+
|
|
442
|
+
1. Follow React hooks best practices
|
|
443
|
+
2. Include comprehensive TypeScript definitions
|
|
444
|
+
3. Add JSDoc documentation
|
|
445
|
+
4. Write unit tests
|
|
446
|
+
5. Include usage examples
|
|
447
|
+
6. Update this README
|
|
448
|
+
|
|
449
|
+
## License
|
|
450
|
+
|
|
451
|
+
MIT
|
package/components.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://ui.shadcn.com/schema.json",
|
|
3
|
+
"style": "new-york",
|
|
4
|
+
"rsc": true,
|
|
5
|
+
"tsx": true,
|
|
6
|
+
"tailwind": {
|
|
7
|
+
"config": "",
|
|
8
|
+
"css": "src/styles/globals.css",
|
|
9
|
+
"baseColor": "neutral",
|
|
10
|
+
"cssVariables": true
|
|
11
|
+
},
|
|
12
|
+
"iconLibrary": "lucide",
|
|
13
|
+
"aliases": {
|
|
14
|
+
"components": "@workspace/ui/components",
|
|
15
|
+
"utils": "@workspace/ui/lib/utils",
|
|
16
|
+
"hooks": "@workspace/ui/hooks",
|
|
17
|
+
"lib": "@workspace/ui/lib",
|
|
18
|
+
"ui": "@workspace/ui/components"
|
|
19
|
+
}
|
|
20
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
declare function useDebouncedValue<T>(value: T, delay: number): T;
|
|
4
|
+
|
|
5
|
+
declare function useLocalStorage<T>(key: string, initialValue: T): readonly [T, (value: T | ((val: T) => T)) => void];
|
|
6
|
+
|
|
7
|
+
declare function useMediaQuery(query: string): boolean;
|
|
8
|
+
|
|
9
|
+
declare function useClickOutside<T extends HTMLElement = HTMLElement>(ref: RefObject<T>, handler: (event: MouseEvent | TouchEvent) => void): void;
|
|
10
|
+
|
|
11
|
+
declare function noop(): void;
|
|
12
|
+
|
|
13
|
+
export { noop, useClickOutside, useDebouncedValue, useLocalStorage, useMediaQuery };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// src/hooks/use-debounced-value.ts
|
|
2
|
+
import { useState, useEffect } from "react";
|
|
3
|
+
function useDebouncedValue(value, delay) {
|
|
4
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
5
|
+
useEffect(() => {
|
|
6
|
+
const handler = setTimeout(() => {
|
|
7
|
+
setDebouncedValue(value);
|
|
8
|
+
}, delay);
|
|
9
|
+
return () => {
|
|
10
|
+
clearTimeout(handler);
|
|
11
|
+
};
|
|
12
|
+
}, [value, delay]);
|
|
13
|
+
return debouncedValue;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/hooks/use-local-storage.ts
|
|
17
|
+
import { useState as useState2 } from "react";
|
|
18
|
+
function useLocalStorage(key, initialValue) {
|
|
19
|
+
const [storedValue, setStoredValue] = useState2(() => {
|
|
20
|
+
if (typeof window === "undefined") {
|
|
21
|
+
return initialValue;
|
|
22
|
+
}
|
|
23
|
+
try {
|
|
24
|
+
const item = window.localStorage.getItem(key);
|
|
25
|
+
return item ? JSON.parse(item) : initialValue;
|
|
26
|
+
} catch (error) {
|
|
27
|
+
console.log(error);
|
|
28
|
+
return initialValue;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
const setValue = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
const valueToStore = value instanceof Function ? value(storedValue) : value;
|
|
34
|
+
setStoredValue(valueToStore);
|
|
35
|
+
if (typeof window !== "undefined") {
|
|
36
|
+
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
37
|
+
}
|
|
38
|
+
} catch (error) {
|
|
39
|
+
console.log(error);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
return [storedValue, setValue];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/hooks/use-media-query.ts
|
|
46
|
+
import { useState as useState3, useEffect as useEffect3 } from "react";
|
|
47
|
+
function useMediaQuery(query) {
|
|
48
|
+
const [matches, setMatches] = useState3(false);
|
|
49
|
+
useEffect3(() => {
|
|
50
|
+
const media = window.matchMedia(query);
|
|
51
|
+
if (media.matches !== matches) {
|
|
52
|
+
setMatches(media.matches);
|
|
53
|
+
}
|
|
54
|
+
const listener = () => setMatches(media.matches);
|
|
55
|
+
window.addEventListener("resize", listener);
|
|
56
|
+
return () => window.removeEventListener("resize", listener);
|
|
57
|
+
}, [matches, query]);
|
|
58
|
+
return matches;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/hooks/use-click-outside.ts
|
|
62
|
+
import { useEffect as useEffect4 } from "react";
|
|
63
|
+
function useClickOutside(ref, handler) {
|
|
64
|
+
useEffect4(() => {
|
|
65
|
+
const listener = (event) => {
|
|
66
|
+
const el = ref?.current;
|
|
67
|
+
if (!el || el.contains(event?.target || null)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
handler(event);
|
|
71
|
+
};
|
|
72
|
+
document.addEventListener("mousedown", listener);
|
|
73
|
+
document.addEventListener("touchstart", listener);
|
|
74
|
+
return () => {
|
|
75
|
+
document.removeEventListener("mousedown", listener);
|
|
76
|
+
document.removeEventListener("touchstart", listener);
|
|
77
|
+
};
|
|
78
|
+
}, [ref, handler]);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/lib/utils.ts
|
|
82
|
+
function noop() {
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
noop,
|
|
86
|
+
useClickOutside,
|
|
87
|
+
useDebouncedValue,
|
|
88
|
+
useLocalStorage,
|
|
89
|
+
useMediaQuery
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-debounced-value.ts","../src/hooks/use-local-storage.ts","../src/hooks/use-media-query.ts","../src/hooks/use-click-outside.ts","../src/lib/utils.ts"],"sourcesContent":["import { useState, useEffect } from 'react';\n\nexport function useDebouncedValue<T>(value: T, delay: number): T {\n const [debouncedValue, setDebouncedValue] = useState<T>(value);\n\n useEffect(() => {\n const handler = setTimeout(() => {\n setDebouncedValue(value);\n }, delay);\n\n return () => {\n clearTimeout(handler);\n };\n }, [value, delay]);\n\n return debouncedValue;\n}\n","import { useState, useEffect } from 'react';\n\nexport function useLocalStorage<T>(key: string, initialValue: T) {\n // Get from local storage then parse stored json or return initialValue\n const [storedValue, setStoredValue] = useState<T>(() => {\n if (typeof window === 'undefined') {\n return initialValue;\n }\n try {\n const item = window.localStorage.getItem(key);\n return item ? JSON.parse(item) : initialValue;\n } catch (error) {\n console.log(error);\n return initialValue;\n }\n });\n\n // Return a wrapped version of useState's setter function that persists the new value to localStorage\n const setValue = (value: T | ((val: T) => T)) => {\n try {\n // Allow value to be a function so we have the same API as useState\n const valueToStore =\n value instanceof Function ? value(storedValue) : value;\n setStoredValue(valueToStore);\n if (typeof window !== 'undefined') {\n window.localStorage.setItem(key, JSON.stringify(valueToStore));\n }\n } catch (error) {\n console.log(error);\n }\n };\n\n return [storedValue, setValue] as const;\n}\n","import { useState, useEffect } from 'react';\n\nexport function useMediaQuery(query: string): boolean {\n const [matches, setMatches] = useState(false);\n\n useEffect(() => {\n const media = window.matchMedia(query);\n if (media.matches !== matches) {\n setMatches(media.matches);\n }\n const listener = () => setMatches(media.matches);\n window.addEventListener('resize', listener);\n return () => window.removeEventListener('resize', listener);\n }, [matches, query]);\n\n return matches;\n}\n","import { useEffect, RefObject } from 'react';\n\nexport function useClickOutside<T extends HTMLElement = HTMLElement>(\n ref: RefObject<T>,\n handler: (event: MouseEvent | TouchEvent) => void\n) {\n useEffect(() => {\n const listener = (event: MouseEvent | TouchEvent) => {\n const el = ref?.current;\n if (!el || el.contains((event?.target as Node) || null)) {\n return;\n }\n\n handler(event);\n };\n\n document.addEventListener('mousedown', listener);\n document.addEventListener('touchstart', listener);\n\n return () => {\n document.removeEventListener('mousedown', listener);\n document.removeEventListener('touchstart', listener);\n };\n }, [ref, handler]);\n}\n","// Utility functions for hooks package\nexport function noop() {\n // No operation function\n}\n"],"mappings":";AAAA,SAAS,UAAU,iBAAiB;AAE7B,SAAS,kBAAqB,OAAU,OAAkB;AAC/D,QAAM,CAAC,gBAAgB,iBAAiB,IAAI,SAAY,KAAK;AAE7D,YAAU,MAAM;AACd,UAAM,UAAU,WAAW,MAAM;AAC/B,wBAAkB,KAAK;AAAA,IACzB,GAAG,KAAK;AAER,WAAO,MAAM;AACX,mBAAa,OAAO;AAAA,IACtB;AAAA,EACF,GAAG,CAAC,OAAO,KAAK,CAAC;AAEjB,SAAO;AACT;;;AChBA,SAAS,YAAAA,iBAA2B;AAE7B,SAAS,gBAAmB,KAAa,cAAiB;AAE/D,QAAM,CAAC,aAAa,cAAc,IAAIA,UAAY,MAAM;AACtD,QAAI,OAAO,WAAW,aAAa;AACjC,aAAO;AAAA,IACT;AACA,QAAI;AACF,YAAM,OAAO,OAAO,aAAa,QAAQ,GAAG;AAC5C,aAAO,OAAO,KAAK,MAAM,IAAI,IAAI;AAAA,IACnC,SAAS,OAAO;AACd,cAAQ,IAAI,KAAK;AACjB,aAAO;AAAA,IACT;AAAA,EACF,CAAC;AAGD,QAAM,WAAW,CAAC,UAA+B;AAC/C,QAAI;AAEF,YAAM,eACJ,iBAAiB,WAAW,MAAM,WAAW,IAAI;AACnD,qBAAe,YAAY;AAC3B,UAAI,OAAO,WAAW,aAAa;AACjC,eAAO,aAAa,QAAQ,KAAK,KAAK,UAAU,YAAY,CAAC;AAAA,MAC/D;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,IAAI,KAAK;AAAA,IACnB;AAAA,EACF;AAEA,SAAO,CAAC,aAAa,QAAQ;AAC/B;;;ACjCA,SAAS,YAAAC,WAAU,aAAAC,kBAAiB;AAE7B,SAAS,cAAc,OAAwB;AACpD,QAAM,CAAC,SAAS,UAAU,IAAID,UAAS,KAAK;AAE5C,EAAAC,WAAU,MAAM;AACd,UAAM,QAAQ,OAAO,WAAW,KAAK;AACrC,QAAI,MAAM,YAAY,SAAS;AAC7B,iBAAW,MAAM,OAAO;AAAA,IAC1B;AACA,UAAM,WAAW,MAAM,WAAW,MAAM,OAAO;AAC/C,WAAO,iBAAiB,UAAU,QAAQ;AAC1C,WAAO,MAAM,OAAO,oBAAoB,UAAU,QAAQ;AAAA,EAC5D,GAAG,CAAC,SAAS,KAAK,CAAC;AAEnB,SAAO;AACT;;;AChBA,SAAS,aAAAC,kBAA4B;AAE9B,SAAS,gBACd,KACA,SACA;AACA,EAAAA,WAAU,MAAM;AACd,UAAM,WAAW,CAAC,UAAmC;AACnD,YAAM,KAAK,KAAK;AAChB,UAAI,CAAC,MAAM,GAAG,SAAU,OAAO,UAAmB,IAAI,GAAG;AACvD;AAAA,MACF;AAEA,cAAQ,KAAK;AAAA,IACf;AAEA,aAAS,iBAAiB,aAAa,QAAQ;AAC/C,aAAS,iBAAiB,cAAc,QAAQ;AAEhD,WAAO,MAAM;AACX,eAAS,oBAAoB,aAAa,QAAQ;AAClD,eAAS,oBAAoB,cAAc,QAAQ;AAAA,IACrD;AAAA,EACF,GAAG,CAAC,KAAK,OAAO,CAAC;AACnB;;;ACvBO,SAAS,OAAO;AAEvB;","names":["useState","useState","useEffect","useEffect"]}
|
package/eslint.config.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@foundrykit/hooks",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"private": false,
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"access": "public"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
},
|
|
17
|
+
"./hooks/*": "./src/hooks/*.ts",
|
|
18
|
+
"./lib/*": "./src/lib/*.ts"
|
|
19
|
+
},
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"react": "^19.1.1",
|
|
22
|
+
"react-dom": "^19.1.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/node": "^20.19.9",
|
|
26
|
+
"@types/react": "^19.1.9",
|
|
27
|
+
"@types/react-dom": "^19.1.7",
|
|
28
|
+
"eslint": "^9.0.0",
|
|
29
|
+
"tsup": "^8.0.0",
|
|
30
|
+
"typescript": "^5.9.2",
|
|
31
|
+
"@foundrykit/eslint-config": "0.1.0",
|
|
32
|
+
"@foundrykit/typescript-config": "0.1.0"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"react": "^19.1.1",
|
|
36
|
+
"react-dom": "^19.1.1"
|
|
37
|
+
},
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup",
|
|
40
|
+
"dev": "tsup --watch",
|
|
41
|
+
"lint": "eslint . --max-warnings 0",
|
|
42
|
+
"type-check": "tsc --noEmit"
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect, RefObject } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useClickOutside<T extends HTMLElement = HTMLElement>(
|
|
4
|
+
ref: RefObject<T>,
|
|
5
|
+
handler: (event: MouseEvent | TouchEvent) => void
|
|
6
|
+
) {
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const listener = (event: MouseEvent | TouchEvent) => {
|
|
9
|
+
const el = ref?.current;
|
|
10
|
+
if (!el || el.contains((event?.target as Node) || null)) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
handler(event);
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
document.addEventListener('mousedown', listener);
|
|
18
|
+
document.addEventListener('touchstart', listener);
|
|
19
|
+
|
|
20
|
+
return () => {
|
|
21
|
+
document.removeEventListener('mousedown', listener);
|
|
22
|
+
document.removeEventListener('touchstart', listener);
|
|
23
|
+
};
|
|
24
|
+
}, [ref, handler]);
|
|
25
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useDebouncedValue<T>(value: T, delay: number): T {
|
|
4
|
+
const [debouncedValue, setDebouncedValue] = useState<T>(value);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const handler = setTimeout(() => {
|
|
8
|
+
setDebouncedValue(value);
|
|
9
|
+
}, delay);
|
|
10
|
+
|
|
11
|
+
return () => {
|
|
12
|
+
clearTimeout(handler);
|
|
13
|
+
};
|
|
14
|
+
}, [value, delay]);
|
|
15
|
+
|
|
16
|
+
return debouncedValue;
|
|
17
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useLocalStorage<T>(key: string, initialValue: T) {
|
|
4
|
+
// Get from local storage then parse stored json or return initialValue
|
|
5
|
+
const [storedValue, setStoredValue] = useState<T>(() => {
|
|
6
|
+
if (typeof window === 'undefined') {
|
|
7
|
+
return initialValue;
|
|
8
|
+
}
|
|
9
|
+
try {
|
|
10
|
+
const item = window.localStorage.getItem(key);
|
|
11
|
+
return item ? JSON.parse(item) : initialValue;
|
|
12
|
+
} catch (error) {
|
|
13
|
+
console.log(error);
|
|
14
|
+
return initialValue;
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
// Return a wrapped version of useState's setter function that persists the new value to localStorage
|
|
19
|
+
const setValue = (value: T | ((val: T) => T)) => {
|
|
20
|
+
try {
|
|
21
|
+
// Allow value to be a function so we have the same API as useState
|
|
22
|
+
const valueToStore =
|
|
23
|
+
value instanceof Function ? value(storedValue) : value;
|
|
24
|
+
setStoredValue(valueToStore);
|
|
25
|
+
if (typeof window !== 'undefined') {
|
|
26
|
+
window.localStorage.setItem(key, JSON.stringify(valueToStore));
|
|
27
|
+
}
|
|
28
|
+
} catch (error) {
|
|
29
|
+
console.log(error);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return [storedValue, setValue] as const;
|
|
34
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { useState, useEffect } from 'react';
|
|
2
|
+
|
|
3
|
+
export function useMediaQuery(query: string): boolean {
|
|
4
|
+
const [matches, setMatches] = useState(false);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const media = window.matchMedia(query);
|
|
8
|
+
if (media.matches !== matches) {
|
|
9
|
+
setMatches(media.matches);
|
|
10
|
+
}
|
|
11
|
+
const listener = () => setMatches(media.matches);
|
|
12
|
+
window.addEventListener('resize', listener);
|
|
13
|
+
return () => window.removeEventListener('resize', listener);
|
|
14
|
+
}, [matches, query]);
|
|
15
|
+
|
|
16
|
+
return matches;
|
|
17
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Export all hooks
|
|
2
|
+
export { useDebouncedValue } from './hooks/use-debounced-value.js';
|
|
3
|
+
export { useLocalStorage } from './hooks/use-local-storage.js';
|
|
4
|
+
export { useMediaQuery } from './hooks/use-media-query.js';
|
|
5
|
+
export { useClickOutside } from './hooks/use-click-outside.js';
|
|
6
|
+
|
|
7
|
+
// Export utilities
|
|
8
|
+
export * from './lib/utils.js';
|
package/src/lib/utils.ts
ADDED
package/tsconfig.json
ADDED