@vitus-labs/hooks 2.0.0-alpha.9 → 2.0.0-beta.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 +265 -40
- package/lib/index.d.ts +295 -1
- package/lib/index.js +569 -3
- package/lib/vitus-labs-hooks.native.js +435 -0
- package/package.json +20 -9
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @vitus-labs/hooks
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
28 React hooks for UI interactions, state management, DOM observation, accessibility, and theming. 2.15KB gzipped.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@vitus-labs/hooks)
|
|
6
6
|
[](https://github.com/vitus-labs/ui-system/blob/main/LICENSE)
|
|
@@ -13,63 +13,288 @@ npm install @vitus-labs/hooks
|
|
|
13
13
|
|
|
14
14
|
## Hooks
|
|
15
15
|
|
|
16
|
-
###
|
|
16
|
+
### Primitives
|
|
17
17
|
|
|
18
|
-
|
|
18
|
+
#### useLatest
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
20
|
+
Returns a ref that always holds the latest value. Avoids stale closures in callbacks and effects.
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
const ref = useLatest(callback)
|
|
24
|
+
// ref.current is always the latest callback
|
|
25
|
+
```
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
const { hover, onMouseEnter, onMouseLeave } = useHover()
|
|
27
|
+
#### useToggle
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
style={{ background: hover ? '#f0f0f0' : '#fff' }}
|
|
31
|
-
>
|
|
32
|
-
{hover ? 'Hovered' : 'Hover me'}
|
|
33
|
-
</div>
|
|
34
|
-
)
|
|
35
|
-
}
|
|
29
|
+
Boolean state with `toggle`, `setTrue`, and `setFalse` helpers.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
const [isOpen, toggle, open, close] = useToggle(false)
|
|
36
33
|
```
|
|
37
34
|
|
|
38
|
-
|
|
35
|
+
#### usePrevious
|
|
39
36
|
|
|
40
|
-
|
|
41
|
-
| ----- | ---- | ------- | ----------- |
|
|
42
|
-
| initialValue | `boolean` | `false` | Initial hover state |
|
|
37
|
+
Returns the value from the previous render.
|
|
43
38
|
|
|
44
|
-
|
|
39
|
+
```ts
|
|
40
|
+
const prev = usePrevious(count)
|
|
41
|
+
// undefined on first render, then the previous value
|
|
42
|
+
```
|
|
45
43
|
|
|
46
|
-
###
|
|
44
|
+
### Callbacks
|
|
47
45
|
|
|
48
|
-
|
|
46
|
+
#### useDebouncedCallback
|
|
47
|
+
|
|
48
|
+
Stable debounced function with `.cancel()` and `.flush()`.
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
const search = useDebouncedCallback((query: string) => {
|
|
52
|
+
fetchResults(query)
|
|
53
|
+
}, 300)
|
|
54
|
+
|
|
55
|
+
search('hello') // fires after 300ms of inactivity
|
|
56
|
+
search.cancel() // cancel pending
|
|
57
|
+
search.flush() // fire immediately
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
#### useThrottledCallback
|
|
61
|
+
|
|
62
|
+
Stable throttled function with `.cancel()`. Uses `throttle` from `@vitus-labs/core`.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const handleScroll = useThrottledCallback(() => {
|
|
66
|
+
updatePosition()
|
|
67
|
+
}, 100)
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### State
|
|
71
|
+
|
|
72
|
+
#### useDebouncedValue
|
|
73
|
+
|
|
74
|
+
Returns a debounced version of the value that only updates after `delay` ms of inactivity.
|
|
75
|
+
|
|
76
|
+
```ts
|
|
77
|
+
const [search, setSearch] = useState('')
|
|
78
|
+
const debouncedSearch = useDebouncedValue(search, 300)
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
#### useControllableState
|
|
82
|
+
|
|
83
|
+
Unified controlled/uncontrolled state pattern.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
const [value, setValue] = useControllableState({
|
|
87
|
+
value: props.value, // controlled (optional)
|
|
88
|
+
defaultValue: '', // uncontrolled fallback
|
|
89
|
+
onChange: props.onChange, // fires in both modes
|
|
90
|
+
})
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Effects
|
|
94
|
+
|
|
95
|
+
#### useUpdateEffect
|
|
96
|
+
|
|
97
|
+
Like `useEffect` but skips the initial mount.
|
|
98
|
+
|
|
99
|
+
```ts
|
|
100
|
+
useUpdateEffect(() => {
|
|
101
|
+
// only fires on updates, not on mount
|
|
102
|
+
saveToStorage(value)
|
|
103
|
+
}, [value])
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
#### useIsomorphicLayoutEffect
|
|
107
|
+
|
|
108
|
+
`useLayoutEffect` on the client, `useEffect` on the server. Avoids SSR warnings.
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
useIsomorphicLayoutEffect(() => {
|
|
112
|
+
measureElement()
|
|
113
|
+
}, [])
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### useInterval
|
|
117
|
+
|
|
118
|
+
Declarative `setInterval` with auto-cleanup. Pass `null` to pause.
|
|
119
|
+
|
|
120
|
+
```ts
|
|
121
|
+
useInterval(() => tick(), 1000) // every second
|
|
122
|
+
useInterval(() => tick(), null) // paused
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
#### useTimeout
|
|
126
|
+
|
|
127
|
+
Declarative `setTimeout` with `reset` and `clear` controls.
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const { reset, clear } = useTimeout(() => {
|
|
131
|
+
showNotification()
|
|
132
|
+
}, 5000)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### DOM & Observers
|
|
136
|
+
|
|
137
|
+
#### useElementSize
|
|
138
|
+
|
|
139
|
+
Tracks element `width` and `height` via `ResizeObserver`.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
const [ref, { width, height }] = useElementSize()
|
|
143
|
+
return <div ref={ref}>Size: {width}x{height}</div>
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
#### useIntersection
|
|
147
|
+
|
|
148
|
+
`IntersectionObserver` wrapper for visibility detection.
|
|
49
149
|
|
|
50
150
|
```tsx
|
|
51
|
-
|
|
151
|
+
const [ref, entry] = useIntersection({ threshold: 0.5 })
|
|
152
|
+
const isVisible = entry?.isIntersecting
|
|
153
|
+
return <div ref={ref}>{isVisible ? 'Visible' : 'Hidden'}</div>
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Interaction
|
|
157
|
+
|
|
158
|
+
#### useHover
|
|
159
|
+
|
|
160
|
+
Tracks hover state with stable callback references.
|
|
161
|
+
|
|
162
|
+
```ts
|
|
163
|
+
const { hover, onMouseEnter, onMouseLeave } = useHover()
|
|
164
|
+
```
|
|
52
165
|
|
|
53
|
-
|
|
54
|
-
const { width, height } = useWindowResize({
|
|
55
|
-
throttleDelay: 300,
|
|
56
|
-
onChange: ({ width }) => console.log('Width:', width),
|
|
57
|
-
})
|
|
166
|
+
#### useFocus
|
|
58
167
|
|
|
59
|
-
|
|
60
|
-
|
|
168
|
+
Tracks focus state with stable callback references.
|
|
169
|
+
|
|
170
|
+
```ts
|
|
171
|
+
const { focused, onFocus, onBlur } = useFocus()
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
#### useClickOutside
|
|
175
|
+
|
|
176
|
+
Calls handler when a click occurs outside the referenced element.
|
|
177
|
+
|
|
178
|
+
```ts
|
|
179
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
180
|
+
useClickOutside(ref, () => setOpen(false))
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
#### useScrollLock
|
|
184
|
+
|
|
185
|
+
Locks page scroll by setting `overflow: hidden` on `document.body`.
|
|
186
|
+
|
|
187
|
+
```ts
|
|
188
|
+
useScrollLock(isModalOpen)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### useKeyboard
|
|
192
|
+
|
|
193
|
+
Listens for a specific keyboard key.
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
useKeyboard('Escape', () => setOpen(false))
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
#### useFocusTrap
|
|
200
|
+
|
|
201
|
+
Traps Tab/Shift+Tab focus within a container. Essential for modals and dialogs.
|
|
202
|
+
|
|
203
|
+
```ts
|
|
204
|
+
const ref = useRef<HTMLDivElement>(null)
|
|
205
|
+
useFocusTrap(ref, isOpen)
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
### Responsive
|
|
209
|
+
|
|
210
|
+
#### useMediaQuery
|
|
211
|
+
|
|
212
|
+
Subscribes to a CSS media query and returns whether it matches.
|
|
213
|
+
|
|
214
|
+
```ts
|
|
215
|
+
const isDesktop = useMediaQuery('(min-width: 1024px)')
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### useBreakpoint
|
|
219
|
+
|
|
220
|
+
Returns the currently active breakpoint name from the theme context.
|
|
221
|
+
|
|
222
|
+
```ts
|
|
223
|
+
const bp = useBreakpoint() // "xs" | "sm" | "md" | "lg" | "xl" | undefined
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
#### useColorScheme
|
|
227
|
+
|
|
228
|
+
Returns the user's preferred color scheme. Pairs with rocketstyle's `mode`.
|
|
229
|
+
|
|
230
|
+
```ts
|
|
231
|
+
const scheme = useColorScheme() // "light" | "dark"
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
#### useReducedMotion
|
|
235
|
+
|
|
236
|
+
Returns `true` when the user prefers reduced motion.
|
|
237
|
+
|
|
238
|
+
```ts
|
|
239
|
+
const reduced = useReducedMotion()
|
|
240
|
+
const duration = reduced ? 0 : 300
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Theme & Styling
|
|
244
|
+
|
|
245
|
+
#### useThemeValue
|
|
246
|
+
|
|
247
|
+
Deep-reads a value from the current theme by dot-separated path.
|
|
248
|
+
|
|
249
|
+
```ts
|
|
250
|
+
const primary = useThemeValue<string>('colors.primary')
|
|
251
|
+
const columns = useThemeValue<number>('grid.columns')
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### useRootSize
|
|
255
|
+
|
|
256
|
+
Returns `rootSize` from the theme with px/rem conversion utilities.
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
const { rootSize, pxToRem, remToPx } = useRootSize()
|
|
260
|
+
pxToRem(32) // "2rem"
|
|
261
|
+
remToPx(2) // 32
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
#### useSpacing
|
|
265
|
+
|
|
266
|
+
Returns a spacing function based on the theme's root size.
|
|
267
|
+
|
|
268
|
+
```ts
|
|
269
|
+
const spacing = useSpacing()
|
|
270
|
+
spacing(1) // "8px"
|
|
271
|
+
spacing(2) // "16px"
|
|
272
|
+
spacing(0.5) // "4px"
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
### Composition
|
|
276
|
+
|
|
277
|
+
#### useMergedRef
|
|
278
|
+
|
|
279
|
+
Merges multiple refs (callback or object) into a single stable callback ref.
|
|
280
|
+
|
|
281
|
+
```tsx
|
|
282
|
+
const Component = forwardRef((props, ref) => {
|
|
283
|
+
const localRef = useRef(null)
|
|
284
|
+
const merged = useMergedRef(ref, localRef)
|
|
285
|
+
return <div ref={merged} />
|
|
286
|
+
})
|
|
61
287
|
```
|
|
62
288
|
|
|
63
|
-
|
|
289
|
+
### Viewport
|
|
64
290
|
|
|
65
|
-
|
|
66
|
-
| ----- | ---- | ------- | ----------- |
|
|
67
|
-
| params.throttleDelay | `number` | `200` | Milliseconds between resize handler calls |
|
|
68
|
-
| params.onChange | `(sizes) => void` | — | Callback fired on each resize |
|
|
69
|
-
| initialValues.width | `number` | `0` | Initial width (useful for SSR) |
|
|
70
|
-
| initialValues.height | `number` | `0` | Initial height (useful for SSR) |
|
|
291
|
+
#### useWindowResize
|
|
71
292
|
|
|
72
|
-
|
|
293
|
+
Tracks viewport dimensions with throttled updates.
|
|
294
|
+
|
|
295
|
+
```ts
|
|
296
|
+
const { width, height } = useWindowResize({ throttleDelay: 300 })
|
|
297
|
+
```
|
|
73
298
|
|
|
74
299
|
## Peer Dependencies
|
|
75
300
|
|
package/lib/index.d.ts
CHANGED
|
@@ -1,3 +1,116 @@
|
|
|
1
|
+
import { DependencyList, EffectCallback, Ref, useLayoutEffect } from "react";
|
|
2
|
+
|
|
3
|
+
//#region src/useBreakpoint.d.ts
|
|
4
|
+
type UseBreakpoint = () => string | undefined;
|
|
5
|
+
/**
|
|
6
|
+
* Returns the name of the currently active breakpoint from the
|
|
7
|
+
* unistyle/core theme context (e.g. `"xs"`, `"md"`, `"lg"`).
|
|
8
|
+
*
|
|
9
|
+
* Reads `theme.breakpoints` from the nearest `Provider` and
|
|
10
|
+
* subscribes to viewport changes via `matchMedia`.
|
|
11
|
+
*
|
|
12
|
+
* Returns `undefined` when no Provider or breakpoints are available.
|
|
13
|
+
*/
|
|
14
|
+
declare const useBreakpoint: UseBreakpoint;
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/useClickOutside.d.ts
|
|
17
|
+
type UseClickOutside = (ref: {
|
|
18
|
+
current: Element | null;
|
|
19
|
+
}, handler: (event: Event) => void) => void;
|
|
20
|
+
/**
|
|
21
|
+
* Calls `handler` when a click (mousedown or touchstart) occurs
|
|
22
|
+
* outside the element referenced by `ref`.
|
|
23
|
+
*/
|
|
24
|
+
declare const useClickOutside: UseClickOutside;
|
|
25
|
+
//#endregion
|
|
26
|
+
//#region src/useColorScheme.d.ts
|
|
27
|
+
type UseColorScheme = () => 'light' | 'dark';
|
|
28
|
+
/**
|
|
29
|
+
* Returns the user's preferred color scheme (`"light"` or `"dark"`).
|
|
30
|
+
* Reacts to OS-level preference changes in real time.
|
|
31
|
+
* Pairs with rocketstyle's `mode` system.
|
|
32
|
+
*/
|
|
33
|
+
declare const useColorScheme: UseColorScheme;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/useControllableState.d.ts
|
|
36
|
+
type UseControllableStateOptions<T> = {
|
|
37
|
+
value?: T;
|
|
38
|
+
defaultValue: T;
|
|
39
|
+
onChange?: (value: T) => void;
|
|
40
|
+
};
|
|
41
|
+
type UseControllableState = <T>(options: UseControllableStateOptions<T>) => [T, (next: T | ((prev: T) => T)) => void];
|
|
42
|
+
/**
|
|
43
|
+
* Unified controlled/uncontrolled state pattern.
|
|
44
|
+
* When `value` is provided the component is controlled; otherwise
|
|
45
|
+
* internal state is used with `defaultValue` as the initial value.
|
|
46
|
+
* The `onChange` callback fires in both modes.
|
|
47
|
+
*/
|
|
48
|
+
declare const useControllableState: UseControllableState;
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/useDebouncedCallback.d.ts
|
|
51
|
+
type DebouncedFn<T extends (...args: any[]) => any> = {
|
|
52
|
+
(...args: Parameters<T>): void;
|
|
53
|
+
cancel: () => void;
|
|
54
|
+
flush: () => void;
|
|
55
|
+
};
|
|
56
|
+
type UseDebouncedCallback = <T extends (...args: any[]) => any>(callback: T, delay: number) => DebouncedFn<T>;
|
|
57
|
+
/**
|
|
58
|
+
* Returns a stable debounced version of the callback.
|
|
59
|
+
* The returned function has `.cancel()` and `.flush()` methods.
|
|
60
|
+
* Always calls the latest callback (no stale closures).
|
|
61
|
+
* Cleans up on unmount.
|
|
62
|
+
*/
|
|
63
|
+
declare const useDebouncedCallback: UseDebouncedCallback;
|
|
64
|
+
//#endregion
|
|
65
|
+
//#region src/useDebouncedValue.d.ts
|
|
66
|
+
type UseDebouncedValue = <T>(value: T, delay: number) => T;
|
|
67
|
+
/**
|
|
68
|
+
* Returns a debounced version of the value that only updates
|
|
69
|
+
* after `delay` ms of inactivity.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* const [search, setSearch] = useState('')
|
|
74
|
+
* const debouncedSearch = useDebouncedValue(search, 300)
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
declare const useDebouncedValue: UseDebouncedValue;
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/useElementSize.d.ts
|
|
80
|
+
type Size = {
|
|
81
|
+
width: number;
|
|
82
|
+
height: number;
|
|
83
|
+
};
|
|
84
|
+
type UseElementSize = () => [(node: Element | null) => void, Size];
|
|
85
|
+
/**
|
|
86
|
+
* Tracks an element's `width` and `height` via `ResizeObserver`.
|
|
87
|
+
* Returns `[ref, { width, height }]` — pass `ref` as a callback ref.
|
|
88
|
+
*/
|
|
89
|
+
declare const useElementSize: UseElementSize;
|
|
90
|
+
//#endregion
|
|
91
|
+
//#region src/useFocus.d.ts
|
|
92
|
+
type UseFocus = (initialValue?: boolean) => {
|
|
93
|
+
focused: boolean;
|
|
94
|
+
onFocus: () => void;
|
|
95
|
+
onBlur: () => void;
|
|
96
|
+
};
|
|
97
|
+
/**
|
|
98
|
+
* Simple focus-state hook that returns a boolean plus stable
|
|
99
|
+
* `onFocus`/`onBlur` handlers ready to spread onto an element.
|
|
100
|
+
*/
|
|
101
|
+
declare const useFocus: UseFocus;
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/useFocusTrap.d.ts
|
|
104
|
+
type UseFocusTrap = (ref: {
|
|
105
|
+
current: HTMLElement | null;
|
|
106
|
+
}, enabled?: boolean) => void;
|
|
107
|
+
/**
|
|
108
|
+
* Traps keyboard focus within the referenced container.
|
|
109
|
+
* Tab and Shift+Tab cycle through focusable elements inside.
|
|
110
|
+
* Useful for modals, dialogs, and dropdown menus.
|
|
111
|
+
*/
|
|
112
|
+
declare const useFocusTrap: UseFocusTrap;
|
|
113
|
+
//#endregion
|
|
1
114
|
//#region src/useHover.d.ts
|
|
2
115
|
type UseHover = (initialValue?: boolean) => {
|
|
3
116
|
hover: boolean;
|
|
@@ -10,6 +123,187 @@ type UseHover = (initialValue?: boolean) => {
|
|
|
10
123
|
*/
|
|
11
124
|
declare const useHover: UseHover;
|
|
12
125
|
//#endregion
|
|
126
|
+
//#region src/useIntersection.d.ts
|
|
127
|
+
type UseIntersectionOptions = {
|
|
128
|
+
threshold?: number | number[];
|
|
129
|
+
rootMargin?: string;
|
|
130
|
+
root?: Element | null;
|
|
131
|
+
};
|
|
132
|
+
type UseIntersection = (options?: UseIntersectionOptions) => [(node: Element | null) => void, IntersectionObserverEntry | null];
|
|
133
|
+
/**
|
|
134
|
+
* Observes an element's intersection with the viewport (or a root element).
|
|
135
|
+
* Returns `[ref, entry]` — pass `ref` as a callback ref.
|
|
136
|
+
*/
|
|
137
|
+
declare const useIntersection: UseIntersection;
|
|
138
|
+
//#endregion
|
|
139
|
+
//#region src/useInterval.d.ts
|
|
140
|
+
type UseInterval = (callback: () => void, delay: number | null) => void;
|
|
141
|
+
/**
|
|
142
|
+
* Declarative `setInterval` with auto-cleanup.
|
|
143
|
+
* Pass `null` as `delay` to pause the interval.
|
|
144
|
+
* Always calls the latest callback (no stale closures).
|
|
145
|
+
*/
|
|
146
|
+
declare const useInterval: UseInterval;
|
|
147
|
+
//#endregion
|
|
148
|
+
//#region src/useIsomorphicLayoutEffect.d.ts
|
|
149
|
+
/**
|
|
150
|
+
* `useLayoutEffect` on the client, `useEffect` on the server.
|
|
151
|
+
* Avoids the React SSR warning about useLayoutEffect.
|
|
152
|
+
*/
|
|
153
|
+
declare const useIsomorphicLayoutEffect: typeof useLayoutEffect;
|
|
154
|
+
type UseIsomorphicLayoutEffect = typeof useIsomorphicLayoutEffect;
|
|
155
|
+
//#endregion
|
|
156
|
+
//#region src/useKeyboard.d.ts
|
|
157
|
+
type UseKeyboard = (key: string, handler: (event: KeyboardEvent) => void) => void;
|
|
158
|
+
/**
|
|
159
|
+
* Listens for a specific keyboard key and calls the handler.
|
|
160
|
+
* Matches `event.key` (e.g. `"Escape"`, `"Enter"`, `"a"`).
|
|
161
|
+
* Always calls the latest handler (no stale closures).
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* useKeyboard('Escape', () => setOpen(false))
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
declare const useKeyboard: UseKeyboard;
|
|
169
|
+
//#endregion
|
|
170
|
+
//#region src/useLatest.d.ts
|
|
171
|
+
type UseLatest = <T>(value: T) => {
|
|
172
|
+
readonly current: T;
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Returns a ref that always holds the latest value.
|
|
176
|
+
* Useful to avoid stale closures in callbacks and effects.
|
|
177
|
+
*/
|
|
178
|
+
declare const useLatest: UseLatest;
|
|
179
|
+
//#endregion
|
|
180
|
+
//#region src/useMediaQuery.d.ts
|
|
181
|
+
type UseMediaQuery = (query: string) => boolean;
|
|
182
|
+
/**
|
|
183
|
+
* Subscribes to a CSS media query and returns whether it currently matches.
|
|
184
|
+
* Uses `window.matchMedia` with an event listener for live updates.
|
|
185
|
+
*/
|
|
186
|
+
declare const useMediaQuery: UseMediaQuery;
|
|
187
|
+
//#endregion
|
|
188
|
+
//#region src/useMergedRef.d.ts
|
|
189
|
+
type UseMergedRef = <T>(...refs: (Ref<T> | undefined)[]) => (node: T | null) => void;
|
|
190
|
+
/**
|
|
191
|
+
* Merges multiple refs (callback or object) into a single stable callback ref.
|
|
192
|
+
* Handles null, callback refs, and object refs with `.current`.
|
|
193
|
+
*/
|
|
194
|
+
declare const useMergedRef: <T>(...refs: (Ref<T> | undefined)[]) => (node: T | null) => void;
|
|
195
|
+
//#endregion
|
|
196
|
+
//#region src/usePrevious.d.ts
|
|
197
|
+
type UsePrevious = <T>(value: T) => T | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Returns the value from the previous render.
|
|
200
|
+
* Returns `undefined` on the first render.
|
|
201
|
+
*/
|
|
202
|
+
declare const usePrevious: UsePrevious;
|
|
203
|
+
//#endregion
|
|
204
|
+
//#region src/useReducedMotion.d.ts
|
|
205
|
+
type UseReducedMotion = () => boolean;
|
|
206
|
+
/**
|
|
207
|
+
* Returns `true` when the user prefers reduced motion.
|
|
208
|
+
* Use to disable or simplify animations for accessibility.
|
|
209
|
+
*/
|
|
210
|
+
declare const useReducedMotion: UseReducedMotion;
|
|
211
|
+
//#endregion
|
|
212
|
+
//#region src/useRootSize.d.ts
|
|
213
|
+
type RootSizeResult = {
|
|
214
|
+
rootSize: number;
|
|
215
|
+
pxToRem: (px: number) => string;
|
|
216
|
+
remToPx: (rem: number) => number;
|
|
217
|
+
};
|
|
218
|
+
type UseRootSize = () => RootSizeResult;
|
|
219
|
+
/**
|
|
220
|
+
* Returns `rootSize` from the theme context along with
|
|
221
|
+
* `pxToRem` and `remToPx` conversion utilities.
|
|
222
|
+
*
|
|
223
|
+
* Defaults to `16` when no Provider is mounted.
|
|
224
|
+
*/
|
|
225
|
+
declare const useRootSize: UseRootSize;
|
|
226
|
+
//#endregion
|
|
227
|
+
//#region src/useScrollLock.d.ts
|
|
228
|
+
type UseScrollLock = (enabled: boolean) => void;
|
|
229
|
+
/**
|
|
230
|
+
* Locks page scroll by setting `overflow: hidden` on `document.body`.
|
|
231
|
+
* Uses a reference counter so concurrent locks don't clobber each other.
|
|
232
|
+
*/
|
|
233
|
+
declare const useScrollLock: UseScrollLock;
|
|
234
|
+
//#endregion
|
|
235
|
+
//#region src/useSpacing.d.ts
|
|
236
|
+
type UseSpacing = (base?: number) => (multiplier: number) => string;
|
|
237
|
+
/**
|
|
238
|
+
* Returns a `spacing(n)` function that computes spacing values
|
|
239
|
+
* based on `rootSize` from the theme.
|
|
240
|
+
*
|
|
241
|
+
* @param base - Base spacing unit in px (defaults to `rootSize / 2`, i.e. 8px)
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* ```ts
|
|
245
|
+
* const spacing = useSpacing()
|
|
246
|
+
* spacing(1) // "8px"
|
|
247
|
+
* spacing(2) // "16px"
|
|
248
|
+
* spacing(0.5) // "4px"
|
|
249
|
+
* ```
|
|
250
|
+
*/
|
|
251
|
+
declare const useSpacing: UseSpacing;
|
|
252
|
+
//#endregion
|
|
253
|
+
//#region src/useThemeValue.d.ts
|
|
254
|
+
type UseThemeValue = <T = unknown>(path: string) => T | undefined;
|
|
255
|
+
/**
|
|
256
|
+
* Deep-reads a value from the current theme by dot-separated path.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* const primary = useThemeValue<string>('colors.primary')
|
|
261
|
+
* const columns = useThemeValue<number>('grid.columns')
|
|
262
|
+
* ```
|
|
263
|
+
*/
|
|
264
|
+
declare const useThemeValue: UseThemeValue;
|
|
265
|
+
//#endregion
|
|
266
|
+
//#region src/useThrottledCallback.d.ts
|
|
267
|
+
type ThrottledFn<T extends (...args: any[]) => any> = {
|
|
268
|
+
(...args: Parameters<T>): void;
|
|
269
|
+
cancel: () => void;
|
|
270
|
+
};
|
|
271
|
+
type UseThrottledCallback = <T extends (...args: any[]) => any>(callback: T, delay: number) => ThrottledFn<T>;
|
|
272
|
+
/**
|
|
273
|
+
* Returns a stable throttled version of the callback.
|
|
274
|
+
* Uses `throttle` from `@vitus-labs/core`.
|
|
275
|
+
* Always calls the latest callback (no stale closures).
|
|
276
|
+
* Cleans up on unmount.
|
|
277
|
+
*/
|
|
278
|
+
declare const useThrottledCallback: UseThrottledCallback;
|
|
279
|
+
//#endregion
|
|
280
|
+
//#region src/useTimeout.d.ts
|
|
281
|
+
type UseTimeout = (callback: () => void, delay: number | null) => {
|
|
282
|
+
reset: () => void;
|
|
283
|
+
clear: () => void;
|
|
284
|
+
};
|
|
285
|
+
/**
|
|
286
|
+
* Declarative `setTimeout` with auto-cleanup.
|
|
287
|
+
* Pass `null` as `delay` to disable. Returns `reset` and `clear` controls.
|
|
288
|
+
* Always calls the latest callback (no stale closures).
|
|
289
|
+
*/
|
|
290
|
+
declare const useTimeout: UseTimeout;
|
|
291
|
+
//#endregion
|
|
292
|
+
//#region src/useToggle.d.ts
|
|
293
|
+
type UseToggle = (initialValue?: boolean) => [boolean, () => void, () => void, () => void];
|
|
294
|
+
/**
|
|
295
|
+
* Boolean state with `toggle`, `setTrue`, and `setFalse` helpers.
|
|
296
|
+
* Returns `[value, toggle, setTrue, setFalse]`.
|
|
297
|
+
*/
|
|
298
|
+
declare const useToggle: UseToggle;
|
|
299
|
+
//#endregion
|
|
300
|
+
//#region src/useUpdateEffect.d.ts
|
|
301
|
+
type UseUpdateEffect = (effect: EffectCallback, deps?: DependencyList) => void;
|
|
302
|
+
/**
|
|
303
|
+
* Like `useEffect` but skips the initial mount — only fires on updates.
|
|
304
|
+
*/
|
|
305
|
+
declare const useUpdateEffect: UseUpdateEffect;
|
|
306
|
+
//#endregion
|
|
13
307
|
//#region src/useWindowResize.d.ts
|
|
14
308
|
type Sizes = {
|
|
15
309
|
width: number;
|
|
@@ -27,5 +321,5 @@ type UseWindowResize = (params?: Partial<{
|
|
|
27
321
|
*/
|
|
28
322
|
declare const useWindowResize: UseWindowResize;
|
|
29
323
|
//#endregion
|
|
30
|
-
export { type UseHover, type UseWindowResize, useHover, useWindowResize };
|
|
324
|
+
export { type UseBreakpoint, type UseClickOutside, type UseColorScheme, type UseControllableState, type UseDebouncedCallback, type UseDebouncedValue, type UseElementSize, type UseFocus, type UseFocusTrap, type UseHover, type UseIntersection, type UseInterval, type UseIsomorphicLayoutEffect, type UseKeyboard, type UseLatest, type UseMediaQuery, type UseMergedRef, type UsePrevious, type UseReducedMotion, type UseRootSize, type UseScrollLock, type UseSpacing, type UseThemeValue, type UseThrottledCallback, type UseTimeout, type UseToggle, type UseUpdateEffect, type UseWindowResize, useBreakpoint, useClickOutside, useColorScheme, useControllableState, useDebouncedCallback, useDebouncedValue, useElementSize, useFocus, useFocusTrap, useHover, useIntersection, useInterval, useIsomorphicLayoutEffect, useKeyboard, useLatest, useMediaQuery, useMergedRef, usePrevious, useReducedMotion, useRootSize, useScrollLock, useSpacing, useThemeValue, useThrottledCallback, useTimeout, useToggle, useUpdateEffect, useWindowResize };
|
|
31
325
|
//# sourceMappingURL=index2.d.ts.map
|