@cognitap/radiant 0.0.5 → 0.0.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.
Files changed (234) hide show
  1. package/CHANGELOG.md +68 -368
  2. package/README.md +41 -1006
  3. package/RELEASE_POLICY.md +3 -3
  4. package/dist/library/Accordion.d.ts +3 -0
  5. package/dist/library/Accordion.d.ts.map +1 -1
  6. package/dist/library/Accordion.js +1 -1
  7. package/dist/library/Accordion.js.map +1 -1
  8. package/dist/library/Button.d.ts +8 -1
  9. package/dist/library/Button.d.ts.map +1 -1
  10. package/dist/library/Button.js +44 -2
  11. package/dist/library/Button.js.map +1 -1
  12. package/dist/library/Card.d.ts +2 -0
  13. package/dist/library/Card.d.ts.map +1 -1
  14. package/dist/library/Card.js +1 -1
  15. package/dist/library/Card.js.map +1 -1
  16. package/dist/library/Checkbox.d.ts +5 -0
  17. package/dist/library/Checkbox.d.ts.map +1 -1
  18. package/dist/library/Checkbox.js +3 -2
  19. package/dist/library/Checkbox.js.map +1 -1
  20. package/dist/library/Collapsible.d.ts +6 -1
  21. package/dist/library/Collapsible.d.ts.map +1 -1
  22. package/dist/library/Collapsible.js +24 -3
  23. package/dist/library/Collapsible.js.map +1 -1
  24. package/dist/library/Dialog.d.ts +5 -1
  25. package/dist/library/Dialog.d.ts.map +1 -1
  26. package/dist/library/Dialog.js +5 -2
  27. package/dist/library/Dialog.js.map +1 -1
  28. package/dist/library/Field.d.ts +8 -0
  29. package/dist/library/Field.d.ts.map +1 -1
  30. package/dist/library/Field.js +2 -0
  31. package/dist/library/Field.js.map +1 -1
  32. package/dist/library/Input.d.ts +3 -0
  33. package/dist/library/Input.d.ts.map +1 -1
  34. package/dist/library/Input.js +1 -0
  35. package/dist/library/Input.js.map +1 -1
  36. package/dist/library/Link.d.ts +5 -0
  37. package/dist/library/Link.d.ts.map +1 -1
  38. package/dist/library/Link.js.map +1 -1
  39. package/dist/library/RadioGroup.d.ts +8 -0
  40. package/dist/library/RadioGroup.d.ts.map +1 -1
  41. package/dist/library/RadioGroup.js +4 -2
  42. package/dist/library/RadioGroup.js.map +1 -1
  43. package/dist/library/ThemeScope.d.ts.map +1 -1
  44. package/dist/library/ThemeScope.js +10 -9
  45. package/dist/library/ThemeScope.js.map +1 -1
  46. package/dist/library/examples/expressive.d.ts +4 -0
  47. package/dist/library/examples/expressive.d.ts.map +1 -0
  48. package/dist/library/{themes → examples}/expressive.js +4 -3
  49. package/dist/library/examples/expressive.js.map +1 -0
  50. package/dist/library/examples/glass.d.ts +4 -0
  51. package/dist/library/examples/glass.d.ts.map +1 -0
  52. package/dist/library/{themes → examples}/glass.js +4 -3
  53. package/dist/library/examples/glass.js.map +1 -0
  54. package/dist/library/examples/index.d.ts +13 -0
  55. package/dist/library/examples/index.d.ts.map +1 -0
  56. package/dist/library/examples/index.js +32 -0
  57. package/dist/library/examples/index.js.map +1 -0
  58. package/dist/library/examples/jelly.d.ts +4 -0
  59. package/dist/library/examples/jelly.d.ts.map +1 -0
  60. package/dist/library/{themes → examples}/jelly.js +4 -3
  61. package/dist/library/examples/jelly.js.map +1 -0
  62. package/dist/library/examples/preset-values.d.ts.map +1 -0
  63. package/dist/library/examples/preset-values.js.map +1 -0
  64. package/dist/library/examples/radiant.d.ts +4 -0
  65. package/dist/library/examples/radiant.d.ts.map +1 -0
  66. package/dist/library/{themes → examples}/radiant.js +4 -3
  67. package/dist/library/examples/radiant.js.map +1 -0
  68. package/dist/library/examples/sharp-3d.d.ts +4 -0
  69. package/dist/library/examples/sharp-3d.d.ts.map +1 -0
  70. package/dist/library/{themes → examples}/sharp-3d.js +4 -3
  71. package/dist/library/examples/sharp-3d.js.map +1 -0
  72. package/dist/library/examples/soft-squircle.d.ts +4 -0
  73. package/dist/library/examples/soft-squircle.d.ts.map +1 -0
  74. package/dist/library/{themes → examples}/soft-squircle.js +4 -3
  75. package/dist/library/examples/soft-squircle.js.map +1 -0
  76. package/dist/library/examples/ticket.d.ts +4 -0
  77. package/dist/library/examples/ticket.d.ts.map +1 -0
  78. package/dist/library/{themes → examples}/ticket.js +4 -3
  79. package/dist/library/examples/ticket.js.map +1 -0
  80. package/dist/library/index.d.ts +4 -2
  81. package/dist/library/index.d.ts.map +1 -1
  82. package/dist/library/index.js +2 -1
  83. package/dist/library/index.js.map +1 -1
  84. package/dist/library/motion/MotionLink.d.ts +8 -0
  85. package/dist/library/motion/MotionLink.d.ts.map +1 -0
  86. package/dist/library/motion/MotionLink.js +44 -0
  87. package/dist/library/motion/MotionLink.js.map +1 -0
  88. package/dist/library/motion/browser.d.ts +5 -0
  89. package/dist/library/motion/browser.d.ts.map +1 -0
  90. package/dist/library/motion/browser.js +35 -0
  91. package/dist/library/motion/browser.js.map +1 -0
  92. package/dist/library/motion/config.d.ts +26 -0
  93. package/dist/library/motion/config.d.ts.map +1 -0
  94. package/dist/library/motion/config.js +58 -0
  95. package/dist/library/motion/config.js.map +1 -0
  96. package/dist/library/motion/context.d.ts +2 -0
  97. package/dist/library/motion/context.d.ts.map +1 -0
  98. package/dist/library/motion/context.js +5 -0
  99. package/dist/library/motion/context.js.map +1 -0
  100. package/dist/library/motion/drag.d.ts +20 -0
  101. package/dist/library/motion/drag.d.ts.map +1 -0
  102. package/dist/library/motion/drag.js +138 -0
  103. package/dist/library/motion/drag.js.map +1 -0
  104. package/dist/library/motion/dragBrowser.d.ts +4 -0
  105. package/dist/library/motion/dragBrowser.d.ts.map +1 -0
  106. package/dist/library/motion/dragBrowser.js +156 -0
  107. package/dist/library/motion/dragBrowser.js.map +1 -0
  108. package/dist/library/motion/follow.d.ts +9 -0
  109. package/dist/library/motion/follow.d.ts.map +1 -0
  110. package/dist/library/motion/follow.js +26 -0
  111. package/dist/library/motion/follow.js.map +1 -0
  112. package/dist/library/motion/host.d.ts +30 -0
  113. package/dist/library/motion/host.d.ts.map +1 -0
  114. package/dist/library/motion/host.js +70 -0
  115. package/dist/library/motion/host.js.map +1 -0
  116. package/dist/library/motion/index.d.ts +16 -0
  117. package/dist/library/motion/index.d.ts.map +1 -0
  118. package/dist/library/motion/index.js +9 -0
  119. package/dist/library/motion/index.js.map +1 -0
  120. package/dist/library/motion/layout.d.ts +20 -0
  121. package/dist/library/motion/layout.d.ts.map +1 -0
  122. package/dist/library/motion/layout.js +67 -0
  123. package/dist/library/motion/layout.js.map +1 -0
  124. package/dist/library/motion/layoutDom.d.ts +17 -0
  125. package/dist/library/motion/layoutDom.d.ts.map +1 -0
  126. package/dist/library/motion/layoutDom.js +272 -0
  127. package/dist/library/motion/layoutDom.js.map +1 -0
  128. package/dist/library/motion/press.d.ts +5 -0
  129. package/dist/library/motion/press.d.ts.map +1 -0
  130. package/dist/library/motion/press.js +99 -0
  131. package/dist/library/motion/press.js.map +1 -0
  132. package/dist/library/motion/pressPolicy.d.ts +5 -0
  133. package/dist/library/motion/pressPolicy.d.ts.map +1 -0
  134. package/dist/library/motion/pressPolicy.js +8 -0
  135. package/dist/library/motion/pressPolicy.js.map +1 -0
  136. package/dist/library/motion/react.d.ts +35 -0
  137. package/dist/library/motion/react.d.ts.map +1 -0
  138. package/dist/library/motion/react.js +224 -0
  139. package/dist/library/motion/react.js.map +1 -0
  140. package/dist/library/motion/runtime.d.ts +22 -0
  141. package/dist/library/motion/runtime.d.ts.map +1 -0
  142. package/dist/library/motion/runtime.js +117 -0
  143. package/dist/library/motion/runtime.js.map +1 -0
  144. package/dist/library/motion/transitions.d.ts +5 -0
  145. package/dist/library/motion/transitions.d.ts.map +1 -0
  146. package/dist/library/motion/transitions.js +244 -0
  147. package/dist/library/motion/transitions.js.map +1 -0
  148. package/dist/library/styles.css +1 -1
  149. package/dist/library/themes/defineTheme.d.ts +3 -0
  150. package/dist/library/themes/defineTheme.d.ts.map +1 -1
  151. package/dist/library/themes/defineTheme.js +4 -2
  152. package/dist/library/themes/defineTheme.js.map +1 -1
  153. package/dist/library/themes/index.d.ts +2 -12
  154. package/dist/library/themes/index.d.ts.map +1 -1
  155. package/dist/library/themes/index.js +1 -19
  156. package/dist/library/themes/index.js.map +1 -1
  157. package/dist/library/themes/themeDocument.d.ts.map +1 -1
  158. package/dist/library/themes/themeDocument.js +11 -5
  159. package/dist/library/themes/themeDocument.js.map +1 -1
  160. package/dist/library/themes/themeDocumentTypes.d.ts +5 -1
  161. package/dist/library/themes/themeDocumentTypes.d.ts.map +1 -1
  162. package/dist/library/themes/themeDocumentTypes.js.map +1 -1
  163. package/docs/accessibility.md +76 -142
  164. package/docs/architecture.md +197 -0
  165. package/docs/motion.md +153 -0
  166. package/docs/theming.md +275 -882
  167. package/package.json +22 -7
  168. package/src/Accordion.tsx +4 -1
  169. package/src/Button.tsx +55 -1
  170. package/src/Card.tsx +3 -1
  171. package/src/Checkbox.tsx +7 -2
  172. package/src/Collapsible.tsx +38 -2
  173. package/src/Dialog.tsx +13 -1
  174. package/src/Field.tsx +8 -0
  175. package/src/Input.tsx +3 -0
  176. package/src/Link.tsx +5 -0
  177. package/src/RadioGroup.tsx +10 -2
  178. package/src/ThemeScope.tsx +21 -17
  179. package/src/{themes → examples}/expressive.ts +5 -3
  180. package/src/{themes → examples}/glass.ts +5 -3
  181. package/src/examples/index.ts +35 -0
  182. package/src/{themes → examples}/jelly.ts +5 -3
  183. package/src/{themes → examples}/radiant.ts +5 -3
  184. package/src/{themes → examples}/sharp-3d.ts +5 -3
  185. package/src/{themes → examples}/soft-squircle.ts +5 -3
  186. package/src/{themes → examples}/ticket.ts +5 -3
  187. package/src/index.ts +10 -13
  188. package/src/motion/MotionLink.tsx +69 -0
  189. package/src/motion/browser.ts +45 -0
  190. package/src/motion/config.ts +79 -0
  191. package/src/motion/context.ts +4 -0
  192. package/src/motion/drag.ts +151 -0
  193. package/src/motion/dragBrowser.ts +159 -0
  194. package/src/motion/follow.ts +28 -0
  195. package/src/motion/host.ts +96 -0
  196. package/src/motion/index.ts +28 -0
  197. package/src/motion/layout.ts +78 -0
  198. package/src/motion/layoutDom.ts +293 -0
  199. package/src/motion/press.ts +97 -0
  200. package/src/motion/pressPolicy.ts +14 -0
  201. package/src/motion/react.ts +272 -0
  202. package/src/motion/runtime.ts +122 -0
  203. package/src/motion/transitions.ts +237 -0
  204. package/src/themes/defineTheme.ts +10 -2
  205. package/src/themes/index.ts +2 -22
  206. package/src/themes/themeDocument.ts +14 -6
  207. package/src/themes/themeDocumentTypes.ts +12 -5
  208. package/dist/library/themes/expressive.d.ts +0 -2
  209. package/dist/library/themes/expressive.d.ts.map +0 -1
  210. package/dist/library/themes/expressive.js.map +0 -1
  211. package/dist/library/themes/glass.d.ts +0 -2
  212. package/dist/library/themes/glass.d.ts.map +0 -1
  213. package/dist/library/themes/glass.js.map +0 -1
  214. package/dist/library/themes/jelly.d.ts +0 -2
  215. package/dist/library/themes/jelly.d.ts.map +0 -1
  216. package/dist/library/themes/jelly.js.map +0 -1
  217. package/dist/library/themes/preset-values.d.ts.map +0 -1
  218. package/dist/library/themes/preset-values.js.map +0 -1
  219. package/dist/library/themes/radiant.d.ts +0 -2
  220. package/dist/library/themes/radiant.d.ts.map +0 -1
  221. package/dist/library/themes/radiant.js.map +0 -1
  222. package/dist/library/themes/sharp-3d.d.ts +0 -2
  223. package/dist/library/themes/sharp-3d.d.ts.map +0 -1
  224. package/dist/library/themes/sharp-3d.js.map +0 -1
  225. package/dist/library/themes/soft-squircle.d.ts +0 -2
  226. package/dist/library/themes/soft-squircle.d.ts.map +0 -1
  227. package/dist/library/themes/soft-squircle.js.map +0 -1
  228. package/dist/library/themes/ticket.d.ts +0 -2
  229. package/dist/library/themes/ticket.d.ts.map +0 -1
  230. package/dist/library/themes/ticket.js.map +0 -1
  231. package/docs/system.md +0 -238
  232. /package/dist/library/{themes → examples}/preset-values.d.ts +0 -0
  233. /package/dist/library/{themes → examples}/preset-values.js +0 -0
  234. /package/src/{themes → examples}/preset-values.ts +0 -0
@@ -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,237 @@
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
+ const observed = new Set<HTMLElement>()
27
+ function sync() {
28
+ const current = new Set(contents())
29
+ for (const child of observed) {
30
+ if (current.has(child)) continue
31
+ resize.unobserve(child)
32
+ restore(child)
33
+ observed.delete(child)
34
+ }
35
+ for (const child of current) {
36
+ if (!observed.has(child)) {
37
+ observed.add(child)
38
+ resize.observe(child)
39
+ }
40
+ if (!node.open) {
41
+ if (!saved.has(child))
42
+ saved.set(child, { inert: child.inert, hidden: child.getAttribute('aria-hidden') })
43
+ child.inert = true
44
+ child.setAttribute('aria-hidden', 'true')
45
+ } else restore(child)
46
+ }
47
+ spring.to(node.open ? measure() : 0)
48
+ }
49
+ function restore(child: HTMLElement) {
50
+ const prior = saved.get(child)
51
+ if (!prior) return
52
+ child.inert = prior.inert
53
+ if (prior.hidden === null) child.removeAttribute('aria-hidden')
54
+ else child.setAttribute('aria-hidden', prior.hidden)
55
+ saved.delete(child)
56
+ }
57
+ const observer = new MutationObserver(sync)
58
+ observer.observe(node, { attributes: true, attributeFilter: ['open'], childList: true })
59
+ const resize = new ResizeObserver(sync)
60
+ function update(next: MotionProfile) {
61
+ profile = next
62
+ spring.configure(resolveMotion(profile, 'disclosure', query.matches))
63
+ sync()
64
+ }
65
+ const preference = () => update(profile)
66
+ query.addEventListener('change', preference)
67
+ sync()
68
+ return {
69
+ update,
70
+ destroy() {
71
+ observer.disconnect()
72
+ resize.disconnect()
73
+ observed.clear()
74
+ query.removeEventListener('change', preference)
75
+ off()
76
+ spring.destroy()
77
+ for (const child of saved.keys()) restore(child)
78
+ if (previous) node.style.setProperty('--_rad-spring-details-height', previous)
79
+ else node.style.removeProperty('--_rad-spring-details-height')
80
+ }
81
+ }
82
+ }
83
+
84
+ export function bindDialogMotion(host: HTMLElement, initial: MotionProfile): MotionHostBinding {
85
+ const node = host as HTMLDialogElement
86
+ let profile = initial
87
+ const query = node.ownerDocument.defaultView!.matchMedia('(prefers-reduced-motion: reduce)')
88
+ let open = node.open
89
+ const geometry = () => {
90
+ const style = getComputedStyle(node)
91
+ return {
92
+ left: node.offsetLeft,
93
+ top: node.offsetTop,
94
+ width: node.offsetWidth,
95
+ height: node.offsetHeight,
96
+ variables: Array.from(style)
97
+ .filter(key => key.startsWith('--'))
98
+ .map(key => [key, style.getPropertyValue(key)])
99
+ }
100
+ }
101
+ let bounds = node.open ? geometry() : null
102
+ let ghost: HTMLDivElement | null = null
103
+ let exitTimer: number | undefined
104
+ const spring = createSpring(open ? 1 : 0.94, {
105
+ ...resolveMotion(profile, 'dialog', query.matches),
106
+ reducedValue: open ? 1 : 0.94
107
+ })
108
+ const unbind = bindSpringStyle(node, spring, 'scale')
109
+ function clear() {
110
+ node.ownerDocument.defaultView!.clearTimeout(exitTimer)
111
+ exitTimer = undefined
112
+ ghost?.remove()
113
+ ghost = null
114
+ }
115
+ function snapshot() {
116
+ clear()
117
+ if (!bounds || bounds.width === 0) return
118
+ const descendants = node.querySelectorAll('*')
119
+ // A visual copy is optional: bound its work and avoid activating nested platform lifecycles.
120
+ if (
121
+ descendants.length > 500 ||
122
+ node.hasAttribute('is') ||
123
+ node.parentElement?.closest('dialog[open]') ||
124
+ Array.from(descendants).some(
125
+ child =>
126
+ child.localName.includes('-') ||
127
+ child.hasAttribute('is') ||
128
+ /^(dialog|script|style|link|iframe|object|embed|canvas|video|audio)$/.test(
129
+ child.localName
130
+ )
131
+ )
132
+ )
133
+ return
134
+ const copy = node.cloneNode(true) as HTMLDialogElement
135
+ ghost = node.ownerDocument.createElement('div')
136
+ for (const attribute of copy.attributes) ghost.setAttribute(attribute.name, attribute.value)
137
+ while (copy.firstChild) ghost.append(copy.firstChild)
138
+ ghost.setAttribute('open', '')
139
+ ghost.removeAttribute('data-slot')
140
+ ghost.setAttribute('role', 'presentation')
141
+ ghost.setAttribute('data-state', 'open')
142
+ ghost.inert = true
143
+ ghost.setAttribute('aria-hidden', 'true')
144
+ ghost.setAttribute('data-rad-motion-exit', '')
145
+ for (const child of [ghost, ...ghost.querySelectorAll('*')]) {
146
+ for (const attribute of Array.from(child.attributes))
147
+ if (attribute.name.toLowerCase().startsWith('on')) child.removeAttribute(attribute.name)
148
+ for (const attribute of ['id', 'name', 'form', 'autofocus']) child.removeAttribute(attribute)
149
+ if (
150
+ child instanceof HTMLInputElement ||
151
+ child instanceof HTMLButtonElement ||
152
+ child instanceof HTMLSelectElement ||
153
+ child instanceof HTMLTextAreaElement
154
+ )
155
+ child.disabled = true
156
+ }
157
+ for (const [key, value] of bounds.variables) ghost.style.setProperty(key, value)
158
+ ghost.style.font = getComputedStyle(node).font
159
+ ghost.style.colorScheme = getComputedStyle(node).colorScheme
160
+ Object.assign(ghost.style, {
161
+ position: 'fixed',
162
+ margin: '0px',
163
+ left: `${bounds.left}px`,
164
+ top: `${bounds.top}px`,
165
+ width: `${bounds.width}px`,
166
+ height: `${bounds.height}px`,
167
+ maxHeight: 'none',
168
+ pointerEvents: 'none',
169
+ zIndex: '2147483647',
170
+ transition: 'none'
171
+ })
172
+ node.ownerDocument.body.append(ghost)
173
+ // Theme physics can be arbitrarily slow. Never retain an invisible copy or engine work indefinitely.
174
+ exitTimer = node.ownerDocument.defaultView!.setTimeout(() => {
175
+ clear()
176
+ if (!node.open) spring.jump(0.94)
177
+ }, 2000)
178
+ }
179
+ const off = spring.subscribe(value => {
180
+ if (ghost) {
181
+ ghost.style.scale = String(Math.max(0, value))
182
+ ghost.style.opacity = String(Math.max(0, Math.min(1, (value - 0.94) / 0.06)))
183
+ if (!node.open && Math.abs(value - 0.94) < 0.0001) clear()
184
+ }
185
+ })
186
+ function sync() {
187
+ if (node.open === open) return
188
+ open = node.open
189
+ const enabled = resolveMotion(profile, 'dialog', query.matches).enabled
190
+ if (open) {
191
+ clear()
192
+ bounds = geometry()
193
+ spring.configure({ reducedValue: 1 })
194
+ spring.to(1)
195
+ } else {
196
+ if (enabled) snapshot()
197
+ spring.configure({ reducedValue: 0.94 })
198
+ if (ghost) spring.to(0.94)
199
+ else spring.jump(0.94)
200
+ if (!enabled) clear()
201
+ }
202
+ }
203
+ const beforeToggle = () => {
204
+ if (node.open) bounds = geometry()
205
+ }
206
+ node.addEventListener('beforetoggle', beforeToggle)
207
+ const observer = new MutationObserver(sync)
208
+ observer.observe(node, { attributes: true, attributeFilter: ['open'] })
209
+ const resize = new ResizeObserver(() => {
210
+ if (node.open) bounds = geometry()
211
+ })
212
+ resize.observe(node)
213
+ function update(next: MotionProfile) {
214
+ profile = next
215
+ spring.configure({
216
+ ...resolveMotion(profile, 'dialog', query.matches),
217
+ reducedValue: node.open ? 1 : 0.94
218
+ })
219
+ if (!resolveMotion(profile, 'dialog', query.matches).enabled) clear()
220
+ sync()
221
+ }
222
+ const preference = () => update(profile)
223
+ query.addEventListener('change', preference)
224
+ return {
225
+ update,
226
+ destroy() {
227
+ observer.disconnect()
228
+ resize.disconnect()
229
+ query.removeEventListener('change', preference)
230
+ off()
231
+ spring.destroy()
232
+ unbind()
233
+ node.removeEventListener('beforetoggle', beforeToggle)
234
+ clear()
235
+ }
236
+ }
237
+ }
@@ -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
  }