@cognitap/radiant 0.0.5 → 0.0.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/CHANGELOG.md +17 -0
- package/README.md +8 -0
- package/dist/library/Button.d.ts +3 -1
- package/dist/library/Button.d.ts.map +1 -1
- package/dist/library/Button.js +43 -2
- package/dist/library/Button.js.map +1 -1
- package/dist/library/Collapsible.d.ts +2 -1
- package/dist/library/Collapsible.d.ts.map +1 -1
- package/dist/library/Collapsible.js +5 -2
- package/dist/library/Collapsible.js.map +1 -1
- package/dist/library/Dialog.d.ts +2 -1
- package/dist/library/Dialog.d.ts.map +1 -1
- package/dist/library/Dialog.js +5 -2
- package/dist/library/Dialog.js.map +1 -1
- package/dist/library/ThemeScope.d.ts.map +1 -1
- package/dist/library/ThemeScope.js +10 -9
- package/dist/library/ThemeScope.js.map +1 -1
- package/dist/library/index.d.ts +3 -1
- package/dist/library/index.d.ts.map +1 -1
- package/dist/library/index.js +1 -0
- package/dist/library/index.js.map +1 -1
- package/dist/library/motion/MotionLink.d.ts +7 -0
- package/dist/library/motion/MotionLink.d.ts.map +1 -0
- package/dist/library/motion/MotionLink.js +45 -0
- package/dist/library/motion/MotionLink.js.map +1 -0
- package/dist/library/motion/browser.d.ts +5 -0
- package/dist/library/motion/browser.d.ts.map +1 -0
- package/dist/library/motion/browser.js +35 -0
- package/dist/library/motion/browser.js.map +1 -0
- package/dist/library/motion/config.d.ts +26 -0
- package/dist/library/motion/config.d.ts.map +1 -0
- package/dist/library/motion/config.js +58 -0
- package/dist/library/motion/config.js.map +1 -0
- package/dist/library/motion/context.d.ts +2 -0
- package/dist/library/motion/context.d.ts.map +1 -0
- package/dist/library/motion/context.js +5 -0
- package/dist/library/motion/context.js.map +1 -0
- package/dist/library/motion/drag.d.ts +20 -0
- package/dist/library/motion/drag.d.ts.map +1 -0
- package/dist/library/motion/drag.js +138 -0
- package/dist/library/motion/drag.js.map +1 -0
- package/dist/library/motion/dragBrowser.d.ts +4 -0
- package/dist/library/motion/dragBrowser.d.ts.map +1 -0
- package/dist/library/motion/dragBrowser.js +156 -0
- package/dist/library/motion/dragBrowser.js.map +1 -0
- package/dist/library/motion/follow.d.ts +9 -0
- package/dist/library/motion/follow.d.ts.map +1 -0
- package/dist/library/motion/follow.js +26 -0
- package/dist/library/motion/follow.js.map +1 -0
- package/dist/library/motion/host.d.ts +27 -0
- package/dist/library/motion/host.d.ts.map +1 -0
- package/dist/library/motion/host.js +62 -0
- package/dist/library/motion/host.js.map +1 -0
- package/dist/library/motion/index.d.ts +16 -0
- package/dist/library/motion/index.d.ts.map +1 -0
- package/dist/library/motion/index.js +9 -0
- package/dist/library/motion/index.js.map +1 -0
- package/dist/library/motion/layout.d.ts +20 -0
- package/dist/library/motion/layout.d.ts.map +1 -0
- package/dist/library/motion/layout.js +67 -0
- package/dist/library/motion/layout.js.map +1 -0
- package/dist/library/motion/layoutDom.d.ts +17 -0
- package/dist/library/motion/layoutDom.d.ts.map +1 -0
- package/dist/library/motion/layoutDom.js +272 -0
- package/dist/library/motion/layoutDom.js.map +1 -0
- package/dist/library/motion/press.d.ts +5 -0
- package/dist/library/motion/press.d.ts.map +1 -0
- package/dist/library/motion/press.js +72 -0
- package/dist/library/motion/press.js.map +1 -0
- package/dist/library/motion/pressPolicy.d.ts +5 -0
- package/dist/library/motion/pressPolicy.d.ts.map +1 -0
- package/dist/library/motion/pressPolicy.js +8 -0
- package/dist/library/motion/pressPolicy.js.map +1 -0
- package/dist/library/motion/react.d.ts +35 -0
- package/dist/library/motion/react.d.ts.map +1 -0
- package/dist/library/motion/react.js +224 -0
- package/dist/library/motion/react.js.map +1 -0
- package/dist/library/motion/runtime.d.ts +22 -0
- package/dist/library/motion/runtime.d.ts.map +1 -0
- package/dist/library/motion/runtime.js +117 -0
- package/dist/library/motion/runtime.js.map +1 -0
- package/dist/library/motion/transitions.d.ts +5 -0
- package/dist/library/motion/transitions.d.ts.map +1 -0
- package/dist/library/motion/transitions.js +211 -0
- package/dist/library/motion/transitions.js.map +1 -0
- package/dist/library/styles.css +1 -1
- package/dist/library/themes/defineTheme.d.ts +3 -0
- package/dist/library/themes/defineTheme.d.ts.map +1 -1
- package/dist/library/themes/defineTheme.js +4 -2
- package/dist/library/themes/defineTheme.js.map +1 -1
- package/dist/library/themes/index.d.ts +1 -1
- package/dist/library/themes/index.d.ts.map +1 -1
- package/dist/library/themes/index.js.map +1 -1
- package/dist/library/themes/themeDocument.d.ts.map +1 -1
- package/dist/library/themes/themeDocument.js +11 -5
- package/dist/library/themes/themeDocument.js.map +1 -1
- package/dist/library/themes/themeDocumentTypes.d.ts +5 -1
- package/dist/library/themes/themeDocumentTypes.d.ts.map +1 -1
- package/dist/library/themes/themeDocumentTypes.js.map +1 -1
- package/docs/motion.md +434 -0
- package/docs/system.md +26 -0
- package/package.json +14 -3
- package/src/Button.tsx +49 -1
- package/src/Collapsible.tsx +13 -1
- package/src/Dialog.tsx +10 -1
- package/src/ThemeScope.tsx +21 -17
- package/src/index.ts +9 -0
- package/src/motion/MotionLink.tsx +64 -0
- package/src/motion/browser.ts +45 -0
- package/src/motion/config.ts +79 -0
- package/src/motion/context.ts +4 -0
- package/src/motion/drag.ts +151 -0
- package/src/motion/dragBrowser.ts +159 -0
- package/src/motion/follow.ts +28 -0
- package/src/motion/host.ts +88 -0
- package/src/motion/index.ts +28 -0
- package/src/motion/layout.ts +78 -0
- package/src/motion/layoutDom.ts +293 -0
- package/src/motion/press.ts +77 -0
- package/src/motion/pressPolicy.ts +12 -0
- package/src/motion/react.ts +272 -0
- package/src/motion/runtime.ts +122 -0
- package/src/motion/transitions.ts +206 -0
- package/src/themes/defineTheme.ts +10 -2
- package/src/themes/index.ts +1 -0
- package/src/themes/themeDocument.ts +14 -6
- package/src/themes/themeDocumentTypes.ts +12 -5
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import {
|
|
3
|
+
useContext,
|
|
4
|
+
useEffect,
|
|
5
|
+
useLayoutEffect,
|
|
6
|
+
useRef,
|
|
7
|
+
useState,
|
|
8
|
+
useSyncExternalStore,
|
|
9
|
+
type RefObject
|
|
10
|
+
} from 'react'
|
|
11
|
+
import { createElement, type ReactNode } from 'react'
|
|
12
|
+
import { MotionHostContext } from './host.js'
|
|
13
|
+
import { bindDisclosureMotion, bindDialogMotion } from './transitions.js'
|
|
14
|
+
import { bindPressMotion } from './press.js'
|
|
15
|
+
import { MotionProfileContext } from './context.js'
|
|
16
|
+
import { resolveMotion, type MotionInteraction } from './config.js'
|
|
17
|
+
import { createSpringDrag, type SpringDragController, type SpringDragOptions } from './drag.js'
|
|
18
|
+
import { bindSpringDrag } from './dragBrowser.js'
|
|
19
|
+
import { createSpring, type SpringController } from './runtime.js'
|
|
20
|
+
import { bindSpringStyle, type SpringStyleProperty } from './browser.js'
|
|
21
|
+
import { bindSpringSource, type SpringSource } from './follow.js'
|
|
22
|
+
import { bindSpringLayout, type SpringLayoutController } from './layoutDom.js'
|
|
23
|
+
|
|
24
|
+
// Start conservatively for SSR/hydration; only subscribing reads the browser.
|
|
25
|
+
const listeners = new Set<() => void>()
|
|
26
|
+
let query: MediaQueryList | undefined
|
|
27
|
+
function subscribeReduced(listener: () => void) {
|
|
28
|
+
if (!query) query = window.matchMedia('(prefers-reduced-motion: reduce)')
|
|
29
|
+
const current = query
|
|
30
|
+
const notify = () => listener()
|
|
31
|
+
listeners.add(notify)
|
|
32
|
+
current.addEventListener('change', notify)
|
|
33
|
+
return () => {
|
|
34
|
+
listeners.delete(notify)
|
|
35
|
+
current.removeEventListener('change', notify)
|
|
36
|
+
if (!listeners.size) query = undefined
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
export function useReducedMotion(): boolean {
|
|
40
|
+
return useSyncExternalStore(
|
|
41
|
+
subscribeReduced,
|
|
42
|
+
() => query?.matches ?? true,
|
|
43
|
+
() => true
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
export function useSpring(
|
|
47
|
+
initial: number,
|
|
48
|
+
interaction: MotionInteraction = 'follow',
|
|
49
|
+
reducedValue?: number
|
|
50
|
+
): SpringController {
|
|
51
|
+
const profile = useContext(MotionProfileContext)
|
|
52
|
+
const reduced = useReducedMotion()
|
|
53
|
+
const resolved = resolveMotion(profile, interaction, reduced)
|
|
54
|
+
const owned = useRef<SpringController | null>(null)
|
|
55
|
+
const requested = useRef(initial)
|
|
56
|
+
const requestedVelocity = useRef<number | undefined>(undefined)
|
|
57
|
+
const callbacks = useRef(new Set<(value: number) => void>())
|
|
58
|
+
const initialOptions = useRef({ ...resolved, reducedValue })
|
|
59
|
+
const policy = useRef(resolved.enabled)
|
|
60
|
+
const initialValue = useRef(initial)
|
|
61
|
+
const [controller] = useState<SpringController>(() => ({
|
|
62
|
+
get: () => owned.current?.get() ?? requested.current,
|
|
63
|
+
getVelocity: () => owned.current?.getVelocity() ?? 0,
|
|
64
|
+
isAnimating: () => owned.current?.isAnimating() ?? false,
|
|
65
|
+
to(next, velocity) {
|
|
66
|
+
if (velocity !== undefined && !Number.isFinite(velocity))
|
|
67
|
+
throw new TypeError('Velocity must be finite.')
|
|
68
|
+
requestedVelocity.current = velocity
|
|
69
|
+
if (!Number.isFinite(next)) throw new TypeError('Spring value must be finite.')
|
|
70
|
+
requested.current = next
|
|
71
|
+
owned.current?.to(next, velocity)
|
|
72
|
+
},
|
|
73
|
+
jump(next) {
|
|
74
|
+
if (!Number.isFinite(next)) throw new TypeError('Spring value must be finite.')
|
|
75
|
+
requested.current = next
|
|
76
|
+
owned.current?.jump(next)
|
|
77
|
+
},
|
|
78
|
+
stop() {
|
|
79
|
+
owned.current?.stop()
|
|
80
|
+
requested.current = owned.current?.get() ?? requested.current
|
|
81
|
+
},
|
|
82
|
+
configure(next) {
|
|
83
|
+
owned.current?.configure({ ...next, enabled: policy.current && (next.enabled ?? true) })
|
|
84
|
+
},
|
|
85
|
+
subscribe(listener) {
|
|
86
|
+
callbacks.current.add(listener)
|
|
87
|
+
return () => {
|
|
88
|
+
callbacks.current.delete(listener)
|
|
89
|
+
}
|
|
90
|
+
},
|
|
91
|
+
destroy() {
|
|
92
|
+
owned.current?.destroy()
|
|
93
|
+
}
|
|
94
|
+
}))
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const spring = createSpring(initialValue.current, initialOptions.current)
|
|
97
|
+
owned.current = spring
|
|
98
|
+
const off = spring.subscribe(next => callbacks.current.forEach(listener => listener(next)))
|
|
99
|
+
spring.to(requested.current, requestedVelocity.current)
|
|
100
|
+
return () => {
|
|
101
|
+
off()
|
|
102
|
+
spring.destroy()
|
|
103
|
+
owned.current = null
|
|
104
|
+
}
|
|
105
|
+
}, [])
|
|
106
|
+
const { stiffness, damping, mass } = resolved.parameters
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
policy.current = resolved.enabled
|
|
109
|
+
owned.current?.configure({
|
|
110
|
+
enabled: resolved.enabled,
|
|
111
|
+
parameters: { stiffness, damping, mass },
|
|
112
|
+
reducedValue
|
|
113
|
+
})
|
|
114
|
+
}, [resolved.enabled, stiffness, damping, mass, reducedValue])
|
|
115
|
+
return controller
|
|
116
|
+
}
|
|
117
|
+
/** Use the real native ref; events/state remain with the component and caller. */
|
|
118
|
+
export function useSpringStyle(
|
|
119
|
+
ref: RefObject<HTMLElement | null>,
|
|
120
|
+
property: SpringStyleProperty,
|
|
121
|
+
initial: number,
|
|
122
|
+
interaction: MotionInteraction = 'follow',
|
|
123
|
+
reducedValue?: number
|
|
124
|
+
): SpringController {
|
|
125
|
+
const spring = useSpring(initial, interaction, reducedValue)
|
|
126
|
+
// Rebind after every commit to handle a replaced render host; no animation state enters React.
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
if (!ref.current) return
|
|
129
|
+
return bindSpringStyle(ref.current, spring, property)
|
|
130
|
+
})
|
|
131
|
+
return spring
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const hostRuntime = {
|
|
135
|
+
press: bindPressMotion,
|
|
136
|
+
disclosure: bindDisclosureMotion,
|
|
137
|
+
dialog: bindDialogMotion
|
|
138
|
+
}
|
|
139
|
+
/** Opt into native Radiant spring interactions without adding a DOM wrapper. */
|
|
140
|
+
export function MotionProvider({ children }: { children?: ReactNode }) {
|
|
141
|
+
return createElement(MotionHostContext.Provider, { value: hostRuntime }, children)
|
|
142
|
+
}
|
|
143
|
+
export { MotionLink } from './MotionLink.js'
|
|
144
|
+
export type { MotionLinkProps } from './MotionLink.js'
|
|
145
|
+
|
|
146
|
+
/** Dedicated one-axis handle binding; explicit native keyboard controls are retained. */
|
|
147
|
+
export function useSpringDrag(
|
|
148
|
+
ref: RefObject<HTMLElement | null>,
|
|
149
|
+
initial: number,
|
|
150
|
+
options: Omit<SpringDragOptions, 'parameters' | 'enabled'> & { axis?: 'x' | 'y' }
|
|
151
|
+
) {
|
|
152
|
+
const profile = useContext(MotionProfileContext)
|
|
153
|
+
const reduced = useReducedMotion()
|
|
154
|
+
const resolved = resolveMotion(profile, 'drag', reduced)
|
|
155
|
+
const latest = useRef({ initial, options, resolved })
|
|
156
|
+
const owned = useRef<SpringDragController | null>(null)
|
|
157
|
+
const [control] = useState(() => ({
|
|
158
|
+
get: () => owned.current?.get() ?? initial,
|
|
159
|
+
getVelocity: () => owned.current?.getVelocity() ?? 0,
|
|
160
|
+
subscribe: (listener: (value: number) => void) =>
|
|
161
|
+
owned.current?.subscribe(listener) ?? (() => {}),
|
|
162
|
+
to: (target: number) => owned.current?.to(target),
|
|
163
|
+
cancel: () => owned.current?.cancel(),
|
|
164
|
+
step: (direction: -1 | 1 | 'first' | 'last') => owned.current?.step(direction),
|
|
165
|
+
isAnimating: () => owned.current?.isAnimating() ?? false
|
|
166
|
+
}))
|
|
167
|
+
useEffect(() => {
|
|
168
|
+
const current = latest.current
|
|
169
|
+
const drag = createSpringDrag(current.initial, { ...current.options, ...current.resolved })
|
|
170
|
+
owned.current = drag
|
|
171
|
+
return () => {
|
|
172
|
+
drag.destroy()
|
|
173
|
+
owned.current = null
|
|
174
|
+
}
|
|
175
|
+
}, [])
|
|
176
|
+
const points = options.snapPoints?.join(',')
|
|
177
|
+
const { stiffness, damping, mass } = resolved.parameters
|
|
178
|
+
useEffect(() => {
|
|
179
|
+
owned.current?.configure({
|
|
180
|
+
min: options.min,
|
|
181
|
+
max: options.max,
|
|
182
|
+
snapPoints: points?.split(',').map(Number),
|
|
183
|
+
parameters: { stiffness, damping, mass },
|
|
184
|
+
enabled: resolved.enabled
|
|
185
|
+
})
|
|
186
|
+
}, [options.min, options.max, points, stiffness, damping, mass, resolved.enabled])
|
|
187
|
+
const binding = useRef<{
|
|
188
|
+
node: HTMLElement
|
|
189
|
+
axis: 'x' | 'y' | undefined
|
|
190
|
+
cleanup: () => void
|
|
191
|
+
} | null>(null)
|
|
192
|
+
useEffect(() => {
|
|
193
|
+
if (
|
|
194
|
+
binding.current &&
|
|
195
|
+
(binding.current.node !== ref.current || binding.current.axis !== options.axis)
|
|
196
|
+
) {
|
|
197
|
+
binding.current.cleanup()
|
|
198
|
+
binding.current = null
|
|
199
|
+
}
|
|
200
|
+
if (!binding.current && ref.current && owned.current)
|
|
201
|
+
binding.current = {
|
|
202
|
+
node: ref.current,
|
|
203
|
+
axis: options.axis,
|
|
204
|
+
cleanup: bindSpringDrag(ref.current, owned.current, options.axis)
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
useEffect(
|
|
208
|
+
() => () => {
|
|
209
|
+
binding.current?.cleanup()
|
|
210
|
+
binding.current = null
|
|
211
|
+
},
|
|
212
|
+
[]
|
|
213
|
+
)
|
|
214
|
+
return control
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** Follow committed numeric props or an external source without rendering each spring frame. */
|
|
218
|
+
export function useSpringFollow(
|
|
219
|
+
target: number | SpringSource,
|
|
220
|
+
interaction: MotionInteraction = 'follow',
|
|
221
|
+
reducedValue?: number
|
|
222
|
+
): SpringController {
|
|
223
|
+
const spring = useSpring(
|
|
224
|
+
typeof target === 'number' ? target : target.get(),
|
|
225
|
+
interaction,
|
|
226
|
+
reducedValue
|
|
227
|
+
)
|
|
228
|
+
useEffect(() => {
|
|
229
|
+
if (typeof target === 'number') spring.to(target)
|
|
230
|
+
else return bindSpringSource(spring, target)
|
|
231
|
+
}, [spring, target])
|
|
232
|
+
return spring
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** Batch direct-child layout slots after each commit and observe later intrinsic changes. */
|
|
236
|
+
export function useSpringLayout(
|
|
237
|
+
ref: RefObject<HTMLElement | null>
|
|
238
|
+
): Pick<SpringLayoutController, 'measure' | 'get' | 'getVelocity' | 'isAnimating'> {
|
|
239
|
+
const profile = useContext(MotionProfileContext)
|
|
240
|
+
const reduced = useReducedMotion()
|
|
241
|
+
const resolved = resolveMotion(profile, 'layout', reduced)
|
|
242
|
+
const binding = useRef<{ node: HTMLElement; controller: SpringLayoutController } | null>(null)
|
|
243
|
+
const [controller] = useState<
|
|
244
|
+
Pick<SpringLayoutController, 'measure' | 'get' | 'getVelocity' | 'isAnimating'>
|
|
245
|
+
>(() => ({
|
|
246
|
+
measure: () => binding.current?.controller.measure(),
|
|
247
|
+
get: slot => binding.current?.controller.get(slot),
|
|
248
|
+
getVelocity: slot => binding.current?.controller.getVelocity(slot),
|
|
249
|
+
isAnimating: () => binding.current?.controller.isAnimating() ?? false
|
|
250
|
+
}))
|
|
251
|
+
useLayoutEffect(() => {
|
|
252
|
+
if (binding.current?.node !== ref.current) {
|
|
253
|
+
binding.current?.controller.destroy()
|
|
254
|
+
binding.current = null
|
|
255
|
+
}
|
|
256
|
+
if (!ref.current) return
|
|
257
|
+
if (!binding.current)
|
|
258
|
+
binding.current = { node: ref.current, controller: bindSpringLayout(ref.current, resolved) }
|
|
259
|
+
else {
|
|
260
|
+
binding.current.controller.configure(resolved)
|
|
261
|
+
binding.current.controller.measure()
|
|
262
|
+
}
|
|
263
|
+
})
|
|
264
|
+
useLayoutEffect(
|
|
265
|
+
() => () => {
|
|
266
|
+
binding.current?.controller.destroy()
|
|
267
|
+
binding.current = null
|
|
268
|
+
},
|
|
269
|
+
[]
|
|
270
|
+
)
|
|
271
|
+
return controller
|
|
272
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { springValue } from 'motion'
|
|
2
|
+
import { resolveSpringParameters, type SpringParameters } from './config.js'
|
|
3
|
+
|
|
4
|
+
export interface SpringOptions {
|
|
5
|
+
readonly parameters?: Partial<SpringParameters>
|
|
6
|
+
readonly enabled?: boolean
|
|
7
|
+
/** Presentation neutral (e.g. scale 1); otherwise off snaps to the latest goal. */
|
|
8
|
+
readonly reducedValue?: number
|
|
9
|
+
}
|
|
10
|
+
export interface SpringController {
|
|
11
|
+
get(): number
|
|
12
|
+
getVelocity(): number
|
|
13
|
+
isAnimating(): boolean
|
|
14
|
+
to(target: number, velocity?: number): void
|
|
15
|
+
jump(target: number): void
|
|
16
|
+
/** Stop at the current presentation and discard the old goal. */
|
|
17
|
+
stop(): void
|
|
18
|
+
configure(options: SpringOptions): void
|
|
19
|
+
subscribe(listener: (value: number) => void): () => void
|
|
20
|
+
destroy(): void
|
|
21
|
+
}
|
|
22
|
+
function finite(value: number) {
|
|
23
|
+
if (typeof value !== 'number' || !Number.isFinite(value))
|
|
24
|
+
throw new TypeError('Spring values must be finite numbers in one consistent unit.')
|
|
25
|
+
return value
|
|
26
|
+
}
|
|
27
|
+
function same(a: SpringParameters, b: SpringParameters) {
|
|
28
|
+
return a.stiffness === b.stiffness && a.damping === b.damping && a.mass === b.mass
|
|
29
|
+
}
|
|
30
|
+
/** One persistent upstream follower; numeric values only, no DOM or React ownership. */
|
|
31
|
+
export function createSpring(initial: number, options: SpringOptions = {}): SpringController {
|
|
32
|
+
let parameters = resolveSpringParameters(options.parameters)
|
|
33
|
+
let pending = parameters
|
|
34
|
+
let value = springValue(finite(initial), parameters)
|
|
35
|
+
let enabled = options.enabled ?? true
|
|
36
|
+
let reducedValue = options.reducedValue === undefined ? undefined : finite(options.reducedValue)
|
|
37
|
+
let target = initial
|
|
38
|
+
let destroyed = false
|
|
39
|
+
const listeners = new Set<(value: number) => void>()
|
|
40
|
+
let offChange: () => void
|
|
41
|
+
let offComplete: () => void
|
|
42
|
+
function attach() {
|
|
43
|
+
offChange = value.on('change', next => listeners.forEach(listener => listener(next)))
|
|
44
|
+
offComplete = value.on('animationComplete', applyPending)
|
|
45
|
+
}
|
|
46
|
+
function applyPending() {
|
|
47
|
+
if (destroyed || same(parameters, pending)) return
|
|
48
|
+
const current = value.get()
|
|
49
|
+
offChange()
|
|
50
|
+
offComplete()
|
|
51
|
+
value.destroy()
|
|
52
|
+
parameters = pending
|
|
53
|
+
value = springValue(current, parameters)
|
|
54
|
+
attach()
|
|
55
|
+
}
|
|
56
|
+
attach()
|
|
57
|
+
const controller: SpringController = {
|
|
58
|
+
get: () => value.get(),
|
|
59
|
+
getVelocity: () => (destroyed ? 0 : value.getVelocity()),
|
|
60
|
+
isAnimating: () => !destroyed && value.isAnimating(),
|
|
61
|
+
to(next, velocity) {
|
|
62
|
+
finite(next)
|
|
63
|
+
if (velocity !== undefined) finite(velocity)
|
|
64
|
+
if (destroyed) return
|
|
65
|
+
target = next
|
|
66
|
+
if (enabled) {
|
|
67
|
+
if (velocity !== undefined) {
|
|
68
|
+
const current = value.get()
|
|
69
|
+
value.jump(current)
|
|
70
|
+
applyPending()
|
|
71
|
+
value.setWithVelocity(current - velocity / 60, current, 1000 / 60)
|
|
72
|
+
}
|
|
73
|
+
value.set(next)
|
|
74
|
+
} else value.jump(reducedValue ?? target)
|
|
75
|
+
},
|
|
76
|
+
jump(next) {
|
|
77
|
+
finite(next)
|
|
78
|
+
if (destroyed) return
|
|
79
|
+
target = next
|
|
80
|
+
value.jump(enabled ? next : (reducedValue ?? next))
|
|
81
|
+
applyPending()
|
|
82
|
+
},
|
|
83
|
+
stop() {
|
|
84
|
+
if (destroyed) return
|
|
85
|
+
target = value.get()
|
|
86
|
+
value.jump(target)
|
|
87
|
+
applyPending()
|
|
88
|
+
},
|
|
89
|
+
configure(next) {
|
|
90
|
+
const resolved = resolveSpringParameters(next.parameters, pending)
|
|
91
|
+
const neutral = Object.hasOwn(next, 'reducedValue')
|
|
92
|
+
? next.reducedValue === undefined
|
|
93
|
+
? undefined
|
|
94
|
+
: finite(next.reducedValue)
|
|
95
|
+
: reducedValue
|
|
96
|
+
if (destroyed) return
|
|
97
|
+
pending = resolved
|
|
98
|
+
reducedValue = neutral
|
|
99
|
+
enabled = next.enabled ?? enabled
|
|
100
|
+
if (!enabled) value.jump(reducedValue ?? target)
|
|
101
|
+
// Parameter changes take effect at rest, so in-flight momentum is never replaced.
|
|
102
|
+
if (!value.isAnimating()) applyPending()
|
|
103
|
+
},
|
|
104
|
+
subscribe(listener) {
|
|
105
|
+
if (destroyed) return () => {}
|
|
106
|
+
listeners.add(listener)
|
|
107
|
+
return () => {
|
|
108
|
+
listeners.delete(listener)
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
destroy() {
|
|
112
|
+
if (destroyed) return
|
|
113
|
+
destroyed = true
|
|
114
|
+
offChange()
|
|
115
|
+
offComplete()
|
|
116
|
+
value.destroy()
|
|
117
|
+
listeners.clear()
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
if (!enabled) value.jump(reducedValue ?? target)
|
|
121
|
+
return controller
|
|
122
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { createSpring } from './runtime.js'
|
|
2
|
+
import { bindSpringStyle } from './browser.js'
|
|
3
|
+
import { resolveMotion, type MotionProfile } from './config.js'
|
|
4
|
+
import type { MotionHostBinding } from './host.js'
|
|
5
|
+
|
|
6
|
+
export function bindDisclosureMotion(host: HTMLElement, initial: MotionProfile): MotionHostBinding {
|
|
7
|
+
const node = host as HTMLDetailsElement
|
|
8
|
+
let profile = initial
|
|
9
|
+
const query = node.ownerDocument.defaultView!.matchMedia('(prefers-reduced-motion: reduce)')
|
|
10
|
+
const contents = () =>
|
|
11
|
+
Array.from(node.children).filter(
|
|
12
|
+
(child): child is HTMLElement => child instanceof HTMLElement && child.tagName !== 'SUMMARY'
|
|
13
|
+
)
|
|
14
|
+
const measure = () =>
|
|
15
|
+
contents().reduce((sum, child) => sum + child.getBoundingClientRect().height, 0)
|
|
16
|
+
const previous = node.style.getPropertyValue('--_rad-spring-details-height')
|
|
17
|
+
const spring = createSpring(
|
|
18
|
+
node.open ? measure() : 0,
|
|
19
|
+
resolveMotion(profile, 'disclosure', query.matches)
|
|
20
|
+
)
|
|
21
|
+
const saved = new Map<HTMLElement, { inert: boolean; hidden: string | null }>()
|
|
22
|
+
const render = (value: number) =>
|
|
23
|
+
node.style.setProperty('--_rad-spring-details-height', `${Math.max(0, value)}px`)
|
|
24
|
+
const off = spring.subscribe(render)
|
|
25
|
+
render(spring.get())
|
|
26
|
+
function sync() {
|
|
27
|
+
for (const child of contents()) {
|
|
28
|
+
if (!node.open) {
|
|
29
|
+
if (!saved.has(child))
|
|
30
|
+
saved.set(child, { inert: child.inert, hidden: child.getAttribute('aria-hidden') })
|
|
31
|
+
child.inert = true
|
|
32
|
+
child.setAttribute('aria-hidden', 'true')
|
|
33
|
+
} else restore(child)
|
|
34
|
+
}
|
|
35
|
+
spring.to(node.open ? measure() : 0)
|
|
36
|
+
}
|
|
37
|
+
function restore(child: HTMLElement) {
|
|
38
|
+
const prior = saved.get(child)
|
|
39
|
+
if (!prior) return
|
|
40
|
+
child.inert = prior.inert
|
|
41
|
+
if (prior.hidden === null) child.removeAttribute('aria-hidden')
|
|
42
|
+
else child.setAttribute('aria-hidden', prior.hidden)
|
|
43
|
+
saved.delete(child)
|
|
44
|
+
}
|
|
45
|
+
const observer = new MutationObserver(sync)
|
|
46
|
+
observer.observe(node, { attributes: true, attributeFilter: ['open'], childList: true })
|
|
47
|
+
const resize = new ResizeObserver(sync)
|
|
48
|
+
contents().forEach(child => resize.observe(child))
|
|
49
|
+
function update(next: MotionProfile) {
|
|
50
|
+
profile = next
|
|
51
|
+
spring.configure(resolveMotion(profile, 'disclosure', query.matches))
|
|
52
|
+
sync()
|
|
53
|
+
}
|
|
54
|
+
const preference = () => update(profile)
|
|
55
|
+
query.addEventListener('change', preference)
|
|
56
|
+
sync()
|
|
57
|
+
return {
|
|
58
|
+
update,
|
|
59
|
+
destroy() {
|
|
60
|
+
observer.disconnect()
|
|
61
|
+
resize.disconnect()
|
|
62
|
+
query.removeEventListener('change', preference)
|
|
63
|
+
off()
|
|
64
|
+
spring.destroy()
|
|
65
|
+
for (const child of saved.keys()) restore(child)
|
|
66
|
+
if (previous) node.style.setProperty('--_rad-spring-details-height', previous)
|
|
67
|
+
else node.style.removeProperty('--_rad-spring-details-height')
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function bindDialogMotion(host: HTMLElement, initial: MotionProfile): MotionHostBinding {
|
|
73
|
+
const node = host as HTMLDialogElement
|
|
74
|
+
let profile = initial
|
|
75
|
+
const query = node.ownerDocument.defaultView!.matchMedia('(prefers-reduced-motion: reduce)')
|
|
76
|
+
let open = node.open
|
|
77
|
+
const geometry = () => ({
|
|
78
|
+
left: node.offsetLeft,
|
|
79
|
+
top: node.offsetTop,
|
|
80
|
+
width: node.offsetWidth,
|
|
81
|
+
height: node.offsetHeight,
|
|
82
|
+
variables: Array.from(getComputedStyle(node))
|
|
83
|
+
.filter(key => key.startsWith('--'))
|
|
84
|
+
.map(key => [key, getComputedStyle(node).getPropertyValue(key)])
|
|
85
|
+
})
|
|
86
|
+
let bounds = node.open ? geometry() : null
|
|
87
|
+
let ghost: HTMLDivElement | null = null
|
|
88
|
+
const spring = createSpring(open ? 1 : 0.94, {
|
|
89
|
+
...resolveMotion(profile, 'dialog', query.matches),
|
|
90
|
+
reducedValue: open ? 1 : 0.94
|
|
91
|
+
})
|
|
92
|
+
const unbind = bindSpringStyle(node, spring, 'scale')
|
|
93
|
+
function clear() {
|
|
94
|
+
ghost?.remove()
|
|
95
|
+
ghost = null
|
|
96
|
+
}
|
|
97
|
+
function snapshot() {
|
|
98
|
+
clear()
|
|
99
|
+
if (!bounds || bounds.width === 0) return
|
|
100
|
+
if (
|
|
101
|
+
node.parentElement?.closest('dialog[open]') ||
|
|
102
|
+
Array.from(node.querySelectorAll('*')).some(
|
|
103
|
+
child =>
|
|
104
|
+
child.localName.includes('-') ||
|
|
105
|
+
/^(script|style|link|iframe|object|embed|canvas|video|audio)$/.test(child.localName)
|
|
106
|
+
)
|
|
107
|
+
)
|
|
108
|
+
return
|
|
109
|
+
const copy = node.cloneNode(true) as HTMLDialogElement
|
|
110
|
+
ghost = node.ownerDocument.createElement('div')
|
|
111
|
+
for (const attribute of copy.attributes) ghost.setAttribute(attribute.name, attribute.value)
|
|
112
|
+
while (copy.firstChild) ghost.append(copy.firstChild)
|
|
113
|
+
ghost.setAttribute('open', '')
|
|
114
|
+
ghost.removeAttribute('data-slot')
|
|
115
|
+
ghost.setAttribute('role', 'presentation')
|
|
116
|
+
ghost.setAttribute('data-state', 'open')
|
|
117
|
+
ghost.inert = true
|
|
118
|
+
ghost.setAttribute('aria-hidden', 'true')
|
|
119
|
+
ghost.setAttribute('data-rad-motion-exit', '')
|
|
120
|
+
for (const child of [ghost, ...ghost.querySelectorAll('*')]) {
|
|
121
|
+
for (const attribute of Array.from(child.attributes))
|
|
122
|
+
if (attribute.name.toLowerCase().startsWith('on')) child.removeAttribute(attribute.name)
|
|
123
|
+
for (const attribute of ['id', 'name', 'form', 'autofocus']) child.removeAttribute(attribute)
|
|
124
|
+
if (
|
|
125
|
+
child instanceof HTMLInputElement ||
|
|
126
|
+
child instanceof HTMLButtonElement ||
|
|
127
|
+
child instanceof HTMLSelectElement ||
|
|
128
|
+
child instanceof HTMLTextAreaElement
|
|
129
|
+
)
|
|
130
|
+
child.disabled = true
|
|
131
|
+
}
|
|
132
|
+
for (const [key, value] of bounds.variables) ghost.style.setProperty(key, value)
|
|
133
|
+
ghost.style.font = getComputedStyle(node).font
|
|
134
|
+
ghost.style.colorScheme = getComputedStyle(node).colorScheme
|
|
135
|
+
Object.assign(ghost.style, {
|
|
136
|
+
position: 'fixed',
|
|
137
|
+
margin: '0px',
|
|
138
|
+
left: `${bounds.left}px`,
|
|
139
|
+
top: `${bounds.top}px`,
|
|
140
|
+
width: `${bounds.width}px`,
|
|
141
|
+
height: `${bounds.height}px`,
|
|
142
|
+
maxHeight: 'none',
|
|
143
|
+
pointerEvents: 'none',
|
|
144
|
+
zIndex: '2147483647',
|
|
145
|
+
transition: 'none'
|
|
146
|
+
})
|
|
147
|
+
node.ownerDocument.body.append(ghost)
|
|
148
|
+
}
|
|
149
|
+
const off = spring.subscribe(value => {
|
|
150
|
+
if (ghost) {
|
|
151
|
+
ghost.style.scale = String(Math.max(0, value))
|
|
152
|
+
ghost.style.opacity = String(Math.max(0, Math.min(1, (value - 0.94) / 0.06)))
|
|
153
|
+
if (!node.open && Math.abs(value - 0.94) < 0.0001) clear()
|
|
154
|
+
}
|
|
155
|
+
})
|
|
156
|
+
function sync() {
|
|
157
|
+
if (node.open === open) return
|
|
158
|
+
open = node.open
|
|
159
|
+
const enabled = resolveMotion(profile, 'dialog', query.matches).enabled
|
|
160
|
+
if (open) {
|
|
161
|
+
clear()
|
|
162
|
+
bounds = geometry()
|
|
163
|
+
spring.configure({ reducedValue: 1 })
|
|
164
|
+
spring.to(1)
|
|
165
|
+
} else {
|
|
166
|
+
if (enabled) snapshot()
|
|
167
|
+
spring.configure({ reducedValue: 0.94 })
|
|
168
|
+
spring.to(0.94)
|
|
169
|
+
if (!enabled) clear()
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const beforeToggle = () => {
|
|
173
|
+
if (node.open) bounds = geometry()
|
|
174
|
+
}
|
|
175
|
+
node.addEventListener('beforetoggle', beforeToggle)
|
|
176
|
+
const observer = new MutationObserver(sync)
|
|
177
|
+
observer.observe(node, { attributes: true, attributeFilter: ['open'] })
|
|
178
|
+
const resize = new ResizeObserver(() => {
|
|
179
|
+
if (node.open) bounds = geometry()
|
|
180
|
+
})
|
|
181
|
+
resize.observe(node)
|
|
182
|
+
function update(next: MotionProfile) {
|
|
183
|
+
profile = next
|
|
184
|
+
spring.configure({
|
|
185
|
+
...resolveMotion(profile, 'dialog', query.matches),
|
|
186
|
+
reducedValue: node.open ? 1 : 0.94
|
|
187
|
+
})
|
|
188
|
+
if (!resolveMotion(profile, 'dialog', query.matches).enabled) clear()
|
|
189
|
+
sync()
|
|
190
|
+
}
|
|
191
|
+
const preference = () => update(profile)
|
|
192
|
+
query.addEventListener('change', preference)
|
|
193
|
+
return {
|
|
194
|
+
update,
|
|
195
|
+
destroy() {
|
|
196
|
+
observer.disconnect()
|
|
197
|
+
resize.disconnect()
|
|
198
|
+
query.removeEventListener('change', preference)
|
|
199
|
+
off()
|
|
200
|
+
spring.destroy()
|
|
201
|
+
unbind()
|
|
202
|
+
node.removeEventListener('beforetoggle', beforeToggle)
|
|
203
|
+
clear()
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defineMotionProfile,
|
|
3
|
+
type MotionProfile,
|
|
4
|
+
type MotionProfileInput
|
|
5
|
+
} from '../motion/config.js'
|
|
1
6
|
import type { CSSProperties } from 'react'
|
|
2
7
|
import {
|
|
3
8
|
flattenTokens,
|
|
@@ -24,6 +29,7 @@ export interface ThemeLayer {
|
|
|
24
29
|
readonly components?: ThemeComponentOverrides
|
|
25
30
|
}
|
|
26
31
|
export interface ThemeDefinition extends ThemeLayer {
|
|
32
|
+
readonly motion?: MotionProfileInput
|
|
27
33
|
readonly id: string
|
|
28
34
|
readonly name: string
|
|
29
35
|
readonly modes?: { readonly light?: ThemeLayer; readonly dark?: ThemeLayer }
|
|
@@ -31,6 +37,7 @@ export interface ThemeDefinition extends ThemeLayer {
|
|
|
31
37
|
|
|
32
38
|
/** Resolved, immutable data. Safe to pass through server/client serialization. */
|
|
33
39
|
export interface Theme {
|
|
40
|
+
readonly motion: MotionProfile
|
|
34
41
|
readonly id: string
|
|
35
42
|
readonly name: string
|
|
36
43
|
readonly modes: {
|
|
@@ -111,7 +118,7 @@ export function defineTheme(definition: ThemeDefinition): Theme {
|
|
|
111
118
|
assertRecord(definition, 'theme')
|
|
112
119
|
assertKeys(
|
|
113
120
|
definition,
|
|
114
|
-
{ id: true, name: true, tokens: true, components: true, modes: true },
|
|
121
|
+
{ id: true, name: true, tokens: true, components: true, modes: true, motion: true },
|
|
115
122
|
'theme'
|
|
116
123
|
)
|
|
117
124
|
if (typeof definition.id !== 'string' || !/^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(definition.id)) {
|
|
@@ -144,6 +151,7 @@ export function defineTheme(definition: ThemeDefinition): Theme {
|
|
|
144
151
|
return freezeTree({
|
|
145
152
|
id: definition.id,
|
|
146
153
|
name: definition.name,
|
|
147
|
-
modes
|
|
154
|
+
modes,
|
|
155
|
+
motion: defineMotionProfile(definition.motion)
|
|
148
156
|
})
|
|
149
157
|
}
|
package/src/themes/index.ts
CHANGED
|
@@ -205,11 +205,16 @@ function validateFonts(value: unknown, path: string) {
|
|
|
205
205
|
}
|
|
206
206
|
|
|
207
207
|
function toDefinition(sections: ThemeSections, id: string, name: string): ThemeDefinition {
|
|
208
|
-
const common: {
|
|
208
|
+
const common: {
|
|
209
|
+
tokens: RecordValue
|
|
210
|
+
components?: ThemeLayer['components']
|
|
211
|
+
motion?: ThemeDefinition['motion']
|
|
212
|
+
} = { tokens: {} }
|
|
209
213
|
const modes = { light: { tokens: {} } as typeof common, dark: { tokens: {} } as typeof common }
|
|
210
214
|
for (const sectionName of sectionNames) {
|
|
211
215
|
const section = sections[sectionName]
|
|
212
216
|
if (!section) continue
|
|
217
|
+
if (sectionName === 'motion') common.motion = sections.motion?.profile
|
|
213
218
|
if (sectionName === 'typography' && sections.typography?.typesetting !== undefined) {
|
|
214
219
|
common.tokens.typography = generateTypography(sections.typography.typesetting).tokens
|
|
215
220
|
}
|
|
@@ -249,11 +254,13 @@ export function parseThemeDocument(input: unknown): ThemeDocument | ThemeSection
|
|
|
249
254
|
[
|
|
250
255
|
'values',
|
|
251
256
|
'modes',
|
|
252
|
-
...(name === '
|
|
253
|
-
? ['
|
|
254
|
-
: name === '
|
|
255
|
-
? ['
|
|
256
|
-
:
|
|
257
|
+
...(name === 'motion'
|
|
258
|
+
? ['profile']
|
|
259
|
+
: name === 'color'
|
|
260
|
+
? ['brand', 'fidelity', 'palettes', 'anchor']
|
|
261
|
+
: name === 'typography'
|
|
262
|
+
? ['fonts', 'typesetting']
|
|
263
|
+
: [])
|
|
257
264
|
],
|
|
258
265
|
path
|
|
259
266
|
)
|
|
@@ -351,6 +358,7 @@ export function themeDocumentFromDefinition(definition: ThemeDefinition): ThemeD
|
|
|
351
358
|
const section: RecordValue = {}
|
|
352
359
|
const values = name === 'components' ? copy.components : copy.tokens?.[name]
|
|
353
360
|
if (values !== undefined) section.values = values
|
|
361
|
+
if (name === 'motion' && copy.motion !== undefined) section.profile = copy.motion
|
|
354
362
|
const modes: RecordValue = {}
|
|
355
363
|
for (const mode of ['light', 'dark'] as const) {
|
|
356
364
|
const values =
|