view-contracts 0.5.12 → 0.5.14
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 +154 -154
- package/dist/preview.mjs.map +4 -4
- package/dist/types/renderers/react/dataAttributes.d.ts +2 -0
- package/dist/types/src/compiler/viewPartial.d.ts +1 -1
- package/dist/types/src/config.d.ts +1 -3
- package/dist/types/src/dsl/types.d.ts +2 -3
- package/dist/types/src/generated/cli/program.d.ts +12 -2
- package/dist/types/src/lib/projectPath.d.ts +2 -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 +219 -213
- package/dist/view-contracts.bundle.mjs.map +4 -4
- package/docs/cli-reference.md +1 -1
- package/package.json +6 -6
- package/renderers/react/dataAttributes.ts +4 -0
- package/renderers/react/lowering/lowerToJsx.ts +45 -11
- package/renderers/react/preview/vocabulary.tsx +7 -3
- package/spec/ui-dsl.schema.json +246 -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
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "view-contracts",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.14",
|
|
4
4
|
"description": "Contract-first UI design toolchain — restricted TSX to View IR to platform renderers",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"bin": {
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
"dependencies": {
|
|
117
117
|
"commander": "^14.0.3",
|
|
118
118
|
"esbuild": "^0.28.0",
|
|
119
|
-
"handlebars": "^4.7.
|
|
119
|
+
"handlebars": "^4.7.10",
|
|
120
120
|
"js-yaml": "^4.1.1",
|
|
121
121
|
"micro-contracts": "^0.17.10",
|
|
122
122
|
"react": "^18.0.0 || ^19.0.0",
|
|
@@ -131,13 +131,13 @@
|
|
|
131
131
|
"@types/react-dom": "^19.2.3",
|
|
132
132
|
"@vitejs/plugin-react": "^4.7.0",
|
|
133
133
|
"ajv": "^8.18.0",
|
|
134
|
-
"cli-contracts": "^0.
|
|
134
|
+
"cli-contracts": "^0.35.0",
|
|
135
135
|
"esbuild": "^0.28.0",
|
|
136
|
-
"pixelmatch": "^
|
|
136
|
+
"pixelmatch": "^8.0.0",
|
|
137
137
|
"playwright": "^1.61.0",
|
|
138
138
|
"pngjs": "^7.0.0",
|
|
139
139
|
"tsx": "^4.21.0",
|
|
140
|
-
"vite": "^6.4.
|
|
141
|
-
"vitest": "^4.1.
|
|
140
|
+
"vite": "^6.4.6",
|
|
141
|
+
"vitest": "^4.1.12"
|
|
142
142
|
}
|
|
143
143
|
}
|
|
@@ -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}
|