@astryxdesign/cli 0.7.15 → 0.7.16-canary.g3aa244b
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/README.md +5 -3
- package/api/build/_adapter.d.mts +1 -0
- package/api/build/blocks/blocks.d.mts +1 -0
- package/api/build/blocks/blocks.mjs +18 -1
- package/api/component/_adapter.d.mts +1 -1
- package/api/component/_adapter.mjs +28 -1
- package/api/doctor/_adapter.d.mts +35 -0
- package/api/doctor/_adapter.mjs +69 -0
- package/api/doctor/built-checks.mjs +1 -1
- package/api/doctor/doctor.d.mts +18 -0
- package/api/doctor/doctor.doc.mjs +1 -1
- package/api/doctor/doctor.mjs +104 -4
- package/api/doctor/theme-checks.mjs +6 -10
- package/api/integration/add-contribution.mjs +3 -3
- package/api/integration/pack-check.mjs +14 -8
- package/api/integration/validate-integration.mjs +1 -1
- package/api/search/_adapter.mjs +15 -16
- package/api/search/_entries.d.mts +2 -1
- package/api/search/_entries.mjs +28 -1
- package/api/swizzle/_moduleGraph.mjs +1 -1
- package/api/theme/_adapter.d.mts +5 -2
- package/api/theme/_adapter.mjs +43 -12
- package/api/theme/_fork.mjs +1 -1
- package/api/theme/build/build.d.mts +116 -32
- package/api/theme/build/build.mjs +449 -102
- package/api/theme/eject/eject.mjs +22 -1
- package/api/theme/template/template.d.mts +19 -0
- package/api/theme/template/template.mjs +102 -25
- package/api/theme/theme.type.d.mts +5 -1
- package/api/theme/theme.type.mjs +2 -1
- package/api/theme/themeBuild.doc.mjs +3 -3
- package/api/theme/themeEject.doc.mjs +1 -1
- package/api/theme/themeTemplate.doc.mjs +9 -3
- package/api/theme-edit/_adapter.d.mts +32 -2
- package/api/theme-edit/_adapter.mjs +264 -40
- package/api/theme-edit/_check.d.mts +93 -4
- package/api/theme-edit/_check.mjs +1727 -223
- package/api/theme-edit/_color.d.mts +2 -8
- package/api/theme-edit/_color.mjs +292 -219
- package/api/theme-edit/_compiler.d.mts +1 -0
- package/api/theme-edit/_compiler.mjs +20 -1
- package/api/theme-edit/_context.d.mts +31 -1
- package/api/theme-edit/_context.mjs +445 -41
- package/api/theme-edit/_css.d.mts +60 -1
- package/api/theme-edit/_css.mjs +404 -28
- package/api/theme-edit/_edit.d.mts +25 -0
- package/api/theme-edit/_edit.mjs +1532 -149
- package/api/theme-edit/_how-commands.d.mts +11 -26
- package/api/theme-edit/_how-commands.mjs +30 -2
- package/api/theme-edit/_how-question.d.mts +7 -0
- package/api/theme-edit/_how-question.mjs +1304 -176
- package/api/theme-edit/_how.mjs +144 -30
- package/api/theme-edit/_overview.mjs +2 -0
- package/api/theme-edit/_palette.d.mts +38 -0
- package/api/theme-edit/_palette.mjs +305 -71
- package/api/theme-edit/_parts.d.mts +33 -0
- package/api/theme-edit/_parts.mjs +138 -9
- package/api/theme-edit/_png.d.mts +18 -0
- package/api/theme-edit/_png.mjs +95 -14
- package/api/theme-edit/_preview-app.d.mts +36 -1
- package/api/theme-edit/_preview-app.mjs +1415 -96
- package/api/theme-edit/_preview.d.mts +64 -3
- package/api/theme-edit/_preview.mjs +514 -31
- package/api/theme-edit/_project.d.mts +11 -0
- package/api/theme-edit/_project.mjs +46 -12
- package/api/theme-edit/_settings.d.mts +278 -5
- package/api/theme-edit/_settings.mjs +2741 -195
- package/api/theme-edit/_tables.d.mts +24 -0
- package/api/theme-edit/_tables.mjs +211 -149
- package/api/theme-edit/_vet.d.mts +42 -0
- package/api/theme-edit/_vet.mjs +144 -0
- package/api/theme-edit/theme-edit.type.d.mts +20 -2
- package/api/theme-edit/theme-edit.type.mjs +6 -2
- package/api/theme-edit/themeEditNew.doc.mjs +1 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +1 -1
- package/api/theme-edit/themeEditShow.doc.mjs +11 -3
- package/api/upgrade/registry/registry.mjs +6 -1
- package/assets/codemods/parse.mjs +2 -2
- package/assets/docs/tree/single-component.doc.mjs +11 -2
- package/assets/docs/tree/tokens-and-setup.doc.mjs +39 -3
- package/assets/docs.generated.json +2 -2
- package/assets/preview-app/next.config.mjs +7 -1
- package/assets/preview-app/package.json +1 -0
- package/assets/preview-app/src/app/globals.css +1 -0
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/ColumnsVirtualizationHookUsage.tsx +53 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/InfiniteScrollHookUsage.tsx +33 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/dashboard/page.tsx +1 -1
- package/authoring/doctypes/_schema.d.mts +52 -5
- package/authoring/doctypes/_schema.mjs +62 -7
- package/authoring/doctypes/base/type.ts +10 -0
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/function/function.doc.mjs +6 -0
- package/authoring/doctypes/function/type.ts +3 -0
- package/authoring/doctypes/legacy.mjs +5 -1
- package/authoring/index.d.ts +1 -0
- package/authoring/shadcn/source-variants.d.mts +1 -1
- package/authoring/shadcn/source-variants.mjs +32 -10
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/doctor.doc.mjs +2 -2
- package/clients/cli/commands/experimental/theme.mjs +239 -45
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-template.doc.mjs +2 -2
- package/clients/cli/commands/theme-build.doc.mjs +2 -2
- package/clients/cli/commands/theme-eject.doc.mjs +1 -1
- package/clients/cli/commands/theme-template.doc.mjs +3 -1
- package/clients/cli/commands/theme.mjs +190 -46
- package/clients/cli/formatters/index.mjs +7 -4
- package/clients/cli/lib/cli-error.mjs +9 -3
- package/clients/cli/lib/component-format.mjs +27 -9
- package/clients/cli/lib/json-shim.mjs +72 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/clients/cli/run.mjs +49 -0
- package/foundation/agent-docs/agent-docs-target.d.mts +2 -6
- package/foundation/agent-docs/agent-docs-target.mjs +9 -17
- package/foundation/agent-docs/agent-docs.mjs +3 -2
- package/foundation/config/config-writer.mjs +2 -2
- package/foundation/config/project.mjs +60 -14
- package/foundation/discovery/component-discovery.mjs +5 -17
- package/foundation/discovery/icon-set-writer.mjs +1 -1
- package/foundation/discovery/module-exports.mjs +1 -15
- package/foundation/discovery/template-adapter.mjs +44 -19
- package/foundation/discovery/template-source-tree.mjs +3 -16
- package/foundation/discovery/theme-discovery.d.mts +5 -1
- package/foundation/discovery/theme-discovery.mjs +61 -43
- package/foundation/fs/exact-codec.d.mts +35 -0
- package/foundation/fs/exact-codec.mjs +148 -0
- package/foundation/fs/gather-cache.d.mts +15 -5
- package/foundation/fs/gather-cache.mjs +129 -31
- package/foundation/fs/paths.d.mts +39 -0
- package/foundation/fs/paths.mjs +44 -11
- package/foundation/fs/read-record.mjs +11 -1
- package/foundation/integrations/contribution-fixes.mjs +1 -16
- package/foundation/integrations/may-have-integrations.mjs +3 -2
- package/foundation/integrations/public-imports.mjs +1 -1
- package/foundation/render/embedded-imports.mjs +1 -1
- package/foundation/render/host-exit.d.mts +8 -0
- package/foundation/render/host-exit.mjs +11 -0
- package/foundation/render/page-host.mjs +56 -10
- package/foundation/render/page-worker.mjs +14 -0
- package/foundation/render/render-page.mjs +37 -2
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.d.ts +1 -0
- package/foundation/response/error-codes.doc.mjs +5 -0
- package/foundation/response/error-codes.mjs +3 -0
- package/foundation/response/json.d.mts +15 -0
- package/foundation/response/json.mjs +52 -1
- package/foundation/response/response-types.doc.mjs +1 -1
- package/foundation/runtime/lazy-dependencies.d.mts +14 -0
- package/foundation/runtime/lazy-dependencies.mjs +82 -0
- package/foundation/xle/browser.d.mts +2 -2
- package/package.json +7 -6
|
@@ -9,16 +9,21 @@
|
|
|
9
9
|
* @position api/theme-edit — checks over the part data
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
|
+
import {AstryxError} from '../error.mjs';
|
|
12
13
|
import * as fs from 'node:fs';
|
|
13
14
|
import * as path from 'node:path';
|
|
15
|
+
import {partForWord} from './_how-question.mjs';
|
|
14
16
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
15
|
-
import {baseThemeFile} from './_project.mjs';
|
|
17
|
+
import {baseInstalled, baseThemeFile, installLine} from './_project.mjs';
|
|
16
18
|
import {levenshteinDistance} from '../../foundation/text/levenshtein.mjs';
|
|
17
19
|
import {
|
|
20
|
+
withFallbacks,
|
|
18
21
|
MATH_FUNCTION,
|
|
22
|
+
cssNumber,
|
|
19
23
|
belowRange,
|
|
20
24
|
fontShorthandParts,
|
|
21
25
|
cssProperties,
|
|
26
|
+
familyListMeant,
|
|
22
27
|
grammarText,
|
|
23
28
|
initialOf,
|
|
24
29
|
keywordsOf,
|
|
@@ -28,6 +33,7 @@ import {
|
|
|
28
33
|
propertyGrammar,
|
|
29
34
|
takesPercentage,
|
|
30
35
|
} from './_css.mjs';
|
|
36
|
+
import {calcVars, coreFallback, multipliedTokens} from './_parts.mjs';
|
|
31
37
|
import {
|
|
32
38
|
AUTO_SPREAD,
|
|
33
39
|
COLOR_PROP_PARTS,
|
|
@@ -50,7 +56,11 @@ import {
|
|
|
50
56
|
SPACING,
|
|
51
57
|
TEXT_SIZE,
|
|
52
58
|
THING_HINTS,
|
|
59
|
+
TYPE_STYLES,
|
|
53
60
|
familyOf,
|
|
61
|
+
wordTable,
|
|
62
|
+
WRAPPED_PARTS,
|
|
63
|
+
CARD_LIKE,
|
|
54
64
|
} from './_tables.mjs';
|
|
55
65
|
import {
|
|
56
66
|
COLOR_NAMES,
|
|
@@ -83,6 +93,14 @@ import {
|
|
|
83
93
|
radiusTokenOf,
|
|
84
94
|
focusStateOf,
|
|
85
95
|
valueProblem,
|
|
96
|
+
partComponentDocs,
|
|
97
|
+
themeFiles,
|
|
98
|
+
ownSourceTokenValues,
|
|
99
|
+
baseTopTokens,
|
|
100
|
+
baseComponentReads,
|
|
101
|
+
partRuleTokens,
|
|
102
|
+
cssFamily,
|
|
103
|
+
familySpaces,
|
|
86
104
|
} from './_settings.mjs';
|
|
87
105
|
import {quoteCommandArgument} from '../../foundation/env/package-manager.mjs';
|
|
88
106
|
|
|
@@ -104,6 +122,21 @@ function tokenPx(base, token) {
|
|
|
104
122
|
return lengthPx(v);
|
|
105
123
|
}
|
|
106
124
|
|
|
125
|
+
/**
|
|
126
|
+
* The `type` preset nearest a scale ratio typed as a number (1.3 is
|
|
127
|
+
* editorial), or null for a number that is no ratio (16).
|
|
128
|
+
* @param {string} v
|
|
129
|
+
* @returns {string | null}
|
|
130
|
+
*/
|
|
131
|
+
function ratioPreset(v) {
|
|
132
|
+
if (!/^\d+(\.\d+)?$/.test(v)) return null;
|
|
133
|
+
const r = Number(v);
|
|
134
|
+
if (!(r > 1 && r <= 2)) return null;
|
|
135
|
+
return Object.entries(TYPE_STYLES).reduce((a, b) =>
|
|
136
|
+
Math.abs(b[1].ratio - r) < Math.abs(a[1].ratio - r) ? b : a,
|
|
137
|
+
)[0];
|
|
138
|
+
}
|
|
139
|
+
|
|
107
140
|
/**
|
|
108
141
|
* A `set` (or `undo`) command for a setting, with its scope, to suggest.
|
|
109
142
|
* @param {string} slug
|
|
@@ -112,7 +145,7 @@ function tokenPx(base, token) {
|
|
|
112
145
|
* @param {{on?: string, when?: string, state?: string, mode?: string, only?: boolean}} s
|
|
113
146
|
* @param {boolean} [unset]
|
|
114
147
|
*/
|
|
115
|
-
const setLine = (slug, thing, value, s, unset = false) =>
|
|
148
|
+
export const setLine = (slug, thing, value, s, unset = false) =>
|
|
116
149
|
`${P} ${unset ? 'undo' : 'set'} ${slug} ${thing}${value ? ` ${quoteCommandArgument(value)}` : ''}${s.on ? ` --on ${s.on}` : ''}${s.when ? ` --when ${s.when}` : ''}${s.state ? ` --state ${s.state}` : ''}${s.mode ? ` --mode ${s.mode}` : ''}${s.only ? ' --only' : ''}`;
|
|
117
150
|
|
|
118
151
|
/** @param {string} v */
|
|
@@ -121,6 +154,298 @@ const cornerPxOf = v =>
|
|
|
121
154
|
|
|
122
155
|
const E = ERROR_CODES;
|
|
123
156
|
|
|
157
|
+
/**
|
|
158
|
+
* The fallbacks of the var() calls inside a value that is not one var()
|
|
159
|
+
* alone (`calc(var(--x, 4px) * 2)`), in order. A var() with an empty
|
|
160
|
+
* fallback (`var(--x,)`) gives ''.
|
|
161
|
+
* @param {string} value
|
|
162
|
+
* @returns {Array<{fallback: string, within: string[]}>} each fallback, with
|
|
163
|
+
* the names of the functions around its var(), innermost first
|
|
164
|
+
*/
|
|
165
|
+
function innerVarFallbacks(value) {
|
|
166
|
+
const text = value.trim();
|
|
167
|
+
if (/^var\(/i.test(text) && matchingParen(text, 3) === text.length - 1)
|
|
168
|
+
return [];
|
|
169
|
+
/** @type {Array<{fallback: string, within: string[]}>} */
|
|
170
|
+
const out = [];
|
|
171
|
+
const re = /\bvar\(/gi;
|
|
172
|
+
let m;
|
|
173
|
+
while ((m = re.exec(text))) {
|
|
174
|
+
const open = m.index + m[0].length - 1;
|
|
175
|
+
const close = matchingParen(text, open);
|
|
176
|
+
if (close < 0) break;
|
|
177
|
+
const inner = text.slice(open + 1, close);
|
|
178
|
+
let depth = 0;
|
|
179
|
+
for (let i = 0; i < inner.length; i += 1) {
|
|
180
|
+
const c = inner[i];
|
|
181
|
+
if (c === '(') depth += 1;
|
|
182
|
+
else if (c === ')') depth -= 1;
|
|
183
|
+
else if (c === ',' && depth === 0) {
|
|
184
|
+
out.push({
|
|
185
|
+
fallback: inner.slice(i + 1).trim(),
|
|
186
|
+
within: enclosingFunctions(text, m.index),
|
|
187
|
+
});
|
|
188
|
+
break;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return out;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* The names of the functions whose parentheses are open at `at`, innermost
|
|
197
|
+
* first.
|
|
198
|
+
* @param {string} text
|
|
199
|
+
* @param {number} at
|
|
200
|
+
* @returns {string[]}
|
|
201
|
+
*/
|
|
202
|
+
function enclosingFunctions(text, at) {
|
|
203
|
+
/** @type {string[]} */
|
|
204
|
+
const open = [];
|
|
205
|
+
for (let i = 0; i < at; i += 1) {
|
|
206
|
+
if (text[i] === '(')
|
|
207
|
+
open.push(/([a-z-]*)$/i.exec(text.slice(0, i))?.[1] ?? '');
|
|
208
|
+
else if (text[i] === ')') open.pop();
|
|
209
|
+
}
|
|
210
|
+
return open.reverse();
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* The index of the parenthesis that closes the one at `open`, or -1.
|
|
215
|
+
* @param {string} text
|
|
216
|
+
* @param {number} open
|
|
217
|
+
*/
|
|
218
|
+
function matchingParen(text, open) {
|
|
219
|
+
let depth = 0;
|
|
220
|
+
for (let i = open; i < text.length; i += 1) {
|
|
221
|
+
if (text[i] === '(') depth += 1;
|
|
222
|
+
else if (text[i] === ')') {
|
|
223
|
+
depth -= 1;
|
|
224
|
+
if (depth === 0) return i;
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return -1;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* A text size written another way (20px, 2e1, +20, 1.25rem, 125%, `20 px`,
|
|
232
|
+
* `1,25rem`) as the body size in pixels text-size takes, kept to its 10 to
|
|
233
|
+
* 32 (a size past an end is that end); null when it reads as no size.
|
|
234
|
+
* @param {string} v
|
|
235
|
+
* @returns {number | null}
|
|
236
|
+
*/
|
|
237
|
+
function textSizePx(v) {
|
|
238
|
+
const m = /^\+?(\d*\.?\d+(?:e[+-]?\d+)?)(px|rem|em|pt|%)?$/i.exec(
|
|
239
|
+
typedPlain(v)
|
|
240
|
+
.replace(/(\d)\.(?=[a-z%]*$)/i, '$1')
|
|
241
|
+
.replace(/\s+/g, '')
|
|
242
|
+
.replace(/^(\d+),(\d+)/, '$1.$2'),
|
|
243
|
+
);
|
|
244
|
+
if (!m) return null;
|
|
245
|
+
const unit = (m[2] ?? '').toLowerCase();
|
|
246
|
+
const n =
|
|
247
|
+
Number(m[1]) *
|
|
248
|
+
(/r?em/.test(unit)
|
|
249
|
+
? 16
|
|
250
|
+
: unit === '%'
|
|
251
|
+
? 16 / 100
|
|
252
|
+
: unit === 'pt'
|
|
253
|
+
? 4 / 3
|
|
254
|
+
: 1);
|
|
255
|
+
const px = Math.round(n * 100) / 100;
|
|
256
|
+
return Math.max(10, Math.min(32, px));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* A value as it reads once what an input method or a stylesheet adds is
|
|
261
|
+
* taken off: full-width digits, letters and signs (`18`, `#`) as their
|
|
262
|
+
* plain forms, and CSS comments.
|
|
263
|
+
* @param {string} v
|
|
264
|
+
*/
|
|
265
|
+
function typedPlain(v) {
|
|
266
|
+
return v
|
|
267
|
+
.normalize('NFKC')
|
|
268
|
+
.replace(/\/\*[\s\S]*?\*\//g, ' ')
|
|
269
|
+
.replace(/\s+/g, ' ')
|
|
270
|
+
.trim();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A value pasted as a CSS declaration (`color: #e11d48;`, `font-weight: 700
|
|
275
|
+
* !important`), from devtools or a stylesheet: its property and value, with
|
|
276
|
+
* the `;` and `!important` gone. Null when the value is not a declaration (a
|
|
277
|
+
* URL's `https:` is not a property).
|
|
278
|
+
* @param {string} v
|
|
279
|
+
* @returns {{property: string, value: string} | null}
|
|
280
|
+
*/
|
|
281
|
+
export function pastedDeclaration(v) {
|
|
282
|
+
const plain = typedPlain(v);
|
|
283
|
+
const m = /^(--[a-z0-9-]+|[a-z][a-z-]*)\s*:\s*(?!\/\/)(.*)$/i.exec(plain);
|
|
284
|
+
if (!m || /^(https?|data|file|blob)$/i.test(m[1])) return null;
|
|
285
|
+
const value = m[2]
|
|
286
|
+
.replace(/;+\s*$/, '')
|
|
287
|
+
.replace(/\s*!\s*important\s*$/i, '')
|
|
288
|
+
.replace(/;+\s*$/, '')
|
|
289
|
+
.trim();
|
|
290
|
+
// `font-family: ;` names a property and no value: value is ''.
|
|
291
|
+
return {property: m[1].toLowerCase(), value};
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* A font pasted as a declaration (`font-family: Georgia, serif;`, from
|
|
296
|
+
* devtools or a stylesheet), as the family list it names; a `font:`
|
|
297
|
+
* shorthand gives its family; '' when it names the property and no value.
|
|
298
|
+
* Null when the value is not a pasted declaration.
|
|
299
|
+
* @param {string} v
|
|
300
|
+
* @returns {string | null}
|
|
301
|
+
*/
|
|
302
|
+
export function pastedFont(v) {
|
|
303
|
+
const d = pastedDeclaration(v);
|
|
304
|
+
if (!d || !/^font(-family)?$/.test(d.property)) return null;
|
|
305
|
+
const rest = d.value;
|
|
306
|
+
// `font-family: ;` gives no font: '' (the caller refuses it).
|
|
307
|
+
if (!rest) return '';
|
|
308
|
+
if (d.property === 'font-family') return rest;
|
|
309
|
+
const parts = fontShorthandParts(rest);
|
|
310
|
+
return parts && typeof parts['font-family'] === 'string'
|
|
311
|
+
? parts['font-family']
|
|
312
|
+
: null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The values a value may have meant, when what is wrong is how it was
|
|
317
|
+
* typed: plain (full-width characters, comments and a trailing `;` gone,
|
|
318
|
+
* quotes around a whole non-font value dropped), and for a font list each
|
|
319
|
+
* family quoted as CSS needs it (`Font Awesome 6 Free`). Only values that
|
|
320
|
+
* differ from the one typed.
|
|
321
|
+
* @param {string} type
|
|
322
|
+
* @param {string} value
|
|
323
|
+
*/
|
|
324
|
+
function valueMeant(type, value) {
|
|
325
|
+
// `!important` is not part of a token's value (the theme places its
|
|
326
|
+
// rules itself): the value without it.
|
|
327
|
+
/** @type {string} */
|
|
328
|
+
let p = (
|
|
329
|
+
type === 'font' || type === 'font-family'
|
|
330
|
+
? pastedFont(value) || value
|
|
331
|
+
: pastedDeclaration(value)?.value || typedPlain(value)
|
|
332
|
+
)
|
|
333
|
+
.replace(/;+\s*$/, '')
|
|
334
|
+
.replace(/\s*!\s*important\s*$/i, '')
|
|
335
|
+
.replace(/;+\s*$/, '')
|
|
336
|
+
.trim();
|
|
337
|
+
const q = /^(["'])(.*)\1$/.exec(p);
|
|
338
|
+
if (q && type !== 'font') p = q[2].trim();
|
|
339
|
+
const out = [p];
|
|
340
|
+
// A var() of the app's own variable with a fallback: the fallback is the
|
|
341
|
+
// value meant (`var(--brand, #e11d48)` → #e11d48).
|
|
342
|
+
const fb = /^var\(\s*--[a-z0-9-]+\s*,\s*(.+)\)$/i.exec(p);
|
|
343
|
+
if (fb) out.push(fb[1].trim());
|
|
344
|
+
if (type === 'font' || type === 'font-family') {
|
|
345
|
+
// A comma left out between families (`"Open Sans" serif`), read with
|
|
346
|
+
// css-tree.
|
|
347
|
+
const listed = familyListMeant(p);
|
|
348
|
+
if (listed) out.push(listed);
|
|
349
|
+
// Each family quoted as CSS needs it, only when no family holds a
|
|
350
|
+
// quote inside (the pieces are whole names).
|
|
351
|
+
const pieces = p.split(',').map(f => f.trim().replace(/^["']|["']$/g, ''));
|
|
352
|
+
if (!listed && pieces.every(f => !/["'\\]/.test(f)))
|
|
353
|
+
out.push(pieces.map(f => cssFamily(f, true)).join(', '));
|
|
354
|
+
}
|
|
355
|
+
return [...new Set(out)].filter(x => x && x !== value.trim());
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Whether a value is one length that works inside calc(): a length with a
|
|
360
|
+
* unit, a variable (with a fallback that is one too), or calc(), min(),
|
|
361
|
+
* max() or clamp() giving a length; never several values, a bare number
|
|
362
|
+
* (calc() cannot add one to a length, 0 included), a percentage or a
|
|
363
|
+
* keyword.
|
|
364
|
+
* @param {string} v
|
|
365
|
+
* @returns {boolean}
|
|
366
|
+
*/
|
|
367
|
+
function oneLength(v) {
|
|
368
|
+
const t = v.trim();
|
|
369
|
+
if (splitTop(t, ' ').length !== 1) return false;
|
|
370
|
+
const ref = /^var\(\s*--[a-z0-9-]+\s*(?:,(.*))?\)$/i.exec(t);
|
|
371
|
+
if (ref) return ref[1] == null || oneLength(ref[1]);
|
|
372
|
+
if (MATH_FUNCTION.test(t)) return matchType('length', t) != null;
|
|
373
|
+
const n = cssNumber(t);
|
|
374
|
+
return !!n && !!n.unit && n.unit !== '%';
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** The size units a typo is read against. */
|
|
378
|
+
const SIZE_UNITS = ['px', 'rem', 'em', '%', 'vh', 'vw', 'ch'];
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* The size meant by a value one slip away from one: a space inside
|
|
382
|
+
* (`16 px`), a decimal comma (`1,5rem`), a doubled or mistyped unit
|
|
383
|
+
* (`16pxx`), a dot with no digit after it (`1.px`); null when none reads.
|
|
384
|
+
* @param {string} v
|
|
385
|
+
* @param {(x: string) => boolean} ok
|
|
386
|
+
*/
|
|
387
|
+
function sizeMeant(v, ok) {
|
|
388
|
+
const t = v.trim();
|
|
389
|
+
const n = typedPlain(t);
|
|
390
|
+
const tries = [
|
|
391
|
+
n,
|
|
392
|
+
n.replace(/(\d)\.(?=[a-z%]|$)/gi, '$1'),
|
|
393
|
+
t.replace(/(\d)\.(?=[a-z%]|$)/gi, '$1'),
|
|
394
|
+
t.replace(/\s+/g, ''),
|
|
395
|
+
t.replace(/^([+-]?\d+),(\d+)/, '$1.$2').replace(/\s+/g, ''),
|
|
396
|
+
mathSpaced(t),
|
|
397
|
+
];
|
|
398
|
+
const m = /^([+-]?\d*\.?\d+)\s*([a-z%]+)$/i.exec(
|
|
399
|
+
t.replace(/^([+-]?\d+),(\d+)/, '$1.$2'),
|
|
400
|
+
);
|
|
401
|
+
if (m) {
|
|
402
|
+
// A unit typed with a letter too many (pxx, remm, pxs).
|
|
403
|
+
const unit = [...SIZE_UNITS]
|
|
404
|
+
.sort((a, b) => b.length - a.length)
|
|
405
|
+
.find(u => m[2].toLowerCase().startsWith(u));
|
|
406
|
+
if (unit) tries.push(`${m[1]}${unit}`);
|
|
407
|
+
}
|
|
408
|
+
return tries.find(x => x !== t && ok(x)) ?? null;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* A math function with the spaces CSS needs around `+` and `-` put back
|
|
413
|
+
* (`calc(1rem+2px)` is `calc(1rem + 2px)`); the value as it is otherwise.
|
|
414
|
+
* @param {string} v
|
|
415
|
+
*/
|
|
416
|
+
function mathSpaced(v) {
|
|
417
|
+
if (!/^(calc|min|max|clamp)\(/i.test(v) || /\bvar\(/i.test(v)) return v;
|
|
418
|
+
return v.replace(/(\d(?<![\d.]e)|[%a-z)])([+-])(?=[\d.(])/gi, '$1 $2 ');
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/**
|
|
422
|
+
* The number meant by a value one slip away from one: a unit on a number
|
|
423
|
+
* (`700px`), a decimal comma (`1,5`), letter O for zero (`7OO`), a weight
|
|
424
|
+
* past the range (`1001` is 1000), a math function with no spaces around
|
|
425
|
+
* `+` or `-`; null when none reads.
|
|
426
|
+
* @param {string} v
|
|
427
|
+
* @param {string} name the token
|
|
428
|
+
* @param {(x: string) => boolean} ok
|
|
429
|
+
*/
|
|
430
|
+
function numberMeant(v, name, ok) {
|
|
431
|
+
const t = v.trim();
|
|
432
|
+
const plain = typedPlain(t).replace(/(\d)\.$/, '$1');
|
|
433
|
+
const comma = plain.replace(/^([+-]?\d+),(\d+)/, '$1.$2');
|
|
434
|
+
const tries = [
|
|
435
|
+
/^[\doO.]+$/.test(plain) && /\d/.test(plain)
|
|
436
|
+
? plain.replace(/[oO]/g, '0')
|
|
437
|
+
: plain,
|
|
438
|
+
comma.replace(/^([+-]?\d*\.?\d+)\s*[a-z]+$/i, '$1'),
|
|
439
|
+
comma,
|
|
440
|
+
mathSpaced(t),
|
|
441
|
+
];
|
|
442
|
+
if (/weight/.test(name)) {
|
|
443
|
+
const n = Number(tries[1]);
|
|
444
|
+
if (Number.isFinite(n)) tries.push(String(Math.min(1000, Math.max(1, n))));
|
|
445
|
+
}
|
|
446
|
+
return tries.find(x => x !== t && ok(x)) ?? null;
|
|
447
|
+
}
|
|
448
|
+
|
|
124
449
|
/**
|
|
125
450
|
* The value to carry into a suggestion for another dial: the one given when
|
|
126
451
|
* that dial takes it, else the dial's example.
|
|
@@ -190,11 +515,23 @@ const COLOR_DIAL_WORDS = (() => {
|
|
|
190
515
|
* @param {import('./_settings.mjs').Ctx} ctx
|
|
191
516
|
* @param {string} slug
|
|
192
517
|
* @param {import('./_settings.mjs').ThemeData} data
|
|
193
|
-
* @param {string} [skip]
|
|
194
|
-
* @returns {Array<{setting: import('./_settings.mjs').Setting, why: string, scope?: boolean}>} every saved setting the editor no longer accepts
|
|
518
|
+
* @param {string | {thing: string, on?: string, when?: string, state?: string, mode?: string}} [skip]
|
|
519
|
+
* @returns {Array<{setting: import('./_settings.mjs').Setting, why: string, scope?: boolean, fix?: string}>} every saved setting the editor no longer accepts (`fix`: the clean value to set, when one reads)
|
|
195
520
|
*/
|
|
196
521
|
export function storedProblems(ctx, slug, data, skip = '') {
|
|
197
|
-
|
|
522
|
+
// The setting the command replaces (its thing in the same scope); a
|
|
523
|
+
// setting of the same thing elsewhere is still checked.
|
|
524
|
+
const skipThing = typeof skip === 'string' ? skip : skip.thing;
|
|
525
|
+
/** @param {import('./_settings.mjs').Setting} s */
|
|
526
|
+
const replaced = s =>
|
|
527
|
+
typeof skip === 'string'
|
|
528
|
+
? s.thing === skip
|
|
529
|
+
: s.thing === skip.thing &&
|
|
530
|
+
(s.on ?? '') === (skip.on ?? '') &&
|
|
531
|
+
(s.when ?? '') === (skip.when ?? '') &&
|
|
532
|
+
(s.state ?? '') === (skip.state ?? '') &&
|
|
533
|
+
(s.mode ?? '') === (skip.mode ?? '');
|
|
534
|
+
/** @type {Array<{setting: import('./_settings.mjs').Setting, why: string, scope?: boolean, fix?: string}>} */
|
|
198
535
|
const found = [];
|
|
199
536
|
const TOKENS = ctx.project.core.tokenDefaults;
|
|
200
537
|
// Where each saved setting applies names a real part, group, state and
|
|
@@ -211,7 +548,7 @@ export function storedProblems(ctx, slug, data, skip = '') {
|
|
|
211
548
|
...PALETTE_DIALS,
|
|
212
549
|
];
|
|
213
550
|
for (const s of data.settings) {
|
|
214
|
-
if (s.thing ===
|
|
551
|
+
if (s.thing === skipThing) continue;
|
|
215
552
|
/** @param {string} why */
|
|
216
553
|
const scope = why => ({setting: s, why, scope: true});
|
|
217
554
|
const problem = (() => {
|
|
@@ -223,6 +560,32 @@ export function storedProblems(ctx, slug, data, skip = '') {
|
|
|
223
560
|
!(s.thing === 'font' && FONT_ROLES.includes(s.on))
|
|
224
561
|
)
|
|
225
562
|
return scope(`there is no part or group "${s.on}"`);
|
|
563
|
+
// A token that is no design token, or a part property that is no
|
|
564
|
+
// CSS property or names no part, styles nothing: reported, never
|
|
565
|
+
// built.
|
|
566
|
+
if (s.source === 'token' && !(s.thing in TOKENS))
|
|
567
|
+
return {setting: s, why: `${s.thing} is not a design token`};
|
|
568
|
+
if (s.source === 'property' && !s.on)
|
|
569
|
+
return {
|
|
570
|
+
setting: s,
|
|
571
|
+
why: `${s.thing} is a CSS property, so it needs a part (--on)`,
|
|
572
|
+
};
|
|
573
|
+
if (
|
|
574
|
+
s.source === 'property' &&
|
|
575
|
+
!s.thing.startsWith('--') &&
|
|
576
|
+
!cssProperties().has(s.thing)
|
|
577
|
+
)
|
|
578
|
+
return {setting: s, why: `${s.thing} is not a CSS property`};
|
|
579
|
+
// A value its CSS grammar does not take (a hand edit: padding
|
|
580
|
+
// banana) is dropped by the browser: reported by name, never built.
|
|
581
|
+
if (s.source === 'property' && !s.thing.startsWith('--')) {
|
|
582
|
+
const bad = propertyProblem(s.thing, String(s.value ?? ''));
|
|
583
|
+
if (bad)
|
|
584
|
+
return {
|
|
585
|
+
setting: s,
|
|
586
|
+
why: `${s.thing} takes ${bad.takes}, not "${String(s.value ?? '').slice(0, 60)}"`,
|
|
587
|
+
};
|
|
588
|
+
}
|
|
226
589
|
if (s.source === 'dial' && s.on && WHOLE_ONLY.includes(s.thing))
|
|
227
590
|
return scope(`${s.thing} applies to the whole theme, not to ${s.on}`);
|
|
228
591
|
if (s.mode && s.source === 'dial' && !PALETTE_DIALS.includes(s.thing))
|
|
@@ -240,9 +603,70 @@ export function storedProblems(ctx, slug, data, skip = '') {
|
|
|
240
603
|
})();
|
|
241
604
|
if (problem) found.push(problem);
|
|
242
605
|
}
|
|
606
|
+
// Every part setting and the font dial are checked as set checks a value
|
|
607
|
+
// typed in (a CSS keyword where no family is, one length where Core reads
|
|
608
|
+
// a padding inside calc(), a value the property's grammar refuses …), so
|
|
609
|
+
// a hand edit set would refuse is reported, never built, as compile does.
|
|
610
|
+
const scoped = new Set(found.map(f => f.setting));
|
|
243
611
|
for (const s of data.settings) {
|
|
244
|
-
if (s
|
|
612
|
+
if (replaced(s) || scoped.has(s)) continue;
|
|
613
|
+
if (!(
|
|
614
|
+
s.source === 'property' ||
|
|
615
|
+
(s.source === 'dial' && s.thing === 'font')
|
|
616
|
+
))
|
|
245
617
|
continue;
|
|
618
|
+
try {
|
|
619
|
+
checkSetting(
|
|
620
|
+
ctx,
|
|
621
|
+
slug,
|
|
622
|
+
{
|
|
623
|
+
...data,
|
|
624
|
+
// A saved setting for one mode stays saved when the theme keeps
|
|
625
|
+
// one mode (it applies again with both): the rule that refuses
|
|
626
|
+
// --mode on a one-mode theme is for a value typed in.
|
|
627
|
+
...(s.mode ? {mode: 'both'} : {}),
|
|
628
|
+
settings: data.settings.filter(x => x !== s),
|
|
629
|
+
},
|
|
630
|
+
s.thing,
|
|
631
|
+
s.value,
|
|
632
|
+
{on: s.on, when: s.when, state: s.state, mode: s.mode, only: s.only},
|
|
633
|
+
);
|
|
634
|
+
} catch (e) {
|
|
635
|
+
if (!(e instanceof AstryxError)) throw e;
|
|
636
|
+
found.push({
|
|
637
|
+
setting: s,
|
|
638
|
+
why:
|
|
639
|
+
e instanceof Error
|
|
640
|
+
? e.message.replace(/ Nothing was changed\.$/, '')
|
|
641
|
+
: String(e),
|
|
642
|
+
});
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
// A hand edit that put an invisible or non-breaking space in any value
|
|
646
|
+
// (the browser reads `rgb(1\u00A02\u00A03)` as invalid, a family with a
|
|
647
|
+
// zero-width space as another font): named, with the value as set writes
|
|
648
|
+
// it.
|
|
649
|
+
for (const s of data.settings) {
|
|
650
|
+
if (replaced(s) || typeof s.value !== 'string') continue;
|
|
651
|
+
const clean = normalValueText(s.value);
|
|
652
|
+
if (clean !== s.value)
|
|
653
|
+
found.push({
|
|
654
|
+
setting: s,
|
|
655
|
+
why: `${s.thing} holds a no-break, thin or zero-width space, which the browser does not read as a space`,
|
|
656
|
+
fix: clean,
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
for (const s of data.settings) {
|
|
660
|
+
if (s.source !== 'token' || s.thing === skipThing || !(s.thing in TOKENS))
|
|
661
|
+
continue;
|
|
662
|
+
if (found.some(f => f.setting === s)) continue;
|
|
663
|
+
// A var() a hand edit put in that names no design token reads nothing,
|
|
664
|
+
// as set refuses it.
|
|
665
|
+
const bad = unknownVarRef(String(s.value ?? ''), TOKENS, s.thing);
|
|
666
|
+
if (bad) {
|
|
667
|
+
found.push({setting: s, why: bad.message});
|
|
668
|
+
continue;
|
|
669
|
+
}
|
|
246
670
|
try {
|
|
247
671
|
checkTypedValue(
|
|
248
672
|
ctx.C.typeOf(s.thing),
|
|
@@ -252,15 +676,24 @@ export function storedProblems(ctx, slug, data, skip = '') {
|
|
|
252
676
|
ctx.C.typeOf,
|
|
253
677
|
slug,
|
|
254
678
|
ctx.C.propsReading(s.thing),
|
|
679
|
+
multipliedTokens(ctx.project).has(s.thing),
|
|
255
680
|
);
|
|
256
|
-
checkCycle(s.thing, s.value, TOKENS, data);
|
|
681
|
+
checkCycle(s.thing, s.value, TOKENS, data, ctx.project.cwd, s);
|
|
257
682
|
} catch (e) {
|
|
683
|
+
// The value the check itself suggests, as for typed input.
|
|
684
|
+
const line = /** @type {any} */ (e)?.suggestions?.find(
|
|
685
|
+
(/** @type {any} */ x) =>
|
|
686
|
+
x.name.startsWith(`${P} set ${slug} ${s.thing} `),
|
|
687
|
+
)?.name;
|
|
258
688
|
found.push({
|
|
259
689
|
setting: s,
|
|
260
690
|
why:
|
|
261
691
|
e instanceof Error
|
|
262
692
|
? e.message.replace(/ Nothing was changed\.$/, '')
|
|
263
693
|
: String(e),
|
|
694
|
+
...(line
|
|
695
|
+
? {fix: unquoteArg(line.slice(`${P} set ${slug} ${s.thing} `.length))}
|
|
696
|
+
: {}),
|
|
264
697
|
});
|
|
265
698
|
}
|
|
266
699
|
}
|
|
@@ -305,6 +738,7 @@ function groupOfPropType(prop) {
|
|
|
305
738
|
* @param {(token: string) => string} typeOf
|
|
306
739
|
* @param {string} slug
|
|
307
740
|
* @param {string[]} [readers] the CSS properties that read the token
|
|
741
|
+
* @param {boolean} [multiplied] whether Core multiplies the token by a size
|
|
308
742
|
*/
|
|
309
743
|
function checkTypedValue(
|
|
310
744
|
type,
|
|
@@ -314,21 +748,58 @@ function checkTypedValue(
|
|
|
314
748
|
typeOf,
|
|
315
749
|
slug,
|
|
316
750
|
readers = [],
|
|
751
|
+
multiplied = false,
|
|
317
752
|
) {
|
|
753
|
+
// The value meant, when what is wrong is how it was typed (a trailing
|
|
754
|
+
// `;`, a comment, quotes around it, full-width characters, a font name
|
|
755
|
+
// that needs quotes): the same value, if the token takes it.
|
|
756
|
+
const meant = () => {
|
|
757
|
+
for (const c of valueMeant(type, value))
|
|
758
|
+
try {
|
|
759
|
+
checkTypedValue(
|
|
760
|
+
type,
|
|
761
|
+
c,
|
|
762
|
+
name,
|
|
763
|
+
TOKENS,
|
|
764
|
+
typeOf,
|
|
765
|
+
slug,
|
|
766
|
+
readers,
|
|
767
|
+
multiplied,
|
|
768
|
+
);
|
|
769
|
+
return c;
|
|
770
|
+
} catch {
|
|
771
|
+
// Not this one.
|
|
772
|
+
}
|
|
773
|
+
return null;
|
|
774
|
+
};
|
|
318
775
|
/** @param {string} what @param {string} example */
|
|
319
776
|
const bad = (what, example) =>
|
|
320
777
|
refuse(
|
|
321
778
|
E.ERR_THEME_BAD_VALUE,
|
|
322
779
|
`${name} is ${what}; "${value}" is not one.`,
|
|
323
|
-
{
|
|
780
|
+
{
|
|
781
|
+
run: [
|
|
782
|
+
`${P} set ${slug} ${name} ${quoteCommandArgument(meant() ?? example)}`,
|
|
783
|
+
],
|
|
784
|
+
},
|
|
324
785
|
);
|
|
786
|
+
// A font file or a link is no family name: never quoted into one.
|
|
787
|
+
if (type === 'font') {
|
|
788
|
+
const file = fontFileReason(pastedFont(value) || value);
|
|
789
|
+
if (file)
|
|
790
|
+
refuse(E.ERR_THEME_BAD_VALUE, file, {
|
|
791
|
+
run: [
|
|
792
|
+
`${P} set ${slug} ${name} ${quoteCommandArgument(exampleOf(type, name))}`,
|
|
793
|
+
],
|
|
794
|
+
});
|
|
795
|
+
}
|
|
325
796
|
if (!isSafeValue(value))
|
|
326
797
|
refuse(
|
|
327
798
|
E.ERR_THEME_BAD_VALUE,
|
|
328
|
-
`"${value}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote
|
|
799
|
+
`"${value}" cannot be a CSS value: it has a ; { } < > or a newline, a comment, !important, or an unclosed bracket or quote.${/!\s*important/i.test(value) ? ' !important is not used: the theme places its own rules.' : ''}`,
|
|
329
800
|
{
|
|
330
801
|
run: [
|
|
331
|
-
`${P} set ${slug} ${name} ${quoteCommandArgument(exampleOf(type, name))}`,
|
|
802
|
+
`${P} set ${slug} ${name} ${quoteCommandArgument(meant() ?? exampleOf(type, name))}`,
|
|
332
803
|
],
|
|
333
804
|
},
|
|
334
805
|
);
|
|
@@ -350,6 +821,41 @@ function checkTypedValue(
|
|
|
350
821
|
],
|
|
351
822
|
},
|
|
352
823
|
);
|
|
824
|
+
// A var() inside a larger value (calc, max, color-mix) has a fallback of
|
|
825
|
+
// the token's kind too, checked like a whole-value var().
|
|
826
|
+
if (type !== 'other')
|
|
827
|
+
for (const {fallback, within} of innerVarFallbacks(value)) {
|
|
828
|
+
// Inside a color's channels (rgb(var(--r, 0) 0 0)) a fallback is a
|
|
829
|
+
// channel, not a color.
|
|
830
|
+
if (
|
|
831
|
+
type === 'color' &&
|
|
832
|
+
within.some(f =>
|
|
833
|
+
/^(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)$/i.test(f),
|
|
834
|
+
)
|
|
835
|
+
)
|
|
836
|
+
continue;
|
|
837
|
+
if (fallback === '')
|
|
838
|
+
refuse(
|
|
839
|
+
E.ERR_THEME_BAD_VALUE,
|
|
840
|
+
`${name} is a ${type}; the var() in "${value}" has an empty fallback, which leaves it with no value when the variable is unset.`,
|
|
841
|
+
{
|
|
842
|
+
run: [
|
|
843
|
+
`${P} set ${slug} ${name} ${quoteCommandArgument(exampleOf(type, name))}`,
|
|
844
|
+
],
|
|
845
|
+
},
|
|
846
|
+
);
|
|
847
|
+
if (!/\bvar\(/i.test(fallback) && !/^[a-z-]+\(/i.test(fallback))
|
|
848
|
+
checkTypedValue(
|
|
849
|
+
type,
|
|
850
|
+
fallback,
|
|
851
|
+
name,
|
|
852
|
+
TOKENS,
|
|
853
|
+
typeOf,
|
|
854
|
+
slug,
|
|
855
|
+
readers,
|
|
856
|
+
multiplied,
|
|
857
|
+
);
|
|
858
|
+
}
|
|
353
859
|
// A var() naming a design token must name one of the same kind; the
|
|
354
860
|
// app's own variables are its business.
|
|
355
861
|
const ref = /^var\(\s*(--[a-z0-9-]+)\s*(,.*)?\)$/i.exec(value.trim());
|
|
@@ -364,10 +870,65 @@ function checkTypedValue(
|
|
|
364
870
|
],
|
|
365
871
|
},
|
|
366
872
|
);
|
|
873
|
+
// The fallback is what shows when the variable is unset: it must be a
|
|
874
|
+
// value of the token's kind too.
|
|
875
|
+
const fallback = ref[2]?.slice(1).trim();
|
|
876
|
+
if (fallback === '' && type !== 'other')
|
|
877
|
+
refuse(
|
|
878
|
+
E.ERR_THEME_BAD_VALUE,
|
|
879
|
+
`${name} is a ${type}; "${value}" has an empty fallback, which leaves it with no value when the variable is unset.`,
|
|
880
|
+
{
|
|
881
|
+
run: [
|
|
882
|
+
`${P} set ${slug} ${name} ${quoteCommandArgument(exampleOf(type, name))}`,
|
|
883
|
+
],
|
|
884
|
+
},
|
|
885
|
+
);
|
|
886
|
+
if (fallback)
|
|
887
|
+
checkTypedValue(
|
|
888
|
+
type,
|
|
889
|
+
fallback,
|
|
890
|
+
name,
|
|
891
|
+
TOKENS,
|
|
892
|
+
typeOf,
|
|
893
|
+
slug,
|
|
894
|
+
readers,
|
|
895
|
+
multiplied,
|
|
896
|
+
);
|
|
367
897
|
return;
|
|
368
898
|
}
|
|
369
899
|
if (type === 'color' && !isFixedColor(value)) bad('a color', '#1f2937');
|
|
370
|
-
if (type === 'size' && !isSize(value, name))
|
|
900
|
+
if (type === 'size' && !isSize(value, name)) {
|
|
901
|
+
// With nothing to read from the value, the token's own default (a text
|
|
902
|
+
// size stays readable), never one size for every token.
|
|
903
|
+
// Followed through the tokens it reads (var(--font-size-base)).
|
|
904
|
+
let own = String(TOKENS[name] ?? '').trim();
|
|
905
|
+
for (let hop = 0; hop < 4; hop += 1) {
|
|
906
|
+
const ref = /^var\(\s*(--[a-z0-9-]+)\s*\)$/i.exec(own)?.[1];
|
|
907
|
+
if (!ref) break;
|
|
908
|
+
own = String(TOKENS[ref] ?? '').trim();
|
|
909
|
+
}
|
|
910
|
+
const sizeExample =
|
|
911
|
+
own && isSize(own, name) && !/var\(/.test(own) ? own : '6px';
|
|
912
|
+
// The size meant, when one dot or sign is all that is wrong (1.px).
|
|
913
|
+
const meant = value.trim().replace(/(\d)\.(?=[a-z%]|$)/gi, '$1');
|
|
914
|
+
// A bare number is the size in px.
|
|
915
|
+
// A bare number is the size in px; a minus sign is dropped.
|
|
916
|
+
const plain = value.trim().replace(/^-(?=\d|\.\d)/, '');
|
|
917
|
+
const px = /^[+]?\d*\.?\d+(e[+-]?\d+)?$/i.test(plain)
|
|
918
|
+
? `${Number(plain)}px`
|
|
919
|
+
: /^\d*\.?\d+[a-z%]+$/i.test(plain) && plain !== value.trim()
|
|
920
|
+
? plain
|
|
921
|
+
: null;
|
|
922
|
+
const slip = sizeMeant(value, x => isSize(x, name));
|
|
923
|
+
bad(
|
|
924
|
+
'a size',
|
|
925
|
+
meant !== value.trim() && isSize(meant, name)
|
|
926
|
+
? meant
|
|
927
|
+
: px && isSize(px, name)
|
|
928
|
+
? px
|
|
929
|
+
: (slip ?? sizeExample),
|
|
930
|
+
);
|
|
931
|
+
}
|
|
371
932
|
// A percentage reaches every property that reads the token; one that
|
|
372
933
|
// takes none (a border or outline width, an outline offset) drops it.
|
|
373
934
|
if (type === 'size' && /%/.test(value.replace(/var\([^)]*\)/g, ''))) {
|
|
@@ -379,7 +940,38 @@ function checkTypedValue(
|
|
|
379
940
|
);
|
|
380
941
|
}
|
|
381
942
|
if (type === 'number' && !isNumber(value, name))
|
|
382
|
-
bad(
|
|
943
|
+
bad(
|
|
944
|
+
'a number',
|
|
945
|
+
numberMeant(value, name, x => isNumber(x, name)) ??
|
|
946
|
+
(/weight/.test(name) ? '600' : '1.4'),
|
|
947
|
+
);
|
|
948
|
+
// A token Core's stylesheet multiplies by a size inside calc() (the line
|
|
949
|
+
// heights) takes a number with no unit: there a length, a percentage or
|
|
950
|
+
// normal is invalid.
|
|
951
|
+
const coreDefault = String(TOKENS[name] ?? '').trim();
|
|
952
|
+
const v0 = value.trim();
|
|
953
|
+
const read = cssNumber(v0);
|
|
954
|
+
if (
|
|
955
|
+
type === 'number' &&
|
|
956
|
+
multiplied &&
|
|
957
|
+
!/^var\(--[a-z0-9-]+\)$/i.test(v0) &&
|
|
958
|
+
read?.unit !== '' &&
|
|
959
|
+
!(MATH_FUNCTION.test(v0) && matchType('number', v0) != null)
|
|
960
|
+
) {
|
|
961
|
+
const unitless =
|
|
962
|
+
read?.unit === 'em'
|
|
963
|
+
? String(read.value)
|
|
964
|
+
: read?.unit === '%'
|
|
965
|
+
? String(read.value / 100)
|
|
966
|
+
: coreDefault;
|
|
967
|
+
refuse(
|
|
968
|
+
E.ERR_THEME_BAD_VALUE,
|
|
969
|
+
`${name} takes a number with no unit: Core multiplies it by a text size inside calc() (field heights, dialog headers, code blocks), where "${value}" is invalid. ${unitless === coreDefault ? `Core's own value is ${coreDefault}.` : `${unitless} is the same line height as a number.`}`,
|
|
970
|
+
{
|
|
971
|
+
run: [`${P} set ${slug} ${name} ${quoteCommandArgument(unitless)}`],
|
|
972
|
+
},
|
|
973
|
+
);
|
|
974
|
+
}
|
|
383
975
|
if (type === 'time' && !isTime(value)) bad('a time up to 60s', '150ms');
|
|
384
976
|
if (type === 'easing' && !isEasing(value))
|
|
385
977
|
bad('an easing', 'cubic-bezier(0.2, 0, 0, 1)');
|
|
@@ -408,9 +1000,13 @@ function isFixedColor(value) {
|
|
|
408
1000
|
if (/^var\(/.test(v)) return true;
|
|
409
1001
|
if (SYSTEM_COLORS.includes(v)) return true;
|
|
410
1002
|
// Percentages that add up to 0 leave color-mix() no color.
|
|
1003
|
+
// A share is 0% to 100%.
|
|
411
1004
|
const zeroMix = (/** @type {string[]} */ colors) => {
|
|
412
1005
|
const pcts = colors.map(c => mixSide(c).pct);
|
|
413
|
-
return
|
|
1006
|
+
return (
|
|
1007
|
+
pcts.some(p => p != null && (p < 0 || p > 100)) ||
|
|
1008
|
+
(pcts.every(p => p != null) && pcts.every(p => p === 0))
|
|
1009
|
+
);
|
|
414
1010
|
};
|
|
415
1011
|
const inner = (/** @type {string} */ fn) =>
|
|
416
1012
|
splitTop(v.slice(fn.length + 1, -1), ',').map(x => x.trim());
|
|
@@ -517,18 +1113,66 @@ function isShadow(value) {
|
|
|
517
1113
|
|
|
518
1114
|
/**
|
|
519
1115
|
* Refuse a value that would read the token itself, through other tokens.
|
|
1116
|
+
* A token set on a part (`--on`) overrides only inside that part: there
|
|
1117
|
+
* it reads the part's own settings first and the whole theme's after, and
|
|
1118
|
+
* a whole-theme value never reads a part's. So a loop counts only when it
|
|
1119
|
+
* closes within one scope: a part token that reads a whole-theme token
|
|
1120
|
+
* reads the inherited value, which is already resolved.
|
|
520
1121
|
* @param {string} thing
|
|
521
1122
|
* @param {string} value
|
|
522
1123
|
* @param {Record<string, unknown>} TOKENS
|
|
523
1124
|
* @param {import('./_settings.mjs').ThemeData} data
|
|
1125
|
+
* @param {string} [cwd] the project, whose `<slug>.theme.ts` tokens win
|
|
1126
|
+
* over the settings and are read too
|
|
1127
|
+
* @param {{on?: string, state?: string, when?: string}} [scope] where
|
|
1128
|
+
* the token is set; none for the whole theme
|
|
524
1129
|
*/
|
|
525
|
-
function checkCycle(thing, value, TOKENS, data) {
|
|
1130
|
+
function checkCycle(thing, value, TOKENS, data, cwd, scope = {}) {
|
|
1131
|
+
const own = cwd ? ownSourceTokenValues(cwd, data.name) : {};
|
|
1132
|
+
// The base theme's own values come under the settings: a base that maps
|
|
1133
|
+
// one token to another closes a loop too.
|
|
1134
|
+
const baseTokens = cwd ? baseTopTokens(cwd, data.like) : new Map();
|
|
1135
|
+
const on = scope.on ?? null;
|
|
1136
|
+
const settings = [...data.settings].reverse();
|
|
1137
|
+
/**
|
|
1138
|
+
* The token's value set on the part, or undefined: a setting with the
|
|
1139
|
+
* same state and variant wins over the part's plain one.
|
|
1140
|
+
* @param {string} t
|
|
1141
|
+
*/
|
|
1142
|
+
const partValue = t => {
|
|
1143
|
+
if (on == null) return undefined;
|
|
1144
|
+
const onPart = settings.filter(
|
|
1145
|
+
s => s.source === 'token' && s.thing === t && s.on === on,
|
|
1146
|
+
);
|
|
1147
|
+
const same = onPart.find(
|
|
1148
|
+
s =>
|
|
1149
|
+
(s.state ?? null) === (scope.state ?? null) &&
|
|
1150
|
+
(s.when ?? null) === (scope.when ?? null),
|
|
1151
|
+
);
|
|
1152
|
+
const set = (same ?? onPart.find(s => s.state == null && s.when == null))
|
|
1153
|
+
?.value;
|
|
1154
|
+
if (set !== undefined) return set;
|
|
1155
|
+
// Else what the part's plain rule holds as built: the own source's and
|
|
1156
|
+
// the base theme's part rules (neutral's progress-bar reads
|
|
1157
|
+
// --color-border-emphasized).
|
|
1158
|
+
// A state or variant rule of the part inherits its plain rule.
|
|
1159
|
+
if (!cwd) return undefined;
|
|
1160
|
+
const rule = partRule().get(t);
|
|
1161
|
+
return rule ?? undefined;
|
|
1162
|
+
};
|
|
1163
|
+
/** @type {Map<string, string | null> | null} */
|
|
1164
|
+
let rule = null;
|
|
1165
|
+
const partRule = () =>
|
|
1166
|
+
(rule ??= on
|
|
1167
|
+
? partRuleTokens(/** @type {string} */ (cwd), data.name, data.like, on)
|
|
1168
|
+
: new Map());
|
|
526
1169
|
/** @param {string} t */
|
|
527
|
-
const
|
|
1170
|
+
const globalValue = t =>
|
|
528
1171
|
String(
|
|
529
|
-
[
|
|
530
|
-
.
|
|
531
|
-
|
|
1172
|
+
(Object.hasOwn(own, t) ? own[t] : null) ??
|
|
1173
|
+
settings.find(s => s.source === 'token' && s.thing === t && !s.on)
|
|
1174
|
+
?.value ??
|
|
1175
|
+
baseTokens.get(t) ??
|
|
532
1176
|
TOKENS[t] ??
|
|
533
1177
|
'',
|
|
534
1178
|
);
|
|
@@ -537,23 +1181,40 @@ function checkCycle(thing, value, TOKENS, data) {
|
|
|
537
1181
|
/** @type {string[]} */
|
|
538
1182
|
const path = [];
|
|
539
1183
|
const seen = new Set();
|
|
540
|
-
/**
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
1184
|
+
/**
|
|
1185
|
+
* Whether `t`, read in the part (`inPart`) or the whole theme, reaches
|
|
1186
|
+
* the token being set in its own scope.
|
|
1187
|
+
* @param {string} t @param {boolean} inPart @returns {boolean}
|
|
1188
|
+
*/
|
|
1189
|
+
const reaches = (t, inPart) => {
|
|
1190
|
+
// The token being set is reached only in the scope it is set in.
|
|
1191
|
+
if (t === thing && inPart === (on != null)) return true;
|
|
1192
|
+
const part = inPart ? partValue(t) : undefined;
|
|
1193
|
+
const local = part !== undefined;
|
|
1194
|
+
const key = `${local ? 'part' : 'theme'}:${t}`;
|
|
1195
|
+
if (seen.has(key)) return false;
|
|
1196
|
+
seen.add(key);
|
|
1197
|
+
path.push(
|
|
1198
|
+
local
|
|
1199
|
+
? `${t} (on ${on})`
|
|
1200
|
+
: Object.hasOwn(own, t)
|
|
1201
|
+
? `${t} (set in ${data.name}.theme.ts)`
|
|
1202
|
+
: t,
|
|
1203
|
+
);
|
|
1204
|
+
if (
|
|
1205
|
+
refs(local ? String(part) : globalValue(t)).some(r => reaches(r, local))
|
|
1206
|
+
)
|
|
1207
|
+
return true;
|
|
547
1208
|
path.pop();
|
|
548
1209
|
return false;
|
|
549
1210
|
};
|
|
550
|
-
if (refs(value).some(reaches))
|
|
1211
|
+
if (refs(value).some(r => reaches(r, on != null)))
|
|
551
1212
|
refuse(
|
|
552
1213
|
E.ERR_THEME_BAD_VALUE,
|
|
553
1214
|
`${thing} would read itself${path.length ? ` through ${path.join(', ')}` : ''}, so it would have no value at all.`,
|
|
554
1215
|
{
|
|
555
1216
|
run: [
|
|
556
|
-
`${P} set ${data.name} ${thing} <a value that does not read ${thing}
|
|
1217
|
+
`${P} set ${data.name} ${thing} <a value that does not read ${thing}>${on != null ? ` --on ${on}` : ''}`,
|
|
557
1218
|
],
|
|
558
1219
|
},
|
|
559
1220
|
);
|
|
@@ -582,10 +1243,31 @@ function propertyProblem(prop, value) {
|
|
|
582
1243
|
const v = value.trim();
|
|
583
1244
|
const known = propertyGrammar(prop);
|
|
584
1245
|
if (!known || COLOR_PROPS.test(prop)) return null;
|
|
1246
|
+
// Each var() fallback is what shows when its variable is unset: the
|
|
1247
|
+
// value with the fallbacks in place must be one the property takes too.
|
|
1248
|
+
const unset = withFallbacks(v);
|
|
1249
|
+
if (unset != null && matchValue(prop, v) && !matchValue(prop, unset))
|
|
1250
|
+
return {
|
|
1251
|
+
takes: `a value its CSS grammar takes in each var() fallback too (${grammarText(known)})`,
|
|
1252
|
+
near: initialOf(prop),
|
|
1253
|
+
};
|
|
585
1254
|
if (matchValue(prop, v))
|
|
586
1255
|
return belowRange(prop, v)
|
|
587
|
-
? {
|
|
1256
|
+
? {
|
|
1257
|
+
takes: 'a value of 0 or more',
|
|
1258
|
+
near: v.replace(/(^|[\s(,/])-(?=\.?\d)/g, '$1'),
|
|
1259
|
+
}
|
|
588
1260
|
: null;
|
|
1261
|
+
// The same value as it was meant to be typed (a font name quoted, a
|
|
1262
|
+
// trailing `;` gone), when the property takes that.
|
|
1263
|
+
const fixed = valueMeant(prop === 'font-family' ? 'font' : 'other', v).find(
|
|
1264
|
+
c => matchValue(prop, c),
|
|
1265
|
+
);
|
|
1266
|
+
if (fixed)
|
|
1267
|
+
return {
|
|
1268
|
+
takes: `a value its CSS grammar takes (${grammarText(known)})`,
|
|
1269
|
+
near: fixed,
|
|
1270
|
+
};
|
|
589
1271
|
// Only keywords the property takes on their own are offered; the same
|
|
590
1272
|
// word without its dashes or a trailing -ed/-s comes first (no-wrap is
|
|
591
1273
|
// nowrap, underlined is underline), then the nearest in spelling.
|
|
@@ -602,57 +1284,90 @@ function propertyProblem(prop, value) {
|
|
|
602
1284
|
// the property's initial value, or an example of its kind; each checked.
|
|
603
1285
|
const example = exampleOf(groupOfPropType(prop), prop);
|
|
604
1286
|
const initial = initialOf(prop);
|
|
1287
|
+
// A bare number on a length property means px (`padding 4` → `4px`, each
|
|
1288
|
+
// number in a list too), when the property takes that.
|
|
1289
|
+
// Each bare number token gets px (a mixed list `4 8px`, `8 / 4`, an
|
|
1290
|
+
// exponent `1e1`), a unit split off by a space (`4 px`) joins its number.
|
|
1291
|
+
const joined = v.replace(/(\d)\s+(px|rem|em|%)\b/gi, '$1$2');
|
|
1292
|
+
const withPx = /\d/.test(joined)
|
|
1293
|
+
? joined.replace(
|
|
1294
|
+
/(^|[\s/(,])([+-]?(?:\d+\.?\d*|\.\d+)(?:e[+-]?\d+)?)(?=$|[\s/),])/gi,
|
|
1295
|
+
(_, pre, num) =>
|
|
1296
|
+
Number(num) === 0 ? `${pre}${num}` : `${pre}${num}px`,
|
|
1297
|
+
)
|
|
1298
|
+
: null;
|
|
1299
|
+
// A suggestion is offered only when the property takes it, in range too.
|
|
1300
|
+
const takes = (/** @type {string | null} */ x) =>
|
|
1301
|
+
x != null && matchValue(prop, x) != null && !belowRange(prop, x);
|
|
605
1302
|
return {
|
|
606
1303
|
takes: `a value its CSS grammar takes (${grammarText(known)})`,
|
|
607
1304
|
near:
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
(
|
|
1305
|
+
(takes(withPx) ? withPx : null) ??
|
|
1306
|
+
// Below the property's range (a negative width): the same, 0 or more.
|
|
1307
|
+
(withPx && takes(withPx.replace(/(^|[\s/(,])-(?=\.?\d)/g, '$1'))
|
|
1308
|
+
? withPx.replace(/(^|[\s/(,])-(?=\.?\d)/g, '$1')
|
|
1309
|
+
: null) ??
|
|
1310
|
+
(takes(joined) && joined !== v ? joined : null) ??
|
|
1311
|
+
// Several values on a property that takes one (padding-top with four):
|
|
1312
|
+
// the first of them, when it is one the property takes.
|
|
1313
|
+
(() => {
|
|
1314
|
+
const first = splitTop((withPx ?? joined).trim(), ' ')[0];
|
|
1315
|
+
return first && first !== v && takes(first) ? first : null;
|
|
1316
|
+
})() ??
|
|
1317
|
+
(near && takes(near) ? near : null) ??
|
|
1318
|
+
// A value with numbers never falls back to the property's reset
|
|
1319
|
+
// (padding 0 would remove what was asked for).
|
|
1320
|
+
(/\d/.test(v)
|
|
1321
|
+
? null
|
|
1322
|
+
: ((takes(initial) ? initial : null) ??
|
|
1323
|
+
(takes(example) ? example : null))),
|
|
611
1324
|
};
|
|
612
1325
|
}
|
|
613
1326
|
|
|
614
1327
|
/** Words people use for a dial's values, by dial. */
|
|
615
|
-
const DIAL_WORDS =
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
}
|
|
1328
|
+
const DIAL_WORDS = wordTable(
|
|
1329
|
+
/** @type {Record<string, Record<string, string>>} */ ({
|
|
1330
|
+
surfaces: {
|
|
1331
|
+
elevated: 'raised',
|
|
1332
|
+
elevation: 'raised',
|
|
1333
|
+
shadow: 'raised',
|
|
1334
|
+
shadows: 'raised',
|
|
1335
|
+
lifted: 'raised',
|
|
1336
|
+
frosted: 'glass',
|
|
1337
|
+
blur: 'glass',
|
|
1338
|
+
blurred: 'glass',
|
|
1339
|
+
translucent: 'glass',
|
|
1340
|
+
bordered: 'outlined',
|
|
1341
|
+
border: 'outlined',
|
|
1342
|
+
borders: 'outlined',
|
|
1343
|
+
lined: 'outlined',
|
|
1344
|
+
outline: 'outlined',
|
|
1345
|
+
plain: 'flat',
|
|
1346
|
+
minimal: 'flat',
|
|
1347
|
+
},
|
|
1348
|
+
buttons: {
|
|
1349
|
+
uppercase: 'caps',
|
|
1350
|
+
upper: 'caps',
|
|
1351
|
+
allcaps: 'caps',
|
|
1352
|
+
capitals: 'caps',
|
|
1353
|
+
tint: 'soft',
|
|
1354
|
+
tinted: 'soft',
|
|
1355
|
+
subtle: 'soft',
|
|
1356
|
+
outlined: 'outline',
|
|
1357
|
+
bordered: 'outline',
|
|
1358
|
+
ghost: 'outline',
|
|
1359
|
+
filled: 'solid',
|
|
1360
|
+
fill: 'solid',
|
|
1361
|
+
},
|
|
1362
|
+
mode: {
|
|
1363
|
+
auto: 'both',
|
|
1364
|
+
system: 'both',
|
|
1365
|
+
default: 'both',
|
|
1366
|
+
light: 'light-only',
|
|
1367
|
+
dark: 'dark-only',
|
|
1368
|
+
},
|
|
1369
|
+
}),
|
|
1370
|
+
);
|
|
656
1371
|
|
|
657
1372
|
/**
|
|
658
1373
|
/**
|
|
@@ -693,6 +1408,111 @@ export function checkSetting(
|
|
|
693
1408
|
value,
|
|
694
1409
|
flags,
|
|
695
1410
|
unset = false,
|
|
1411
|
+
) {
|
|
1412
|
+
// Any other dial's value pasted with its property name (`font-size:
|
|
1413
|
+
// 18px;` for text-size, `color: #e11d48` for accent) is the value: one
|
|
1414
|
+
// path for every dial (the font dial reads its own below).
|
|
1415
|
+
if (!unset && typeof value === 'string' && DIALS[thing] && thing !== 'font') {
|
|
1416
|
+
const d = pastedDeclaration(value);
|
|
1417
|
+
if (d?.value) value = d.value;
|
|
1418
|
+
}
|
|
1419
|
+
// The font dial's value as pasted from a stylesheet (`Georgia, serif;`,
|
|
1420
|
+
// a comment, `!important`): a refusal suggests the font it names, when
|
|
1421
|
+
// set takes that.
|
|
1422
|
+
// A font pasted with its property name is the family list it names.
|
|
1423
|
+
if (
|
|
1424
|
+
!unset &&
|
|
1425
|
+
typeof value === 'string' &&
|
|
1426
|
+
(thing === 'font' ||
|
|
1427
|
+
(thing.startsWith('--') && ctx.C.typeOf(thing) === 'font'))
|
|
1428
|
+
) {
|
|
1429
|
+
const pasted = pastedFont(value);
|
|
1430
|
+
if (pasted === '')
|
|
1431
|
+
refuse(
|
|
1432
|
+
E.ERR_MISSING_ARGUMENT,
|
|
1433
|
+
`"${value.trim()}" names a font property but gives no font. Give the family after the colon, or alone.`,
|
|
1434
|
+
{
|
|
1435
|
+
run: [
|
|
1436
|
+
`${P} set ${slug} ${thing} ${quoteCommandArgument(thing === 'font' ? 'inter' : 'Inter, sans-serif')}${flags.on ? ` --on ${quoteCommandArgument(flags.on)}` : ''}`,
|
|
1437
|
+
],
|
|
1438
|
+
},
|
|
1439
|
+
);
|
|
1440
|
+
if (pasted) value = pasted;
|
|
1441
|
+
}
|
|
1442
|
+
if (thing === 'font' && !unset && typeof value === 'string') {
|
|
1443
|
+
const meant = typedPlain(value)
|
|
1444
|
+
.replace(/;+\s*$/, '')
|
|
1445
|
+
.replace(/\s*!\s*important\s*$/i, '')
|
|
1446
|
+
.replace(/;+\s*$/, '')
|
|
1447
|
+
.replace(/\s+/g, ' ')
|
|
1448
|
+
.trim();
|
|
1449
|
+
if (meant && meant !== value.trim())
|
|
1450
|
+
try {
|
|
1451
|
+
return checkSettingAsTyped(ctx, slug, data, thing, value, flags);
|
|
1452
|
+
} catch (e) {
|
|
1453
|
+
try {
|
|
1454
|
+
checkSettingAsTyped(ctx, slug, data, thing, meant, flags);
|
|
1455
|
+
} catch {
|
|
1456
|
+
throw e;
|
|
1457
|
+
}
|
|
1458
|
+
if (e instanceof AstryxError)
|
|
1459
|
+
e.suggestions = [
|
|
1460
|
+
{
|
|
1461
|
+
name: `${P} set ${slug} font ${quoteCommandArgument(meant)}${flags.on ? ` --on ${quoteCommandArgument(flags.on)}` : ''}`,
|
|
1462
|
+
reason: 'do this instead',
|
|
1463
|
+
},
|
|
1464
|
+
];
|
|
1465
|
+
throw e;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
const s = checkSettingAsTyped(ctx, slug, data, thing, value, flags, unset);
|
|
1469
|
+
// A var() with a fallback always gives a value; a name one slip from a
|
|
1470
|
+
// design token is noted as a likely typo, and the setting stands.
|
|
1471
|
+
if (!unset && typeof value === 'string' && !s.note) {
|
|
1472
|
+
const typo = fallbackVarTypo(value, ctx.project.core.tokenDefaults ?? {});
|
|
1473
|
+
if (typo) s.note = typo;
|
|
1474
|
+
}
|
|
1475
|
+
return s;
|
|
1476
|
+
}
|
|
1477
|
+
|
|
1478
|
+
/**
|
|
1479
|
+
* A note for a var() with a fallback whose name is a letter or two off a
|
|
1480
|
+
* design token (`var(--spacing-xl, 2rem)` when the token is `--spacing-x1`):
|
|
1481
|
+
* the fallback shows until it is fixed. Null when there is none.
|
|
1482
|
+
* @param {string} v
|
|
1483
|
+
* @param {Record<string, unknown>} TOKENS
|
|
1484
|
+
*/
|
|
1485
|
+
function fallbackVarTypo(v, TOKENS) {
|
|
1486
|
+
for (const [, name] of v.matchAll(/var\(\s*(--[a-z0-9-]+)\s*,/gi)) {
|
|
1487
|
+
if (name in TOKENS || name.startsWith('--_')) continue;
|
|
1488
|
+
const near = nearest(name, Object.keys(TOKENS)).filter(
|
|
1489
|
+
n => levenshteinDistance(n, name) <= 2,
|
|
1490
|
+
);
|
|
1491
|
+
if (near.length)
|
|
1492
|
+
return `${name} is not a design token, so its fallback always shows; did you mean ${near[0]}?`;
|
|
1493
|
+
}
|
|
1494
|
+
return null;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
/**
|
|
1498
|
+
* checkSetting for the value as typed.
|
|
1499
|
+
* @param {import('./_settings.mjs').Ctx} ctx
|
|
1500
|
+
* @param {string} slug
|
|
1501
|
+
* @param {import('./_settings.mjs').ThemeData} data
|
|
1502
|
+
* @param {string} thing
|
|
1503
|
+
* @param {string | undefined} value
|
|
1504
|
+
* @param {SetFlags} flags
|
|
1505
|
+
* @param {boolean} [unset]
|
|
1506
|
+
* @returns {import('./_settings.mjs').Setting}
|
|
1507
|
+
*/
|
|
1508
|
+
function checkSettingAsTyped(
|
|
1509
|
+
ctx,
|
|
1510
|
+
slug,
|
|
1511
|
+
data,
|
|
1512
|
+
thing,
|
|
1513
|
+
value,
|
|
1514
|
+
flags,
|
|
1515
|
+
unset = false,
|
|
696
1516
|
) {
|
|
697
1517
|
const s = checkOne(ctx, slug, data, thing, value, flags, unset);
|
|
698
1518
|
const why = unset ? null : valueProblem(s);
|
|
@@ -757,42 +1577,25 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
757
1577
|
run: [`${P} ${unset ? 'undo' : 'set'} --help`],
|
|
758
1578
|
});
|
|
759
1579
|
// A value pasted over several lines is one value.
|
|
760
|
-
const v = (value ?? '')
|
|
761
|
-
.replace(/\s*[\r\n]+\s*/g, ' ')
|
|
762
|
-
.replace(/\t+/g, ' ')
|
|
763
|
-
.trim();
|
|
1580
|
+
const v = normalValue(thing, value ?? '');
|
|
764
1581
|
// A var() that misspells a design token would read nothing.
|
|
765
|
-
if (!unset)
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
},
|
|
783
|
-
);
|
|
784
|
-
if (near.length)
|
|
785
|
-
refuse(
|
|
786
|
-
E.ERR_THEME_UNKNOWN_TOKEN,
|
|
787
|
-
`${name} is not a design token, so var(${name}) would read nothing.`,
|
|
788
|
-
{
|
|
789
|
-
didYouMean: near,
|
|
790
|
-
run: [
|
|
791
|
-
`${HOW} --list tokens ${near[0].replace(/^--/, '').split('-')[0]}`,
|
|
792
|
-
],
|
|
793
|
-
},
|
|
794
|
-
);
|
|
795
|
-
}
|
|
1582
|
+
if (!unset) {
|
|
1583
|
+
const bad = unknownVarRef(v, TOKENS, thing);
|
|
1584
|
+
if (bad)
|
|
1585
|
+
refuse(E.ERR_THEME_UNKNOWN_TOKEN, bad.message, {
|
|
1586
|
+
didYouMean: bad.near,
|
|
1587
|
+
run: [
|
|
1588
|
+
...(bad.fixed
|
|
1589
|
+
? [
|
|
1590
|
+
`${P} set ${slug} ${thing} ${quoteCommandArgument(bad.fixed)}${/** @type {const} */ (['on', 'state', 'when', 'mode']).map(k => (flags[k] ? ` --${k} ${quoteCommandArgument(String(flags[k]))}` : '')).join('')}`,
|
|
1591
|
+
]
|
|
1592
|
+
: []),
|
|
1593
|
+
bad.close
|
|
1594
|
+
? `${HOW} --list tokens ${bad.near[0].replace(/^--/, '').split('-')[0]}`
|
|
1595
|
+
: `${HOW} --list tokens`,
|
|
1596
|
+
],
|
|
1597
|
+
});
|
|
1598
|
+
}
|
|
796
1599
|
// A placeholder copied from a suggestion is not a value.
|
|
797
1600
|
if (!unset && /<[a-z][\w -]*>/i.test(v))
|
|
798
1601
|
refuse(
|
|
@@ -814,7 +1617,15 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
814
1617
|
});
|
|
815
1618
|
if (flags.on) s.on = flags.on.trim();
|
|
816
1619
|
if (flags.when) s.when = flags.when.trim();
|
|
817
|
-
|
|
1620
|
+
// A state in any case or spelling of its words (focusVisible,
|
|
1621
|
+
// focus_visible, 'focus visible') is the state, written as CSS writes it.
|
|
1622
|
+
if (flags.state)
|
|
1623
|
+
s.state = flags.state
|
|
1624
|
+
.trim()
|
|
1625
|
+
.replace(/^:+/, '')
|
|
1626
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
1627
|
+
.replace(/[\s_]+/g, '-')
|
|
1628
|
+
.toLowerCase();
|
|
818
1629
|
if (flags.only) s.only = true;
|
|
819
1630
|
if (flags.mode) {
|
|
820
1631
|
const mode = flags.mode.toLowerCase();
|
|
@@ -879,17 +1690,39 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
879
1690
|
if (s.on && !isRole && !FAMILIES[s.on]) {
|
|
880
1691
|
const t = TARGET.get(s.on);
|
|
881
1692
|
if (!t) {
|
|
882
|
-
// Another case of a part or group
|
|
883
|
-
|
|
884
|
-
|
|
1693
|
+
// Another case of a part or group, its class name copied from the
|
|
1694
|
+
// page (`.astryx-card`), or its component (`<Card>`, `Card`): that
|
|
1695
|
+
// part, as a working command with every flag kept.
|
|
1696
|
+
const lower = s.on
|
|
1697
|
+
.trim()
|
|
1698
|
+
.replace(/^<\/?\s*|\s*\/?>$/g, '')
|
|
1699
|
+
.replace(/^\./, '')
|
|
1700
|
+
.replace(/^astryx-/, '')
|
|
1701
|
+
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
|
|
1702
|
+
.toLowerCase();
|
|
1703
|
+
// A plain word how reads as a part or group (checkboxes: checks).
|
|
1704
|
+
const byWord = partForWord(lower);
|
|
1705
|
+
// A plain plural of a part (cards, dialogs, tabs) is that part.
|
|
1706
|
+
const singular = [
|
|
1707
|
+
lower.replace(/ies$/, 'y'),
|
|
1708
|
+
lower.replace(/(ch|sh|x|s)es$/, '$1'),
|
|
1709
|
+
lower.replace(/s$/, ''),
|
|
1710
|
+
].find(x => x !== lower && (TARGET.has(x) || FAMILIES[x]));
|
|
1711
|
+
const named =
|
|
1712
|
+
TARGET.has(lower) || FAMILIES[lower]
|
|
1713
|
+
? lower
|
|
1714
|
+
: singular
|
|
1715
|
+
? singular
|
|
1716
|
+
: byWord && (TARGET.has(byWord) || FAMILIES[byWord])
|
|
1717
|
+
? byWord
|
|
1718
|
+
: null;
|
|
1719
|
+
if (named)
|
|
885
1720
|
refuse(
|
|
886
1721
|
E.ERR_THEME_UNKNOWN_PART,
|
|
887
|
-
`"${s.on}" is not a part
|
|
1722
|
+
`"${s.on}" is not a part name; the part is ${named}.`,
|
|
888
1723
|
{
|
|
889
|
-
didYouMean: [
|
|
890
|
-
run: [
|
|
891
|
-
`${P} ${unset ? 'undo' : 'set'} ${slug} ${thing}${v ? ` ${quoteCommandArgument(v)}` : ''} --on ${lower}${s.when ? ` --when ${s.when}` : ''}${s.state ? ` --state ${s.state}` : ''}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
892
|
-
],
|
|
1724
|
+
didYouMean: [named],
|
|
1725
|
+
run: [setLine(slug, thing, v, {...s, on: named}, unset)],
|
|
893
1726
|
},
|
|
894
1727
|
);
|
|
895
1728
|
refuse(
|
|
@@ -902,7 +1735,7 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
902
1735
|
...Object.keys(FAMILIES),
|
|
903
1736
|
]),
|
|
904
1737
|
run: [
|
|
905
|
-
`${HOW} --list parts ${
|
|
1738
|
+
`${HOW} --list parts ${quoteCommandArgument(lower.replace(/s$/, '').split('-')[0] || 'button')}`,
|
|
906
1739
|
`${HOW} ${quoteCommandArgument(s.on)}`,
|
|
907
1740
|
],
|
|
908
1741
|
},
|
|
@@ -931,20 +1764,37 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
931
1764
|
);
|
|
932
1765
|
}
|
|
933
1766
|
const states = [...t.states, ...COMMON_STATES];
|
|
1767
|
+
// Other words for a state, mapped to the one the part has; with none
|
|
1768
|
+
// near, the list of its states (never a guess such as hover).
|
|
1769
|
+
const SYNONYMS = wordTable(
|
|
1770
|
+
/** @type {Record<string, string[]>} */ ({
|
|
1771
|
+
pressed: ['active'],
|
|
1772
|
+
press: ['active'],
|
|
1773
|
+
down: ['active'],
|
|
1774
|
+
clicked: ['active'],
|
|
1775
|
+
hovered: ['hover'],
|
|
1776
|
+
focused: ['focus-visible', 'focus-within', 'focus'],
|
|
1777
|
+
checked: ['checked', 'selected'],
|
|
1778
|
+
selected: ['selected', 'checked'],
|
|
1779
|
+
open: ['open', 'expanded'],
|
|
1780
|
+
expanded: ['expanded', 'open'],
|
|
1781
|
+
inactive: ['disabled'],
|
|
1782
|
+
}),
|
|
1783
|
+
);
|
|
1784
|
+
const meant =
|
|
1785
|
+
(SYNONYMS[String(s.state ?? '').toLowerCase()] ?? []).find(x =>
|
|
1786
|
+
states.includes(x),
|
|
1787
|
+
) ?? nearest(String(s.state ?? ''), states)[0];
|
|
934
1788
|
if (s.state && !states.includes(s.state))
|
|
935
1789
|
refuse(
|
|
936
1790
|
E.ERR_THEME_UNKNOWN_STATE,
|
|
937
1791
|
`"${s.state}" is not a state of ${s.on}. States: ${states.join(', ')}.`,
|
|
938
1792
|
{
|
|
939
|
-
didYouMean:
|
|
1793
|
+
...(meant ? {didYouMean: [meant]} : {}),
|
|
940
1794
|
run: [
|
|
941
|
-
|
|
942
|
-
slug,
|
|
943
|
-
|
|
944
|
-
v,
|
|
945
|
-
{...s, state: nearest(s.state, states)[0] ?? 'hover'},
|
|
946
|
-
unset,
|
|
947
|
-
),
|
|
1795
|
+
meant
|
|
1796
|
+
? setLine(slug, thing, v, {...s, state: meant}, unset)
|
|
1797
|
+
: `${HOW} ${quoteCommandArgument(s.on)}`,
|
|
948
1798
|
],
|
|
949
1799
|
},
|
|
950
1800
|
);
|
|
@@ -990,16 +1840,10 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
990
1840
|
// The part's component, or the one named like the part (tab is Tab,
|
|
991
1841
|
// under TabList), takes a disabled prop.
|
|
992
1842
|
const disables = (/** @type {string} */ t) => {
|
|
993
|
-
const comps = /** @type {Record<string, any>} */ (
|
|
994
|
-
ctx.project.components ?? {}
|
|
995
|
-
);
|
|
996
|
-
const pascal = t.replace(/(^|-)([a-z0-9])/g, (_, __, c) =>
|
|
997
|
-
c.toUpperCase(),
|
|
998
|
-
);
|
|
999
1843
|
// Or its built module sets aria-disabled (a tab takes it through its
|
|
1000
1844
|
// other props): what Core renders, read from Core itself.
|
|
1001
|
-
const marks = (/** @type {string} */ n) => {
|
|
1002
|
-
const at =
|
|
1845
|
+
const marks = (/** @type {string} */ n, /** @type {any} */ doc) => {
|
|
1846
|
+
const at = doc?.importPath?.split('/').pop();
|
|
1003
1847
|
const file =
|
|
1004
1848
|
at && ctx.project.coreDir
|
|
1005
1849
|
? path.join(ctx.project.coreDir, 'dist', at, `${n}.js`)
|
|
@@ -1012,13 +1856,25 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1012
1856
|
return false;
|
|
1013
1857
|
}
|
|
1014
1858
|
};
|
|
1015
|
-
return
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
(
|
|
1019
|
-
marks(n)),
|
|
1859
|
+
return partComponentDocs(ctx, t).some(
|
|
1860
|
+
({name, doc}) =>
|
|
1861
|
+
Object.keys(doc.props ?? {}).some(p => /disabled/i.test(p)) ||
|
|
1862
|
+
marks(name, doc),
|
|
1020
1863
|
);
|
|
1021
1864
|
};
|
|
1865
|
+
// A part holds content that can take focus, so focus-within applies
|
|
1866
|
+
// there: its component takes children or elements in a prop (a dialog,
|
|
1867
|
+
// a section, a chat composer's input).
|
|
1868
|
+
const holdsContent = (/** @type {string} */ t) =>
|
|
1869
|
+
partComponentDocs(ctx, t).some(({doc}) =>
|
|
1870
|
+
Object.entries(doc.props ?? {}).some(
|
|
1871
|
+
([k, v]) =>
|
|
1872
|
+
k === 'children' ||
|
|
1873
|
+
/\bReact(Node|Element)\b/.test(
|
|
1874
|
+
String(/** @type {any} */ (v)?.type ?? ''),
|
|
1875
|
+
),
|
|
1876
|
+
),
|
|
1877
|
+
);
|
|
1022
1878
|
// A part Core never styles in focus is not what takes focus (the
|
|
1023
1879
|
// focusable element is inside it, or beside it).
|
|
1024
1880
|
const focusable = (/** @type {string} */ t) =>
|
|
@@ -1026,7 +1882,8 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1026
1882
|
const dead = parts.filter(
|
|
1027
1883
|
t =>
|
|
1028
1884
|
!states(t).includes(/** @type {string} */ (s.state)) &&
|
|
1029
|
-
(Object.keys(own(t)).length === 0
|
|
1885
|
+
((Object.keys(own(t)).length === 0 &&
|
|
1886
|
+
!(s.state === 'focus-within' && holdsContent(t))) ||
|
|
1030
1887
|
((s.state === 'focus' || s.state === 'focus-visible') &&
|
|
1031
1888
|
!focusable(t)) ||
|
|
1032
1889
|
(s.state === 'disabled' &&
|
|
@@ -1038,7 +1895,8 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1038
1895
|
// focusable is inside it (a switch's input sits beside its track).
|
|
1039
1896
|
(s.state === 'focus-within' &&
|
|
1040
1897
|
!keys(t, 'focus-within') &&
|
|
1041
|
-
!focusable(t)
|
|
1898
|
+
!focusable(t) &&
|
|
1899
|
+
!holdsContent(t))),
|
|
1042
1900
|
);
|
|
1043
1901
|
if (parts.length && dead.length === parts.length) {
|
|
1044
1902
|
const bare = Object.keys(own(dead[0])).length === 0;
|
|
@@ -1069,6 +1927,43 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1069
1927
|
);
|
|
1070
1928
|
}
|
|
1071
1929
|
}
|
|
1930
|
+
// Core expands a part's padding into its container tokens from a plain
|
|
1931
|
+
// padding only, so in a state that padding never reaches what sits edge
|
|
1932
|
+
// to edge inside it: refused, judged on the part named (a group member
|
|
1933
|
+
// spreads; when only another member is the reason, --only keeps it to
|
|
1934
|
+
// the part named).
|
|
1935
|
+
if (!unset && s.on && s.state && ctx.derived) {
|
|
1936
|
+
// A padding property goes through the padding model, which writes a
|
|
1937
|
+
// state's own side variables; the spacing dial does not.
|
|
1938
|
+
const prop = thing === 'spacing' ? 'padding' : null;
|
|
1939
|
+
const expands = (/** @type {string} */ t) =>
|
|
1940
|
+
(ctx.derived?.[t] ?? []).some(
|
|
1941
|
+
d => d.property === prop && d.expand === 'container',
|
|
1942
|
+
);
|
|
1943
|
+
const named = ctx.project.target.get(s.on) ? s.on : null;
|
|
1944
|
+
const hit = prop
|
|
1945
|
+
? named && expands(named)
|
|
1946
|
+
? named
|
|
1947
|
+
: partsOf(ctx, s).find(expands)
|
|
1948
|
+
: undefined;
|
|
1949
|
+
if (hit)
|
|
1950
|
+
refuse(
|
|
1951
|
+
E.ERR_THEME_NO_EFFECT,
|
|
1952
|
+
`Core sets ${hit}'s inner padding (what sits edge to edge inside it) from a plain padding setting only, so with --state ${s.state} it would stay behind.`,
|
|
1953
|
+
{
|
|
1954
|
+
// The value as set takes it: a bare number in px.
|
|
1955
|
+
run: (() => {
|
|
1956
|
+
const fixed =
|
|
1957
|
+
thing === 'spacing' ? v : (propertyProblem(thing, v)?.near ?? v);
|
|
1958
|
+
return [
|
|
1959
|
+
named && !expands(named) && !s.only
|
|
1960
|
+
? setLine(slug, thing, fixed, {...s, only: true})
|
|
1961
|
+
: setLine(slug, thing, fixed, {...s, state: undefined}),
|
|
1962
|
+
];
|
|
1963
|
+
})(),
|
|
1964
|
+
},
|
|
1965
|
+
);
|
|
1966
|
+
}
|
|
1072
1967
|
// On a group, a variant or state must belong to at least one member.
|
|
1073
1968
|
if (s.on && FAMILIES[s.on] && (s.when || s.state)) {
|
|
1074
1969
|
const members =
|
|
@@ -1100,6 +1995,23 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1100
1995
|
{run: [`${HOW} ${s.on}`]},
|
|
1101
1996
|
);
|
|
1102
1997
|
}
|
|
1998
|
+
// A comparative is a step from the value now (never a rung picked by
|
|
1999
|
+
// spelling, which can go the other way).
|
|
2000
|
+
if (!unset && (thing === 'corners' || thing === 'spacing')) {
|
|
2001
|
+
const w = v.toLowerCase();
|
|
2002
|
+
const MORE =
|
|
2003
|
+
thing === 'corners'
|
|
2004
|
+
? /^(rounder|softer|bigger|larger|curvier|more rounded)$/
|
|
2005
|
+
: /^(roomier|looser|bigger|larger|wider|airier|more spacious)$/;
|
|
2006
|
+
const LESS =
|
|
2007
|
+
thing === 'corners'
|
|
2008
|
+
? /^(sharper|squarer|smaller|tighter|harder|less rounded)$/
|
|
2009
|
+
: /^(tighter|denser|smaller|narrower|more compact|closer)$/;
|
|
2010
|
+
if (MORE.test(w))
|
|
2011
|
+
return checkOne(ctx, slug, data, thing, 'more', flags, unset);
|
|
2012
|
+
if (LESS.test(w))
|
|
2013
|
+
return checkOne(ctx, slug, data, thing, 'less', flags, unset);
|
|
2014
|
+
}
|
|
1103
2015
|
const step = v === 'more' || v === 'less';
|
|
1104
2016
|
// more/less step a dial with a scale; on a dial of named looks, they name
|
|
1105
2017
|
// nothing, so the answer is what the dial takes, not one of its values.
|
|
@@ -1151,23 +2063,43 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1151
2063
|
`${thing} takes ${DIALS[thing].values}, not "${v}".`,
|
|
1152
2064
|
{
|
|
1153
2065
|
didYouMean: near,
|
|
1154
|
-
run:
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
2066
|
+
run:
|
|
2067
|
+
// `type` is the scale's shape; a text-size word or a length
|
|
2068
|
+
// is the body size: text-size, never the corners.
|
|
2069
|
+
thing === 'type' &&
|
|
2070
|
+
(['small', 'medium', 'large', 'larger'].includes(lower) ||
|
|
2071
|
+
(/[a-z%]$/.test(lower) && textSizePx(lower) != null))
|
|
1160
2072
|
? [
|
|
1161
|
-
|
|
1162
|
-
slug,
|
|
1163
|
-
'corners',
|
|
1164
|
-
CORNERS.find(
|
|
1165
|
-
([n]) => lower.replace(/s$/, '') === n,
|
|
1166
|
-
)?.[0] ?? lower,
|
|
1167
|
-
thing === 'buttons' ? {on: 'button'} : {},
|
|
1168
|
-
),
|
|
2073
|
+
`${P} set ${slug} text-size ${['small', 'medium', 'large', 'larger'].includes(lower) ? lower : textSizePx(lower)}`,
|
|
1169
2074
|
]
|
|
1170
|
-
: [
|
|
2075
|
+
: near[0]
|
|
2076
|
+
? [`${P} set ${slug} ${thing} ${near[0]}`]
|
|
2077
|
+
: // A type-scale ratio (1.25) is the nearest type preset.
|
|
2078
|
+
thing === 'type' && ratioPreset(lower)
|
|
2079
|
+
? [`${P} set ${slug} type ${ratioPreset(lower)}`]
|
|
2080
|
+
: // A corner word or a size with a unit is the corners dial:
|
|
2081
|
+
// on the buttons for `buttons`, for the whole theme
|
|
2082
|
+
// otherwise. A bare number is no size: the dial's own
|
|
2083
|
+
// words.
|
|
2084
|
+
CORNERS.some(([n]) => lower.replace(/s$/, '') === n) ||
|
|
2085
|
+
(/[a-z%]$/.test(lower) && lengthPx(lower) != null)
|
|
2086
|
+
? [
|
|
2087
|
+
setLine(
|
|
2088
|
+
slug,
|
|
2089
|
+
'corners',
|
|
2090
|
+
CORNERS.find(
|
|
2091
|
+
([n]) => lower.replace(/s$/, '') === n,
|
|
2092
|
+
)?.[0] ?? lower,
|
|
2093
|
+
thing === 'buttons' ? {on: 'button'} : {},
|
|
2094
|
+
),
|
|
2095
|
+
]
|
|
2096
|
+
: [
|
|
2097
|
+
...(/^[+-]?\d*\.?\d+$/.test(lower) ? ok : [])
|
|
2098
|
+
.filter(w => /^[a-z-]+$/.test(w))
|
|
2099
|
+
.slice(0, 4)
|
|
2100
|
+
.map(w => `${P} set ${slug} ${thing} ${w}`),
|
|
2101
|
+
`${HOW} ${thing}`,
|
|
2102
|
+
],
|
|
1171
2103
|
},
|
|
1172
2104
|
);
|
|
1173
2105
|
}
|
|
@@ -1179,9 +2111,24 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1179
2111
|
E.ERR_THEME_BAD_VALUE,
|
|
1180
2112
|
'headings applies to every heading; drop --on. For one heading level use font-weight or letter-spacing --on heading --when level:<1-6>.',
|
|
1181
2113
|
{
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
2114
|
+
// The dial without --on, then the asked weight or tracking on
|
|
2115
|
+
// one level; a word with no meaning here gets neither.
|
|
2116
|
+
run: (() => {
|
|
2117
|
+
const w = String(v ?? '')
|
|
2118
|
+
.trim()
|
|
2119
|
+
.toLowerCase();
|
|
2120
|
+
if (HEADING_WEIGHTS[w] !== undefined)
|
|
2121
|
+
return [
|
|
2122
|
+
`${P} set ${slug} headings ${w}`,
|
|
2123
|
+
`${P} set ${slug} font-weight ${HEADING_WEIGHTS[w]} --on heading --when level:1`,
|
|
2124
|
+
];
|
|
2125
|
+
if (HEADING_TRACKING[w] !== undefined)
|
|
2126
|
+
return [
|
|
2127
|
+
`${P} set ${slug} headings ${w}`,
|
|
2128
|
+
`${P} set ${slug} letter-spacing ${quoteCommandArgument(HEADING_TRACKING[w])} --on heading --when level:1`,
|
|
2129
|
+
];
|
|
2130
|
+
return [`${HOW} headings`];
|
|
2131
|
+
})(),
|
|
1185
2132
|
},
|
|
1186
2133
|
);
|
|
1187
2134
|
if (s.mode)
|
|
@@ -1197,10 +2144,14 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1197
2144
|
if (unset) {
|
|
1198
2145
|
// With no word, the one headings setting the theme has.
|
|
1199
2146
|
const held = data.settings.filter(x => x.thing === 'headings');
|
|
2147
|
+
// The half word in any letters: `Tracking`, `TIGHT`.
|
|
2148
|
+
const w = String(v ?? '')
|
|
2149
|
+
.trim()
|
|
2150
|
+
.toLowerCase();
|
|
1200
2151
|
s.axis =
|
|
1201
|
-
HEADING_TRACKING[
|
|
2152
|
+
HEADING_TRACKING[w] !== undefined || w === 'tracking'
|
|
1202
2153
|
? 'tracking'
|
|
1203
|
-
: HEADING_WEIGHTS[
|
|
2154
|
+
: HEADING_WEIGHTS[w] !== undefined || w === 'weight'
|
|
1204
2155
|
? 'weight'
|
|
1205
2156
|
: held.length === 1
|
|
1206
2157
|
? (held[0].axis ?? 'weight')
|
|
@@ -1209,28 +2160,36 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1209
2160
|
}
|
|
1210
2161
|
if (HEADING_WEIGHTS[v] !== undefined) s.axis = 'weight';
|
|
1211
2162
|
else if (HEADING_TRACKING[v] !== undefined) s.axis = 'tracking';
|
|
1212
|
-
else
|
|
2163
|
+
else {
|
|
2164
|
+
// The word meant: in other letters (BOLD), or the rung nearest a
|
|
2165
|
+
// weight number (800 is heavy); never a rung the other way.
|
|
2166
|
+
const lower = v.trim().toLowerCase();
|
|
2167
|
+
const words = [
|
|
2168
|
+
...Object.keys(HEADING_WEIGHTS),
|
|
2169
|
+
...Object.keys(HEADING_TRACKING),
|
|
2170
|
+
];
|
|
2171
|
+
const weight = /^\d+(\.\d+)?$/.test(lower) ? Number(lower) : null;
|
|
2172
|
+
const meant =
|
|
2173
|
+
words.find(w => w === lower) ??
|
|
2174
|
+
(weight != null
|
|
2175
|
+
? Object.entries(HEADING_WEIGHTS).reduce((a, b) =>
|
|
2176
|
+
Math.abs(Number(b[1]) - weight) <
|
|
2177
|
+
Math.abs(Number(a[1]) - weight)
|
|
2178
|
+
? b
|
|
2179
|
+
: a,
|
|
2180
|
+
)[0]
|
|
2181
|
+
: nearest(lower, words)[0]);
|
|
1213
2182
|
refuse(
|
|
1214
2183
|
E.ERR_THEME_BAD_VALUE,
|
|
1215
|
-
`headings takes ${DIALS.headings.values}, not "${v}"
|
|
2184
|
+
`headings takes ${DIALS.headings.values}, not "${v}".${weight != null && meant ? ` ${meant} sets weight ${HEADING_WEIGHTS[meant]}.` : ''}`,
|
|
1216
2185
|
{
|
|
1217
|
-
didYouMean:
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
run: [
|
|
1222
|
-
setLine(
|
|
1223
|
-
slug,
|
|
1224
|
-
'headings',
|
|
1225
|
-
nearest(v, [
|
|
1226
|
-
...Object.keys(HEADING_WEIGHTS),
|
|
1227
|
-
...Object.keys(HEADING_TRACKING),
|
|
1228
|
-
])[0] ?? Object.keys(HEADING_WEIGHTS)[0],
|
|
1229
|
-
s,
|
|
1230
|
-
),
|
|
1231
|
-
],
|
|
2186
|
+
didYouMean: meant ? [meant] : [],
|
|
2187
|
+
run: meant
|
|
2188
|
+
? [setLine(slug, 'headings', meant, s)]
|
|
2189
|
+
: [`${HOW} headings`],
|
|
1232
2190
|
},
|
|
1233
2191
|
);
|
|
2192
|
+
}
|
|
1234
2193
|
return s;
|
|
1235
2194
|
}
|
|
1236
2195
|
if (unset) return s;
|
|
@@ -1309,17 +2268,26 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1309
2268
|
const edge = /** @type {[string, number]} */ (
|
|
1310
2269
|
v === 'more' ? L[L.length - 1] : L[0]
|
|
1311
2270
|
);
|
|
2271
|
+
// The example stays inside what the dial takes (text-size: 10 to
|
|
2272
|
+
// 32); at that end, the exact token takes the size instead.
|
|
2273
|
+
const [lo, hi] = thing === 'text-size' ? [10, 32] : [0, Infinity];
|
|
2274
|
+
const want = Math.round(px + (v === 'more' ? 2 : -2));
|
|
2275
|
+
const atEnd = v === 'more' ? px >= hi : px <= lo;
|
|
1312
2276
|
const example = String(cur).replace(
|
|
1313
2277
|
/\d*\.?\d+/,
|
|
1314
|
-
String(Math.max(
|
|
2278
|
+
String(Math.max(lo, Math.min(hi, want))),
|
|
1315
2279
|
);
|
|
1316
2280
|
refuse(
|
|
1317
2281
|
E.ERR_THEME_BAD_VALUE,
|
|
1318
|
-
`${thing} is ${typeof cur === 'number' ? `${cur}px` : cur}, already ${v === 'more' ? 'above' : 'below'} its ${v === 'more' ? 'largest' : 'smallest'} step (${edge[0]}, ${edge[1]}px), so "${v}" has nowhere to go
|
|
2282
|
+
`${thing} is ${typeof cur === 'number' ? `${cur}px` : cur}, already ${v === 'more' ? 'above' : 'below'} its ${v === 'more' ? 'largest' : 'smallest'} step (${edge[0]}, ${edge[1]}px), so "${v}" has nowhere to go.${atEnd ? ` It is also the ${v === 'more' ? 'largest' : 'smallest'} size ${thing} takes (${lo} to ${hi}); the base font size token takes any size.` : ' Give an exact size.'}`,
|
|
1319
2283
|
{
|
|
1320
|
-
run:
|
|
1321
|
-
|
|
1322
|
-
|
|
2284
|
+
run: atEnd
|
|
2285
|
+
? [
|
|
2286
|
+
`${P} set ${slug} --font-size-base ${quoteCommandArgument(`${Math.max(1, want)}px`)}`,
|
|
2287
|
+
]
|
|
2288
|
+
: [
|
|
2289
|
+
`${P} set ${slug} ${thing} ${quoteCommandArgument(example)}${s.on ? ` --on ${s.on}` : ''}`,
|
|
2290
|
+
],
|
|
1323
2291
|
},
|
|
1324
2292
|
);
|
|
1325
2293
|
}
|
|
@@ -1469,10 +2437,15 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1469
2437
|
} else {
|
|
1470
2438
|
// A rung in any case, a size in px written any way: the plain form.
|
|
1471
2439
|
const lower = v.toLowerCase();
|
|
1472
|
-
|
|
2440
|
+
// Any CSS number in px (2e1px, +4px), the shared length parse.
|
|
2441
|
+
const pxLen = /px$/.test(lower) ? lengthPx(lower) : null;
|
|
1473
2442
|
if (CORNERS.some(([n]) => n === lower)) s.value = lower;
|
|
1474
|
-
else if (
|
|
1475
|
-
|
|
2443
|
+
else if (pxLen != null && pxLen >= 0)
|
|
2444
|
+
s.value =
|
|
2445
|
+
pxLen >= 1000 ? 'pill' : `${Math.round(pxLen * 10000) / 10000}px`;
|
|
2446
|
+
// No rounding, said any way, is square.
|
|
2447
|
+
else if (/^(0|0%|none|sharp|zero|no|flat)$/.test(lower))
|
|
2448
|
+
s.value = 'square';
|
|
1476
2449
|
else {
|
|
1477
2450
|
// What it most likely means, as a value corners takes: a simple
|
|
1478
2451
|
// calc() of px is worked out.
|
|
@@ -1574,6 +2547,17 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1574
2547
|
run: [setLine(slug, 'spacing', nearFactor, s)],
|
|
1575
2548
|
},
|
|
1576
2549
|
);
|
|
2550
|
+
else if (/^(0|none|zero|no|off)$/i.test(v))
|
|
2551
|
+
// Spacing scales the theme's space; it never goes to nothing. The
|
|
2552
|
+
// tightest rung is the nearest that works.
|
|
2553
|
+
refuse(
|
|
2554
|
+
E.ERR_THEME_BAD_VALUE,
|
|
2555
|
+
`spacing scales the theme's space and never removes it; the tightest it goes is ${SPACING[0][0]}. For no space on one part, set padding or gap 0 on it.`,
|
|
2556
|
+
{
|
|
2557
|
+
didYouMean: [SPACING[0][0]],
|
|
2558
|
+
run: [setLine(slug, 'spacing', SPACING[0][0], s)],
|
|
2559
|
+
},
|
|
2560
|
+
);
|
|
1577
2561
|
else if (!SPACING.some(([n]) => n === v))
|
|
1578
2562
|
refuse(
|
|
1579
2563
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -1601,10 +2585,34 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1601
2585
|
refuse(
|
|
1602
2586
|
E.ERR_THEME_BAD_VALUE,
|
|
1603
2587
|
'text-size applies to the whole theme; for one part use font-size.',
|
|
1604
|
-
{
|
|
2588
|
+
{
|
|
2589
|
+
// The asked size on the part: a rung's px (large is 15px), a
|
|
2590
|
+
// number as px; a step (more, less) has no size to carry.
|
|
2591
|
+
run: (() => {
|
|
2592
|
+
const w = String(v ?? '')
|
|
2593
|
+
.trim()
|
|
2594
|
+
.toLowerCase();
|
|
2595
|
+
const rung = TEXT_SIZE.find(([n]) => n === w)?.[1];
|
|
2596
|
+
const px =
|
|
2597
|
+
rung ?? (/^\d+(\.\d+)?(px)?$/.test(w) ? parseFloat(w) : null);
|
|
2598
|
+
return px != null && px > 0
|
|
2599
|
+
? [setLine(slug, 'font-size', `${px}px`, s)]
|
|
2600
|
+
: [`${HOW} font-size`];
|
|
2601
|
+
})(),
|
|
2602
|
+
},
|
|
1605
2603
|
);
|
|
1606
2604
|
if (step) s.value = ladder(TEXT_SIZE, 'medium');
|
|
1607
2605
|
else if (
|
|
2606
|
+
/^\d+\.\d+$/.test(v.trim()) &&
|
|
2607
|
+
Number(v) >= 10 &&
|
|
2608
|
+
Number(v) <= 32 &&
|
|
2609
|
+
Math.round(Number(v)) !== Number(v)
|
|
2610
|
+
) {
|
|
2611
|
+
// Core builds the type scale in whole pixels: the size is saved as
|
|
2612
|
+
// the whole pixel it builds, and the result says so.
|
|
2613
|
+
s.value = String(Math.round(Number(v)));
|
|
2614
|
+
s.note = `text-size ${v.trim()} is saved as ${s.value}: the type scale is built in whole pixels`;
|
|
2615
|
+
} else if (
|
|
1608
2616
|
!TEXT_SIZE.some(([n]) => n === v) &&
|
|
1609
2617
|
!(/^\d+(\.\d+)?$/.test(v.trim()) && Number(v) >= 10 && Number(v) <= 32)
|
|
1610
2618
|
)
|
|
@@ -1624,7 +2632,19 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1624
2632
|
didYouMean: [near],
|
|
1625
2633
|
run: [`${P} set ${slug} text-size ${near}`],
|
|
1626
2634
|
}
|
|
1627
|
-
: {
|
|
2635
|
+
: {
|
|
2636
|
+
run: [
|
|
2637
|
+
`${P} set ${slug} text-size ${
|
|
2638
|
+
// A size typed full-width or with a trailing dot
|
|
2639
|
+
// (18, 18.) is that size.
|
|
2640
|
+
/^\d+(\.\d+)?\.?$/.test(typedPlain(v)) &&
|
|
2641
|
+
Number(typedPlain(v).replace(/\.$/, '')) >= 10 &&
|
|
2642
|
+
Number(typedPlain(v).replace(/\.$/, '')) <= 32
|
|
2643
|
+
? String(Number(typedPlain(v).replace(/\.$/, '')))
|
|
2644
|
+
: (textSizePx(v) ?? 16)
|
|
2645
|
+
}`,
|
|
2646
|
+
],
|
|
2647
|
+
};
|
|
1628
2648
|
})(),
|
|
1629
2649
|
);
|
|
1630
2650
|
else if (!TEXT_SIZE.some(([n]) => n === v))
|
|
@@ -1639,7 +2659,8 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1639
2659
|
// What the value most likely meant: without quotes, a trailing `;`
|
|
1640
2660
|
// or `!important`, a bare hex with its #, the opaque color, or the
|
|
1641
2661
|
// nearest color name.
|
|
1642
|
-
const cleaned = v
|
|
2662
|
+
const cleaned = (pastedDeclaration(v)?.value || typedPlain(v))
|
|
2663
|
+
.replace(/^var\(\s*--[a-z0-9-]+\s*,\s*(.+)\)$/i, '$1')
|
|
1643
2664
|
.replace(/^(["'])(.*)\1$/, '$2')
|
|
1644
2665
|
.replace(/\s*!important\s*$/i, '')
|
|
1645
2666
|
.replace(/;+\s*$/, '')
|
|
@@ -1652,12 +2673,20 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1652
2673
|
problem.kind === 'unknown'
|
|
1653
2674
|
? nearest(v.toLowerCase(), COLOR_NAMES)
|
|
1654
2675
|
: [];
|
|
2676
|
+
// light-dark(a, b): the dial takes one color, from which it makes
|
|
2677
|
+
// both modes; the light one is offered.
|
|
2678
|
+
const lightHalf =
|
|
2679
|
+
/^light-dark\(\s*([^,]+?)\s*,/i.exec(cleaned)?.[1] ?? null;
|
|
1655
2680
|
const better =
|
|
1656
2681
|
problem.opaque ??
|
|
1657
|
-
(cleaned !== v && !colorProblem(cleaned)
|
|
2682
|
+
(cleaned !== v && !colorProblem(cleaned)
|
|
2683
|
+
? cleaned
|
|
2684
|
+
: lightHalf && !colorProblem(lightHalf)
|
|
2685
|
+
? lightHalf
|
|
2686
|
+
: near[0]);
|
|
1658
2687
|
refuse(
|
|
1659
2688
|
E.ERR_THEME_BAD_VALUE,
|
|
1660
|
-
`"${v}" ${problem.message}.${problem.kind === 'unknown' || problem.kind === 'malformed' ? ' Give a hex, a color name, or an rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch() or color() color.' : ''}`,
|
|
2689
|
+
`"${v}" ${problem.message}.${problem.kind === 'unknown' || problem.kind === 'malformed' ? ' Give a hex, a color name, or an rgb(), hsl(), hwb(), lab(), lch(), oklab(), oklch() or color() color.' : ''}${better && better === lightHalf ? ` ${thing} takes one color and makes both modes from it; the light one (${better}) is offered. For a dark value of your own, set --color-accent --mode dark after.` : ''}`,
|
|
1661
2690
|
{
|
|
1662
2691
|
didYouMean: better ? [better] : near,
|
|
1663
2692
|
run: [
|
|
@@ -1697,23 +2726,34 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1697
2726
|
},
|
|
1698
2727
|
);
|
|
1699
2728
|
s.value = String(next);
|
|
1700
|
-
}
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
|
|
2729
|
+
}
|
|
2730
|
+
const colors = data.settings.filter(
|
|
2731
|
+
x => x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
2732
|
+
);
|
|
2733
|
+
// With no color to saturate, no vibrancy value is suggested.
|
|
2734
|
+
const canApply = colors.some(x => !isGraySeed(seedOf(x.value)));
|
|
2735
|
+
if (!step && canApply && (!/^\d{1,3}$/.test(v) || Number(v) > 100)) {
|
|
2736
|
+
// The value meant: a step in other letters (MORE), or the number
|
|
2737
|
+
// read as CSS reads it (70%, 7e1), kept in range.
|
|
2738
|
+
const word = v.trim().toLowerCase();
|
|
2739
|
+
const n = cssNumber(v);
|
|
2740
|
+
const near = ['more', 'less'].includes(word)
|
|
2741
|
+
? word
|
|
2742
|
+
: n && (n.unit === '' || n.unit === '%')
|
|
2743
|
+
? String(Math.max(0, Math.min(100, Math.round(n.value))))
|
|
2744
|
+
: null;
|
|
1706
2745
|
refuse(
|
|
1707
2746
|
E.ERR_THEME_BAD_VALUE,
|
|
1708
2747
|
`vibrancy takes a whole number from 0 (gray) to 100 (most vivid), or more or less, not "${v}".`,
|
|
1709
|
-
{
|
|
2748
|
+
{
|
|
2749
|
+
run: near
|
|
2750
|
+
? [`${P} set ${slug} vibrancy ${near}`]
|
|
2751
|
+
: [`${HOW} vibrancy`],
|
|
2752
|
+
},
|
|
1710
2753
|
);
|
|
1711
|
-
} else s.value = String(Number(v));
|
|
1712
|
-
const colors = data.settings.filter(
|
|
1713
|
-
x => x.source === 'dial' && PALETTE_DIALS.includes(x.thing),
|
|
1714
|
-
);
|
|
2754
|
+
} else if (!step && /^\d{1,3}$/.test(v)) s.value = String(Number(v));
|
|
1715
2755
|
// A gray has no color to saturate, so vibrancy changes nothing.
|
|
1716
|
-
if (!
|
|
2756
|
+
if (!canApply)
|
|
1717
2757
|
refuse(
|
|
1718
2758
|
E.ERR_THEME_NO_EFFECT,
|
|
1719
2759
|
colors.length
|
|
@@ -1741,7 +2781,7 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1741
2781
|
const family = !parts && matchValue('font-family', v);
|
|
1742
2782
|
const example = `${P} set ${slug} font-family ${quoteCommandArgument(exampleOf('font', 'font-family'))} --on ${s.on}`;
|
|
1743
2783
|
const runs = parts
|
|
1744
|
-
? parts.system
|
|
2784
|
+
? parts.system || parts.repeat
|
|
1745
2785
|
? [example]
|
|
1746
2786
|
: Object.entries(parts).map(
|
|
1747
2787
|
([k, x]) =>
|
|
@@ -1754,9 +2794,55 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1754
2794
|
: [example];
|
|
1755
2795
|
refuse(
|
|
1756
2796
|
E.ERR_THEME_BAD_VALUE,
|
|
1757
|
-
parts?.
|
|
1758
|
-
? `font --on takes body, heading, or code, and "${v}"
|
|
1759
|
-
:
|
|
2797
|
+
parts?.repeat
|
|
2798
|
+
? `font --on takes body, heading, or code, and "${v}" ${parts.part?.includes(',') ? `gives ${parts.repeat} more times than the parts that take it (${parts.part})` : `gives ${parts.part ?? 'one part of the font'} twice (${parts.repeat} is the second)`}, which no CSS font value does. For one part's font use font-family with a family, and set each other part of the font once.`
|
|
2799
|
+
: parts?.system
|
|
2800
|
+
? `font --on takes body, heading, or code, and "${v}" is a system font keyword of the CSS font shorthand, which sets every part of the font at once. For one part's font use font-family with a family.`
|
|
2801
|
+
: `font --on takes body, heading, or code. For one part's font use font-family${parts ? ', and the other parts of the font shorthand one by one' : ''}.`,
|
|
2802
|
+
{run: runs},
|
|
2803
|
+
);
|
|
2804
|
+
}
|
|
2805
|
+
// A size, a weight or a whole CSS font value is no family: the font
|
|
2806
|
+
// dial and the roles take the family alone.
|
|
2807
|
+
// A variable is answered below, as the token form.
|
|
2808
|
+
const fontParts =
|
|
2809
|
+
step || /\b(var|env)\(/i.test(v) ? null : fontShorthandParts(v);
|
|
2810
|
+
if (
|
|
2811
|
+
fontParts &&
|
|
2812
|
+
(fontParts.repeat ||
|
|
2813
|
+
fontParts.system ||
|
|
2814
|
+
Object.keys(fontParts).some(k => k !== 'font-family'))
|
|
2815
|
+
) {
|
|
2816
|
+
const fam = fontParts['font-family'];
|
|
2817
|
+
const on = s.on ? ` --on ${s.on}` : '';
|
|
2818
|
+
/** @type {string[]} */
|
|
2819
|
+
const runs = [
|
|
2820
|
+
`${P} set ${slug} font ${quoteCommandArgument(fam ?? 'inter')}${on}`,
|
|
2821
|
+
];
|
|
2822
|
+
if (fontParts['font-size'] && (!s.on || s.on === 'body'))
|
|
2823
|
+
runs.push(
|
|
2824
|
+
`${P} set ${slug} --font-size-base ${quoteCommandArgument(fontParts['font-size'])}`,
|
|
2825
|
+
);
|
|
2826
|
+
const w = fontParts['font-weight'];
|
|
2827
|
+
if (w && (!s.on || s.on === 'heading')) {
|
|
2828
|
+
const num =
|
|
2829
|
+
w === 'bold' ? 700 : w === 'normal' ? 400 : Number(w) || null;
|
|
2830
|
+
const rung = num
|
|
2831
|
+
? Object.entries(HEADING_WEIGHTS).reduce((a, b) =>
|
|
2832
|
+
Math.abs(Number(b[1]) - num) < Math.abs(Number(a[1]) - num)
|
|
2833
|
+
? b
|
|
2834
|
+
: a,
|
|
2835
|
+
)[0]
|
|
2836
|
+
: null;
|
|
2837
|
+
if (rung) runs.push(`${P} set ${slug} headings ${rung}`);
|
|
2838
|
+
}
|
|
2839
|
+
refuse(
|
|
2840
|
+
E.ERR_THEME_BAD_VALUE,
|
|
2841
|
+
`font${s.on ? ` --on ${s.on}` : ''} takes a family or a preset, and "${v}" ${fam ? 'is a whole CSS font value' : 'is no family'}: ${
|
|
2842
|
+
Object.keys(fontParts)
|
|
2843
|
+
.filter(k => !['font-family', 'repeat', 'system'].includes(k))
|
|
2844
|
+
.join(', ') || 'a keyword'
|
|
2845
|
+
} ${fam ? 'would be read as part of the family name' : 'would be read as a family name'}, and the text would fall back.`,
|
|
1760
2846
|
{run: runs},
|
|
1761
2847
|
);
|
|
1762
2848
|
}
|
|
@@ -1847,6 +2933,11 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1847
2933
|
],
|
|
1848
2934
|
},
|
|
1849
2935
|
);
|
|
2936
|
+
const file = fontFileReason(s.value);
|
|
2937
|
+
if (file)
|
|
2938
|
+
refuse(E.ERR_THEME_BAD_VALUE, file, {
|
|
2939
|
+
run: [`${P} set ${slug} font inter${s.on ? ` --on ${s.on}` : ''}`],
|
|
2940
|
+
});
|
|
1850
2941
|
if (s.value.split(',').some(f => f.trim().length > 100))
|
|
1851
2942
|
refuse(
|
|
1852
2943
|
E.ERR_THEME_BAD_VALUE,
|
|
@@ -1875,16 +2966,40 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1875
2966
|
const meant = v
|
|
1876
2967
|
.toLowerCase()
|
|
1877
2968
|
.replace(/^base\s*[=:]\s*/, '')
|
|
1878
|
-
.trim()
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
2969
|
+
.trim()
|
|
2970
|
+
.replace(/^@astryxdesign\/theme-/, '')
|
|
2971
|
+
.replace(/^theme-/, '');
|
|
2972
|
+
// Only a base this project has installed is suggested; a theme
|
|
2973
|
+
// made here is never a base.
|
|
2974
|
+
const bases = [
|
|
2975
|
+
...LIKE.filter(b => baseInstalled(ctx.project, b)),
|
|
2976
|
+
'none',
|
|
2977
|
+
];
|
|
2978
|
+
// The base named in other letters is the one meant; a guess is
|
|
2979
|
+
// only among installed bases.
|
|
2980
|
+
const near =
|
|
2981
|
+
bases.includes(meant) || LIKE.includes(meant)
|
|
2982
|
+
? [meant]
|
|
2983
|
+
: nearest(meant, bases);
|
|
2984
|
+
// A built-in base named that is not installed: install it, then the
|
|
2985
|
+
// same set.
|
|
2986
|
+
const install =
|
|
2987
|
+
LIKE.includes(meant) && !baseInstalled(ctx.project, meant)
|
|
2988
|
+
? [installLine(ctx.project.cwd, [`@astryxdesign/theme-${meant}`])]
|
|
2989
|
+
: [];
|
|
2990
|
+
const madeHere = fs.existsSync(themeFiles(ctx.project.cwd, v).settings);
|
|
1882
2991
|
refuse(
|
|
1883
2992
|
E.ERR_THEME_BAD_VALUE,
|
|
1884
|
-
`base takes ${DIALS.base.values}, not "${v}"
|
|
2993
|
+
`base takes ${DIALS.base.values}, not "${v}".${madeHere ? ` ${v} is a theme made here, and a theme builds only on a built-in one.` : ''}`,
|
|
1885
2994
|
{
|
|
1886
2995
|
didYouMean: near,
|
|
1887
|
-
|
|
2996
|
+
// With nothing near, every base that works here, none first.
|
|
2997
|
+
run: near[0]
|
|
2998
|
+
? [...install, `${P} set ${slug} base ${near[0]}`]
|
|
2999
|
+
: [
|
|
3000
|
+
'none',
|
|
3001
|
+
...LIKE.filter(b => baseInstalled(ctx.project, b)),
|
|
3002
|
+
].map(b => `${P} set ${slug} base ${b}`),
|
|
1888
3003
|
},
|
|
1889
3004
|
);
|
|
1890
3005
|
}
|
|
@@ -1908,7 +3023,11 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1908
3023
|
`mode takes both, light-only, or dark-only, not "${v}".`,
|
|
1909
3024
|
{
|
|
1910
3025
|
didYouMean: near,
|
|
1911
|
-
run: near[0]
|
|
3026
|
+
run: near[0]
|
|
3027
|
+
? [`${P} set ${slug} mode ${near[0]}`]
|
|
3028
|
+
: ['both', 'light-only', 'dark-only'].map(
|
|
3029
|
+
m => `${P} set ${slug} mode ${m}`,
|
|
3030
|
+
),
|
|
1912
3031
|
},
|
|
1913
3032
|
);
|
|
1914
3033
|
}
|
|
@@ -1922,6 +3041,18 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1922
3041
|
`"${thing}" is private to a component; themes cannot set it.`,
|
|
1923
3042
|
{run: [`${HOW} ${quoteCommandArgument(thing)}`]},
|
|
1924
3043
|
);
|
|
3044
|
+
// A token typed with its declaration's colon (`--color-accent:`).
|
|
3045
|
+
const colonless = thing.replace(/\s*:+\s*$/, '');
|
|
3046
|
+
if (!(thing in TOKENS) && colonless !== thing && colonless in TOKENS)
|
|
3047
|
+
refuse(
|
|
3048
|
+
E.ERR_THEME_UNKNOWN_TOKEN,
|
|
3049
|
+
`"${thing}" ends with a colon; the token is ${colonless}.`,
|
|
3050
|
+
{
|
|
3051
|
+
run: [
|
|
3052
|
+
`${P} set ${slug} ${colonless}${v ? ` ${quoteCommandArgument(v.replace(/;+\s*$/, ''))}` : ''}`,
|
|
3053
|
+
],
|
|
3054
|
+
},
|
|
3055
|
+
);
|
|
1925
3056
|
if (!(thing in TOKENS)) {
|
|
1926
3057
|
refuse(E.ERR_THEME_UNKNOWN_TOKEN, `"${thing}" is not a design token.`, {
|
|
1927
3058
|
didYouMean: nearest(thing, Object.keys(TOKENS)),
|
|
@@ -1965,17 +3096,21 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
1965
3096
|
C.typeOf,
|
|
1966
3097
|
slug,
|
|
1967
3098
|
C.propsReading(thing),
|
|
3099
|
+
multipliedTokens(ctx.project).has(thing),
|
|
1968
3100
|
);
|
|
1969
|
-
checkCycle(thing, v, TOKENS, data);
|
|
3101
|
+
checkCycle(thing, v, TOKENS, data, ctx.project.cwd, s);
|
|
1970
3102
|
// With no part data, what reads the token is unknown: save, unchecked.
|
|
1971
3103
|
if (ctx.unchecked) return s;
|
|
1972
3104
|
const parts = partsOf(ctx, s);
|
|
1973
3105
|
if (parts.length) {
|
|
3106
|
+
// A part the base theme's own rule makes read the token reads it.
|
|
1974
3107
|
const results = parts.map(
|
|
1975
3108
|
t =>
|
|
1976
3109
|
/** @type {[string, import('./_compiler.mjs').CheckFailure | null]} */ ([
|
|
1977
3110
|
t,
|
|
1978
|
-
|
|
3111
|
+
baseComponentReads(ctx.project.cwd, data.like, t).has(thing)
|
|
3112
|
+
? null
|
|
3113
|
+
: C.checkTokenOn(slug, t, thing, v, P),
|
|
1979
3114
|
]),
|
|
1980
3115
|
);
|
|
1981
3116
|
const recorded = results.filter(([, r]) => !r?.soft);
|
|
@@ -2030,8 +3165,15 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2030
3165
|
),
|
|
2031
3166
|
also: r0.also,
|
|
2032
3167
|
});
|
|
2033
|
-
} else if (hard.length)
|
|
2034
|
-
|
|
3168
|
+
} else if (hard.length) {
|
|
3169
|
+
// Some members read it and take the change; name them, and say
|
|
3170
|
+
// what the others read instead (from the check's own message).
|
|
3171
|
+
const readers = recorded.filter(([, r]) => !r).map(([t]) => t);
|
|
3172
|
+
const instead = /reads: ([^.]+)\./.exec(
|
|
3173
|
+
String(/** @type {any} */ (hard[0][1])?.message ?? ''),
|
|
3174
|
+
)?.[1];
|
|
3175
|
+
s.note = `${readers.length ? `${readers.join(', ')} read ${thing} and take this change; ` : ''}the editor's render of ${hard.map(([t]) => t).join(', ')} did not read it${instead ? ` (${hard[0][0]} reads ${instead})` : ''}, so those stay as they were; each part is rendered once, with its default props, so a variant that reads it may be missed`;
|
|
3176
|
+
}
|
|
2035
3177
|
if (!s.note && recorded.length) {
|
|
2036
3178
|
const fed = C.feeds(recorded[0][0], thing);
|
|
2037
3179
|
if (fed.length) s.note = `feeds ${C.fmtFeeds(fed, recorded[0][0])}`;
|
|
@@ -2071,6 +3213,24 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2071
3213
|
return s;
|
|
2072
3214
|
}
|
|
2073
3215
|
const prop = kebab(thing);
|
|
3216
|
+
// A token typed without its leading dashes (color-accent,
|
|
3217
|
+
// text-body-size): the token, when no CSS property has that name.
|
|
3218
|
+
if (
|
|
3219
|
+
!unset &&
|
|
3220
|
+
!thing.startsWith('-') &&
|
|
3221
|
+
`--${thing}` in TOKENS &&
|
|
3222
|
+
!cssProperties().has(thing)
|
|
3223
|
+
)
|
|
3224
|
+
refuse(
|
|
3225
|
+
E.ERR_THEME_UNKNOWN_THING,
|
|
3226
|
+
`"${thing}" is not a dial, a token, or a CSS property; the token is --${thing}.`,
|
|
3227
|
+
{
|
|
3228
|
+
didYouMean: [`--${thing}`],
|
|
3229
|
+
run: [
|
|
3230
|
+
`${P} set ${slug} --${thing}${value ? ` ${quoteCommandArgument(value)}` : ''}`,
|
|
3231
|
+
],
|
|
3232
|
+
},
|
|
3233
|
+
);
|
|
2074
3234
|
// A dial typed in other letters (Font, FONT) is the dial, not the CSS
|
|
2075
3235
|
// property of the same name.
|
|
2076
3236
|
if (thing !== thing.toLowerCase() && DIALS[thing.toLowerCase()])
|
|
@@ -2101,6 +3261,134 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2101
3261
|
s.source = 'property';
|
|
2102
3262
|
s.thing = prop;
|
|
2103
3263
|
if (unset) return s;
|
|
3264
|
+
// A length property (padding, a corner radius, a gap) that reads a
|
|
3265
|
+
// design token of another kind (a color, a font, a duration) gets
|
|
3266
|
+
// nothing the browser keeps: refused like the token form.
|
|
3267
|
+
if (matchValue(prop, '1px') && !matchValue(prop, 'red'))
|
|
3268
|
+
for (const m of v.matchAll(/var\(\s*(--[a-z0-9-]+)/gi)) {
|
|
3269
|
+
const kind = ctx.C.isToken(m[1]) ? ctx.C.typeOf(m[1]) : null;
|
|
3270
|
+
if (kind && !['size', 'number', 'other'].includes(kind)) {
|
|
3271
|
+
const like = /radius/.test(prop)
|
|
3272
|
+
? 'var(--radius-container)'
|
|
3273
|
+
: /^(padding|margin|gap|row-gap|column-gap|inset)/.test(prop)
|
|
3274
|
+
? 'var(--spacing-4)'
|
|
3275
|
+
: exampleOf('size', prop);
|
|
3276
|
+
refuse(
|
|
3277
|
+
E.ERR_THEME_BAD_VALUE,
|
|
3278
|
+
`${prop} takes a length; ${m[1]} is a ${kind}, so the browser would drop it.`,
|
|
3279
|
+
{
|
|
3280
|
+
run: [
|
|
3281
|
+
`${P} set ${slug} ${prop} ${quoteCommandArgument(v.replace(new RegExp(`var\\(\\s*${m[1]}\\s*(,[^)]*)?\\)`, 'i'), like))} --on ${quoteCommandArgument(String(s.on))}${s.state ? ` --state ${s.state}` : ''}${s.when ? ` --when ${quoteCommandArgument(s.when)}` : ''}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
3282
|
+
`${HOW} --list tokens ${/radius/.test(prop) ? 'radius' : 'spacing'}`,
|
|
3283
|
+
],
|
|
3284
|
+
},
|
|
3285
|
+
);
|
|
3286
|
+
}
|
|
3287
|
+
}
|
|
3288
|
+
// initial and unset leave a part's padding variables without a value:
|
|
3289
|
+
// the part takes Core's own default, which the result names.
|
|
3290
|
+
if (/^padding/.test(prop) && /^(initial|unset)$/i.test(v.trim()) && s.on) {
|
|
3291
|
+
const owner = (ctx.derived?.[s.on] ?? []).some(
|
|
3292
|
+
d => d.property === 'padding' && d.expand,
|
|
3293
|
+
)
|
|
3294
|
+
? s.on
|
|
3295
|
+
: CARD_LIKE.includes(s.on)
|
|
3296
|
+
? 'card'
|
|
3297
|
+
: null;
|
|
3298
|
+
const fallback = owner
|
|
3299
|
+
? coreFallback(ctx.project, `--astryx-${owner}-padding`)
|
|
3300
|
+
: null;
|
|
3301
|
+
if (fallback) {
|
|
3302
|
+
const token = /^var\((--[a-z0-9-]+)\)$/i.exec(fallback)?.[1];
|
|
3303
|
+
const px = token
|
|
3304
|
+
? lengthPx(String(ctx.project.core.tokenDefaults?.[token] ?? ''))
|
|
3305
|
+
: lengthPx(fallback);
|
|
3306
|
+
s.note = `${v.trim()} means ${s.on}'s own default padding from Core: ${fallback}${px != null ? ` (${px}px)` : ''}`;
|
|
3307
|
+
}
|
|
3308
|
+
}
|
|
3309
|
+
// A part whose Core box reads this property's variable inside calc()
|
|
3310
|
+
// (a composer's or a menu's padding in its corners): one length is all
|
|
3311
|
+
// that works there, whether a length, a variable, or calc(), min(),
|
|
3312
|
+
// max() or clamp() giving one.
|
|
3313
|
+
if (s.on && ctx.derived && !WRAPPED_PARTS[s.on]) {
|
|
3314
|
+
const inCalc = calcVars(ctx.project);
|
|
3315
|
+
const hit = partsOf(ctx, s).find(t =>
|
|
3316
|
+
(ctx.derived?.[t] ?? []).some(
|
|
3317
|
+
d =>
|
|
3318
|
+
d.property === camel(prop) &&
|
|
3319
|
+
(d.vars ?? []).some(x => inCalc.has(x.toLowerCase())),
|
|
3320
|
+
),
|
|
3321
|
+
);
|
|
3322
|
+
if (hit && !oneLength(v)) {
|
|
3323
|
+
const first = splitTop(v.trim(), ' ')[0] ?? '';
|
|
3324
|
+
const n = cssNumber(first);
|
|
3325
|
+
const fix = oneLength(first)
|
|
3326
|
+
? first
|
|
3327
|
+
: n && !n.unit
|
|
3328
|
+
? `${n.value}px`
|
|
3329
|
+
: '12px';
|
|
3330
|
+
refuse(
|
|
3331
|
+
E.ERR_THEME_BAD_VALUE,
|
|
3332
|
+
`${hit} reads ${prop} inside calc() (its corners and inner rows follow it), where only one length works: a length, a variable, or calc(), min(), max() or clamp() giving one; not "${v.trim()}".`,
|
|
3333
|
+
{run: [setLine(slug, prop, fix, s)]},
|
|
3334
|
+
);
|
|
3335
|
+
}
|
|
3336
|
+
}
|
|
3337
|
+
// A part whose visible box is drawn inside its themed wrapper: only
|
|
3338
|
+
// what reaches that box is taken.
|
|
3339
|
+
const wrapped = s.on ? WRAPPED_PARTS[s.on] : undefined;
|
|
3340
|
+
if (wrapped) {
|
|
3341
|
+
const surface =
|
|
3342
|
+
/^(background|border|box-shadow|outline)(-|$)/.test(prop) &&
|
|
3343
|
+
!/radius/.test(prop);
|
|
3344
|
+
const side = /^padding-/.test(prop);
|
|
3345
|
+
const density = prop === 'padding' && /^density:/.test(s.when ?? '');
|
|
3346
|
+
// Core reads the box's padding only from a variable of its own, which
|
|
3347
|
+
// a theme may not write, and a padding property would pad the
|
|
3348
|
+
// transparent wrapper too.
|
|
3349
|
+
if (prop === 'padding' && !density)
|
|
3350
|
+
refuse(
|
|
3351
|
+
E.ERR_THEME_NO_EFFECT,
|
|
3352
|
+
`${s.on}'s theme class is on a transparent wrapper; ${wrapped.box} inside it reads its padding from a variable of Core's own, which a theme cannot set, and a padding on the wrapper would add a gap outside the box. Its padding follows the theme's spacing.`,
|
|
3353
|
+
{run: [`${HOW} ${s.on}`]},
|
|
3354
|
+
);
|
|
3355
|
+
if (surface || side || density)
|
|
3356
|
+
refuse(
|
|
3357
|
+
E.ERR_THEME_BAD_VALUE,
|
|
3358
|
+
surface
|
|
3359
|
+
? `${s.on}'s theme class is on a transparent wrapper; Core draws ${wrapped.box} inside it, and that box has no class of its own, so ${prop} would paint the wrapper's square corners, not the box. Its colors come from the theme's popover tokens.`
|
|
3360
|
+
: side
|
|
3361
|
+
? `${s.on}'s theme class is on a transparent wrapper; ${wrapped.box} inside it reads one padding for every side, so a side padding would add a strip outside the box. Set padding (every side).`
|
|
3362
|
+
: `${s.on} sets its own padding on ${wrapped.box} for each density, which a theme padding for one density cannot reach. Set padding for every density.`,
|
|
3363
|
+
{
|
|
3364
|
+
run: surface
|
|
3365
|
+
? [
|
|
3366
|
+
`${P} set ${slug} --color-background-popover ${quoteCommandArgument('#ffffff')}`,
|
|
3367
|
+
`${HOW} ${s.on}`,
|
|
3368
|
+
]
|
|
3369
|
+
: [`${HOW} ${s.on}`],
|
|
3370
|
+
},
|
|
3371
|
+
);
|
|
3372
|
+
}
|
|
3373
|
+
// Core writes a container part's padding to variables of its own, where
|
|
3374
|
+
// a keyword that takes its value from elsewhere means no value at all.
|
|
3375
|
+
if (
|
|
3376
|
+
/^padding/.test(prop) &&
|
|
3377
|
+
/^(inherit|revert|revert-layer)$/i.test(v.trim()) &&
|
|
3378
|
+
(ctx.derived?.[/** @type {string} */ (s.on)] ?? []).some(
|
|
3379
|
+
d => d.property === 'padding' && d.expand,
|
|
3380
|
+
)
|
|
3381
|
+
)
|
|
3382
|
+
refuse(
|
|
3383
|
+
E.ERR_THEME_BAD_VALUE,
|
|
3384
|
+
`${s.on} writes ${prop} to variables of its own, where "${v.trim()}" has nothing to take its value from, so ${s.on} would fall back to its default padding. Give a length, or undo the padding to go back to the base theme's.`,
|
|
3385
|
+
{
|
|
3386
|
+
run: [
|
|
3387
|
+
`${P} set ${slug} ${prop} 0 --on ${s.on}`,
|
|
3388
|
+
`${P} undo ${slug} ${prop} --on ${s.on}`,
|
|
3389
|
+
],
|
|
3390
|
+
},
|
|
3391
|
+
);
|
|
2104
3392
|
// Text, headings and links take their color from the color prop.
|
|
2105
3393
|
const textParts = COLOR_PROP_PARTS;
|
|
2106
3394
|
if (
|
|
@@ -2109,16 +3397,22 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2109
3397
|
) {
|
|
2110
3398
|
if (partsOf(ctx, s).every(t => textParts.includes(t))) {
|
|
2111
3399
|
const named = /^color:([\w-]+)$/.exec(s.when ?? '')?.[1];
|
|
3400
|
+
// The typed value goes into the token only when it is a plain color;
|
|
3401
|
+
// else the nearest color name, else an example.
|
|
3402
|
+
const color =
|
|
3403
|
+
v && isColorValue(v) && isFixedColor(v) && !/\bvar\(/i.test(v)
|
|
3404
|
+
? v
|
|
3405
|
+
: (nearestColorName(v) ?? '#1f2937');
|
|
2112
3406
|
refuse(
|
|
2113
3407
|
E.ERR_THEME_NO_EFFECT,
|
|
2114
3408
|
`${s.on} takes its color from the color prop, which Core sets after part styles, so a color set on ${s.on} never shows. Set the text color token instead.`,
|
|
2115
3409
|
{
|
|
2116
3410
|
run: [
|
|
2117
|
-
`${P} set ${slug} --color-text-${named ?? 'primary'} ${quoteCommandArgument(
|
|
3411
|
+
`${P} set ${slug} --color-text-${named ?? 'primary'} ${quoteCommandArgument(color)}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
2118
3412
|
...(named
|
|
2119
3413
|
? []
|
|
2120
3414
|
: [
|
|
2121
|
-
`${P} set ${slug} --color-text-secondary ${quoteCommandArgument(
|
|
3415
|
+
`${P} set ${slug} --color-text-secondary ${quoteCommandArgument(color)}${s.mode ? ` --mode ${s.mode}` : ''}`,
|
|
2122
3416
|
]),
|
|
2123
3417
|
],
|
|
2124
3418
|
},
|
|
@@ -2131,7 +3425,13 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2131
3425
|
// With a state, the other mode would take one variant's value for
|
|
2132
3426
|
// every variant, so any part whose variants read their own value needs
|
|
2133
3427
|
// the variant named.
|
|
2134
|
-
|
|
3428
|
+
// Per-variant colors only: a value that is not a color is refused for
|
|
3429
|
+
// --mode below, with the reason that holds.
|
|
3430
|
+
if (
|
|
3431
|
+
s.mode &&
|
|
3432
|
+
!s.when &&
|
|
3433
|
+
(COLOR_PROPS.test(prop) || (prop === 'background' && isColorValue(v)))
|
|
3434
|
+
) {
|
|
2135
3435
|
const g = C.groupOfProp(
|
|
2136
3436
|
prop === 'background' ? 'background-color' : prop,
|
|
2137
3437
|
);
|
|
@@ -2225,6 +3525,27 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2225
3525
|
},
|
|
2226
3526
|
);
|
|
2227
3527
|
}
|
|
3528
|
+
// A var() inside a color (color-mix(in srgb, var(--x, 12px), red)) shows
|
|
3529
|
+
// its fallback when the variable is unset: that must be a color too,
|
|
3530
|
+
// except in a color's channels (rgb(var(--r, 0) 0 0)).
|
|
3531
|
+
if (COLOR_PROPS.test(prop) || (prop === 'background' && isColorValue(v)))
|
|
3532
|
+
for (const {fallback, within} of innerVarFallbacks(v)) {
|
|
3533
|
+
if (
|
|
3534
|
+
within.some(f =>
|
|
3535
|
+
/^(rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)$/i.test(f),
|
|
3536
|
+
) ||
|
|
3537
|
+
/\bvar\(/i.test(fallback)
|
|
3538
|
+
)
|
|
3539
|
+
continue;
|
|
3540
|
+
if (fallback === '' || !isColorValue(fallback))
|
|
3541
|
+
refuse(
|
|
3542
|
+
E.ERR_THEME_BAD_VALUE,
|
|
3543
|
+
fallback === ''
|
|
3544
|
+
? `${prop} takes a color; the var() in "${v}" has an empty fallback, which leaves it with no color when the variable is unset.`
|
|
3545
|
+
: `${prop} takes a color; "${fallback}", a var() fallback in "${v}", is not one, so the ${prop} is lost when the variable is unset.`,
|
|
3546
|
+
{run: [setLine(slug, prop, exampleOf('color', prop), s)]},
|
|
3547
|
+
);
|
|
3548
|
+
}
|
|
2228
3549
|
// Every scope flag but --mode carries into the runs.
|
|
2229
3550
|
const whenFlag = `${s.when ? ` --when ${s.when}` : ''}${s.state ? ` --state ${s.state}` : ''}${s.only ? ' --only' : ''}`;
|
|
2230
3551
|
if (
|
|
@@ -2232,8 +3553,12 @@ function checkOne(ctx, slug, data, thing, value, flags, unset) {
|
|
|
2232
3553
|
!COLOR_PROPS.test(prop) &&
|
|
2233
3554
|
!(prop === 'background' && isColorValue(v))
|
|
2234
3555
|
) {
|
|
2235
|
-
|
|
2236
|
-
|
|
3556
|
+
// A color mixed into the value (a border's) can go per mode on its
|
|
3557
|
+
// own; a value with none (a radius) is just set for both modes.
|
|
3558
|
+
const hasColor = /#[0-9a-f]{3,8}\b|rgba?\(|hsla?\(/i.test(v);
|
|
3559
|
+
const alt = !hasColor
|
|
3560
|
+
? null
|
|
3561
|
+
: prop === 'border' || prop.startsWith('border-')
|
|
2237
3562
|
? 'border-color'
|
|
2238
3563
|
: prop === 'background'
|
|
2239
3564
|
? 'background-color'
|
|
@@ -2435,3 +3760,182 @@ const dialWrites = token =>
|
|
|
2435
3760
|
|
|
2436
3761
|
/** The aspects `how` answers at the whole-theme level. */
|
|
2437
3762
|
export const ASPECTS = [...Object.keys(GROUPS), 'colors'];
|
|
3763
|
+
|
|
3764
|
+
/**
|
|
3765
|
+
* A setting's value as set reads it: one line, and tabs as spaces; in a font
|
|
3766
|
+
* value every run of spaces or tabs is one space, so `'Open Sans'` and
|
|
3767
|
+
* `'Open\tSans'` both name Open Sans.
|
|
3768
|
+
* @param {string} thing
|
|
3769
|
+
* @param {string} value
|
|
3770
|
+
*/
|
|
3771
|
+
export function normalValue(thing, value) {
|
|
3772
|
+
const one = value.replace(/\s*[\r\n]+\s*/g, ' ');
|
|
3773
|
+
return (
|
|
3774
|
+
thing === 'font' || thing.startsWith('--font-family')
|
|
3775
|
+
? familySpaces(one)
|
|
3776
|
+
: one.replace(/\t+/g, ' ')
|
|
3777
|
+
).trim();
|
|
3778
|
+
}
|
|
3779
|
+
|
|
3780
|
+
/** @see familySpaces in _settings.mjs, re-exported for the checks. */
|
|
3781
|
+
export {familySpaces};
|
|
3782
|
+
|
|
3783
|
+
/**
|
|
3784
|
+
* The first var() in a value that names no design token and would read
|
|
3785
|
+
* nothing (no fallback), or one a letter or two off a token, else null.
|
|
3786
|
+
* Dials take their own words, so a dial's var() with no near token passes.
|
|
3787
|
+
* @param {string} v
|
|
3788
|
+
* @param {Record<string, unknown>} TOKENS
|
|
3789
|
+
* @param {string} thing
|
|
3790
|
+
* @returns {{message: string, near: string[], close: boolean, fixed?: string} | null}
|
|
3791
|
+
*/
|
|
3792
|
+
export function unknownVarRef(v, TOKENS, thing) {
|
|
3793
|
+
// A var() whose name is not a custom property (`var(color-x)`, `var(-x)`,
|
|
3794
|
+
// `var()`, `var(--)`) makes the whole value invalid: the browser drops
|
|
3795
|
+
// it. Named with the repaired form when there is one.
|
|
3796
|
+
for (const m of v.matchAll(/var\(\s*([^,)]*?)\s*(?=[,)]|$)/gi)) {
|
|
3797
|
+
const raw = m[1];
|
|
3798
|
+
if (/^--[a-z0-9_][a-z0-9_-]*$/i.test(raw)) continue;
|
|
3799
|
+
const bare = raw.replace(/^-+/, '');
|
|
3800
|
+
const repaired = /^[a-z0-9][a-z0-9_-]*$/i.test(bare) ? `--${bare}` : null;
|
|
3801
|
+
return {
|
|
3802
|
+
message: `var(${raw}) is not a CSS variable reference: a variable's name starts with --${repaired ? `, as in var(${repaired})` : ''}, and the browser drops the whole value otherwise.`,
|
|
3803
|
+
near: repaired ? [repaired] : [],
|
|
3804
|
+
close: Boolean(repaired && repaired in TOKENS),
|
|
3805
|
+
...(repaired
|
|
3806
|
+
? {fixed: v.replace(m[0], m[0].replace(raw, repaired))}
|
|
3807
|
+
: {}),
|
|
3808
|
+
};
|
|
3809
|
+
}
|
|
3810
|
+
for (const [, name, comma] of v.matchAll(/var\(\s*(--[a-z0-9-]+)\s*(,)?/gi)) {
|
|
3811
|
+
if (name in TOKENS || name.startsWith('--_')) continue;
|
|
3812
|
+
// A fallback always gives a value: never "reads nothing" (a likely
|
|
3813
|
+
// typo is noted when the setting is made).
|
|
3814
|
+
if (comma) continue;
|
|
3815
|
+
const near = nearest(name, Object.keys(TOKENS)).filter(
|
|
3816
|
+
n => levenshteinDistance(n, name) <= 2,
|
|
3817
|
+
);
|
|
3818
|
+
if (near.length)
|
|
3819
|
+
return {
|
|
3820
|
+
message: `${name} is not a design token, so var(${name}) would read nothing.`,
|
|
3821
|
+
near,
|
|
3822
|
+
close: true,
|
|
3823
|
+
};
|
|
3824
|
+
// Without a fallback, a variable no design token names reads nothing;
|
|
3825
|
+
// var(--x, <value>) falls back to the value.
|
|
3826
|
+
if (!comma && !DIALS[thing])
|
|
3827
|
+
return {
|
|
3828
|
+
message: `${name} is not a design token, so var(${name}) would read nothing. Give a fallback (var(${name}, <value>)) for a variable your app sets, or use a design token.`,
|
|
3829
|
+
near: nearest(name, Object.keys(TOKENS)).slice(0, 3),
|
|
3830
|
+
close: false,
|
|
3831
|
+
};
|
|
3832
|
+
}
|
|
3833
|
+
return null;
|
|
3834
|
+
}
|
|
3835
|
+
|
|
3836
|
+
/**
|
|
3837
|
+
* Why a font value names a font file or a link rather than a family
|
|
3838
|
+
* (`./fonts/My.woff2`, `Inter.ttf`, `https://fonts.googleapis.com/…`,
|
|
3839
|
+
* `url(…)`), or null. A theme names families; a file is loaded with
|
|
3840
|
+
* @font-face or a font package.
|
|
3841
|
+
* @param {string} value
|
|
3842
|
+
* @returns {string | null}
|
|
3843
|
+
*/
|
|
3844
|
+
export function fontFileReason(value) {
|
|
3845
|
+
const hit = String(value)
|
|
3846
|
+
.split(',')
|
|
3847
|
+
.map(f => f.trim().replace(/^["']|["']$/g, ''))
|
|
3848
|
+
.find(
|
|
3849
|
+
f =>
|
|
3850
|
+
/^url\(/i.test(f) ||
|
|
3851
|
+
/:\/\//.test(f) ||
|
|
3852
|
+
/[\\/]/.test(f) ||
|
|
3853
|
+
/\.(woff2?|ttf|otf|eot)$/i.test(f),
|
|
3854
|
+
);
|
|
3855
|
+
const colon = String(value)
|
|
3856
|
+
.split(',')
|
|
3857
|
+
.map(f => f.trim().replace(/^["']|["']$/g, ''))
|
|
3858
|
+
.find(f => f.includes(':'));
|
|
3859
|
+
if (!hit && colon)
|
|
3860
|
+
return `"${colon}" holds a colon, so it is a pasted declaration or a link, not a font family name. Give the family alone, such as Inter, sans-serif.`;
|
|
3861
|
+
return hit
|
|
3862
|
+
? `"${hit}" is a font file or a link, not a font family name. A theme names a family (such as Inter); load the file with @font-face in your app's CSS, or install a font package, then set the family it defines.`
|
|
3863
|
+
: null;
|
|
3864
|
+
}
|
|
3865
|
+
|
|
3866
|
+
/**
|
|
3867
|
+
* A value with every Unicode space (no-break, thin, ideographic…) as a plain
|
|
3868
|
+
* space and zero-width and format characters dropped, for every value type:
|
|
3869
|
+
* the browser reads only a plain space as one.
|
|
3870
|
+
* @param {string} v
|
|
3871
|
+
*/
|
|
3872
|
+
export function normalValueText(v) {
|
|
3873
|
+
// Curly quotes (a word processor's, a phone's) are the straight ones CSS
|
|
3874
|
+
// reads.
|
|
3875
|
+
const straight = v
|
|
3876
|
+
.replace(/[\u2018\u2019\u201A\u201B]/g, "'")
|
|
3877
|
+
.replace(/[\u201C\u201D\u201E\u201F]/g, '"');
|
|
3878
|
+
// A quoted CSS string (`content: "a\u00A0b"`) keeps what is inside it.
|
|
3879
|
+
return straight
|
|
3880
|
+
.split(/("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*')/)
|
|
3881
|
+
.map((part, i) =>
|
|
3882
|
+
i % 2
|
|
3883
|
+
? part
|
|
3884
|
+
: part
|
|
3885
|
+
.replace(/[\u200B-\u200D\u2060\uFEFF]/g, '')
|
|
3886
|
+
.replace(/[\u00A0\u1680\u2000-\u200A\u202F\u205F\u3000]/g, ' '),
|
|
3887
|
+
)
|
|
3888
|
+
.join('');
|
|
3889
|
+
}
|
|
3890
|
+
|
|
3891
|
+
/**
|
|
3892
|
+
* The thing and value a setting was typed as, put right before any check: a
|
|
3893
|
+
* declaration pasted whole or split (`accent: #e11d48`, `--color-accent=red`,
|
|
3894
|
+
* `font:inter`, `accent:` with the value next), a token typed with an em or
|
|
3895
|
+
* en dash (`—color-accent`) or as `var(--color-accent)`, and the value's
|
|
3896
|
+
* Unicode spaces. A thing it cannot read is left as typed.
|
|
3897
|
+
* @param {string} thing
|
|
3898
|
+
* @param {string | undefined} value
|
|
3899
|
+
* @returns {{thing: string, value: string | undefined}}
|
|
3900
|
+
*/
|
|
3901
|
+
export function normalizeEntry(thing, value) {
|
|
3902
|
+
let t = normalValueText(String(thing ?? '')).trim();
|
|
3903
|
+
let v = value == null ? value : normalValueText(String(value));
|
|
3904
|
+
// A pasted declaration (the whole of it in the thing, or the thing with
|
|
3905
|
+
// its colon and the value next): its `;` and `!important` go with it.
|
|
3906
|
+
let pasted = false;
|
|
3907
|
+
if (v == null || v.trim() === '') {
|
|
3908
|
+
const m = /^(\S+?)\s*[:=]\s*(.+?)\s*;?\s*$/.exec(t);
|
|
3909
|
+
if (m && !/^https?$/i.test(m[1])) {
|
|
3910
|
+
t = m[1];
|
|
3911
|
+
v = m[2];
|
|
3912
|
+
pasted = true;
|
|
3913
|
+
}
|
|
3914
|
+
}
|
|
3915
|
+
// A thing ending in its colon or equals sign, the value given next.
|
|
3916
|
+
if (/\s*[:=]+$/.test(t)) {
|
|
3917
|
+
t = t.replace(/\s*[:=]+$/, '');
|
|
3918
|
+
pasted = true;
|
|
3919
|
+
}
|
|
3920
|
+
// Dash-like characters where a token's two dashes go (an em or en dash,
|
|
3921
|
+
// a non-breaking hyphen, a minus sign, a full-width hyphen, alone or
|
|
3922
|
+
// mixed with hyphens): `--`.
|
|
3923
|
+
t = t.replace(/^[-\u2010-\u2015\u2212\uFE58\uFE63\uFF0D]+/, run =>
|
|
3924
|
+
/[^-]/.test(run) ? '--' : run,
|
|
3925
|
+
);
|
|
3926
|
+
// A token typed as a var() reference.
|
|
3927
|
+
const ref = /^var\(\s*(--[a-z0-9-]+)\s*\)$/i.exec(t);
|
|
3928
|
+
if (ref) t = ref[1];
|
|
3929
|
+
if (pasted && typeof v === 'string')
|
|
3930
|
+
v = v.replace(/\s*!\s*important\s*;?\s*$/i, '').replace(/;+\s*$/, '');
|
|
3931
|
+
return {thing: t, value: v};
|
|
3932
|
+
}
|
|
3933
|
+
|
|
3934
|
+
/**
|
|
3935
|
+
* A word as a suggested command quotes it, without the quotes.
|
|
3936
|
+
* @param {string} word
|
|
3937
|
+
*/
|
|
3938
|
+
function unquoteArg(word) {
|
|
3939
|
+
const m = /^'(.*)'$/s.exec(word.trim());
|
|
3940
|
+
return m ? m[1].replace(/'\\''/g, "'") : word.trim();
|
|
3941
|
+
}
|