@pyreon/hooks 0.11.5 → 0.11.7
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 +5 -5
- package/lib/index.d.ts +4 -4
- package/package.json +24 -24
- package/src/__tests__/useBreakpoint.test.ts +48 -48
- package/src/__tests__/useClickOutside.test.ts +31 -31
- package/src/__tests__/useColorScheme.test.ts +22 -22
- package/src/__tests__/useControllableState.test.ts +18 -18
- package/src/__tests__/useDebouncedCallback.test.ts +19 -19
- package/src/__tests__/useDebouncedValue.test.ts +28 -28
- package/src/__tests__/useElementSize.test.ts +21 -21
- package/src/__tests__/useFocus.test.ts +12 -12
- package/src/__tests__/useFocusTrap.test.ts +36 -36
- package/src/__tests__/useHover.test.ts +13 -13
- package/src/__tests__/useIntersection.test.ts +20 -20
- package/src/__tests__/useInterval.test.ts +7 -7
- package/src/__tests__/useIsomorphicLayoutEffect.test.ts +5 -5
- package/src/__tests__/useKeyboard.test.ts +38 -38
- package/src/__tests__/useLatest.test.ts +11 -11
- package/src/__tests__/useMediaQuery.test.ts +29 -29
- package/src/__tests__/useMergedRef.test.ts +10 -10
- package/src/__tests__/usePrevious.test.ts +20 -20
- package/src/__tests__/useReducedMotion.test.ts +15 -15
- package/src/__tests__/useRootSize.test.ts +9 -9
- package/src/__tests__/useScrollLock.test.ts +33 -33
- package/src/__tests__/useSpacing.test.ts +11 -11
- package/src/__tests__/useThemeValue.test.ts +5 -5
- package/src/__tests__/useThrottledCallback.test.ts +16 -16
- package/src/__tests__/useTimeout.test.ts +8 -8
- package/src/__tests__/useToggle.test.ts +14 -14
- package/src/__tests__/useUpdateEffect.test.ts +8 -8
- package/src/__tests__/useWindowResize.test.ts +34 -34
- package/src/index.ts +56 -56
- package/src/useBreakpoint.ts +6 -6
- package/src/useClickOutside.ts +5 -5
- package/src/useClipboard.ts +2 -2
- package/src/useColorScheme.ts +5 -5
- package/src/useControllableState.ts +2 -2
- package/src/useDebouncedCallback.ts +1 -1
- package/src/useDebouncedValue.ts +2 -2
- package/src/useDialog.ts +4 -4
- package/src/useElementSize.ts +2 -2
- package/src/useEventListener.ts +2 -2
- package/src/useFocus.ts +1 -1
- package/src/useFocusTrap.ts +4 -4
- package/src/useHover.ts +1 -1
- package/src/useInfiniteScroll.ts +10 -10
- package/src/useIntersection.ts +2 -2
- package/src/useInterval.ts +1 -1
- package/src/useIsomorphicLayoutEffect.ts +2 -2
- package/src/useKeyboard.ts +3 -3
- package/src/useMediaQuery.ts +4 -4
- package/src/useMergedRef.ts +1 -1
- package/src/useOnline.ts +6 -6
- package/src/usePrevious.ts +1 -1
- package/src/useReducedMotion.ts +2 -2
- package/src/useRootSize.ts +1 -1
- package/src/useScrollLock.ts +3 -3
- package/src/useSpacing.ts +1 -1
- package/src/useThemeValue.ts +2 -2
- package/src/useThrottledCallback.ts +2 -2
- package/src/useTimeAgo.ts +15 -15
- package/src/useTimeout.ts +1 -1
- package/src/useToggle.ts +1 -1
- package/src/useUpdateEffect.ts +2 -2
- package/src/useWindowResize.ts +6 -6
package/src/useInfiniteScroll.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onCleanup, signal } from
|
|
1
|
+
import { onCleanup, signal } from '@pyreon/reactivity'
|
|
2
2
|
|
|
3
3
|
export interface UseInfiniteScrollOptions {
|
|
4
4
|
/** Distance from bottom (px) to trigger load. Default: 100 */
|
|
@@ -8,7 +8,7 @@ export interface UseInfiniteScrollOptions {
|
|
|
8
8
|
/** Whether there's more data to load. Default: true */
|
|
9
9
|
hasMore?: () => boolean
|
|
10
10
|
/** Scroll direction. Default: "down" */
|
|
11
|
-
direction?:
|
|
11
|
+
direction?: 'up' | 'down'
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface UseInfiniteScrollResult {
|
|
@@ -48,7 +48,7 @@ export function useInfiniteScroll(
|
|
|
48
48
|
options?: UseInfiniteScrollOptions,
|
|
49
49
|
): UseInfiniteScrollResult {
|
|
50
50
|
const threshold = options?.threshold ?? 100
|
|
51
|
-
const direction = options?.direction ??
|
|
51
|
+
const direction = options?.direction ?? 'down'
|
|
52
52
|
const triggered = signal(false)
|
|
53
53
|
let observer: IntersectionObserver | null = null
|
|
54
54
|
let sentinel: HTMLDivElement | null = null
|
|
@@ -72,13 +72,13 @@ export function useInfiniteScroll(
|
|
|
72
72
|
containerEl = el
|
|
73
73
|
|
|
74
74
|
// Create an invisible sentinel element at the scroll boundary
|
|
75
|
-
sentinel = document.createElement(
|
|
76
|
-
sentinel.style.height =
|
|
77
|
-
sentinel.style.width =
|
|
78
|
-
sentinel.style.pointerEvents =
|
|
79
|
-
sentinel.setAttribute(
|
|
75
|
+
sentinel = document.createElement('div')
|
|
76
|
+
sentinel.style.height = '1px'
|
|
77
|
+
sentinel.style.width = '100%'
|
|
78
|
+
sentinel.style.pointerEvents = 'none'
|
|
79
|
+
sentinel.setAttribute('aria-hidden', 'true')
|
|
80
80
|
|
|
81
|
-
if (direction ===
|
|
81
|
+
if (direction === 'down') {
|
|
82
82
|
el.appendChild(sentinel)
|
|
83
83
|
} else {
|
|
84
84
|
el.insertBefore(sentinel, el.firstChild)
|
|
@@ -87,7 +87,7 @@ export function useInfiniteScroll(
|
|
|
87
87
|
observer = new IntersectionObserver(handleIntersect, {
|
|
88
88
|
root: el,
|
|
89
89
|
rootMargin:
|
|
90
|
-
direction ===
|
|
90
|
+
direction === 'down' ? `0px 0px ${threshold}px 0px` : `${threshold}px 0px 0px 0px`,
|
|
91
91
|
threshold: 0,
|
|
92
92
|
})
|
|
93
93
|
observer.observe(sentinel)
|
package/src/useIntersection.ts
CHANGED
package/src/useInterval.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onMount } from
|
|
1
|
+
import { onMount } from '@pyreon/core'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* In Pyreon there is no SSR warning distinction between effect and
|
|
@@ -14,6 +14,6 @@ import { onMount } from "@pyreon/core"
|
|
|
14
14
|
export type UseIsomorphicLayoutEffect = typeof onMount
|
|
15
15
|
|
|
16
16
|
const useIsomorphicLayoutEffect: UseIsomorphicLayoutEffect =
|
|
17
|
-
typeof window !==
|
|
17
|
+
typeof window !== 'undefined' ? onMount : onMount
|
|
18
18
|
|
|
19
19
|
export default useIsomorphicLayoutEffect
|
package/src/useKeyboard.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onMount, onUnmount } from
|
|
1
|
+
import { onMount, onUnmount } from '@pyreon/core'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Listen for a specific key press.
|
|
@@ -6,9 +6,9 @@ import { onMount, onUnmount } from "@pyreon/core"
|
|
|
6
6
|
export function useKeyboard(
|
|
7
7
|
key: string,
|
|
8
8
|
handler: (event: KeyboardEvent) => void,
|
|
9
|
-
options?: { event?:
|
|
9
|
+
options?: { event?: 'keydown' | 'keyup'; target?: EventTarget },
|
|
10
10
|
): void {
|
|
11
|
-
const eventName = options?.event ??
|
|
11
|
+
const eventName = options?.event ?? 'keydown'
|
|
12
12
|
|
|
13
13
|
const listener = (e: Event) => {
|
|
14
14
|
const ke = e as KeyboardEvent
|
package/src/useMediaQuery.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { onMount, onUnmount } from
|
|
2
|
-
import { signal } from
|
|
1
|
+
import { onMount, onUnmount } from '@pyreon/core'
|
|
2
|
+
import { signal } from '@pyreon/reactivity'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Subscribe to a CSS media query, returns a reactive boolean.
|
|
@@ -15,12 +15,12 @@ export function useMediaQuery(query: string): () => boolean {
|
|
|
15
15
|
onMount(() => {
|
|
16
16
|
mql = window.matchMedia(query)
|
|
17
17
|
matches.set(mql.matches)
|
|
18
|
-
mql.addEventListener(
|
|
18
|
+
mql.addEventListener('change', onChange)
|
|
19
19
|
return undefined
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
onUnmount(() => {
|
|
23
|
-
mql?.removeEventListener(
|
|
23
|
+
mql?.removeEventListener('change', onChange)
|
|
24
24
|
})
|
|
25
25
|
|
|
26
26
|
return matches
|
package/src/useMergedRef.ts
CHANGED
|
@@ -12,7 +12,7 @@ export const useMergedRef = <T>(...refs: (Ref<T> | undefined)[]): ((node: T | nu
|
|
|
12
12
|
return (node: T | null) => {
|
|
13
13
|
for (const ref of refs) {
|
|
14
14
|
if (!ref) continue
|
|
15
|
-
if (typeof ref ===
|
|
15
|
+
if (typeof ref === 'function') {
|
|
16
16
|
ref(node)
|
|
17
17
|
} else {
|
|
18
18
|
;(ref as RefObject<unknown>).current = node
|
package/src/useOnline.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { onCleanup, signal } from
|
|
1
|
+
import { onCleanup, signal } from '@pyreon/reactivity'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Reactive online/offline status.
|
|
@@ -13,17 +13,17 @@ import { onCleanup, signal } from "@pyreon/reactivity"
|
|
|
13
13
|
* ```
|
|
14
14
|
*/
|
|
15
15
|
export function useOnline(): () => boolean {
|
|
16
|
-
const isBrowser = typeof window !==
|
|
16
|
+
const isBrowser = typeof window !== 'undefined'
|
|
17
17
|
const online = signal(isBrowser ? navigator.onLine : true)
|
|
18
18
|
|
|
19
19
|
if (isBrowser) {
|
|
20
20
|
const setOnline = () => online.set(true)
|
|
21
21
|
const setOffline = () => online.set(false)
|
|
22
|
-
window.addEventListener(
|
|
23
|
-
window.addEventListener(
|
|
22
|
+
window.addEventListener('online', setOnline)
|
|
23
|
+
window.addEventListener('offline', setOffline)
|
|
24
24
|
onCleanup(() => {
|
|
25
|
-
window.removeEventListener(
|
|
26
|
-
window.removeEventListener(
|
|
25
|
+
window.removeEventListener('online', setOnline)
|
|
26
|
+
window.removeEventListener('offline', setOffline)
|
|
27
27
|
})
|
|
28
28
|
}
|
|
29
29
|
|
package/src/usePrevious.ts
CHANGED
package/src/useReducedMotion.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { useMediaQuery } from
|
|
1
|
+
import { useMediaQuery } from './useMediaQuery'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Returns true when the user prefers reduced motion.
|
|
5
5
|
*/
|
|
6
6
|
export function useReducedMotion(): () => boolean {
|
|
7
|
-
return useMediaQuery(
|
|
7
|
+
return useMediaQuery('(prefers-reduced-motion: reduce)')
|
|
8
8
|
}
|
package/src/useRootSize.ts
CHANGED
package/src/useScrollLock.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { onUnmount } from
|
|
1
|
+
import { onUnmount } from '@pyreon/core'
|
|
2
2
|
|
|
3
3
|
let lockCount = 0
|
|
4
|
-
let savedOverflow =
|
|
4
|
+
let savedOverflow = ''
|
|
5
5
|
|
|
6
6
|
/**
|
|
7
7
|
* Lock page scroll. Uses reference counting for concurrent locks.
|
|
@@ -15,7 +15,7 @@ export function useScrollLock(): { lock: () => void; unlock: () => void } {
|
|
|
15
15
|
isLocked = true
|
|
16
16
|
if (lockCount === 0) {
|
|
17
17
|
savedOverflow = document.body.style.overflow
|
|
18
|
-
document.body.style.overflow =
|
|
18
|
+
document.body.style.overflow = 'hidden'
|
|
19
19
|
}
|
|
20
20
|
lockCount++
|
|
21
21
|
}
|
package/src/useSpacing.ts
CHANGED
package/src/useThemeValue.ts
CHANGED
package/src/useTimeAgo.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { onCleanup, signal } from
|
|
1
|
+
import { onCleanup, signal } from '@pyreon/reactivity'
|
|
2
2
|
|
|
3
|
-
type TimeUnit =
|
|
3
|
+
type TimeUnit = 'second' | 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
|
|
4
4
|
|
|
5
5
|
interface TimeInterval {
|
|
6
6
|
unit: TimeUnit
|
|
@@ -8,13 +8,13 @@ interface TimeInterval {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
const INTERVALS: TimeInterval[] = [
|
|
11
|
-
{ unit:
|
|
12
|
-
{ unit:
|
|
13
|
-
{ unit:
|
|
14
|
-
{ unit:
|
|
15
|
-
{ unit:
|
|
16
|
-
{ unit:
|
|
17
|
-
{ unit:
|
|
11
|
+
{ unit: 'year', seconds: 31536000 },
|
|
12
|
+
{ unit: 'month', seconds: 2592000 },
|
|
13
|
+
{ unit: 'week', seconds: 604800 },
|
|
14
|
+
{ unit: 'day', seconds: 86400 },
|
|
15
|
+
{ unit: 'hour', seconds: 3600 },
|
|
16
|
+
{ unit: 'minute', seconds: 60 },
|
|
17
|
+
{ unit: 'second', seconds: 1 },
|
|
18
18
|
]
|
|
19
19
|
|
|
20
20
|
/**
|
|
@@ -40,7 +40,7 @@ export interface UseTimeAgoOptions {
|
|
|
40
40
|
*/
|
|
41
41
|
const defaultFormatter = (() => {
|
|
42
42
|
const rtf =
|
|
43
|
-
typeof Intl !==
|
|
43
|
+
typeof Intl !== 'undefined' ? new Intl.RelativeTimeFormat('en', { numeric: 'auto' }) : undefined
|
|
44
44
|
|
|
45
45
|
return (value: number, unit: TimeUnit, isPast: boolean): string => {
|
|
46
46
|
if (rtf) return rtf.format(isPast ? -value : value, unit)
|
|
@@ -58,19 +58,19 @@ function computeTimeAgo(
|
|
|
58
58
|
formatter: (value: number, unit: TimeUnit, isPast: boolean) => string,
|
|
59
59
|
): string {
|
|
60
60
|
const now = Date.now()
|
|
61
|
-
const target = typeof date ===
|
|
61
|
+
const target = typeof date === 'number' ? date : date.getTime()
|
|
62
62
|
const diff = Math.abs(now - target)
|
|
63
63
|
const diffSeconds = Math.floor(diff / 1000)
|
|
64
64
|
const isPast = target < now
|
|
65
65
|
|
|
66
|
-
if (diffSeconds < 5) return
|
|
66
|
+
if (diffSeconds < 5) return 'just now'
|
|
67
67
|
|
|
68
68
|
for (const { unit, seconds } of INTERVALS) {
|
|
69
69
|
const value = Math.floor(diffSeconds / seconds)
|
|
70
70
|
if (value >= 1) return formatter(value, unit, isPast)
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
-
return
|
|
73
|
+
return 'just now'
|
|
74
74
|
}
|
|
75
75
|
|
|
76
76
|
/**
|
|
@@ -99,7 +99,7 @@ export function useTimeAgo(
|
|
|
99
99
|
options?: UseTimeAgoOptions,
|
|
100
100
|
): () => string {
|
|
101
101
|
const formatter = options?.formatter ?? defaultFormatter
|
|
102
|
-
const resolveDate = typeof date ===
|
|
102
|
+
const resolveDate = typeof date === 'function' ? date : () => date
|
|
103
103
|
|
|
104
104
|
const result = signal(computeTimeAgo(resolveDate(), formatter))
|
|
105
105
|
|
|
@@ -116,7 +116,7 @@ export function useTimeAgo(
|
|
|
116
116
|
result.set(computeTimeAgo(d, formatter))
|
|
117
117
|
|
|
118
118
|
// Schedule next update with adaptive interval
|
|
119
|
-
const target = typeof d ===
|
|
119
|
+
const target = typeof d === 'number' ? d : d.getTime()
|
|
120
120
|
const diffSeconds = Math.floor(Math.abs(Date.now() - target) / 1000)
|
|
121
121
|
const interval = options?.interval ?? getRefreshInterval(diffSeconds)
|
|
122
122
|
timer = setTimeout(tick, interval)
|
package/src/useTimeout.ts
CHANGED
package/src/useToggle.ts
CHANGED
package/src/useUpdateEffect.ts
CHANGED
package/src/useWindowResize.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { onMount, onUnmount } from
|
|
2
|
-
import { signal } from
|
|
1
|
+
import { onMount, onUnmount } from '@pyreon/core'
|
|
2
|
+
import { signal } from '@pyreon/reactivity'
|
|
3
3
|
|
|
4
4
|
export interface WindowSize {
|
|
5
5
|
width: number
|
|
@@ -11,8 +11,8 @@ export interface WindowSize {
|
|
|
11
11
|
*/
|
|
12
12
|
export function useWindowResize(throttleMs = 200): () => WindowSize {
|
|
13
13
|
const size = signal<WindowSize>({
|
|
14
|
-
width: typeof window !==
|
|
15
|
-
height: typeof window !==
|
|
14
|
+
width: typeof window !== 'undefined' ? window.innerWidth : 0,
|
|
15
|
+
height: typeof window !== 'undefined' ? window.innerHeight : 0,
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
let timer: ReturnType<typeof setTimeout> | undefined
|
|
@@ -26,12 +26,12 @@ export function useWindowResize(throttleMs = 200): () => WindowSize {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
onMount(() => {
|
|
29
|
-
window.addEventListener(
|
|
29
|
+
window.addEventListener('resize', onResize)
|
|
30
30
|
return undefined
|
|
31
31
|
})
|
|
32
32
|
|
|
33
33
|
onUnmount(() => {
|
|
34
|
-
window.removeEventListener(
|
|
34
|
+
window.removeEventListener('resize', onResize)
|
|
35
35
|
if (timer !== undefined) clearTimeout(timer)
|
|
36
36
|
})
|
|
37
37
|
|