@atom.io/spa 0.0.0 → 0.0.2
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/README.md +201 -6
- package/README.mdx +62 -1
- package/dist/astro-application.d.ts +1 -1
- package/dist/astro-client.d.ts +1 -1
- package/dist/astro-client.js +7 -4
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +4 -3
- package/dist/astro-server.js.map +1 -1
- package/dist/astro-shared-BT45auwR.js.map +1 -1
- package/dist/{astro-shared-DkZaqX7L.d.ts → astro-shared-DyzbUi6c.d.ts} +4 -2
- package/dist/astro-shared-DyzbUi6c.d.ts.map +1 -0
- package/dist/astro.d.ts +1 -1
- package/dist/astro.js +1 -1
- package/dist/{core-Dvqm7w2y.js → capabilities-BuZUQpiD.js} +64 -2
- package/dist/capabilities-BuZUQpiD.js.map +1 -0
- package/dist/{core-BhiA9fFi.d.ts → capabilities-yC9TsHDI.d.ts} +20 -2
- package/dist/capabilities-yC9TsHDI.d.ts.map +1 -0
- package/dist/client.min.js +1 -1
- package/dist/development/astro-client.js +7 -4
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +4 -3
- package/dist/development/astro-server.js.map +1 -1
- package/dist/development/astro-shared-BT45auwR.js.map +1 -1
- package/dist/development/astro.js +1 -1
- package/dist/development/{core-DB3sQz4h.js → capabilities-DhOJmeoJ.js} +64 -2
- package/dist/development/capabilities-DhOJmeoJ.js.map +1 -0
- package/dist/development/{dom-D9cY6l1R.js → dom-BKc6V_Kz.js} +8 -3
- package/dist/development/dom-BKc6V_Kz.js.map +1 -0
- package/dist/development/index.js +3 -38
- package/dist/development/jsx-runtime.js.map +1 -1
- package/dist/development/konva.js +121 -0
- package/dist/development/konva.js.map +1 -0
- package/dist/development/motion.js +217 -0
- package/dist/development/motion.js.map +1 -0
- package/dist/development/renderer.js +177 -0
- package/dist/development/renderer.js.map +1 -0
- package/dist/development/server.js +17 -7
- package/dist/development/server.js.map +1 -1
- package/dist/development/src-Dh-d6w4y.js +38 -0
- package/dist/development/src-Dh-d6w4y.js.map +1 -0
- package/dist/development/three.js +438 -0
- package/dist/development/three.js.map +1 -0
- package/dist/development/vite-CSHoxzXU.js +240 -0
- package/dist/development/vite-CSHoxzXU.js.map +1 -0
- package/dist/development/vite.js +1 -1
- package/dist/{dom-BR9o9wZW.js → dom-B2rm8o2L.js} +8 -3
- package/dist/dom-B2rm8o2L.js.map +1 -0
- package/dist/index-BdwxmSKg.d.ts +24 -0
- package/dist/index-BdwxmSKg.d.ts.map +1 -0
- package/dist/index.d.ts +4 -24
- package/dist/index.js +3 -38
- package/dist/{jsx-runtime-BzGV18VO.d.ts → jsx-runtime-CZU7F8Qz.d.ts} +6 -4
- package/dist/jsx-runtime-CZU7F8Qz.d.ts.map +1 -0
- package/dist/jsx-runtime.d.ts +2 -2
- package/dist/jsx-runtime.js.map +1 -1
- package/dist/konva.d.ts +32 -0
- package/dist/konva.d.ts.map +1 -0
- package/dist/konva.js +121 -0
- package/dist/konva.js.map +1 -0
- package/dist/motion.d.ts +44 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +217 -0
- package/dist/motion.js.map +1 -0
- package/dist/renderer.d.ts +24 -0
- package/dist/renderer.d.ts.map +1 -0
- package/dist/renderer.js +177 -0
- package/dist/renderer.js.map +1 -0
- package/dist/server.d.ts +1 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +17 -7
- package/dist/server.js.map +1 -1
- package/dist/size.json +16 -16
- package/dist/src-BZxX4xbU.js +38 -0
- package/dist/src-BZxX4xbU.js.map +1 -0
- package/dist/three.d.ts +110 -0
- package/dist/three.d.ts.map +1 -0
- package/dist/three.js +438 -0
- package/dist/three.js.map +1 -0
- package/dist/vite-CSHoxzXU.js +240 -0
- package/dist/vite-CSHoxzXU.js.map +1 -0
- package/dist/vite.js +1 -1
- package/package.json +176 -131
- package/src/astro-client.ts +1 -0
- package/src/astro-server.ts +1 -0
- package/src/astro-shared.ts +3 -0
- package/src/capabilities.ts +88 -0
- package/src/compiler.ts +251 -177
- package/src/core.ts +11 -0
- package/src/dom.ts +4 -1
- package/src/index.ts +5 -0
- package/src/jsx-runtime.ts +4 -0
- package/src/jsx-types.d.ts +3 -1
- package/src/konva.ts +159 -0
- package/src/motion.ts +406 -0
- package/src/renderer.ts +241 -0
- package/src/server.ts +21 -13
- package/src/three-events.ts +330 -0
- package/src/three.ts +350 -0
- package/dist/astro-shared-DkZaqX7L.d.ts.map +0 -1
- package/dist/core-BhiA9fFi.d.ts.map +0 -1
- package/dist/core-Dvqm7w2y.js.map +0 -1
- package/dist/development/core-DB3sQz4h.js.map +0 -1
- package/dist/development/dom-D9cY6l1R.js.map +0 -1
- package/dist/development/index.js.map +0 -1
- package/dist/development/vite-BadrFSIM.js +0 -126
- package/dist/development/vite-BadrFSIM.js.map +0 -1
- package/dist/dom-BR9o9wZW.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/jsx-runtime-BzGV18VO.d.ts.map +0 -1
- package/dist/vite-BadrFSIM.js +0 -126
- package/dist/vite-BadrFSIM.js.map +0 -1
package/src/konva.ts
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import Konva from "konva"
|
|
2
|
+
|
|
3
|
+
import { capabilityProps } from "./capabilities.ts"
|
|
4
|
+
import { owner } from "./core.ts"
|
|
5
|
+
import type { Child, VNode } from "./index.ts"
|
|
6
|
+
import { onCleanup, onMount } from "./index.ts"
|
|
7
|
+
import type { CapabilityAttributes } from "./jsx-runtime.ts"
|
|
8
|
+
import { jsx } from "./jsx-runtime.ts"
|
|
9
|
+
import { bindScene, createSceneRenderer, sceneComponent } from "./renderer.ts"
|
|
10
|
+
|
|
11
|
+
type Props<C, N> = C &
|
|
12
|
+
CapabilityAttributes<`konva`> & {
|
|
13
|
+
children?: Child
|
|
14
|
+
ref?: (node: N | null) => void
|
|
15
|
+
onClick?: (event: Konva.KonvaEventObject<MouseEvent>) => void
|
|
16
|
+
onPointerDown?: (event: Konva.KonvaEventObject<PointerEvent>) => void
|
|
17
|
+
onDragMove?: (event: Konva.KonvaEventObject<DragEvent>) => void
|
|
18
|
+
onDragEnd?: (event: Konva.KonvaEventObject<DragEvent>) => void
|
|
19
|
+
}
|
|
20
|
+
const constructors = {
|
|
21
|
+
Layer: Konva.Layer,
|
|
22
|
+
Group: Konva.Group,
|
|
23
|
+
Rect: Konva.Rect,
|
|
24
|
+
Circle: Konva.Circle,
|
|
25
|
+
Ellipse: Konva.Ellipse,
|
|
26
|
+
Line: Konva.Line,
|
|
27
|
+
Text: Konva.Text,
|
|
28
|
+
Image: Konva.Image,
|
|
29
|
+
Path: Konva.Path,
|
|
30
|
+
Star: Konva.Star,
|
|
31
|
+
Transformer: Konva.Transformer,
|
|
32
|
+
}
|
|
33
|
+
function updateNode(
|
|
34
|
+
node: Konva.Node,
|
|
35
|
+
props: Record<string, any>,
|
|
36
|
+
previous: Record<string, any>,
|
|
37
|
+
): void {
|
|
38
|
+
for (const key of new Set([...Object.keys(previous), ...Object.keys(props)])) {
|
|
39
|
+
if (/^(key|children|ref)$/.test(key) || Object.is(props[key], previous[key]))
|
|
40
|
+
continue
|
|
41
|
+
if (key.startsWith(`on`)) {
|
|
42
|
+
const event = key.slice(2).toLowerCase()
|
|
43
|
+
if (previous[key]) node.off(event, previous[key])
|
|
44
|
+
if (props[key]) node.on(event, props[key])
|
|
45
|
+
} else node.setAttr(key, props[key])
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const renderer = createSceneRenderer<Konva.Node>({
|
|
49
|
+
target(node) {
|
|
50
|
+
return {
|
|
51
|
+
kind: `konva`,
|
|
52
|
+
node,
|
|
53
|
+
read: (key) => node.getAttr(key),
|
|
54
|
+
write: (key, value) => {
|
|
55
|
+
node.setAttr(key, value)
|
|
56
|
+
},
|
|
57
|
+
listen(event, listener) {
|
|
58
|
+
node.on(event, listener)
|
|
59
|
+
return () => {
|
|
60
|
+
node.off(event, listener)
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
create(type) {
|
|
66
|
+
const Constructor = constructors[type as keyof typeof constructors]
|
|
67
|
+
if (!Constructor) throw new Error(`spa/konva: unknown node ${type}`)
|
|
68
|
+
return new Constructor({} as any)
|
|
69
|
+
},
|
|
70
|
+
update: updateNode,
|
|
71
|
+
children(parent, children) {
|
|
72
|
+
if (children.length && !(parent instanceof Konva.Container))
|
|
73
|
+
throw new Error(`spa/konva: shapes cannot contain children`)
|
|
74
|
+
if (parent instanceof Konva.Container) {
|
|
75
|
+
for (const child of [...parent.getChildren()])
|
|
76
|
+
if (!children.includes(child)) child.remove()
|
|
77
|
+
children.forEach((child, index) => {
|
|
78
|
+
if (child.getParent() !== parent) parent.add(child as any)
|
|
79
|
+
child.zIndex(index)
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
destroy(node) {
|
|
84
|
+
node.off()
|
|
85
|
+
node.destroy()
|
|
86
|
+
},
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
/** A DOM boundary owning a Konva stage and its scene subtree. */
|
|
90
|
+
export function Stage(
|
|
91
|
+
props: Props<Omit<Konva.StageConfig, `container`>, Konva.Stage> & {
|
|
92
|
+
width?: number
|
|
93
|
+
height?: number
|
|
94
|
+
},
|
|
95
|
+
): VNode {
|
|
96
|
+
let container: HTMLDivElement | null = null
|
|
97
|
+
onMount(() => {
|
|
98
|
+
const stage = new Konva.Stage({
|
|
99
|
+
container: container!,
|
|
100
|
+
width: props.width ?? 300,
|
|
101
|
+
height: props.height ?? 150,
|
|
102
|
+
})
|
|
103
|
+
onCleanup(() => {
|
|
104
|
+
stage.off()
|
|
105
|
+
stage.destroy()
|
|
106
|
+
})
|
|
107
|
+
const nativeProps = capabilityProps(owner!, {
|
|
108
|
+
kind: `konva`,
|
|
109
|
+
node: stage,
|
|
110
|
+
listen(event, listener) {
|
|
111
|
+
stage.on(event, listener)
|
|
112
|
+
return () => {
|
|
113
|
+
stage.off(event, listener)
|
|
114
|
+
}
|
|
115
|
+
},
|
|
116
|
+
read: (key) => stage.getAttr(key),
|
|
117
|
+
write: (key, value) => {
|
|
118
|
+
stage.setAttr(key, value)
|
|
119
|
+
},
|
|
120
|
+
})
|
|
121
|
+
let previous: Record<string, any> = {}
|
|
122
|
+
bindScene(
|
|
123
|
+
() => ({ ...props }),
|
|
124
|
+
(all) => {
|
|
125
|
+
const next = nativeProps(all)
|
|
126
|
+
updateNode(stage, next, previous)
|
|
127
|
+
previous = next
|
|
128
|
+
},
|
|
129
|
+
)
|
|
130
|
+
props.ref?.(stage)
|
|
131
|
+
onCleanup(() => props.ref?.(null))
|
|
132
|
+
renderer.mount(() => props.children, stage)
|
|
133
|
+
})
|
|
134
|
+
return jsx(`div`, {
|
|
135
|
+
ref: (element: HTMLDivElement | null) => {
|
|
136
|
+
container = element
|
|
137
|
+
},
|
|
138
|
+
})
|
|
139
|
+
}
|
|
140
|
+
export const Layer =
|
|
141
|
+
sceneComponent<Props<Konva.LayerConfig, Konva.Layer>>(`Layer`)
|
|
142
|
+
export const Group =
|
|
143
|
+
sceneComponent<Props<Konva.ContainerConfig, Konva.Group>>(`Group`)
|
|
144
|
+
export const Rect = sceneComponent<Props<Konva.RectConfig, Konva.Rect>>(`Rect`)
|
|
145
|
+
export const Circle =
|
|
146
|
+
sceneComponent<Props<Konva.CircleConfig, Konva.Circle>>(`Circle`)
|
|
147
|
+
export const Ellipse =
|
|
148
|
+
sceneComponent<Props<Konva.EllipseConfig, Konva.Ellipse>>(`Ellipse`)
|
|
149
|
+
export const Line = sceneComponent<Props<Konva.LineConfig, Konva.Line>>(`Line`)
|
|
150
|
+
export const Text = sceneComponent<Props<Konva.TextConfig, Konva.Text>>(`Text`)
|
|
151
|
+
export const Image =
|
|
152
|
+
sceneComponent<Props<Konva.ImageConfig, Konva.Image>>(`Image`)
|
|
153
|
+
export const Path = sceneComponent<Props<Konva.PathConfig, Konva.Path>>(`Path`)
|
|
154
|
+
export const Star = sceneComponent<Props<Konva.StarConfig, Konva.Star>>(`Star`)
|
|
155
|
+
export const Transformer =
|
|
156
|
+
sceneComponent<Props<Konva.TransformerConfig, Konva.Transformer>>(
|
|
157
|
+
`Transformer`,
|
|
158
|
+
)
|
|
159
|
+
export { renderer as konvaRenderer }
|
package/src/motion.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import type { MotionValue, ValueAnimationTransition } from "motion"
|
|
2
|
+
import {
|
|
3
|
+
animate,
|
|
4
|
+
cancelFrame,
|
|
5
|
+
frame,
|
|
6
|
+
hover,
|
|
7
|
+
motionValue,
|
|
8
|
+
press,
|
|
9
|
+
readStyleValue,
|
|
10
|
+
styleEffect,
|
|
11
|
+
} from "motion"
|
|
12
|
+
|
|
13
|
+
import type {
|
|
14
|
+
Capability,
|
|
15
|
+
CapabilityBinding,
|
|
16
|
+
CapabilityTarget,
|
|
17
|
+
} from "./capabilities.ts"
|
|
18
|
+
|
|
19
|
+
export type MotionScalar = number | string
|
|
20
|
+
export type MotionDOMTarget = Partial<
|
|
21
|
+
Record<
|
|
22
|
+
| keyof CSSStyleDeclaration
|
|
23
|
+
| `x`
|
|
24
|
+
| `y`
|
|
25
|
+
| `z`
|
|
26
|
+
| `scale`
|
|
27
|
+
| `scaleX`
|
|
28
|
+
| `scaleY`
|
|
29
|
+
| `rotate`
|
|
30
|
+
| `rotateX`
|
|
31
|
+
| `rotateY`
|
|
32
|
+
| `rotateZ`
|
|
33
|
+
| `skewX`
|
|
34
|
+
| `skewY`
|
|
35
|
+
| `--${string}`,
|
|
36
|
+
MotionScalar
|
|
37
|
+
>
|
|
38
|
+
>
|
|
39
|
+
export interface MotionThreeTarget {
|
|
40
|
+
position?: readonly [number, number, number]
|
|
41
|
+
rotation?: readonly [number, number, number]
|
|
42
|
+
scale?: number | readonly [number, number, number]
|
|
43
|
+
opacity?: number
|
|
44
|
+
intensity?: number
|
|
45
|
+
color?: string
|
|
46
|
+
}
|
|
47
|
+
export type MotionTarget<Kind extends string> = Kind extends `dom`
|
|
48
|
+
? MotionDOMTarget
|
|
49
|
+
: Kind extends `three`
|
|
50
|
+
? MotionThreeTarget
|
|
51
|
+
: Record<string, MotionScalar>
|
|
52
|
+
/** Supported transition options; keep public declarations independent of optional peer types. */
|
|
53
|
+
export interface MotionTransition {
|
|
54
|
+
type?: `spring` | `tween`
|
|
55
|
+
stiffness?: number
|
|
56
|
+
damping?: number
|
|
57
|
+
mass?: number
|
|
58
|
+
velocity?: number
|
|
59
|
+
restSpeed?: number
|
|
60
|
+
restDelta?: number
|
|
61
|
+
bounce?: number
|
|
62
|
+
visualDuration?: number
|
|
63
|
+
duration?: number
|
|
64
|
+
delay?: number
|
|
65
|
+
ease?:
|
|
66
|
+
| `linear`
|
|
67
|
+
| `easeIn`
|
|
68
|
+
| `easeOut`
|
|
69
|
+
| `easeInOut`
|
|
70
|
+
| `circIn`
|
|
71
|
+
| `circOut`
|
|
72
|
+
| `circInOut`
|
|
73
|
+
| `backIn`
|
|
74
|
+
| `backOut`
|
|
75
|
+
| `backInOut`
|
|
76
|
+
| readonly [number, number, number, number]
|
|
77
|
+
}
|
|
78
|
+
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
79
|
+
[`motion:initial`]?: MotionTarget<Kind> | false
|
|
80
|
+
[`motion:animate`]?: MotionTarget<Kind>
|
|
81
|
+
[`motion:transition`]?: MotionTransition
|
|
82
|
+
[`motion:whileHover`]?: MotionTarget<Kind>
|
|
83
|
+
[`motion:whileTap`]?: MotionTarget<Kind>
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// eslint-disable-next-line quotes -- TypeScript module augmentation requires a string literal.
|
|
87
|
+
declare module "@atom.io/spa/jsx-runtime" {
|
|
88
|
+
// oxlint-disable-next-line typescript/no-empty-interface -- Declaration merging adds optional motion attributes.
|
|
89
|
+
interface CapabilityAttributes<
|
|
90
|
+
Kind extends string,
|
|
91
|
+
> extends MotionAttributes<Kind> {}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export interface MotionCapabilityOptions {
|
|
95
|
+
/** Respect the user's reduced-motion preference by default. */
|
|
96
|
+
reducedMotion?: `user` | `always` | `never`
|
|
97
|
+
}
|
|
98
|
+
type TargetValues = Record<string, MotionScalar | readonly number[]>
|
|
99
|
+
interface AnimatedProperty {
|
|
100
|
+
values: MotionValue<MotionScalar>[]
|
|
101
|
+
base: MotionScalar | readonly number[]
|
|
102
|
+
rest: MotionScalar | readonly number[]
|
|
103
|
+
target: MotionScalar | readonly number[]
|
|
104
|
+
release(): void
|
|
105
|
+
}
|
|
106
|
+
const transform =
|
|
107
|
+
/^(x|y|z|translate.*|scale.*|rotate.*|skew.*|transformPerspective)$/
|
|
108
|
+
const allowed = new Set([
|
|
109
|
+
`initial`,
|
|
110
|
+
`animate`,
|
|
111
|
+
`transition`,
|
|
112
|
+
`whileHover`,
|
|
113
|
+
`whileTap`,
|
|
114
|
+
])
|
|
115
|
+
function equal(a: unknown, b: unknown): boolean {
|
|
116
|
+
return (
|
|
117
|
+
Object.is(a, b) ||
|
|
118
|
+
(Array.isArray(a) &&
|
|
119
|
+
Array.isArray(b) &&
|
|
120
|
+
a.length === b.length &&
|
|
121
|
+
a.every((value, index) => Object.is(value, b[index])))
|
|
122
|
+
)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function attach(
|
|
126
|
+
target: CapabilityTarget,
|
|
127
|
+
options: MotionCapabilityOptions,
|
|
128
|
+
): CapabilityBinding {
|
|
129
|
+
if (target.kind !== `dom` && (!target.read || !target.write))
|
|
130
|
+
throw new Error(
|
|
131
|
+
`spa/motion: renderer ${target.kind} does not support motion`,
|
|
132
|
+
)
|
|
133
|
+
const element =
|
|
134
|
+
target.kind === `dom` ? (target.node as HTMLElement | SVGElement) : undefined
|
|
135
|
+
const properties = new Map<string, AnimatedProperty>()
|
|
136
|
+
const originalStyles = new Map<string, { value: string; priority: string }>()
|
|
137
|
+
let styles: (() => void) | undefined
|
|
138
|
+
let props: Record<string, unknown> = {}
|
|
139
|
+
let hovered = false
|
|
140
|
+
let tapped = false
|
|
141
|
+
let started = false
|
|
142
|
+
let dead = false
|
|
143
|
+
let gestures: (() => void)[] = []
|
|
144
|
+
const preference =
|
|
145
|
+
options.reducedMotion !== `never` && typeof matchMedia === `function`
|
|
146
|
+
? matchMedia(`(prefers-reduced-motion: reduce)`)
|
|
147
|
+
: undefined
|
|
148
|
+
const reduced = () =>
|
|
149
|
+
options.reducedMotion === `always` ||
|
|
150
|
+
(options.reducedMotion !== `never` && preference?.matches)
|
|
151
|
+
const read = (key: string, fallback: unknown): any => {
|
|
152
|
+
const value = element ? readStyleValue(element, key) : target.read!(key)
|
|
153
|
+
return value === undefined || value === ``
|
|
154
|
+
? typeof fallback === `number`
|
|
155
|
+
? key.startsWith(`scale`) || key === `opacity`
|
|
156
|
+
? 1
|
|
157
|
+
: 0
|
|
158
|
+
: fallback
|
|
159
|
+
: value
|
|
160
|
+
}
|
|
161
|
+
const bindStyles = () => {
|
|
162
|
+
styles?.()
|
|
163
|
+
if (element)
|
|
164
|
+
styles = styleEffect(
|
|
165
|
+
[element],
|
|
166
|
+
Object.fromEntries(
|
|
167
|
+
[...properties].map(([key, property]) => [key, property.values[0]]),
|
|
168
|
+
) as any,
|
|
169
|
+
)
|
|
170
|
+
}
|
|
171
|
+
const ensure = (
|
|
172
|
+
key: string,
|
|
173
|
+
goal: MotionScalar | readonly number[],
|
|
174
|
+
initial: TargetValues | false,
|
|
175
|
+
): AnimatedProperty => {
|
|
176
|
+
const existing = properties.get(key)
|
|
177
|
+
if (existing) {
|
|
178
|
+
if (existing.values.length !== (Array.isArray(goal) ? goal.length : 1))
|
|
179
|
+
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
180
|
+
return existing
|
|
181
|
+
}
|
|
182
|
+
const base = read(key, goal) as MotionScalar | readonly number[]
|
|
183
|
+
const rest = !Array.isArray(goal) && Array.isArray(base) ? base[0]! : base
|
|
184
|
+
let start = initial === false ? goal : (initial[key] ?? rest)
|
|
185
|
+
if (!Array.isArray(goal) && Array.isArray(start)) start = start[0]!
|
|
186
|
+
if (
|
|
187
|
+
Array.isArray(goal) &&
|
|
188
|
+
(!Array.isArray(start) || start.length !== goal.length)
|
|
189
|
+
)
|
|
190
|
+
throw new Error(`spa/motion: ${key} requires matching numeric tuples`)
|
|
191
|
+
const values = (Array.isArray(start) ? start : [start]).map((value) =>
|
|
192
|
+
motionValue(value),
|
|
193
|
+
) as MotionValue<MotionScalar>[]
|
|
194
|
+
const unsubscribers: (() => void)[] = []
|
|
195
|
+
if (element) {
|
|
196
|
+
if (Array.isArray(goal))
|
|
197
|
+
throw new Error(
|
|
198
|
+
`spa/motion: DOM targets must be scalar styles or transforms`,
|
|
199
|
+
)
|
|
200
|
+
const cssKey = transform.test(key)
|
|
201
|
+
? `transform`
|
|
202
|
+
: key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)
|
|
203
|
+
if (!originalStyles.has(cssKey))
|
|
204
|
+
originalStyles.set(cssKey, {
|
|
205
|
+
value: element.style.getPropertyValue(cssKey),
|
|
206
|
+
priority: element.style.getPropertyPriority(cssKey),
|
|
207
|
+
})
|
|
208
|
+
} else {
|
|
209
|
+
const write = () => {
|
|
210
|
+
target.write!(
|
|
211
|
+
key,
|
|
212
|
+
Array.isArray(goal)
|
|
213
|
+
? values.map((value) => value.get())
|
|
214
|
+
: values[0].get(),
|
|
215
|
+
)
|
|
216
|
+
}
|
|
217
|
+
for (const value of values) unsubscribers.push(value.on(`change`, write))
|
|
218
|
+
write()
|
|
219
|
+
}
|
|
220
|
+
const property: AnimatedProperty = {
|
|
221
|
+
values,
|
|
222
|
+
base,
|
|
223
|
+
rest,
|
|
224
|
+
target: start,
|
|
225
|
+
release() {
|
|
226
|
+
for (const release of unsubscribers) release()
|
|
227
|
+
for (const value of values) value.destroy()
|
|
228
|
+
},
|
|
229
|
+
}
|
|
230
|
+
properties.set(key, property)
|
|
231
|
+
return property
|
|
232
|
+
}
|
|
233
|
+
const refresh = () => {
|
|
234
|
+
if (dead) return
|
|
235
|
+
const initial = (props[`initial`] ?? {}) as TargetValues | false
|
|
236
|
+
const animateTo = (props[`animate`] ?? {}) as TargetValues
|
|
237
|
+
const hoverTo = (props[`whileHover`] ?? {}) as TargetValues
|
|
238
|
+
const tapTo = (props[`whileTap`] ?? {}) as TargetValues
|
|
239
|
+
const goals = {
|
|
240
|
+
...animateTo,
|
|
241
|
+
...(hovered ? hoverTo : {}),
|
|
242
|
+
...(tapped ? tapTo : {}),
|
|
243
|
+
}
|
|
244
|
+
const keys = new Set([
|
|
245
|
+
...Object.keys(initial || {}),
|
|
246
|
+
...Object.keys(animateTo),
|
|
247
|
+
...Object.keys(hoverTo),
|
|
248
|
+
...Object.keys(tapTo),
|
|
249
|
+
])
|
|
250
|
+
let added = false
|
|
251
|
+
for (const key of keys) {
|
|
252
|
+
const sample =
|
|
253
|
+
goals[key] ??
|
|
254
|
+
(initial === false ? undefined : initial[key]) ??
|
|
255
|
+
hoverTo[key] ??
|
|
256
|
+
tapTo[key]
|
|
257
|
+
if (sample === undefined) continue
|
|
258
|
+
const had = properties.has(key)
|
|
259
|
+
ensure(key, sample, started ? {} : initial)
|
|
260
|
+
if (!had) added = true
|
|
261
|
+
}
|
|
262
|
+
if (added) bindStyles()
|
|
263
|
+
const transition = (props[`transition`] ?? {
|
|
264
|
+
type: `spring`,
|
|
265
|
+
}) as ValueAnimationTransition
|
|
266
|
+
for (const [key, property] of properties) {
|
|
267
|
+
const goal =
|
|
268
|
+
goals[key] ??
|
|
269
|
+
(initial === false ? undefined : initial[key]) ??
|
|
270
|
+
property.rest
|
|
271
|
+
if (equal(property.target, goal)) continue
|
|
272
|
+
property.target = Array.isArray(goal) ? [...goal] : goal
|
|
273
|
+
const next = Array.isArray(goal) ? goal : [goal]
|
|
274
|
+
if (next.length !== property.values.length)
|
|
275
|
+
throw new Error(`spa/motion: cannot change the shape of ${key}`)
|
|
276
|
+
property.values.forEach((value, index) => {
|
|
277
|
+
if (reduced() || (!started && initial === false)) value.jump(next[index])
|
|
278
|
+
else animate(value, next[index], transition)
|
|
279
|
+
})
|
|
280
|
+
}
|
|
281
|
+
started = true
|
|
282
|
+
}
|
|
283
|
+
const clearGestures = () => {
|
|
284
|
+
for (const release of gestures) release()
|
|
285
|
+
gestures = []
|
|
286
|
+
}
|
|
287
|
+
const installGestures = () => {
|
|
288
|
+
clearGestures()
|
|
289
|
+
if (element) {
|
|
290
|
+
if (props[`whileHover`])
|
|
291
|
+
gestures.push(
|
|
292
|
+
hover([element], () => {
|
|
293
|
+
hovered = true
|
|
294
|
+
refresh()
|
|
295
|
+
return () => {
|
|
296
|
+
hovered = false
|
|
297
|
+
refresh()
|
|
298
|
+
}
|
|
299
|
+
}),
|
|
300
|
+
)
|
|
301
|
+
if (props[`whileTap`])
|
|
302
|
+
gestures.push(
|
|
303
|
+
press([element], () => {
|
|
304
|
+
tapped = true
|
|
305
|
+
refresh()
|
|
306
|
+
return () => {
|
|
307
|
+
tapped = false
|
|
308
|
+
refresh()
|
|
309
|
+
}
|
|
310
|
+
}),
|
|
311
|
+
)
|
|
312
|
+
} else if (props[`whileHover`] || props[`whileTap`]) {
|
|
313
|
+
if (!target.listen)
|
|
314
|
+
throw new Error(
|
|
315
|
+
`spa/motion: ${target.kind} gestures require a renderer event adapter`,
|
|
316
|
+
)
|
|
317
|
+
const listen = (event: string, run: () => void) =>
|
|
318
|
+
gestures.push(target.listen!(event, run))
|
|
319
|
+
listen(`pointerenter`, () => {
|
|
320
|
+
hovered = true
|
|
321
|
+
refresh()
|
|
322
|
+
})
|
|
323
|
+
listen(`pointerleave`, () => {
|
|
324
|
+
hovered = false
|
|
325
|
+
tapped = false
|
|
326
|
+
refresh()
|
|
327
|
+
})
|
|
328
|
+
listen(`pointerdown`, () => {
|
|
329
|
+
tapped = true
|
|
330
|
+
refresh()
|
|
331
|
+
})
|
|
332
|
+
listen(`pointerup`, () => {
|
|
333
|
+
tapped = false
|
|
334
|
+
refresh()
|
|
335
|
+
})
|
|
336
|
+
listen(`pointercancel`, () => {
|
|
337
|
+
tapped = false
|
|
338
|
+
refresh()
|
|
339
|
+
})
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
const preferenceChanged = () => {
|
|
343
|
+
if (reduced()) {
|
|
344
|
+
for (const property of properties.values()) {
|
|
345
|
+
const goal = Array.isArray(property.target)
|
|
346
|
+
? property.target
|
|
347
|
+
: [property.target]
|
|
348
|
+
property.values.forEach((value, i) => {
|
|
349
|
+
value.jump(goal[i])
|
|
350
|
+
})
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
preference?.addEventListener(`change`, preferenceChanged)
|
|
355
|
+
return {
|
|
356
|
+
update(next) {
|
|
357
|
+
if (dead) return
|
|
358
|
+
for (const key of Object.keys(next))
|
|
359
|
+
if (!allowed.has(key))
|
|
360
|
+
throw new Error(`spa/motion: unknown property motion:${key}`)
|
|
361
|
+
const gestureChange =
|
|
362
|
+
!started ||
|
|
363
|
+
Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||
|
|
364
|
+
Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`])
|
|
365
|
+
props = next
|
|
366
|
+
if (gestureChange) installGestures()
|
|
367
|
+
refresh()
|
|
368
|
+
},
|
|
369
|
+
dispose() {
|
|
370
|
+
if (dead) return
|
|
371
|
+
dead = true
|
|
372
|
+
clearGestures()
|
|
373
|
+
preference?.removeEventListener(`change`, preferenceChanged)
|
|
374
|
+
styles?.()
|
|
375
|
+
for (const [key, property] of properties) {
|
|
376
|
+
property.release()
|
|
377
|
+
if (!element) target.write!(key, property.base)
|
|
378
|
+
}
|
|
379
|
+
for (const [key, value] of originalStyles) {
|
|
380
|
+
if (value.value)
|
|
381
|
+
element!.style.setProperty(key, value.value, value.priority)
|
|
382
|
+
else element!.style.removeProperty(key)
|
|
383
|
+
}
|
|
384
|
+
properties.clear()
|
|
385
|
+
},
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */
|
|
390
|
+
export function motionCapability(
|
|
391
|
+
options: MotionCapabilityOptions = {},
|
|
392
|
+
): Capability {
|
|
393
|
+
return {
|
|
394
|
+
namespace: `motion`,
|
|
395
|
+
attach: (target) => attach(target, options),
|
|
396
|
+
scheduleFrame(callback) {
|
|
397
|
+
const run = ({ timestamp }: { timestamp: number }) => {
|
|
398
|
+
callback(timestamp)
|
|
399
|
+
}
|
|
400
|
+
frame.render(run, true)
|
|
401
|
+
return () => {
|
|
402
|
+
cancelFrame(run)
|
|
403
|
+
}
|
|
404
|
+
},
|
|
405
|
+
}
|
|
406
|
+
}
|