view-contracts 0.5.11 → 0.5.13
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/cli-contract.yaml +1 -1
- package/dist/preview.mjs +148 -140
- package/dist/preview.mjs.map +4 -4
- package/dist/types/renderers/compose/lowering/lowerToCompose.d.ts +2 -1
- package/dist/types/renderers/react/dataAttributes.d.ts +2 -0
- package/dist/types/renderers/swiftui/lowering/lowerToSwiftUI.d.ts +2 -1
- package/dist/types/src/compiler/jsxToIr.d.ts +2 -1
- package/dist/types/src/compiler/viewPartial.d.ts +3 -1
- package/dist/types/src/config.d.ts +1 -3
- package/dist/types/src/dsl/types.d.ts +2 -3
- package/dist/types/src/ir/types.d.ts +8 -0
- package/dist/types/src/lib/projectPath.d.ts +2 -0
- package/dist/types/src/lib/rendererPaths.d.ts +2 -0
- package/dist/types/src/renderer/native/optionalGate.d.ts +33 -0
- package/dist/types/src/scaffold/standalone.d.ts +2 -2
- package/dist/types/src/schema/dsl-catalog.d.ts +1 -0
- package/dist/types/src/schema/loadSchema.d.ts +7 -0
- package/dist/types/src/schema/types.d.ts +31 -0
- package/dist/types/src/validate/validateViewProps.d.ts +0 -3
- package/dist/view-contracts.bundle.mjs +214 -200
- package/dist/view-contracts.bundle.mjs.map +4 -4
- package/docs/cli-reference.md +1 -1
- package/package.json +1 -1
- package/renderers/compose/README.md +1 -0
- package/renderers/compose/lowering/lowerToCompose.ts +54 -25
- package/renderers/compose/renderComponents.ts +6 -18
- package/renderers/react/dataAttributes.ts +4 -0
- package/renderers/react/lowering/lowerToJsx.ts +45 -11
- package/renderers/react/preview/vocabulary.tsx +7 -3
- package/renderers/react/renderComponents.ts +2 -8
- package/renderers/swiftui/README.md +1 -0
- package/renderers/swiftui/lowering/lowerToSwiftUI.ts +34 -13
- package/renderers/swiftui/renderComponents.ts +6 -18
- package/spec/ui-dsl.schema.json +263 -0
- package/src/dsl/types.ts +2 -3
- package/renderers/react/templates/handlers.ts.hbs +0 -4
package/docs/cli-reference.md
CHANGED
package/package.json
CHANGED
|
@@ -7,6 +7,7 @@ Jetpack Compose renderer implemented as a SwiftUI-parity lowering path:
|
|
|
7
7
|
- Modifier synthesis in `lowering/modifiers.ts`.
|
|
8
8
|
- Component emitter in `renderComponents.ts`.
|
|
9
9
|
- Runtime emitter (screens, partials, theme, sample JSON, dispatch stub) in `renderRuntime.ts`.
|
|
10
|
+
- Partial props become function parameters; an optional prop (`x?: T`) is `x: T? = null`, and a node that renders it is emitted inside `if (x != null) { ... }`.
|
|
10
11
|
|
|
11
12
|
- Maps: `elements.yaml`, `properties.yaml` (hand-maintained under this directory)
|
|
12
13
|
- Routes: `routes.yaml`
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { LOWERING_STYLE_PROPERTIES } from '../../../src/schema/dsl-catalog.js';
|
|
2
2
|
import type { ThemeIR, TokenIR } from '../../../src/design/types.js';
|
|
3
3
|
import { mergeVisualProps, type VisualPropSet } from '../../../src/renderer/style/mergeVisualProps.js';
|
|
4
|
+
import {
|
|
5
|
+
bindingScopes, gateOptional, indentBlock, recordEmittedScope, renderUngated, type OptionalGateState,
|
|
6
|
+
} from '../../../src/renderer/native/optionalGate.js';
|
|
4
7
|
import type {
|
|
5
8
|
IrBinding,
|
|
6
9
|
IrComponentNode,
|
|
@@ -39,7 +42,7 @@ import {
|
|
|
39
42
|
visualPropSetToModifiers,
|
|
40
43
|
} from './modifiers.js';
|
|
41
44
|
|
|
42
|
-
export interface ComposeRenderContext {
|
|
45
|
+
export interface ComposeRenderContext extends OptionalGateState {
|
|
43
46
|
map: ComposeElementMap;
|
|
44
47
|
theme: ThemeIR;
|
|
45
48
|
tokens: TokenIR;
|
|
@@ -223,7 +226,7 @@ function renderGridEqualColumns(
|
|
|
223
226
|
const mapNode = node.children[0] as IrMapNode;
|
|
224
227
|
const item = mapNode.item;
|
|
225
228
|
const source = bindingExpr(mapNode.source, ctx, itemScope);
|
|
226
|
-
const body = renderNested(mapNode.body, level + 3, item);
|
|
229
|
+
const body = renderUngated(ctx, [item], () => renderNested(mapNode.body, level + 3, item));
|
|
227
230
|
return [
|
|
228
231
|
`${indent(level)}Row(modifier = ${modifierExpr}, horizontalArrangement = Arrangement.spacedBy(${gap}), verticalAlignment = Alignment.Top) {`,
|
|
229
232
|
`${indent(level + 1)}for (${item} in ${source}) {`,
|
|
@@ -238,7 +241,7 @@ function renderGridEqualColumns(
|
|
|
238
241
|
let content = '';
|
|
239
242
|
if (child.kind === 'map') content = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
240
243
|
else if (child.kind === 'text') content = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
241
|
-
else if (child.kind === 'binding') content =
|
|
244
|
+
else if (child.kind === 'binding') content = renderBindingText(child, level + 2, ctx, itemScope);
|
|
242
245
|
else content = renderNested(child, level + 2, itemScope);
|
|
243
246
|
if (!content) return '';
|
|
244
247
|
return `${indent(level + 1)}Box(modifier = Modifier.weight(1f).fillMaxSize(), propagateMinConstraints = true, contentAlignment = Alignment.TopStart) {\n${content}\n${indent(level + 1)}}`;
|
|
@@ -263,7 +266,7 @@ function renderGridChildBlock(
|
|
|
263
266
|
}
|
|
264
267
|
let rendered = '';
|
|
265
268
|
if (child.kind === 'text') rendered = `${indent(level + 1)}Text(text = ${JSON.stringify((child as IrTextNode).value)}, modifier = Modifier.fillMaxWidth())`;
|
|
266
|
-
else if (child.kind === 'binding') rendered =
|
|
269
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 1, ctx, itemScope, ', modifier = Modifier.fillMaxWidth()');
|
|
267
270
|
else rendered = renderNested(child, level + 1, itemScope);
|
|
268
271
|
if (!rendered) return '';
|
|
269
272
|
if (child.kind !== 'text' && child.kind !== 'binding') {
|
|
@@ -295,7 +298,7 @@ function renderGridFixedColumns(
|
|
|
295
298
|
let rendered = '';
|
|
296
299
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
297
300
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
298
|
-
else if (child.kind === 'binding') rendered =
|
|
301
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
299
302
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
300
303
|
if (!rendered) continue;
|
|
301
304
|
const bgColor = resolveBackgroundColor(child, ctx);
|
|
@@ -311,7 +314,7 @@ function renderGridFixedColumns(
|
|
|
311
314
|
let rendered = '';
|
|
312
315
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
313
316
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
314
|
-
else if (child.kind === 'binding') rendered =
|
|
317
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
315
318
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
316
319
|
ctx.inVerticalScroll = prevScroll;
|
|
317
320
|
if (!rendered) continue;
|
|
@@ -326,7 +329,7 @@ function renderGridFixedColumns(
|
|
|
326
329
|
let rendered = '';
|
|
327
330
|
if (child.kind === 'map') rendered = renderMap(child, level + 2, renderNested, ctx, 'none');
|
|
328
331
|
else if (child.kind === 'text') rendered = `${indent(level + 2)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
329
|
-
else if (child.kind === 'binding') rendered =
|
|
332
|
+
else if (child.kind === 'binding') rendered = renderBindingText(child, level + 2, ctx, itemScope);
|
|
330
333
|
else rendered = renderNested(child, level + 2, itemScope);
|
|
331
334
|
if (!rendered) continue;
|
|
332
335
|
parts.push(rendered);
|
|
@@ -340,18 +343,40 @@ function renderGridFixedColumns(
|
|
|
340
343
|
}
|
|
341
344
|
|
|
342
345
|
function bindingExpr(binding: IrBinding, ctx: ComposeRenderContext, itemScope?: string): string {
|
|
346
|
+
recordEmittedScope(ctx, binding.scope);
|
|
343
347
|
const root = binding.scope ?? itemScope ?? ctx.propsRoot ?? 'vm';
|
|
344
348
|
return binding.path ? `${root}.${binding.path}` : root;
|
|
345
349
|
}
|
|
346
350
|
|
|
347
|
-
|
|
351
|
+
/** Makes `inner` render only when `names` are non-null; Kotlin smart-casts them inside. */
|
|
352
|
+
function unwrapIn(level: number): (names: string[], inner: string) => string {
|
|
353
|
+
return (names, inner) => {
|
|
354
|
+
const condition = names.map((name) => `${name} != null`).join(' && ');
|
|
355
|
+
return `${indent(level)}if (${condition}) {\n${indentBlock(inner, indent(1))}\n${indent(level)}}`;
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function renderBindingText(
|
|
360
|
+
binding: IrBinding,
|
|
361
|
+
level: number,
|
|
362
|
+
ctx: ComposeRenderContext,
|
|
363
|
+
itemScope?: string,
|
|
364
|
+
extraArgs = '',
|
|
365
|
+
): string {
|
|
366
|
+
return gateOptional(
|
|
367
|
+
ctx,
|
|
368
|
+
bindingScopes([binding]),
|
|
369
|
+
() => `${indent(level)}Text(text = ${bindingExpr(binding, ctx, itemScope)}${extraArgs})`,
|
|
370
|
+
unwrapIn(level),
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
function resolveComposeValue(props: Record<string, IrValue>, ctx: ComposeRenderContext, itemScope?: string): string {
|
|
348
375
|
const valueRaw = props.value;
|
|
349
376
|
if (!valueRaw) return '""';
|
|
350
377
|
if (typeof valueRaw === 'string') return JSON.stringify(valueRaw);
|
|
351
378
|
if (typeof valueRaw === 'object' && valueRaw !== null && 'kind' in valueRaw && valueRaw.kind === 'binding') {
|
|
352
|
-
|
|
353
|
-
const root = binding.scope ?? (itemScope ?? 'vm');
|
|
354
|
-
return binding.path ? `${root}.${binding.path}` : root;
|
|
379
|
+
return bindingExpr(valueRaw, ctx, itemScope);
|
|
355
380
|
}
|
|
356
381
|
return '""';
|
|
357
382
|
}
|
|
@@ -603,7 +628,7 @@ function renderChildren(
|
|
|
603
628
|
const parts = children.map((child) => {
|
|
604
629
|
if (child.kind === 'map') return renderMap(child, level, renderNested, ctx, listScope);
|
|
605
630
|
if (child.kind === 'text') return `${indent(level)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
606
|
-
if (child.kind === 'binding') return
|
|
631
|
+
if (child.kind === 'binding') return renderBindingText(child, level, ctx, itemScope);
|
|
607
632
|
return renderNested(child, level, itemScope);
|
|
608
633
|
}).filter(Boolean);
|
|
609
634
|
return parts.join('\n');
|
|
@@ -622,7 +647,7 @@ function renderLazyGridChildren(
|
|
|
622
647
|
return renderMap(child, level, renderNested, ctx, 'lazyGrid');
|
|
623
648
|
}
|
|
624
649
|
if (child.kind === 'text') content = `${indent(level + 1)}Text(text = ${JSON.stringify((child as IrTextNode).value)})`;
|
|
625
|
-
else if (child.kind === 'binding') content =
|
|
650
|
+
else if (child.kind === 'binding') content = renderBindingText(child, level + 1, ctx, itemScope);
|
|
626
651
|
else content = renderNested(child, level + 1, itemScope);
|
|
627
652
|
if (!content) return '';
|
|
628
653
|
return `${indent(level)}item {\n${content}\n${indent(level)}}`;
|
|
@@ -639,7 +664,7 @@ function renderMap(
|
|
|
639
664
|
): string {
|
|
640
665
|
const source = bindingExpr(node.source, ctx);
|
|
641
666
|
const item = node.item;
|
|
642
|
-
const body = renderNested(node.body, level + 2, item);
|
|
667
|
+
const body = renderUngated(ctx, [item], () => renderNested(node.body, level + 2, item));
|
|
643
668
|
if (listScope === 'lazyGrid') {
|
|
644
669
|
return `${indent(level)}gridItems(${source}, key = { it.id }) { ${item} ->\n${indent(level + 1)}Box(modifier = Modifier.fillMaxWidth()) {\n${body}\n${indent(level + 1)}}\n${indent(level)}}`;
|
|
645
670
|
}
|
|
@@ -650,13 +675,14 @@ function renderMap(
|
|
|
650
675
|
}
|
|
651
676
|
|
|
652
677
|
function renderViewRef(node: IrViewRefNode, level: number, itemScope: string | undefined, ctx: ComposeRenderContext): string {
|
|
653
|
-
|
|
678
|
+
// A view() partial declares its own optional props, so optional values pass through ungated.
|
|
679
|
+
const props = renderUngated(ctx, [], () => Object.entries(node.props).map(([key, value]) => {
|
|
654
680
|
if (typeof value === 'object' && value !== null && 'kind' in value && value.kind === 'binding') {
|
|
655
681
|
return `${key} = ${bindingExpr(value as IrBinding, ctx, itemScope)}`;
|
|
656
682
|
}
|
|
657
683
|
return `${key} = ${JSON.stringify(value)}`;
|
|
658
|
-
});
|
|
659
|
-
return `${indent(level)}${node.name}(${props
|
|
684
|
+
}).join(', '));
|
|
685
|
+
return `${indent(level)}${node.name}(${props})`;
|
|
660
686
|
}
|
|
661
687
|
|
|
662
688
|
function renderVcButton(
|
|
@@ -728,7 +754,7 @@ function renderLeaf(
|
|
|
728
754
|
const textStyleLine = textStyleParts.length > 0
|
|
729
755
|
? `${pad} textStyle = TextStyle(${textStyleParts.join(', ')}),`
|
|
730
756
|
: `${pad} textStyle = TextStyle(color = ${textColor}, fontSize = 14.sp),`;
|
|
731
|
-
const composeVal = resolveComposeValue(node.props, itemScope);
|
|
757
|
+
const composeVal = resolveComposeValue(node.props, ctx, itemScope);
|
|
732
758
|
return [
|
|
733
759
|
`${pad}BasicTextField(`,
|
|
734
760
|
`${pad} value = ${composeVal},`,
|
|
@@ -759,7 +785,7 @@ function renderLeaf(
|
|
|
759
785
|
const containerExpr = applyModifiers('Modifier', dedupeModifiers(containerMods));
|
|
760
786
|
const textStyleParts = extractTextStyleModifiers(textMods);
|
|
761
787
|
const textStyleSuffix = textStyleParts.length > 0 ? `, textStyle = TextStyle(${textStyleParts.join(', ')})` : '';
|
|
762
|
-
const composeVal = resolveComposeValue(node.props, itemScope);
|
|
788
|
+
const composeVal = resolveComposeValue(node.props, ctx, itemScope);
|
|
763
789
|
return [
|
|
764
790
|
`${pad}Box(modifier = ${containerExpr}.heightIn(min = ${minHeight}.dp)) {`,
|
|
765
791
|
`${pad} BasicTextField(`,
|
|
@@ -970,7 +996,7 @@ function renderContainer(
|
|
|
970
996
|
const mapNode = child as IrMapNode;
|
|
971
997
|
const source = bindingExpr(mapNode.source, ctx);
|
|
972
998
|
const item = mapNode.item;
|
|
973
|
-
const mapBody = renderNested(mapNode.body, level + 3, item);
|
|
999
|
+
const mapBody = renderUngated(ctx, [item], () => renderNested(mapNode.body, level + 3, item));
|
|
974
1000
|
return [
|
|
975
1001
|
`${indent(level + 1)}for (${item} in ${source}) {`,
|
|
976
1002
|
`${indent(level + 2)}${divLine.trim()}`,
|
|
@@ -1030,14 +1056,14 @@ function renderContainer(
|
|
|
1030
1056
|
);
|
|
1031
1057
|
}
|
|
1032
1058
|
if (child.kind === 'binding') {
|
|
1033
|
-
return formatTextComposable(
|
|
1059
|
+
return gateOptional(ctx, bindingScopes([child]), () => formatTextComposable(
|
|
1034
1060
|
indent(level + 1),
|
|
1035
|
-
bindingExpr(child
|
|
1061
|
+
bindingExpr(child, ctx, itemScope),
|
|
1036
1062
|
dedupeModifiers([...cellTextBehaviorMods(), `@textAlign(${textAlign})`, ...paddingMods, ...inheritedMods.map((m) => m)]),
|
|
1037
1063
|
ctx,
|
|
1038
1064
|
undefined,
|
|
1039
1065
|
itemScope,
|
|
1040
|
-
);
|
|
1066
|
+
), unwrapIn(level + 1));
|
|
1041
1067
|
}
|
|
1042
1068
|
return renderNested(child, level + 1, itemScope);
|
|
1043
1069
|
}).filter(Boolean).join('\n');
|
|
@@ -1101,10 +1127,13 @@ export function createComposeNodeRenderer(ctx: ComposeRenderContext): NodeRender
|
|
|
1101
1127
|
const renderNested: NodeRenderer = (node, level, itemScope) => {
|
|
1102
1128
|
if (node.kind === 'viewRef') return renderViewRef(node, level, itemScope, ctx);
|
|
1103
1129
|
if (node.kind !== 'component') return '';
|
|
1104
|
-
|
|
1130
|
+
const entry = ctx.map.elements[node.name];
|
|
1131
|
+
if (!entry) {
|
|
1105
1132
|
throw new Error(`Element "${node.name}" is not in Compose element map`);
|
|
1106
1133
|
}
|
|
1107
|
-
|
|
1134
|
+
const inlinedChildren = entry.leaf ? node.children.filter((child): child is IrBinding => child.kind === 'binding') : [];
|
|
1135
|
+
const scopes = bindingScopes([...Object.values(node.props), ...inlinedChildren]);
|
|
1136
|
+
return gateOptional(ctx, scopes, () => renderComponent(node, entry, level, itemScope, renderNested, ctx), unwrapIn(level));
|
|
1108
1137
|
};
|
|
1109
1138
|
return renderNested;
|
|
1110
1139
|
}
|
|
@@ -21,26 +21,13 @@ function tsTypeToKotlin(type: string): string {
|
|
|
21
21
|
|
|
22
22
|
function partialPropsSignature(doc: ViewIrDocument): string {
|
|
23
23
|
if (!doc.propsType) return '';
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
const colon = part.indexOf(':');
|
|
29
|
-
const name = colon >= 0 ? part.slice(0, colon).trim() : part.trim();
|
|
30
|
-
const rawType = colon >= 0 ? part.slice(colon + 1).trim() : 'Any';
|
|
31
|
-
const type = tsTypeToKotlin(rawType);
|
|
32
|
-
return `${name}: ${type}`;
|
|
24
|
+
if (!doc.propDecls) return `props: ${doc.propsType}`;
|
|
25
|
+
return doc.propDecls.map((decl) => {
|
|
26
|
+
const type = tsTypeToKotlin(decl.type);
|
|
27
|
+
return decl.optional ? `${decl.name}: ${type}? = null` : `${decl.name}: ${type}`;
|
|
33
28
|
}).join(', ');
|
|
34
29
|
}
|
|
35
30
|
|
|
36
|
-
function propsRootFromDoc(doc: ViewIrDocument): string | undefined {
|
|
37
|
-
if (!doc.propsType) return undefined;
|
|
38
|
-
const match = doc.propsType.match(/^\{\s*([^}]+)\s*\}$/);
|
|
39
|
-
if (!match) return undefined;
|
|
40
|
-
const first = match[1].split(',')[0]!.split(':')[0]!.trim();
|
|
41
|
-
return first;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
31
|
export async function renderComposeComponent(
|
|
45
32
|
doc: ViewIrDocument,
|
|
46
33
|
options: RenderComposeOptions,
|
|
@@ -54,7 +41,8 @@ export async function renderComposeComponent(
|
|
|
54
41
|
theme: options.theme,
|
|
55
42
|
tokens: options.tokens,
|
|
56
43
|
allowlistExtra,
|
|
57
|
-
propsRoot:
|
|
44
|
+
propsRoot: doc.propDecls?.[0]?.name,
|
|
45
|
+
optionalProps: new Set(doc.propDecls?.filter((decl) => decl.optional).map((decl) => decl.name)),
|
|
58
46
|
};
|
|
59
47
|
|
|
60
48
|
const body = lowerIrToComposeBody(doc.root, ctx, 2);
|
|
@@ -13,6 +13,7 @@ import type {
|
|
|
13
13
|
IrValue,
|
|
14
14
|
IrViewRefNode,
|
|
15
15
|
} from '../../../src/ir/types.js';
|
|
16
|
+
import { dataAttributeName } from '../dataAttributes.js';
|
|
16
17
|
|
|
17
18
|
function indent(level: number): string {
|
|
18
19
|
return ' '.repeat(level);
|
|
@@ -92,6 +93,30 @@ function propExpr(props: Record<string, IrValue>, key: string): string | undefin
|
|
|
92
93
|
return renderValue(value, 0);
|
|
93
94
|
}
|
|
94
95
|
|
|
96
|
+
/**
|
|
97
|
+
* `data={{ key: value }}` → `data-key={value}` attributes. `ownAttributes` are data attributes the
|
|
98
|
+
* element's lowering emits itself; a key that would duplicate one is an error.
|
|
99
|
+
*/
|
|
100
|
+
function dataAttrs(elementName: string, props: Record<string, IrValue>, ownAttributes: string[] = []): string[] {
|
|
101
|
+
const data = props.data;
|
|
102
|
+
if (typeof data !== 'object' || data === null || !('kind' in data) || data.kind !== 'literal') return [];
|
|
103
|
+
return Object.entries((data as IrLiteral).value as Record<string, IrValue>).map(([key, value]) => {
|
|
104
|
+
const attribute = dataAttributeName(key);
|
|
105
|
+
if (ownAttributes.includes(attribute)) {
|
|
106
|
+
throw new Error(`<${elementName}> data key "${key}" duplicates the ${attribute} attribute this element renders`);
|
|
107
|
+
}
|
|
108
|
+
return `${attribute}={${renderValue(value, 0)}}`;
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/** `aria-<name>` present only while the prop is truthy. */
|
|
113
|
+
function ariaFlagAttr(props: Record<string, IrValue>, key: string, ariaName: string): string | undefined {
|
|
114
|
+
if (props[key] === undefined || props[key] === null) return undefined;
|
|
115
|
+
const literal = propLiteral(props, key);
|
|
116
|
+
if (literal !== undefined) return literal ? `${ariaName}="true"` : undefined;
|
|
117
|
+
return `${ariaName}={${propExpr(props, key)} ? true : undefined}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
95
120
|
function pushStyleAttr(attrs: string[], entry: ElementMapEntry, props: Record<string, IrValue>): void {
|
|
96
121
|
const style = styleAttrForElement(entry, props);
|
|
97
122
|
if (style) attrs.push(style);
|
|
@@ -166,14 +191,14 @@ function classNameAttr(
|
|
|
166
191
|
return `className="${base}"`;
|
|
167
192
|
}
|
|
168
193
|
|
|
169
|
-
function emitFieldWrap(inner: string, props: Record<string, IrValue>, level: number): string {
|
|
194
|
+
function emitFieldWrap(inner: string, props: Record<string, IrValue>, level: number, rootAttrs: string[] = []): string {
|
|
170
195
|
const labelLit = propLiteral(props, 'label');
|
|
171
196
|
const helpLit = propLiteral(props, 'help');
|
|
172
197
|
const errorLit = propLiteral(props, 'error');
|
|
173
198
|
const label = propExpr(props, 'label');
|
|
174
199
|
const help = propExpr(props, 'help');
|
|
175
200
|
const error = propExpr(props, 'error');
|
|
176
|
-
const lines = [`${indent(level)}<label className="vc-field">`];
|
|
201
|
+
const lines = [`${indent(level)}<label ${['className="vc-field"', ...rootAttrs].join(' ')}>`];
|
|
177
202
|
if (typeof labelLit === 'string') {
|
|
178
203
|
lines.push(`${indent(level + 1)}<span className="vc-text vc-text--field-label">{${JSON.stringify(labelLit)}}</span>`);
|
|
179
204
|
} else if (label) {
|
|
@@ -195,7 +220,7 @@ function emitFieldWrap(inner: string, props: Record<string, IrValue>, level: num
|
|
|
195
220
|
}
|
|
196
221
|
|
|
197
222
|
function emitCheckbox(props: Record<string, IrValue>, level: number): string {
|
|
198
|
-
const name = propExpr(props, 'name') ?? '""';
|
|
223
|
+
const name = propExpr(props, 'binding') ?? propExpr(props, 'name') ?? '""';
|
|
199
224
|
const label = propExpr(props, 'label') ?? '""';
|
|
200
225
|
const disabled = props.enabled ? `disabled={${propExpr(props, 'enabled')} === false}` : '';
|
|
201
226
|
const changeAttr = onChangeHandler(props, 'e.target.checked') ?? '';
|
|
@@ -203,7 +228,7 @@ function emitCheckbox(props: Record<string, IrValue>, level: number): string {
|
|
|
203
228
|
? (changeAttr ? `checked={${propExpr(props, 'checked')}}` : `defaultChecked={${propExpr(props, 'checked')}}`)
|
|
204
229
|
: '';
|
|
205
230
|
return `${indent(level)}<label className="vc-checkbox">
|
|
206
|
-
${indent(level + 1)}<input type="checkbox" name={${name}} ${checkedAttr
|
|
231
|
+
${indent(level + 1)}<input type="checkbox" name={${name}} ${[checkedAttr, disabled, changeAttr, ...dataAttrs('Checkbox', props)].join(' ')} />
|
|
207
232
|
${indent(level + 1)}<span>{${label}}</span>
|
|
208
233
|
${indent(level)}</label>`;
|
|
209
234
|
}
|
|
@@ -218,7 +243,7 @@ function emitRadio(props: Record<string, IrValue>, level: number): string {
|
|
|
218
243
|
? (changeAttr ? `checked={${propExpr(props, 'checked')}}` : `defaultChecked={${propExpr(props, 'checked')}}`)
|
|
219
244
|
: '';
|
|
220
245
|
return `${indent(level)}<label className="vc-radio">
|
|
221
|
-
${indent(level + 1)}<input type="radio" name={${name}} value={${value}} ${checkedAttr
|
|
246
|
+
${indent(level + 1)}<input type="radio" name={${name}} value={${value}} ${[checkedAttr, disabled, changeAttr, ...dataAttrs('Radio', props)].join(' ')} />
|
|
222
247
|
${indent(level + 1)}<span>{${label}}</span>
|
|
223
248
|
${indent(level)}</label>`;
|
|
224
249
|
}
|
|
@@ -232,7 +257,7 @@ function emitSwitch(props: Record<string, IrValue>, level: number): string {
|
|
|
232
257
|
? (changeAttr ? `checked={${propExpr(props, 'checked')}}` : `defaultChecked={${propExpr(props, 'checked')}}`)
|
|
233
258
|
: '';
|
|
234
259
|
return `${indent(level)}<label className="vc-switch">
|
|
235
|
-
${indent(level + 1)}<input type="checkbox" role="switch" name={${name}} ${checkedAttr
|
|
260
|
+
${indent(level + 1)}<input type="checkbox" role="switch" name={${name}} ${[checkedAttr, disabled, changeAttr, ...dataAttrs('Switch', props)].join(' ')} />
|
|
236
261
|
${indent(level + 1)}<span>{${label}}</span>
|
|
237
262
|
${indent(level)}</label>`;
|
|
238
263
|
}
|
|
@@ -248,6 +273,7 @@ function emitIcon(node: IrComponentNode, entry: ElementMapEntry, level: number):
|
|
|
248
273
|
];
|
|
249
274
|
pushStyleAttr(attrs, entry, props);
|
|
250
275
|
if (props.id) attrs.push(`id={${propExpr(props, 'id')}}`);
|
|
276
|
+
attrs.push(...dataAttrs(node.name, props));
|
|
251
277
|
return `${indent(level)}<img ${attrs.join(' ')} />`;
|
|
252
278
|
}
|
|
253
279
|
|
|
@@ -257,6 +283,7 @@ function emitIcon(node: IrComponentNode, entry: ElementMapEntry, level: number):
|
|
|
257
283
|
];
|
|
258
284
|
pushStyleAttr(attrs, entry, props);
|
|
259
285
|
if (props.id) attrs.push(`id={${propExpr(props, 'id')}}`);
|
|
286
|
+
attrs.push(...dataAttrs(node.name, props, props.name ? ['data-icon'] : []));
|
|
260
287
|
if (props.name) attrs.push(`data-icon={${propExpr(props, 'name')}}`);
|
|
261
288
|
return `${indent(level)}<span ${attrs.join(' ')} />`;
|
|
262
289
|
}
|
|
@@ -282,13 +309,15 @@ function emitVoidInput(
|
|
|
282
309
|
pushStyleAttr(attrs, entry, props);
|
|
283
310
|
if (entry.ariaHidden) attrs.push('aria-hidden');
|
|
284
311
|
if (props.id) attrs.push(`id={${propExpr(props, 'id')}}`);
|
|
312
|
+
attrs.push(...dataAttrs(elementName, props));
|
|
285
313
|
if (props.role) attrs.push(`role={${propExpr(props, 'role')}}`);
|
|
286
314
|
else if (entry.defaultRole) attrs.push(`role="${entry.defaultRole}"`);
|
|
287
315
|
if (entry.ariaLabel) {
|
|
288
|
-
if (props.
|
|
289
|
-
else if (props.label) attrs.push(`aria-label={${propExpr(props, 'label')}}`);
|
|
316
|
+
if (props.label) attrs.push(`aria-label={${propExpr(props, 'label')}}`);
|
|
290
317
|
else if (props.title) attrs.push(`aria-label={${propExpr(props, 'title')}}`);
|
|
291
318
|
}
|
|
319
|
+
const invalid = ariaFlagAttr(props, 'error', 'aria-invalid');
|
|
320
|
+
if (invalid) attrs.push(invalid);
|
|
292
321
|
if (props.name) attrs.push(`name={${propExpr(props, 'name')}}`);
|
|
293
322
|
if (props.placeholder) attrs.push(`placeholder={${propExpr(props, 'placeholder')}}`);
|
|
294
323
|
if (entry.inputType) attrs.push(`type={${propExpr(props, 'inputType') ?? JSON.stringify(entry.inputType)}}`);
|
|
@@ -318,19 +347,23 @@ function emitOpenTag(
|
|
|
318
347
|
const attrs: string[] = [];
|
|
319
348
|
if (entry.ariaHidden) attrs.push('aria-hidden');
|
|
320
349
|
if (props.id) attrs.push(`id={${propExpr(props, 'id')}}`);
|
|
350
|
+
attrs.push(...dataAttrs(elementName, props));
|
|
321
351
|
if (props.role) attrs.push(`role={${propExpr(props, 'role')}}`);
|
|
322
352
|
else if (entry.defaultRole) attrs.push(`role="${entry.defaultRole}"`);
|
|
323
353
|
if (entry.ariaLabel) {
|
|
324
|
-
if (props.
|
|
325
|
-
else if (props.label) attrs.push(`aria-label={${propExpr(props, 'label')}}`);
|
|
354
|
+
if (props.label) attrs.push(`aria-label={${propExpr(props, 'label')}}`);
|
|
326
355
|
else if (props.title) attrs.push(`aria-label={${propExpr(props, 'title')}}`);
|
|
327
356
|
}
|
|
328
357
|
if (entry.actionEvent) {
|
|
329
358
|
const handler = actionHandler(props, entry.actionEvent);
|
|
330
359
|
if (handler) attrs.push(handler);
|
|
331
360
|
}
|
|
361
|
+
const current = ariaFlagAttr(props, 'selected', 'aria-current');
|
|
362
|
+
if (current) attrs.push(current);
|
|
332
363
|
if (props.name) attrs.push(`name={${propExpr(props, 'name')}}`);
|
|
333
364
|
if (entry.tag === 'select' || entry.tag === 'textarea' || entry.tag === 'input') {
|
|
365
|
+
const invalid = ariaFlagAttr(props, 'error', 'aria-invalid');
|
|
366
|
+
if (invalid) attrs.push(invalid);
|
|
334
367
|
if (props.required) attrs.push(`required={${propExpr(props, 'required')}}`);
|
|
335
368
|
if (props.enabled) attrs.push(`disabled={${propExpr(props, 'enabled')} === false}`);
|
|
336
369
|
const changeHandler = onChangeHandler(props);
|
|
@@ -425,6 +458,7 @@ function lowerVocabularyElement(
|
|
|
425
458
|
];
|
|
426
459
|
pushStyleAttr(attrs, entry, props);
|
|
427
460
|
if (props.id) attrs.push(`id={${propExpr(props, 'id')}}`);
|
|
461
|
+
attrs.push(...dataAttrs(node.name, props));
|
|
428
462
|
if (props.role) attrs.push(`role={${propExpr(props, 'role')}}`);
|
|
429
463
|
else if (entry.defaultRole) attrs.push(`role="${entry.defaultRole}"`);
|
|
430
464
|
return visibilityGuard(`${indent(level)}<div ${attrs.join(' ')} />`, props, level);
|
|
@@ -432,7 +466,7 @@ function lowerVocabularyElement(
|
|
|
432
466
|
|
|
433
467
|
if (entry.fieldContainer) {
|
|
434
468
|
const inner = childContent || `${indent(level + 1)}<div />`;
|
|
435
|
-
return visibilityGuard(emitFieldWrap(inner, props, level), props, level);
|
|
469
|
+
return visibilityGuard(emitFieldWrap(inner, props, level, dataAttrs(node.name, props)), props, level);
|
|
436
470
|
}
|
|
437
471
|
|
|
438
472
|
if (entry.fieldWrap) {
|
|
@@ -10,6 +10,7 @@ import { isHidden, toneClass } from '../runtime/style.js';
|
|
|
10
10
|
import { foldLiteralsToCss } from '../style/foldLiterals.js';
|
|
11
11
|
import { ELEMENT_CATALOG, LOWERING_STYLE_PROPERTIES } from '../../../src/schema/dsl-catalog.js';
|
|
12
12
|
import type { Action } from '../runtime/types.js';
|
|
13
|
+
import { dataAttributeName } from '../dataAttributes.js';
|
|
13
14
|
|
|
14
15
|
function componentToKebab(name: string): string {
|
|
15
16
|
return name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
|
@@ -18,9 +19,6 @@ function componentToKebab(name: string): string {
|
|
|
18
19
|
function previewClassName(elementName: string, props: Record<string, unknown>): string {
|
|
19
20
|
const kebab = componentToKebab(elementName);
|
|
20
21
|
const parts = [`vc-${kebab}`];
|
|
21
|
-
if (typeof props.level === 'number') {
|
|
22
|
-
parts.push(`vc-${kebab}-${props.level}`);
|
|
23
|
-
}
|
|
24
22
|
const variant = props.variant ?? props.tone;
|
|
25
23
|
if (typeof variant === 'string') {
|
|
26
24
|
parts.push(`vc-${kebab}--${variant}`);
|
|
@@ -29,6 +27,11 @@ function previewClassName(elementName: string, props: Record<string, unknown>):
|
|
|
29
27
|
return parts.join(' ');
|
|
30
28
|
}
|
|
31
29
|
|
|
30
|
+
function previewDataAttributes(data: unknown): Record<string, unknown> {
|
|
31
|
+
if (typeof data !== 'object' || data === null) return {};
|
|
32
|
+
return Object.fromEntries(Object.entries(data).map(([key, value]) => [dataAttributeName(key), value]));
|
|
33
|
+
}
|
|
34
|
+
|
|
32
35
|
function previewStyleProps(props: Record<string, unknown>): Record<string, unknown> {
|
|
33
36
|
const out: Record<string, unknown> = {};
|
|
34
37
|
for (const [key, value] of Object.entries(props)) {
|
|
@@ -56,6 +59,7 @@ function vocabularyStub(elementName: string) {
|
|
|
56
59
|
const inlineStyle = foldLiteralsToCss(previewStyleProps(props));
|
|
57
60
|
return (
|
|
58
61
|
<div
|
|
62
|
+
{...previewDataAttributes(props.data)}
|
|
59
63
|
data-vc-element={elementName}
|
|
60
64
|
className={previewClassName(elementName, props)}
|
|
61
65
|
style={Object.keys(inlineStyle).length > 0 ? inlineStyle : undefined}
|
|
@@ -51,14 +51,8 @@ function extractContractTypeNames(propsType: string | undefined): string[] {
|
|
|
51
51
|
|
|
52
52
|
function partialFunctionParams(doc: ViewIrDocument): string {
|
|
53
53
|
if (!doc.propsType) return '';
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const inner = match[1].trim();
|
|
57
|
-
const propNames = inner
|
|
58
|
-
.split(/[,;]/)
|
|
59
|
-
.map((part) => part.split(':')[0]!.trim())
|
|
60
|
-
.filter(Boolean);
|
|
61
|
-
return `{ ${propNames.join(', ')} }: ${doc.propsType}`;
|
|
54
|
+
if (!doc.propDecls) return `props: ${doc.propsType}`;
|
|
55
|
+
return `{ ${doc.propDecls.map((decl) => decl.name).join(', ')} }: ${doc.propsType}`;
|
|
62
56
|
}
|
|
63
57
|
|
|
64
58
|
/** Root-level `{cond ? ...}` from visibilityGuard is invalid inside `return (...)` — wrap in fragment. */
|
|
@@ -11,6 +11,7 @@ Compiles View IR into native SwiftUI code at build time.
|
|
|
11
11
|
- **Dynamic variants** — `IrConcat` expressions (e.g. `` `badge-${row.statusTone}` ``) are lowered to runtime `VcTheme.resolve()` calls with a pre-built theme dictionary
|
|
12
12
|
- **Table layout** — `Table` / `TableHeader` / `TableRow` / `TableColumn` / `TableCell` with fixed widths, flex columns, and configurable `separator` / `separatorColor`
|
|
13
13
|
- **justify="between"** — `Row` elements with `justify="between"` emit `Spacer()` between children
|
|
14
|
+
- **Partial props** — each prop of a `view<{ ... }>()` partial becomes a stored property; an optional prop (`x?: T`) is `var x: T? = nil`. A node that renders an optional prop is emitted inside `if let x { ... }`, so it appears only when the prop is present
|
|
14
15
|
- **Mock data SSoT** — `mock.yaml` is converted to `SampleData.json` and bundled into the Xcode project; Swift ViewModels decode via `JSONDecoder` (no hand-written sample data)
|
|
15
16
|
|
|
16
17
|
## Files
|