@astryxdesign/cli 0.7.13 → 0.7.14-canary.d08ce65

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.
Files changed (146) hide show
  1. package/README.md +18 -18
  2. package/api/component/__fixtures__/resolutions/modules.txt +1 -1
  3. package/api/doctor/doctor.mjs +3 -7
  4. package/api/experimental.d.mts +2 -0
  5. package/api/experimental.mjs +10 -0
  6. package/api/icon/_adapter.d.mts +10 -0
  7. package/api/icon/_adapter.mjs +31 -2
  8. package/api/icon/_authoring.d.mts +2 -1
  9. package/api/icon/_authoring.mjs +91 -6
  10. package/api/icon/_options.mjs +1 -0
  11. package/api/icon/_records.d.mts +18 -0
  12. package/api/icon/_records.mjs +26 -0
  13. package/api/icon/add/add.mjs +7 -1
  14. package/api/icon/definitions/definitions.d.mts +12 -0
  15. package/api/icon/definitions/definitions.mjs +83 -0
  16. package/api/icon/icon.doc.mjs +5 -4
  17. package/api/icon/icon.mjs +5 -16
  18. package/api/icon/icon.type.d.mts +29 -2
  19. package/api/icon/icon.type.mjs +17 -3
  20. package/api/icon/iconAdd.doc.mjs +1 -1
  21. package/api/icon/iconMap.doc.mjs +2 -2
  22. package/api/icon/iconNewSet.doc.mjs +8 -2
  23. package/api/icon/map/map.mjs +21 -2
  24. package/api/icon/new-set/new-set.mjs +14 -2
  25. package/api/icon/search/search.mjs +2 -15
  26. package/api/icon/use/use.mjs +4 -3
  27. package/api/integration/validate-integration.mjs +6 -1
  28. package/api/integration/validate-integration.type.d.mts +12 -0
  29. package/api/integration/validate-integration.type.mjs +5 -0
  30. package/api/theme-edit/_adapter.d.mts +96 -0
  31. package/api/theme-edit/_adapter.mjs +1039 -0
  32. package/api/theme-edit/_catalog.d.mts +59 -0
  33. package/api/theme-edit/_catalog.mjs +564 -0
  34. package/api/theme-edit/_check.d.mts +49 -0
  35. package/api/theme-edit/_check.mjs +842 -0
  36. package/api/theme-edit/_color.d.mts +35 -0
  37. package/api/theme-edit/_color.mjs +125 -0
  38. package/api/theme-edit/_compiler.d.mts +61 -0
  39. package/api/theme-edit/_compiler.mjs +522 -0
  40. package/api/theme-edit/_css-properties.d.mts +10 -0
  41. package/api/theme-edit/_css-properties.mjs +556 -0
  42. package/api/theme-edit/_parts.d.mts +111 -0
  43. package/api/theme-edit/_parts.mjs +409 -0
  44. package/api/theme-edit/_png.d.mts +40 -0
  45. package/api/theme-edit/_png.mjs +187 -0
  46. package/api/theme-edit/_preview.d.mts +86 -0
  47. package/api/theme-edit/_preview.mjs +564 -0
  48. package/api/theme-edit/_project.d.mts +83 -0
  49. package/api/theme-edit/_project.mjs +289 -0
  50. package/api/theme-edit/_settings.d.mts +222 -0
  51. package/api/theme-edit/_settings.mjs +848 -0
  52. package/api/theme-edit/_tables.d.mts +130 -0
  53. package/api/theme-edit/_tables.mjs +697 -0
  54. package/api/theme-edit/theme-edit.d.mts +89 -0
  55. package/api/theme-edit/theme-edit.mjs +119 -0
  56. package/api/theme-edit/theme-edit.type.d.mts +270 -0
  57. package/api/theme-edit/theme-edit.type.mjs +155 -0
  58. package/api/theme-edit/themeEditExplain.doc.d.mts +6 -0
  59. package/api/theme-edit/themeEditExplain.doc.mjs +88 -0
  60. package/api/theme-edit/themeEditNew.doc.d.mts +6 -0
  61. package/api/theme-edit/themeEditNew.doc.mjs +89 -0
  62. package/api/theme-edit/themeEditPreview.doc.d.mts +6 -0
  63. package/api/theme-edit/themeEditPreview.doc.mjs +96 -0
  64. package/api/theme-edit/themeEditReset.doc.d.mts +6 -0
  65. package/api/theme-edit/themeEditReset.doc.mjs +161 -0
  66. package/api/theme-edit/themeEditSet.doc.d.mts +6 -0
  67. package/api/theme-edit/themeEditSet.doc.mjs +165 -0
  68. package/api/theme-edit/themeEditShow.doc.d.mts +6 -0
  69. package/api/theme-edit/themeEditShow.doc.mjs +86 -0
  70. package/api/theme-edit/themeEditUndo.doc.d.mts +6 -0
  71. package/api/theme-edit/themeEditUndo.doc.mjs +80 -0
  72. package/assets/docs/color.doc.mjs +5 -5
  73. package/assets/docs/tree/icon-sets.doc.mjs +6 -2
  74. package/assets/docs/tree/tokens-border.doc.mjs +1 -1
  75. package/assets/docs/tree/tokens-color.doc.mjs +2 -2
  76. package/assets/docs/tree/tokens-data.doc.mjs +2 -2
  77. package/assets/docs/tree/tokens-duration.doc.mjs +1 -1
  78. package/assets/docs/tree/tokens-ease.doc.mjs +1 -1
  79. package/assets/docs/tree/tokens-focus.doc.mjs +1 -1
  80. package/assets/docs/tree/tokens-font-weight.doc.mjs +1 -1
  81. package/assets/docs/tree/tokens-radius.doc.mjs +1 -1
  82. package/assets/docs/tree/tokens-shadow.doc.mjs +1 -1
  83. package/assets/docs/tree/tokens-size.doc.mjs +1 -1
  84. package/assets/docs/tree/tokens-spacing.doc.mjs +1 -1
  85. package/assets/docs/tree/tokens-syntax.doc.mjs +1 -1
  86. package/assets/docs/tree/tokens-text-size.doc.mjs +1 -1
  87. package/assets/docs/tree/tokens-type-scale.doc.mjs +1 -1
  88. package/assets/docs/tree/tokens-typography.doc.mjs +1 -1
  89. package/assets/icons/definitions/bank.doc.mjs +1 -1
  90. package/assets/icons/definitions/save.doc.mjs +1 -1
  91. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider01ShippedLocale.doc.mjs +2 -1
  92. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider02Overrides.doc.mjs +2 -1
  93. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider03RtlDirection.doc.mjs +2 -1
  94. package/assets/templates/blocks/components/InternationalizationProvider/InternationalizationProvider04Calendar.doc.mjs +2 -1
  95. package/assets/templates/blocks/components/Text/useTruncationHookUsage.doc.mjs +14 -0
  96. package/assets/templates/blocks/components/Text/useTruncationHookUsage.tsx +112 -0
  97. package/authoring/doctypes/_schema.d.mts +1 -0
  98. package/authoring/doctypes/_schema.mjs +1 -0
  99. package/authoring/doctypes/command/command.doc.mjs +7 -0
  100. package/authoring/doctypes/command/type.ts +3 -0
  101. package/authoring/doctypes/icon-set/parse.d.mts +7 -0
  102. package/authoring/doctypes/icon-set/parse.mjs +18 -8
  103. package/clients/cli/commands/doctor.mjs +33 -3
  104. package/clients/cli/commands/experimental/icon.mjs +67 -14
  105. package/clients/cli/commands/experimental/index.mjs +2 -0
  106. package/clients/cli/commands/experimental/theme.mjs +542 -0
  107. package/clients/cli/commands/experimental-icon-new-set.doc.mjs +7 -1
  108. package/clients/cli/commands/experimental-icon.doc.mjs +3 -2
  109. package/clients/cli/commands/experimental-theme-add.doc.mjs +78 -0
  110. package/clients/cli/commands/experimental-theme-compile.doc.mjs +97 -0
  111. package/clients/cli/commands/experimental-theme-eject.doc.mjs +71 -0
  112. package/clients/cli/commands/experimental-theme-explain.doc.mjs +84 -0
  113. package/clients/cli/commands/experimental-theme-list.doc.mjs +49 -0
  114. package/clients/cli/commands/experimental-theme-new.doc.mjs +66 -0
  115. package/clients/cli/commands/experimental-theme-palette-generate.doc.mjs +73 -0
  116. package/clients/cli/commands/experimental-theme-palette.doc.mjs +37 -0
  117. package/clients/cli/commands/experimental-theme-preview.doc.mjs +76 -0
  118. package/clients/cli/commands/experimental-theme-remove.doc.mjs +55 -0
  119. package/clients/cli/commands/experimental-theme-reset.doc.mjs +103 -0
  120. package/clients/cli/commands/experimental-theme-set.doc.mjs +121 -0
  121. package/clients/cli/commands/experimental-theme-show.doc.mjs +66 -0
  122. package/clients/cli/commands/experimental-theme-targets.doc.mjs +61 -0
  123. package/clients/cli/commands/experimental-theme-template.doc.mjs +61 -0
  124. package/clients/cli/commands/experimental-theme-undo.doc.mjs +55 -0
  125. package/clients/cli/commands/experimental-theme-use.doc.mjs +55 -0
  126. package/clients/cli/commands/experimental-theme.doc.mjs +66 -0
  127. package/clients/cli/commands/experimental.doc.mjs +10 -3
  128. package/clients/cli/commands/theme.mjs +101 -34
  129. package/clients/cli/index.mjs +16 -0
  130. package/clients/cli/lib/define-command.mjs +3 -0
  131. package/foundation/discovery/icon-set-checks.d.mts +16 -1
  132. package/foundation/discovery/icon-set-checks.mjs +68 -7
  133. package/foundation/discovery/icon-set-discovery.d.mts +21 -6
  134. package/foundation/discovery/icon-set-discovery.mjs +17 -8
  135. package/foundation/discovery/template-icon-meanings.d.mts +39 -0
  136. package/foundation/discovery/template-icon-meanings.mjs +95 -0
  137. package/foundation/discovery/theming-targets.d.mts +6 -0
  138. package/foundation/discovery/theming-targets.mjs +33 -0
  139. package/foundation/response/error-codes.d.mts +12 -1
  140. package/foundation/response/error-codes.doc.mjs +66 -0
  141. package/foundation/response/error-codes.mjs +55 -0
  142. package/foundation/response/response-types.doc.mjs +43 -0
  143. package/foundation/text/template-meanings-line.d.mts +16 -0
  144. package/foundation/text/template-meanings-line.mjs +37 -0
  145. package/foundation/xle/browser.d.mts +2 -2
  146. package/package.json +6 -6
@@ -0,0 +1,842 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file The type check of one `set` or `reset`: the part, the variant, the
5
+ * state, the mode, the token's type, and whether the part (or anything in
6
+ * it) reads the token. A change that cannot take effect is refused, and the
7
+ * refusal leads with the command that works. Also `how`, which answers a
8
+ * plain-words question with exact commands from the same part data.
9
+ *
10
+ * @position api/theme-edit — checks and questions over the part data
11
+ */
12
+
13
+ import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
14
+ import {CSS_PROPERTIES} from './_css-properties.mjs';
15
+ import {
16
+ AUTO_SPREAD,
17
+ COLOR_PROPS,
18
+ COMMON_STATES,
19
+ CORNERS,
20
+ DESIGN,
21
+ DIALS,
22
+ DIRECT,
23
+ FAMILIES,
24
+ GROUPS,
25
+ HEADING_TRACKING,
26
+ HEADING_WEIGHTS,
27
+ INTENTS,
28
+ LIKE,
29
+ P,
30
+ PART_WORDS,
31
+ SPACING,
32
+ TEXT_SIZE,
33
+ THING_HINTS,
34
+ familyOf,
35
+ } from './_tables.mjs';
36
+ import {isColorValue, isNumber, isSize} from './_color.mjs';
37
+ import {MODAL} from './_compiler.mjs';
38
+ import {
39
+ camel,
40
+ kebab,
41
+ nearest,
42
+ partDefault,
43
+ partsOf,
44
+ q,
45
+ refuse,
46
+ } from './_settings.mjs';
47
+
48
+ const E = ERROR_CODES;
49
+
50
+ /**
51
+ * @typedef {object} SetFlags
52
+ * @property {string} [on]
53
+ * @property {string} [when]
54
+ * @property {string} [state]
55
+ * @property {string} [mode]
56
+ * @property {boolean} [only]
57
+ */
58
+
59
+ /**
60
+ * Refuse a value that does not parse as the token's type.
61
+ * @param {string} type @param {string} value @param {string} name
62
+ */
63
+ function checkTypedValue(type, value, name) {
64
+ if (type === 'color' && !isColorValue(value))
65
+ refuse(
66
+ E.ERR_THEME_BAD_VALUE,
67
+ `${name} is a color; "${value}" is not a CSS color.`,
68
+ {fix: 'give one like "#1f2937", "rgb(31 41 55)" or "oklch(0.5 0.1 250)"'},
69
+ );
70
+ if (type === 'size' && !isSize(value))
71
+ refuse(
72
+ E.ERR_THEME_BAD_VALUE,
73
+ `${name} is a size; "${value}" is not a CSS size.`,
74
+ {fix: 'give one like 6px, 0.75rem or 0'},
75
+ );
76
+ if (type === 'number' && !isNumber(value))
77
+ refuse(
78
+ E.ERR_THEME_BAD_VALUE,
79
+ `${name} is a number; "${value}" is not one.`,
80
+ {fix: 'give one like 600 or 1.4'},
81
+ );
82
+ }
83
+
84
+ /**
85
+ * Check one setting and give it back ready to store, or refuse.
86
+ * @param {import('./_settings.mjs').Ctx} ctx
87
+ * @param {string} slug
88
+ * @param {import('./_settings.mjs').ThemeData} data
89
+ * @param {string} thing
90
+ * @param {string | undefined} value
91
+ * @param {SetFlags} flags
92
+ * @param {boolean} [unset] a reset: only the identity of the setting is checked
93
+ * @returns {import('./_settings.mjs').Setting}
94
+ */
95
+ export function checkSetting(
96
+ ctx,
97
+ slug,
98
+ data,
99
+ thing,
100
+ value,
101
+ flags,
102
+ unset = false,
103
+ ) {
104
+ const {C} = ctx;
105
+ const TARGET = ctx.project.target;
106
+ const TOKENS = ctx.project.core.tokenDefaults;
107
+ if (!thing)
108
+ refuse(E.ERR_MISSING_ARGUMENT, 'Name the thing to change.', {
109
+ run: [`${P} ${unset ? 'reset' : 'set'} --help`],
110
+ });
111
+ if (!unset && (value === undefined || value === ''))
112
+ refuse(E.ERR_MISSING_ARGUMENT, `Give a value for ${thing}.`, {
113
+ run: [`${P} set --help`],
114
+ });
115
+ const v = value ?? '';
116
+ /** @type {import('./_settings.mjs').Setting} */
117
+ const s = {thing, value: v, source: 'dial'};
118
+ if (flags.on) s.on = flags.on;
119
+ if (flags.when) s.when = flags.when;
120
+ if (flags.state) s.state = flags.state;
121
+ if (flags.only) s.only = true;
122
+ if (flags.mode) {
123
+ if (flags.mode !== 'light' && flags.mode !== 'dark')
124
+ refuse(
125
+ E.ERR_THEME_BAD_VALUE,
126
+ `--mode takes light or dark, not "${flags.mode}".`,
127
+ );
128
+ s.mode = flags.mode;
129
+ }
130
+ const onFlag = s.on ? ` --on ${s.on}` : '';
131
+ if (s.mode && data.mode !== 'both')
132
+ refuse(
133
+ E.ERR_THEME_NO_MODE,
134
+ `This theme is ${data.mode}; --mode is for themes with both modes.`,
135
+ {run: [`${P} set ${slug} ${thing} ${q(v)}${onFlag}`]},
136
+ );
137
+ if ((s.when || s.state) && !s.on)
138
+ refuse(E.ERR_MISSING_ARGUMENT, '--when and --state need --on <part>.', {
139
+ run: [`${P} explain <part>`],
140
+ });
141
+ if (s.only) {
142
+ const fam = s.on ? familyOf(s.on) : null;
143
+ if (!fam || FAMILIES[/** @type {string} */ (s.on)])
144
+ refuse(
145
+ E.ERR_INVALID_ARGUMENT,
146
+ '--only is for one member of a family, such as text-input (explain --list families).',
147
+ {run: [`${P} explain --list families`]},
148
+ );
149
+ if (!AUTO_SPREAD.includes(/** @type {string} */ (fam)))
150
+ refuse(
151
+ E.ERR_INVALID_ARGUMENT,
152
+ `${s.on} is styled on its own already: only ${AUTO_SPREAD.join(' and ')} spread to their family, so drop --only.`,
153
+ {
154
+ run: [
155
+ `${P} ${unset ? 'reset' : 'set'} ${slug} ${thing} ${q(v)} --on ${s.on}`,
156
+ ],
157
+ },
158
+ );
159
+ }
160
+ const isRole =
161
+ thing === 'font' && ['body', 'heading', 'code'].includes(s.on ?? '');
162
+ if (s.on && !isRole && !FAMILIES[s.on]) {
163
+ const t = TARGET.get(s.on);
164
+ if (!t) {
165
+ refuse(
166
+ E.ERR_THEME_UNKNOWN_PART,
167
+ `"${s.on}" is not a part you can style.`,
168
+ {
169
+ didYouMean: nearest(s.on, [
170
+ ...TARGET.keys(),
171
+ ...Object.keys(FAMILIES),
172
+ ]),
173
+ run: [
174
+ `${P} explain ${s.on.split('-')[0]} --list parts`,
175
+ `${P} explain "${s.on}"`,
176
+ ],
177
+ },
178
+ );
179
+ }
180
+ if (s.when) {
181
+ const [p, wv] = s.when.split(':');
182
+ if (!wv)
183
+ refuse(
184
+ E.ERR_THEME_BAD_VALUE,
185
+ '--when takes <prop>:<value>, such as variant:primary.',
186
+ {run: [`${P} explain ${s.on}`]},
187
+ );
188
+ if (!t.props.includes(p))
189
+ refuse(
190
+ E.ERR_THEME_UNKNOWN_VARIANT,
191
+ `${s.on} has no prop "${p}". It has: ${t.props.join(', ') || 'none'}.`,
192
+ {didYouMean: nearest(p, t.props), run: [`${P} explain ${s.on}`]},
193
+ );
194
+ const vals = C.variantValues(s.on)[p] ?? [];
195
+ if (vals.length && !vals.includes(wv))
196
+ refuse(
197
+ E.ERR_THEME_UNKNOWN_VARIANT,
198
+ `${s.on} has no ${p} "${wv}". ${p} is one of: ${vals.join(', ')}.`,
199
+ {didYouMean: nearest(wv, vals), run: [`${P} explain ${s.on}`]},
200
+ );
201
+ }
202
+ const states = [...t.states, ...COMMON_STATES];
203
+ if (s.state && !states.includes(s.state))
204
+ refuse(
205
+ E.ERR_THEME_UNKNOWN_STATE,
206
+ `"${s.state}" is not a state of ${s.on}. States: ${states.join(', ')}.`,
207
+ {didYouMean: nearest(s.state, states)},
208
+ );
209
+ }
210
+ const step = v === 'more' || v === 'less';
211
+ if (DIALS[thing]) {
212
+ s.source = 'dial';
213
+ if (['surfaces', 'buttons', 'type'].includes(thing)) {
214
+ if (s.on)
215
+ refuse(
216
+ E.ERR_THEME_BAD_VALUE,
217
+ `${thing} moves every part it covers; drop --on. For one part, set CSS properties --on <part>.`,
218
+ {
219
+ run: [
220
+ `${P} set ${slug} ${thing} ${q(v || DIALS[thing].values.split('|')[0])}`,
221
+ ],
222
+ },
223
+ );
224
+ if (s.mode)
225
+ refuse(
226
+ E.ERR_THEME_NO_MODE,
227
+ `${thing} is the same in light and dark; drop --mode.`,
228
+ {run: [`${P} set ${slug} ${thing} ${q(v)}`]},
229
+ );
230
+ if (unset) return s;
231
+ const ok = DIALS[thing].values.split('|');
232
+ if (!ok.includes(v))
233
+ refuse(
234
+ E.ERR_THEME_BAD_VALUE,
235
+ `${thing} takes ${DIALS[thing].values}, not "${v}".`,
236
+ {didYouMean: nearest(v, ok)},
237
+ );
238
+ return s;
239
+ }
240
+ if (thing === 'headings') {
241
+ if (s.on)
242
+ refuse(
243
+ E.ERR_THEME_BAD_VALUE,
244
+ 'headings applies to every heading; drop --on. For one heading level use font-weight or letter-spacing --on heading --when level:<1-6>.',
245
+ {
246
+ run: [
247
+ `${P} set ${slug} font-weight 700 --on heading --when level:1`,
248
+ ],
249
+ },
250
+ );
251
+ if (unset) {
252
+ s.axis =
253
+ HEADING_TRACKING[v] !== undefined || v === 'tracking'
254
+ ? 'tracking'
255
+ : 'weight';
256
+ return s;
257
+ }
258
+ if (HEADING_WEIGHTS[v] !== undefined) s.axis = 'weight';
259
+ else if (HEADING_TRACKING[v] !== undefined) s.axis = 'tracking';
260
+ else
261
+ refuse(
262
+ E.ERR_THEME_BAD_VALUE,
263
+ `headings takes ${DIALS.headings.values}, not "${v}".`,
264
+ {
265
+ didYouMean: nearest(v, [
266
+ ...Object.keys(HEADING_WEIGHTS),
267
+ ...Object.keys(HEADING_TRACKING),
268
+ ]),
269
+ },
270
+ );
271
+ return s;
272
+ }
273
+ if (unset) return s;
274
+ /** @param {Array<[string, number]>} L @param {string} def */
275
+ const ladder = (L, def) => {
276
+ const cur =
277
+ [...data.settings]
278
+ .reverse()
279
+ .find(x => x.thing === thing && (x.on ?? '') === (s.on ?? ''))
280
+ ?.value ?? def;
281
+ let i = L.findIndex(([n]) => n === cur);
282
+ if (i < 0) {
283
+ const px = parseFloat(cur);
284
+ i = L.findIndex(([, n]) => n >= px);
285
+ if (i < 0) i = L.length - 1;
286
+ }
287
+ return L[
288
+ Math.max(0, Math.min(L.length - 1, i + (v === 'more' ? 1 : -1)))
289
+ ][0];
290
+ };
291
+ if (s.mode && thing !== 'accent')
292
+ refuse(
293
+ E.ERR_THEME_NO_MODE,
294
+ `${thing} is not a color, so it is the same in light and dark; drop --mode.`,
295
+ {run: [`${P} set ${slug} ${thing} ${q(v)}${onFlag}`]},
296
+ );
297
+ if (thing === 'corners') {
298
+ if (s.on) refuseInvisibleCorners(ctx, slug, s);
299
+ if (step) s.value = ladder(CORNERS, 'medium');
300
+ else if (!CORNERS.some(([n]) => n === v) && !/^\d+(\.\d+)?px$/.test(v))
301
+ refuse(
302
+ E.ERR_THEME_BAD_VALUE,
303
+ `corners takes ${DIALS.corners.values}, not "${v}".`,
304
+ {
305
+ didYouMean: nearest(
306
+ v,
307
+ CORNERS.map(([n]) => n),
308
+ ),
309
+ },
310
+ );
311
+ } else if (thing === 'spacing') {
312
+ if (step) s.value = ladder(SPACING, 'medium');
313
+ else if (!SPACING.some(([n]) => n === v))
314
+ refuse(
315
+ E.ERR_THEME_BAD_VALUE,
316
+ `spacing takes ${DIALS.spacing.values}, not "${v}".`,
317
+ {
318
+ didYouMean: nearest(
319
+ v,
320
+ SPACING.map(([n]) => n),
321
+ ),
322
+ },
323
+ );
324
+ } else if (thing === 'text-size') {
325
+ if (s.on)
326
+ refuse(
327
+ E.ERR_THEME_BAD_VALUE,
328
+ 'text-size applies to the whole theme; for one part use font-size.',
329
+ {run: [`${P} set ${slug} font-size 13px --on ${s.on}`]},
330
+ );
331
+ if (step) s.value = ladder(TEXT_SIZE, 'medium');
332
+ else if (!TEXT_SIZE.some(([n]) => n === v) && !Number.isFinite(Number(v)))
333
+ refuse(
334
+ E.ERR_THEME_BAD_VALUE,
335
+ `text-size takes ${DIALS['text-size'].values}, not "${v}".`,
336
+ );
337
+ } else if (thing === 'accent') {
338
+ if (step)
339
+ refuse(E.ERR_THEME_BAD_VALUE, 'accent takes a color, not a step.', {
340
+ run: [`${P} set ${slug} accent "#7c3aed"`],
341
+ });
342
+ if (!isColorValue(v))
343
+ refuse(E.ERR_THEME_BAD_VALUE, `"${v}" is not a CSS color.`, {
344
+ fix: 'give one like "#7c3aed"',
345
+ });
346
+ } else if (thing === 'font') {
347
+ if (s.on && !isRole)
348
+ refuse(
349
+ E.ERR_THEME_BAD_VALUE,
350
+ "font --on takes body, heading, or code. For one part's font use font-family.",
351
+ {run: [`${P} set ${slug} font-family ${q(v)} --on ${s.on}`]},
352
+ );
353
+ if (step)
354
+ refuse(
355
+ E.ERR_THEME_BAD_VALUE,
356
+ 'font takes a preset or a family name, not a step.',
357
+ {fix: `the values are ${DIALS.font.values}`},
358
+ );
359
+ } else if (thing === 'base') {
360
+ if (s.on)
361
+ refuse(
362
+ E.ERR_THEME_BAD_VALUE,
363
+ 'base applies to the whole theme; drop --on.',
364
+ {run: [`${P} set ${slug} base ${q(v)}`]},
365
+ );
366
+ if (![...LIKE, 'none'].includes(v))
367
+ refuse(
368
+ E.ERR_THEME_BAD_VALUE,
369
+ `base takes ${DIALS.base.values}, not "${v}".`,
370
+ {didYouMean: nearest(v, LIKE)},
371
+ );
372
+ } else if (thing === 'mode') {
373
+ if (s.on)
374
+ refuse(
375
+ E.ERR_THEME_BAD_VALUE,
376
+ 'mode applies to the whole theme; drop --on.',
377
+ {run: [`${P} set ${slug} mode ${q(v)}`]},
378
+ );
379
+ if (!['both', 'light-only', 'dark-only'].includes(v))
380
+ refuse(
381
+ E.ERR_THEME_BAD_VALUE,
382
+ `mode takes both, light-only, or dark-only, not "${v}".`,
383
+ {didYouMean: nearest(v, ['both', 'light-only', 'dark-only'])},
384
+ );
385
+ }
386
+ return s;
387
+ }
388
+ if (thing.startsWith('--')) {
389
+ if (thing.startsWith('--_'))
390
+ refuse(
391
+ E.ERR_THEME_PRIVATE_TOKEN,
392
+ `"${thing}" is private to a component; themes cannot set it.`,
393
+ {run: [`${P} explain "<what you want>"`]},
394
+ );
395
+ if (!(thing in TOKENS)) {
396
+ refuse(E.ERR_THEME_UNKNOWN_TOKEN, `"${thing}" is not a design token.`, {
397
+ didYouMean: nearest(thing, Object.keys(TOKENS)),
398
+ run: [
399
+ `${P} explain ${thing.replace(/^--(color-)?/, '').split('-')[0]} --list tokens`,
400
+ ],
401
+ });
402
+ }
403
+ const type = C.typeOf(thing);
404
+ if (s.mode && !MODAL.has(type))
405
+ refuse(
406
+ E.ERR_THEME_NO_MODE,
407
+ `${thing} is a ${type}, so it is the same in light and dark; drop --mode. Only colors and shadows differ by mode.`,
408
+ {run: [`${P} set ${slug} ${thing} ${q(v)}${onFlag}`]},
409
+ );
410
+ s.source = 'token';
411
+ if (unset) return s;
412
+ if (step)
413
+ refuse(
414
+ E.ERR_THEME_BAD_VALUE,
415
+ `"more"/"less" work only on dials (${Object.keys(DIALS).join(', ')}). Give ${thing} a ${type} value.`,
416
+ );
417
+ checkTypedValue(type, v, thing);
418
+ const parts = partsOf(ctx, s);
419
+ if (parts.length) {
420
+ const results = parts.map(
421
+ t =>
422
+ /** @type {[string, import('./_compiler.mjs').CheckFailure | null]} */ ([
423
+ t,
424
+ C.checkTokenOn(slug, t, thing, v, P),
425
+ ]),
426
+ );
427
+ const recorded = results.filter(([, r]) => !r?.soft);
428
+ const hard = recorded.filter(([, r]) => r);
429
+ const spread = Boolean(
430
+ FAMILIES[s.on ?? ''] || (familyOf(s.on ?? '') && parts.length > 1),
431
+ );
432
+ // A part with no record is judged by its family: refused when no
433
+ // member that has a record reads the token, as the family form is.
434
+ const fam = s.on ? familyOf(s.on) : null;
435
+ if (!recorded.length && fam && !spread) {
436
+ const siblings = FAMILIES[fam].filter(t => C.MAP[t]);
437
+ const famResults = siblings.map(
438
+ t =>
439
+ /** @type {[string, import('./_compiler.mjs').CheckFailure | null]} */ ([
440
+ t,
441
+ C.checkTokenOn(slug, t, thing, v, P),
442
+ ]),
443
+ );
444
+ if (famResults.length && famResults.every(([, r]) => r)) {
445
+ const r = /** @type {import('./_compiler.mjs').CheckFailure} */ (
446
+ famResults[0][1]
447
+ );
448
+ refuse(
449
+ E.ERR_THEME_NO_EFFECT,
450
+ `No part of the ${fam} family reads ${thing}, so ${s.on} cannot either. ${r.message}`,
451
+ {
452
+ run: r.run.map(x =>
453
+ x.replace(`--on ${famResults[0][0]}`, `--on ${s.on}`),
454
+ ),
455
+ also: r.also,
456
+ },
457
+ );
458
+ }
459
+ if (famResults.length)
460
+ s.note = `${s.on} has no compiled record; checked against the ${fam} family, where ${famResults
461
+ .filter(([, r]) => !r)
462
+ .map(([t]) => t)
463
+ .join(', ')} read ${thing}`;
464
+ } else if (recorded.length && hard.length === recorded.length) {
465
+ const [t0, r0] =
466
+ /** @type {[string, import('./_compiler.mjs').CheckFailure]} */ (
467
+ hard[0]
468
+ );
469
+ refuse(E.ERR_THEME_NO_EFFECT, r0.message, {
470
+ run: r0.run.map(x =>
471
+ spread ? x.replace(`--on ${t0}`, `--on ${s.on}`) : x,
472
+ ),
473
+ also: r0.also,
474
+ });
475
+ } else if (hard.length)
476
+ s.note = `no effect on ${hard.map(([t]) => t).join(', ')} (they never read ${thing})`;
477
+ if (!s.note && recorded.length) {
478
+ const fed = C.feeds(recorded[0][0], thing);
479
+ if (fed.length) s.note = `feeds ${C.fmtFeeds(fed)}`;
480
+ }
481
+ const soft = results.filter(([, r]) => r?.soft);
482
+ if (!s.note && soft.length && soft.length === results.length)
483
+ s.note = /** @type {import('./_compiler.mjs').CheckFailure} */ (
484
+ soft[0][1]
485
+ ).message;
486
+ } else {
487
+ const r = C.checkTokenGlobal(thing, P);
488
+ if (r)
489
+ refuse(E.ERR_THEME_NO_EFFECT, r.message, {run: r.run, also: r.also});
490
+ }
491
+ return s;
492
+ }
493
+ const prop = kebab(thing);
494
+ if (CSS_PROPERTIES.has(prop)) {
495
+ if (!s.on)
496
+ refuse(
497
+ E.ERR_MISSING_ARGUMENT,
498
+ `${prop} is a CSS property, so it needs --on <part|family>. For the whole theme use a dial or a token.`,
499
+ {
500
+ run: [
501
+ `${P} set ${slug} ${prop} ${q(v || '<value>')} --on <part>`,
502
+ `${P} explain "${prop}"`,
503
+ ],
504
+ },
505
+ );
506
+ s.source = 'property';
507
+ s.thing = prop;
508
+ if (unset) return s;
509
+ if (/radius$/.test(prop)) refuseInvisibleCorners(ctx, slug, s);
510
+ if (step)
511
+ refuse(
512
+ E.ERR_THEME_BAD_VALUE,
513
+ `"more"/"less" work only on dials. Give ${prop} an exact CSS value.`,
514
+ );
515
+ if (COLOR_PROPS.test(prop) && !isColorValue(v)) {
516
+ refuse(
517
+ E.ERR_THEME_BAD_VALUE,
518
+ `${prop} takes a color; "${v}" is not one.${/gradient/.test(v) ? ' Gradients go in background.' : ''}`,
519
+ {
520
+ run: /gradient/.test(v)
521
+ ? [`${P} set ${slug} background ${q(v)} --on ${s.on}`]
522
+ : [],
523
+ },
524
+ );
525
+ }
526
+ const whenFlag = s.when ? ` --when ${s.when}` : '';
527
+ if (
528
+ s.mode &&
529
+ !COLOR_PROPS.test(prop) &&
530
+ !(prop === 'background' && isColorValue(v))
531
+ ) {
532
+ const alt =
533
+ prop === 'border' || prop.startsWith('border-')
534
+ ? 'border-color'
535
+ : prop === 'background'
536
+ ? 'background-color'
537
+ : null;
538
+ refuse(
539
+ E.ERR_THEME_NO_MODE,
540
+ `--mode works on plain colors; ${prop} ${prop === 'box-shadow' || prop === 'border' ? 'mixes a color with sizes' : 'is not a color'}, so it cannot differ between light and dark.`,
541
+ {
542
+ run: alt
543
+ ? [
544
+ `${P} set ${slug} ${prop} ${q(v.replace(/#[0-9a-f]{3,8}|rgba?\([^)]*\)/i, 'currentcolor'))} --on ${s.on}${whenFlag} # the shape, both modes`,
545
+ `${P} set ${slug} ${alt} "<color>" --on ${s.on}${whenFlag} --mode ${s.mode} # then the color per mode`,
546
+ ]
547
+ : [
548
+ `${P} set ${slug} ${prop} ${q(v)} --on ${s.on}${whenFlag} # both modes`,
549
+ ],
550
+ },
551
+ );
552
+ }
553
+ if (
554
+ s.mode &&
555
+ !partsOf(ctx, s).some(t => partDefault(ctx, t, camel(prop), s.when))
556
+ )
557
+ s.note = `the ${s.mode === 'light' ? 'dark' : 'light'} side keeps the part's current ${prop}`;
558
+ return s;
559
+ }
560
+ const hint =
561
+ /** @type {((v: string, on: string | undefined) => string[]) | undefined} */ (
562
+ THING_HINTS[thing.toLowerCase()]
563
+ );
564
+ return refuse(
565
+ E.ERR_THEME_UNKNOWN_THING,
566
+ `"${thing}" is not a dial, a token, or a CSS property.`,
567
+ {
568
+ run: hint
569
+ ? hint(v, s.on)
570
+ : [`${P} explain "${thing}${s.on ? ` on ${s.on}` : ''}"`],
571
+ didYouMean: hint
572
+ ? []
573
+ : nearest(prop, [...Object.keys(DIALS), ...CSS_PROPERTIES]),
574
+ },
575
+ );
576
+ }
577
+
578
+ /**
579
+ * Refuse rounding a part that shows no edge: no background, border, shadow
580
+ * or radius of its own, so a radius on it changes nothing you see.
581
+ * @param {import('./_settings.mjs').Ctx} ctx
582
+ * @param {string} slug
583
+ * @param {import('./_settings.mjs').Setting} s
584
+ */
585
+ function refuseInvisibleCorners(ctx, slug, s) {
586
+ const parts = partsOf(ctx, s);
587
+ if (!parts.length || parts.some(t => ctx.C.showsCorners(t))) return;
588
+ const kids = (ctx.C.MAP[parts[0]]?.contains ?? [])
589
+ .filter(k => ctx.C.showsCorners(k) && ctx.C.MAP[k])
590
+ .slice(0, 3);
591
+ refuse(
592
+ E.ERR_THEME_NO_EFFECT,
593
+ `${s.on} has no background, border or shadow of its own, so rounding its corners changes nothing you see.${kids.length ? ` Parts inside it that do show an edge: ${kids.join(', ')}.` : ''}`,
594
+ {
595
+ run: kids.length
596
+ ? kids.map(
597
+ k => `${P} set ${slug} corners ${q(s.value || 'round')} --on ${k}`,
598
+ )
599
+ : [`${P} explain ${s.on}`],
600
+ },
601
+ );
602
+ }
603
+
604
+ /**
605
+ * Answer a plain-words question with exact commands: the parts it names,
606
+ * the aspects it asks about, and what each part really reads for them.
607
+ * @param {import('./_settings.mjs').Ctx} ctx
608
+ * @param {string} question
609
+ * @returns {{parts: string[], aspects: string[], results: Array<{text: string, commands: string[]}>}}
610
+ */
611
+ export function answerHow(ctx, question) {
612
+ const {C} = ctx;
613
+ const TARGET = ctx.project.target;
614
+ const TOKENS = ctx.project.core.tokenDefaults;
615
+ const text = question.toLowerCase();
616
+ /** @type {string[]} */
617
+ const parts = [];
618
+ for (const [re, ts] of PART_WORDS)
619
+ if (re.test(text))
620
+ for (const t of ts) if (!parts.includes(t)) parts.push(t);
621
+ /** @type {Array<string | null>} */
622
+ let intents = INTENTS.filter(([, re]) => re.test(text)).map(([g]) => g);
623
+ if (
624
+ intents.includes('text') &&
625
+ intents.includes('background') &&
626
+ !/background|fill|gradient|glossy/.test(text)
627
+ )
628
+ intents = intents.filter(g => g !== 'background');
629
+ if (!intents.length && parts.length) intents = [null];
630
+ const design = Object.keys(DESIGN).filter(w => text.includes(w));
631
+ const tracking = /letter ?spacing|tracking|kerning/.test(text);
632
+ /** @type {Array<{text: string, commands: string[]}>} */
633
+ const results = [];
634
+ /** @param {string | null} g */
635
+ const V = g => {
636
+ const bg = design.find(d => ['gradient', 'glossy'].includes(d));
637
+ const sh = design.find(d =>
638
+ ['bevel', 'raised', 'flat', 'sunken'].includes(d),
639
+ );
640
+ if (g === 'background' && bg) return DESIGN[bg];
641
+ if (g === 'shadow' && sh) return DESIGN[sh];
642
+ if (g === 'font' && tracking)
643
+ return /tight/.test(text) ? '-0.02em' : '0.04em';
644
+ if (g === 'font' && /bold|heav/.test(text)) return '700';
645
+ if (g === 'text' && /white/.test(text)) return '"#ffffff"';
646
+ return '<value>';
647
+ };
648
+ for (const raw of parts.slice(0, 3)) {
649
+ const [part, wp, wv] = raw.split(':');
650
+ const when = wp ? ` --when ${wp}:${wv}` : '';
651
+ const one = FAMILIES[part] ? FAMILIES[part][0] : part;
652
+ if (!TARGET.has(one)) continue;
653
+ // A part with variants: a change without --when reaches every variant.
654
+ const variants = Object.entries(C.variantValues(one)).filter(
655
+ ([p, vals]) =>
656
+ ['status', 'variant', 'tone', 'color'].includes(p) && vals.length > 1,
657
+ );
658
+ for (const g of intents.slice(0, 3)) {
659
+ if (g === null) {
660
+ results.push({
661
+ text: `what ${one} reads:`,
662
+ commands: C.explain(one, P).slice(1),
663
+ });
664
+ continue;
665
+ }
666
+ if (g === 'corners') {
667
+ results.push({
668
+ text: `corners on ${part}${when ? ` (${wp} ${wv})` : ''}`,
669
+ commands: [
670
+ `${P} set <theme> corners ${/pill|circ/.test(text) ? 'pill' : /sharp|square/.test(text) ? 'square' : 'round'} --on ${part}${when}`,
671
+ `${P} set <theme> border-radius 6px --on ${part}${when}`,
672
+ ],
673
+ });
674
+ continue;
675
+ }
676
+ if (g === 'shadow') {
677
+ const shadows = Object.keys(ctx.project.core.tokenDefaults).filter(k =>
678
+ /^--shadow-(low|med|high)$/.test(k),
679
+ );
680
+ const value =
681
+ V('shadow') !== '<value>'
682
+ ? V('shadow')
683
+ : shadows.length
684
+ ? `"var(${/soft|subtle|light/.test(text) ? shadows[0] : /strong|deep|heavy/.test(text) ? shadows[shadows.length - 1] : shadows[Math.min(1, shadows.length - 1)]})"`
685
+ : '"0 2px 6px rgb(0 0 0 / 0.2)"';
686
+ results.push({
687
+ text: `shadow on ${part}${when ? ` (${wp} ${wv})` : ''}: box-shadow${shadows.length ? `; the theme's shadow tokens are ${shadows.join(', ')}` : ''}`,
688
+ commands: [
689
+ `${P} set <theme> box-shadow ${value} --on ${part}${when}`,
690
+ ...(shadows.length
691
+ ? [
692
+ `${P} set <theme> ${shadows[0]} "0 1px 3px rgb(0 0 0 / 0.12)" # change the shared shadow everywhere`,
693
+ ]
694
+ : []),
695
+ ],
696
+ });
697
+ continue;
698
+ }
699
+ if (g === 'spacing' && !tracking) {
700
+ results.push({
701
+ text: `room inside ${part}`,
702
+ commands: [
703
+ `${P} set <theme> spacing ${/dense|compact|tight|cramped/.test(text) ? 'compact' : 'roomy'} --on ${part}`,
704
+ `${P} set <theme> padding 12px --on ${part}${when}`,
705
+ ],
706
+ });
707
+ continue;
708
+ }
709
+ const group = g === 'spacing' ? 'font' : g;
710
+ const reads = C.partReads(one, group);
711
+ const filtered = wv
712
+ ? reads.filter(
713
+ r =>
714
+ r.when.some(w => w.includes(`${wp}=${wv}`)) ||
715
+ r.when.includes(''),
716
+ )
717
+ : reads;
718
+ const list = (filtered.length ? filtered : reads).slice(0, 4);
719
+ const direct =
720
+ group === 'font' && tracking
721
+ ? 'letter-spacing'
722
+ : group === 'font' && /bold|weight|heav/.test(text)
723
+ ? 'font-weight'
724
+ : group === 'font'
725
+ ? 'font-family'
726
+ : DIRECT[group];
727
+ const tokenRoute =
728
+ (group === 'text' || group === 'background' || group === 'border') &&
729
+ !/gradient|inset/.test(V(group));
730
+ results.push({
731
+ text: `${part} ${group}${when ? ` (${wp} ${wv})` : ''}: ${list.length ? `comes from ${list.map(r => r.token + C.fmtWhen(r.when) + C.statusNote(one, r.token)).join(', ')}` : 'no token; set the property directly'}`,
732
+ commands: [
733
+ `${P} set <theme> ${direct} ${V(group)} --on ${part}${when} # direct: always works`,
734
+ ...(tokenRoute
735
+ ? list
736
+ .slice(0, 2)
737
+ .map(
738
+ r =>
739
+ `${P} set <theme> ${r.token} ${V(group)} --on ${part} # via the token${C.fmtWhen(r.when)}`,
740
+ )
741
+ : []),
742
+ ],
743
+ });
744
+ if (
745
+ !when &&
746
+ variants.length &&
747
+ ['text', 'background', 'border'].includes(group)
748
+ ) {
749
+ const [vp, vals] = variants[0];
750
+ results.push({
751
+ text: `${part} has ${vp} ${vals.join('|')}: without --when the change reaches every ${vp}. To change one, add --when ${vp}:<value>.`,
752
+ commands: [
753
+ `${P} set <theme> ${direct} ${V(group)} --on ${part} --when ${vp}:${vals[0]}`,
754
+ ],
755
+ });
756
+ }
757
+ const kids = (C.MAP[one]?.contains ?? [])
758
+ .filter(
759
+ k =>
760
+ C.partReads(k, group).length && !(FAMILIES[part] ?? []).includes(k),
761
+ )
762
+ .slice(0, 3);
763
+ if (kids.length && group === 'text') {
764
+ results.push({
765
+ text: `parts inside ${part} with their own text color: ${kids.join(', ')}. A text token set --on ${part} reaches them; a color property does not.`,
766
+ commands: kids.slice(0, 1).map(k => `${P} explain ${k}`),
767
+ });
768
+ }
769
+ }
770
+ }
771
+ if (!parts.length) {
772
+ if (tracking)
773
+ results.push({
774
+ text: 'letter spacing is a CSS property on a part; for headings there is a dial',
775
+ commands: [
776
+ `${P} set <theme> letter-spacing 0.02em --on <part>`,
777
+ `${P} set <theme> headings tight`,
778
+ ],
779
+ });
780
+ for (const g of /** @type {string[]} */ (intents.filter(Boolean)).slice(
781
+ 0,
782
+ 2,
783
+ )) {
784
+ const dial = /** @type {Record<string, string>} */ ({
785
+ corners: 'corners',
786
+ spacing: 'spacing',
787
+ font: 'font',
788
+ size: 'text-size',
789
+ })[g];
790
+ if (dial && !(tracking && (g === 'spacing' || g === 'font')))
791
+ results.push({
792
+ text: DIALS[dial].help,
793
+ commands: DIALS[dial].examples.map(e => `${P} ${e}`),
794
+ });
795
+ if (g === 'background' || g === 'text')
796
+ results.push({
797
+ text: DIALS.accent.help,
798
+ commands: DIALS.accent.examples.map(e => `${P} ${e}`),
799
+ });
800
+ results.push(...wholeThemeTokens(ctx, g));
801
+ }
802
+ }
803
+ if (!results.length) {
804
+ for (const d of Object.values(DIALS)) {
805
+ if (d.words.split(' ').some(w => w.length > 3 && text.includes(w)))
806
+ results.push({
807
+ text: d.help,
808
+ commands: d.examples.map(e => `${P} ${e}`),
809
+ });
810
+ }
811
+ }
812
+ void TOKENS;
813
+ return {
814
+ parts,
815
+ aspects: /** @type {string[]} */ (intents.filter(Boolean)),
816
+ results: results.slice(0, 10),
817
+ };
818
+ }
819
+
820
+ /**
821
+ * The tokens most parts read for one aspect, for a whole-theme change.
822
+ * @param {import('./_settings.mjs').Ctx} ctx
823
+ * @param {string} aspect
824
+ * @returns {Array<{text: string, commands: string[]}>}
825
+ */
826
+ export function wholeThemeTokens(ctx, aspect) {
827
+ const {C} = ctx;
828
+ const toks = Object.keys(ctx.project.core.tokenDefaults)
829
+ .filter(k => C.groupOfToken(k) === aspect && C.anyReader(k).length)
830
+ .sort((a, b) => C.anyReader(b).length - C.anyReader(a).length)
831
+ .slice(0, 6);
832
+ if (!toks.length) return [];
833
+ return [
834
+ {
835
+ text: `whole-theme tokens for ${aspect}, most used first: ${toks.map(k => `${k} (${C.anyReader(k).length} parts)`).join(', ')}`,
836
+ commands: [`${P} set <theme> ${toks[0]} <value>`],
837
+ },
838
+ ];
839
+ }
840
+
841
+ /** The aspects `explain` answers at the whole-theme level. */
842
+ export const ASPECTS = [...Object.keys(GROUPS), 'colors'];