@pairdraw/design-jsx 0.0.0-stage → 0.16.1
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/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/chunks/attributes.js +32 -0
- package/dist/chunks/attributes.js.map +1 -0
- package/dist/chunks/behaviours/elements.js +321 -0
- package/dist/chunks/behaviours/elements.js.map +1 -0
- package/dist/chunks/behaviours/render.js +368 -0
- package/dist/chunks/behaviours/render.js.map +1 -0
- package/dist/chunks/component-properties.js +809 -0
- package/dist/chunks/component-properties.js.map +1 -0
- package/dist/chunks/components.js +78 -0
- package/dist/chunks/components.js.map +1 -0
- package/dist/chunks/connectors.js +290 -0
- package/dist/chunks/connectors.js.map +1 -0
- package/dist/chunks/effects.js +57 -0
- package/dist/chunks/effects.js.map +1 -0
- package/dist/chunks/elements.js +618 -0
- package/dist/chunks/elements.js.map +1 -0
- package/dist/chunks/export/behaviours.js +113 -0
- package/dist/chunks/export/behaviours.js.map +1 -0
- package/dist/chunks/export/bindings.js +273 -0
- package/dist/chunks/export/bindings.js.map +1 -0
- package/dist/chunks/export/decoration.js +96 -0
- package/dist/chunks/export/decoration.js.map +1 -0
- package/dist/chunks/export/font.js +17 -0
- package/dist/chunks/export/font.js.map +1 -0
- package/dist/chunks/export/icons.js +34 -0
- package/dist/chunks/export/icons.js.map +1 -0
- package/dist/chunks/export/index.js +1390 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/fields.js +1065 -0
- package/dist/chunks/fields.js.map +1 -0
- package/dist/chunks/index.js +438 -0
- package/dist/chunks/index.js.map +1 -0
- package/dist/chunks/jsx-dev-runtime.js +16 -0
- package/dist/chunks/jsx-dev-runtime.js.map +1 -0
- package/dist/index.d.ts +543 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/jsx-dev-runtime.d.ts +2 -0
- package/dist/jsx-dev-runtime.js +2 -0
- package/dist/jsx-runtime.d.ts +44 -0
- package/dist/jsx-runtime.d.ts.map +1 -0
- package/dist/jsx-runtime.js +2 -0
- package/dist/tree.d.ts +261 -0
- package/dist/tree.d.ts.map +1 -0
- package/package.json +66 -4
- package/src/attributes.ts +30 -0
- package/src/behaviours/elements.ts +47 -0
- package/src/behaviours/index.ts +253 -0
- package/src/behaviours/render.ts +144 -0
- package/src/component-properties.ts +428 -0
- package/src/components.ts +109 -0
- package/src/connectors.ts +316 -0
- package/src/definitions.ts +277 -0
- package/src/effects.ts +80 -0
- package/src/elements.ts +321 -0
- package/src/enums.ts +182 -0
- package/src/export/behaviours.ts +143 -0
- package/src/export/bindings.ts +155 -0
- package/src/export/decoration.ts +39 -0
- package/src/export/font.ts +25 -0
- package/src/export/helpers.ts +75 -0
- package/src/export/icons.ts +42 -0
- package/src/export/index.ts +297 -0
- package/src/export/paint.ts +276 -0
- package/src/export/props.ts +369 -0
- package/src/export/state.ts +52 -0
- package/src/export/text.ts +155 -0
- package/src/export/value.ts +99 -0
- package/src/export/variables.ts +393 -0
- package/src/fields.ts +39 -0
- package/src/figjam-text.ts +37 -0
- package/src/global.d.ts +4 -0
- package/src/helpers.ts +135 -0
- package/src/index.ts +155 -0
- package/src/jsx-dev-runtime.ts +1 -0
- package/src/jsx-runtime.ts +51 -0
- package/src/mini-react.ts +23 -0
- package/src/overrides/effects.ts +39 -0
- package/src/overrides/fills.ts +56 -0
- package/src/overrides/state.ts +55 -0
- package/src/overrides/strokes.ts +109 -0
- package/src/overrides/text.ts +116 -0
- package/src/overrides/vector.ts +122 -0
- package/src/paints.ts +230 -0
- package/src/props-overrides.ts +481 -0
- package/src/reconcile.ts +134 -0
- package/src/reference/authoring.md +76 -0
- package/src/reference/examples.ts +49 -0
- package/src/reference.ts +19 -0
- package/src/refs.ts +48 -0
- package/src/render.ts +147 -0
- package/src/renderer.ts +598 -0
- package/src/scalar-bindings.ts +36 -0
- package/src/schema.ts +213 -0
- package/src/services.ts +36 -0
- package/src/shapes-with-text.ts +73 -0
- package/src/stickies.ts +72 -0
- package/src/streaming/index.ts +47 -0
- package/src/streaming/literals.ts +82 -0
- package/src/streaming/projection.ts +189 -0
- package/src/text-content.ts +233 -0
- package/src/transform.ts +78 -0
- package/src/tree.ts +285 -0
- package/src/types.ts +13 -0
- package/src/variables/graph.ts +265 -0
- package/src/variables/index.ts +124 -0
- package/src/variables/props.ts +77 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Danila Poyarkov and OpenPencil contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @pairdraw/design-jsx
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Pairdraw design JSX: build editable scene trees with `<Frame>`, `<Text>`, and the other elements, style them with paint and effect helpers and design variables, and export scene nodes back to JSX.
|
|
4
|
+
|
|
5
|
+
Set the JSX import source in `tsconfig.json` (`"jsx": "react-jsx"`, `"jsxImportSource": "@pairdraw/design-jsx"`) or per file:
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
/** @jsxImportSource @pairdraw/design-jsx */
|
|
9
|
+
import { renderTree } from '@pairdraw/core/design-jsx'
|
|
10
|
+
import { Frame, Text, solid } from '@pairdraw/design-jsx'
|
|
11
|
+
import { SceneGraph } from '@pairdraw/scene-graph'
|
|
12
|
+
|
|
13
|
+
const graph = new SceneGraph()
|
|
14
|
+
await renderTree(
|
|
15
|
+
graph,
|
|
16
|
+
<Frame w={320} p={16} fill={solid('#FFFFFF')}>
|
|
17
|
+
<Text>Hello</Text>
|
|
18
|
+
</Frame>
|
|
19
|
+
)
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
JSX written as a string, as agents and the MCP `render` tool send it, needs no build step:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { renderJSX } from '@pairdraw/core/design-jsx'
|
|
26
|
+
|
|
27
|
+
await renderJSX(graph, '<Frame w={320} p={16} bg="#FFFFFF"><Text>Hello</Text></Frame>')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
Inside `<Text>`, `<Span>` styles some of its characters and `<Paragraph>` writes its paragraphs and list items:
|
|
31
|
+
|
|
32
|
+
```tsx
|
|
33
|
+
<Text size={16}>
|
|
34
|
+
<Paragraph spacing={8}>Hello <Span weight="bold" color="#EE2222">world</Span></Paragraph>
|
|
35
|
+
<Paragraph list="ordered">First</Paragraph>
|
|
36
|
+
<Paragraph list="ordered" level={2}>Nested</Paragraph>
|
|
37
|
+
</Text>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Components that behave as controls are written with Reka UI's element names, in TSX (import `Switch`, `Slider`, `Tabs`, and the other namespaces, typed with their behaviour props) or in strings, and export writes them back the same way:
|
|
41
|
+
|
|
42
|
+
```tsx
|
|
43
|
+
<Switch.Root name="Switch" modelValue="State">
|
|
44
|
+
<Component name="State=Off" w={44} h={24}><Switch.Thumb x={2} y={2} w={20} h={20} /></Component>
|
|
45
|
+
<Component name="State=On" w={44} h={24}><Switch.Thumb x={22} y={2} w={20} h={20} /></Component>
|
|
46
|
+
</Switch.Root>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
This package depends only on `@pairdraw/scene-graph` and `@pairdraw/emit`. Rendering needs icons, SVG conversion, and layout, which `@pairdraw/core/design-jsx` provides; other engines can supply their own through `createDesignJSXRenderer(services)`.
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { parser } from "@lezer/javascript";
|
|
2
|
+
//#region src/attributes.ts
|
|
3
|
+
const elementParser = parser.configure({
|
|
4
|
+
dialect: "jsx",
|
|
5
|
+
top: "SingleExpression"
|
|
6
|
+
});
|
|
7
|
+
/**
|
|
8
|
+
* Split JSX attributes, such as `w={200} bg="#FF0000"`, into each attribute as written.
|
|
9
|
+
* Throws on a syntax error or a spread, whose props cannot be named.
|
|
10
|
+
*/
|
|
11
|
+
function parseJSXAttributes(source) {
|
|
12
|
+
const element = `<Frame ${source} />`;
|
|
13
|
+
const tree = elementParser.parse(element);
|
|
14
|
+
const attributes = [];
|
|
15
|
+
const invalid = () => /* @__PURE__ */ new SyntaxError(`Invalid JSX attributes: ${source}`);
|
|
16
|
+
tree.iterate({ enter(node) {
|
|
17
|
+
if (node.type.isError || node.name === "JSXSpreadAttribute") throw invalid();
|
|
18
|
+
if (node.name !== "JSXAttribute") return void 0;
|
|
19
|
+
const key = node.node.firstChild;
|
|
20
|
+
if (key?.name !== "JSXIdentifier") throw invalid();
|
|
21
|
+
attributes.push({
|
|
22
|
+
name: element.slice(key.from, key.to),
|
|
23
|
+
source: element.slice(node.from, node.to)
|
|
24
|
+
});
|
|
25
|
+
return false;
|
|
26
|
+
} });
|
|
27
|
+
return attributes;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
export { parseJSXAttributes };
|
|
31
|
+
|
|
32
|
+
//# sourceMappingURL=attributes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"attributes.js","names":[],"sources":["../../src/attributes.ts"],"sourcesContent":["import { parser } from '@lezer/javascript'\n\nimport type { JSXAttributeSource } from './export'\n\nconst elementParser = parser.configure({ dialect: 'jsx', top: 'SingleExpression' })\n\n/**\n * Split JSX attributes, such as `w={200} bg=\"#FF0000\"`, into each attribute as written.\n * Throws on a syntax error or a spread, whose props cannot be named.\n */\nexport function parseJSXAttributes(source: string): JSXAttributeSource[] {\n const element = `<Frame ${source} />`\n const tree = elementParser.parse(element)\n const attributes: JSXAttributeSource[] = []\n const invalid = () => new SyntaxError(`Invalid JSX attributes: ${source}`)\n tree.iterate({\n enter(node) {\n if (node.type.isError || node.name === 'JSXSpreadAttribute') throw invalid()\n if (node.name !== 'JSXAttribute') return undefined\n const key = node.node.firstChild\n if (key?.name !== 'JSXIdentifier') throw invalid()\n attributes.push({\n name: element.slice(key.from, key.to),\n source: element.slice(node.from, node.to)\n })\n return false\n }\n })\n return attributes\n}\n"],"mappings":";;AAIA,MAAM,gBAAgB,OAAO,UAAU;CAAE,SAAS;CAAO,KAAK;AAAmB,CAAC;;;;;AAMlF,SAAgB,mBAAmB,QAAsC;CACvE,MAAM,UAAU,UAAU,OAAO;CACjC,MAAM,OAAO,cAAc,MAAM,OAAO;CACxC,MAAM,aAAmC,CAAC;CAC1C,MAAM,gCAAgB,IAAI,YAAY,2BAA2B,QAAQ;CACzE,KAAK,QAAQ,EACX,MAAM,MAAM;EACV,IAAI,KAAK,KAAK,WAAW,KAAK,SAAS,sBAAsB,MAAM,QAAQ;EAC3E,IAAI,KAAK,SAAS,gBAAgB,OAAO,KAAA;EACzC,MAAM,MAAM,KAAK,KAAK;EACtB,IAAI,KAAK,SAAS,iBAAiB,MAAM,QAAQ;EACjD,WAAW,KAAK;GACd,MAAM,QAAQ,MAAM,IAAI,MAAM,IAAI,EAAE;GACpC,QAAQ,QAAQ,MAAM,KAAK,MAAM,KAAK,EAAE;EAC1C,CAAC;EACD,OAAO;CACT,EACF,CAAC;CACD,OAAO;AACT"}
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import { behaviourFromSpec, withBehaviour } from "@pairdraw/scene-graph/behaviours";
|
|
2
|
+
import { createComponentPropertyId } from "@pairdraw/scene-graph/components";
|
|
3
|
+
import { createSlotProperty } from "@pairdraw/scene-graph/slots";
|
|
4
|
+
function inlineFragments(children) {
|
|
5
|
+
return children.flatMap((child) => typeof child !== "string" && child.type === "" ? child.children : [child]);
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The text an element holds as children, such as a sticky note's body or a connector's label.
|
|
9
|
+
* Elements among its children are not text and are skipped.
|
|
10
|
+
*/
|
|
11
|
+
function plainTextOf(tree) {
|
|
12
|
+
return tree.children.filter((child) => typeof child === "string").join("");
|
|
13
|
+
}
|
|
14
|
+
function isTreeNode(x) {
|
|
15
|
+
if (x === null || typeof x !== "object") return false;
|
|
16
|
+
return "type" in x && typeof x.type === "string" && "props" in x && "children" in x && Array.isArray(x.children);
|
|
17
|
+
}
|
|
18
|
+
function isReactElement(x) {
|
|
19
|
+
return x !== null && typeof x === "object" && "type" in x && "props" in x;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolve any element-like value (ReactElement, TreeNode, function component)
|
|
23
|
+
* into a TreeNode. Handles recursive function components up to depth 100.
|
|
24
|
+
*/
|
|
25
|
+
function resolveToTree(element, depth = 0) {
|
|
26
|
+
if (depth > 100) throw new Error("Component resolution depth exceeded");
|
|
27
|
+
if (element == null) return null;
|
|
28
|
+
if (isTreeNode(element)) return element;
|
|
29
|
+
if (!isReactElement(element)) return null;
|
|
30
|
+
if (typeof element.type === "function") {
|
|
31
|
+
const component = element.type;
|
|
32
|
+
return resolveToTree(component(element.props), depth + 1);
|
|
33
|
+
}
|
|
34
|
+
if (typeof element.type === "string") {
|
|
35
|
+
const children = [];
|
|
36
|
+
const elChildren = element.props.children;
|
|
37
|
+
if (elChildren != null) {
|
|
38
|
+
const childArray = Array.isArray(elChildren) ? elChildren : [elChildren];
|
|
39
|
+
for (const child of childArray.flat()) {
|
|
40
|
+
if (child == null) continue;
|
|
41
|
+
if (typeof child === "string" || typeof child === "number") children.push(String(child));
|
|
42
|
+
else {
|
|
43
|
+
const resolved = resolveToTree(child, depth + 1);
|
|
44
|
+
if (resolved) children.push(resolved);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
const { children: _, ...props } = element.props;
|
|
49
|
+
return {
|
|
50
|
+
type: element.type,
|
|
51
|
+
props,
|
|
52
|
+
children: inlineFragments(children)
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
function resolveChild(child) {
|
|
58
|
+
if (child == null) return null;
|
|
59
|
+
if (typeof child === "string" || typeof child === "number") return String(child);
|
|
60
|
+
return resolveToTree(child);
|
|
61
|
+
}
|
|
62
|
+
function node(type, props) {
|
|
63
|
+
const { children, ...rest } = props;
|
|
64
|
+
return {
|
|
65
|
+
type,
|
|
66
|
+
props: rest,
|
|
67
|
+
children: inlineFragments([children].flat(Infinity).map(resolveChild).filter((c) => c !== null))
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/behaviours/index.ts
|
|
72
|
+
const root = (kind) => ({
|
|
73
|
+
role: "root",
|
|
74
|
+
kind
|
|
75
|
+
});
|
|
76
|
+
const part = (id) => ({
|
|
77
|
+
role: "part",
|
|
78
|
+
part: id
|
|
79
|
+
});
|
|
80
|
+
const repeat = (container) => ({
|
|
81
|
+
role: "repeat",
|
|
82
|
+
container
|
|
83
|
+
});
|
|
84
|
+
const input = (valueId) => ({
|
|
85
|
+
role: "input",
|
|
86
|
+
valueId
|
|
87
|
+
});
|
|
88
|
+
/** The same elements, looked up by any element name, as parsing and export do. */
|
|
89
|
+
const REKA_ELEMENTS = {
|
|
90
|
+
Button: { Root: root("button") },
|
|
91
|
+
Toggle: { Root: root("toggle") },
|
|
92
|
+
Switch: {
|
|
93
|
+
Root: root("switch"),
|
|
94
|
+
Thumb: part("thumb")
|
|
95
|
+
},
|
|
96
|
+
Checkbox: {
|
|
97
|
+
Root: root("checkbox"),
|
|
98
|
+
Indicator: part("indicator")
|
|
99
|
+
},
|
|
100
|
+
RadioGroup: {
|
|
101
|
+
Root: root("radioGroup"),
|
|
102
|
+
Item: root("radio"),
|
|
103
|
+
Indicator: part("indicator")
|
|
104
|
+
},
|
|
105
|
+
ToggleGroup: {
|
|
106
|
+
Root: root("toggleGroup"),
|
|
107
|
+
Item: root("toggle")
|
|
108
|
+
},
|
|
109
|
+
Slider: {
|
|
110
|
+
Root: root("slider"),
|
|
111
|
+
Track: part("track"),
|
|
112
|
+
Range: part("range"),
|
|
113
|
+
Thumb: part("thumb")
|
|
114
|
+
},
|
|
115
|
+
Progress: {
|
|
116
|
+
Root: root("progress"),
|
|
117
|
+
Track: part("track"),
|
|
118
|
+
Indicator: part("indicator")
|
|
119
|
+
},
|
|
120
|
+
Tabs: {
|
|
121
|
+
Root: root("tabs"),
|
|
122
|
+
List: part("list"),
|
|
123
|
+
Trigger: repeat("list"),
|
|
124
|
+
Content: repeat("panels")
|
|
125
|
+
},
|
|
126
|
+
Collapsible: {
|
|
127
|
+
Root: root("collapsible"),
|
|
128
|
+
Trigger: part("trigger"),
|
|
129
|
+
Content: part("content")
|
|
130
|
+
},
|
|
131
|
+
Accordion: {
|
|
132
|
+
Root: root("accordion"),
|
|
133
|
+
Item: root("collapsible"),
|
|
134
|
+
Header: { role: "frame" },
|
|
135
|
+
Trigger: part("trigger"),
|
|
136
|
+
Content: part("content")
|
|
137
|
+
},
|
|
138
|
+
NumberField: {
|
|
139
|
+
Root: root("numberField"),
|
|
140
|
+
Input: input("text"),
|
|
141
|
+
Increment: part("increment"),
|
|
142
|
+
Decrement: part("decrement")
|
|
143
|
+
},
|
|
144
|
+
TextField: {
|
|
145
|
+
Root: root("textField"),
|
|
146
|
+
Input: input("value")
|
|
147
|
+
},
|
|
148
|
+
Textarea: {
|
|
149
|
+
Root: root("textarea"),
|
|
150
|
+
Input: input("value")
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
/** The container a repeated part goes in when written directly under its root. */
|
|
154
|
+
const CONTAINER_NAMES = {
|
|
155
|
+
list: "List",
|
|
156
|
+
panels: "Panels",
|
|
157
|
+
items: "Items"
|
|
158
|
+
};
|
|
159
|
+
/** The role of a Reka element type such as `Switch.Thumb`, or undefined for other elements. */
|
|
160
|
+
function rekaRole(type) {
|
|
161
|
+
const dot = type.indexOf(".");
|
|
162
|
+
if (dot === -1) return void 0;
|
|
163
|
+
return REKA_ELEMENTS[type.slice(0, dot)]?.[type.slice(dot + 1)];
|
|
164
|
+
}
|
|
165
|
+
/** The element type for a part container that JSX leaves implicit. */
|
|
166
|
+
const containerType = (container) => `reka-container:${container}`;
|
|
167
|
+
function containerPart(type) {
|
|
168
|
+
return type.startsWith("reka-container:") ? type.slice(15) : void 0;
|
|
169
|
+
}
|
|
170
|
+
/** Whether an element names an existing component, as an item of a group does. */
|
|
171
|
+
function namesComponent(props) {
|
|
172
|
+
return props.of !== void 0 || props.component !== void 0 || props.componentId !== void 0;
|
|
173
|
+
}
|
|
174
|
+
const GROUPS = /* @__PURE__ */ new Set([
|
|
175
|
+
"radioGroup",
|
|
176
|
+
"toggleGroup",
|
|
177
|
+
"accordion"
|
|
178
|
+
]);
|
|
179
|
+
/** The container slot a child written directly under a root belongs in, if any. */
|
|
180
|
+
function containerOf(child, kind) {
|
|
181
|
+
const role = rekaRole(child.type);
|
|
182
|
+
if (role?.role === "repeat") return role.container;
|
|
183
|
+
if (GROUPS.has(kind) && role?.role === "root" && namesComponent(child.props)) return "items";
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* A root's children with repeated parts and group items that are written directly under it
|
|
187
|
+
* wrapped in their container slot, as the behaviour binds them: tab panels in Panels, a group's
|
|
188
|
+
* items in Items. Containers the JSX writes itself are kept.
|
|
189
|
+
*/
|
|
190
|
+
function wrapRepeatedParts(tree, kind) {
|
|
191
|
+
const result = [];
|
|
192
|
+
let open = null;
|
|
193
|
+
for (const child of tree.children) {
|
|
194
|
+
const container = typeof child === "string" ? void 0 : containerOf(child, kind);
|
|
195
|
+
if (!container || typeof child === "string") {
|
|
196
|
+
open = null;
|
|
197
|
+
result.push(child);
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (open?.type !== containerType(container)) {
|
|
201
|
+
open = node(containerType(container), {
|
|
202
|
+
name: CONTAINER_NAMES[container] ?? container,
|
|
203
|
+
flex: kind === "toggleGroup" ? "row" : "col",
|
|
204
|
+
gap: kind === "toggleGroup" ? 4 : 8
|
|
205
|
+
});
|
|
206
|
+
result.push(open);
|
|
207
|
+
}
|
|
208
|
+
open.children.push(child);
|
|
209
|
+
}
|
|
210
|
+
return result;
|
|
211
|
+
}
|
|
212
|
+
function createScope(kind, ownerId) {
|
|
213
|
+
return {
|
|
214
|
+
kind,
|
|
215
|
+
ownerId,
|
|
216
|
+
slots: /* @__PURE__ */ new Map(),
|
|
217
|
+
texts: /* @__PURE__ */ new Map()
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
/** Make a rendered part frame its part's slot, with the id the part has in this component. */
|
|
221
|
+
function bindPart(graph, scope, frame, partId) {
|
|
222
|
+
const id = scope.slots.get(partId) ?? createComponentPropertyId();
|
|
223
|
+
if (!createSlotProperty(graph, frame.id, id)) throw new Error(`<${partId}> must be inside a main component`);
|
|
224
|
+
scope.slots.set(partId, id);
|
|
225
|
+
}
|
|
226
|
+
/** Make a rendered text layer show the value's text property, defined on the root. */
|
|
227
|
+
function bindInput(graph, scope, text, valueId, propertyName) {
|
|
228
|
+
let id = scope.texts.get(valueId);
|
|
229
|
+
if (!id) {
|
|
230
|
+
id = createComponentPropertyId();
|
|
231
|
+
scope.texts.set(valueId, id);
|
|
232
|
+
const owner = graph.getNode(scope.ownerId);
|
|
233
|
+
graph.updateNode(scope.ownerId, { componentPropertyDefinitions: [...owner?.componentPropertyDefinitions ?? [], {
|
|
234
|
+
id,
|
|
235
|
+
name: propertyName,
|
|
236
|
+
type: "TEXT",
|
|
237
|
+
defaultValue: text.text
|
|
238
|
+
}] });
|
|
239
|
+
}
|
|
240
|
+
graph.updateNode(text.id, { componentPropertyReferences: [...text.componentPropertyReferences, {
|
|
241
|
+
propertyId: id,
|
|
242
|
+
field: "TEXT"
|
|
243
|
+
}] });
|
|
244
|
+
}
|
|
245
|
+
/** Root props that describe the behaviour rather than the drawing. */
|
|
246
|
+
const BEHAVIOUR_PROPS = [
|
|
247
|
+
"modelValue",
|
|
248
|
+
"open",
|
|
249
|
+
"disabled",
|
|
250
|
+
"filled",
|
|
251
|
+
"states",
|
|
252
|
+
"min",
|
|
253
|
+
"max",
|
|
254
|
+
"step",
|
|
255
|
+
"defaultValue"
|
|
256
|
+
];
|
|
257
|
+
function isBinding(value) {
|
|
258
|
+
return typeof value === "string" || typeof value === "object" && value !== null && "property" in value && typeof value.property === "string";
|
|
259
|
+
}
|
|
260
|
+
/** The spec a root's props give: values by property name, a range, and states. */
|
|
261
|
+
function rootSpec(kind, props) {
|
|
262
|
+
const values = {};
|
|
263
|
+
const main = kind === "collapsible" ? props.open : props.modelValue;
|
|
264
|
+
if (isBinding(main)) values[kind === "collapsible" ? "open" : "value"] = main;
|
|
265
|
+
if (isBinding(props.disabled)) values.disabled = props.disabled;
|
|
266
|
+
if (isBinding(props.filled)) values.filled = props.filled;
|
|
267
|
+
const range = Object.fromEntries([
|
|
268
|
+
"min",
|
|
269
|
+
"max",
|
|
270
|
+
"step"
|
|
271
|
+
].flatMap((key) => typeof props[key] === "number" ? [[key, props[key]]] : []));
|
|
272
|
+
if (typeof props.defaultValue === "number") range.default = props.defaultValue;
|
|
273
|
+
const spec = { kind };
|
|
274
|
+
if (Object.keys(values).length) spec.values = values;
|
|
275
|
+
if (Object.keys(range).length) spec.numbers = { value: range };
|
|
276
|
+
if (typeof props.states === "string") spec.states = props.states;
|
|
277
|
+
return spec;
|
|
278
|
+
}
|
|
279
|
+
/** Store the behaviour a rendered root describes, with the slots and texts its parts made. */
|
|
280
|
+
function finishRoot(graph, scope, props) {
|
|
281
|
+
const owner = graph.getNode(scope.ownerId);
|
|
282
|
+
if (!owner) return;
|
|
283
|
+
const behaviour = behaviourFromSpec(graph, owner, rootSpec(scope.kind, props));
|
|
284
|
+
behaviour.parts = {
|
|
285
|
+
...behaviour.parts,
|
|
286
|
+
...Object.fromEntries(scope.slots)
|
|
287
|
+
};
|
|
288
|
+
behaviour.texts = {
|
|
289
|
+
...behaviour.texts,
|
|
290
|
+
...Object.fromEntries([...scope.texts].map(([valueId, id]) => [valueId, { propertyId: id }]))
|
|
291
|
+
};
|
|
292
|
+
graph.updateNode(owner.id, { pluginData: withBehaviour(owner, behaviour) });
|
|
293
|
+
}
|
|
294
|
+
//#endregion
|
|
295
|
+
//#region src/behaviours/elements.ts
|
|
296
|
+
/** One Reka namespace as element functions, such as `Switch.Root` and `Switch.Thumb`. */
|
|
297
|
+
function namespace(name) {
|
|
298
|
+
const element = (part) => (props = {}, ...children) => node(`${name}.${part}`, children.length > 0 ? {
|
|
299
|
+
...props,
|
|
300
|
+
children
|
|
301
|
+
} : props);
|
|
302
|
+
return Object.fromEntries(Object.keys(REKA_ELEMENTS[name]).map((part) => [part, element(part)]));
|
|
303
|
+
}
|
|
304
|
+
const Button = namespace("Button");
|
|
305
|
+
const Toggle = namespace("Toggle");
|
|
306
|
+
const Switch = namespace("Switch");
|
|
307
|
+
const Checkbox = namespace("Checkbox");
|
|
308
|
+
const RadioGroup = namespace("RadioGroup");
|
|
309
|
+
const ToggleGroup = namespace("ToggleGroup");
|
|
310
|
+
const Slider = namespace("Slider");
|
|
311
|
+
const Progress = namespace("Progress");
|
|
312
|
+
const Tabs = namespace("Tabs");
|
|
313
|
+
const Collapsible = namespace("Collapsible");
|
|
314
|
+
const Accordion = namespace("Accordion");
|
|
315
|
+
const NumberField = namespace("NumberField");
|
|
316
|
+
const TextField = namespace("TextField");
|
|
317
|
+
const Textarea = namespace("Textarea");
|
|
318
|
+
//#endregion
|
|
319
|
+
export { Accordion, BEHAVIOUR_PROPS, Button, Checkbox, Collapsible, NumberField, Progress, REKA_ELEMENTS, RadioGroup, Slider, Switch, Tabs, TextField, Textarea, Toggle, ToggleGroup, bindInput, bindPart, containerPart, createScope, finishRoot, isTreeNode, namesComponent, node, plainTextOf, rekaRole, resolveToTree, wrapRepeatedParts };
|
|
320
|
+
|
|
321
|
+
//# sourceMappingURL=elements.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"elements.js","names":[],"sources":["../../../src/tree.ts","../../../src/behaviours/index.ts","../../../src/behaviours/elements.ts"],"sourcesContent":["import type { Effect, Fill } from '@pairdraw/scene-graph'\nimport type { Color } from '@pairdraw/scene-graph/primitives'\n\nimport type { ComponentProperty } from './component-properties'\nimport type { DesignMode, DesignVariable } from './variables'\n\nexport interface TreeNode {\n type: string\n props: Record<string, unknown>\n children: (TreeNode | string)[]\n /** Where the element was written, when the source is known. */\n source?: { line: number }\n}\n\n/** A fragment (`<>…</>`) groups siblings without creating a node of its own. */\nexport const FRAGMENT = ''\n\nfunction inlineFragments(children: (TreeNode | string)[]): (TreeNode | string)[] {\n return children.flatMap((child) =>\n typeof child !== 'string' && child.type === FRAGMENT ? child.children : [child]\n )\n}\n\n/**\n * The text an element holds as children, such as a sticky note's body or a connector's label.\n * Elements among its children are not text and are skipped.\n */\nexport function plainTextOf(tree: TreeNode): string {\n return tree.children.filter((child): child is string => typeof child === 'string').join('')\n}\n\nexport function isTreeNode(x: unknown): x is TreeNode {\n if (x === null || typeof x !== 'object') return false\n return (\n 'type' in x &&\n typeof x.type === 'string' &&\n 'props' in x &&\n 'children' in x &&\n Array.isArray(x.children)\n )\n}\n\ntype FunctionComponent = (props: Record<string, unknown>) => unknown\n\ninterface ReactElement {\n type: unknown\n props: Record<string, unknown>\n}\n\nfunction isReactElement(x: unknown): x is ReactElement {\n return x !== null && typeof x === 'object' && 'type' in x && 'props' in x\n}\n\n/**\n * Resolve any element-like value (ReactElement, TreeNode, function component)\n * into a TreeNode. Handles recursive function components up to depth 100.\n */\nexport function resolveToTree(element: unknown, depth = 0): TreeNode | null {\n if (depth > 100) throw new Error('Component resolution depth exceeded')\n if (element == null) return null\n if (isTreeNode(element)) return element\n if (!isReactElement(element)) return null\n\n if (typeof element.type === 'function') {\n const component = element.type as FunctionComponent\n return resolveToTree(component(element.props), depth + 1)\n }\n\n if (typeof element.type === 'string') {\n const children: (TreeNode | string)[] = []\n const elChildren = element.props.children\n if (elChildren != null) {\n const childArray = Array.isArray(elChildren) ? elChildren : [elChildren]\n for (const child of childArray.flat()) {\n if (child == null) continue\n if (typeof child === 'string' || typeof child === 'number') {\n children.push(String(child))\n } else {\n const resolved = resolveToTree(child, depth + 1)\n if (resolved) children.push(resolved)\n }\n }\n }\n const { children: _, ...props } = element.props\n return { type: element.type, props, children: inlineFragments(children) }\n }\n\n return null\n}\n\nfunction resolveChild(child: unknown): TreeNode | string | null {\n if (child == null) return null\n if (typeof child === 'string' || typeof child === 'number') return String(child)\n return resolveToTree(child)\n}\n\nexport function node(\n type: string,\n props: { children?: unknown; [key: string]: unknown }\n): TreeNode {\n const { children, ...rest } = props\n const processed = [children]\n .flat(Infinity)\n .map(resolveChild)\n .filter((c): c is TreeNode | string => c !== null)\n return { type, props: rest, children: inlineFragments(processed) }\n}\n\nexport type PaintProp = string | Color | Fill | DesignVariable\n\nexport type StyleProps = {\n flex?: 'row' | 'col' | 'column'\n flow?: 'auto' | 'ltr' | 'rtl'\n dir?: 'auto' | 'ltr' | 'rtl'\n gap?: number | DesignVariable\n wrap?: boolean\n rowGap?: number | DesignVariable\n columnGap?: number | DesignVariable\n justify?: 'start' | 'end' | 'center' | 'between' | 'around'\n justifyContent?: 'start' | 'end' | 'center' | 'between' | 'around'\n items?: 'start' | 'end' | 'center' | 'stretch' | 'baseline'\n align?: 'start' | 'end' | 'center' | 'stretch' | 'baseline'\n alignItems?: 'start' | 'end' | 'center' | 'stretch' | 'baseline'\n grow?: number\n\n w?: number | 'fill' | 'hug' | DesignVariable\n h?: number | 'fill' | 'hug' | DesignVariable\n minW?: number\n maxW?: number\n minH?: number\n maxH?: number\n\n x?: number\n y?: number\n\n p?: number | DesignVariable\n px?: number | DesignVariable\n py?: number | DesignVariable\n pt?: number | DesignVariable\n pr?: number | DesignVariable\n pb?: number | DesignVariable\n pl?: number | DesignVariable\n\n bg?: PaintProp\n fill?: PaintProp\n fills?: PaintProp[]\n stroke?: PaintProp\n strokeWidth?: number | DesignVariable\n strokeAlign?: 'inside' | 'outside' | 'center'\n strokeDash?: number[] | boolean\n dashPattern?: number[]\n rounded?: number | DesignVariable\n roundedTL?: number | DesignVariable\n roundedTR?: number | DesignVariable\n roundedBL?: number | DesignVariable\n roundedBR?: number | DesignVariable\n cornerSmoothing?: number\n opacity?: number | DesignVariable\n blendMode?: string\n mask?: boolean | 'alpha' | 'luminance' | 'vector'\n rotate?: number\n rotation?: number\n /** Reflects the layer across its vertical centre line, before it turns. */\n flipX?: boolean\n /** Reflects the layer across its horizontal centre line, before it turns. */\n flipY?: boolean\n overflow?: 'hidden' | 'visible'\n shadow?: string\n blur?: number\n effects?: Effect[]\n\n size?: number | DesignVariable\n fontSize?: number | DesignVariable\n /** Pixels, or a percent of the font size such as `'150%'`. */\n lineHeight?: number | `${number}%` | 'auto' | DesignVariable\n /** Pixels, or a percent of the font size such as `'5%'`. */\n letterSpacing?: number | `${number}%` | DesignVariable\n /** OpenType features on or off by tag, as CSS's `font-feature-settings`: `{ tnum: true }`. */\n fontFeatures?: Record<string, boolean>\n /** Variable-font axis values by tag, as CSS's `font-variation-settings`: `{ wght: 650 }`. */\n fontVariations?: Record<string, number>\n /** The language the text is in, as a BCP 47 tag such as `de` or `sr-Latn`. */\n lang?: string | null\n /**\n * Whether a variable font's optical size follows the font size (`auto`, the default) or takes\n * the `opsz` in `fontVariations` (`none`), as CSS's `font-optical-sizing`.\n */\n fontOpticalSizing?: 'auto' | 'none'\n font?: string\n fontFamily?: string\n weight?: number | 'bold' | 'medium' | 'normal'\n fontWeight?: number | 'bold' | 'medium' | 'normal'\n color?: PaintProp\n text?: string | ComponentProperty<string>\n characters?: string\n textAlign?: 'left' | 'center' | 'right' | 'justified'\n textAlignHorizontal?: 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED'\n textHorizontalAlignment?: 'LEFT' | 'CENTER' | 'RIGHT' | 'JUSTIFIED'\n textAlignVertical?: 'TOP' | 'CENTER' | 'BOTTOM'\n textVerticalAlignment?: 'TOP' | 'CENTER' | 'BOTTOM'\n textAutoResize?: 'none' | 'width' | 'height'\n}\n\ntype NodeProps = StyleProps & {\n name?: string\n key?: string | number\n children?: unknown\n bind?: Record<string, unknown>\n visible?: boolean | ComponentProperty<boolean>\n /** The modes the layer shows collections in, as the Appearance panel sets them. */\n mode?: DesignMode | DesignMode[]\n [key: string]: unknown\n}\n\nexport type BaseProps = NodeProps & { properties?: never }\n\n/** An `<Ellipse>`'s arc: angles in degrees, clockwise from three o'clock, and its hole. */\nexport type EllipseProps = {\n startAngle?: number\n endAngle?: number\n /** The hole as a share of the ellipse, from 0 (none) to 1. */\n innerRadius?: number\n}\n/** A `<Vector>`'s path, in SVG's syntax as `d` or as Figma's `vectorPaths`. */\nexport type VectorProps = {\n /** Path data as SVG writes it, filled under `fillRule`; an open subpath fills only without a stroke. */\n d?: string\n fillRule?: 'nonzero' | 'evenodd'\n /** Paths with their own winding rules, as export writes them; `NONE` draws an open path. */\n paths?: Array<{ windingRule: 'NONZERO' | 'EVENODD' | 'NONE'; data: string }>\n}\nexport type TextProps = BaseProps\n\n/** A `<Span>`'s props: the character styles of `<Text>`, under the same names. */\nexport type SpanProps = Pick<\n StyleProps,\n | 'size'\n | 'fontSize'\n | 'font'\n | 'fontFamily'\n | 'weight'\n | 'fontWeight'\n | 'color'\n | 'fill'\n | 'fills'\n | 'lineHeight'\n | 'letterSpacing'\n | 'fontFeatures'\n | 'fontVariations'\n | 'lang'\n> & {\n key?: string | number\n style?: Record<string, unknown>\n italic?: boolean\n textDecoration?: 'none' | 'underline' | 'strikethrough'\n textDecorationStyle?: 'solid' | 'dotted' | 'wavy'\n textDecorationColor?: string | Color | Fill\n textDecorationThickness?: number | 'auto'\n textUnderlineOffset?: number | 'auto'\n textDecorationSkipInk?: boolean\n children?: unknown\n}\n\n/** A `<Paragraph>`'s props: a list item at `level` 1–5, or a plain paragraph, and its spacing. */\nexport type ParagraphProps = {\n key?: string | number\n list?: 'ordered' | 'unordered'\n level?: number\n spacing?: number\n listSpacing?: number\n indent?: number\n children?: unknown\n}\n\nexport type ComponentProps = BaseProps\n\nexport type InstanceProps = NodeProps & {\n component?: string\n componentId?: string\n of?: string | ComponentProperty<string>\n /** Show the instance's own properties on instances of the component it is in, as Figma does. */\n exposed?: boolean\n /** Values by property ID or name, or an exposed nested instance's values by its name. */\n properties?: Record<string, string | boolean | number | Record<string, string | boolean | number>>\n}\n","import type { SceneGraph, SceneNode } from '@pairdraw/scene-graph'\nimport {\n behaviourFromSpec,\n withBehaviour,\n type BehaviourKind,\n type BehaviourSpec\n} from '@pairdraw/scene-graph/behaviours'\nimport { createComponentPropertyId } from '@pairdraw/scene-graph/components'\nimport { createSlotProperty } from '@pairdraw/scene-graph/slots'\n\nimport { node, type TreeNode } from '../tree'\n\n/** What a Reka UI element is to the component it builds. */\nexport type RekaRole =\n | { role: 'root'; kind: BehaviourKind }\n /** A Reka subcomponent drawn by a slot of the component, such as a switch's thumb. */\n | { role: 'part'; part: string }\n /** One of a slot's repeated parts, such as a tab trigger in the tab list. */\n | { role: 'repeat'; container: string }\n /** The text layer a text value shows, which becomes the field's text property. */\n | { role: 'input'; valueId: string }\n /** A Reka wrapper with no state of its own, drawn as a plain frame. */\n | { role: 'frame' }\n\nconst root = (kind: BehaviourKind): RekaRole => ({ role: 'root', kind })\nconst part = (id: string): RekaRole => ({ role: 'part', part: id })\nconst repeat = (container: string): RekaRole => ({ role: 'repeat', container })\nconst input = (valueId: string): RekaRole => ({ role: 'input', valueId })\n\n/**\n * Design JSX's Reka UI elements, by namespace and part, after Reka's own anatomy: `Switch.Root`\n * is a main component that behaves as a switch and `Switch.Thumb` its thumb slot. A group's\n * `Item` is the component of its items when written on its own, and an item when it names one\n * with `of`, as an `Instance` does.\n */\nconst REKA_PARTS = {\n Button: { Root: root('button') },\n Toggle: { Root: root('toggle') },\n Switch: { Root: root('switch'), Thumb: part('thumb') },\n Checkbox: { Root: root('checkbox'), Indicator: part('indicator') },\n RadioGroup: { Root: root('radioGroup'), Item: root('radio'), Indicator: part('indicator') },\n ToggleGroup: { Root: root('toggleGroup'), Item: root('toggle') },\n Slider: {\n Root: root('slider'),\n Track: part('track'),\n Range: part('range'),\n Thumb: part('thumb')\n },\n Progress: { Root: root('progress'), Track: part('track'), Indicator: part('indicator') },\n Tabs: {\n Root: root('tabs'),\n List: part('list'),\n Trigger: repeat('list'),\n Content: repeat('panels')\n },\n Collapsible: { Root: root('collapsible'), Trigger: part('trigger'), Content: part('content') },\n Accordion: {\n Root: root('accordion'),\n Item: root('collapsible'),\n Header: { role: 'frame' },\n Trigger: part('trigger'),\n Content: part('content')\n },\n NumberField: {\n Root: root('numberField'),\n Input: input('text'),\n Increment: part('increment'),\n Decrement: part('decrement')\n },\n TextField: { Root: root('textField'), Input: input('value') },\n Textarea: { Root: root('textarea'), Input: input('value') }\n} as const satisfies Readonly<Record<string, Readonly<Record<string, RekaRole>>>>\n\n/** Each Reka namespace and its parts, as typed elements are built from them. */\nexport type RekaNamespaces = typeof REKA_PARTS\n\n/** The same elements, looked up by any element name, as parsing and export do. */\nexport const REKA_ELEMENTS: Readonly<Record<string, Readonly<Record<string, RekaRole>>>> =\n REKA_PARTS\n\n/** The container a repeated part goes in when written directly under its root. */\nconst CONTAINER_NAMES: Record<string, string> = { list: 'List', panels: 'Panels', items: 'Items' }\n\n/** The role of a Reka element type such as `Switch.Thumb`, or undefined for other elements. */\nexport function rekaRole(type: string): RekaRole | undefined {\n const dot = type.indexOf('.')\n if (dot === -1) return undefined\n const namespace = REKA_ELEMENTS[type.slice(0, dot)] as Record<string, RekaRole> | undefined\n return namespace?.[type.slice(dot + 1)]\n}\n\n/** The element type for a part container that JSX leaves implicit. */\nconst containerType = (container: string) => `reka-container:${container}`\n\nexport function containerPart(type: string): string | undefined {\n return type.startsWith('reka-container:') ? type.slice('reka-container:'.length) : undefined\n}\n\n/** Whether an element names an existing component, as an item of a group does. */\nexport function namesComponent(props: Record<string, unknown>): boolean {\n return props.of !== undefined || props.component !== undefined || props.componentId !== undefined\n}\n\nconst GROUPS = new Set<BehaviourKind>(['radioGroup', 'toggleGroup', 'accordion'])\n\n/** The container slot a child written directly under a root belongs in, if any. */\nfunction containerOf(child: TreeNode, kind: BehaviourKind): string | undefined {\n const role = rekaRole(child.type)\n if (role?.role === 'repeat') return role.container\n if (GROUPS.has(kind) && role?.role === 'root' && namesComponent(child.props)) return 'items'\n return undefined\n}\n\n/**\n * A root's children with repeated parts and group items that are written directly under it\n * wrapped in their container slot, as the behaviour binds them: tab panels in Panels, a group's\n * items in Items. Containers the JSX writes itself are kept.\n */\nexport function wrapRepeatedParts(tree: TreeNode, kind: BehaviourKind): TreeNode['children'] {\n const result: TreeNode['children'] = []\n let open: TreeNode | null = null\n for (const child of tree.children) {\n const container = typeof child === 'string' ? undefined : containerOf(child, kind)\n if (!container || typeof child === 'string') {\n open = null\n result.push(child)\n continue\n }\n if (open?.type !== containerType(container)) {\n open = node(containerType(container), {\n name: CONTAINER_NAMES[container] ?? container,\n flex: kind === 'toggleGroup' ? 'row' : 'col',\n gap: kind === 'toggleGroup' ? 4 : 8\n })\n result.push(open)\n }\n open.children.push(child)\n }\n return result\n}\n\n/** What a root collects from its parts while they render. */\nexport interface RekaScope {\n kind: BehaviourKind\n ownerId: string\n /** Slot property ids by part, shared by every variant of a set. */\n slots: Map<string, string>\n /** Text property ids by value, defined once on the root. */\n texts: Map<string, string>\n}\n\nexport function createScope(kind: BehaviourKind, ownerId: string): RekaScope {\n return { kind, ownerId, slots: new Map(), texts: new Map() }\n}\n\n/** Make a rendered part frame its part's slot, with the id the part has in this component. */\nexport function bindPart(graph: SceneGraph, scope: RekaScope, frame: SceneNode, partId: string) {\n const id = scope.slots.get(partId) ?? createComponentPropertyId()\n if (!createSlotProperty(graph, frame.id, id))\n throw new Error(`<${partId}> must be inside a main component`)\n scope.slots.set(partId, id)\n}\n\n/** Make a rendered text layer show the value's text property, defined on the root. */\nexport function bindInput(\n graph: SceneGraph,\n scope: RekaScope,\n text: SceneNode,\n valueId: string,\n propertyName: string\n) {\n let id = scope.texts.get(valueId)\n if (!id) {\n id = createComponentPropertyId()\n scope.texts.set(valueId, id)\n const owner = graph.getNode(scope.ownerId)\n graph.updateNode(scope.ownerId, {\n componentPropertyDefinitions: [\n ...(owner?.componentPropertyDefinitions ?? []),\n { id, name: propertyName, type: 'TEXT', defaultValue: text.text }\n ]\n })\n }\n graph.updateNode(text.id, {\n componentPropertyReferences: [\n ...text.componentPropertyReferences,\n { propertyId: id, field: 'TEXT' }\n ]\n })\n}\n\n/** Root props that describe the behaviour rather than the drawing. */\nexport const BEHAVIOUR_PROPS = [\n 'modelValue',\n 'open',\n 'disabled',\n 'filled',\n 'states',\n 'min',\n 'max',\n 'step',\n 'defaultValue'\n] as const\n\n/** A value bound to a component property by name; a variant property may name its on and off. */\nexport type BindingProp = string | { property: string; on?: string; off?: string }\n\nfunction isBinding(value: unknown): value is BindingProp {\n return (\n typeof value === 'string' ||\n (typeof value === 'object' &&\n value !== null &&\n 'property' in value &&\n typeof value.property === 'string')\n )\n}\n\n/** The spec a root's props give: values by property name, a range, and states. */\nfunction rootSpec(kind: BehaviourKind, props: Record<string, unknown>): BehaviourSpec {\n const values: NonNullable<BehaviourSpec['values']> = {}\n const main = kind === 'collapsible' ? props.open : props.modelValue\n if (isBinding(main)) values[kind === 'collapsible' ? 'open' : 'value'] = main\n if (isBinding(props.disabled)) values.disabled = props.disabled\n if (isBinding(props.filled)) values.filled = props.filled\n const range = Object.fromEntries(\n (['min', 'max', 'step'] as const).flatMap((key) =>\n typeof props[key] === 'number' ? [[key, props[key]]] : []\n )\n )\n if (typeof props.defaultValue === 'number') range.default = props.defaultValue\n const spec: BehaviourSpec = { kind }\n if (Object.keys(values).length) spec.values = values\n if (Object.keys(range).length) spec.numbers = { value: range }\n if (typeof props.states === 'string') spec.states = props.states\n return spec\n}\n\n/** Store the behaviour a rendered root describes, with the slots and texts its parts made. */\nexport function finishRoot(\n graph: SceneGraph,\n scope: RekaScope,\n props: Record<string, unknown>\n): void {\n const owner = graph.getNode(scope.ownerId)\n if (!owner) return\n const behaviour = behaviourFromSpec(graph, owner, rootSpec(scope.kind, props))\n behaviour.parts = { ...behaviour.parts, ...Object.fromEntries(scope.slots) }\n behaviour.texts = {\n ...behaviour.texts,\n ...Object.fromEntries([...scope.texts].map(([valueId, id]) => [valueId, { propertyId: id }]))\n }\n graph.updateNode(owner.id, { pluginData: withBehaviour(owner, behaviour) })\n}\n","import { node, type BaseProps, type TreeNode } from '../tree'\nimport { REKA_ELEMENTS, type BindingProp, type RekaNamespaces } from './index'\n\n/** Props of a Reka element: a frame's, plus the behaviour a root binds and a group item's component. */\nexport interface RekaProps extends BaseProps {\n modelValue?: BindingProp\n open?: BindingProp\n disabled?: BindingProp\n filled?: BindingProp\n /** The variant property whose values draw rest, hover, pressed, focus, and disabled. */\n states?: string\n min?: number\n max?: number\n step?: number\n defaultValue?: number\n /** For a group's item: the component or set it is an instance of. */\n of?: string\n}\n\ntype Child = TreeNode | string\nexport type RekaElement = (props?: RekaProps, ...children: Child[]) => TreeNode\n\n/** One Reka namespace as element functions, such as `Switch.Root` and `Switch.Thumb`. */\nfunction namespace<N extends keyof RekaNamespaces>(name: N) {\n const element =\n (part: string): RekaElement =>\n (props = {}, ...children) =>\n node(`${name}.${part}`, children.length > 0 ? { ...props, children } : props)\n return Object.fromEntries(\n Object.keys(REKA_ELEMENTS[name]).map((part) => [part, element(part)])\n ) as { [P in keyof RekaNamespaces[N]]: RekaElement }\n}\n\nexport const Button = namespace('Button')\nexport const Toggle = namespace('Toggle')\nexport const Switch = namespace('Switch')\nexport const Checkbox = namespace('Checkbox')\nexport const RadioGroup = namespace('RadioGroup')\nexport const ToggleGroup = namespace('ToggleGroup')\nexport const Slider = namespace('Slider')\nexport const Progress = namespace('Progress')\nexport const Tabs = namespace('Tabs')\nexport const Collapsible = namespace('Collapsible')\nexport const Accordion = namespace('Accordion')\nexport const NumberField = namespace('NumberField')\nexport const TextField = namespace('TextField')\nexport const Textarea = namespace('Textarea')\n"],"mappings":";;;AAiBA,SAAS,gBAAgB,UAAwD;CAC/E,OAAO,SAAS,SAAS,UACvB,OAAO,UAAU,YAAY,MAAM,SAAA,KAAoB,MAAM,WAAW,CAAC,KAAK,CAChF;AACF;;;;;AAMA,SAAgB,YAAY,MAAwB;CAClD,OAAO,KAAK,SAAS,QAAQ,UAA2B,OAAO,UAAU,QAAQ,CAAC,CAAC,KAAK,EAAE;AAC5F;AAEA,SAAgB,WAAW,GAA2B;CACpD,IAAI,MAAM,QAAQ,OAAO,MAAM,UAAU,OAAO;CAChD,OACE,UAAU,KACV,OAAO,EAAE,SAAS,YAClB,WAAW,KACX,cAAc,KACd,MAAM,QAAQ,EAAE,QAAQ;AAE5B;AASA,SAAS,eAAe,GAA+B;CACrD,OAAO,MAAM,QAAQ,OAAO,MAAM,YAAY,UAAU,KAAK,WAAW;AAC1E;;;;;AAMA,SAAgB,cAAc,SAAkB,QAAQ,GAAoB;CAC1E,IAAI,QAAQ,KAAK,MAAM,IAAI,MAAM,qCAAqC;CACtE,IA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