@atom.io/spa 0.0.1 → 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 +6 -3
- package/dist/astro-client.js.map +1 -1
- package/dist/astro-server.js +2 -1
- 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 +6 -3
- package/dist/development/astro-client.js.map +1 -1
- package/dist/development/astro-server.js +2 -1
- 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 +3 -2
- 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 +3 -2
- 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 +56 -11
- 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 -178
- 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 +3 -0
- 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-CQchIQ_-.js +0 -126
- package/dist/development/vite-CQchIQ_-.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-CQchIQ_-.js +0 -126
- package/dist/vite-CQchIQ_-.js.map +0 -1
package/dist/konva.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { d as onMount, f as owner, n as capabilityProps, u as onCleanup } from "./capabilities-BuZUQpiD.js";
|
|
2
|
+
import { jsx } from "./jsx-runtime.js";
|
|
3
|
+
import { bindScene, createSceneRenderer, sceneComponent } from "./renderer.js";
|
|
4
|
+
import Konva from "konva";
|
|
5
|
+
// src/konva.ts
|
|
6
|
+
const constructors = {
|
|
7
|
+
Layer: Konva.Layer,
|
|
8
|
+
Group: Konva.Group,
|
|
9
|
+
Rect: Konva.Rect,
|
|
10
|
+
Circle: Konva.Circle,
|
|
11
|
+
Ellipse: Konva.Ellipse,
|
|
12
|
+
Line: Konva.Line,
|
|
13
|
+
Text: Konva.Text,
|
|
14
|
+
Image: Konva.Image,
|
|
15
|
+
Path: Konva.Path,
|
|
16
|
+
Star: Konva.Star,
|
|
17
|
+
Transformer: Konva.Transformer
|
|
18
|
+
};
|
|
19
|
+
function updateNode(node, props, previous) {
|
|
20
|
+
for (const key of /* @__PURE__ */ new Set([...Object.keys(previous), ...Object.keys(props)])) {
|
|
21
|
+
if (/^(key|children|ref)$/.test(key) || Object.is(props[key], previous[key])) continue;
|
|
22
|
+
if (key.startsWith(`on`)) {
|
|
23
|
+
const event = key.slice(2).toLowerCase();
|
|
24
|
+
if (previous[key]) node.off(event, previous[key]);
|
|
25
|
+
if (props[key]) node.on(event, props[key]);
|
|
26
|
+
} else node.setAttr(key, props[key]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
const renderer = createSceneRenderer({
|
|
30
|
+
target(node) {
|
|
31
|
+
return {
|
|
32
|
+
kind: `konva`,
|
|
33
|
+
node,
|
|
34
|
+
read: (key) => node.getAttr(key),
|
|
35
|
+
write: (key, value) => {
|
|
36
|
+
node.setAttr(key, value);
|
|
37
|
+
},
|
|
38
|
+
listen(event, listener) {
|
|
39
|
+
node.on(event, listener);
|
|
40
|
+
return () => {
|
|
41
|
+
node.off(event, listener);
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
},
|
|
46
|
+
create(type) {
|
|
47
|
+
const Constructor = constructors[type];
|
|
48
|
+
if (!Constructor) throw new Error(`spa/konva: unknown node ${type}`);
|
|
49
|
+
return new Constructor({});
|
|
50
|
+
},
|
|
51
|
+
update: updateNode,
|
|
52
|
+
children(parent, children) {
|
|
53
|
+
if (children.length && !(parent instanceof Konva.Container)) throw new Error(`spa/konva: shapes cannot contain children`);
|
|
54
|
+
if (parent instanceof Konva.Container) {
|
|
55
|
+
for (const child of [...parent.getChildren()]) if (!children.includes(child)) child.remove();
|
|
56
|
+
children.forEach((child, index) => {
|
|
57
|
+
if (child.getParent() !== parent) parent.add(child);
|
|
58
|
+
child.zIndex(index);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
destroy(node) {
|
|
63
|
+
node.off();
|
|
64
|
+
node.destroy();
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
/** A DOM boundary owning a Konva stage and its scene subtree. */
|
|
68
|
+
function Stage(props) {
|
|
69
|
+
let container = null;
|
|
70
|
+
onMount(() => {
|
|
71
|
+
const stage = new Konva.Stage({
|
|
72
|
+
container,
|
|
73
|
+
width: props.width ?? 300,
|
|
74
|
+
height: props.height ?? 150
|
|
75
|
+
});
|
|
76
|
+
onCleanup(() => {
|
|
77
|
+
stage.off();
|
|
78
|
+
stage.destroy();
|
|
79
|
+
});
|
|
80
|
+
const nativeProps = capabilityProps(owner, {
|
|
81
|
+
kind: `konva`,
|
|
82
|
+
node: stage,
|
|
83
|
+
listen(event, listener) {
|
|
84
|
+
stage.on(event, listener);
|
|
85
|
+
return () => {
|
|
86
|
+
stage.off(event, listener);
|
|
87
|
+
};
|
|
88
|
+
},
|
|
89
|
+
read: (key) => stage.getAttr(key),
|
|
90
|
+
write: (key, value) => {
|
|
91
|
+
stage.setAttr(key, value);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
let previous = {};
|
|
95
|
+
bindScene(() => ({ ...props }), (all) => {
|
|
96
|
+
const next = nativeProps(all);
|
|
97
|
+
updateNode(stage, next, previous);
|
|
98
|
+
previous = next;
|
|
99
|
+
});
|
|
100
|
+
props.ref?.(stage);
|
|
101
|
+
onCleanup(() => props.ref?.(null));
|
|
102
|
+
renderer.mount(() => props.children, stage);
|
|
103
|
+
});
|
|
104
|
+
return jsx(`div`, { ref: (element) => {
|
|
105
|
+
container = element;
|
|
106
|
+
} });
|
|
107
|
+
}
|
|
108
|
+
const Layer = sceneComponent(`Layer`);
|
|
109
|
+
const Group = sceneComponent(`Group`);
|
|
110
|
+
const Rect = sceneComponent(`Rect`);
|
|
111
|
+
const Circle = sceneComponent(`Circle`);
|
|
112
|
+
const Ellipse = sceneComponent(`Ellipse`);
|
|
113
|
+
const Line = sceneComponent(`Line`);
|
|
114
|
+
const Text = sceneComponent(`Text`);
|
|
115
|
+
const Image = sceneComponent(`Image`);
|
|
116
|
+
const Path = sceneComponent(`Path`);
|
|
117
|
+
const Star = sceneComponent(`Star`);
|
|
118
|
+
const Transformer = sceneComponent(`Transformer`);
|
|
119
|
+
export { Circle, Ellipse, Group, Image, Layer, Line, Path, Rect, Stage, Star, Text, Transformer, renderer as konvaRenderer };
|
|
120
|
+
|
|
121
|
+
//# sourceMappingURL=konva.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"konva.js","names":[],"sources":["../src/konva.ts"],"sourcesContent":["import Konva from \"konva\"\n\nimport { capabilityProps } from \"./capabilities.ts\"\nimport { owner } from \"./core.ts\"\nimport type { Child, VNode } from \"./index.ts\"\nimport { onCleanup, onMount } from \"./index.ts\"\nimport type { CapabilityAttributes } from \"./jsx-runtime.ts\"\nimport { jsx } from \"./jsx-runtime.ts\"\nimport { bindScene, createSceneRenderer, sceneComponent } from \"./renderer.ts\"\n\ntype Props<C, N> = C &\n\tCapabilityAttributes<`konva`> & {\n\t\tchildren?: Child\n\t\tref?: (node: N | null) => void\n\t\tonClick?: (event: Konva.KonvaEventObject<MouseEvent>) => void\n\t\tonPointerDown?: (event: Konva.KonvaEventObject<PointerEvent>) => void\n\t\tonDragMove?: (event: Konva.KonvaEventObject<DragEvent>) => void\n\t\tonDragEnd?: (event: Konva.KonvaEventObject<DragEvent>) => void\n\t}\nconst constructors = {\n\tLayer: Konva.Layer,\n\tGroup: Konva.Group,\n\tRect: Konva.Rect,\n\tCircle: Konva.Circle,\n\tEllipse: Konva.Ellipse,\n\tLine: Konva.Line,\n\tText: Konva.Text,\n\tImage: Konva.Image,\n\tPath: Konva.Path,\n\tStar: Konva.Star,\n\tTransformer: Konva.Transformer,\n}\nfunction updateNode(\n\tnode: Konva.Node,\n\tprops: Record<string, any>,\n\tprevious: Record<string, any>,\n): void {\n\tfor (const key of new Set([...Object.keys(previous), ...Object.keys(props)])) {\n\t\tif (/^(key|children|ref)$/.test(key) || Object.is(props[key], previous[key]))\n\t\t\tcontinue\n\t\tif (key.startsWith(`on`)) {\n\t\t\tconst event = key.slice(2).toLowerCase()\n\t\t\tif (previous[key]) node.off(event, previous[key])\n\t\t\tif (props[key]) node.on(event, props[key])\n\t\t} else node.setAttr(key, props[key])\n\t}\n}\nconst renderer = createSceneRenderer<Konva.Node>({\n\ttarget(node) {\n\t\treturn {\n\t\t\tkind: `konva`,\n\t\t\tnode,\n\t\t\tread: (key) => node.getAttr(key),\n\t\t\twrite: (key, value) => {\n\t\t\t\tnode.setAttr(key, value)\n\t\t\t},\n\t\t\tlisten(event, listener) {\n\t\t\t\tnode.on(event, listener)\n\t\t\t\treturn () => {\n\t\t\t\t\tnode.off(event, listener)\n\t\t\t\t}\n\t\t\t},\n\t\t}\n\t},\n\tcreate(type) {\n\t\tconst Constructor = constructors[type as keyof typeof constructors]\n\t\tif (!Constructor) throw new Error(`spa/konva: unknown node ${type}`)\n\t\treturn new Constructor({} as any)\n\t},\n\tupdate: updateNode,\n\tchildren(parent, children) {\n\t\tif (children.length && !(parent instanceof Konva.Container))\n\t\t\tthrow new Error(`spa/konva: shapes cannot contain children`)\n\t\tif (parent instanceof Konva.Container) {\n\t\t\tfor (const child of [...parent.getChildren()])\n\t\t\t\tif (!children.includes(child)) child.remove()\n\t\t\tchildren.forEach((child, index) => {\n\t\t\t\tif (child.getParent() !== parent) parent.add(child as any)\n\t\t\t\tchild.zIndex(index)\n\t\t\t})\n\t\t}\n\t},\n\tdestroy(node) {\n\t\tnode.off()\n\t\tnode.destroy()\n\t},\n})\n\n/** A DOM boundary owning a Konva stage and its scene subtree. */\nexport function Stage(\n\tprops: Props<Omit<Konva.StageConfig, `container`>, Konva.Stage> & {\n\t\twidth?: number\n\t\theight?: number\n\t},\n): VNode {\n\tlet container: HTMLDivElement | null = null\n\tonMount(() => {\n\t\tconst stage = new Konva.Stage({\n\t\t\tcontainer: container!,\n\t\t\twidth: props.width ?? 300,\n\t\t\theight: props.height ?? 150,\n\t\t})\n\t\tonCleanup(() => {\n\t\t\tstage.off()\n\t\t\tstage.destroy()\n\t\t})\n\t\tconst nativeProps = capabilityProps(owner!, {\n\t\t\tkind: `konva`,\n\t\t\tnode: stage,\n\t\t\tlisten(event, listener) {\n\t\t\t\tstage.on(event, listener)\n\t\t\t\treturn () => {\n\t\t\t\t\tstage.off(event, listener)\n\t\t\t\t}\n\t\t\t},\n\t\t\tread: (key) => stage.getAttr(key),\n\t\t\twrite: (key, value) => {\n\t\t\t\tstage.setAttr(key, value)\n\t\t\t},\n\t\t})\n\t\tlet previous: Record<string, any> = {}\n\t\tbindScene(\n\t\t\t() => ({ ...props }),\n\t\t\t(all) => {\n\t\t\t\tconst next = nativeProps(all)\n\t\t\t\tupdateNode(stage, next, previous)\n\t\t\t\tprevious = next\n\t\t\t},\n\t\t)\n\t\tprops.ref?.(stage)\n\t\tonCleanup(() => props.ref?.(null))\n\t\trenderer.mount(() => props.children, stage)\n\t})\n\treturn jsx(`div`, {\n\t\tref: (element: HTMLDivElement | null) => {\n\t\t\tcontainer = element\n\t\t},\n\t})\n}\nexport const Layer =\n\tsceneComponent<Props<Konva.LayerConfig, Konva.Layer>>(`Layer`)\nexport const Group =\n\tsceneComponent<Props<Konva.ContainerConfig, Konva.Group>>(`Group`)\nexport const Rect = sceneComponent<Props<Konva.RectConfig, Konva.Rect>>(`Rect`)\nexport const Circle =\n\tsceneComponent<Props<Konva.CircleConfig, Konva.Circle>>(`Circle`)\nexport const Ellipse =\n\tsceneComponent<Props<Konva.EllipseConfig, Konva.Ellipse>>(`Ellipse`)\nexport const Line = sceneComponent<Props<Konva.LineConfig, Konva.Line>>(`Line`)\nexport const Text = sceneComponent<Props<Konva.TextConfig, Konva.Text>>(`Text`)\nexport const Image =\n\tsceneComponent<Props<Konva.ImageConfig, Konva.Image>>(`Image`)\nexport const Path = sceneComponent<Props<Konva.PathConfig, Konva.Path>>(`Path`)\nexport const Star = sceneComponent<Props<Konva.StarConfig, Konva.Star>>(`Star`)\nexport const Transformer =\n\tsceneComponent<Props<Konva.TransformerConfig, Konva.Transformer>>(\n\t\t`Transformer`,\n\t)\nexport { renderer as konvaRenderer }\n"],"mappings":";;;;;AAmBA,MAAM,eAAe;CACpB,OAAO,MAAM;CACb,OAAO,MAAM;CACb,MAAM,MAAM;CACZ,QAAQ,MAAM;CACd,SAAS,MAAM;CACf,MAAM,MAAM;CACZ,MAAM,MAAM;CACZ,OAAO,MAAM;CACb,MAAM,MAAM;CACZ,MAAM,MAAM;CACZ,aAAa,MAAM;AACpB;AACA,SAAS,WACR,MACA,OACA,UACO;CACP,KAAK,MAAM,uBAAO,IAAI,IAAI,CAAC,GAAG,OAAO,KAAK,QAAQ,GAAG,GAAG,OAAO,KAAK,KAAK,CAAC,CAAC,GAAG;EAC7E,IAAI,uBAAuB,KAAK,GAAG,KAAK,OAAO,GAAG,MAAM,MAAM,SAAS,IAAI,GAC1E;EACD,IAAI,IAAI,WAAW,IAAI,GAAG;GACzB,MAAM,QAAQ,IAAI,MAAM,CAAC,CAAC,CAAC,YAAY;GACvC,IAAI,SAAS,MAAM,KAAK,IAAI,OAAO,SAAS,IAAI;GAChD,IAAI,MAAM,MAAM,KAAK,GAAG,OAAO,MAAM,IAAI;EAC1C,OAAO,KAAK,QAAQ,KAAK,MAAM,IAAI;CACpC;AACD;AACA,MAAM,WAAW,oBAAgC;CAChD,OAAO,MAAM;EACZ,OAAO;GACN,MAAM;GACN;GACA,OAAO,QAAQ,KAAK,QAAQ,GAAG;GAC/B,QAAQ,KAAK,UAAU;IACtB,KAAK,QAAQ,KAAK,KAAK;GACxB;GACA,OAAO,OAAO,UAAU;IACvB,KAAK,GAAG,OAAO,QAAQ;IACvB,aAAa;KACZ,KAAK,IAAI,OAAO,QAAQ;IACzB;GACD;EACD;CACD;CACA,OAAO,MAAM;EACZ,MAAM,cAAc,aAAa;EACjC,IAAI,CAAC,aAAa,MAAM,IAA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;EACjC,SAAS,YAAY,MAAM,UAAU,KAAK;CAC3C,CAAC;CACD,OAAO,IAAI,OAAO,EACjB,MAAM,YAAmC;EACxC,YAAY;CACb,EACD,CAAC;AACF;AACA,MAAa,QACZ,eAAsD,OAAO;AAC9D,MAAa,QACZ,eAA0D,OAAO;AAClE,MAAa,OAAO,eAAoD,MAAM;AAC9E,MAAa,SACZ,eAAwD,QAAQ;AACjE,MAAa,UACZ,eAA0D,SAAS;AACpE,MAAa,OAAO,eAAoD,MAAM;AAC9E,MAAa,OAAO,eAAoD,MAAM;AAC9E,MAAa,QACZ,eAAsD,OAAO;AAC9D,MAAa,OAAO,eAAoD,MAAM;AAC9E,MAAa,OAAO,eAAoD,MAAM;AAC9E,MAAa,cACZ,eACC,aACD"}
|
package/dist/motion.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { t as Capability } from "./capabilities-yC9TsHDI.js";
|
|
2
|
+
export type MotionScalar = number | string;
|
|
3
|
+
export type MotionDOMTarget = Partial<Record<keyof CSSStyleDeclaration | `x` | `y` | `z` | `scale` | `scaleX` | `scaleY` | `rotate` | `rotateX` | `rotateY` | `rotateZ` | `skewX` | `skewY` | `--${string}`, MotionScalar>>;
|
|
4
|
+
export interface MotionThreeTarget {
|
|
5
|
+
position?: readonly [number, number, number];
|
|
6
|
+
rotation?: readonly [number, number, number];
|
|
7
|
+
scale?: number | readonly [number, number, number];
|
|
8
|
+
opacity?: number;
|
|
9
|
+
intensity?: number;
|
|
10
|
+
color?: string;
|
|
11
|
+
}
|
|
12
|
+
export type MotionTarget<Kind extends string> = Kind extends `dom` ? MotionDOMTarget : Kind extends `three` ? MotionThreeTarget : Record<string, MotionScalar>;
|
|
13
|
+
/** Supported transition options; keep public declarations independent of optional peer types. */
|
|
14
|
+
export interface MotionTransition {
|
|
15
|
+
type?: `spring` | `tween`;
|
|
16
|
+
stiffness?: number;
|
|
17
|
+
damping?: number;
|
|
18
|
+
mass?: number;
|
|
19
|
+
velocity?: number;
|
|
20
|
+
restSpeed?: number;
|
|
21
|
+
restDelta?: number;
|
|
22
|
+
bounce?: number;
|
|
23
|
+
visualDuration?: number;
|
|
24
|
+
duration?: number;
|
|
25
|
+
delay?: number;
|
|
26
|
+
ease?: `linear` | `easeIn` | `easeOut` | `easeInOut` | `circIn` | `circOut` | `circInOut` | `backIn` | `backOut` | `backInOut` | readonly [number, number, number, number];
|
|
27
|
+
}
|
|
28
|
+
export interface MotionAttributes<Kind extends string = `dom`> {
|
|
29
|
+
[`motion:initial`]?: MotionTarget<Kind> | false;
|
|
30
|
+
[`motion:animate`]?: MotionTarget<Kind>;
|
|
31
|
+
[`motion:transition`]?: MotionTransition;
|
|
32
|
+
[`motion:whileHover`]?: MotionTarget<Kind>;
|
|
33
|
+
[`motion:whileTap`]?: MotionTarget<Kind>;
|
|
34
|
+
}
|
|
35
|
+
declare module "@atom.io/spa/jsx-runtime" {
|
|
36
|
+
interface CapabilityAttributes<Kind extends string> extends MotionAttributes<Kind> {}
|
|
37
|
+
}
|
|
38
|
+
export interface MotionCapabilityOptions {
|
|
39
|
+
/** Respect the user's reduced-motion preference by default. */
|
|
40
|
+
reducedMotion?: `user` | `always` | `never`;
|
|
41
|
+
}
|
|
42
|
+
/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */
|
|
43
|
+
export declare function motionCapability(options?: MotionCapabilityOptions): Capability;
|
|
44
|
+
//# sourceMappingURL=motion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motion.d.ts","names":[],"sources":["../src/motion.ts"],"mappings":";YAkBY;YACA,kBAAkB,QAC7B,aACS,0JAcR;iBAGe;EAChB;EACA;EACA;EACA;EACA;EACA;;YAEW,aAAa,uBAAuB,qBAC7C,kBACA,uBACC,oBACA,eAAe;;iBAEF;EAChB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAagB,iBAAiB;GAChC,oBAAoB,aAAa;GACjC,oBAAoB,aAAa;GACjC,uBAAuB;GACvB,uBAAuB,aAAa;GACpC,qBAAqB,aAAa;;;YAMzB,qBACT,6BACS,iBAAiB;;iBAGX;;EAEhB;;;wBAsSe,iBACf,UAAS,0BACP"}
|
package/dist/motion.js
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import { animate, cancelFrame, frame, hover, motionValue, press, readStyleValue, styleEffect } from "motion";
|
|
2
|
+
// src/motion.ts
|
|
3
|
+
const transform = /^(x|y|z|translate.*|scale.*|rotate.*|skew.*|transformPerspective)$/;
|
|
4
|
+
const allowed = /* @__PURE__ */ new Set([
|
|
5
|
+
`initial`,
|
|
6
|
+
`animate`,
|
|
7
|
+
`transition`,
|
|
8
|
+
`whileHover`,
|
|
9
|
+
`whileTap`
|
|
10
|
+
]);
|
|
11
|
+
function equal(a, b) {
|
|
12
|
+
return Object.is(a, b) || Array.isArray(a) && Array.isArray(b) && a.length === b.length && a.every((value, index) => Object.is(value, b[index]));
|
|
13
|
+
}
|
|
14
|
+
function attach(target, options) {
|
|
15
|
+
if (target.kind !== `dom` && (!target.read || !target.write)) throw new Error(`spa/motion: renderer ${target.kind} does not support motion`);
|
|
16
|
+
const element = target.kind === `dom` ? target.node : void 0;
|
|
17
|
+
const properties = /* @__PURE__ */ new Map();
|
|
18
|
+
const originalStyles = /* @__PURE__ */ new Map();
|
|
19
|
+
let styles;
|
|
20
|
+
let props = {};
|
|
21
|
+
let hovered = false;
|
|
22
|
+
let tapped = false;
|
|
23
|
+
let started = false;
|
|
24
|
+
let dead = false;
|
|
25
|
+
let gestures = [];
|
|
26
|
+
const preference = options.reducedMotion !== `never` && typeof matchMedia === `function` ? matchMedia(`(prefers-reduced-motion: reduce)`) : void 0;
|
|
27
|
+
const reduced = () => options.reducedMotion === `always` || options.reducedMotion !== `never` && preference?.matches;
|
|
28
|
+
const read = (key, fallback) => {
|
|
29
|
+
const value = element ? readStyleValue(element, key) : target.read(key);
|
|
30
|
+
return value === void 0 || value === `` ? typeof fallback === `number` ? key.startsWith(`scale`) || key === `opacity` ? 1 : 0 : fallback : value;
|
|
31
|
+
};
|
|
32
|
+
const bindStyles = () => {
|
|
33
|
+
styles?.();
|
|
34
|
+
if (element) styles = styleEffect([element], Object.fromEntries([...properties].map(([key, property]) => [key, property.values[0]])));
|
|
35
|
+
};
|
|
36
|
+
const ensure = (key, goal, initial) => {
|
|
37
|
+
const existing = properties.get(key);
|
|
38
|
+
if (existing) {
|
|
39
|
+
if (existing.values.length !== (Array.isArray(goal) ? goal.length : 1)) throw new Error(`spa/motion: cannot change the shape of ${key}`);
|
|
40
|
+
return existing;
|
|
41
|
+
}
|
|
42
|
+
const base = read(key, goal);
|
|
43
|
+
const rest = !Array.isArray(goal) && Array.isArray(base) ? base[0] : base;
|
|
44
|
+
let start = initial === false ? goal : initial[key] ?? rest;
|
|
45
|
+
if (!Array.isArray(goal) && Array.isArray(start)) start = start[0];
|
|
46
|
+
if (Array.isArray(goal) && (!Array.isArray(start) || start.length !== goal.length)) throw new Error(`spa/motion: ${key} requires matching numeric tuples`);
|
|
47
|
+
const values = (Array.isArray(start) ? start : [start]).map((value) => motionValue(value));
|
|
48
|
+
const unsubscribers = [];
|
|
49
|
+
if (element) {
|
|
50
|
+
if (Array.isArray(goal)) throw new Error(`spa/motion: DOM targets must be scalar styles or transforms`);
|
|
51
|
+
const cssKey = transform.test(key) ? `transform` : key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`);
|
|
52
|
+
if (!originalStyles.has(cssKey)) originalStyles.set(cssKey, {
|
|
53
|
+
value: element.style.getPropertyValue(cssKey),
|
|
54
|
+
priority: element.style.getPropertyPriority(cssKey)
|
|
55
|
+
});
|
|
56
|
+
} else {
|
|
57
|
+
const write = () => {
|
|
58
|
+
target.write(key, Array.isArray(goal) ? values.map((value) => value.get()) : values[0].get());
|
|
59
|
+
};
|
|
60
|
+
for (const value of values) unsubscribers.push(value.on(`change`, write));
|
|
61
|
+
write();
|
|
62
|
+
}
|
|
63
|
+
const property = {
|
|
64
|
+
values,
|
|
65
|
+
base,
|
|
66
|
+
rest,
|
|
67
|
+
target: start,
|
|
68
|
+
release() {
|
|
69
|
+
for (const release of unsubscribers) release();
|
|
70
|
+
for (const value of values) value.destroy();
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
properties.set(key, property);
|
|
74
|
+
return property;
|
|
75
|
+
};
|
|
76
|
+
const refresh = () => {
|
|
77
|
+
if (dead) return;
|
|
78
|
+
const initial = props[`initial`] ?? {};
|
|
79
|
+
const animateTo = props[`animate`] ?? {};
|
|
80
|
+
const hoverTo = props[`whileHover`] ?? {};
|
|
81
|
+
const tapTo = props[`whileTap`] ?? {};
|
|
82
|
+
const goals = {
|
|
83
|
+
...animateTo,
|
|
84
|
+
...hovered ? hoverTo : {},
|
|
85
|
+
...tapped ? tapTo : {}
|
|
86
|
+
};
|
|
87
|
+
const keys = /* @__PURE__ */ new Set([
|
|
88
|
+
...Object.keys(initial || {}),
|
|
89
|
+
...Object.keys(animateTo),
|
|
90
|
+
...Object.keys(hoverTo),
|
|
91
|
+
...Object.keys(tapTo)
|
|
92
|
+
]);
|
|
93
|
+
let added = false;
|
|
94
|
+
for (const key of keys) {
|
|
95
|
+
const sample = goals[key] ?? (initial === false ? void 0 : initial[key]) ?? hoverTo[key] ?? tapTo[key];
|
|
96
|
+
if (sample === void 0) continue;
|
|
97
|
+
const had = properties.has(key);
|
|
98
|
+
ensure(key, sample, started ? {} : initial);
|
|
99
|
+
if (!had) added = true;
|
|
100
|
+
}
|
|
101
|
+
if (added) bindStyles();
|
|
102
|
+
const transition = props[`transition`] ?? { type: `spring` };
|
|
103
|
+
for (const [key, property] of properties) {
|
|
104
|
+
const goal = goals[key] ?? (initial === false ? void 0 : initial[key]) ?? property.rest;
|
|
105
|
+
if (equal(property.target, goal)) continue;
|
|
106
|
+
property.target = Array.isArray(goal) ? [...goal] : goal;
|
|
107
|
+
const next = Array.isArray(goal) ? goal : [goal];
|
|
108
|
+
if (next.length !== property.values.length) throw new Error(`spa/motion: cannot change the shape of ${key}`);
|
|
109
|
+
property.values.forEach((value, index) => {
|
|
110
|
+
if (reduced() || !started && initial === false) value.jump(next[index]);
|
|
111
|
+
else animate(value, next[index], transition);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
started = true;
|
|
115
|
+
};
|
|
116
|
+
const clearGestures = () => {
|
|
117
|
+
for (const release of gestures) release();
|
|
118
|
+
gestures = [];
|
|
119
|
+
};
|
|
120
|
+
const installGestures = () => {
|
|
121
|
+
clearGestures();
|
|
122
|
+
if (element) {
|
|
123
|
+
if (props[`whileHover`]) gestures.push(hover([element], () => {
|
|
124
|
+
hovered = true;
|
|
125
|
+
refresh();
|
|
126
|
+
return () => {
|
|
127
|
+
hovered = false;
|
|
128
|
+
refresh();
|
|
129
|
+
};
|
|
130
|
+
}));
|
|
131
|
+
if (props[`whileTap`]) gestures.push(press([element], () => {
|
|
132
|
+
tapped = true;
|
|
133
|
+
refresh();
|
|
134
|
+
return () => {
|
|
135
|
+
tapped = false;
|
|
136
|
+
refresh();
|
|
137
|
+
};
|
|
138
|
+
}));
|
|
139
|
+
} else if (props[`whileHover`] || props[`whileTap`]) {
|
|
140
|
+
if (!target.listen) throw new Error(`spa/motion: ${target.kind} gestures require a renderer event adapter`);
|
|
141
|
+
const listen = (event, run) => gestures.push(target.listen(event, run));
|
|
142
|
+
listen(`pointerenter`, () => {
|
|
143
|
+
hovered = true;
|
|
144
|
+
refresh();
|
|
145
|
+
});
|
|
146
|
+
listen(`pointerleave`, () => {
|
|
147
|
+
hovered = false;
|
|
148
|
+
tapped = false;
|
|
149
|
+
refresh();
|
|
150
|
+
});
|
|
151
|
+
listen(`pointerdown`, () => {
|
|
152
|
+
tapped = true;
|
|
153
|
+
refresh();
|
|
154
|
+
});
|
|
155
|
+
listen(`pointerup`, () => {
|
|
156
|
+
tapped = false;
|
|
157
|
+
refresh();
|
|
158
|
+
});
|
|
159
|
+
listen(`pointercancel`, () => {
|
|
160
|
+
tapped = false;
|
|
161
|
+
refresh();
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
};
|
|
165
|
+
const preferenceChanged = () => {
|
|
166
|
+
if (reduced()) for (const property of properties.values()) {
|
|
167
|
+
const goal = Array.isArray(property.target) ? property.target : [property.target];
|
|
168
|
+
property.values.forEach((value, i) => {
|
|
169
|
+
value.jump(goal[i]);
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
preference?.addEventListener(`change`, preferenceChanged);
|
|
174
|
+
return {
|
|
175
|
+
update(next) {
|
|
176
|
+
if (dead) return;
|
|
177
|
+
for (const key of Object.keys(next)) if (!allowed.has(key)) throw new Error(`spa/motion: unknown property motion:${key}`);
|
|
178
|
+
const gestureChange = !started || Boolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) || Boolean(next[`whileTap`]) !== Boolean(props[`whileTap`]);
|
|
179
|
+
props = next;
|
|
180
|
+
if (gestureChange) installGestures();
|
|
181
|
+
refresh();
|
|
182
|
+
},
|
|
183
|
+
dispose() {
|
|
184
|
+
if (dead) return;
|
|
185
|
+
dead = true;
|
|
186
|
+
clearGestures();
|
|
187
|
+
preference?.removeEventListener(`change`, preferenceChanged);
|
|
188
|
+
styles?.();
|
|
189
|
+
for (const [key, property] of properties) {
|
|
190
|
+
property.release();
|
|
191
|
+
if (!element) target.write(key, property.base);
|
|
192
|
+
}
|
|
193
|
+
for (const [key, value] of originalStyles) if (value.value) element.style.setProperty(key, value.value, value.priority);
|
|
194
|
+
else element.style.removeProperty(key);
|
|
195
|
+
properties.clear();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */
|
|
200
|
+
function motionCapability(options = {}) {
|
|
201
|
+
return {
|
|
202
|
+
namespace: `motion`,
|
|
203
|
+
attach: (target) => attach(target, options),
|
|
204
|
+
scheduleFrame(callback) {
|
|
205
|
+
const run = ({ timestamp }) => {
|
|
206
|
+
callback(timestamp);
|
|
207
|
+
};
|
|
208
|
+
frame.render(run, true);
|
|
209
|
+
return () => {
|
|
210
|
+
cancelFrame(run);
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
export { motionCapability };
|
|
216
|
+
|
|
217
|
+
//# sourceMappingURL=motion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"motion.js","names":[],"sources":["../src/motion.ts"],"sourcesContent":["import type { MotionValue, ValueAnimationTransition } from \"motion\"\nimport {\n\tanimate,\n\tcancelFrame,\n\tframe,\n\thover,\n\tmotionValue,\n\tpress,\n\treadStyleValue,\n\tstyleEffect,\n} from \"motion\"\n\nimport type {\n\tCapability,\n\tCapabilityBinding,\n\tCapabilityTarget,\n} from \"./capabilities.ts\"\n\nexport type MotionScalar = number | string\nexport type MotionDOMTarget = Partial<\n\tRecord<\n\t\t| keyof CSSStyleDeclaration\n\t\t| `x`\n\t\t| `y`\n\t\t| `z`\n\t\t| `scale`\n\t\t| `scaleX`\n\t\t| `scaleY`\n\t\t| `rotate`\n\t\t| `rotateX`\n\t\t| `rotateY`\n\t\t| `rotateZ`\n\t\t| `skewX`\n\t\t| `skewY`\n\t\t| `--${string}`,\n\t\tMotionScalar\n\t>\n>\nexport interface MotionThreeTarget {\n\tposition?: readonly [number, number, number]\n\trotation?: readonly [number, number, number]\n\tscale?: number | readonly [number, number, number]\n\topacity?: number\n\tintensity?: number\n\tcolor?: string\n}\nexport type MotionTarget<Kind extends string> = Kind extends `dom`\n\t? MotionDOMTarget\n\t: Kind extends `three`\n\t\t? MotionThreeTarget\n\t\t: Record<string, MotionScalar>\n/** Supported transition options; keep public declarations independent of optional peer types. */\nexport interface MotionTransition {\n\ttype?: `spring` | `tween`\n\tstiffness?: number\n\tdamping?: number\n\tmass?: number\n\tvelocity?: number\n\trestSpeed?: number\n\trestDelta?: number\n\tbounce?: number\n\tvisualDuration?: number\n\tduration?: number\n\tdelay?: number\n\tease?:\n\t\t| `linear`\n\t\t| `easeIn`\n\t\t| `easeOut`\n\t\t| `easeInOut`\n\t\t| `circIn`\n\t\t| `circOut`\n\t\t| `circInOut`\n\t\t| `backIn`\n\t\t| `backOut`\n\t\t| `backInOut`\n\t\t| readonly [number, number, number, number]\n}\nexport interface MotionAttributes<Kind extends string = `dom`> {\n\t[`motion:initial`]?: MotionTarget<Kind> | false\n\t[`motion:animate`]?: MotionTarget<Kind>\n\t[`motion:transition`]?: MotionTransition\n\t[`motion:whileHover`]?: MotionTarget<Kind>\n\t[`motion:whileTap`]?: MotionTarget<Kind>\n}\n\n// eslint-disable-next-line quotes -- TypeScript module augmentation requires a string literal.\ndeclare module \"@atom.io/spa/jsx-runtime\" {\n\t// oxlint-disable-next-line typescript/no-empty-interface -- Declaration merging adds optional motion attributes.\n\tinterface CapabilityAttributes<\n\t\tKind extends string,\n\t> extends MotionAttributes<Kind> {}\n}\n\nexport interface MotionCapabilityOptions {\n\t/** Respect the user's reduced-motion preference by default. */\n\treducedMotion?: `user` | `always` | `never`\n}\ntype TargetValues = Record<string, MotionScalar | readonly number[]>\ninterface AnimatedProperty {\n\tvalues: MotionValue<MotionScalar>[]\n\tbase: MotionScalar | readonly number[]\n\trest: MotionScalar | readonly number[]\n\ttarget: MotionScalar | readonly number[]\n\trelease(): void\n}\nconst transform =\n\t/^(x|y|z|translate.*|scale.*|rotate.*|skew.*|transformPerspective)$/\nconst allowed = new Set([\n\t`initial`,\n\t`animate`,\n\t`transition`,\n\t`whileHover`,\n\t`whileTap`,\n])\nfunction equal(a: unknown, b: unknown): boolean {\n\treturn (\n\t\tObject.is(a, b) ||\n\t\t(Array.isArray(a) &&\n\t\t\tArray.isArray(b) &&\n\t\t\ta.length === b.length &&\n\t\t\ta.every((value, index) => Object.is(value, b[index])))\n\t)\n}\n\nfunction attach(\n\ttarget: CapabilityTarget,\n\toptions: MotionCapabilityOptions,\n): CapabilityBinding {\n\tif (target.kind !== `dom` && (!target.read || !target.write))\n\t\tthrow new Error(\n\t\t\t`spa/motion: renderer ${target.kind} does not support motion`,\n\t\t)\n\tconst element =\n\t\ttarget.kind === `dom` ? (target.node as HTMLElement | SVGElement) : undefined\n\tconst properties = new Map<string, AnimatedProperty>()\n\tconst originalStyles = new Map<string, { value: string; priority: string }>()\n\tlet styles: (() => void) | undefined\n\tlet props: Record<string, unknown> = {}\n\tlet hovered = false\n\tlet tapped = false\n\tlet started = false\n\tlet dead = false\n\tlet gestures: (() => void)[] = []\n\tconst preference =\n\t\toptions.reducedMotion !== `never` && typeof matchMedia === `function`\n\t\t\t? matchMedia(`(prefers-reduced-motion: reduce)`)\n\t\t\t: undefined\n\tconst reduced = () =>\n\t\toptions.reducedMotion === `always` ||\n\t\t(options.reducedMotion !== `never` && preference?.matches)\n\tconst read = (key: string, fallback: unknown): any => {\n\t\tconst value = element ? readStyleValue(element, key) : target.read!(key)\n\t\treturn value === undefined || value === ``\n\t\t\t? typeof fallback === `number`\n\t\t\t\t? key.startsWith(`scale`) || key === `opacity`\n\t\t\t\t\t? 1\n\t\t\t\t\t: 0\n\t\t\t\t: fallback\n\t\t\t: value\n\t}\n\tconst bindStyles = () => {\n\t\tstyles?.()\n\t\tif (element)\n\t\t\tstyles = styleEffect(\n\t\t\t\t[element],\n\t\t\t\tObject.fromEntries(\n\t\t\t\t\t[...properties].map(([key, property]) => [key, property.values[0]]),\n\t\t\t\t) as any,\n\t\t\t)\n\t}\n\tconst ensure = (\n\t\tkey: string,\n\t\tgoal: MotionScalar | readonly number[],\n\t\tinitial: TargetValues | false,\n\t): AnimatedProperty => {\n\t\tconst existing = properties.get(key)\n\t\tif (existing) {\n\t\t\tif (existing.values.length !== (Array.isArray(goal) ? goal.length : 1))\n\t\t\t\tthrow new Error(`spa/motion: cannot change the shape of ${key}`)\n\t\t\treturn existing\n\t\t}\n\t\tconst base = read(key, goal) as MotionScalar | readonly number[]\n\t\tconst rest = !Array.isArray(goal) && Array.isArray(base) ? base[0]! : base\n\t\tlet start = initial === false ? goal : (initial[key] ?? rest)\n\t\tif (!Array.isArray(goal) && Array.isArray(start)) start = start[0]!\n\t\tif (\n\t\t\tArray.isArray(goal) &&\n\t\t\t(!Array.isArray(start) || start.length !== goal.length)\n\t\t)\n\t\t\tthrow new Error(`spa/motion: ${key} requires matching numeric tuples`)\n\t\tconst values = (Array.isArray(start) ? start : [start]).map((value) =>\n\t\t\tmotionValue(value),\n\t\t) as MotionValue<MotionScalar>[]\n\t\tconst unsubscribers: (() => void)[] = []\n\t\tif (element) {\n\t\t\tif (Array.isArray(goal))\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`spa/motion: DOM targets must be scalar styles or transforms`,\n\t\t\t\t)\n\t\t\tconst cssKey = transform.test(key)\n\t\t\t\t? `transform`\n\t\t\t\t: key.replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)\n\t\t\tif (!originalStyles.has(cssKey))\n\t\t\t\toriginalStyles.set(cssKey, {\n\t\t\t\t\tvalue: element.style.getPropertyValue(cssKey),\n\t\t\t\t\tpriority: element.style.getPropertyPriority(cssKey),\n\t\t\t\t})\n\t\t} else {\n\t\t\tconst write = () => {\n\t\t\t\ttarget.write!(\n\t\t\t\t\tkey,\n\t\t\t\t\tArray.isArray(goal)\n\t\t\t\t\t\t? values.map((value) => value.get())\n\t\t\t\t\t\t: values[0].get(),\n\t\t\t\t)\n\t\t\t}\n\t\t\tfor (const value of values) unsubscribers.push(value.on(`change`, write))\n\t\t\twrite()\n\t\t}\n\t\tconst property: AnimatedProperty = {\n\t\t\tvalues,\n\t\t\tbase,\n\t\t\trest,\n\t\t\ttarget: start,\n\t\t\trelease() {\n\t\t\t\tfor (const release of unsubscribers) release()\n\t\t\t\tfor (const value of values) value.destroy()\n\t\t\t},\n\t\t}\n\t\tproperties.set(key, property)\n\t\treturn property\n\t}\n\tconst refresh = () => {\n\t\tif (dead) return\n\t\tconst initial = (props[`initial`] ?? {}) as TargetValues | false\n\t\tconst animateTo = (props[`animate`] ?? {}) as TargetValues\n\t\tconst hoverTo = (props[`whileHover`] ?? {}) as TargetValues\n\t\tconst tapTo = (props[`whileTap`] ?? {}) as TargetValues\n\t\tconst goals = {\n\t\t\t...animateTo,\n\t\t\t...(hovered ? hoverTo : {}),\n\t\t\t...(tapped ? tapTo : {}),\n\t\t}\n\t\tconst keys = new Set([\n\t\t\t...Object.keys(initial || {}),\n\t\t\t...Object.keys(animateTo),\n\t\t\t...Object.keys(hoverTo),\n\t\t\t...Object.keys(tapTo),\n\t\t])\n\t\tlet added = false\n\t\tfor (const key of keys) {\n\t\t\tconst sample =\n\t\t\t\tgoals[key] ??\n\t\t\t\t(initial === false ? undefined : initial[key]) ??\n\t\t\t\thoverTo[key] ??\n\t\t\t\ttapTo[key]\n\t\t\tif (sample === undefined) continue\n\t\t\tconst had = properties.has(key)\n\t\t\tensure(key, sample, started ? {} : initial)\n\t\t\tif (!had) added = true\n\t\t}\n\t\tif (added) bindStyles()\n\t\tconst transition = (props[`transition`] ?? {\n\t\t\ttype: `spring`,\n\t\t}) as ValueAnimationTransition\n\t\tfor (const [key, property] of properties) {\n\t\t\tconst goal =\n\t\t\t\tgoals[key] ??\n\t\t\t\t(initial === false ? undefined : initial[key]) ??\n\t\t\t\tproperty.rest\n\t\t\tif (equal(property.target, goal)) continue\n\t\t\tproperty.target = Array.isArray(goal) ? [...goal] : goal\n\t\t\tconst next = Array.isArray(goal) ? goal : [goal]\n\t\t\tif (next.length !== property.values.length)\n\t\t\t\tthrow new Error(`spa/motion: cannot change the shape of ${key}`)\n\t\t\tproperty.values.forEach((value, index) => {\n\t\t\t\tif (reduced() || (!started && initial === false)) value.jump(next[index])\n\t\t\t\telse animate(value, next[index], transition)\n\t\t\t})\n\t\t}\n\t\tstarted = true\n\t}\n\tconst clearGestures = () => {\n\t\tfor (const release of gestures) release()\n\t\tgestures = []\n\t}\n\tconst installGestures = () => {\n\t\tclearGestures()\n\t\tif (element) {\n\t\t\tif (props[`whileHover`])\n\t\t\t\tgestures.push(\n\t\t\t\t\thover([element], () => {\n\t\t\t\t\t\thovered = true\n\t\t\t\t\t\trefresh()\n\t\t\t\t\t\treturn () => {\n\t\t\t\t\t\t\thovered = false\n\t\t\t\t\t\t\trefresh()\n\t\t\t\t\t\t}\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t\tif (props[`whileTap`])\n\t\t\t\tgestures.push(\n\t\t\t\t\tpress([element], () => {\n\t\t\t\t\t\ttapped = true\n\t\t\t\t\t\trefresh()\n\t\t\t\t\t\treturn () => {\n\t\t\t\t\t\t\ttapped = false\n\t\t\t\t\t\t\trefresh()\n\t\t\t\t\t\t}\n\t\t\t\t\t}),\n\t\t\t\t)\n\t\t} else if (props[`whileHover`] || props[`whileTap`]) {\n\t\t\tif (!target.listen)\n\t\t\t\tthrow new Error(\n\t\t\t\t\t`spa/motion: ${target.kind} gestures require a renderer event adapter`,\n\t\t\t\t)\n\t\t\tconst listen = (event: string, run: () => void) =>\n\t\t\t\tgestures.push(target.listen!(event, run))\n\t\t\tlisten(`pointerenter`, () => {\n\t\t\t\thovered = true\n\t\t\t\trefresh()\n\t\t\t})\n\t\t\tlisten(`pointerleave`, () => {\n\t\t\t\thovered = false\n\t\t\t\ttapped = false\n\t\t\t\trefresh()\n\t\t\t})\n\t\t\tlisten(`pointerdown`, () => {\n\t\t\t\ttapped = true\n\t\t\t\trefresh()\n\t\t\t})\n\t\t\tlisten(`pointerup`, () => {\n\t\t\t\ttapped = false\n\t\t\t\trefresh()\n\t\t\t})\n\t\t\tlisten(`pointercancel`, () => {\n\t\t\t\ttapped = false\n\t\t\t\trefresh()\n\t\t\t})\n\t\t}\n\t}\n\tconst preferenceChanged = () => {\n\t\tif (reduced()) {\n\t\t\tfor (const property of properties.values()) {\n\t\t\t\tconst goal = Array.isArray(property.target)\n\t\t\t\t\t? property.target\n\t\t\t\t\t: [property.target]\n\t\t\t\tproperty.values.forEach((value, i) => {\n\t\t\t\t\tvalue.jump(goal[i])\n\t\t\t\t})\n\t\t\t}\n\t\t}\n\t}\n\tpreference?.addEventListener(`change`, preferenceChanged)\n\treturn {\n\t\tupdate(next) {\n\t\t\tif (dead) return\n\t\t\tfor (const key of Object.keys(next))\n\t\t\t\tif (!allowed.has(key))\n\t\t\t\t\tthrow new Error(`spa/motion: unknown property motion:${key}`)\n\t\t\tconst gestureChange =\n\t\t\t\t!started ||\n\t\t\t\tBoolean(next[`whileHover`]) !== Boolean(props[`whileHover`]) ||\n\t\t\t\tBoolean(next[`whileTap`]) !== Boolean(props[`whileTap`])\n\t\t\tprops = next\n\t\t\tif (gestureChange) installGestures()\n\t\t\trefresh()\n\t\t},\n\t\tdispose() {\n\t\t\tif (dead) return\n\t\t\tdead = true\n\t\t\tclearGestures()\n\t\t\tpreference?.removeEventListener(`change`, preferenceChanged)\n\t\t\tstyles?.()\n\t\t\tfor (const [key, property] of properties) {\n\t\t\t\tproperty.release()\n\t\t\t\tif (!element) target.write!(key, property.base)\n\t\t\t}\n\t\t\tfor (const [key, value] of originalStyles) {\n\t\t\t\tif (value.value)\n\t\t\t\t\telement!.style.setProperty(key, value.value, value.priority)\n\t\t\t\telse element!.style.removeProperty(key)\n\t\t\t}\n\t\t\tproperties.clear()\n\t\t},\n\t}\n}\n\n/** Explicit, root-scoped motion:* integration. Importing it never registers globally. */\nexport function motionCapability(\n\toptions: MotionCapabilityOptions = {},\n): Capability {\n\treturn {\n\t\tnamespace: `motion`,\n\t\tattach: (target) => attach(target, options),\n\t\tscheduleFrame(callback) {\n\t\t\tconst run = ({ timestamp }: { timestamp: number }) => {\n\t\t\t\tcallback(timestamp)\n\t\t\t}\n\t\t\tframe.render(run, true)\n\t\t\treturn () => {\n\t\t\t\tcancelFrame(run)\n\t\t\t}\n\t\t},\n\t}\n}\n"],"mappings":";;AAyGA,MAAM,YACL;AACD,MAAM,0BAAU,IAAI,IAAI;CACvB;CACA;CACA;CACA;CACA;AACD,CAAC;AACD,SAAS,MAAM,GAAY,GAAqB;CAC/C,OACC,OAAO,G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
|
1
|
+
import { a as Child, l as VNode, r as CapabilityTarget, s as RootOptions } from "./capabilities-yC9TsHDI.js";
|
|
2
|
+
export interface SceneHost<N> {
|
|
3
|
+
target?(node: N): CapabilityTarget;
|
|
4
|
+
create(type: string, props: Record<string, any>): N;
|
|
5
|
+
update(node: N, props: Record<string, any>, previous: Record<string, any>): void;
|
|
6
|
+
children(parent: N, children: N[]): void;
|
|
7
|
+
destroy(node: N): void;
|
|
8
|
+
}
|
|
9
|
+
export interface SceneRenderer<N> {
|
|
10
|
+
mount(setup: () => Child, parent: N): void;
|
|
11
|
+
render(setup: () => Child, parent: N, options?: RootOptions): {
|
|
12
|
+
flush(): void;
|
|
13
|
+
dispose(): void;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/** A scene renderer shares SPA's owner, store, scheduler, and component lifecycle. */
|
|
17
|
+
export declare function createSceneRenderer<N>(host: SceneHost<N>): SceneRenderer<N>;
|
|
18
|
+
/** Define a typed scene component without introducing another JSX runtime. */
|
|
19
|
+
export declare function sceneComponent<P>(type: string): (props: P) => VNode;
|
|
20
|
+
/** Bind imperative scene work to the current owner and its reactive dependencies. */
|
|
21
|
+
export declare function bindScene<T>(read: () => T, apply: (value: T) => void): void;
|
|
22
|
+
/** Scene drawing follows an installed capability clock, or the browser clock. */
|
|
23
|
+
export declare function onSceneFrame(callback: (time: number) => void): void;
|
|
24
|
+
//# sourceMappingURL=renderer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderer.d.ts","names":[],"sources":["../src/renderer.ts"],"mappings":";iBAgBiB,UAAU;EAC1B,QAAQ,MAAM,IAAI;EAClB,OAAO,cAAc,OAAO,sBAAsB;EAClD,OACC,MAAM,GACN,OAAO,qBACP,UAAU;EAEX,SAAS,QAAQ,GAAG,UAAU;EAC9B,QAAQ,MAAM;;iBAQE,cAAc;EAC9B,MAAM,aAAa,OAAO,QAAQ;EAClC,OACC,aAAa,OACb,QAAQ,GACR,UAAU;IACN;IAAe;;;;wBAIL,oBAAoB,GAAG,MAAM,UAAU,KAAK,cAAc;;wBAyJ1D,eAAe,GAAG,gBAAgB,OAAO,MAAM;;wBAK/C,UAAU,GAAG,YAAY,GAAG,QAAQ,OAAO;;wBAK3C,aAAa,WAAW"}
|
package/dist/renderer.js
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { C as within, a as createScope, c as invalidate, f as owner, h as untrack, i as createRoot, l as observe, n as capabilityProps, o as dispose, s as fail } from "./capabilities-BuZUQpiD.js";
|
|
2
|
+
import { Fragment, merge } from "./jsx-runtime.js";
|
|
3
|
+
// src/renderer.ts
|
|
4
|
+
/** A scene renderer shares SPA's owner, store, scheduler, and component lifecycle. */
|
|
5
|
+
function createSceneRenderer(host) {
|
|
6
|
+
function mount(source, scope, changed) {
|
|
7
|
+
let entries = [];
|
|
8
|
+
observe(scope, source, (next) => {
|
|
9
|
+
const values = Array.isArray(next) ? next.flat(Infinity) : [next];
|
|
10
|
+
const old = new Map(entries.map((entry, i) => [key(entry.value, i), entry]));
|
|
11
|
+
const seen = /* @__PURE__ */ new Set();
|
|
12
|
+
entries = values.map((value, i) => {
|
|
13
|
+
const id = key(value, i);
|
|
14
|
+
if (seen.has(id)) throw new Error(`spa: duplicate scene key`);
|
|
15
|
+
seen.add(id);
|
|
16
|
+
let entry = old.get(id);
|
|
17
|
+
old.delete(id);
|
|
18
|
+
if (entry && !same(entry.value, value)) {
|
|
19
|
+
dispose(entry.scope);
|
|
20
|
+
entry = void 0;
|
|
21
|
+
}
|
|
22
|
+
if (entry) {
|
|
23
|
+
entry.value = value;
|
|
24
|
+
invalidate(entry.scope);
|
|
25
|
+
return entry;
|
|
26
|
+
}
|
|
27
|
+
const childScope = createScope(scope);
|
|
28
|
+
const created = {
|
|
29
|
+
scope: childScope,
|
|
30
|
+
value,
|
|
31
|
+
nodes: null
|
|
32
|
+
};
|
|
33
|
+
created.nodes = build(() => created.value, childScope, changed);
|
|
34
|
+
return created;
|
|
35
|
+
});
|
|
36
|
+
for (const entry of old.values()) dispose(entry.scope);
|
|
37
|
+
changed();
|
|
38
|
+
});
|
|
39
|
+
return () => entries.flatMap((entry) => entry.nodes());
|
|
40
|
+
}
|
|
41
|
+
function key(value, i) {
|
|
42
|
+
const explicit = value?.props?.[`key`];
|
|
43
|
+
return explicit == null ? `index:${i}` : `key:${explicit}`;
|
|
44
|
+
}
|
|
45
|
+
function same(a, b) {
|
|
46
|
+
return typeof a === typeof b && a?.type === b?.type;
|
|
47
|
+
}
|
|
48
|
+
function build(source, scope, changed) {
|
|
49
|
+
const value = untrack(source);
|
|
50
|
+
if (typeof value === `function`) return mount(() => source()(), scope, changed);
|
|
51
|
+
if (value == null || typeof value === `boolean`) return () => [];
|
|
52
|
+
if (typeof value !== `object`) throw new Error(`spa: scene children must be nodes`);
|
|
53
|
+
if (!(`type` in value) || !(`props` in value)) throw new Error(`spa: invalid scene child`);
|
|
54
|
+
const vnode = value;
|
|
55
|
+
const props = merge(() => source().props);
|
|
56
|
+
if (!vnode.type || vnode.type === Fragment) return mount(() => props[`children`], scope, changed);
|
|
57
|
+
if (typeof vnode.type === `function`) {
|
|
58
|
+
const output = within(scope, () => untrack(() => vnode.type(props)));
|
|
59
|
+
return mount(() => output, scope, changed);
|
|
60
|
+
}
|
|
61
|
+
const node = host.create(vnode.type, untrack(() => props));
|
|
62
|
+
scope._cleanups.add(() => {
|
|
63
|
+
host.destroy(node);
|
|
64
|
+
});
|
|
65
|
+
const nativeProps = capabilityProps(scope, host.target?.(node) ?? {
|
|
66
|
+
kind: `scene`,
|
|
67
|
+
node
|
|
68
|
+
});
|
|
69
|
+
let previous = {};
|
|
70
|
+
observe(scope, () => ({ ...props }), (all) => {
|
|
71
|
+
const next = nativeProps(all);
|
|
72
|
+
host.update(node, next, previous);
|
|
73
|
+
previous = next;
|
|
74
|
+
});
|
|
75
|
+
let children = () => [];
|
|
76
|
+
const sync = () => {
|
|
77
|
+
host.children(node, children());
|
|
78
|
+
};
|
|
79
|
+
children = mount(() => props[`children`], scope, sync);
|
|
80
|
+
sync();
|
|
81
|
+
const ref = untrack(() => props[`ref`]);
|
|
82
|
+
if (ref) scope._root._mounts.unshift(() => {
|
|
83
|
+
if (!scope._dead) {
|
|
84
|
+
scope._cleanups.add(() => ref(null));
|
|
85
|
+
ref(node);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
return () => [node];
|
|
89
|
+
}
|
|
90
|
+
function attach(setup, parent, scope) {
|
|
91
|
+
let nodes = () => [];
|
|
92
|
+
const sync = () => {
|
|
93
|
+
host.children(parent, nodes());
|
|
94
|
+
};
|
|
95
|
+
nodes = mount(() => untrack(setup), scope, sync);
|
|
96
|
+
sync();
|
|
97
|
+
scope._cleanups.add(() => {
|
|
98
|
+
host.children(parent, []);
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
return {
|
|
102
|
+
/** Attach beneath the current component; disposal follows its lifetime. */
|
|
103
|
+
mount(setup, parent) {
|
|
104
|
+
if (!owner) throw new Error(`spa: scene mount requires an owner`);
|
|
105
|
+
const scope = createScope(owner);
|
|
106
|
+
try {
|
|
107
|
+
attach(setup, parent, scope);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
fail(scope, error);
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
render(setup, parent, options = {}) {
|
|
113
|
+
const root = createRoot(options);
|
|
114
|
+
try {
|
|
115
|
+
within(root, () => {
|
|
116
|
+
attach(setup, parent, root);
|
|
117
|
+
});
|
|
118
|
+
root.flush();
|
|
119
|
+
} catch (error) {
|
|
120
|
+
fail(root, error);
|
|
121
|
+
}
|
|
122
|
+
return {
|
|
123
|
+
flush: root.flush,
|
|
124
|
+
dispose: () => {
|
|
125
|
+
dispose(root);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
/** Define a typed scene component without introducing another JSX runtime. */
|
|
132
|
+
function sceneComponent(type) {
|
|
133
|
+
return (props) => ({
|
|
134
|
+
type,
|
|
135
|
+
props
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
/** Bind imperative scene work to the current owner and its reactive dependencies. */
|
|
139
|
+
function bindScene(read, apply) {
|
|
140
|
+
observe(owner, read, apply);
|
|
141
|
+
}
|
|
142
|
+
/** Scene drawing follows an installed capability clock, or the browser clock. */
|
|
143
|
+
function onSceneFrame(callback) {
|
|
144
|
+
const scope = owner;
|
|
145
|
+
const clocks = [...scope._root._capabilities.values()].filter((capability) => capability.scheduleFrame);
|
|
146
|
+
if (clocks.length > 1) throw new Error(`spa: multiple frame clock capabilities`);
|
|
147
|
+
const draw = (time) => {
|
|
148
|
+
if (scope._dead) return;
|
|
149
|
+
try {
|
|
150
|
+
callback(time);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
try {
|
|
153
|
+
fail(scope._root, error);
|
|
154
|
+
} catch (failure) {
|
|
155
|
+
scope._root.error(failure);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
let stop;
|
|
160
|
+
if (clocks[0]) stop = clocks[0].scheduleFrame(draw);
|
|
161
|
+
else {
|
|
162
|
+
let frame = 0;
|
|
163
|
+
const tick = (time) => {
|
|
164
|
+
if (scope._dead) return;
|
|
165
|
+
draw(time);
|
|
166
|
+
if (!scope._dead) frame = requestAnimationFrame(tick);
|
|
167
|
+
};
|
|
168
|
+
frame = requestAnimationFrame(tick);
|
|
169
|
+
stop = () => {
|
|
170
|
+
cancelAnimationFrame(frame);
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
scope._cleanups.add(stop);
|
|
174
|
+
}
|
|
175
|
+
export { bindScene, createSceneRenderer, onSceneFrame, sceneComponent };
|
|
176
|
+
|
|
177
|
+
//# sourceMappingURL=renderer.js.map
|