@furystack/shades 12.3.0 → 12.5.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.
Files changed (58) hide show
  1. package/CHANGELOG.md +68 -0
  2. package/esm/components/lazy-load.d.ts +2 -0
  3. package/esm/components/lazy-load.d.ts.map +1 -1
  4. package/esm/components/lazy-load.js +8 -3
  5. package/esm/components/lazy-load.js.map +1 -1
  6. package/esm/components/lazy-load.spec.js +59 -1
  7. package/esm/components/lazy-load.spec.js.map +1 -1
  8. package/esm/components/nested-route-link.d.ts.map +1 -1
  9. package/esm/components/nested-route-link.js +1 -0
  10. package/esm/components/nested-route-link.js.map +1 -1
  11. package/esm/components/nested-router.d.ts +20 -0
  12. package/esm/components/nested-router.d.ts.map +1 -1
  13. package/esm/components/nested-router.js +34 -12
  14. package/esm/components/nested-router.js.map +1 -1
  15. package/esm/components/nested-router.spec.js +167 -1
  16. package/esm/components/nested-router.spec.js.map +1 -1
  17. package/esm/components/route-link.d.ts.map +1 -1
  18. package/esm/components/route-link.js +1 -0
  19. package/esm/components/route-link.js.map +1 -1
  20. package/esm/index.d.ts +1 -0
  21. package/esm/index.d.ts.map +1 -1
  22. package/esm/index.js +1 -0
  23. package/esm/index.js.map +1 -1
  24. package/esm/services/location-service.d.ts +13 -0
  25. package/esm/services/location-service.d.ts.map +1 -1
  26. package/esm/services/location-service.js +21 -1
  27. package/esm/services/location-service.js.map +1 -1
  28. package/esm/services/screen-service.d.ts.map +1 -1
  29. package/esm/services/screen-service.js +4 -0
  30. package/esm/services/screen-service.js.map +1 -1
  31. package/esm/shade-resources.integration.spec.js.map +1 -1
  32. package/esm/shade.spec.js +1 -0
  33. package/esm/shade.spec.js.map +1 -1
  34. package/esm/view-transition.d.ts +38 -0
  35. package/esm/view-transition.d.ts.map +1 -0
  36. package/esm/view-transition.js +50 -0
  37. package/esm/view-transition.js.map +1 -0
  38. package/esm/view-transition.spec.d.ts +2 -0
  39. package/esm/view-transition.spec.d.ts.map +1 -0
  40. package/esm/view-transition.spec.js +184 -0
  41. package/esm/view-transition.spec.js.map +1 -0
  42. package/esm/vnode.integration.spec.js +3 -1
  43. package/esm/vnode.integration.spec.js.map +1 -1
  44. package/package.json +4 -4
  45. package/src/components/lazy-load.spec.tsx +78 -1
  46. package/src/components/lazy-load.tsx +10 -3
  47. package/src/components/nested-route-link.tsx +1 -0
  48. package/src/components/nested-router.spec.tsx +249 -0
  49. package/src/components/nested-router.tsx +57 -12
  50. package/src/components/route-link.tsx +1 -0
  51. package/src/index.ts +1 -0
  52. package/src/services/location-service.tsx +22 -1
  53. package/src/services/screen-service.ts +4 -0
  54. package/src/shade-resources.integration.spec.tsx +1 -0
  55. package/src/shade.spec.tsx +1 -0
  56. package/src/view-transition.spec.ts +218 -0
  57. package/src/view-transition.ts +66 -0
  58. package/src/vnode.integration.spec.tsx +3 -1
@@ -0,0 +1,218 @@
1
+ import { afterEach, describe, expect, it, vi } from 'vitest'
2
+ import type { ViewTransitionConfig } from './view-transition.js'
3
+ import { maybeViewTransition, transitionedValue } from './view-transition.js'
4
+
5
+ describe('maybeViewTransition', () => {
6
+ afterEach(() => {
7
+ delete (document as unknown as Record<string, unknown>).startViewTransition
8
+ })
9
+
10
+ const mockStartViewTransition = () => {
11
+ const spy = vi.fn((optionsOrCallback: StartViewTransitionOptions | (() => void)) => {
12
+ const update = typeof optionsOrCallback === 'function' ? optionsOrCallback : optionsOrCallback.update
13
+ update?.()
14
+ return {
15
+ finished: Promise.resolve(),
16
+ ready: Promise.resolve(),
17
+ updateCallbackDone: Promise.resolve(),
18
+ skipTransition: vi.fn(),
19
+ } as unknown as ViewTransition
20
+ })
21
+ document.startViewTransition = spy as typeof document.startViewTransition
22
+ return spy
23
+ }
24
+
25
+ it('should call update directly when config is undefined', () => {
26
+ const spy = mockStartViewTransition()
27
+ const update = vi.fn()
28
+ const result = maybeViewTransition(undefined, update)
29
+ expect(update).toHaveBeenCalledTimes(1)
30
+ expect(spy).not.toHaveBeenCalled()
31
+ expect(result).toBeUndefined()
32
+ })
33
+
34
+ it('should call update directly when config is false', () => {
35
+ const spy = mockStartViewTransition()
36
+ const update = vi.fn()
37
+ const result = maybeViewTransition(false, update)
38
+ expect(update).toHaveBeenCalledTimes(1)
39
+ expect(spy).not.toHaveBeenCalled()
40
+ expect(result).toBeUndefined()
41
+ })
42
+
43
+ it('should call update directly when startViewTransition is not available', () => {
44
+ const update = vi.fn()
45
+ const result = maybeViewTransition(true, update)
46
+ expect(update).toHaveBeenCalledTimes(1)
47
+ expect(result).toBeUndefined()
48
+ })
49
+
50
+ it('should call startViewTransition when config is true and API is available', () => {
51
+ const spy = mockStartViewTransition()
52
+ const update = vi.fn()
53
+ const result = maybeViewTransition(true, update)
54
+ expect(spy).toHaveBeenCalledTimes(1)
55
+ expect(update).toHaveBeenCalledTimes(1)
56
+ expect(result).toBeInstanceOf(Promise)
57
+ })
58
+
59
+ it('should use callback form when config is true', () => {
60
+ const spy = mockStartViewTransition()
61
+ const update = vi.fn()
62
+ void maybeViewTransition(true, update)
63
+ expect(spy).toHaveBeenCalledWith(update)
64
+ })
65
+
66
+ it('should pass types when config is an object with types', () => {
67
+ const spy = mockStartViewTransition()
68
+ const update = vi.fn()
69
+ const config: ViewTransitionConfig = { types: ['slide', 'fade'] }
70
+ void maybeViewTransition(config, update)
71
+ expect(spy).toHaveBeenCalledWith({ update, types: ['slide', 'fade'] })
72
+ })
73
+
74
+ it('should use callback form when config object has empty types array', () => {
75
+ const spy = mockStartViewTransition()
76
+ const update = vi.fn()
77
+ const config: ViewTransitionConfig = { types: [] }
78
+ void maybeViewTransition(config, update)
79
+ expect(spy).toHaveBeenCalledWith(update)
80
+ })
81
+
82
+ it('should use callback form when config object has no types', () => {
83
+ const spy = mockStartViewTransition()
84
+ const update = vi.fn()
85
+ const config: ViewTransitionConfig = {}
86
+ void maybeViewTransition(config, update)
87
+ expect(spy).toHaveBeenCalledWith(update)
88
+ })
89
+
90
+ it('should return updateCallbackDone promise when transition is started', async () => {
91
+ mockStartViewTransition()
92
+ const update = vi.fn()
93
+ const result = maybeViewTransition(true, update)
94
+ expect(result).toBeInstanceOf(Promise)
95
+ await expect(result).resolves.toBeUndefined()
96
+ })
97
+ })
98
+
99
+ describe('transitionedValue', () => {
100
+ afterEach(() => {
101
+ delete (document as unknown as Record<string, unknown>).startViewTransition
102
+ })
103
+
104
+ const mockStartViewTransition = () => {
105
+ const spy = vi.fn((optionsOrCallback: StartViewTransitionOptions | (() => void)) => {
106
+ const update = typeof optionsOrCallback === 'function' ? optionsOrCallback : optionsOrCallback.update
107
+ update?.()
108
+ return {
109
+ finished: Promise.resolve(),
110
+ ready: Promise.resolve(),
111
+ updateCallbackDone: Promise.resolve(),
112
+ skipTransition: vi.fn(),
113
+ } as unknown as ViewTransition
114
+ })
115
+ document.startViewTransition = spy as typeof document.startViewTransition
116
+ return spy
117
+ }
118
+
119
+ const createMockUseState = () => {
120
+ const store = new Map<string, unknown>()
121
+ const setters = new Map<string, (v: unknown) => void>()
122
+ const mockUseState = <S>(key: string, initialValue: S): [S, (v: S) => void] => {
123
+ if (!store.has(key)) {
124
+ store.set(key, initialValue)
125
+ }
126
+ const setValue = (v: S) => {
127
+ store.set(key, v)
128
+ }
129
+ setters.set(key, setValue as (v: unknown) => void)
130
+ return [store.get(key) as S, setValue]
131
+ }
132
+ return { mockUseState, store }
133
+ }
134
+
135
+ it('should return the value when it equals the displayed value', () => {
136
+ const { mockUseState } = createMockUseState()
137
+ const result = transitionedValue(mockUseState, 'key', 'hello', true)
138
+ expect(result).toBe('hello')
139
+ })
140
+
141
+ it('should not call startViewTransition when value has not changed', () => {
142
+ const spy = mockStartViewTransition()
143
+ const { mockUseState } = createMockUseState()
144
+ transitionedValue(mockUseState, 'key', 'hello', true)
145
+ expect(spy).not.toHaveBeenCalled()
146
+ })
147
+
148
+ it('should call startViewTransition when value changes and config is truthy', () => {
149
+ const spy = mockStartViewTransition()
150
+ const { mockUseState, store } = createMockUseState()
151
+
152
+ transitionedValue(mockUseState, 'key', 'initial', true)
153
+ store.set('key', 'initial')
154
+
155
+ transitionedValue(mockUseState, 'key', 'updated', true)
156
+ expect(spy).toHaveBeenCalledTimes(1)
157
+ expect(store.get('key')).toBe('updated')
158
+ })
159
+
160
+ it('should not call startViewTransition when config is falsy', () => {
161
+ const spy = mockStartViewTransition()
162
+ const { mockUseState, store } = createMockUseState()
163
+
164
+ transitionedValue(mockUseState, 'key', 'initial', undefined)
165
+ store.set('key', 'initial')
166
+
167
+ transitionedValue(mockUseState, 'key', 'updated', undefined)
168
+ expect(spy).not.toHaveBeenCalled()
169
+ expect(store.get('key')).toBe('updated')
170
+ })
171
+
172
+ it('should not call startViewTransition when shouldTransition returns false', () => {
173
+ const spy = mockStartViewTransition()
174
+ const { mockUseState, store } = createMockUseState()
175
+
176
+ transitionedValue(mockUseState, 'key', 'initial', true, () => false)
177
+ store.set('key', 'initial')
178
+
179
+ transitionedValue(mockUseState, 'key', 'updated', true, () => false)
180
+ expect(spy).not.toHaveBeenCalled()
181
+ expect(store.get('key')).toBe('updated')
182
+ })
183
+
184
+ it('should call startViewTransition when shouldTransition returns true', () => {
185
+ const spy = mockStartViewTransition()
186
+ const { mockUseState, store } = createMockUseState()
187
+
188
+ transitionedValue(mockUseState, 'key', 'initial', true, () => true)
189
+ store.set('key', 'initial')
190
+
191
+ transitionedValue(mockUseState, 'key', 'updated', true, () => true)
192
+ expect(spy).toHaveBeenCalledTimes(1)
193
+ expect(store.get('key')).toBe('updated')
194
+ })
195
+
196
+ it('should pass prev and next values to shouldTransition', () => {
197
+ mockStartViewTransition()
198
+ const { mockUseState, store } = createMockUseState()
199
+ const shouldTransition = vi.fn(() => true)
200
+
201
+ transitionedValue(mockUseState, 'key', 'initial', true, shouldTransition)
202
+ store.set('key', 'initial')
203
+
204
+ transitionedValue(mockUseState, 'key', 'updated', true, shouldTransition)
205
+ expect(shouldTransition).toHaveBeenCalledWith('initial', 'updated')
206
+ })
207
+
208
+ it('should default shouldTransition to always true', () => {
209
+ const spy = mockStartViewTransition()
210
+ const { mockUseState, store } = createMockUseState()
211
+
212
+ transitionedValue(mockUseState, 'key', 'a', true)
213
+ store.set('key', 'a')
214
+
215
+ transitionedValue(mockUseState, 'key', 'b', true)
216
+ expect(spy).toHaveBeenCalledTimes(1)
217
+ })
218
+ })
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Configuration for the View Transition API integration.
3
+ * When provided as an object, allows specifying transition types for CSS targeting
4
+ * via the `:active-view-transition-type()` pseudo-class.
5
+ */
6
+ export type ViewTransitionConfig = {
7
+ types?: string[]
8
+ }
9
+
10
+ /**
11
+ * Wraps a DOM update in `document.startViewTransition()` when the View Transition API
12
+ * is available and `config` is truthy. Falls back to calling `update()` directly otherwise.
13
+ *
14
+ * Returns the `updateCallbackDone` promise when a transition is started, allowing callers
15
+ * that need to wait for the DOM update (e.g. to run lifecycle hooks) to `await` the result.
16
+ * Returns `undefined` when no transition is used (the update runs synchronously).
17
+ *
18
+ * @param config - The view transition configuration (boolean or object with types)
19
+ * @param update - The synchronous DOM update callback
20
+ * @returns A promise that resolves after the update callback completes inside the transition, or `undefined`
21
+ */
22
+ export const maybeViewTransition = (
23
+ config: boolean | ViewTransitionConfig | undefined,
24
+ update: () => void,
25
+ ): Promise<void> | undefined => {
26
+ if (config && document.startViewTransition) {
27
+ const types = typeof config === 'object' && config.types?.length ? config.types : undefined
28
+ const transition = types ? document.startViewTransition({ update, types }) : document.startViewTransition(update)
29
+ return transition.updateCallbackDone
30
+ } else {
31
+ update()
32
+ }
33
+ }
34
+
35
+ /**
36
+ * Keeps a "displayed" copy of `value` that is updated through a view transition
37
+ * whenever the value changes and `shouldTransition` returns true.
38
+ *
39
+ * When the transition is skipped (config is falsy or `shouldTransition` returns false),
40
+ * the displayed value is updated synchronously without an animation.
41
+ *
42
+ * @param useState - The component's `useState` hook
43
+ * @param key - Unique state key for caching the displayed value
44
+ * @param value - The latest source value (e.g. from `useObservable` or derived from props)
45
+ * @param config - View transition configuration forwarded to `maybeViewTransition`
46
+ * @param shouldTransition - Predicate that decides whether a value change warrants a transition.
47
+ * Defaults to `() => true` (always transition).
48
+ * @returns The currently displayed value
49
+ */
50
+ export const transitionedValue = <T>(
51
+ useState: <S>(key: string, initialValue: S) => [S, (v: S) => void],
52
+ key: string,
53
+ value: T,
54
+ config: boolean | ViewTransitionConfig | undefined,
55
+ shouldTransition: (prev: T, next: T) => boolean = () => true,
56
+ ): T => {
57
+ const [displayed, setDisplayed] = useState(key, value)
58
+ if (value !== displayed) {
59
+ if (shouldTransition(displayed, value)) {
60
+ void maybeViewTransition(config, () => setDisplayed(value))
61
+ } else {
62
+ setDisplayed(value)
63
+ }
64
+ }
65
+ return displayed
66
+ }
@@ -335,7 +335,9 @@ describe('VNode reconciliation integration tests', () => {
335
335
  const ExampleComponent = Shade({
336
336
  shadowDomName: 'morph-animation-test',
337
337
  render: ({ useState }) => {
338
- const [isActive, setIsActive] = useState('isActive', false)
338
+ const [isActive, setIsActive] =
339
+ // eslint-disable-next-line furystack/no-css-state-hooks -- test for re-render behavior, not CSS state
340
+ useState('isActive', false)
339
341
  return (
340
342
  <div>
341
343
  <div id="animated-box" className={isActive ? 'active' : 'inactive'} />