view-contracts 0.5.12 → 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.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Contract-first UI design toolchain. Compiles restricted view-contract TSX to language-neutral View IR and renders platform targets (React, SwiftUI, Compose).
4
4
 
5
- **Version:** 0.5.12
5
+ **Version:** 0.5.13
6
6
 
7
7
  ## Table of Contents
8
8
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "view-contracts",
3
- "version": "0.5.12",
3
+ "version": "0.5.13",
4
4
  "description": "Contract-first UI design toolchain — restricted TSX to View IR to platform renderers",
5
5
  "type": "module",
6
6
  "bin": {
@@ -0,0 +1,4 @@
1
+ /** HTML attribute name for a `data={{ ... }}` key: `testId` → `data-test-id`. */
2
+ export function dataAttributeName(key: string): string {
3
+ return `data-${key.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()}`;
4
+ }
@@ -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} ${disabled} ${changeAttr} />
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} ${disabled} ${changeAttr} />
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} ${disabled} ${changeAttr} />
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.ariaLabel) attrs.push(`aria-label={${propExpr(props, 'ariaLabel')}}`);
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.ariaLabel) attrs.push(`aria-label={${propExpr(props, 'ariaLabel')}}`);
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}