@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,88 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { createContext, useContext, useEffect, useRef, 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?(): 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
|
+
press(event: PointerEvent | KeyboardEvent) {
|
|
50
|
+
binding.current?.value.press?.(event)
|
|
51
|
+
},
|
|
52
|
+
release() {
|
|
53
|
+
binding.current?.value.release?.()
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Shared lifecycle for native presentation adapters; no engine enters the root path. */
|
|
59
|
+
export function useTransitionHost<Node extends HTMLElement>(
|
|
60
|
+
kind: 'disclosure' | 'dialog',
|
|
61
|
+
ref: Ref<Node> | undefined,
|
|
62
|
+
disabled: boolean
|
|
63
|
+
) {
|
|
64
|
+
const runtime = useContext(MotionHostContext)
|
|
65
|
+
const profile = useContext(MotionProfileContext)
|
|
66
|
+
const node = useRef<Node>(null)
|
|
67
|
+
const binding = useRef<{ node: Node; value: MotionHostBinding } | null>(null)
|
|
68
|
+
const composed = useComposedRefs(node, ref)
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
const attach = runtime?.[kind]
|
|
71
|
+
if (disabled || !attach || !node.current || binding.current?.node !== node.current) {
|
|
72
|
+
binding.current?.value.destroy()
|
|
73
|
+
binding.current = null
|
|
74
|
+
}
|
|
75
|
+
if (!disabled && attach && node.current) {
|
|
76
|
+
binding.current ??= { node: node.current, value: attach(node.current, profile) }
|
|
77
|
+
binding.current.value.update(profile)
|
|
78
|
+
}
|
|
79
|
+
})
|
|
80
|
+
useEffect(
|
|
81
|
+
() => () => {
|
|
82
|
+
binding.current?.value.destroy()
|
|
83
|
+
binding.current = null
|
|
84
|
+
},
|
|
85
|
+
[]
|
|
86
|
+
)
|
|
87
|
+
return { ref: composed, active: !disabled && runtime?.[kind] !== undefined }
|
|
88
|
+
}
|
|
@@ -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,77 @@
|
|
|
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
|
+
const blocked = () =>
|
|
19
|
+
node.matches(':disabled') ||
|
|
20
|
+
node.getAttribute('aria-busy') === 'true' ||
|
|
21
|
+
node.getAttribute('aria-disabled') === 'true'
|
|
22
|
+
const release = () => spring.to(1)
|
|
23
|
+
function update(next: MotionProfile) {
|
|
24
|
+
profile = next
|
|
25
|
+
spring.configure({ ...resolveMotion(profile, 'press', query.matches), reducedValue: 1 })
|
|
26
|
+
if (blocked()) spring.jump(1)
|
|
27
|
+
}
|
|
28
|
+
const changed = () => update(profile)
|
|
29
|
+
query.addEventListener('change', changed)
|
|
30
|
+
const observer = new MutationObserver(() => {
|
|
31
|
+
if (blocked()) spring.jump(1)
|
|
32
|
+
})
|
|
33
|
+
observer.observe(node, {
|
|
34
|
+
attributes: true,
|
|
35
|
+
attributeFilter: ['disabled', 'aria-busy', 'aria-disabled']
|
|
36
|
+
})
|
|
37
|
+
for (let parent = node.parentElement; parent; parent = parent.parentElement) {
|
|
38
|
+
if (parent.tagName === 'FIELDSET')
|
|
39
|
+
observer.observe(parent, { attributes: true, attributeFilter: ['disabled'] })
|
|
40
|
+
}
|
|
41
|
+
const document = node.ownerDocument
|
|
42
|
+
document.addEventListener('pointerup', release)
|
|
43
|
+
document.addEventListener('pointercancel', release)
|
|
44
|
+
document.addEventListener('keyup', release)
|
|
45
|
+
document.defaultView!.addEventListener('blur', release)
|
|
46
|
+
return {
|
|
47
|
+
update,
|
|
48
|
+
press(event) {
|
|
49
|
+
if (event.defaultPrevented || blocked()) {
|
|
50
|
+
release()
|
|
51
|
+
return
|
|
52
|
+
}
|
|
53
|
+
if ('key' in event) {
|
|
54
|
+
if (
|
|
55
|
+
event.repeat ||
|
|
56
|
+
!['Enter', ...(node.tagName === 'BUTTON' ? [' '] : [])].includes(event.key)
|
|
57
|
+
)
|
|
58
|
+
return
|
|
59
|
+
} else if (event.button !== 0 || event.isPrimary === false) return
|
|
60
|
+
const scale = Number(
|
|
61
|
+
getComputedStyle(node).getPropertyValue('--rad-button-press-scale').trim() || '.97'
|
|
62
|
+
)
|
|
63
|
+
spring.to(Number.isFinite(scale) && scale >= 0.5 && scale <= 1 ? scale : 0.97)
|
|
64
|
+
},
|
|
65
|
+
release,
|
|
66
|
+
destroy() {
|
|
67
|
+
observer.disconnect()
|
|
68
|
+
query.removeEventListener('change', changed)
|
|
69
|
+
document.removeEventListener('pointerup', release)
|
|
70
|
+
document.removeEventListener('pointercancel', release)
|
|
71
|
+
document.removeEventListener('keyup', release)
|
|
72
|
+
document.defaultView!.removeEventListener('blur', release)
|
|
73
|
+
spring.destroy()
|
|
74
|
+
unbind()
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { CSSProperties } from 'react'
|
|
2
|
+
export function callerOwnsPress(style: CSSProperties | undefined, className: string | undefined) {
|
|
3
|
+
return (
|
|
4
|
+
style?.scale !== undefined ||
|
|
5
|
+
/(?:scale-|\[scale:|transition-all|transition-\[[^\]]*scale)/.test(className ?? '') ||
|
|
6
|
+
/(?:scale|all)/.test(String(style?.transition ?? style?.transitionProperty ?? ''))
|
|
7
|
+
)
|
|
8
|
+
}
|
|
9
|
+
export const springPressClass =
|
|
10
|
+
'transition-[translate,border-radius,corner-shape,box-shadow,background-color,color] not-disabled:not-aria-busy:motion-safe:active:scale-100'
|
|
11
|
+
export const offPressClass =
|
|
12
|
+
'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'
|