@pyreon/hooks 0.11.4 → 0.11.6
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 +6 -6
- package/lib/index.js +4 -6
- 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 +3 -4
- 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 +3 -4
- package/src/useToggle.ts +1 -1
- package/src/useUpdateEffect.ts +2 -2
- package/src/useWindowResize.ts +6 -6
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
let mountCallbacks: Array<() => unknown> = []
|
|
4
4
|
let unmountCallbacks: Array<() => void> = []
|
|
5
5
|
|
|
6
|
-
vi.mock(
|
|
6
|
+
vi.mock('@pyreon/core', () => ({
|
|
7
7
|
onMount: (fn: () => unknown) => {
|
|
8
8
|
mountCallbacks.push(fn)
|
|
9
9
|
},
|
|
@@ -12,9 +12,9 @@ vi.mock("@pyreon/core", () => ({
|
|
|
12
12
|
},
|
|
13
13
|
}))
|
|
14
14
|
|
|
15
|
-
import { useMediaQuery } from
|
|
15
|
+
import { useMediaQuery } from '../useMediaQuery'
|
|
16
16
|
|
|
17
|
-
describe(
|
|
17
|
+
describe('useMediaQuery', () => {
|
|
18
18
|
let listeners: Map<string, (e: MediaQueryListEvent) => void>
|
|
19
19
|
let matchMediaMock: ReturnType<typeof vi.fn>
|
|
20
20
|
|
|
@@ -28,10 +28,10 @@ describe("useMediaQuery", () => {
|
|
|
28
28
|
matches: false,
|
|
29
29
|
media: query,
|
|
30
30
|
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
31
|
-
if (event ===
|
|
31
|
+
if (event === 'change') listeners.set(query, cb)
|
|
32
32
|
}),
|
|
33
33
|
removeEventListener: vi.fn((event: string) => {
|
|
34
|
-
if (event ===
|
|
34
|
+
if (event === 'change') listeners.delete(query)
|
|
35
35
|
}),
|
|
36
36
|
dispatchEvent: vi.fn(),
|
|
37
37
|
onchange: null,
|
|
@@ -41,66 +41,66 @@ describe("useMediaQuery", () => {
|
|
|
41
41
|
return mql
|
|
42
42
|
})
|
|
43
43
|
|
|
44
|
-
Object.defineProperty(window,
|
|
44
|
+
Object.defineProperty(window, 'matchMedia', {
|
|
45
45
|
writable: true,
|
|
46
46
|
value: matchMediaMock,
|
|
47
47
|
})
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
-
it(
|
|
51
|
-
const matches = useMediaQuery(
|
|
50
|
+
it('returns false initially before mount', () => {
|
|
51
|
+
const matches = useMediaQuery('(min-width: 768px)')
|
|
52
52
|
expect(matches()).toBe(false)
|
|
53
53
|
})
|
|
54
54
|
|
|
55
|
-
it(
|
|
55
|
+
it('returns the current match state after mount', () => {
|
|
56
56
|
matchMediaMock.mockReturnValue({
|
|
57
57
|
matches: true,
|
|
58
|
-
media:
|
|
58
|
+
media: '(min-width: 768px)',
|
|
59
59
|
addEventListener: vi.fn(),
|
|
60
60
|
removeEventListener: vi.fn(),
|
|
61
61
|
})
|
|
62
62
|
|
|
63
|
-
const matches = useMediaQuery(
|
|
63
|
+
const matches = useMediaQuery('(min-width: 768px)')
|
|
64
64
|
mountCallbacks.forEach((cb) => {
|
|
65
65
|
cb()
|
|
66
66
|
})
|
|
67
67
|
expect(matches()).toBe(true)
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
-
it(
|
|
70
|
+
it('updates when media query changes', () => {
|
|
71
71
|
const mql = {
|
|
72
72
|
matches: false,
|
|
73
|
-
media:
|
|
73
|
+
media: '(min-width: 768px)',
|
|
74
74
|
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
75
|
-
if (event ===
|
|
75
|
+
if (event === 'change') listeners.set('(min-width: 768px)', cb)
|
|
76
76
|
}),
|
|
77
77
|
removeEventListener: vi.fn(),
|
|
78
78
|
}
|
|
79
79
|
matchMediaMock.mockReturnValue(mql)
|
|
80
80
|
|
|
81
|
-
const matches = useMediaQuery(
|
|
81
|
+
const matches = useMediaQuery('(min-width: 768px)')
|
|
82
82
|
mountCallbacks.forEach((cb) => {
|
|
83
83
|
cb()
|
|
84
84
|
})
|
|
85
85
|
expect(matches()).toBe(false)
|
|
86
86
|
|
|
87
87
|
// Simulate media query change
|
|
88
|
-
const changeListener = listeners.get(
|
|
88
|
+
const changeListener = listeners.get('(min-width: 768px)')
|
|
89
89
|
changeListener?.({ matches: true } as MediaQueryListEvent)
|
|
90
90
|
expect(matches()).toBe(true)
|
|
91
91
|
})
|
|
92
92
|
|
|
93
|
-
it(
|
|
93
|
+
it('removes listener on unmount', () => {
|
|
94
94
|
const removeEventListenerSpy = vi.fn()
|
|
95
95
|
const mql = {
|
|
96
96
|
matches: false,
|
|
97
|
-
media:
|
|
97
|
+
media: '(min-width: 768px)',
|
|
98
98
|
addEventListener: vi.fn(),
|
|
99
99
|
removeEventListener: removeEventListenerSpy,
|
|
100
100
|
}
|
|
101
101
|
matchMediaMock.mockReturnValue(mql)
|
|
102
102
|
|
|
103
|
-
useMediaQuery(
|
|
103
|
+
useMediaQuery('(min-width: 768px)')
|
|
104
104
|
mountCallbacks.forEach((cb) => {
|
|
105
105
|
cb()
|
|
106
106
|
})
|
|
@@ -108,35 +108,35 @@ describe("useMediaQuery", () => {
|
|
|
108
108
|
cb()
|
|
109
109
|
})
|
|
110
110
|
|
|
111
|
-
expect(removeEventListenerSpy).toHaveBeenCalledWith(
|
|
111
|
+
expect(removeEventListenerSpy).toHaveBeenCalledWith('change', expect.any(Function))
|
|
112
112
|
})
|
|
113
113
|
|
|
114
|
-
it(
|
|
115
|
-
useMediaQuery(
|
|
114
|
+
it('passes the correct query string to matchMedia', () => {
|
|
115
|
+
useMediaQuery('(prefers-color-scheme: dark)')
|
|
116
116
|
mountCallbacks.forEach((cb) => {
|
|
117
117
|
cb()
|
|
118
118
|
})
|
|
119
|
-
expect(matchMediaMock).toHaveBeenCalledWith(
|
|
119
|
+
expect(matchMediaMock).toHaveBeenCalledWith('(prefers-color-scheme: dark)')
|
|
120
120
|
})
|
|
121
121
|
|
|
122
|
-
it(
|
|
122
|
+
it('updates from true to false on change', () => {
|
|
123
123
|
const mql = {
|
|
124
124
|
matches: true,
|
|
125
|
-
media:
|
|
125
|
+
media: '(min-width: 768px)',
|
|
126
126
|
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
127
|
-
if (event ===
|
|
127
|
+
if (event === 'change') listeners.set('q', cb)
|
|
128
128
|
}),
|
|
129
129
|
removeEventListener: vi.fn(),
|
|
130
130
|
}
|
|
131
131
|
matchMediaMock.mockReturnValue(mql)
|
|
132
132
|
|
|
133
|
-
const matches = useMediaQuery(
|
|
133
|
+
const matches = useMediaQuery('(min-width: 768px)')
|
|
134
134
|
mountCallbacks.forEach((cb) => {
|
|
135
135
|
cb()
|
|
136
136
|
})
|
|
137
137
|
expect(matches()).toBe(true)
|
|
138
138
|
|
|
139
|
-
const changeListener = listeners.get(
|
|
139
|
+
const changeListener = listeners.get('q')
|
|
140
140
|
changeListener?.({ matches: false } as MediaQueryListEvent)
|
|
141
141
|
expect(matches()).toBe(false)
|
|
142
142
|
})
|
|
@@ -1,44 +1,44 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from
|
|
2
|
-
import { useMergedRef } from
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
|
+
import { useMergedRef } from '../useMergedRef'
|
|
3
3
|
|
|
4
|
-
describe(
|
|
5
|
-
it(
|
|
4
|
+
describe('useMergedRef', () => {
|
|
5
|
+
it('sets object refs', () => {
|
|
6
6
|
const ref1 = { current: null as HTMLDivElement | null }
|
|
7
7
|
const ref2 = { current: null as HTMLDivElement | null }
|
|
8
8
|
|
|
9
9
|
const merged = useMergedRef(ref1, ref2)
|
|
10
10
|
|
|
11
|
-
const node = document.createElement(
|
|
11
|
+
const node = document.createElement('div')
|
|
12
12
|
merged(node)
|
|
13
13
|
|
|
14
14
|
expect(ref1.current).toBe(node)
|
|
15
15
|
expect(ref2.current).toBe(node)
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
it(
|
|
18
|
+
it('calls callback refs', () => {
|
|
19
19
|
const cb = vi.fn()
|
|
20
20
|
const objRef = { current: null as HTMLDivElement | null }
|
|
21
21
|
|
|
22
22
|
const merged = useMergedRef(cb, objRef)
|
|
23
23
|
|
|
24
|
-
const node = document.createElement(
|
|
24
|
+
const node = document.createElement('div')
|
|
25
25
|
merged(node)
|
|
26
26
|
|
|
27
27
|
expect(cb).toHaveBeenCalledWith(node)
|
|
28
28
|
expect(objRef.current).toBe(node)
|
|
29
29
|
})
|
|
30
30
|
|
|
31
|
-
it(
|
|
31
|
+
it('skips undefined refs', () => {
|
|
32
32
|
const ref = { current: null as HTMLDivElement | null }
|
|
33
33
|
const merged = useMergedRef(undefined, ref)
|
|
34
34
|
|
|
35
|
-
const node = document.createElement(
|
|
35
|
+
const node = document.createElement('div')
|
|
36
36
|
merged(node)
|
|
37
37
|
|
|
38
38
|
expect(ref.current).toBe(node)
|
|
39
39
|
})
|
|
40
40
|
|
|
41
|
-
it(
|
|
41
|
+
it('handles null node (unmount)', () => {
|
|
42
42
|
const cb = vi.fn()
|
|
43
43
|
const merged = useMergedRef(cb)
|
|
44
44
|
|
|
@@ -1,15 +1,15 @@
|
|
|
1
|
-
import { signal } from
|
|
2
|
-
import { describe, expect, it } from
|
|
3
|
-
import { usePrevious } from
|
|
1
|
+
import { signal } from '@pyreon/reactivity'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
import { usePrevious } from '../usePrevious'
|
|
4
4
|
|
|
5
|
-
describe(
|
|
6
|
-
it(
|
|
5
|
+
describe('usePrevious', () => {
|
|
6
|
+
it('returns undefined initially', () => {
|
|
7
7
|
const source = signal(1)
|
|
8
8
|
const prev = usePrevious(source)
|
|
9
9
|
expect(prev()).toBeUndefined()
|
|
10
10
|
})
|
|
11
11
|
|
|
12
|
-
it(
|
|
12
|
+
it('returns the previous value after source changes', () => {
|
|
13
13
|
const source = signal(1)
|
|
14
14
|
const prev = usePrevious(source)
|
|
15
15
|
expect(prev()).toBeUndefined()
|
|
@@ -18,22 +18,22 @@ describe("usePrevious", () => {
|
|
|
18
18
|
expect(prev()).toBe(1)
|
|
19
19
|
})
|
|
20
20
|
|
|
21
|
-
it(
|
|
22
|
-
const source = signal(
|
|
21
|
+
it('tracks multiple changes', () => {
|
|
22
|
+
const source = signal('a')
|
|
23
23
|
const prev = usePrevious(source)
|
|
24
24
|
expect(prev()).toBeUndefined()
|
|
25
25
|
|
|
26
|
-
source.set(
|
|
27
|
-
expect(prev()).toBe(
|
|
26
|
+
source.set('b')
|
|
27
|
+
expect(prev()).toBe('a')
|
|
28
28
|
|
|
29
|
-
source.set(
|
|
30
|
-
expect(prev()).toBe(
|
|
29
|
+
source.set('c')
|
|
30
|
+
expect(prev()).toBe('b')
|
|
31
31
|
|
|
32
|
-
source.set(
|
|
33
|
-
expect(prev()).toBe(
|
|
32
|
+
source.set('d')
|
|
33
|
+
expect(prev()).toBe('c')
|
|
34
34
|
})
|
|
35
35
|
|
|
36
|
-
it(
|
|
36
|
+
it('works with number values', () => {
|
|
37
37
|
const source = signal(10)
|
|
38
38
|
const prev = usePrevious(source)
|
|
39
39
|
expect(prev()).toBeUndefined()
|
|
@@ -45,7 +45,7 @@ describe("usePrevious", () => {
|
|
|
45
45
|
expect(prev()).toBe(20)
|
|
46
46
|
})
|
|
47
47
|
|
|
48
|
-
it(
|
|
48
|
+
it('works with object values', () => {
|
|
49
49
|
const obj1 = { x: 1 }
|
|
50
50
|
const obj2 = { x: 2 }
|
|
51
51
|
const source = signal(obj1)
|
|
@@ -56,15 +56,15 @@ describe("usePrevious", () => {
|
|
|
56
56
|
expect(prev()).toBe(obj1)
|
|
57
57
|
})
|
|
58
58
|
|
|
59
|
-
it(
|
|
60
|
-
const source = signal<string | null>(
|
|
59
|
+
it('works with null values', () => {
|
|
60
|
+
const source = signal<string | null>('hello')
|
|
61
61
|
const prev = usePrevious(source)
|
|
62
62
|
expect(prev()).toBeUndefined()
|
|
63
63
|
|
|
64
64
|
source.set(null)
|
|
65
|
-
expect(prev()).toBe(
|
|
65
|
+
expect(prev()).toBe('hello')
|
|
66
66
|
|
|
67
|
-
source.set(
|
|
67
|
+
source.set('world')
|
|
68
68
|
expect(prev()).toBeNull()
|
|
69
69
|
})
|
|
70
70
|
})
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { beforeEach, describe, expect, it, vi } from
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
let mountCallbacks: Array<() => unknown> = []
|
|
4
4
|
let unmountCallbacks: Array<() => void> = []
|
|
5
5
|
|
|
6
|
-
vi.mock(
|
|
6
|
+
vi.mock('@pyreon/core', () => ({
|
|
7
7
|
onMount: (fn: () => unknown) => {
|
|
8
8
|
mountCallbacks.push(fn)
|
|
9
9
|
},
|
|
@@ -12,9 +12,9 @@ vi.mock("@pyreon/core", () => ({
|
|
|
12
12
|
},
|
|
13
13
|
}))
|
|
14
14
|
|
|
15
|
-
import { useReducedMotion } from
|
|
15
|
+
import { useReducedMotion } from '../useReducedMotion'
|
|
16
16
|
|
|
17
|
-
describe(
|
|
17
|
+
describe('useReducedMotion', () => {
|
|
18
18
|
let changeListeners: Map<string, (e: MediaQueryListEvent) => void>
|
|
19
19
|
|
|
20
20
|
beforeEach(() => {
|
|
@@ -22,20 +22,20 @@ describe("useReducedMotion", () => {
|
|
|
22
22
|
unmountCallbacks = []
|
|
23
23
|
changeListeners = new Map()
|
|
24
24
|
|
|
25
|
-
Object.defineProperty(window,
|
|
25
|
+
Object.defineProperty(window, 'matchMedia', {
|
|
26
26
|
writable: true,
|
|
27
27
|
value: vi.fn((query: string) => ({
|
|
28
28
|
matches: false,
|
|
29
29
|
media: query,
|
|
30
30
|
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
31
|
-
if (event ===
|
|
31
|
+
if (event === 'change') changeListeners.set(query, cb)
|
|
32
32
|
}),
|
|
33
33
|
removeEventListener: vi.fn(),
|
|
34
34
|
})),
|
|
35
35
|
})
|
|
36
36
|
})
|
|
37
37
|
|
|
38
|
-
it(
|
|
38
|
+
it('returns false when no motion preference', () => {
|
|
39
39
|
const reduced = useReducedMotion()
|
|
40
40
|
mountCallbacks.forEach((cb) => {
|
|
41
41
|
cb()
|
|
@@ -43,14 +43,14 @@ describe("useReducedMotion", () => {
|
|
|
43
43
|
expect(reduced()).toBe(false)
|
|
44
44
|
})
|
|
45
45
|
|
|
46
|
-
it(
|
|
47
|
-
Object.defineProperty(window,
|
|
46
|
+
it('returns true when reduced motion is preferred', () => {
|
|
47
|
+
Object.defineProperty(window, 'matchMedia', {
|
|
48
48
|
writable: true,
|
|
49
49
|
value: vi.fn((query: string) => ({
|
|
50
50
|
matches: true,
|
|
51
51
|
media: query,
|
|
52
52
|
addEventListener: vi.fn((event: string, cb: (e: MediaQueryListEvent) => void) => {
|
|
53
|
-
if (event ===
|
|
53
|
+
if (event === 'change') changeListeners.set(query, cb)
|
|
54
54
|
}),
|
|
55
55
|
removeEventListener: vi.fn(),
|
|
56
56
|
})),
|
|
@@ -63,26 +63,26 @@ describe("useReducedMotion", () => {
|
|
|
63
63
|
expect(reduced()).toBe(true)
|
|
64
64
|
})
|
|
65
65
|
|
|
66
|
-
it(
|
|
66
|
+
it('updates when preference changes', () => {
|
|
67
67
|
const reduced = useReducedMotion()
|
|
68
68
|
mountCallbacks.forEach((cb) => {
|
|
69
69
|
cb()
|
|
70
70
|
})
|
|
71
71
|
expect(reduced()).toBe(false)
|
|
72
72
|
|
|
73
|
-
const listener = changeListeners.get(
|
|
73
|
+
const listener = changeListeners.get('(prefers-reduced-motion: reduce)')
|
|
74
74
|
listener?.({ matches: true } as MediaQueryListEvent)
|
|
75
75
|
expect(reduced()).toBe(true)
|
|
76
76
|
})
|
|
77
77
|
|
|
78
|
-
it(
|
|
78
|
+
it('queries the correct media string', () => {
|
|
79
79
|
const matchMediaSpy = vi.fn((query: string) => ({
|
|
80
80
|
matches: false,
|
|
81
81
|
media: query,
|
|
82
82
|
addEventListener: vi.fn(),
|
|
83
83
|
removeEventListener: vi.fn(),
|
|
84
84
|
}))
|
|
85
|
-
Object.defineProperty(window,
|
|
85
|
+
Object.defineProperty(window, 'matchMedia', {
|
|
86
86
|
writable: true,
|
|
87
87
|
value: matchMediaSpy,
|
|
88
88
|
})
|
|
@@ -91,6 +91,6 @@ describe("useReducedMotion", () => {
|
|
|
91
91
|
mountCallbacks.forEach((cb) => {
|
|
92
92
|
cb()
|
|
93
93
|
})
|
|
94
|
-
expect(matchMediaSpy).toHaveBeenCalledWith(
|
|
94
|
+
expect(matchMediaSpy).toHaveBeenCalledWith('(prefers-reduced-motion: reduce)')
|
|
95
95
|
})
|
|
96
96
|
})
|
|
@@ -1,25 +1,25 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
// Mock @pyreon/styler to provide theme values
|
|
4
|
-
vi.mock(
|
|
4
|
+
vi.mock('@pyreon/styler', () => ({
|
|
5
5
|
useTheme: () => ({}),
|
|
6
6
|
}))
|
|
7
7
|
|
|
8
|
-
import { useRootSize } from
|
|
8
|
+
import { useRootSize } from '../useRootSize'
|
|
9
9
|
|
|
10
|
-
describe(
|
|
11
|
-
it(
|
|
10
|
+
describe('useRootSize', () => {
|
|
11
|
+
it('defaults rootSize to 16', () => {
|
|
12
12
|
const result = useRootSize()
|
|
13
13
|
expect(result.rootSize).toBe(16)
|
|
14
14
|
})
|
|
15
15
|
|
|
16
|
-
it(
|
|
16
|
+
it('pxToRem converts correctly with default rootSize', () => {
|
|
17
17
|
const result = useRootSize()
|
|
18
|
-
expect(result.pxToRem(32)).toBe(
|
|
19
|
-
expect(result.pxToRem(8)).toBe(
|
|
18
|
+
expect(result.pxToRem(32)).toBe('2rem')
|
|
19
|
+
expect(result.pxToRem(8)).toBe('0.5rem')
|
|
20
20
|
})
|
|
21
21
|
|
|
22
|
-
it(
|
|
22
|
+
it('remToPx converts correctly with default rootSize', () => {
|
|
23
23
|
const result = useRootSize()
|
|
24
24
|
expect(result.remToPx(2)).toBe(32)
|
|
25
25
|
expect(result.remToPx(0.5)).toBe(8)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
|
-
vi.mock(
|
|
3
|
+
vi.mock('@pyreon/core', () => ({
|
|
4
4
|
onMount: (_fn: () => unknown) => {
|
|
5
5
|
/* no-op */
|
|
6
6
|
},
|
|
@@ -9,19 +9,19 @@ vi.mock("@pyreon/core", () => ({
|
|
|
9
9
|
},
|
|
10
10
|
}))
|
|
11
11
|
|
|
12
|
-
describe(
|
|
12
|
+
describe('useScrollLock', () => {
|
|
13
13
|
beforeEach(async () => {
|
|
14
14
|
vi.resetModules()
|
|
15
|
-
document.body.style.overflow =
|
|
15
|
+
document.body.style.overflow = ''
|
|
16
16
|
})
|
|
17
17
|
|
|
18
18
|
afterEach(() => {
|
|
19
|
-
document.body.style.overflow =
|
|
19
|
+
document.body.style.overflow = ''
|
|
20
20
|
})
|
|
21
21
|
|
|
22
22
|
async function getUseScrollLock() {
|
|
23
23
|
// Re-mock after resetModules
|
|
24
|
-
vi.doMock(
|
|
24
|
+
vi.doMock('@pyreon/core', () => ({
|
|
25
25
|
onMount: (_fn: () => unknown) => {
|
|
26
26
|
/* no-op */
|
|
27
27
|
},
|
|
@@ -29,76 +29,76 @@ describe("useScrollLock", () => {
|
|
|
29
29
|
/* no-op */
|
|
30
30
|
},
|
|
31
31
|
}))
|
|
32
|
-
const mod = await import(
|
|
32
|
+
const mod = await import('../useScrollLock')
|
|
33
33
|
return mod.useScrollLock
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
-
it(
|
|
36
|
+
it('sets overflow to hidden when locked', async () => {
|
|
37
37
|
const useScrollLock = await getUseScrollLock()
|
|
38
38
|
const { lock } = useScrollLock()
|
|
39
39
|
lock()
|
|
40
|
-
expect(document.body.style.overflow).toBe(
|
|
40
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
41
41
|
})
|
|
42
42
|
|
|
43
|
-
it(
|
|
43
|
+
it('does not change overflow until lock is called', async () => {
|
|
44
44
|
const useScrollLock = await getUseScrollLock()
|
|
45
|
-
document.body.style.overflow =
|
|
45
|
+
document.body.style.overflow = 'auto'
|
|
46
46
|
useScrollLock()
|
|
47
|
-
expect(document.body.style.overflow).toBe(
|
|
47
|
+
expect(document.body.style.overflow).toBe('auto')
|
|
48
48
|
})
|
|
49
49
|
|
|
50
|
-
it(
|
|
50
|
+
it('restores overflow when unlocked', async () => {
|
|
51
51
|
const useScrollLock = await getUseScrollLock()
|
|
52
|
-
document.body.style.overflow =
|
|
52
|
+
document.body.style.overflow = 'auto'
|
|
53
53
|
const { lock, unlock } = useScrollLock()
|
|
54
54
|
lock()
|
|
55
|
-
expect(document.body.style.overflow).toBe(
|
|
55
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
56
56
|
unlock()
|
|
57
|
-
expect(document.body.style.overflow).toBe(
|
|
57
|
+
expect(document.body.style.overflow).toBe('auto')
|
|
58
58
|
})
|
|
59
59
|
|
|
60
|
-
it(
|
|
60
|
+
it('lock is idempotent', async () => {
|
|
61
61
|
const useScrollLock = await getUseScrollLock()
|
|
62
62
|
const { lock, unlock } = useScrollLock()
|
|
63
63
|
lock()
|
|
64
64
|
lock() // second call should be no-op
|
|
65
|
-
expect(document.body.style.overflow).toBe(
|
|
65
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
66
66
|
unlock()
|
|
67
|
-
expect(document.body.style.overflow).toBe(
|
|
67
|
+
expect(document.body.style.overflow).toBe('')
|
|
68
68
|
})
|
|
69
69
|
|
|
70
|
-
it(
|
|
70
|
+
it('unlock is idempotent', async () => {
|
|
71
71
|
const useScrollLock = await getUseScrollLock()
|
|
72
72
|
const { lock, unlock } = useScrollLock()
|
|
73
73
|
lock()
|
|
74
74
|
unlock()
|
|
75
75
|
unlock() // second call should be no-op
|
|
76
|
-
expect(document.body.style.overflow).toBe(
|
|
76
|
+
expect(document.body.style.overflow).toBe('')
|
|
77
77
|
})
|
|
78
78
|
|
|
79
|
-
it(
|
|
79
|
+
it('handles concurrent locks with reference counting', async () => {
|
|
80
80
|
const useScrollLock = await getUseScrollLock()
|
|
81
81
|
const lock1 = useScrollLock()
|
|
82
82
|
const lock2 = useScrollLock()
|
|
83
83
|
|
|
84
84
|
lock1.lock()
|
|
85
|
-
expect(document.body.style.overflow).toBe(
|
|
85
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
86
86
|
|
|
87
87
|
lock2.lock()
|
|
88
|
-
expect(document.body.style.overflow).toBe(
|
|
88
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
89
89
|
|
|
90
90
|
// Unlocking first should not restore (still 1 active)
|
|
91
91
|
lock1.unlock()
|
|
92
|
-
expect(document.body.style.overflow).toBe(
|
|
92
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
93
93
|
|
|
94
94
|
// Unlocking second should restore
|
|
95
95
|
lock2.unlock()
|
|
96
|
-
expect(document.body.style.overflow).toBe(
|
|
96
|
+
expect(document.body.style.overflow).toBe('')
|
|
97
97
|
})
|
|
98
98
|
|
|
99
|
-
it(
|
|
99
|
+
it('preserves original overflow across concurrent locks', async () => {
|
|
100
100
|
const useScrollLock = await getUseScrollLock()
|
|
101
|
-
document.body.style.overflow =
|
|
101
|
+
document.body.style.overflow = 'scroll'
|
|
102
102
|
|
|
103
103
|
const lock1 = useScrollLock()
|
|
104
104
|
const lock2 = useScrollLock()
|
|
@@ -107,17 +107,17 @@ describe("useScrollLock", () => {
|
|
|
107
107
|
lock2.lock()
|
|
108
108
|
|
|
109
109
|
lock1.unlock()
|
|
110
|
-
expect(document.body.style.overflow).toBe(
|
|
110
|
+
expect(document.body.style.overflow).toBe('hidden')
|
|
111
111
|
|
|
112
112
|
lock2.unlock()
|
|
113
|
-
expect(document.body.style.overflow).toBe(
|
|
113
|
+
expect(document.body.style.overflow).toBe('scroll')
|
|
114
114
|
})
|
|
115
115
|
|
|
116
|
-
it(
|
|
116
|
+
it('unlock without prior lock is no-op', async () => {
|
|
117
117
|
const useScrollLock = await getUseScrollLock()
|
|
118
|
-
document.body.style.overflow =
|
|
118
|
+
document.body.style.overflow = 'auto'
|
|
119
119
|
const { unlock } = useScrollLock()
|
|
120
120
|
unlock()
|
|
121
|
-
expect(document.body.style.overflow).toBe(
|
|
121
|
+
expect(document.body.style.overflow).toBe('auto')
|
|
122
122
|
})
|
|
123
123
|
})
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { describe, expect, it, vi } from
|
|
1
|
+
import { describe, expect, it, vi } from 'vitest'
|
|
2
2
|
|
|
3
3
|
// Mock @pyreon/styler to provide theme values
|
|
4
|
-
vi.mock(
|
|
4
|
+
vi.mock('@pyreon/styler', () => ({
|
|
5
5
|
useTheme: () => ({}),
|
|
6
6
|
}))
|
|
7
7
|
|
|
8
|
-
import { useSpacing } from
|
|
8
|
+
import { useSpacing } from '../useSpacing'
|
|
9
9
|
|
|
10
|
-
describe(
|
|
11
|
-
it(
|
|
10
|
+
describe('useSpacing', () => {
|
|
11
|
+
it('returns spacing function with default base (rootSize/2 = 8)', () => {
|
|
12
12
|
const spacing = useSpacing()
|
|
13
|
-
expect(spacing(1)).toBe(
|
|
14
|
-
expect(spacing(2)).toBe(
|
|
15
|
-
expect(spacing(0.5)).toBe(
|
|
13
|
+
expect(spacing(1)).toBe('8px')
|
|
14
|
+
expect(spacing(2)).toBe('16px')
|
|
15
|
+
expect(spacing(0.5)).toBe('4px')
|
|
16
16
|
})
|
|
17
17
|
|
|
18
|
-
it(
|
|
18
|
+
it('accepts custom base unit', () => {
|
|
19
19
|
const spacing = useSpacing(4)
|
|
20
|
-
expect(spacing(1)).toBe(
|
|
21
|
-
expect(spacing(3)).toBe(
|
|
20
|
+
expect(spacing(1)).toBe('4px')
|
|
21
|
+
expect(spacing(3)).toBe('12px')
|
|
22
22
|
})
|
|
23
23
|
})
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { describe, expect, it } from
|
|
2
|
-
import { useThemeValue } from
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
import { useThemeValue } from '../useThemeValue'
|
|
3
3
|
|
|
4
4
|
// Without a ThemeProvider, useTheme returns the default (empty object {}).
|
|
5
5
|
// So get(theme, path) returns undefined for any path.
|
|
6
6
|
|
|
7
|
-
describe(
|
|
8
|
-
it(
|
|
9
|
-
const result = useThemeValue(
|
|
7
|
+
describe('useThemeValue', () => {
|
|
8
|
+
it('returns undefined when no theme values exist', () => {
|
|
9
|
+
const result = useThemeValue('colors.primary')
|
|
10
10
|
expect(result).toBeUndefined()
|
|
11
11
|
})
|
|
12
12
|
})
|