@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,28 @@
1
+ import type { SpringController } from './runtime.js'
2
+
3
+ /** Synchronous numeric snapshot with notifications in the same units as the follower. */
4
+ export interface SpringSource {
5
+ get(): number
6
+ subscribe(listener: (value: number) => void): () => void
7
+ }
8
+ /** The source owns destinations; the existing spring owns position, momentum and physics. */
9
+ export function bindSpringSource(spring: SpringController, source: SpringSource): () => void {
10
+ if (source === spring) throw new TypeError('A spring cannot follow itself.')
11
+ let active = true
12
+ const off = source.subscribe(value => {
13
+ if (active) spring.to(value)
14
+ })
15
+ try {
16
+ // Read after subscribing so a synchronous subscription cannot leave a stale goal.
17
+ spring.to(source.get())
18
+ } catch (error) {
19
+ active = false
20
+ off()
21
+ throw error
22
+ }
23
+ return () => {
24
+ if (!active) return
25
+ active = false
26
+ off()
27
+ }
28
+ }
@@ -0,0 +1,96 @@
1
+ 'use client'
2
+ import { createContext, useContext, useEffect, useRef, useState, type Ref } from 'react'
3
+ import type { MotionProfile } from './config.js'
4
+ import { MotionProfileContext } from './context.js'
5
+ import { useComposedRefs } from '../react/useComposedRefs.js'
6
+
7
+ export interface MotionHostBinding {
8
+ update(profile: MotionProfile): void
9
+ press?(event: PointerEvent | KeyboardEvent): void
10
+ release?(event?: PointerEvent | KeyboardEvent): void
11
+ destroy(): void
12
+ }
13
+ export interface MotionHostRuntime {
14
+ press(node: HTMLElement, profile: MotionProfile): MotionHostBinding
15
+ disclosure?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
16
+ dialog?(node: HTMLElement, profile: MotionProfile): MotionHostBinding
17
+ }
18
+ export const MotionHostContext = createContext<MotionHostRuntime | null>(null)
19
+ /** Engine-free bridge: only the opt-in provider supplies implementation. */
20
+ export function usePressHost<Node extends HTMLElement>(
21
+ ref: Ref<Node> | undefined,
22
+ disabled: boolean
23
+ ) {
24
+ const runtime = useContext(MotionHostContext)
25
+ const profile = useContext(MotionProfileContext)
26
+ const node = useRef<Node>(null)
27
+ const binding = useRef<{ node: HTMLElement; value: MotionHostBinding } | null>(null)
28
+ const composed = useComposedRefs(node, ref)
29
+ useEffect(() => {
30
+ if (disabled || !runtime || !node.current || binding.current?.node !== node.current) {
31
+ binding.current?.value.destroy()
32
+ binding.current = null
33
+ }
34
+ if (!disabled && runtime && node.current) {
35
+ binding.current ??= { node: node.current, value: runtime.press(node.current, profile) }
36
+ binding.current.value.update(profile)
37
+ }
38
+ })
39
+ useEffect(
40
+ () => () => {
41
+ binding.current?.value.destroy()
42
+ binding.current = null
43
+ },
44
+ []
45
+ )
46
+ return {
47
+ ref: composed,
48
+ active: runtime !== null && !disabled,
49
+ off: profile.mode === 'off',
50
+ press(event: PointerEvent | KeyboardEvent) {
51
+ binding.current?.value.press?.(event)
52
+ },
53
+ release(event?: PointerEvent | KeyboardEvent) {
54
+ binding.current?.value.release?.(event)
55
+ }
56
+ }
57
+ }
58
+
59
+ /** Shared lifecycle for native presentation adapters; no engine enters the root path. */
60
+ export function useTransitionHost<Node extends HTMLElement>(
61
+ kind: 'disclosure' | 'dialog',
62
+ ref: Ref<Node> | undefined,
63
+ disabled: boolean
64
+ ) {
65
+ const runtime = useContext(MotionHostContext)
66
+ const profile = useContext(MotionProfileContext)
67
+ const node = useRef<Node>(null)
68
+ const binding = useRef<{ node: Node; value: MotionHostBinding } | null>(null)
69
+ const composed = useComposedRefs(node, ref)
70
+ const [ready, setReady] = useState(false)
71
+ useEffect(() => {
72
+ const attach = runtime?.[kind]
73
+ if (disabled || !attach || !node.current || binding.current?.node !== node.current) {
74
+ binding.current?.value.destroy()
75
+ binding.current = null
76
+ }
77
+ if (!disabled && attach && node.current) {
78
+ binding.current ??= { node: node.current, value: attach(node.current, profile) }
79
+ binding.current.value.update(profile)
80
+ }
81
+ setReady(binding.current !== null)
82
+ }, [disabled, kind, runtime, profile, composed])
83
+ useEffect(
84
+ () => () => {
85
+ binding.current?.value.destroy()
86
+ binding.current = null
87
+ },
88
+ []
89
+ )
90
+ return {
91
+ ref: composed,
92
+ active: !disabled && runtime?.[kind] !== undefined,
93
+ ready,
94
+ off: profile.mode === 'off'
95
+ }
96
+ }
@@ -0,0 +1,28 @@
1
+ export {
2
+ defineMotionProfile,
3
+ defaultMotionProfile,
4
+ motionInteractions,
5
+ resolveMotion,
6
+ resolveSpringParameters
7
+ } from './config.js'
8
+ export type {
9
+ MotionProfile,
10
+ MotionProfileInput,
11
+ MotionInteraction,
12
+ SpringParameters
13
+ } from './config.js'
14
+ export { createSpring } from './runtime.js'
15
+ export type { SpringController, SpringOptions } from './runtime.js'
16
+ export { bindSpringStyle } from './browser.js'
17
+ export type { SpringStyleProperty } from './browser.js'
18
+ export { createLayoutSpring } from './layout.js'
19
+ export type { LayoutBounds } from './layout.js'
20
+ export { createSpringDrag } from './drag.js'
21
+ export type { SpringDragOptions, SpringDragController } from './drag.js'
22
+ export { bindSpringDrag } from './dragBrowser.js'
23
+
24
+ export { bindSpringSource } from './follow.js'
25
+ export type { SpringSource } from './follow.js'
26
+
27
+ export { bindSpringLayout } from './layoutDom.js'
28
+ export type { SpringLayoutController } from './layoutDom.js'
@@ -0,0 +1,78 @@
1
+ import { createSpring, type SpringOptions } from './runtime.js'
2
+
3
+ export interface LayoutBounds {
4
+ readonly x: number
5
+ readonly y: number
6
+ readonly width: number
7
+ readonly height: number
8
+ }
9
+ const axes = ['x', 'y', 'width', 'height'] as const
10
+ function validate(bounds: LayoutBounds) {
11
+ for (const axis of axes) {
12
+ if (
13
+ !Number.isFinite(bounds[axis]) ||
14
+ ((axis === 'width' || axis === 'height') && bounds[axis] < 0)
15
+ )
16
+ throw new TypeError(
17
+ 'Layout bounds require finite local pixel coordinates and nonnegative dimensions.'
18
+ )
19
+ }
20
+ }
21
+ /** Measured local-pixel foundation; measurement and host projection belong to the layout adapter. */
22
+ export function createLayoutSpring(initial: LayoutBounds, options: SpringOptions = {}) {
23
+ validate(initial)
24
+ if (options.reducedValue !== undefined)
25
+ throw new TypeError('Layout off mode snaps to measured bounds; reducedValue is scalar only.')
26
+ const values = {
27
+ x: createSpring(initial.x, options),
28
+ y: createSpring(initial.y, options),
29
+ width: createSpring(initial.width, options),
30
+ height: createSpring(initial.height, options)
31
+ }
32
+ return {
33
+ get: (): LayoutBounds => ({
34
+ x: values.x.get(),
35
+ y: values.y.get(),
36
+ width: values.width.get(),
37
+ height: values.height.get()
38
+ }),
39
+ getVelocity: (): LayoutBounds => ({
40
+ x: values.x.getVelocity(),
41
+ y: values.y.getVelocity(),
42
+ width: values.width.getVelocity(),
43
+ height: values.height.getVelocity()
44
+ }),
45
+ isAnimating: () => axes.some(axis => values[axis].isAnimating()),
46
+ to(next: LayoutBounds) {
47
+ validate(next)
48
+ axes.forEach(axis => values[axis].to(next[axis]))
49
+ },
50
+ jump(next: LayoutBounds) {
51
+ validate(next)
52
+ axes.forEach(axis => values[axis].jump(next[axis]))
53
+ },
54
+ configure(next: SpringOptions) {
55
+ if (next.reducedValue !== undefined)
56
+ throw new TypeError('Layout off mode snaps to measured bounds.')
57
+ // Validate all options before changing any axis.
58
+ axes.forEach(axis => values[axis].configure(next))
59
+ },
60
+ subscribe(listener: (bounds: LayoutBounds) => void) {
61
+ const snapshot = () =>
62
+ listener({
63
+ x: values.x.get(),
64
+ y: values.y.get(),
65
+ width: values.width.get(),
66
+ height: values.height.get()
67
+ })
68
+ const off = axes.map(axis => values[axis].subscribe(snapshot))
69
+ return () => off.forEach(remove => remove())
70
+ },
71
+ stop() {
72
+ axes.forEach(axis => values[axis].stop())
73
+ },
74
+ destroy() {
75
+ axes.forEach(axis => values[axis].destroy())
76
+ }
77
+ }
78
+ }
@@ -0,0 +1,293 @@
1
+ import { createLayoutSpring, type LayoutBounds } from './layout.js'
2
+ import { resolveSpringParameters } from './config.js'
3
+ import type { SpringOptions } from './runtime.js'
4
+
5
+ export interface SpringLayoutController {
6
+ /** Measure all natural-flow slots together and retarget their persistent springs. */
7
+ measure(): void
8
+ configure(options: SpringOptions): void
9
+ get(slot: HTMLElement): LayoutBounds | undefined
10
+ getVelocity(slot: HTMLElement): LayoutBounds | undefined
11
+ isAnimating(): boolean
12
+ destroy(): void
13
+ }
14
+ /** Inline property lease. Capture the latest author value on each projection. */
15
+ function styles(node: HTMLElement) {
16
+ const previous = new Map<string, { value: string; priority: string }>()
17
+ const written = new Map<string, string>()
18
+ return {
19
+ set(key: string, value: string) {
20
+ if (!written.has(key))
21
+ previous.set(key, {
22
+ value: node.style.getPropertyValue(key),
23
+ priority: node.style.getPropertyPriority(key)
24
+ })
25
+ node.style.setProperty(key, value)
26
+ written.set(key, node.style.getPropertyValue(key))
27
+ },
28
+ restore() {
29
+ for (const [key, value] of written) {
30
+ if (node.style.getPropertyValue(key) !== value) continue
31
+ const saved = previous.get(key)!
32
+ if (saved.value) node.style.setProperty(key, saved.value, saved.priority)
33
+ else node.style.removeProperty(key)
34
+ }
35
+ written.clear()
36
+ }
37
+ }
38
+ }
39
+ type Entry = {
40
+ part: HTMLElement
41
+ slotStyle: ReturnType<typeof styles>
42
+ partStyle: ReturnType<typeof styles>
43
+ spring: ReturnType<typeof createLayoutSpring>
44
+ target: LayoutBounds
45
+ }
46
+ /**
47
+ * Direct-child natural-flow slots with one dedicated motion part each.
48
+ * Never clone/reparent a semantic host or scale text, borders or children.
49
+ */
50
+ export function bindSpringLayout(
51
+ root: HTMLElement,
52
+ options: SpringOptions = {}
53
+ ): SpringLayoutController {
54
+ if (options.reducedValue !== undefined)
55
+ throw new TypeError('Layout off mode snaps to measured bounds.')
56
+ const win = root.ownerDocument.defaultView!
57
+ const entries = new Map<HTMLElement, Entry>()
58
+ let currentOptions = { ...options, parameters: resolveSpringParameters(options.parameters) }
59
+ let frame: number | undefined
60
+ let needsMeasure = false
61
+ let destroyed = false
62
+ const resize = new win.ResizeObserver(schedule)
63
+ function contentChanged(records: MutationRecord[]) {
64
+ if (
65
+ records.some(record => {
66
+ // These style mutations are our own projection writes. Other content/style
67
+ // mutations can change natural layout and must be measured as a group.
68
+ if (record.type === 'attributes' && record.attributeName === 'style') {
69
+ if (entries.has(record.target as HTMLElement)) return false
70
+ if (Array.from(entries.values()).some(entry => entry.part === record.target)) return false
71
+ }
72
+ return true
73
+ })
74
+ )
75
+ schedule()
76
+ }
77
+ const mutations = new win.MutationObserver(contentChanged)
78
+ function restore(entry: Entry) {
79
+ entry.partStyle.restore()
80
+ entry.slotStyle.restore()
81
+ }
82
+ function dispose(slot: HTMLElement, entry: Entry) {
83
+ restore(entry)
84
+ entry.spring.destroy()
85
+ resize.unobserve(slot)
86
+ entries.delete(slot)
87
+ }
88
+ function draw() {
89
+ // Preserve external mutations queued by an earlier frame callback before
90
+ // discarding the records generated by our own presentation writes.
91
+ contentChanged(mutations.takeRecords())
92
+ for (const entry of entries.values()) {
93
+ const bounds = entry.spring.get()
94
+ entry.partStyle.set('width', `${Math.max(0, bounds.width)}px`)
95
+ entry.partStyle.set('height', `${Math.max(0, bounds.height)}px`)
96
+ entry.partStyle.set(
97
+ 'translate',
98
+ `${bounds.x - entry.target.x}px ${bounds.y - entry.target.y}px`
99
+ )
100
+ }
101
+ mutations.takeRecords()
102
+ }
103
+ function project(slot: HTMLElement, entry: Entry) {
104
+ entry.slotStyle.set('position', 'relative')
105
+ entry.slotStyle.set('box-sizing', 'border-box')
106
+ entry.slotStyle.set('flex', 'none')
107
+ entry.slotStyle.set('width', `${entry.target.width}px`)
108
+ entry.slotStyle.set('height', `${entry.target.height}px`)
109
+ entry.partStyle.set('position', 'absolute')
110
+ entry.partStyle.set('left', '0px')
111
+ entry.partStyle.set('top', '0px')
112
+ entry.partStyle.set('box-sizing', 'border-box')
113
+ }
114
+ function measure() {
115
+ if (destroyed) return
116
+ needsMeasure = false
117
+ for (let ancestor: HTMLElement | null = root; ancestor; ancestor = ancestor.parentElement) {
118
+ const css = win.getComputedStyle(ancestor)
119
+ const matrix = css.transform === 'none' ? undefined : new win.DOMMatrixReadOnly(css.transform)
120
+ if (
121
+ (matrix &&
122
+ (!matrix.is2D || matrix.a !== 1 || matrix.b !== 0 || matrix.c !== 0 || matrix.d !== 1)) ||
123
+ (css.scale !== 'none' && css.scale.split(' ').some(value => Number(value) !== 1)) ||
124
+ (css.rotate !== 'none' && parseFloat(css.rotate) !== 0) ||
125
+ (css.zoom && Number(css.zoom) !== 1) ||
126
+ css.perspective !== 'none'
127
+ )
128
+ throw new TypeError('Layout measurement requires an unscaled, unrotated coordinate space.')
129
+ }
130
+ // Release every reservation before any geometry read. Measuring one item
131
+ // while its siblings are projected would feed animation back into layout.
132
+ entries.forEach(restore)
133
+ const slots = Array.from(root.children).filter(
134
+ (node): node is HTMLElement =>
135
+ node instanceof win.HTMLElement && node.hasAttribute('data-rad-layout-slot')
136
+ )
137
+ for (const [slot, entry] of entries) {
138
+ if (!slots.includes(slot) || slot.firstElementChild !== entry.part) dispose(slot, entry)
139
+ }
140
+ const origin = root.getBoundingClientRect()
141
+ const targets = slots.map(slot => {
142
+ const part = slot.firstElementChild
143
+ if (
144
+ slot.children.length !== 1 ||
145
+ !(part instanceof win.HTMLElement) ||
146
+ !part.hasAttribute('data-rad-layout-part')
147
+ )
148
+ throw new TypeError('Each layout slot requires a first-child data-rad-layout-part.')
149
+ const css = win.getComputedStyle(slot)
150
+ if (
151
+ !['static', 'relative'].includes(css.position) ||
152
+ ['inline', 'contents'].includes(css.display) ||
153
+ [
154
+ 'borderLeftWidth',
155
+ 'borderTopWidth',
156
+ 'borderRightWidth',
157
+ 'borderBottomWidth',
158
+ 'paddingLeft',
159
+ 'paddingTop',
160
+ 'paddingRight',
161
+ 'paddingBottom'
162
+ ].some(key => parseFloat(css[key as keyof CSSStyleDeclaration] as string) > 0)
163
+ )
164
+ throw new TypeError('Layout slots must be unpadded, borderless normal-flow wrappers.')
165
+ const partCss = win.getComputedStyle(part)
166
+ if (
167
+ !['static', 'relative'].includes(partCss.position) ||
168
+ [css, partCss].some(
169
+ style =>
170
+ style.transform !== 'none' ||
171
+ style.translate !== 'none' ||
172
+ style.scale !== 'none' ||
173
+ style.rotate !== 'none'
174
+ ) ||
175
+ ['marginLeft', 'marginTop', 'marginRight', 'marginBottom'].some(
176
+ key => parseFloat(partCss[key as keyof CSSStyleDeclaration] as string) !== 0
177
+ )
178
+ )
179
+ throw new TypeError(
180
+ 'Use dedicated transform-free layout slots and motion parts with no part margins.'
181
+ )
182
+ const rect = slot.getBoundingClientRect()
183
+ return {
184
+ slot,
185
+ part,
186
+ bounds: {
187
+ x: rect.left - origin.left - root.clientLeft + root.scrollLeft,
188
+ y: rect.top - origin.top - root.clientTop + root.scrollTop,
189
+ width: rect.width,
190
+ height: rect.height
191
+ }
192
+ }
193
+ })
194
+ for (const { slot, part, bounds } of targets) {
195
+ let entry = entries.get(slot)
196
+ if (!entry) {
197
+ entry = {
198
+ part,
199
+ slotStyle: styles(slot),
200
+ partStyle: styles(part),
201
+ spring: createLayoutSpring(bounds, currentOptions),
202
+ target: bounds
203
+ }
204
+ entries.set(slot, entry)
205
+ resize.observe(slot)
206
+ } else {
207
+ entry.target = bounds
208
+ entry.spring.to(bounds)
209
+ }
210
+ project(slot, entry)
211
+ }
212
+ mutations.takeRecords()
213
+ draw()
214
+ if (controller.isAnimating()) requestFrame()
215
+ else entries.forEach(restore)
216
+ mutations.takeRecords()
217
+ }
218
+ function tick() {
219
+ frame = undefined
220
+ if (destroyed) return
221
+ if (needsMeasure) measure()
222
+ else draw()
223
+ if (controller.isAnimating()) requestFrame()
224
+ else {
225
+ entries.forEach(restore)
226
+ mutations.takeRecords()
227
+ }
228
+ }
229
+ function requestFrame() {
230
+ if (frame === undefined) frame = win.requestAnimationFrame(tick)
231
+ }
232
+ function schedule() {
233
+ if (destroyed) return
234
+ needsMeasure = true
235
+ requestFrame()
236
+ }
237
+ const controller: SpringLayoutController = {
238
+ measure,
239
+ configure(next) {
240
+ if (destroyed) return
241
+ if (next.reducedValue !== undefined)
242
+ throw new TypeError('Layout off mode snaps to measured bounds.')
243
+ // Validate before changing any entry or remembering options for new slots.
244
+ currentOptions = {
245
+ ...currentOptions,
246
+ ...next,
247
+ parameters: resolveSpringParameters(next.parameters, currentOptions.parameters)
248
+ }
249
+ entries.forEach(entry => entry.spring.configure(next))
250
+ if (controller.isAnimating()) {
251
+ draw()
252
+ requestFrame()
253
+ } else {
254
+ entries.forEach(restore)
255
+ mutations.takeRecords()
256
+ }
257
+ },
258
+ get: slot => entries.get(slot)?.spring.get(),
259
+ getVelocity: slot => entries.get(slot)?.spring.getVelocity(),
260
+ isAnimating: () => Array.from(entries.values()).some(entry => entry.spring.isAnimating()),
261
+ destroy() {
262
+ if (destroyed) return
263
+ destroyed = true
264
+ if (frame !== undefined) win.cancelAnimationFrame(frame)
265
+ mutations.disconnect()
266
+ resize.disconnect()
267
+ root.removeEventListener('load', schedule, true)
268
+ root.removeEventListener('scroll', schedule, true)
269
+ win.removeEventListener('resize', schedule)
270
+ root.ownerDocument.fonts?.removeEventListener('loadingdone', schedule)
271
+ for (const [slot, entry] of entries) dispose(slot, entry)
272
+ }
273
+ }
274
+ try {
275
+ measure()
276
+ } catch (error) {
277
+ controller.destroy()
278
+ throw error
279
+ }
280
+ resize.observe(root)
281
+ mutations.observe(root, {
282
+ subtree: true,
283
+ childList: true,
284
+ characterData: true,
285
+ attributes: true,
286
+ attributeFilter: ['class', 'style', 'hidden', 'open']
287
+ })
288
+ root.addEventListener('load', schedule, true)
289
+ root.addEventListener('scroll', schedule, true)
290
+ win.addEventListener('resize', schedule)
291
+ root.ownerDocument.fonts?.addEventListener('loadingdone', schedule)
292
+ return controller
293
+ }
@@ -0,0 +1,97 @@
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
+ /** Presentation only: this adapter never clicks, submits, captures or prevents input. */
7
+ export function bindPressMotion(
8
+ node: HTMLElement,
9
+ initialProfile: MotionProfile
10
+ ): MotionHostBinding {
11
+ let profile = initialProfile
12
+ const query = node.ownerDocument.defaultView!.matchMedia('(prefers-reduced-motion: reduce)')
13
+ const spring = createSpring(1, {
14
+ ...resolveMotion(profile, 'press', query.matches),
15
+ reducedValue: 1
16
+ })
17
+ const unbind = bindSpringStyle(node, spring, 'scale')
18
+ let pressed: { key: string } | { pointerId: number } | null = null
19
+ const blocked = () =>
20
+ (node.tagName === 'A' && !node.hasAttribute('href')) ||
21
+ node.matches(':disabled') ||
22
+ node.getAttribute('aria-busy') === 'true' ||
23
+ node.getAttribute('aria-disabled') === 'true'
24
+ function release(event?: PointerEvent | KeyboardEvent) {
25
+ if (!pressed) return
26
+ if (event) {
27
+ if ('key' in event) {
28
+ if (!('key' in pressed) || event.key !== pressed.key) return
29
+ } else if (!('pointerId' in pressed) || event.pointerId !== pressed.pointerId) return
30
+ }
31
+ pressed = null
32
+ spring.to(1)
33
+ }
34
+ function reset() {
35
+ pressed = null
36
+ spring.jump(1)
37
+ }
38
+ function update(next: MotionProfile) {
39
+ profile = next
40
+ const resolved = resolveMotion(profile, 'press', query.matches)
41
+ spring.configure({ ...resolved, reducedValue: 1 })
42
+ if (blocked() || !resolved.enabled) reset()
43
+ }
44
+ const changed = () => update(profile)
45
+ query.addEventListener('change', changed)
46
+ const observer = new MutationObserver(() => {
47
+ if (blocked()) reset()
48
+ })
49
+ observer.observe(node, {
50
+ attributes: true,
51
+ attributeFilter: ['disabled', 'aria-busy', 'aria-disabled', 'href']
52
+ })
53
+ for (let parent = node.parentElement; parent; parent = parent.parentElement) {
54
+ if (parent.tagName === 'FIELDSET')
55
+ observer.observe(parent, { attributes: true, attributeFilter: ['disabled'] })
56
+ }
57
+ const document = node.ownerDocument
58
+ document.addEventListener('pointerup', release)
59
+ document.addEventListener('pointercancel', release)
60
+ document.addEventListener('keyup', release)
61
+ const blur = () => release()
62
+ document.defaultView!.addEventListener('blur', blur)
63
+ return {
64
+ update,
65
+ press(event) {
66
+ if (event.defaultPrevented || blocked()) return
67
+ if (!resolveMotion(profile, 'press', query.matches).enabled) return
68
+ if ('key' in event) {
69
+ if (
70
+ event.repeat ||
71
+ !['Enter', ...(node.tagName === 'BUTTON' ? [' '] : [])].includes(event.key)
72
+ )
73
+ return
74
+ if (pressed) return
75
+ pressed = { key: event.key }
76
+ } else {
77
+ if (event.button !== 0 || event.isPrimary === false || pressed) return
78
+ pressed = { pointerId: event.pointerId }
79
+ }
80
+ const scale = Number(
81
+ getComputedStyle(node).getPropertyValue('--rad-button-press-scale').trim() || '.97'
82
+ )
83
+ spring.to(Number.isFinite(scale) && scale >= 0.5 && scale <= 1 ? scale : 0.97)
84
+ },
85
+ release,
86
+ destroy() {
87
+ observer.disconnect()
88
+ query.removeEventListener('change', changed)
89
+ document.removeEventListener('pointerup', release)
90
+ document.removeEventListener('pointercancel', release)
91
+ document.removeEventListener('keyup', release)
92
+ document.defaultView!.removeEventListener('blur', blur)
93
+ spring.destroy()
94
+ unbind()
95
+ }
96
+ }
97
+ }
@@ -0,0 +1,14 @@
1
+ import type { CSSProperties } from 'react'
2
+ export function callerOwnsPress(style: CSSProperties | undefined, className: string | undefined) {
3
+ return (
4
+ style?.scale !== undefined ||
5
+ /(?:^|[\s:!])(?:-?scale-|\[scale:|transition-all(?:$|[\s!])|transition-\[[^\]]*(?:\bscale\b|\ball\b))/.test(
6
+ className ?? ''
7
+ ) ||
8
+ /\b(?:scale|all)\b/.test(`${style?.transition ?? ''} ${style?.transitionProperty ?? ''}`)
9
+ )
10
+ }
11
+ export const springPressClass =
12
+ 'transition-[translate,border-radius,corner-shape,box-shadow,background-color,color] not-disabled:not-aria-busy:motion-safe:active:scale-100'
13
+ export const offPressClass =
14
+ 'not-disabled:not-aria-busy:motion-safe:active:scale-100 not-disabled:not-aria-busy:motion-safe:active:translate-0 not-disabled:not-aria-busy:motion-safe:hover:translate-0'