@colixsystems/widget-sdk 0.309.0 → 0.314.0

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/dist/linter.cjs CHANGED
@@ -2276,6 +2276,30 @@ function _openTagStack() {
2276
2276
  };
2277
2277
  }
2278
2278
 
2279
+ /**
2280
+ * The next opening tag in `structure` from `_JSX_TOKEN_RE`'s position, keeping
2281
+ * `open` in step with the fragments and closing tags passed on the way.
2282
+ */
2283
+ function _nextOpeningTag(structure, open, fragmentFrame) {
2284
+ let token;
2285
+ while ((token = _JSX_TOKEN_RE.exec(structure))) {
2286
+ const [, closing, name] = token;
2287
+ if (!name) {
2288
+ // A fragment, `<>` or `</>`; any other `<` is a comparison.
2289
+ if (structure[token.index + 1 + closing.length] !== ">") continue;
2290
+ if (closing) open.pop();
2291
+ else open.push(fragmentFrame(token));
2292
+ continue;
2293
+ }
2294
+ if (closing) {
2295
+ open.close(name);
2296
+ continue;
2297
+ }
2298
+ return token;
2299
+ }
2300
+ return null;
2301
+ }
2302
+
2279
2303
  function _fieldInBorderedRowRules(source) {
2280
2304
  const findings = [];
2281
2305
  const scan = {
@@ -2310,19 +2334,8 @@ function _fieldInBorderedRowRules(source) {
2310
2334
  const open = _openTagStack();
2311
2335
  _JSX_TOKEN_RE.lastIndex = 0;
2312
2336
  let token;
2313
- while ((token = _JSX_TOKEN_RE.exec(structure))) {
2314
- const [, closing, name] = token;
2315
- if (!name) {
2316
- // A fragment, `<>` or `</>`; any other `<` is a comparison.
2317
- if (structure[token.index + 1 + closing.length] !== ">") continue;
2318
- if (closing) open.pop();
2319
- else open.push(null);
2320
- continue;
2321
- }
2322
- if (closing) {
2323
- open.close(name);
2324
- continue;
2325
- }
2337
+ while ((token = _nextOpeningTag(structure, open, () => null))) {
2338
+ const name = token[2];
2326
2339
  const end = scan.index.tagEnd(token.index);
2327
2340
  if (end === -1) break;
2328
2341
  const tag = { name, attrs: [token.index + token[0].length, end], borderedRow: undefined };
@@ -2386,20 +2399,14 @@ function _gridCellWidthRules(source) {
2386
2399
  const open = _openTagStack();
2387
2400
  _JSX_TOKEN_RE.lastIndex = 0;
2388
2401
  let token;
2389
- while ((token = _JSX_TOKEN_RE.exec(structure))) {
2390
- const [, closing, name] = token;
2402
+ // A fragment is transparent: its children are its parent's children.
2403
+ const fragment = () => {
2404
+ const parent = open.top();
2405
+ return { name: "", narrow: parent && parent.narrow, cell: parent && parent.cell };
2406
+ };
2407
+ while ((token = _nextOpeningTag(structure, open, fragment))) {
2408
+ const name = token[2];
2391
2409
  const parent = open.top();
2392
- if (!name) {
2393
- if (structure[token.index + 1 + closing.length] !== ">") continue;
2394
- // A fragment is transparent: its children are its parent's children.
2395
- if (closing) open.pop();
2396
- else open.push({ name: "", narrow: parent && parent.narrow, cell: parent && parent.cell });
2397
- continue;
2398
- }
2399
- if (closing) {
2400
- open.close(name);
2401
- continue;
2402
- }
2403
2410
  const end = index.tagEnd(token.index);
2404
2411
  if (end === -1) break;
2405
2412
  const tag = { name, narrow: undefined, cell: parent && parent.cell };
@@ -2429,6 +2436,120 @@ function _gridCellWidthRules(source) {
2429
2436
  return findings;
2430
2437
  }
2431
2438
 
2439
+ // sc-16682 — two sibling patterns a production build's widgets shipped.
2440
+ //
2441
+ // An action beside an <EmptyState> sits outside its panel; EmptyState takes
2442
+ // its action as `children`. Flagged when a Button or Pressable is the next
2443
+ // element after an EmptyState, or the first child of the View that is.
2444
+ //
2445
+ // Field labels in two styles: one field given the SDK `label`, another
2446
+ // labelled by a <Text> drawn just before it, so the two labels render in
2447
+ // different type. Only a Text that is the control's previous sibling counts
2448
+ // as its label, which keeps a heading above a whole form out of it.
2449
+ const _EMPTY_STATE_ACTION_TAGS = new Set(["Button", "Pressable"]);
2450
+ const _SELF_LABELLED_FIELD_TAGS = new Set([
2451
+ "TextField",
2452
+ "Select",
2453
+ "RadioGroup",
2454
+ "PeoplePicker",
2455
+ "FileUpload",
2456
+ "FormField",
2457
+ ]);
2458
+ const _LABELLABLE_CONTROL_TAGS = new Set([
2459
+ ..._SELF_LABELLED_FIELD_TAGS,
2460
+ "TextInput",
2461
+ "ChipGroup",
2462
+ "DateTimePicker",
2463
+ ]);
2464
+ const _LABEL_ATTR_RE = /(?:^|\s)label\s*=/;
2465
+
2466
+ function _siblingRules(source) {
2467
+ if (!source.includes("<EmptyState") && !source.includes("label")) return [];
2468
+ const structure = _stripNonCode(source);
2469
+ const index = _jsxScanIndex(structure);
2470
+ const lineAt = _lineCounter(structure);
2471
+ const sourceLines = source.split(/\r?\n/);
2472
+ const findings = [];
2473
+ const report = (rule, at, label) => {
2474
+ const line = lineAt(at);
2475
+ findings.push({
2476
+ rule,
2477
+ severity: "warning",
2478
+ label,
2479
+ line,
2480
+ snippet: (sourceLines[line - 1] || "").trim().slice(0, 200),
2481
+ });
2482
+ };
2483
+ // Not when the two are alternatives (`empty ? <EmptyState /> : <Button />`)
2484
+ // or sit in separate expressions (`{a && <EmptyState />}{b && <Button />}`).
2485
+ const renderedTogether = (emptyState, at) => {
2486
+ let depth = 0;
2487
+ for (let i = emptyState.at; i < at; i++) {
2488
+ const ch = structure[i];
2489
+ if (ch === "{") depth++;
2490
+ else if (ch === "}" && --depth < 0) return false;
2491
+ else if (ch === ":" && depth === 0) return false;
2492
+ }
2493
+ return true;
2494
+ };
2495
+ const afterEmptyState = (tag) =>
2496
+ tag && tag.name === "EmptyState" ? tag : null;
2497
+ let sdkLabelled = null;
2498
+ let ownLabelled = null;
2499
+ const open = _openTagStack();
2500
+ _JSX_TOKEN_RE.lastIndex = 0;
2501
+ let token;
2502
+ const fragment = (at) => ({ name: "", at: at.index, before: null, last: null });
2503
+ while ((token = _nextOpeningTag(structure, open, fragment))) {
2504
+ const name = token[2];
2505
+ // `useState<Item>` and `a<B` are not tags.
2506
+ if (/[\w$)\]]/.test(structure[token.index - 1] || "")) continue;
2507
+ const end = index.tagEnd(token.index);
2508
+ if (end === -1) break;
2509
+ const parent = open.top();
2510
+ const before = parent ? parent.last : null;
2511
+ const tag = { name, at: token.index, before, last: null };
2512
+ if (parent) parent.last = tag;
2513
+ if (_EMPTY_STATE_ACTION_TAGS.has(name) && parent) {
2514
+ const emptyState =
2515
+ afterEmptyState(before) ||
2516
+ (parent.name === "View" && !before ? afterEmptyState(parent.before) : null);
2517
+ if (emptyState && renderedTogether(emptyState, token.index)) {
2518
+ report(
2519
+ "empty-state-action-outside",
2520
+ token.index,
2521
+ `This <${name}> sits beside the <EmptyState>, outside its panel. ` +
2522
+ `EmptyState takes its action as children: ` +
2523
+ `<EmptyState title={…}><Button title={…} onPress={…} /></EmptyState> ` +
2524
+ `draws it inside the panel, under the message.`,
2525
+ );
2526
+ }
2527
+ }
2528
+ if (_LABELLABLE_CONTROL_TAGS.has(name)) {
2529
+ const labelled = _LABEL_ATTR_RE.test(structure.slice(token.index + token[0].length, end));
2530
+ if (labelled && _SELF_LABELLED_FIELD_TAGS.has(name)) {
2531
+ if (!sdkLabelled) sdkLabelled = tag;
2532
+ } else if (!labelled && before && before.name === "Text") {
2533
+ if (!ownLabelled) ownLabelled = { tag, label: before };
2534
+ }
2535
+ }
2536
+ if (!_selfClosing(structure, end)) open.push(tag);
2537
+ _JSX_TOKEN_RE.lastIndex = end + 1;
2538
+ }
2539
+ if (sdkLabelled && ownLabelled) {
2540
+ report(
2541
+ "field-label-styles-mixed",
2542
+ ownLabelled.label.at,
2543
+ `This <Text> labels the <${ownLabelled.tag.name}> after it, while ` +
2544
+ `line ${lineAt(sdkLabelled.at)} gives <${sdkLabelled.name}> the ` +
2545
+ `SDK label, so the form shows labels in two styles. Label every ` +
2546
+ `field one way: the label prop, or <FormField label> round a ` +
2547
+ `control that has none.`,
2548
+ );
2549
+ }
2550
+ return findings;
2551
+ }
2552
+
2432
2553
  // sc-4913 — a measured frame INCLUDES the element's own padding.
2433
2554
  //
2434
2555
  // `onLayout` reports the frame width, and padding sits inside that frame. A
@@ -3264,6 +3385,8 @@ function lintSource(source, options) {
3264
3385
  findings.push(..._fieldInBorderedRowRules(source));
3265
3386
  // sc-15754 — a Grid cell under 200px holding more than one Text is refused.
3266
3387
  findings.push(..._gridCellWidthRules(source));
3388
+ // sc-16682 — soft warnings: an action beside an EmptyState, mixed field labels.
3389
+ findings.push(..._siblingRules(source));
3267
3390
  // sc-4913 — soft warning: a measured width that includes the widget's own
3268
3391
  // padding wraps the last grid column into an empty one.
3269
3392
  findings.push(..._measuredPaddingRules(source));
package/dist/linter.js CHANGED
@@ -2271,6 +2271,30 @@ function _openTagStack() {
2271
2271
  };
2272
2272
  }
2273
2273
 
2274
+ /**
2275
+ * The next opening tag in `structure` from `_JSX_TOKEN_RE`'s position, keeping
2276
+ * `open` in step with the fragments and closing tags passed on the way.
2277
+ */
2278
+ function _nextOpeningTag(structure, open, fragmentFrame) {
2279
+ let token;
2280
+ while ((token = _JSX_TOKEN_RE.exec(structure))) {
2281
+ const [, closing, name] = token;
2282
+ if (!name) {
2283
+ // A fragment, `<>` or `</>`; any other `<` is a comparison.
2284
+ if (structure[token.index + 1 + closing.length] !== ">") continue;
2285
+ if (closing) open.pop();
2286
+ else open.push(fragmentFrame(token));
2287
+ continue;
2288
+ }
2289
+ if (closing) {
2290
+ open.close(name);
2291
+ continue;
2292
+ }
2293
+ return token;
2294
+ }
2295
+ return null;
2296
+ }
2297
+
2274
2298
  function _fieldInBorderedRowRules(source) {
2275
2299
  const findings = [];
2276
2300
  const scan = {
@@ -2305,19 +2329,8 @@ function _fieldInBorderedRowRules(source) {
2305
2329
  const open = _openTagStack();
2306
2330
  _JSX_TOKEN_RE.lastIndex = 0;
2307
2331
  let token;
2308
- while ((token = _JSX_TOKEN_RE.exec(structure))) {
2309
- const [, closing, name] = token;
2310
- if (!name) {
2311
- // A fragment, `<>` or `</>`; any other `<` is a comparison.
2312
- if (structure[token.index + 1 + closing.length] !== ">") continue;
2313
- if (closing) open.pop();
2314
- else open.push(null);
2315
- continue;
2316
- }
2317
- if (closing) {
2318
- open.close(name);
2319
- continue;
2320
- }
2332
+ while ((token = _nextOpeningTag(structure, open, () => null))) {
2333
+ const name = token[2];
2321
2334
  const end = scan.index.tagEnd(token.index);
2322
2335
  if (end === -1) break;
2323
2336
  const tag = { name, attrs: [token.index + token[0].length, end], borderedRow: undefined };
@@ -2381,20 +2394,14 @@ function _gridCellWidthRules(source) {
2381
2394
  const open = _openTagStack();
2382
2395
  _JSX_TOKEN_RE.lastIndex = 0;
2383
2396
  let token;
2384
- while ((token = _JSX_TOKEN_RE.exec(structure))) {
2385
- const [, closing, name] = token;
2397
+ // A fragment is transparent: its children are its parent's children.
2398
+ const fragment = () => {
2399
+ const parent = open.top();
2400
+ return { name: "", narrow: parent && parent.narrow, cell: parent && parent.cell };
2401
+ };
2402
+ while ((token = _nextOpeningTag(structure, open, fragment))) {
2403
+ const name = token[2];
2386
2404
  const parent = open.top();
2387
- if (!name) {
2388
- if (structure[token.index + 1 + closing.length] !== ">") continue;
2389
- // A fragment is transparent: its children are its parent's children.
2390
- if (closing) open.pop();
2391
- else open.push({ name: "", narrow: parent && parent.narrow, cell: parent && parent.cell });
2392
- continue;
2393
- }
2394
- if (closing) {
2395
- open.close(name);
2396
- continue;
2397
- }
2398
2405
  const end = index.tagEnd(token.index);
2399
2406
  if (end === -1) break;
2400
2407
  const tag = { name, narrow: undefined, cell: parent && parent.cell };
@@ -2424,6 +2431,120 @@ function _gridCellWidthRules(source) {
2424
2431
  return findings;
2425
2432
  }
2426
2433
 
2434
+ // sc-16682 — two sibling patterns a production build's widgets shipped.
2435
+ //
2436
+ // An action beside an <EmptyState> sits outside its panel; EmptyState takes
2437
+ // its action as `children`. Flagged when a Button or Pressable is the next
2438
+ // element after an EmptyState, or the first child of the View that is.
2439
+ //
2440
+ // Field labels in two styles: one field given the SDK `label`, another
2441
+ // labelled by a <Text> drawn just before it, so the two labels render in
2442
+ // different type. Only a Text that is the control's previous sibling counts
2443
+ // as its label, which keeps a heading above a whole form out of it.
2444
+ const _EMPTY_STATE_ACTION_TAGS = new Set(["Button", "Pressable"]);
2445
+ const _SELF_LABELLED_FIELD_TAGS = new Set([
2446
+ "TextField",
2447
+ "Select",
2448
+ "RadioGroup",
2449
+ "PeoplePicker",
2450
+ "FileUpload",
2451
+ "FormField",
2452
+ ]);
2453
+ const _LABELLABLE_CONTROL_TAGS = new Set([
2454
+ ..._SELF_LABELLED_FIELD_TAGS,
2455
+ "TextInput",
2456
+ "ChipGroup",
2457
+ "DateTimePicker",
2458
+ ]);
2459
+ const _LABEL_ATTR_RE = /(?:^|\s)label\s*=/;
2460
+
2461
+ function _siblingRules(source) {
2462
+ if (!source.includes("<EmptyState") && !source.includes("label")) return [];
2463
+ const structure = _stripNonCode(source);
2464
+ const index = _jsxScanIndex(structure);
2465
+ const lineAt = _lineCounter(structure);
2466
+ const sourceLines = source.split(/\r?\n/);
2467
+ const findings = [];
2468
+ const report = (rule, at, label) => {
2469
+ const line = lineAt(at);
2470
+ findings.push({
2471
+ rule,
2472
+ severity: "warning",
2473
+ label,
2474
+ line,
2475
+ snippet: (sourceLines[line - 1] || "").trim().slice(0, 200),
2476
+ });
2477
+ };
2478
+ // Not when the two are alternatives (`empty ? <EmptyState /> : <Button />`)
2479
+ // or sit in separate expressions (`{a && <EmptyState />}{b && <Button />}`).
2480
+ const renderedTogether = (emptyState, at) => {
2481
+ let depth = 0;
2482
+ for (let i = emptyState.at; i < at; i++) {
2483
+ const ch = structure[i];
2484
+ if (ch === "{") depth++;
2485
+ else if (ch === "}" && --depth < 0) return false;
2486
+ else if (ch === ":" && depth === 0) return false;
2487
+ }
2488
+ return true;
2489
+ };
2490
+ const afterEmptyState = (tag) =>
2491
+ tag && tag.name === "EmptyState" ? tag : null;
2492
+ let sdkLabelled = null;
2493
+ let ownLabelled = null;
2494
+ const open = _openTagStack();
2495
+ _JSX_TOKEN_RE.lastIndex = 0;
2496
+ let token;
2497
+ const fragment = (at) => ({ name: "", at: at.index, before: null, last: null });
2498
+ while ((token = _nextOpeningTag(structure, open, fragment))) {
2499
+ const name = token[2];
2500
+ // `useState<Item>` and `a<B` are not tags.
2501
+ if (/[\w$)\]]/.test(structure[token.index - 1] || "")) continue;
2502
+ const end = index.tagEnd(token.index);
2503
+ if (end === -1) break;
2504
+ const parent = open.top();
2505
+ const before = parent ? parent.last : null;
2506
+ const tag = { name, at: token.index, before, last: null };
2507
+ if (parent) parent.last = tag;
2508
+ if (_EMPTY_STATE_ACTION_TAGS.has(name) && parent) {
2509
+ const emptyState =
2510
+ afterEmptyState(before) ||
2511
+ (parent.name === "View" && !before ? afterEmptyState(parent.before) : null);
2512
+ if (emptyState && renderedTogether(emptyState, token.index)) {
2513
+ report(
2514
+ "empty-state-action-outside",
2515
+ token.index,
2516
+ `This <${name}> sits beside the <EmptyState>, outside its panel. ` +
2517
+ `EmptyState takes its action as children: ` +
2518
+ `<EmptyState title={…}><Button title={…} onPress={…} /></EmptyState> ` +
2519
+ `draws it inside the panel, under the message.`,
2520
+ );
2521
+ }
2522
+ }
2523
+ if (_LABELLABLE_CONTROL_TAGS.has(name)) {
2524
+ const labelled = _LABEL_ATTR_RE.test(structure.slice(token.index + token[0].length, end));
2525
+ if (labelled && _SELF_LABELLED_FIELD_TAGS.has(name)) {
2526
+ if (!sdkLabelled) sdkLabelled = tag;
2527
+ } else if (!labelled && before && before.name === "Text") {
2528
+ if (!ownLabelled) ownLabelled = { tag, label: before };
2529
+ }
2530
+ }
2531
+ if (!_selfClosing(structure, end)) open.push(tag);
2532
+ _JSX_TOKEN_RE.lastIndex = end + 1;
2533
+ }
2534
+ if (sdkLabelled && ownLabelled) {
2535
+ report(
2536
+ "field-label-styles-mixed",
2537
+ ownLabelled.label.at,
2538
+ `This <Text> labels the <${ownLabelled.tag.name}> after it, while ` +
2539
+ `line ${lineAt(sdkLabelled.at)} gives <${sdkLabelled.name}> the ` +
2540
+ `SDK label, so the form shows labels in two styles. Label every ` +
2541
+ `field one way: the label prop, or <FormField label> round a ` +
2542
+ `control that has none.`,
2543
+ );
2544
+ }
2545
+ return findings;
2546
+ }
2547
+
2427
2548
  // sc-4913 — a measured frame INCLUDES the element's own padding.
2428
2549
  //
2429
2550
  // `onLayout` reports the frame width, and padding sits inside that frame. A
@@ -3259,6 +3380,8 @@ export function lintSource(source, options) {
3259
3380
  findings.push(..._fieldInBorderedRowRules(source));
3260
3381
  // sc-15754 — a Grid cell under 200px holding more than one Text is refused.
3261
3382
  findings.push(..._gridCellWidthRules(source));
3383
+ // sc-16682 — soft warnings: an action beside an EmptyState, mixed field labels.
3384
+ findings.push(..._siblingRules(source));
3262
3385
  // sc-4913 — soft warning: a measured width that includes the widget's own
3263
3386
  // padding wraps the last grid column into an empty one.
3264
3387
  findings.push(..._measuredPaddingRules(source));
@@ -27,12 +27,15 @@ const { isPlainObject } = require("./plain-object.cjs");
27
27
  // must never drop a colour the runner already accepted.
28
28
  const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
29
29
 
30
+ // Trimmed first, so a padded hex in a hand-edited theme survives.
31
+ function coerceThemeColor(value) {
32
+ if (typeof value !== "string") return undefined;
33
+ const trimmed = value.trim();
34
+ return HEX_COLOR.test(trimmed) ? trimmed : undefined;
35
+ }
36
+
30
37
  function coerceToken(def, value) {
31
- if (def.type === "color") {
32
- if (typeof value !== "string") return undefined;
33
- const trimmed = value.trim();
34
- return HEX_COLOR.test(trimmed) ? trimmed : undefined;
35
- }
38
+ if (def.type === "color") return coerceThemeColor(value);
36
39
  if (def.type === "size") {
37
40
  const n = typeof value === "number" ? value : Number(value);
38
41
  if (!Number.isFinite(n)) return undefined;
@@ -397,6 +400,7 @@ function applyThemeComponentStyle(manifestId, theme, props, styleSchema) {
397
400
  return { ...base, style: { ...themed, ...authored } };
398
401
  }
399
402
  module.exports = {
403
+ coerceThemeColor,
400
404
  normaliseThemeComponents,
401
405
  MAX_STYLE_STRING_LENGTH,
402
406
  styleValueBytes,
@@ -22,12 +22,15 @@ import { isPlainObject } from "./plain-object.js";
22
22
  // must never drop a colour the runner already accepted.
23
23
  const HEX_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
24
24
 
25
+ // Trimmed first, so a padded hex in a hand-edited theme survives.
26
+ export function coerceThemeColor(value) {
27
+ if (typeof value !== "string") return undefined;
28
+ const trimmed = value.trim();
29
+ return HEX_COLOR.test(trimmed) ? trimmed : undefined;
30
+ }
31
+
25
32
  function coerceToken(def, value) {
26
- if (def.type === "color") {
27
- if (typeof value !== "string") return undefined;
28
- const trimmed = value.trim();
29
- return HEX_COLOR.test(trimmed) ? trimmed : undefined;
30
- }
33
+ if (def.type === "color") return coerceThemeColor(value);
31
34
  if (def.type === "size") {
32
35
  const n = typeof value === "number" ? value : Number(value);
33
36
  if (!Number.isFinite(n)) return undefined;
@@ -0,0 +1,64 @@
1
+ // GENERATED from theme-text-roles.js by scripts/generate-cjs.js — DO NOT EDIT.
2
+ // Change theme-text-roles.js, then run `npm run generate:cjs` in packages/widget-sdk.
3
+ "use strict";
4
+
5
+ const { CONTRACT } = require("./contract.cjs");
6
+ const { coerceThemeColor } = require("./theme-components.cjs");
7
+
8
+ // REQ-THEME-ROLE: the one typography-role validator for both hosts and the backend.
9
+
10
+ // One stored role value, checked against its declared type and bounds.
11
+ function coerceRoleField(def, value) {
12
+ if (def.type === "color") return coerceThemeColor(value);
13
+ if (def.type === "enum") {
14
+ // A weight may arrive as a number; both hosts' `fontWeight` takes the string.
15
+ if (typeof value !== "string" && typeof value !== "number") return undefined;
16
+ const candidate = String(value);
17
+ return def.values.includes(candidate) ? candidate : undefined;
18
+ }
19
+ if (def.type === "textTransform") {
20
+ return CONTRACT.themeComponentTextTransforms.includes(value) ? value : undefined;
21
+ }
22
+ // `Number(true)` is 1 and `Number(["12"])` is 12: a trust boundary drops a
23
+ // wrong-typed value rather than inventing an in-range one from it.
24
+ if (typeof value !== "number" && typeof value !== "string") return undefined;
25
+ const n = typeof value === "number" ? value : Number(value);
26
+ if (!Number.isFinite(n)) return undefined;
27
+ const clamped = Math.min(def.max, Math.max(def.min, n));
28
+ if (def.type === "size") return Math.round(clamped);
29
+ // Leading and tracking must keep the decimals `size` rounds away.
30
+ if (def.type === "decimal") return Math.round(clamped * 100) / 100;
31
+ return undefined;
32
+ }
33
+
34
+ /** A raw blob cut down to the contract; an emptied role is omitted. */
35
+ function normaliseTypographyRoles(raw) {
36
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
37
+ const out = {};
38
+ for (const role of CONTRACT.themeTextRoles) {
39
+ const requested = raw[role];
40
+ if (!requested || typeof requested !== "object" || Array.isArray(requested)) continue;
41
+ const fields = {};
42
+ for (const [field, def] of Object.entries(CONTRACT.themeTextRoleFields)) {
43
+ if (requested[field] === undefined) continue;
44
+ const coerced = coerceRoleField(def, requested[field]);
45
+ if (coerced !== undefined) fields[field] = coerced;
46
+ }
47
+ if (Object.keys(fields).length) out[role] = fields;
48
+ }
49
+ return out;
50
+ }
51
+
52
+ /** Every declared role, merged per field over its default; re-validates. */
53
+ function resolveTypographyRoles(raw) {
54
+ const overrides = normaliseTypographyRoles(raw);
55
+ const merged = {};
56
+ for (const [role, defaults] of Object.entries(CONTRACT.themeTokens.typography.roles)) {
57
+ merged[role] = overrides[role] ? { ...defaults, ...overrides[role] } : { ...defaults };
58
+ }
59
+ return merged;
60
+ }
61
+ module.exports = {
62
+ normaliseTypographyRoles,
63
+ resolveTypographyRoles,
64
+ };
@@ -0,0 +1,55 @@
1
+ // REQ-THEME-ROLE: the one typography-role validator for both hosts and the backend.
2
+ import { CONTRACT } from "./contract.js";
3
+ import { coerceThemeColor } from "./theme-components.js";
4
+
5
+ // One stored role value, checked against its declared type and bounds.
6
+ function coerceRoleField(def, value) {
7
+ if (def.type === "color") return coerceThemeColor(value);
8
+ if (def.type === "enum") {
9
+ // A weight may arrive as a number; both hosts' `fontWeight` takes the string.
10
+ if (typeof value !== "string" && typeof value !== "number") return undefined;
11
+ const candidate = String(value);
12
+ return def.values.includes(candidate) ? candidate : undefined;
13
+ }
14
+ if (def.type === "textTransform") {
15
+ return CONTRACT.themeComponentTextTransforms.includes(value) ? value : undefined;
16
+ }
17
+ // `Number(true)` is 1 and `Number(["12"])` is 12: a trust boundary drops a
18
+ // wrong-typed value rather than inventing an in-range one from it.
19
+ if (typeof value !== "number" && typeof value !== "string") return undefined;
20
+ const n = typeof value === "number" ? value : Number(value);
21
+ if (!Number.isFinite(n)) return undefined;
22
+ const clamped = Math.min(def.max, Math.max(def.min, n));
23
+ if (def.type === "size") return Math.round(clamped);
24
+ // Leading and tracking must keep the decimals `size` rounds away.
25
+ if (def.type === "decimal") return Math.round(clamped * 100) / 100;
26
+ return undefined;
27
+ }
28
+
29
+ /** A raw blob cut down to the contract; an emptied role is omitted. */
30
+ export function normaliseTypographyRoles(raw) {
31
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return {};
32
+ const out = {};
33
+ for (const role of CONTRACT.themeTextRoles) {
34
+ const requested = raw[role];
35
+ if (!requested || typeof requested !== "object" || Array.isArray(requested)) continue;
36
+ const fields = {};
37
+ for (const [field, def] of Object.entries(CONTRACT.themeTextRoleFields)) {
38
+ if (requested[field] === undefined) continue;
39
+ const coerced = coerceRoleField(def, requested[field]);
40
+ if (coerced !== undefined) fields[field] = coerced;
41
+ }
42
+ if (Object.keys(fields).length) out[role] = fields;
43
+ }
44
+ return out;
45
+ }
46
+
47
+ /** Every declared role, merged per field over its default; re-validates. */
48
+ export function resolveTypographyRoles(raw) {
49
+ const overrides = normaliseTypographyRoles(raw);
50
+ const merged = {};
51
+ for (const [role, defaults] of Object.entries(CONTRACT.themeTokens.typography.roles)) {
52
+ merged[role] = overrides[role] ? { ...defaults, ...overrides[role] } : { ...defaults };
53
+ }
54
+ return merged;
55
+ }
package/dist/webbundle.js CHANGED
@@ -40,7 +40,7 @@ export async function loadEsbuild() {
40
40
  return await import("esbuild");
41
41
  } catch {
42
42
  throw new Error(
43
- "Bundling a widget's web entry needs the optional `esbuild` dependency.\n" +
43
+ "Bundling a widget's web entry needs `esbuild`, an optional peer of the SDK.\n" +
44
44
  "Install it in your widget project:\n" +
45
45
  " npm install --save-dev esbuild",
46
46
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@colixsystems/widget-sdk",
3
- "version": "0.309.0",
3
+ "version": "0.314.0",
4
4
  "description": "Common widget interface for AppStudio. Implements WidgetManifest, WidgetContext, property schema, and helper hooks.",
5
5
  "homepage": "https://github.com/Colix-AB/AppStudio",
6
6
  "type": "module",
@@ -77,7 +77,9 @@
77
77
  "react-native": "*",
78
78
  "react-dom": ">=18.0.0",
79
79
  "react-native-web": ">=0.19.0",
80
- "react-native-webview": ">=13.7.0"
80
+ "react-native-webview": ">=13.7.0",
81
+ "esbuild": "^0.28.0",
82
+ "sucrase": "^3.35.0"
81
83
  },
82
84
  "peerDependenciesMeta": {
83
85
  "react": {
@@ -94,12 +96,14 @@
94
96
  },
95
97
  "react-native-webview": {
96
98
  "optional": true
99
+ },
100
+ "esbuild": {
101
+ "optional": true
102
+ },
103
+ "sucrase": {
104
+ "optional": true
97
105
  }
98
106
  },
99
- "optionalDependencies": {
100
- "esbuild": "^0.28.0",
101
- "sucrase": "^3.35.0"
102
- },
103
107
  "keywords": [
104
108
  "appstudio",
105
109
  "widget",
@@ -108,6 +112,8 @@
108
112
  ],
109
113
  "license": "MIT",
110
114
  "devDependencies": {
111
- "@babel/parser": "^7.29.7"
115
+ "@babel/parser": "^7.29.7",
116
+ "esbuild": "^0.28.0",
117
+ "sucrase": "^3.35.0"
112
118
  }
113
119
  }