@pairdraw/dom-css 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 +219 -2
- package/dist/browser.d.ts +29 -0
- package/dist/browser.js +2 -0
- package/dist/chunks/behaviours/args.js +59 -0
- package/dist/chunks/behaviours/args.js.map +1 -0
- package/dist/chunks/behaviours/controls.js +109 -0
- package/dist/chunks/behaviours/controls.js.map +1 -0
- package/dist/chunks/behaviours/reset.js +40 -0
- package/dist/chunks/behaviours/reset.js.map +1 -0
- package/dist/chunks/behaviours/roles.js +51 -0
- package/dist/chunks/behaviours/roles.js.map +1 -0
- package/dist/chunks/behaviours/states/conditions.js +103 -0
- package/dist/chunks/behaviours/states/conditions.js.map +1 -0
- package/dist/chunks/behaviours/states/css.js +172 -0
- package/dist/chunks/behaviours/states/css.js.map +1 -0
- package/dist/chunks/behaviours/states/layers.js +1736 -0
- package/dist/chunks/behaviours/states/layers.js.map +1 -0
- package/dist/chunks/behaviours/states/model.js +144 -0
- package/dist/chunks/behaviours/states/model.js.map +1 -0
- package/dist/chunks/behaviours/states/tailwind.js +198 -0
- package/dist/chunks/behaviours/states/tailwind.js.map +1 -0
- package/dist/chunks/browser.js +1352 -0
- package/dist/chunks/browser.js.map +1 -0
- package/dist/chunks/export/bundle.js +337 -0
- package/dist/chunks/export/bundle.js.map +1 -0
- package/dist/chunks/export/components/drawn.js +60 -0
- package/dist/chunks/export/components/drawn.js.map +1 -0
- package/dist/chunks/export/components/fields.js +178 -0
- package/dist/chunks/export/components/fields.js.map +1 -0
- package/dist/chunks/export/components/groups.js +297 -0
- package/dist/chunks/export/components/groups.js.map +1 -0
- package/dist/chunks/export/components/icons.js +260 -0
- package/dist/chunks/export/components/icons.js.map +1 -0
- package/dist/chunks/export/components/layers.js +1599 -0
- package/dist/chunks/export/components/layers.js.map +1 -0
- package/dist/chunks/export/index.js +477 -0
- package/dist/chunks/export/index.js.map +1 -0
- package/dist/chunks/export/storybook/component.js +448 -0
- package/dist/chunks/export/storybook/component.js.map +1 -0
- package/dist/chunks/export/storybook/export.js +410 -0
- package/dist/chunks/export/storybook/export.js.map +1 -0
- package/dist/chunks/import/html.js +406 -0
- package/dist/chunks/import/html.js.map +1 -0
- package/dist/chunks/import/jsx/dev-runtime.js +37 -0
- package/dist/chunks/import/jsx/dev-runtime.js.map +1 -0
- package/dist/chunks/tokens/cssom-validator.js +10 -0
- package/dist/chunks/tokens/cssom-validator.js.map +1 -0
- package/dist/export.d.ts +594 -0
- package/dist/export.js +13 -0
- package/dist/index.d.ts +115 -0
- package/dist/index.js +7 -0
- package/dist/jsx-dev-runtime.d.ts +8 -0
- package/dist/jsx-dev-runtime.js +3 -0
- package/dist/jsx-runtime.d.ts +20 -0
- package/dist/jsx-runtime.js +3 -0
- package/dist/rolldown-runtime.js +13 -0
- package/dist/runtime.d.ts +10 -0
- package/dist/scene-graph.d.ts +2 -0
- package/dist/scene-graph.js +2 -0
- package/dist/scene-graph2.d.ts +10 -0
- package/dist/styling.d.ts +93 -0
- package/dist/tailwind.d.ts +39 -0
- package/dist/types.d.ts +89 -0
- package/package.json +88 -4
- package/src/behaviours/args.ts +85 -0
- package/src/behaviours/controls.ts +195 -0
- package/src/behaviours/index.ts +11 -0
- package/src/behaviours/reset.ts +37 -0
- package/src/behaviours/roles.ts +44 -0
- package/src/behaviours/states/conditions.ts +105 -0
- package/src/behaviours/states/css.ts +123 -0
- package/src/behaviours/states/index.ts +4 -0
- package/src/behaviours/states/layers.ts +205 -0
- package/src/behaviours/states/model.ts +175 -0
- package/src/behaviours/states/names.ts +39 -0
- package/src/behaviours/states/tailwind.ts +148 -0
- package/src/behaviours/states/types.ts +56 -0
- package/src/browser.ts +146 -0
- package/src/cssom.d.ts +38 -0
- package/src/export/bundle.ts +461 -0
- package/src/export/components/drawn.ts +73 -0
- package/src/export/components/fields.ts +178 -0
- package/src/export/components/groups.ts +78 -0
- package/src/export/components/icons.ts +230 -0
- package/src/export/components/layers.ts +121 -0
- package/src/export/components/model.ts +544 -0
- package/src/export/components/properties.ts +167 -0
- package/src/export/components/react/fields.ts +129 -0
- package/src/export/components/react/index.ts +412 -0
- package/src/export/components/react/props.ts +151 -0
- package/src/export/components/react/radix.ts +52 -0
- package/src/export/components/react/shared.ts +76 -0
- package/src/export/components/references.ts +212 -0
- package/src/export/components/repeats.ts +128 -0
- package/src/export/components/shaders.ts +125 -0
- package/src/export/components/styling.ts +31 -0
- package/src/export/components/tree.ts +72 -0
- package/src/export/components/vue.ts +517 -0
- package/src/export/css.ts +57 -0
- package/src/export/grid.ts +42 -0
- package/src/export/html.ts +163 -0
- package/src/export/index.ts +44 -0
- package/src/export/projection.ts +581 -0
- package/src/export/storybook/component.ts +334 -0
- package/src/export/storybook/export.ts +430 -0
- package/src/export/storybook/groups.ts +161 -0
- package/src/export/storybook/module.ts +319 -0
- package/src/export/storybook/names.ts +51 -0
- package/src/export/tailwind-jsx.ts +156 -0
- package/src/export/text.ts +415 -0
- package/src/import/css-text.ts +4 -0
- package/src/import/css-values.ts +31 -0
- package/src/import/flow.ts +248 -0
- package/src/import/html.ts +88 -0
- package/src/import/jsx/core.ts +130 -0
- package/src/import/jsx/dev-runtime.ts +8 -0
- package/src/import/jsx/runtime.ts +76 -0
- package/src/import/scene-graph.ts +582 -0
- package/src/import/style-attribute.ts +25 -0
- package/src/import/tailwind.ts +54 -0
- package/src/import/text.ts +518 -0
- package/src/index.ts +51 -0
- package/src/runtime/browser/runtime.ts +301 -0
- package/src/runtime/headless/runtime.ts +99 -0
- package/src/runtime/headless/styles.ts +330 -0
- package/src/runtime/index.ts +11 -0
- package/src/tokens/conditions.ts +66 -0
- package/src/tokens/cssom-validator.ts +8 -0
- package/src/tokens/custom-properties.ts +331 -0
- package/src/tokens/expression.ts +128 -0
- package/src/tokens/formula.ts +140 -0
- package/src/tokens/index.ts +43 -0
- package/src/tokens/names.ts +161 -0
- package/src/tokens/references.ts +201 -0
- package/src/tokens/stylesheet.ts +304 -0
- package/src/tokens/validate.ts +86 -0
- package/src/tokens/values.ts +44 -0
- package/src/types.ts +59 -0
|
@@ -0,0 +1,1599 @@
|
|
|
1
|
+
import { allElements, isPositioned } from "../../behaviours/states/layers.js";
|
|
2
|
+
import { serializeHTML, serializedElements, stateStylesToTailwind, stateTailwindClasses } from "../../behaviours/states/tailwind.js";
|
|
3
|
+
import { extractImageAssets } from "../bundle.js";
|
|
4
|
+
import { behaviourArgs } from "../../behaviours/args.js";
|
|
5
|
+
import { BUTTON_RESET, HEADING_RESET } from "../../behaviours/reset.js";
|
|
6
|
+
import { ownerVariants, restLayerOf, stateStyles } from "../../behaviours/states/model.js";
|
|
7
|
+
import { claimName, identifierName, layerClassNames, propAttribute, stateStylesToCSS } from "../../behaviours/states/css.js";
|
|
8
|
+
import { blockRoot, freePlacedParts, inputLayers, inputValue, numberInput, progressWidth, rangeModel, textModel } from "./fields.js";
|
|
9
|
+
import { GROUP_ITEMS, activeTriggers, groupItems, isGroup, referencedLayers, tabParts } from "./groups.js";
|
|
10
|
+
import { drawingImports, iconDrawing, iconImportDeclarations, iconNamer } from "./icons.js";
|
|
11
|
+
import { isShaderPresetPaint, shaderOfPaint } from "@pairdraw/scene-graph/shaders";
|
|
12
|
+
import { compact, uniq, zip } from "es-toolkit/array";
|
|
13
|
+
import { omit } from "es-toolkit/object";
|
|
14
|
+
import { behaviourContract, behaviourProperties, findLayerByPath, layerPath, readBehaviour, textBinding } from "@pairdraw/scene-graph/behaviours";
|
|
15
|
+
import { slotPropertyId } from "@pairdraw/scene-graph/slots";
|
|
16
|
+
import { variantDefaultValue } from "@pairdraw/scene-graph/components";
|
|
17
|
+
import { camelCase, kebabCase, upperFirst } from "es-toolkit/string";
|
|
18
|
+
import { es, jsx, vue } from "@pairdraw/emit";
|
|
19
|
+
import dedent from "dedent";
|
|
20
|
+
//#region src/export/components/properties.ts
|
|
21
|
+
/**
|
|
22
|
+
* The layers bound to a property through `field`, each with the prop it draws. A layer is
|
|
23
|
+
* bound when it is in any variant, found by the path every variant shares.
|
|
24
|
+
*/
|
|
25
|
+
function boundLayers(graph, set, elements, field, props) {
|
|
26
|
+
const variants = ownerVariants(graph, set);
|
|
27
|
+
const bound = /* @__PURE__ */ new Map();
|
|
28
|
+
for (const element of elements) {
|
|
29
|
+
const path = element.key.split("\0")[0] ?? "";
|
|
30
|
+
for (const variant of variants) {
|
|
31
|
+
const reference = findLayerByPath(graph, variant.id, path)?.componentPropertyReferences.find((item) => item.field === field);
|
|
32
|
+
const prop = reference && props.get(reference.propertyId);
|
|
33
|
+
if (prop) {
|
|
34
|
+
bound.set(element, prop);
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return bound;
|
|
40
|
+
}
|
|
41
|
+
const propName = (property, taken, fallback) => claimName(camelCase(identifierName(property, fallback)), taken);
|
|
42
|
+
/** The set's boolean properties as props, and the layers each shows while it is on. */
|
|
43
|
+
function booleanProps(graph, set, elements, taken) {
|
|
44
|
+
const booleans = behaviourProperties(graph, set).filter((definition) => definition.type === "BOOLEAN").map((definition) => ({
|
|
45
|
+
name: propName(definition.name, taken, "Shown"),
|
|
46
|
+
id: definition.id,
|
|
47
|
+
property: definition.name,
|
|
48
|
+
default: definition.defaultValue === "true"
|
|
49
|
+
}));
|
|
50
|
+
return {
|
|
51
|
+
booleans,
|
|
52
|
+
shownBy: boundLayers(graph, set, elements, "VISIBLE", new Map(booleans.map((prop) => [prop.id, prop.name])))
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The set's slot properties as props, and the frame each fills, except frames a behaviour
|
|
57
|
+
* draws its parts in, such as a switch's thumb, which stay its parts.
|
|
58
|
+
*/
|
|
59
|
+
function slotProps(graph, set, elements, taken, parts) {
|
|
60
|
+
const definitions = behaviourProperties(graph, set).filter((definition) => definition.type === "SLOT");
|
|
61
|
+
const frames = elements.filter((element) => !parts.has(element));
|
|
62
|
+
const slots = definitions.map((definition) => ({
|
|
63
|
+
name: propName(definition.name, taken, "Content"),
|
|
64
|
+
id: definition.id,
|
|
65
|
+
property: definition.name
|
|
66
|
+
}));
|
|
67
|
+
const slotOf = boundLayers(graph, set, frames, "SLOT_CONTENT", new Map(slots.map((prop) => [prop.id, prop.name])));
|
|
68
|
+
const drawn = new Set(slotOf.values());
|
|
69
|
+
return {
|
|
70
|
+
slots: slots.filter((slot) => drawn.has(slot.name)),
|
|
71
|
+
slotOf
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* The set's text properties as props, and the layers bound to each. A layer is bound when it
|
|
76
|
+
* is in any variant, found by the path every variant shares.
|
|
77
|
+
*/
|
|
78
|
+
function textProps(graph, set, elements, taken, input) {
|
|
79
|
+
const definitions = behaviourProperties(graph, set).filter((item) => item.type === "TEXT" && item.id !== input);
|
|
80
|
+
const texts = definitions.map((definition) => ({
|
|
81
|
+
name: claimName(camelCase(identifierName(definition.name, "Text")), taken),
|
|
82
|
+
id: definition.id,
|
|
83
|
+
property: definition.name,
|
|
84
|
+
default: definition.defaultValue
|
|
85
|
+
}));
|
|
86
|
+
const byId = new Map(definitions.map((definition, i) => [definition.id, texts[i]]));
|
|
87
|
+
const variants = ownerVariants(graph, set);
|
|
88
|
+
const bound = /* @__PURE__ */ new Map();
|
|
89
|
+
const inputs = [];
|
|
90
|
+
for (const element of elements) {
|
|
91
|
+
const path = element.key.split("\0")[0] ?? "";
|
|
92
|
+
for (const variant of variants) {
|
|
93
|
+
const reference = findLayerByPath(graph, variant.id, path)?.componentPropertyReferences.find((item) => item.field === "TEXT");
|
|
94
|
+
if (reference && reference.propertyId === input) {
|
|
95
|
+
inputs.push(element);
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
const prop = reference && byId.get(reference.propertyId);
|
|
99
|
+
if (prop) {
|
|
100
|
+
bound.set(element, prop.name);
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
texts,
|
|
107
|
+
bound,
|
|
108
|
+
input: inputs
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//#endregion
|
|
112
|
+
//#region src/export/components/shaders.ts
|
|
113
|
+
function shaderLayer(node, className, preset) {
|
|
114
|
+
if (!preset) return void 0;
|
|
115
|
+
const position = node.base.position;
|
|
116
|
+
return {
|
|
117
|
+
preset,
|
|
118
|
+
className: `${className}-shader`,
|
|
119
|
+
positioned: isPositioned(position)
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* The shader each element's layer fills with in the rest variant: the topmost visible shader
|
|
124
|
+
* fill, which the component plays behind the layer's content.
|
|
125
|
+
*/
|
|
126
|
+
function shaderLayers(graph, restId, elements) {
|
|
127
|
+
const shaders = /* @__PURE__ */ new Map();
|
|
128
|
+
for (const element of elements) {
|
|
129
|
+
const layer = findLayerByPath(graph, restId, element.key.split("\0")[0] ?? "");
|
|
130
|
+
if (!layer) continue;
|
|
131
|
+
const shader = layer.fills.filter((fill) => fill.visible).map((fill) => shaderOfPaint(layer, fill)).findLast((found) => found !== null && isShaderPresetPaint(found));
|
|
132
|
+
if (shader) shaders.set(element, shader.preset);
|
|
133
|
+
}
|
|
134
|
+
return shaders;
|
|
135
|
+
}
|
|
136
|
+
/** Whether an effect's name can be imported, as every effect the library has can. */
|
|
137
|
+
const isEffectName = (type) => es.isIdentifierName(type);
|
|
138
|
+
/** The effects of `components` a generated component draws: those it can import by name. */
|
|
139
|
+
const drawnEffects = (components) => (components ?? []).filter((component) => isEffectName(component.type));
|
|
140
|
+
/**
|
|
141
|
+
* `import { Shader as ShaderCanvas, Aurora as ShaderAurora } from 'shaders/vue'`: the canvas and
|
|
142
|
+
* effects a component draws, under names apart from its own.
|
|
143
|
+
*/
|
|
144
|
+
function shaderImport(source, effects) {
|
|
145
|
+
return {
|
|
146
|
+
type: "ImportDeclaration",
|
|
147
|
+
importKind: "value",
|
|
148
|
+
specifiers: ["Shader", ...[...effects].sort()].map((name) => ({
|
|
149
|
+
type: "ImportSpecifier",
|
|
150
|
+
imported: es.identifier(name),
|
|
151
|
+
local: es.identifier(name === "Shader" ? SHADER_CANVAS : shaderEffectName(name))
|
|
152
|
+
})),
|
|
153
|
+
source: es.string(source)
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/** The local name a generated component imports effect `type` as, apart from its own names. */
|
|
157
|
+
const shaderEffectName = (type) => `Shader${type}`;
|
|
158
|
+
/** An effect's props as JSX attributes: strings as they are, anything else as JSON. */
|
|
159
|
+
function shaderEffectAttributes(component) {
|
|
160
|
+
return Object.entries(component.props ?? {}).map(([key, value]) => jsx.attribute(key, typeof value === "string" ? jsx.stringValue(value) : jsx.container(es.json(value))));
|
|
161
|
+
}
|
|
162
|
+
/** The local name of the library's `Shader` canvas. */
|
|
163
|
+
const SHADER_CANVAS = "ShaderCanvas";
|
|
164
|
+
/** The elements of a component's tree that fill with a shader. */
|
|
165
|
+
function shaderElements(node) {
|
|
166
|
+
if (node.type !== "element") return [];
|
|
167
|
+
return [...node.shader ? [node] : [], ...node.children.flatMap(shaderElements)];
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* The style that puts each shader behind its layer's content: the layer becomes its own
|
|
171
|
+
* stacking context, positioned if it is not already, and the shader fills it beneath
|
|
172
|
+
* everything it holds, clipped to its corners.
|
|
173
|
+
*/
|
|
174
|
+
function shaderCSS(tree) {
|
|
175
|
+
return shaderElements(tree).flatMap(({ className, shader }) => shader ? [`.${className} { isolation: isolate;${shader.positioned ? "" : " position: relative;"} }`, `.${shader.className} { position: absolute; inset: 0; z-index: -1; border-radius: inherit; overflow: hidden; pointer-events: none; }`] : []).join("\n");
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/export/components/tree.ts
|
|
179
|
+
function componentTree(node, labels, bindings) {
|
|
180
|
+
const repeated = labels.repeated.get(node);
|
|
181
|
+
const layerId = labels.layerOf(node);
|
|
182
|
+
const part = repeated?.part ?? labels.parts.get(node) ?? null;
|
|
183
|
+
const text = labels.texts.get(node);
|
|
184
|
+
const className = labels.classes.get(node) ?? "";
|
|
185
|
+
return {
|
|
186
|
+
type: "element",
|
|
187
|
+
part,
|
|
188
|
+
...repeated ? { value: repeated.value } : {},
|
|
189
|
+
tag: node.tagName,
|
|
190
|
+
className,
|
|
191
|
+
layerId,
|
|
192
|
+
attrs: node.attrs,
|
|
193
|
+
bindings: part === "root" ? bindings : [],
|
|
194
|
+
shownBy: labels.shownBy.get(node),
|
|
195
|
+
slot: labels.slotOf.get(node),
|
|
196
|
+
shader: shaderLayer(node, className, labels.shaders.get(node)),
|
|
197
|
+
children: text ? [{
|
|
198
|
+
type: "textProp",
|
|
199
|
+
name: text
|
|
200
|
+
}] : node.children.flatMap((child) => child.type === "text" ? [{
|
|
201
|
+
type: "text",
|
|
202
|
+
value: child.text
|
|
203
|
+
}] : childNode(child, labels))
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
function childNode(element, labels) {
|
|
207
|
+
const className = labels.classes.get(element) ?? "";
|
|
208
|
+
const layerId = labels.layerOf(element);
|
|
209
|
+
if (labels.input?.element === element) return [{
|
|
210
|
+
type: "input",
|
|
211
|
+
className,
|
|
212
|
+
layerId
|
|
213
|
+
}];
|
|
214
|
+
if (labels.input?.replaced.includes(element)) return [];
|
|
215
|
+
const used = labels.used.get(element);
|
|
216
|
+
if (!used) return [componentTree(element, labels, [])];
|
|
217
|
+
return [{
|
|
218
|
+
...used,
|
|
219
|
+
className,
|
|
220
|
+
layerId
|
|
221
|
+
}];
|
|
222
|
+
}
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/export/components/model.ts
|
|
225
|
+
/** Kinds generated as components so far; the rest keep static stories. */
|
|
226
|
+
const GENERATED_KINDS = [
|
|
227
|
+
"button",
|
|
228
|
+
"switch",
|
|
229
|
+
"checkbox",
|
|
230
|
+
"toggle",
|
|
231
|
+
"collapsible",
|
|
232
|
+
"tabs",
|
|
233
|
+
"radioGroup",
|
|
234
|
+
"toggleGroup",
|
|
235
|
+
"accordion",
|
|
236
|
+
"slider",
|
|
237
|
+
"progress",
|
|
238
|
+
"numberField",
|
|
239
|
+
"textField",
|
|
240
|
+
"textarea"
|
|
241
|
+
];
|
|
242
|
+
/** Parts each kind renders as a native button, which the reset clears for the design. */
|
|
243
|
+
const BUTTON_PARTS = {
|
|
244
|
+
button: ["root"],
|
|
245
|
+
switch: ["root"],
|
|
246
|
+
checkbox: ["root"],
|
|
247
|
+
toggle: ["root"],
|
|
248
|
+
collapsible: ["trigger"],
|
|
249
|
+
tabs: ["trigger"],
|
|
250
|
+
radioGroup: [],
|
|
251
|
+
toggleGroup: [],
|
|
252
|
+
accordion: [],
|
|
253
|
+
slider: [],
|
|
254
|
+
progress: [],
|
|
255
|
+
numberField: ["increment", "decrement"],
|
|
256
|
+
textField: [],
|
|
257
|
+
textarea: [],
|
|
258
|
+
plain: [],
|
|
259
|
+
radioGroupItem: ["root"],
|
|
260
|
+
toggleGroupItem: ["root"],
|
|
261
|
+
accordionItem: ["trigger"]
|
|
262
|
+
};
|
|
263
|
+
const isGenerated = (kind) => GENERATED_KINDS.includes(kind);
|
|
264
|
+
/** What an owner generates as: a group's item when it is one, its own kind, or nothing. */
|
|
265
|
+
function generatedKind(kind, itemOf) {
|
|
266
|
+
if (itemOf) return GROUP_ITEMS[itemOf];
|
|
267
|
+
return isGenerated(kind) ? kind : null;
|
|
268
|
+
}
|
|
269
|
+
/** Layer paths of the slot frames that draw each part, the same in every variant. */
|
|
270
|
+
function partPaths(graph, set, behaviour) {
|
|
271
|
+
const partOfSlot = new Map(Object.entries(behaviour.parts).map(([part, slot]) => [slot, part]));
|
|
272
|
+
const paths = /* @__PURE__ */ new Map();
|
|
273
|
+
const visit = (variant, node) => {
|
|
274
|
+
const part = partOfSlot.get(slotPropertyId(node) ?? "");
|
|
275
|
+
if (part) paths.set(layerPath(graph, variant.id, node.id), part);
|
|
276
|
+
for (const child of graph.getChildren(node.id)) visit(variant, child);
|
|
277
|
+
};
|
|
278
|
+
for (const variant of ownerVariants(graph, set)) for (const child of graph.getChildren(variant.id)) visit(variant, child);
|
|
279
|
+
return paths;
|
|
280
|
+
}
|
|
281
|
+
/** Each layer's behaviour part: the root, and the slot frames that draw the others. */
|
|
282
|
+
function behaviourParts(graph, set, behaviour, root) {
|
|
283
|
+
const paths = partPaths(graph, set, behaviour);
|
|
284
|
+
const contract = behaviourContract(behaviour.kind).parts;
|
|
285
|
+
const parts = /* @__PURE__ */ new Map([[root, "root"]]);
|
|
286
|
+
for (const element of allElements(root)) {
|
|
287
|
+
const part = paths.get(element.key);
|
|
288
|
+
if (part && contract.some((item) => item.id === part)) parts.set(element, part);
|
|
289
|
+
}
|
|
290
|
+
return parts;
|
|
291
|
+
}
|
|
292
|
+
/** Clears the native button look from the parts a kind renders as buttons. */
|
|
293
|
+
function resetButtons(kind, parts, repeated) {
|
|
294
|
+
const all = [...parts, ...[...repeated].map(([element, item]) => [element, item.part])];
|
|
295
|
+
for (const [element, part] of all) if (BUTTON_PARTS[kind].includes(part)) element.base = {
|
|
296
|
+
...BUTTON_RESET,
|
|
297
|
+
...element.base
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
/** The variant properties that are not values or states, as props set as `data-*`. */
|
|
301
|
+
function variantProps(graph, set, args, taken) {
|
|
302
|
+
const bound = /* @__PURE__ */ new Set([
|
|
303
|
+
...args.booleans.keys(),
|
|
304
|
+
args.states?.property,
|
|
305
|
+
args.filled?.property
|
|
306
|
+
]);
|
|
307
|
+
return behaviourProperties(graph, set).filter((definition) => definition.type === "VARIANT" && !bound.has(definition.name)).map((definition) => ({
|
|
308
|
+
name: claimName(camelCase(identifierName(definition.name, "Prop")), taken),
|
|
309
|
+
property: definition.name,
|
|
310
|
+
options: uniq([...definition.variantOptions ?? [], ...ownerVariants(graph, set).map((variant) => variant.componentPropertyValues[definition.name] ?? "")]).filter((option) => option !== ""),
|
|
311
|
+
default: variantDefaultValue(graph, set, definition)
|
|
312
|
+
}));
|
|
313
|
+
}
|
|
314
|
+
/** What the root binds: its model, an item's value, `disabled`, and its variant props. */
|
|
315
|
+
function rootBindings(model, item, disabled, props) {
|
|
316
|
+
return [
|
|
317
|
+
...model ? [{
|
|
318
|
+
type: "model",
|
|
319
|
+
name: model
|
|
320
|
+
}] : [],
|
|
321
|
+
...item ? [{ type: "value" }] : [],
|
|
322
|
+
...disabled ? [{ type: "disabled" }] : [],
|
|
323
|
+
...props.map((prop) => ({
|
|
324
|
+
type: "prop",
|
|
325
|
+
prop,
|
|
326
|
+
attribute: propAttribute(prop.property)
|
|
327
|
+
}))
|
|
328
|
+
];
|
|
329
|
+
}
|
|
330
|
+
/**
|
|
331
|
+
* A group's items, built as the group's item component; null for other owners and for a
|
|
332
|
+
* group's item itself.
|
|
333
|
+
*/
|
|
334
|
+
function itemsOf(graph, behaviour, variantIds, parts, options, build) {
|
|
335
|
+
if (options.itemOf || !isGroup(behaviour.kind)) return null;
|
|
336
|
+
const itemsElement = [...parts].find(([, part]) => part === "items")?.[0];
|
|
337
|
+
return groupItems(graph, variantIds, itemsElement, build(behaviour.kind));
|
|
338
|
+
}
|
|
339
|
+
/**
|
|
340
|
+
* The value the component binds two ways: `value` when it chooses among options or holds a
|
|
341
|
+
* number or text, its boolean such as `checked`, or none for a group's item, which its group
|
|
342
|
+
* chooses.
|
|
343
|
+
*/
|
|
344
|
+
function modelName(args, item, valued) {
|
|
345
|
+
if (valued) return "value";
|
|
346
|
+
if (item) return null;
|
|
347
|
+
return [...args.booleans.values()].find((arg) => arg.name !== "disabled")?.name ?? null;
|
|
348
|
+
}
|
|
349
|
+
/** Whether the set draws a disabled look, as a boolean or an interaction state. */
|
|
350
|
+
const drawsDisabled = (args) => [...args.booleans.values()].some((arg) => arg.name === "disabled") || args.states?.disabled !== void 0;
|
|
351
|
+
/** The layers used rather than drawn: a group's items, then other generated components. */
|
|
352
|
+
function usedLayers(graph, root, variantIds, items, references) {
|
|
353
|
+
const used = new Map(items);
|
|
354
|
+
if (references) for (const [element, layer] of referencedLayers(graph, root, variantIds, references, used)) used.set(element, layer);
|
|
355
|
+
return used;
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* Sizes the design gives include a layer's padding and border, which a component's own
|
|
359
|
+
* stylesheet has to say, since no page reset around it does. A root without a width hugs its
|
|
360
|
+
* content, as the design draws it.
|
|
361
|
+
*/
|
|
362
|
+
function borderBoxes(root) {
|
|
363
|
+
if (!Object.hasOwn(root.base, "width")) root.base = {
|
|
364
|
+
...root.base,
|
|
365
|
+
width: "fit-content"
|
|
366
|
+
};
|
|
367
|
+
for (const element of allElements(root)) {
|
|
368
|
+
const has = (property) => Object.hasOwn(element.base, property);
|
|
369
|
+
if ((has("width") || has("height")) && !has("box-sizing")) element.base = {
|
|
370
|
+
"box-sizing": "border-box",
|
|
371
|
+
...element.base
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Each layer's part, and tabs' triggers and panels, with the styles the generated parts need:
|
|
377
|
+
* native buttons cleared, the chosen tab's look, and moving parts free of their drawn place.
|
|
378
|
+
*/
|
|
379
|
+
function drawnParts(graph, set, behaviour, kind, root) {
|
|
380
|
+
const parts = behaviour ? behaviourParts(graph, set, behaviour, root) : /* @__PURE__ */ new Map([[root, "root"]]);
|
|
381
|
+
const tabs = behaviour?.kind === "tabs" ? tabParts(parts) : null;
|
|
382
|
+
const repeated = tabs?.repeated ?? /* @__PURE__ */ new Map();
|
|
383
|
+
activeTriggers(repeated);
|
|
384
|
+
resetButtons(kind, parts, repeated);
|
|
385
|
+
freePlacedParts(kind, parts);
|
|
386
|
+
blockRoot(kind, root);
|
|
387
|
+
borderBoxes(root);
|
|
388
|
+
return {
|
|
389
|
+
parts,
|
|
390
|
+
tabs,
|
|
391
|
+
repeated
|
|
392
|
+
};
|
|
393
|
+
}
|
|
394
|
+
/** The number or text the component holds, and the text property a field's input shows. */
|
|
395
|
+
function valueModels(graph, set, kind, behaviour, args) {
|
|
396
|
+
if (!behaviour) return {
|
|
397
|
+
range: null,
|
|
398
|
+
text: null,
|
|
399
|
+
inputProperty: void 0
|
|
400
|
+
};
|
|
401
|
+
const valueText = inputValue(kind);
|
|
402
|
+
const inputProperty = valueText && textBinding(behaviour, valueText);
|
|
403
|
+
const definition = behaviourProperties(graph, set).find((item) => item.id === inputProperty);
|
|
404
|
+
return {
|
|
405
|
+
range: rangeModel(kind, behaviour),
|
|
406
|
+
text: textModel(kind, definition, args),
|
|
407
|
+
inputProperty
|
|
408
|
+
};
|
|
409
|
+
}
|
|
410
|
+
function componentModel(graph, set, options = {}) {
|
|
411
|
+
const behaviour = readBehaviour(set);
|
|
412
|
+
const kind = behaviour ? generatedKind(behaviour.kind, options.itemOf) : "plain";
|
|
413
|
+
const styles = kind ? stateStyles(graph, set, options) : null;
|
|
414
|
+
const args = behaviourArgs(graph, set) ?? { booleans: /* @__PURE__ */ new Map() };
|
|
415
|
+
if (!kind || !styles) return null;
|
|
416
|
+
const name = options.name ?? identifierName(set.name, "Component");
|
|
417
|
+
const { parts, tabs, repeated } = drawnParts(graph, set, behaviour, kind, styles.root);
|
|
418
|
+
const variantIds = [styles.restId, ...ownerVariants(graph, set).map((variant) => variant.id)];
|
|
419
|
+
const items = behaviour && itemsOf(graph, behaviour, variantIds, parts, options, (itemOf) => (owner) => componentModel(graph, owner, {
|
|
420
|
+
...options,
|
|
421
|
+
name: options.itemName ?? `${name}Item`,
|
|
422
|
+
itemName: void 0,
|
|
423
|
+
itemOf
|
|
424
|
+
}));
|
|
425
|
+
const choice = tabs?.choice ?? items?.choice ?? null;
|
|
426
|
+
const { range, text, inputProperty } = valueModels(graph, set, kind, behaviour, args);
|
|
427
|
+
const model = modelName(args, !!options.itemOf, !!(choice ?? range ?? text));
|
|
428
|
+
const taken = /* @__PURE__ */ new Set([
|
|
429
|
+
...model ? [model] : [],
|
|
430
|
+
"disabled",
|
|
431
|
+
"value"
|
|
432
|
+
]);
|
|
433
|
+
const props = variantProps(graph, set, args, taken);
|
|
434
|
+
const disabled = drawsDisabled(args);
|
|
435
|
+
const used = usedLayers(graph, styles.root, variantIds, items?.used, options.references);
|
|
436
|
+
const elements = allElements(styles.root);
|
|
437
|
+
const texts = textProps(graph, set, elements, taken, inputProperty);
|
|
438
|
+
const input = inputLayers(texts.input);
|
|
439
|
+
numberInput(input, range);
|
|
440
|
+
const { booleans, shownBy } = booleanProps(graph, set, elements, taken);
|
|
441
|
+
const { slots, slotOf } = slotProps(graph, set, elements, taken, parts);
|
|
442
|
+
return {
|
|
443
|
+
name,
|
|
444
|
+
kind,
|
|
445
|
+
styles,
|
|
446
|
+
tree: componentTree(styles.root, {
|
|
447
|
+
parts,
|
|
448
|
+
classes: layerClassNames(styles),
|
|
449
|
+
texts: texts.bound,
|
|
450
|
+
used,
|
|
451
|
+
repeated,
|
|
452
|
+
input,
|
|
453
|
+
shownBy,
|
|
454
|
+
slotOf,
|
|
455
|
+
shaders: shaderLayers(graph, styles.restId, elements),
|
|
456
|
+
layerOf: (element) => restLayerOf(graph, styles, element)
|
|
457
|
+
}, rootBindings(model, !!options.itemOf, disabled, props)),
|
|
458
|
+
model,
|
|
459
|
+
choice,
|
|
460
|
+
range,
|
|
461
|
+
text,
|
|
462
|
+
valueProp: !!options.itemOf,
|
|
463
|
+
item: items?.item ?? null,
|
|
464
|
+
disabled,
|
|
465
|
+
props,
|
|
466
|
+
texts: texts.texts,
|
|
467
|
+
booleans,
|
|
468
|
+
slots,
|
|
469
|
+
args
|
|
470
|
+
};
|
|
471
|
+
}
|
|
472
|
+
//#endregion
|
|
473
|
+
//#region src/export/components/styling.ts
|
|
474
|
+
/** How a generated component is styled: a stylesheet, or Tailwind utilities in its markup. */
|
|
475
|
+
const COMPONENT_STYLINGS = ["css", "tailwind"];
|
|
476
|
+
/** Where a layer's shader plays: filling the layer, beneath its content, inside its corners. */
|
|
477
|
+
const SHADER_UTILITIES = "pointer-events-none absolute inset-0 -z-10 rounded-[inherit]";
|
|
478
|
+
/**
|
|
479
|
+
* The class each of a component's readable class names stands for. Styled with a stylesheet, a
|
|
480
|
+
* class is itself; with Tailwind it is the layer's utilities, its states behind their variants,
|
|
481
|
+
* and a shader canvas's placement.
|
|
482
|
+
*/
|
|
483
|
+
function componentClasses(component, styling) {
|
|
484
|
+
if (styling === "css") return (className) => className;
|
|
485
|
+
const readable = layerClassNames(component.styles);
|
|
486
|
+
const utilities = stateTailwindClasses(component.styles);
|
|
487
|
+
const byClass = /* @__PURE__ */ new Map();
|
|
488
|
+
for (const [element, className] of readable) byClass.set(className, utilities.get(element) ?? "");
|
|
489
|
+
for (const { shader } of shaderElements(component.tree)) if (shader) byClass.set(shader.className, SHADER_UTILITIES);
|
|
490
|
+
return (className) => byClass.get(className) ?? className;
|
|
491
|
+
}
|
|
492
|
+
//#endregion
|
|
493
|
+
//#region src/export/components/react/shared.ts
|
|
494
|
+
/** `styles.thumb`, or `styles['switch-thumb']` for a class that isn't an identifier. */
|
|
495
|
+
function moduleClass(className) {
|
|
496
|
+
return {
|
|
497
|
+
type: "MemberExpression",
|
|
498
|
+
object: es.identifier("styles"),
|
|
499
|
+
property: es.isIdentifierName(className) ? es.identifier(className) : es.string(className),
|
|
500
|
+
computed: !es.isIdentifierName(className),
|
|
501
|
+
optional: false
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
const numeric = (name, value) => jsx.attribute(name, jsx.container(es.number(value)));
|
|
505
|
+
/** A `className` value: a plain string as an attribute string, anything else in braces. */
|
|
506
|
+
function classValue(value) {
|
|
507
|
+
return value.type === "Literal" && typeof value.value === "string" ? jsx.stringValue(value.value) : jsx.container(value);
|
|
508
|
+
}
|
|
509
|
+
/** `element`, recorded as drawing `layerId`, so code can link back to that layer. */
|
|
510
|
+
function drawing$1(element, layerId, uses) {
|
|
511
|
+
if (layerId) uses.layers.set(element, layerId);
|
|
512
|
+
return element;
|
|
513
|
+
}
|
|
514
|
+
/** The layer of each JSX element in `program`, in the order the elements open. */
|
|
515
|
+
function elementLayers(program, layers) {
|
|
516
|
+
const found = [];
|
|
517
|
+
const visit = (value) => {
|
|
518
|
+
if (Array.isArray(value)) {
|
|
519
|
+
for (const item of value) visit(item);
|
|
520
|
+
return;
|
|
521
|
+
}
|
|
522
|
+
if (!es.isNode(value)) return;
|
|
523
|
+
if (value.type === "JSXElement") found.push(layers.get(value) ?? null);
|
|
524
|
+
for (const item of Object.values(value)) visit(item);
|
|
525
|
+
};
|
|
526
|
+
visit(program);
|
|
527
|
+
return found;
|
|
528
|
+
}
|
|
529
|
+
//#endregion
|
|
530
|
+
//#region src/export/components/react/fields.ts
|
|
531
|
+
/** Kinds whose native input takes the caller's props, rather than their root. */
|
|
532
|
+
const INPUT_PROPS = {
|
|
533
|
+
textField: "input",
|
|
534
|
+
textarea: "textarea"
|
|
535
|
+
};
|
|
536
|
+
/** A number field's stepper parts, which change its value by a step. */
|
|
537
|
+
const STEPPERS = {
|
|
538
|
+
increment: {
|
|
539
|
+
label: "Increase",
|
|
540
|
+
sign: "+"
|
|
541
|
+
},
|
|
542
|
+
decrement: {
|
|
543
|
+
label: "Decrease",
|
|
544
|
+
sign: "-"
|
|
545
|
+
}
|
|
546
|
+
};
|
|
547
|
+
/** What a part of a native or measured control needs: a stepper's step, an indicator's width. */
|
|
548
|
+
function partAttributes(node, uses) {
|
|
549
|
+
const { range } = uses.component;
|
|
550
|
+
if (!range || !node.part) return [];
|
|
551
|
+
if (uses.kind === "progress" && node.part === "indicator") {
|
|
552
|
+
const style = progressWidth(range, es.identifier("value"));
|
|
553
|
+
return style ? [jsx.attribute("style", jsx.container(style))] : [];
|
|
554
|
+
}
|
|
555
|
+
const stepper = uses.kind === "numberField" ? STEPPERS[node.part] : void 0;
|
|
556
|
+
if (!stepper) return [];
|
|
557
|
+
return [
|
|
558
|
+
jsx.attribute("aria-label", jsx.stringValue(stepper.label)),
|
|
559
|
+
jsx.attribute("disabled", jsx.container(es.identifier("disabled"))),
|
|
560
|
+
jsx.attribute("onClick", jsx.container(es.parseExpression(`() => commit(value ${stepper.sign} ${String(range.step)})`)))
|
|
561
|
+
];
|
|
562
|
+
}
|
|
563
|
+
const NUMBER_INPUT = [
|
|
564
|
+
["type", jsx.stringValue("number")],
|
|
565
|
+
["value", jsx.container(es.parseExpression("Number.isNaN(value) ? '' : value"))],
|
|
566
|
+
["onChange", jsx.container(es.parseExpression("(event) => setValue(event.target.valueAsNumber)"))],
|
|
567
|
+
["onBlur", jsx.container(es.parseExpression("() => commit(value)"))]
|
|
568
|
+
];
|
|
569
|
+
/** A field's input: a number input that commits within its range, or the input or textarea. */
|
|
570
|
+
function inputElement(className, uses, depth) {
|
|
571
|
+
const { component } = uses;
|
|
572
|
+
const classAttribute = jsx.attribute("className", classValue(uses.classOf(className)));
|
|
573
|
+
const { range } = component;
|
|
574
|
+
if (range) return jsx.element("input", [
|
|
575
|
+
...NUMBER_INPUT.map(([name, value]) => jsx.attribute(name, value)),
|
|
576
|
+
numeric("min", range.min),
|
|
577
|
+
numeric("max", range.max),
|
|
578
|
+
numeric("step", range.step),
|
|
579
|
+
jsx.attribute("disabled", jsx.container(es.identifier("disabled"))),
|
|
580
|
+
classAttribute
|
|
581
|
+
], [], depth);
|
|
582
|
+
const start = component.text?.default;
|
|
583
|
+
const placeholder = component.text?.placeholder;
|
|
584
|
+
return jsx.element(INPUT_PROPS[uses.kind] ?? "input", [
|
|
585
|
+
...placeholder ? [jsx.attribute("placeholder", jsx.stringValue(placeholder))] : [],
|
|
586
|
+
...start ? [jsx.attribute("defaultValue", jsx.stringValue(start))] : [],
|
|
587
|
+
jsx.spread(es.identifier("props")),
|
|
588
|
+
classAttribute
|
|
589
|
+
], [], depth);
|
|
590
|
+
}
|
|
591
|
+
/** Props a kind reads itself: a progress bar's value, a number field's start and changes. */
|
|
592
|
+
function kindParameters(component) {
|
|
593
|
+
const { range } = component;
|
|
594
|
+
if (component.kind === "progress" && range) return [{
|
|
595
|
+
name: "value",
|
|
596
|
+
default: es.number(range.default)
|
|
597
|
+
}];
|
|
598
|
+
if (component.kind === "numberField" && range) return [
|
|
599
|
+
{
|
|
600
|
+
name: "defaultValue",
|
|
601
|
+
default: es.number(range.default)
|
|
602
|
+
},
|
|
603
|
+
{ name: "onValueChange" },
|
|
604
|
+
{ name: "disabled" }
|
|
605
|
+
];
|
|
606
|
+
return [];
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* A number field's value: typed freely, and settled within its range when it loses focus or a
|
|
610
|
+
* stepper moves it, which is when the caller hears of it.
|
|
611
|
+
*/
|
|
612
|
+
const NUMBER_STATE = (range) => es.fill(es.parseModule(dedent`
|
|
613
|
+
const [value, setValue] = useState(defaultValue)
|
|
614
|
+
const commit = (next: number) => {
|
|
615
|
+
const settled = Number.isNaN(next) ? $min : Math.min($max, Math.max($min, next))
|
|
616
|
+
setValue(settled)
|
|
617
|
+
onValueChange?.(settled)
|
|
618
|
+
}
|
|
619
|
+
`), {
|
|
620
|
+
$min: es.number(range.min),
|
|
621
|
+
$max: es.number(range.max)
|
|
622
|
+
}).body;
|
|
623
|
+
/** The component function's statements before it returns, such as a number field's state. */
|
|
624
|
+
function prepend(declaration, statements) {
|
|
625
|
+
const fn = es.child(declaration, "declaration");
|
|
626
|
+
const body = es.child(fn, "body");
|
|
627
|
+
if (!body || statements.length === 0) return;
|
|
628
|
+
body.body = [...statements, ...es.children(body, "body")];
|
|
629
|
+
}
|
|
630
|
+
//#endregion
|
|
631
|
+
//#region src/export/components/react/radix.ts
|
|
632
|
+
/**
|
|
633
|
+
* The Radix primitive a kind renders, from the unified `radix-ui` package, and the component
|
|
634
|
+
* of it each part uses. Its root takes the value two ways (`checked` and `onCheckedChange`,
|
|
635
|
+
* `pressed`, `open`) and `disabled` itself, so the generated props extend the root's.
|
|
636
|
+
*/
|
|
637
|
+
const RADIX = {
|
|
638
|
+
button: null,
|
|
639
|
+
switch: {
|
|
640
|
+
namespace: "Switch",
|
|
641
|
+
parts: {
|
|
642
|
+
root: "Root",
|
|
643
|
+
thumb: "Thumb"
|
|
644
|
+
}
|
|
645
|
+
},
|
|
646
|
+
checkbox: {
|
|
647
|
+
namespace: "Checkbox",
|
|
648
|
+
parts: {
|
|
649
|
+
root: "Root",
|
|
650
|
+
indicator: "Indicator"
|
|
651
|
+
}
|
|
652
|
+
},
|
|
653
|
+
toggle: {
|
|
654
|
+
namespace: "Toggle",
|
|
655
|
+
parts: { root: "Root" }
|
|
656
|
+
},
|
|
657
|
+
collapsible: {
|
|
658
|
+
namespace: "Collapsible",
|
|
659
|
+
parts: {
|
|
660
|
+
root: "Root",
|
|
661
|
+
trigger: "Trigger",
|
|
662
|
+
content: "Content"
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
tabs: {
|
|
666
|
+
namespace: "Tabs",
|
|
667
|
+
parts: {
|
|
668
|
+
root: "Root",
|
|
669
|
+
list: "List",
|
|
670
|
+
trigger: "Trigger",
|
|
671
|
+
content: "Content"
|
|
672
|
+
}
|
|
673
|
+
},
|
|
674
|
+
radioGroup: {
|
|
675
|
+
namespace: "RadioGroup",
|
|
676
|
+
parts: { root: "Root" }
|
|
677
|
+
},
|
|
678
|
+
toggleGroup: {
|
|
679
|
+
namespace: "ToggleGroup",
|
|
680
|
+
parts: { root: "Root" }
|
|
681
|
+
},
|
|
682
|
+
accordion: {
|
|
683
|
+
namespace: "Accordion",
|
|
684
|
+
parts: { root: "Root" }
|
|
685
|
+
},
|
|
686
|
+
radioGroupItem: {
|
|
687
|
+
namespace: "RadioGroup",
|
|
688
|
+
parts: {
|
|
689
|
+
root: "Item",
|
|
690
|
+
indicator: "Indicator"
|
|
691
|
+
}
|
|
692
|
+
},
|
|
693
|
+
toggleGroupItem: {
|
|
694
|
+
namespace: "ToggleGroup",
|
|
695
|
+
parts: { root: "Item" }
|
|
696
|
+
},
|
|
697
|
+
accordionItem: {
|
|
698
|
+
namespace: "Accordion",
|
|
699
|
+
parts: {
|
|
700
|
+
root: "Item",
|
|
701
|
+
trigger: "Trigger",
|
|
702
|
+
content: "Content"
|
|
703
|
+
}
|
|
704
|
+
},
|
|
705
|
+
slider: {
|
|
706
|
+
namespace: "Slider",
|
|
707
|
+
parts: {
|
|
708
|
+
root: "Root",
|
|
709
|
+
track: "Track",
|
|
710
|
+
range: "Range",
|
|
711
|
+
thumb: "Thumb"
|
|
712
|
+
}
|
|
713
|
+
},
|
|
714
|
+
progress: {
|
|
715
|
+
namespace: "Progress",
|
|
716
|
+
parts: {
|
|
717
|
+
root: "Root",
|
|
718
|
+
indicator: "Indicator"
|
|
719
|
+
}
|
|
720
|
+
},
|
|
721
|
+
numberField: null,
|
|
722
|
+
textField: null,
|
|
723
|
+
textarea: null,
|
|
724
|
+
plain: null
|
|
725
|
+
};
|
|
726
|
+
/** The local name a Radix namespace is imported as, such as `SwitchPrimitive`. */
|
|
727
|
+
const PRIMITIVE = (namespace) => `${namespace}Primitive`;
|
|
728
|
+
/** `defaultChecked`: Radix's uncontrolled value, which a nested control starts from. */
|
|
729
|
+
const defaultOf = (model) => `default${upperFirst(model)}`;
|
|
730
|
+
//#endregion
|
|
731
|
+
//#region src/export/components/react/props.ts
|
|
732
|
+
/**
|
|
733
|
+
* The props of a group that fixes one choice at a time, without the `type` it fixes. The
|
|
734
|
+
* root's own props join the single and multiple choice, which a single value can't satisfy.
|
|
735
|
+
*/
|
|
736
|
+
const SINGLE_PROPS = {
|
|
737
|
+
toggleGroup: "ToggleGroupSingleProps",
|
|
738
|
+
accordion: "AccordionSingleProps"
|
|
739
|
+
};
|
|
740
|
+
const qualified = (namespace, name) => ({
|
|
741
|
+
type: "TSQualifiedName",
|
|
742
|
+
left: es.identifier(PRIMITIVE(namespace)),
|
|
743
|
+
right: es.identifier(name)
|
|
744
|
+
});
|
|
745
|
+
/** Props a kind declares itself, where no Radix root has them. */
|
|
746
|
+
const OWN_PROPS = {
|
|
747
|
+
progress: "Omit<ComponentProps<typeof ProgressPrimitive.Root>, 'value'> & { value?: number }",
|
|
748
|
+
numberField: "Omit<ComponentProps<'div'>, 'defaultValue' | 'onChange'> & { defaultValue?: number; onValueChange?: (value: number) => void; disabled?: boolean }",
|
|
749
|
+
textField: "ComponentProps<'input'>",
|
|
750
|
+
textarea: "ComponentProps<'textarea'>"
|
|
751
|
+
};
|
|
752
|
+
/** The root's own props, or a single choice group's. */
|
|
753
|
+
function rootPropsType(kind, tag) {
|
|
754
|
+
const own = OWN_PROPS[kind];
|
|
755
|
+
if (own) return es.parseType(own);
|
|
756
|
+
if (kind === "plain") return es.parseType(`ComponentProps<'${tag}'>`);
|
|
757
|
+
const radix = RADIX[kind];
|
|
758
|
+
if (!radix) return es.parseType("ComponentProps<'button'>");
|
|
759
|
+
const single = SINGLE_PROPS[kind];
|
|
760
|
+
if (single) return es.fill(es.parseType("Omit<$Props, 'type'>"), { $Props: qualified(radix.namespace, single) });
|
|
761
|
+
return es.fill(es.parseType("ComponentProps<typeof $Root>"), { $Root: qualified(radix.namespace, radix.parts.root) });
|
|
762
|
+
}
|
|
763
|
+
/** The root's own props, extended with the component's variant props when it has any. */
|
|
764
|
+
function propsType$1(component) {
|
|
765
|
+
const base = rootPropsType(component.kind, component.tree.tag);
|
|
766
|
+
if ([
|
|
767
|
+
component.props,
|
|
768
|
+
component.texts,
|
|
769
|
+
component.booleans,
|
|
770
|
+
component.slots
|
|
771
|
+
].every((list) => list.length === 0)) return base;
|
|
772
|
+
return {
|
|
773
|
+
type: "TSIntersectionType",
|
|
774
|
+
types: [base, es.objectType([
|
|
775
|
+
...component.props.map((prop) => [
|
|
776
|
+
prop.name,
|
|
777
|
+
es.stringUnionType(prop.options),
|
|
778
|
+
true
|
|
779
|
+
]),
|
|
780
|
+
...component.texts.map((text) => [
|
|
781
|
+
text.name,
|
|
782
|
+
es.parseType("string"),
|
|
783
|
+
true
|
|
784
|
+
]),
|
|
785
|
+
...component.booleans.map((prop) => [
|
|
786
|
+
prop.name,
|
|
787
|
+
es.parseType("boolean"),
|
|
788
|
+
true
|
|
789
|
+
]),
|
|
790
|
+
...component.slots.map((slot) => [
|
|
791
|
+
slot.name,
|
|
792
|
+
es.parseType("ReactNode"),
|
|
793
|
+
true
|
|
794
|
+
])
|
|
795
|
+
])]
|
|
796
|
+
};
|
|
797
|
+
}
|
|
798
|
+
/**
|
|
799
|
+
* `{ size = 'Small', label = 'Save', ...props }`: variant and text props with their defaults,
|
|
800
|
+
* kept off the root, and the rest passed on.
|
|
801
|
+
*/
|
|
802
|
+
function parameters(component) {
|
|
803
|
+
const own = [
|
|
804
|
+
...kindParameters(component),
|
|
805
|
+
...[...component.props, ...component.texts].map((prop) => ({
|
|
806
|
+
name: prop.name,
|
|
807
|
+
default: es.string(prop.default)
|
|
808
|
+
})),
|
|
809
|
+
...component.booleans.map((prop) => ({
|
|
810
|
+
name: prop.name,
|
|
811
|
+
default: es.parseExpression(String(prop.default))
|
|
812
|
+
})),
|
|
813
|
+
...component.slots.map((slot) => ({ name: slot.name }))
|
|
814
|
+
];
|
|
815
|
+
if (own.length === 0) return es.identifier("props");
|
|
816
|
+
return {
|
|
817
|
+
type: "ObjectPattern",
|
|
818
|
+
properties: [...own.map((prop) => ({
|
|
819
|
+
type: "Property",
|
|
820
|
+
kind: "init",
|
|
821
|
+
key: es.identifier(prop.name),
|
|
822
|
+
value: prop.default ? {
|
|
823
|
+
type: "AssignmentPattern",
|
|
824
|
+
left: es.identifier(prop.name),
|
|
825
|
+
right: prop.default
|
|
826
|
+
} : es.identifier(prop.name),
|
|
827
|
+
computed: false,
|
|
828
|
+
method: false,
|
|
829
|
+
shorthand: true
|
|
830
|
+
})), {
|
|
831
|
+
type: "RestElement",
|
|
832
|
+
argument: es.identifier("props")
|
|
833
|
+
}]
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
/**
|
|
837
|
+
* `import { useState, type ComponentProps, type ReactNode } from 'react'` with only what the
|
|
838
|
+
* component uses: a number field's state, its root's props, and its slots' content.
|
|
839
|
+
*/
|
|
840
|
+
function reactImport(uses) {
|
|
841
|
+
const types = [...uses.props ? ["ComponentProps"] : [], ...uses.slots ? ["ReactNode"] : []];
|
|
842
|
+
if (!uses.state && types.length === 0) return [];
|
|
843
|
+
const specifiers = uses.state ? ["useState", ...types.map((name) => `type ${name}`)].join(", ") : types.join(", ");
|
|
844
|
+
return es.parseModule(`import ${uses.state ? "" : "type "}{ ${specifiers} } from 'react'`).body;
|
|
845
|
+
}
|
|
846
|
+
/**
|
|
847
|
+
* The prop a story sets the value with: Radix's uncontrolled value, so a story's control and
|
|
848
|
+
* its play function can both change it, which a slider takes as a list; a progress bar's own
|
|
849
|
+
* value, which nothing else changes.
|
|
850
|
+
*/
|
|
851
|
+
function valueArg(kind, model) {
|
|
852
|
+
if (!model) return { valueArg: null };
|
|
853
|
+
if (kind === "progress") return { valueArg: "value" };
|
|
854
|
+
return {
|
|
855
|
+
valueArg: defaultOf(model),
|
|
856
|
+
...kind === "slider" ? { valueList: true } : {}
|
|
857
|
+
};
|
|
858
|
+
}
|
|
859
|
+
//#endregion
|
|
860
|
+
//#region src/export/components/react/index.ts
|
|
861
|
+
/** What a group's root fixes: one item chosen at a time, and an accordion that can all close. */
|
|
862
|
+
const ROOT_ATTRIBUTES$1 = {
|
|
863
|
+
toggleGroup: [jsx.attribute("type", jsx.stringValue("single"))],
|
|
864
|
+
accordion: [jsx.attribute("type", jsx.stringValue("single")), jsx.attribute("collapsible", null)]
|
|
865
|
+
};
|
|
866
|
+
const HEADING_STYLE$1 = jsx.container(es.object(Object.entries(HEADING_RESET).map(([property, value]) => [camelCase(property), es.string(value)])));
|
|
867
|
+
/** Several classes on one element, skipping a caller's that is absent. */
|
|
868
|
+
const CLASS_NAMES = es.parseExpression("$classes.filter(Boolean).join(' ')");
|
|
869
|
+
/** Whether a layer renders as a native button: a button's root, or a number field's stepper. */
|
|
870
|
+
const isNativeButton = (node, kind) => kind === "button" && node.part === "root" || kind === "numberField" && node.part !== null && Object.hasOwn(STEPPERS, node.part);
|
|
871
|
+
/** A Radix primitive's part, a native button, or the design's tag. */
|
|
872
|
+
function tagOf(node, kind) {
|
|
873
|
+
const radix = RADIX[kind];
|
|
874
|
+
const part = node.part ? radix?.parts[node.part] : void 0;
|
|
875
|
+
if (radix && part) return `${PRIMITIVE(radix.namespace)}.${part}`;
|
|
876
|
+
return isNativeButton(node, kind) ? "button" : node.tag;
|
|
877
|
+
}
|
|
878
|
+
/** `disabled || undefined`, so a native root sets `data-disabled` only while disabled. */
|
|
879
|
+
const disabledFlag = (kind) => es.parseExpression(kind === "numberField" ? "disabled || undefined" : "props.disabled || undefined");
|
|
880
|
+
/** The Iconify component, named apart from any component the design calls `Icon`. */
|
|
881
|
+
const ICONIFY$1 = "IconifyIcon";
|
|
882
|
+
/** An icon: its package's own component where it has the icon, Iconify's otherwise. */
|
|
883
|
+
function iconElement$1(node, uses, depth) {
|
|
884
|
+
const className = jsx.attribute("className", classValue(uses.classOf(node.className)));
|
|
885
|
+
const found = iconDrawing(node.icon, uses.iconPackage, "react");
|
|
886
|
+
const drawn = found && uses.nameIcon(found);
|
|
887
|
+
if (!drawn) {
|
|
888
|
+
uses.icons = true;
|
|
889
|
+
return jsx.element(ICONIFY$1, [jsx.attribute("icon", jsx.stringValue(node.icon)), className], [], depth);
|
|
890
|
+
}
|
|
891
|
+
uses.iconImports.push(...drawingImports(drawn));
|
|
892
|
+
if (drawn.type === "component") return jsx.element(drawn.import.local, [className], [], depth);
|
|
893
|
+
return jsx.element(drawn.component.local, [jsx.attribute("icon", jsx.container(es.identifier(drawn.import.local))), className], [], depth);
|
|
894
|
+
}
|
|
895
|
+
/** A reference's prop value: a bare name for true, `{false}`, or a string. */
|
|
896
|
+
function referenceValue(value) {
|
|
897
|
+
if (value === true) return null;
|
|
898
|
+
if (value === false) return jsx.container(jsx.literal(false));
|
|
899
|
+
return jsx.stringValue(value);
|
|
900
|
+
}
|
|
901
|
+
function reference$1(node, uses, depth) {
|
|
902
|
+
uses.components.add(node.component);
|
|
903
|
+
return drawing$1(jsx.element(node.component, [
|
|
904
|
+
jsx.attribute("className", classValue(uses.classOf(node.className))),
|
|
905
|
+
...node.props.map((prop) => jsx.attribute(prop.name, referenceValue(prop.value))),
|
|
906
|
+
...node.model ? [jsx.attribute(defaultOf(node.model), null)] : []
|
|
907
|
+
], [], depth), node.layerId, uses);
|
|
908
|
+
}
|
|
909
|
+
function element(node, uses, depth) {
|
|
910
|
+
if (node.type === "text") return jsx.text(node.value);
|
|
911
|
+
if (node.type === "textProp") return jsx.container(es.identifier(node.name));
|
|
912
|
+
if (node.type === "reference") return reference$1(node, uses, depth);
|
|
913
|
+
if (node.type === "input") return drawing$1(inputElement(node.className, uses, depth), node.layerId, uses);
|
|
914
|
+
if (node.type === "icon") return drawing$1(iconElement$1(node, uses, depth), node.layerId, uses);
|
|
915
|
+
return designElement$1(node, uses, depth);
|
|
916
|
+
}
|
|
917
|
+
/**
|
|
918
|
+
* The design's own classes, the state styles' module class, and on the root the caller's,
|
|
919
|
+
* which the spread would otherwise lose to the generated one.
|
|
920
|
+
*/
|
|
921
|
+
function classNameOf(node, root, uses) {
|
|
922
|
+
const classes = [
|
|
923
|
+
...node.attrs.class ? [es.string(node.attrs.class)] : [],
|
|
924
|
+
uses.classOf(node.className),
|
|
925
|
+
...root ? [es.parseExpression("props.className")] : []
|
|
926
|
+
];
|
|
927
|
+
return (classes.length === 1 ? classes.at(0) : void 0) ?? es.fill(CLASS_NAMES, { $classes: es.array(classes) });
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* What a root fixes before the caller's props: a group's single choice and its start, and a
|
|
931
|
+
* slider's or progress bar's range and the value it starts at.
|
|
932
|
+
*/
|
|
933
|
+
function rootAttributes(uses) {
|
|
934
|
+
const { component } = uses;
|
|
935
|
+
const start = uses.choice?.default;
|
|
936
|
+
const { range } = component;
|
|
937
|
+
return [
|
|
938
|
+
...ROOT_ATTRIBUTES$1[uses.kind] ?? [],
|
|
939
|
+
...start ? [jsx.attribute("defaultValue", jsx.stringValue(start))] : [],
|
|
940
|
+
...range && uses.kind === "slider" ? [
|
|
941
|
+
jsx.attribute("defaultValue", jsx.container(es.array([es.number(range.default)]))),
|
|
942
|
+
numeric("min", range.min),
|
|
943
|
+
numeric("max", range.max),
|
|
944
|
+
numeric("step", range.step)
|
|
945
|
+
] : [],
|
|
946
|
+
...range && uses.kind === "progress" ? [jsx.attribute("value", jsx.container(es.fill(es.parseExpression("Math.min($max, Math.max($min, value))"), {
|
|
947
|
+
$min: es.number(range.min),
|
|
948
|
+
$max: es.number(range.max)
|
|
949
|
+
}))), numeric("max", range.max)] : []
|
|
950
|
+
];
|
|
951
|
+
}
|
|
952
|
+
function bindingAttributes$1(node, kind, native) {
|
|
953
|
+
return node.bindings.flatMap((binding) => {
|
|
954
|
+
if (binding.type === "prop") return [jsx.attribute(binding.attribute, jsx.container(es.identifier(binding.prop.name)))];
|
|
955
|
+
if (binding.type === "disabled" && native) return [jsx.attribute("data-disabled", jsx.container(disabledFlag(kind)))];
|
|
956
|
+
return [];
|
|
957
|
+
});
|
|
958
|
+
}
|
|
959
|
+
/** A shader playing behind its layer's content, with telemetry off, as every export has it. */
|
|
960
|
+
function shaderCanvas$1(shader, uses, depth) {
|
|
961
|
+
const effect = (at) => (component) => {
|
|
962
|
+
uses.shaders.add(component.type);
|
|
963
|
+
const children = drawnEffects(component.children).map(effect(at + 1));
|
|
964
|
+
return jsx.element(shaderEffectName(component.type), shaderEffectAttributes(component), children, at);
|
|
965
|
+
};
|
|
966
|
+
return jsx.element(SHADER_CANVAS, [jsx.attribute("className", classValue(uses.classOf(shader.className))), jsx.attribute("disableTelemetry", null)], drawnEffects(shader.preset.components).map(effect(depth + 1)), depth);
|
|
967
|
+
}
|
|
968
|
+
/** Words alone stay on the element's line, where a line break would add a space. */
|
|
969
|
+
function wordsOnly(node) {
|
|
970
|
+
const only = node.children.length === 1 ? node.children[0]?.type : void 0;
|
|
971
|
+
return !node.slot && !node.shader && (only === "text" || only === "textProp");
|
|
972
|
+
}
|
|
973
|
+
function designElement$1(node, uses, depth) {
|
|
974
|
+
const kind = uses.kind;
|
|
975
|
+
const root = node.part === "root";
|
|
976
|
+
const native = root && !(node.part && RADIX[kind]?.parts[node.part]);
|
|
977
|
+
const attributes = [
|
|
978
|
+
...isNativeButton(node, kind) ? [jsx.attribute("type", jsx.stringValue("button"))] : [],
|
|
979
|
+
...root ? rootAttributes(uses) : [],
|
|
980
|
+
...root && !INPUT_PROPS[kind] ? [jsx.spread(es.identifier("props"))] : [],
|
|
981
|
+
...node.value === void 0 ? [] : [jsx.attribute("value", jsx.stringValue(node.value))],
|
|
982
|
+
...Object.entries(omit(node.attrs, ["class"])).map(([name, value]) => jsx.attribute(name, jsx.stringValue(value))),
|
|
983
|
+
...partAttributes(node, uses),
|
|
984
|
+
jsx.attribute("className", classValue(classNameOf(node, root, uses))),
|
|
985
|
+
...bindingAttributes$1(node, kind, native)
|
|
986
|
+
];
|
|
987
|
+
const inline = wordsOnly(node);
|
|
988
|
+
const header = kind === "accordionItem" && node.part === "trigger";
|
|
989
|
+
const childDepth = depth + (header ? 2 : 1);
|
|
990
|
+
const content = node.children.map((child) => element(child, uses, childDepth));
|
|
991
|
+
const shader = node.shader ? [shaderCanvas$1(node.shader, uses, childDepth)] : [];
|
|
992
|
+
const drawn = drawing$1(jsx.element(tagOf(node, kind), attributes, [...shader, ...node.slot ? [slotContent(node.slot, content, childDepth)] : content], depth + (header ? 1 : 0), inline), node.layerId, uses);
|
|
993
|
+
const shown = header ? jsx.element(`${PRIMITIVE("Accordion")}.Header`, [jsx.attribute("style", HEADING_STYLE$1)], [drawn], depth) : drawn;
|
|
994
|
+
return node.shownBy ? whenShown(node.shownBy, shown) : shown;
|
|
995
|
+
}
|
|
996
|
+
const LOGICAL = (operator, left, right) => ({
|
|
997
|
+
type: "LogicalExpression",
|
|
998
|
+
operator,
|
|
999
|
+
left,
|
|
1000
|
+
right
|
|
1001
|
+
});
|
|
1002
|
+
/** `{content ?? <>…</>}`: what the caller passes for a slot, or the design's content. */
|
|
1003
|
+
function slotContent(slot, children, depth) {
|
|
1004
|
+
return jsx.container(LOGICAL("??", es.identifier(slot), jsx.fragment(children, depth)));
|
|
1005
|
+
}
|
|
1006
|
+
/** `{showIcon && <…/>}`: a layer a boolean prop shows. */
|
|
1007
|
+
function whenShown(prop, drawn) {
|
|
1008
|
+
return jsx.container(LOGICAL("&&", es.identifier(prop), drawn));
|
|
1009
|
+
}
|
|
1010
|
+
const MODULE = es.parseModule(dedent`
|
|
1011
|
+
import type { ComponentProps } from 'react'
|
|
1012
|
+
|
|
1013
|
+
import styles from '$styles'
|
|
1014
|
+
|
|
1015
|
+
export type $Props = $Type
|
|
1016
|
+
|
|
1017
|
+
export function $Name($params: $Props) {
|
|
1018
|
+
return $body
|
|
1019
|
+
}
|
|
1020
|
+
`);
|
|
1021
|
+
const RADIX_IMPORT = es.parseModule(`import { $Namespace as $Primitive } from 'radix-ui'`);
|
|
1022
|
+
const ICONIFY_IMPORT = es.parseModule(`import { Icon as ${ICONIFY$1} } from '@iconify/react'`).body;
|
|
1023
|
+
const IMPORT_COMPONENT$1 = es.parseModule(`import { $Component } from '$path'`);
|
|
1024
|
+
/**
|
|
1025
|
+
* A component as a React component on Radix UI: its parts as Radix primitives from the
|
|
1026
|
+
* `radix-ui` package, its variants' state styles in a CSS module, and props that extend the
|
|
1027
|
+
* Radix root's with its other variant properties.
|
|
1028
|
+
*/
|
|
1029
|
+
const reactComponent = async (component, options = {}) => {
|
|
1030
|
+
const styling = options.styling ?? "css";
|
|
1031
|
+
const radix = RADIX[component.kind];
|
|
1032
|
+
const stylesPath = `${component.name}.module.css`;
|
|
1033
|
+
const classOf = componentClasses(component, styling);
|
|
1034
|
+
const uses = {
|
|
1035
|
+
component,
|
|
1036
|
+
classOf: (className) => styling === "css" ? moduleClass(className) : es.string(classOf(className)),
|
|
1037
|
+
kind: component.kind,
|
|
1038
|
+
choice: component.choice,
|
|
1039
|
+
components: /* @__PURE__ */ new Set(),
|
|
1040
|
+
layers: /* @__PURE__ */ new WeakMap(),
|
|
1041
|
+
icons: false,
|
|
1042
|
+
iconPackage: options.icons ?? "iconify",
|
|
1043
|
+
iconImports: [],
|
|
1044
|
+
nameIcon: iconNamer(component, [
|
|
1045
|
+
ICONIFY$1,
|
|
1046
|
+
"styles",
|
|
1047
|
+
`${component.name}Props`,
|
|
1048
|
+
...radix ? [radix.namespace, PRIMITIVE(radix.namespace)] : []
|
|
1049
|
+
]),
|
|
1050
|
+
shaders: /* @__PURE__ */ new Set()
|
|
1051
|
+
};
|
|
1052
|
+
const body = element(component.tree, uses, 1);
|
|
1053
|
+
const imports = [
|
|
1054
|
+
...uses.icons ? ICONIFY_IMPORT : [],
|
|
1055
|
+
...iconImportDeclarations(uses.iconImports),
|
|
1056
|
+
...uses.shaders.size > 0 ? [shaderImport("shaders/react", uses.shaders)] : [],
|
|
1057
|
+
...radix ? es.fill(RADIX_IMPORT, {
|
|
1058
|
+
$Namespace: es.identifier(radix.namespace),
|
|
1059
|
+
$Primitive: es.identifier(PRIMITIVE(radix.namespace))
|
|
1060
|
+
}).body : [],
|
|
1061
|
+
...[...uses.components].sort().flatMap((name) => es.fill(IMPORT_COMPONENT$1, {
|
|
1062
|
+
$Component: es.identifier(name),
|
|
1063
|
+
$path: es.string(`./${name}`)
|
|
1064
|
+
}).body)
|
|
1065
|
+
];
|
|
1066
|
+
const [, stylesImport, ...rest] = es.fill(MODULE, {
|
|
1067
|
+
$styles: es.string(`./${stylesPath}`),
|
|
1068
|
+
$Props: es.identifier(`${component.name}Props`),
|
|
1069
|
+
$Type: propsType$1(component),
|
|
1070
|
+
$Name: es.identifier(component.name),
|
|
1071
|
+
$params: parameters(component),
|
|
1072
|
+
$body: body
|
|
1073
|
+
}).body;
|
|
1074
|
+
const state = component.kind === "numberField" && component.range ? NUMBER_STATE(component.range) : [];
|
|
1075
|
+
prepend(rest.at(-1), state);
|
|
1076
|
+
const program = {
|
|
1077
|
+
type: "Program",
|
|
1078
|
+
sourceType: "module",
|
|
1079
|
+
body: [
|
|
1080
|
+
...reactImport({
|
|
1081
|
+
state: state.length > 0,
|
|
1082
|
+
props: !SINGLE_PROPS[component.kind],
|
|
1083
|
+
slots: component.slots.length > 0
|
|
1084
|
+
}),
|
|
1085
|
+
...imports,
|
|
1086
|
+
...styling === "css" ? [stylesImport] : [],
|
|
1087
|
+
...rest
|
|
1088
|
+
]
|
|
1089
|
+
};
|
|
1090
|
+
const stylesheet = styling === "css" ? [{
|
|
1091
|
+
path: stylesPath,
|
|
1092
|
+
content: compact([(await stateStylesToCSS(component.styles)).css, shaderCSS(component.tree)]).join("\n")
|
|
1093
|
+
}] : [];
|
|
1094
|
+
const model = component.model;
|
|
1095
|
+
return {
|
|
1096
|
+
files: [
|
|
1097
|
+
...(component.item ? await reactComponent(component.item, options) : null)?.files ?? [],
|
|
1098
|
+
{
|
|
1099
|
+
path: `${component.name}.tsx`,
|
|
1100
|
+
content: `${jsx.printModule(program)}\n`,
|
|
1101
|
+
layerIds: elementLayers(body, uses.layers)
|
|
1102
|
+
},
|
|
1103
|
+
...stylesheet
|
|
1104
|
+
],
|
|
1105
|
+
entry: {
|
|
1106
|
+
path: `./${component.name}`,
|
|
1107
|
+
named: true
|
|
1108
|
+
},
|
|
1109
|
+
...valueArg(component.kind, model)
|
|
1110
|
+
};
|
|
1111
|
+
};
|
|
1112
|
+
//#endregion
|
|
1113
|
+
//#region src/export/components/vue.ts
|
|
1114
|
+
/** The Reka UI components a kind renders, by part, and the prop its model binds. */
|
|
1115
|
+
const REKA = {
|
|
1116
|
+
button: { parts: {} },
|
|
1117
|
+
switch: {
|
|
1118
|
+
parts: {
|
|
1119
|
+
root: "SwitchRoot",
|
|
1120
|
+
thumb: "SwitchThumb"
|
|
1121
|
+
},
|
|
1122
|
+
model: "modelValue"
|
|
1123
|
+
},
|
|
1124
|
+
checkbox: {
|
|
1125
|
+
parts: {
|
|
1126
|
+
root: "CheckboxRoot",
|
|
1127
|
+
indicator: "CheckboxIndicator"
|
|
1128
|
+
},
|
|
1129
|
+
model: "modelValue"
|
|
1130
|
+
},
|
|
1131
|
+
toggle: {
|
|
1132
|
+
parts: { root: "Toggle" },
|
|
1133
|
+
model: "modelValue"
|
|
1134
|
+
},
|
|
1135
|
+
collapsible: {
|
|
1136
|
+
parts: {
|
|
1137
|
+
root: "CollapsibleRoot",
|
|
1138
|
+
trigger: "CollapsibleTrigger",
|
|
1139
|
+
content: "CollapsibleContent"
|
|
1140
|
+
},
|
|
1141
|
+
model: "open"
|
|
1142
|
+
},
|
|
1143
|
+
tabs: {
|
|
1144
|
+
parts: {
|
|
1145
|
+
root: "TabsRoot",
|
|
1146
|
+
list: "TabsList",
|
|
1147
|
+
trigger: "TabsTrigger",
|
|
1148
|
+
content: "TabsContent"
|
|
1149
|
+
},
|
|
1150
|
+
model: "modelValue"
|
|
1151
|
+
},
|
|
1152
|
+
radioGroup: {
|
|
1153
|
+
parts: { root: "RadioGroupRoot" },
|
|
1154
|
+
model: "modelValue"
|
|
1155
|
+
},
|
|
1156
|
+
toggleGroup: {
|
|
1157
|
+
parts: { root: "ToggleGroupRoot" },
|
|
1158
|
+
model: "modelValue"
|
|
1159
|
+
},
|
|
1160
|
+
accordion: {
|
|
1161
|
+
parts: { root: "AccordionRoot" },
|
|
1162
|
+
model: "modelValue"
|
|
1163
|
+
},
|
|
1164
|
+
radioGroupItem: { parts: {
|
|
1165
|
+
root: "RadioGroupItem",
|
|
1166
|
+
indicator: "RadioGroupIndicator"
|
|
1167
|
+
} },
|
|
1168
|
+
toggleGroupItem: { parts: { root: "ToggleGroupItem" } },
|
|
1169
|
+
accordionItem: { parts: {
|
|
1170
|
+
root: "AccordionItem",
|
|
1171
|
+
trigger: "AccordionTrigger",
|
|
1172
|
+
content: "AccordionContent"
|
|
1173
|
+
} },
|
|
1174
|
+
slider: {
|
|
1175
|
+
parts: {
|
|
1176
|
+
root: "SliderRoot",
|
|
1177
|
+
track: "SliderTrack",
|
|
1178
|
+
range: "SliderRange",
|
|
1179
|
+
thumb: "SliderThumb"
|
|
1180
|
+
},
|
|
1181
|
+
model: "modelValue"
|
|
1182
|
+
},
|
|
1183
|
+
progress: {
|
|
1184
|
+
parts: {
|
|
1185
|
+
root: "ProgressRoot",
|
|
1186
|
+
indicator: "ProgressIndicator"
|
|
1187
|
+
},
|
|
1188
|
+
model: "modelValue"
|
|
1189
|
+
},
|
|
1190
|
+
numberField: {
|
|
1191
|
+
parts: {
|
|
1192
|
+
root: "NumberFieldRoot",
|
|
1193
|
+
increment: "NumberFieldIncrement",
|
|
1194
|
+
decrement: "NumberFieldDecrement"
|
|
1195
|
+
},
|
|
1196
|
+
model: "modelValue"
|
|
1197
|
+
},
|
|
1198
|
+
textField: { parts: {} },
|
|
1199
|
+
textarea: { parts: {} },
|
|
1200
|
+
plain: { parts: {} }
|
|
1201
|
+
};
|
|
1202
|
+
/** Kinds whose two-way value is their input's, which binds it rather than the root. */
|
|
1203
|
+
const INPUT_MODEL = /* @__PURE__ */ new Set(["textField", "textarea"]);
|
|
1204
|
+
/** What a group's root fixes: one item chosen at a time, and an accordion that can all close. */
|
|
1205
|
+
const ROOT_ATTRIBUTES = {
|
|
1206
|
+
toggleGroup: [vue.attribute("type", "single")],
|
|
1207
|
+
accordion: [vue.attribute("type", "single"), vue.bound("collapsible", es.parseExpression("true"))]
|
|
1208
|
+
};
|
|
1209
|
+
const identifier = (name) => es.identifier(name);
|
|
1210
|
+
const HEADING_STYLE = Object.entries(HEADING_RESET).map(([property, value]) => `${property}: ${value}`).join("; ");
|
|
1211
|
+
/** `disabled || undefined`, so a native button sets `data-disabled` only while disabled. */
|
|
1212
|
+
const DISABLED_FLAG = es.parseExpression("disabled || undefined");
|
|
1213
|
+
function bindingAttributes(binding, uses, native) {
|
|
1214
|
+
const { kind } = uses;
|
|
1215
|
+
if (binding.type === "model") {
|
|
1216
|
+
if (INPUT_MODEL.has(kind)) return [];
|
|
1217
|
+
const bound = uses.sliderValues ?? binding.name;
|
|
1218
|
+
return [vue.model(identifier(bound), REKA[kind].model === "open" ? "open" : void 0)];
|
|
1219
|
+
}
|
|
1220
|
+
if (binding.type === "prop") return [vue.bound(binding.attribute, identifier(binding.prop.name))];
|
|
1221
|
+
if (binding.type === "value") return [vue.bound("value", identifier("value"))];
|
|
1222
|
+
return [...native && kind !== "button" ? [] : [vue.bound("disabled", identifier("disabled"))], ...native ? [vue.bound("data-disabled", DISABLED_FLAG)] : []];
|
|
1223
|
+
}
|
|
1224
|
+
/** The number range a slider, progress bar, or number field's root is given. */
|
|
1225
|
+
function rangeAttributes(component) {
|
|
1226
|
+
const { range } = component;
|
|
1227
|
+
if (!range) return [];
|
|
1228
|
+
const bound = (name) => vue.bound(name, es.number(range[name]));
|
|
1229
|
+
return component.kind === "progress" ? [bound("max")] : [
|
|
1230
|
+
bound("min"),
|
|
1231
|
+
bound("max"),
|
|
1232
|
+
bound("step")
|
|
1233
|
+
];
|
|
1234
|
+
}
|
|
1235
|
+
function progressStyle(component) {
|
|
1236
|
+
const { range, model } = component;
|
|
1237
|
+
const style = range && model ? progressWidth(range, identifier(model)) : null;
|
|
1238
|
+
return style ? [vue.bound("style", style)] : [];
|
|
1239
|
+
}
|
|
1240
|
+
/** `element`, recorded as drawing `layerId`, so code can link back to that layer. */
|
|
1241
|
+
function drawing(element, layerId, uses) {
|
|
1242
|
+
if (layerId) uses.layers.set(element, layerId);
|
|
1243
|
+
return element;
|
|
1244
|
+
}
|
|
1245
|
+
/** A field's input: Reka's for a number field, else a native input or textarea. */
|
|
1246
|
+
function inputNode(className, uses) {
|
|
1247
|
+
const { component } = uses;
|
|
1248
|
+
if (component.kind === "numberField") {
|
|
1249
|
+
uses.reka.add("NumberFieldInput");
|
|
1250
|
+
return vue.element("NumberFieldInput", [vue.attribute("class", uses.classOf(className))]);
|
|
1251
|
+
}
|
|
1252
|
+
const placeholder = component.text?.placeholder;
|
|
1253
|
+
return vue.element(component.kind === "textarea" ? "textarea" : "input", [
|
|
1254
|
+
vue.attribute("class", uses.classOf(className)),
|
|
1255
|
+
...component.model ? [vue.model(identifier(component.model))] : [],
|
|
1256
|
+
...placeholder ? [vue.attribute("placeholder", placeholder)] : [],
|
|
1257
|
+
...component.disabled ? [vue.bound("disabled", identifier("disabled"))] : []
|
|
1258
|
+
]);
|
|
1259
|
+
}
|
|
1260
|
+
/** The Iconify component, named apart from any component the design calls `Icon`. */
|
|
1261
|
+
const ICONIFY = "IconifyIcon";
|
|
1262
|
+
/** An icon: its package's own component where it has the icon, Iconify's otherwise. */
|
|
1263
|
+
function iconElement(node, uses) {
|
|
1264
|
+
const className = vue.attribute("class", uses.classOf(node.className));
|
|
1265
|
+
const found = iconDrawing(node.icon, uses.iconPackage, "vue");
|
|
1266
|
+
const drawn = found && uses.nameIcon(found);
|
|
1267
|
+
if (!drawn) {
|
|
1268
|
+
uses.icons = true;
|
|
1269
|
+
return vue.element(ICONIFY, [vue.attribute("icon", node.icon), className]);
|
|
1270
|
+
}
|
|
1271
|
+
uses.iconImports.push(...drawingImports(drawn));
|
|
1272
|
+
if (drawn.type === "component") return vue.element(drawn.import.local, [className]);
|
|
1273
|
+
return vue.element(drawn.component.local, [vue.bound("icon", identifier(drawn.import.local)), className]);
|
|
1274
|
+
}
|
|
1275
|
+
function reference(node, uses) {
|
|
1276
|
+
uses.components.add(node.component);
|
|
1277
|
+
const model = node.model ? claimName(`${camelCase(node.component)}${upperFirst(node.model)}`, uses.taken) : null;
|
|
1278
|
+
if (model) uses.models.push({ name: model });
|
|
1279
|
+
return vue.element(node.component, [
|
|
1280
|
+
vue.attribute("class", uses.classOf(node.className)),
|
|
1281
|
+
...node.props.map((prop) => typeof prop.value === "boolean" ? vue.bound(prop.name, es.parseExpression(String(prop.value))) : vue.attribute(prop.name, prop.value)),
|
|
1282
|
+
...model && node.model ? [vue.model(identifier(model), node.model)] : []
|
|
1283
|
+
]);
|
|
1284
|
+
}
|
|
1285
|
+
/** A design element's attributes: what shows it, its part's own, the design's, and bindings. */
|
|
1286
|
+
function elementAttributes(node, uses, { native, button }) {
|
|
1287
|
+
const root = node.part === "root";
|
|
1288
|
+
return [
|
|
1289
|
+
...node.shownBy ? [vue.renderIf(identifier(node.shownBy))] : [],
|
|
1290
|
+
...button ? [vue.attribute("type", "button")] : [],
|
|
1291
|
+
...root ? [...ROOT_ATTRIBUTES[uses.kind] ?? [], ...rangeAttributes(uses.component)] : [],
|
|
1292
|
+
...uses.kind === "progress" && node.part === "indicator" ? progressStyle(uses.component) : [],
|
|
1293
|
+
...Object.entries(omit(node.attrs, ["class"])).map(([name, value]) => vue.attribute(name, value)),
|
|
1294
|
+
vue.attribute("class", compact([node.attrs.class, uses.classOf(node.className)]).join(" ")),
|
|
1295
|
+
...node.value === void 0 ? [] : [vue.attribute("value", node.value)],
|
|
1296
|
+
...node.bindings.flatMap((binding) => bindingAttributes(binding, uses, native))
|
|
1297
|
+
];
|
|
1298
|
+
}
|
|
1299
|
+
/** A shader playing behind its layer's content, with telemetry off, as every export has it. */
|
|
1300
|
+
function shaderCanvas(shader, uses) {
|
|
1301
|
+
const effect = (component) => {
|
|
1302
|
+
uses.shaders.add(component.type);
|
|
1303
|
+
return vue.element(shaderEffectName(component.type), Object.entries(component.props ?? {}).map(([key, value]) => typeof value === "string" ? vue.attribute(kebabCase(key), value) : vue.bound(kebabCase(key), es.json(value))), drawnEffects(component.children).map(effect));
|
|
1304
|
+
};
|
|
1305
|
+
return vue.element(SHADER_CANVAS, [vue.attribute("class", uses.classOf(shader.className)), vue.bound("disable-telemetry", es.json(true))], drawnEffects(shader.preset.components).map(effect));
|
|
1306
|
+
}
|
|
1307
|
+
function designElement(node, uses) {
|
|
1308
|
+
const reka = node.part ? REKA[uses.kind].parts[node.part] : void 0;
|
|
1309
|
+
if (reka) uses.reka.add(reka);
|
|
1310
|
+
const native = node.part === "root" && !reka;
|
|
1311
|
+
const button = native && uses.kind === "button";
|
|
1312
|
+
const content = node.children.map((child) => templateNode(child, uses));
|
|
1313
|
+
const children = node.shader ? [shaderCanvas(node.shader, uses), ...content] : content;
|
|
1314
|
+
const element = drawing(vue.element(reka ?? (button ? "button" : node.tag), elementAttributes(node, uses, {
|
|
1315
|
+
native,
|
|
1316
|
+
button
|
|
1317
|
+
}), node.slot ? [...node.shader ? [shaderCanvas(node.shader, uses)] : [], vue.element("slot", [vue.attribute("name", node.slot)], content)] : children), node.layerId, uses);
|
|
1318
|
+
if (uses.kind !== "accordionItem" || node.part !== "trigger") return element;
|
|
1319
|
+
uses.reka.add("AccordionHeader");
|
|
1320
|
+
return vue.element("AccordionHeader", [vue.attribute("style", HEADING_STYLE)], [element]);
|
|
1321
|
+
}
|
|
1322
|
+
function templateNode(node, uses) {
|
|
1323
|
+
if (node.type === "text") return vue.text(node.value);
|
|
1324
|
+
if (node.type === "textProp") return vue.interpolation(identifier(node.name));
|
|
1325
|
+
if (node.type === "reference") return drawing(reference(node, uses), node.layerId, uses);
|
|
1326
|
+
if (node.type === "input") return drawing(inputNode(node.className, uses), node.layerId, uses);
|
|
1327
|
+
if (node.type === "icon") return drawing(iconElement(node, uses), node.layerId, uses);
|
|
1328
|
+
return designElement(node, uses);
|
|
1329
|
+
}
|
|
1330
|
+
function propsType(component) {
|
|
1331
|
+
const members = component.props.map((prop) => [
|
|
1332
|
+
prop.name,
|
|
1333
|
+
es.stringUnionType(prop.options),
|
|
1334
|
+
true
|
|
1335
|
+
]);
|
|
1336
|
+
if (component.disabled) members.unshift([
|
|
1337
|
+
"disabled",
|
|
1338
|
+
es.parseType("boolean"),
|
|
1339
|
+
true
|
|
1340
|
+
]);
|
|
1341
|
+
if (component.valueProp) members.unshift([
|
|
1342
|
+
"value",
|
|
1343
|
+
es.parseType("string"),
|
|
1344
|
+
false
|
|
1345
|
+
]);
|
|
1346
|
+
for (const text of component.texts) members.push([
|
|
1347
|
+
text.name,
|
|
1348
|
+
es.parseType("string"),
|
|
1349
|
+
true
|
|
1350
|
+
]);
|
|
1351
|
+
for (const prop of component.booleans) members.push([
|
|
1352
|
+
prop.name,
|
|
1353
|
+
es.parseType("boolean"),
|
|
1354
|
+
true
|
|
1355
|
+
]);
|
|
1356
|
+
return es.objectType(members);
|
|
1357
|
+
}
|
|
1358
|
+
/** `defineProps`, with `withDefaults` when any prop has a default to give. */
|
|
1359
|
+
/** Whether any prop has a default to give: `disabled`, a variant, a text, or a boolean. */
|
|
1360
|
+
const hasDefaults = (component) => component.disabled || component.props.length > 0 || component.texts.length > 0 || component.booleans.length > 0;
|
|
1361
|
+
function propsDeclaration(component) {
|
|
1362
|
+
return hasDefaults(component) ? es.fill(PROPS, {
|
|
1363
|
+
$Props: propsType(component),
|
|
1364
|
+
$defaults: propsDefaults(component)
|
|
1365
|
+
}).body : es.fill(REQUIRED_PROPS, { $Props: propsType(component) }).body;
|
|
1366
|
+
}
|
|
1367
|
+
function propsDefaults(component) {
|
|
1368
|
+
return es.object([
|
|
1369
|
+
...component.disabled ? [["disabled", es.parseExpression("false")]] : [],
|
|
1370
|
+
...component.props.map((prop) => [prop.name, es.string(prop.default)]),
|
|
1371
|
+
...component.texts.map((text) => [text.name, es.string(text.default)]),
|
|
1372
|
+
...component.booleans.map((prop) => [prop.name, es.parseExpression(String(prop.default))])
|
|
1373
|
+
]);
|
|
1374
|
+
}
|
|
1375
|
+
const PROPS = es.parseModule("withDefaults(defineProps<$Props>(), $defaults)");
|
|
1376
|
+
const REQUIRED_PROPS = es.parseModule("defineProps<$Props>()");
|
|
1377
|
+
const MODEL = es.parseModule("const $model = defineModel<boolean>($name, { default: false })");
|
|
1378
|
+
const CHOICE_MODEL = es.parseModule("const $model = defineModel<$Type>($name, { default: $default })");
|
|
1379
|
+
const OPEN_CHOICE_MODEL = es.parseModule("const $model = defineModel<$Type>($name)");
|
|
1380
|
+
const VALUE_MODEL = es.parseModule("const $model = defineModel<$Type>($name, { default: $default })");
|
|
1381
|
+
const SLIDER_VALUES = es.parseModule(dedent`
|
|
1382
|
+
const $values = computed({
|
|
1383
|
+
get: () => [$model.value],
|
|
1384
|
+
set: (next: number[] | undefined) => {
|
|
1385
|
+
$model.value = next?.[0] ?? $model.value
|
|
1386
|
+
}
|
|
1387
|
+
})
|
|
1388
|
+
`);
|
|
1389
|
+
function namedImport(names, source, local = (name) => name) {
|
|
1390
|
+
return {
|
|
1391
|
+
type: "ImportDeclaration",
|
|
1392
|
+
importKind: "value",
|
|
1393
|
+
specifiers: names.map((name) => ({
|
|
1394
|
+
type: "ImportSpecifier",
|
|
1395
|
+
imported: identifier(name),
|
|
1396
|
+
local: identifier(local(name))
|
|
1397
|
+
})),
|
|
1398
|
+
source: es.string(source)
|
|
1399
|
+
};
|
|
1400
|
+
}
|
|
1401
|
+
const IMPORT_COMPONENT = es.parseModule(`import $Component from '$path'`);
|
|
1402
|
+
const MODEL_REF = es.parseModule("const $name = ref(true)");
|
|
1403
|
+
/** What the script imports: Vue's helpers, Iconify, Reka, and other generated components. */
|
|
1404
|
+
function scriptImports(uses) {
|
|
1405
|
+
const helpers = compact([uses.sliderValues && "computed", uses.models.length > 0 && "ref"]);
|
|
1406
|
+
return [
|
|
1407
|
+
...helpers.length > 0 ? [namedImport(helpers, "vue")] : [],
|
|
1408
|
+
...uses.icons ? [namedImport(["Icon"], "@iconify/vue", () => ICONIFY)] : [],
|
|
1409
|
+
...iconImportDeclarations(uses.iconImports),
|
|
1410
|
+
...uses.reka.size > 0 ? [namedImport([...uses.reka].sort(), "reka-ui")] : [],
|
|
1411
|
+
...uses.shaders.size > 0 ? [shaderImport("shaders/vue", uses.shaders)] : [],
|
|
1412
|
+
...[...uses.components].sort().flatMap((name) => es.fill(IMPORT_COMPONENT, {
|
|
1413
|
+
$Component: identifier(name),
|
|
1414
|
+
$path: es.string(`./${name}.vue`)
|
|
1415
|
+
}).body)
|
|
1416
|
+
];
|
|
1417
|
+
}
|
|
1418
|
+
/** The model's declaration: a choice, a number or text, or a boolean, and a slider's list. */
|
|
1419
|
+
function modelDeclaration(component, uses) {
|
|
1420
|
+
const name = component.model;
|
|
1421
|
+
if (!name) return [];
|
|
1422
|
+
const { choice, range, text } = component;
|
|
1423
|
+
const base = {
|
|
1424
|
+
$model: identifier(name),
|
|
1425
|
+
$name: es.string(name)
|
|
1426
|
+
};
|
|
1427
|
+
if (choice) return es.fill(choice.default === null ? OPEN_CHOICE_MODEL : CHOICE_MODEL, {
|
|
1428
|
+
...base,
|
|
1429
|
+
$Type: es.stringUnionType(choice.options),
|
|
1430
|
+
$default: choice.default === null ? es.OMIT : es.string(choice.default)
|
|
1431
|
+
}).body;
|
|
1432
|
+
if (!range && !text) return es.fill(MODEL, base).body;
|
|
1433
|
+
const model = es.fill(VALUE_MODEL, {
|
|
1434
|
+
...base,
|
|
1435
|
+
$Type: es.parseType(range ? "number" : "string"),
|
|
1436
|
+
$default: range ? es.number(range.default) : es.string(text?.default ?? "")
|
|
1437
|
+
}).body;
|
|
1438
|
+
const values = uses.sliderValues ? es.fill(SLIDER_VALUES, {
|
|
1439
|
+
$values: identifier(uses.sliderValues),
|
|
1440
|
+
$model: identifier(name)
|
|
1441
|
+
}).body : [];
|
|
1442
|
+
return [...model, ...values];
|
|
1443
|
+
}
|
|
1444
|
+
function script(component, uses) {
|
|
1445
|
+
const props = component.valueProp || hasDefaults(component) ? propsDeclaration(component) : [];
|
|
1446
|
+
const refs = uses.models.flatMap((item) => es.fill(MODEL_REF, { $name: identifier(item.name) }).body);
|
|
1447
|
+
return {
|
|
1448
|
+
type: "Program",
|
|
1449
|
+
sourceType: "module",
|
|
1450
|
+
body: [
|
|
1451
|
+
...scriptImports(uses),
|
|
1452
|
+
...props,
|
|
1453
|
+
...modelDeclaration(component, uses),
|
|
1454
|
+
...refs
|
|
1455
|
+
]
|
|
1456
|
+
};
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* A component as a Vue single-file component on Reka UI: its variants' state styles in a
|
|
1460
|
+
* scoped stylesheet, its parts as Reka components, and its props from the behaviour and its
|
|
1461
|
+
* other variant properties.
|
|
1462
|
+
*/
|
|
1463
|
+
const vueComponent = async (component, options = {}) => {
|
|
1464
|
+
const styling = options.styling ?? "css";
|
|
1465
|
+
const taken = /* @__PURE__ */ new Set([
|
|
1466
|
+
...component.props.map((prop) => prop.name),
|
|
1467
|
+
...component.texts.map((text) => text.name),
|
|
1468
|
+
...component.booleans.map((prop) => prop.name),
|
|
1469
|
+
...component.slots.map((slot) => slot.name),
|
|
1470
|
+
...component.model ? [component.model] : [],
|
|
1471
|
+
"disabled"
|
|
1472
|
+
]);
|
|
1473
|
+
const uses = {
|
|
1474
|
+
component,
|
|
1475
|
+
classOf: componentClasses(component, styling),
|
|
1476
|
+
layers: /* @__PURE__ */ new WeakMap(),
|
|
1477
|
+
kind: component.kind,
|
|
1478
|
+
sliderValues: component.kind === "slider" && component.model ? claimName("values", taken) : null,
|
|
1479
|
+
reka: /* @__PURE__ */ new Set(),
|
|
1480
|
+
components: /* @__PURE__ */ new Set(),
|
|
1481
|
+
icons: false,
|
|
1482
|
+
iconPackage: options.icons ?? "iconify",
|
|
1483
|
+
iconImports: [],
|
|
1484
|
+
nameIcon: iconNamer(component, [ICONIFY, ...taken]),
|
|
1485
|
+
shaders: /* @__PURE__ */ new Set(),
|
|
1486
|
+
models: [],
|
|
1487
|
+
taken
|
|
1488
|
+
};
|
|
1489
|
+
const template = templateNode(component.tree, uses);
|
|
1490
|
+
const style = styling === "css" ? compact([(await stateStylesToCSS(component.styles)).css, shaderCSS(component.tree)]).join("\n") : void 0;
|
|
1491
|
+
const path = `${component.name}.vue`;
|
|
1492
|
+
const item = component.item ? await vueComponent(component.item, options) : null;
|
|
1493
|
+
const sfc = {
|
|
1494
|
+
script: script(component, uses),
|
|
1495
|
+
template,
|
|
1496
|
+
style
|
|
1497
|
+
};
|
|
1498
|
+
return {
|
|
1499
|
+
files: [...item?.files ?? [], {
|
|
1500
|
+
path,
|
|
1501
|
+
content: vue.printComponent(sfc),
|
|
1502
|
+
layerIds: vue.componentElements(sfc).map((element) => (element && uses.layers.get(element)) ?? null)
|
|
1503
|
+
}],
|
|
1504
|
+
entry: {
|
|
1505
|
+
path: `./${path}`,
|
|
1506
|
+
named: false
|
|
1507
|
+
},
|
|
1508
|
+
valueArg: component.model
|
|
1509
|
+
};
|
|
1510
|
+
};
|
|
1511
|
+
//#endregion
|
|
1512
|
+
//#region src/export/components/layers.ts
|
|
1513
|
+
/** The frameworks a layer can be generated as a component for. */
|
|
1514
|
+
const LAYER_COMPONENT_FRAMEWORKS = ["vue", "react"];
|
|
1515
|
+
/**
|
|
1516
|
+
* Each layer as a Vue or React component, as the Storybook export generates a component set's:
|
|
1517
|
+
* a component set with its variants as props and its behaviour's parts, and any other layer as
|
|
1518
|
+
* a plain component of the elements it draws. Names stay distinct across the layers.
|
|
1519
|
+
*/
|
|
1520
|
+
async function layerComponents(graph, layerIds, framework, options = {}) {
|
|
1521
|
+
const generate = framework === "vue" ? vueComponent : reactComponent;
|
|
1522
|
+
const taken = /* @__PURE__ */ new Set();
|
|
1523
|
+
const components = [];
|
|
1524
|
+
for (const layerId of layerIds) {
|
|
1525
|
+
const layer = graph.getNode(layerId);
|
|
1526
|
+
if (!layer) continue;
|
|
1527
|
+
const name = claimName(identifierName(layer.name, "Component"), taken);
|
|
1528
|
+
const model = componentModel(graph, layer, {
|
|
1529
|
+
...options,
|
|
1530
|
+
name,
|
|
1531
|
+
references: /* @__PURE__ */ new Map()
|
|
1532
|
+
});
|
|
1533
|
+
if (!model) continue;
|
|
1534
|
+
const { files } = await generate(model, {
|
|
1535
|
+
styling: options.styling,
|
|
1536
|
+
icons: options.icons
|
|
1537
|
+
});
|
|
1538
|
+
components.push({
|
|
1539
|
+
name,
|
|
1540
|
+
layerId,
|
|
1541
|
+
files
|
|
1542
|
+
});
|
|
1543
|
+
}
|
|
1544
|
+
return components;
|
|
1545
|
+
}
|
|
1546
|
+
/** The state element each element of markup built from it draws, which it builds one for one. */
|
|
1547
|
+
function drawnBy(state, design, found) {
|
|
1548
|
+
if (state.type === "text" || design.type === "text") return;
|
|
1549
|
+
found.set(design, state);
|
|
1550
|
+
for (const [child, built] of zip(state.children, design.children)) drawnBy(child, built, found);
|
|
1551
|
+
}
|
|
1552
|
+
/** The layer of each element `serializeHTML` writes for the document, in the order they open. */
|
|
1553
|
+
function markupLayers(graph, styles, document) {
|
|
1554
|
+
const states = /* @__PURE__ */ new Map();
|
|
1555
|
+
for (const root of document.children) drawnBy(styles.root, root, states);
|
|
1556
|
+
return serializedElements(document).map((element) => {
|
|
1557
|
+
const state = element && states.get(element);
|
|
1558
|
+
return (state && restLayerOf(graph, styles, state)) ?? null;
|
|
1559
|
+
});
|
|
1560
|
+
}
|
|
1561
|
+
/**
|
|
1562
|
+
* Layers as indented HTML with a readable class per layer and one stylesheet, as the generated
|
|
1563
|
+
* components draw them, or with Tailwind utilities in place of the classes and no stylesheet.
|
|
1564
|
+
* Images are files under `assetBasePath` rather than inlined.
|
|
1565
|
+
*/
|
|
1566
|
+
async function layerMarkup(graph, layerIds, options = {}) {
|
|
1567
|
+
const taken = /* @__PURE__ */ new Set();
|
|
1568
|
+
const html = [];
|
|
1569
|
+
const layers = [];
|
|
1570
|
+
const css = [];
|
|
1571
|
+
for (const layerId of layerIds) {
|
|
1572
|
+
const layer = graph.getNode(layerId);
|
|
1573
|
+
const styles = layer && stateStyles(graph, layer, options);
|
|
1574
|
+
if (!layer || !styles) continue;
|
|
1575
|
+
const name = claimName(styles.name, taken, { separator: " " });
|
|
1576
|
+
const named = {
|
|
1577
|
+
...styles,
|
|
1578
|
+
name
|
|
1579
|
+
};
|
|
1580
|
+
const sheet = options.styling === "tailwind" ? {
|
|
1581
|
+
document: stateStylesToTailwind(named),
|
|
1582
|
+
css: ""
|
|
1583
|
+
} : await stateStylesToCSS(named);
|
|
1584
|
+
html.push(serializeHTML(sheet.document, { indent: true }));
|
|
1585
|
+
layers.push(...markupLayers(graph, named, sheet.document));
|
|
1586
|
+
if (sheet.css) css.push(sheet.css);
|
|
1587
|
+
}
|
|
1588
|
+
const { html: page, files } = extractImageAssets(html.join("\n\n"), options.assetBasePath ?? "assets");
|
|
1589
|
+
return {
|
|
1590
|
+
html: page,
|
|
1591
|
+
layerIds: layers,
|
|
1592
|
+
css: css.join("\n"),
|
|
1593
|
+
images: files
|
|
1594
|
+
};
|
|
1595
|
+
}
|
|
1596
|
+
//#endregion
|
|
1597
|
+
export { COMPONENT_STYLINGS, LAYER_COMPONENT_FRAMEWORKS, SHADER_UTILITIES, componentModel, layerComponents, layerMarkup, reactComponent, shaderEffectAttributes, vueComponent };
|
|
1598
|
+
|
|
1599
|
+
//# sourceMappingURL=layers.js.map
|