@mccaskie-labs/workbench-ui 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +12 -0
- package/dist/cli.d.ts +30 -1
- package/dist/cli.js +117 -11
- package/dist/fonts.d.ts +42 -0
- package/dist/fonts.js +52 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.js +4 -1
- package/dist/layouts.d.ts +60 -0
- package/dist/layouts.js +143 -0
- package/dist/samples.d.ts +30 -0
- package/dist/samples.js +65 -0
- package/dist/style-guide.d.ts +19 -0
- package/dist/style-guide.js +41 -0
- package/dist/styles.d.ts +4 -1
- package/dist/styles.js +7 -1
- package/dist/theme.d.ts +31 -0
- package/dist/theme.js +128 -5
- package/dist/ui-samples.d.ts +2 -0
- package/dist/ui-samples.js +71 -0
- package/dist/ui-styles.js +20 -0
- package/fonts/archivo/latin-400-700.woff2 +0 -0
- package/fonts/archivo/latin-ext-400-700.woff2 +0 -0
- package/fonts/baloo-2/latin-400-700.woff2 +0 -0
- package/fonts/baloo-2/latin-ext-400-700.woff2 +0 -0
- package/fonts/barlow/latin-400.woff2 +0 -0
- package/fonts/barlow/latin-700.woff2 +0 -0
- package/fonts/barlow/latin-ext-400.woff2 +0 -0
- package/fonts/barlow/latin-ext-700.woff2 +0 -0
- package/fonts/barlow-condensed/latin-400.woff2 +0 -0
- package/fonts/barlow-condensed/latin-700.woff2 +0 -0
- package/fonts/barlow-condensed/latin-ext-400.woff2 +0 -0
- package/fonts/barlow-condensed/latin-ext-700.woff2 +0 -0
- package/fonts/fonts.json +298 -0
- package/fonts/fraunces/latin-400-700.woff2 +0 -0
- package/fonts/fraunces/latin-ext-400-700.woff2 +0 -0
- package/fonts/inter/latin-400-700.woff2 +0 -0
- package/fonts/inter/latin-ext-400-700.woff2 +0 -0
- package/fonts/nunito/latin-400-700.woff2 +0 -0
- package/fonts/nunito/latin-ext-400-700.woff2 +0 -0
- package/fonts/nunito-sans/latin-400-700.woff2 +0 -0
- package/fonts/nunito-sans/latin-ext-400-700.woff2 +0 -0
- package/fonts/source-sans-3/latin-400-700.woff2 +0 -0
- package/fonts/source-sans-3/latin-ext-400-700.woff2 +0 -0
- package/fonts/work-sans/latin-400-700.woff2 +0 -0
- package/fonts/work-sans/latin-ext-400-700.woff2 +0 -0
- package/package.json +8 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @mccaskie-labs/workbench-ui
|
|
2
2
|
|
|
3
|
+
## 0.13.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- Tool layouts and the layout kit (docs/look-studio.md step 6, workbench-ui part). A tool's `ToolStyles` may carry `layouts` (`ToolLayouts`: the outer element's class, the default, and up to seven `ToolLayout`s, each with an `id`, `title`, `bestFor`, optional `needs` and `render`, and `rules`). `npx workbench-ui styles` draws each layout's rules under `.<root>[data-layout="<id>"]` inside `@media (min-width: 600px)`, so a phone always gets the stacked one. `styleProblems` refuses a layout that sets anything but where things sit (no colour, font, corner, `order` or reversed direction), names something other than the tool's own parts (no ids or attributes, so a layout arranges kinds of part and never one named item), or a default it doesn't have; `sampleProblems` needs a sample of every layout. `LAYOUT_KIT` (`stack`, `columns`, `split`, `cards`, `strip`, `full`, `steps`) gives every tool the same patterns, and `chooseLayout(layouts, asked, projectDefault)` picks the one to draw.
|
|
8
|
+
- Hosted fonts (docs/look-studio.md step 4). The ten families the library's pairings name (Archivo, Baloo 2, Barlow, Barlow Condensed, Fraunces, Inter, Nunito, Nunito Sans, Source Sans 3, Work Sans) ship in the package's `fonts/` folder, 0.8 MB, Latin and Latin Extended at weights 400 and 700, each file's SIL Open Font License read from the file itself and its letters checked (Barlow and Barlow Condensed lack ₱, which shows in the fallback font). `npx workbench-ui styles` copies the families a project's look uses into its `public/fonts/` and writes their `@font-face` rules at the top of workbench.css, so no visitor's browser calls a font service; `--check` fails when a font file is missing or out of date. A font brand.json names that workbench-ui doesn't host is left to its fallback, as before. The publisher refreshes the set with `npm run fonts` in packages/ui. `styles` prints a note (never a failure) for a family that lacks a letter of the check, such as Barlow's ₱, and `fontGaps` lists them.
|
|
9
|
+
- Tool samples and the style guide (docs/look-studio.md step 3). A tool that draws now exports `./samples`: `ToolSamples` (`format`, `package`, `title`, `prefix`, `samples: [{ id, title, layout?, html() }]`), each sample drawn by the tool's own renderers with made-up data. `sampleProblems(styles, samples)` fails a class no sample draws (so a new element can't skip the style guide), a class a sample draws that the styles don't list, and a sample that throws or draws nothing; `classesIn(html)` lists an HTML's classes. workbench-ui's own page parts are sampled at `@mccaskie-labs/workbench-ui/samples` (`UI_SAMPLES`). `styleGuide(samples, { view?, words? })` draws the style guide page's body: every tool's samples, a jump list, and a switch between light, dark and the admin look (`guideView(url)` reads `?view=`), with every sample's form sent back to the guide by GET so nothing in it acts; new classes `wb-ui-guide`, `-guide-views`, `-guide-view`, `-guide-index`, `-guide-jump`, `-guide-tool`, `-guide-tool-title`, `-guide-sample`, `-guide-caption`, `-guide-title`, `-guide-layout`, `-guide-frame` and `-guide-empty`. `npx workbench-ui styles` also writes `src/samples.generated.ts` (`SAMPLES`, every installed tool's samples) and `public/styles/style-guide.css` (the project's look in light and dark under `:root[data-wb-scheme]`, `renderSchemes`), and `--check` fails when either is out of date; generated files (`*.generated.*`) are no longer scanned as the project's own markup.
|
|
10
|
+
- The accessibility and contrast fixer (docs/look-studio.md step 2). `nearestPassing(colour, against, ratio)` gives the nearest colour that reads, keeping the hue and moving lightness as little as it takes (saturation only when lightness can't get there). `themeFixes(theme)` suggests one fix per colour for every pair that doesn't read, changing the colour that is set (the accent, not the text worked out to sit on it); `fixPalette(palette, scheme)` fixes a whole palette, for an AI proposal before the unchanged check. The keyboard focus ring `--wb-focus` is now a solid colour that stands out from the page by 3:1 (the accent where it does, else its nearest that does), instead of the accent at 40%, and `themeProblems` checks it, and refuses base text under 16px. `npx workbench-ui styles --check` prints each fix with brand.json's own colours ("your colours.primary #9cc596 is too faint for a link on the page. Nearest that reads: …"), and `--fix` writes them into brand.json, changing nothing else; it also names project.css rules that take the underline off plain links. Every project's workbench.css changes in one line, its `--wb-focus`.
|
|
11
|
+
|
|
3
12
|
## 0.12.0
|
|
4
13
|
|
|
5
14
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -94,3 +94,15 @@ A project's look comes from the library (`docs/look-studio.md` §4): palettes, f
|
|
|
94
94
|
| `withProjectItems(items)` | The packaged library plus a project's own items |
|
|
95
95
|
|
|
96
96
|
A project adds its own items in `workbench/looks/*.json`, the same shape with `"since": "project"`. `npx workbench-ui styles` reads them, so brand.json can name them, and `--check` names any that is bad, clashes with a packaged id or doesn't read.
|
|
97
|
+
|
|
98
|
+
### The fixer
|
|
99
|
+
|
|
100
|
+
`nearestPassing(colour, against, ratio)`, `themeFixes(theme)` and `fixPalette(palette, scheme)` give the nearest colour that reads, same hue. `npx workbench-ui styles --check` prints the fix beside each of brand.json's colours that doesn't read; `npx workbench-ui styles --fix` takes them. The focus ring is a solid colour at 3:1 or more, base text is at least 16px, and project.css may not take the underline off plain links.
|
|
101
|
+
|
|
102
|
+
## Samples and the style guide
|
|
103
|
+
|
|
104
|
+
Every tool that draws exports `./samples` (`ToolSamples`), drawn by its own renderers; `sampleProblems(styles, samples)` in each tool's tests fails a class no sample draws. `npx workbench-ui styles` writes `src/samples.generated.ts` (`SAMPLES`) and `public/styles/style-guide.css`; the `/admin/style-guide` page (a workbench-site recipe) draws `styleGuide(SAMPLES, { view: guideView(request.url) })`, with `data-wb-scheme="dark"` on `<html>` for the dark view and `wb-ui-admin` for the admin one.
|
|
105
|
+
|
|
106
|
+
## Hosted fonts
|
|
107
|
+
|
|
108
|
+
`fonts/` holds every family the library's pairings name (open licence only, `fonts/fonts.json` lists each file, its licence and its letters). `npx workbench-ui styles` copies the families a project uses into `public/fonts/` and writes their `@font-face` rules; nothing calls Google. `npm run fonts` (publisher only) fetches them again.
|
package/dist/cli.d.ts
CHANGED
|
@@ -6,6 +6,15 @@ export declare function installedStyles(dir: string): Promise<{
|
|
|
6
6
|
tools: InstalledStyles[];
|
|
7
7
|
uiVersion: string;
|
|
8
8
|
}>;
|
|
9
|
+
/** Where the generated list of every installed tool's samples is written, and the style guide's sheet (docs/look-studio.md §5.1, §9.5). */
|
|
10
|
+
export declare const SAMPLE_FILES: {
|
|
11
|
+
readonly module: "src/samples.generated.ts";
|
|
12
|
+
readonly guide: "public/styles/style-guide.css";
|
|
13
|
+
};
|
|
14
|
+
/** Every Workbench package the project at `dir` depends on whose package.json exports "./samples", sorted. */
|
|
15
|
+
export declare function samplePackages(dir: string): string[];
|
|
16
|
+
/** The generated samples module: one import per package, every ToolSamples it exports, in package order. */
|
|
17
|
+
export declare function samplesModule(packages: readonly string[]): string;
|
|
9
18
|
/** Where a project's own pages are written, from its root. */
|
|
10
19
|
export declare const MARKUP_DIRS: readonly string[];
|
|
11
20
|
/** The project's own source files that can hold markup: code and HTML under src/ and public/, not tests, builds or stylesheets. */
|
|
@@ -23,6 +32,15 @@ export declare function projectLibrary(dir: string): {
|
|
|
23
32
|
library: Library;
|
|
24
33
|
problems: string[];
|
|
25
34
|
};
|
|
35
|
+
/** A fix to one of brand.json's own colours. */
|
|
36
|
+
export interface BrandFix {
|
|
37
|
+
field: "primary" | "text" | "background" | "muted";
|
|
38
|
+
from: string;
|
|
39
|
+
to: string;
|
|
40
|
+
what: string;
|
|
41
|
+
}
|
|
42
|
+
/** brand.json's text with the fixes taken: only `colours.<field>` changes, everything else is kept as it was. */
|
|
43
|
+
export declare function applyBrandFixes(brandText: string, fixes: readonly BrandFix[]): string;
|
|
26
44
|
/**
|
|
27
45
|
* The theme block for the project at `dir`, from its brand.json (Clean, light,
|
|
28
46
|
* when it has none), and what is wrong with it: a field of the wrong kind, or
|
|
@@ -30,13 +48,24 @@ export declare function projectLibrary(dir: string): {
|
|
|
30
48
|
*/
|
|
31
49
|
export declare function projectTheme(dir: string): {
|
|
32
50
|
css: string;
|
|
51
|
+
guide: string;
|
|
33
52
|
problems: string[];
|
|
53
|
+
fixes: BrandFix[];
|
|
54
|
+
families: string[];
|
|
34
55
|
};
|
|
56
|
+
/**
|
|
57
|
+
* Rules in a project's own CSS that take the underline off plain links, so a
|
|
58
|
+
* link is told apart from text by colour alone (WCAG 1.4.1). A link styled as
|
|
59
|
+
* a button or pill carries a class, so only bare `a` selectors count.
|
|
60
|
+
*/
|
|
61
|
+
export declare function linkProblems(css: string): string[];
|
|
35
62
|
export interface StylesPlan {
|
|
36
63
|
/** Files to write, by path relative to the project. */
|
|
37
|
-
files: Record<string, string>;
|
|
64
|
+
files: Record<string, string | Uint8Array>;
|
|
38
65
|
/** Why --check fails. */
|
|
39
66
|
problems: string[];
|
|
67
|
+
/** Worth knowing, never a failure: letters the look's fonts lack. */
|
|
68
|
+
notes: string[];
|
|
40
69
|
}
|
|
41
70
|
/** What `styles` writes in `dir`, and what --check would fail on, without touching the disk. */
|
|
42
71
|
export declare function planStyles(dir: string, tools: readonly InstalledStyles[], uiVersion: string): StylesPlan;
|
package/dist/cli.js
CHANGED
|
@@ -23,10 +23,11 @@ import { createRequire } from "node:module";
|
|
|
23
23
|
import { dirname, join, relative, resolve } from "node:path";
|
|
24
24
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
|
25
25
|
import { UI_STYLES } from "./ui-styles.js";
|
|
26
|
-
import { libraryProblems, renderTheme, resolveAdminTheme, resolveTheme, themeInput, themeProblems } from "./theme.js";
|
|
26
|
+
import { libraryProblems, renderSchemes, renderTheme, resolveAdminTheme, resolveTheme, themeFixes, themeInput, themeProblems } from "./theme.js";
|
|
27
|
+
import { familiesIn, fontGaps, projectFonts } from "./fonts.js";
|
|
27
28
|
import { findItem, itemProblems, LIBRARY, withProjectItems } from "./library.js";
|
|
28
29
|
import { DEFAULT_THEME_CSS, renderStyles, STYLE_FILES, STYLED_SCOPE, starterProjectCss, unknownMarkupNames, unknownNames } from "./styles.js";
|
|
29
|
-
const USAGE = "usage: workbench-ui styles [--check] [dir]";
|
|
30
|
+
const USAGE = "usage: workbench-ui styles [--check | --fix] [dir]";
|
|
30
31
|
const read = (path) => readFileSync(path, "utf8").replace(/\r\n/g, "\n");
|
|
31
32
|
/** The styles of each Workbench tool the project at `dir` has installed, and the workbench-ui version. */
|
|
32
33
|
export async function installedStyles(dir) {
|
|
@@ -69,6 +70,40 @@ export async function installedStyles(dir) {
|
|
|
69
70
|
tools.sort((a, b) => (a.styles.package < b.styles.package ? -1 : a.styles.package > b.styles.package ? 1 : a.styles.prefix < b.styles.prefix ? -1 : 1));
|
|
70
71
|
return { tools, uiVersion };
|
|
71
72
|
}
|
|
73
|
+
/** Where the generated list of every installed tool's samples is written, and the style guide's sheet (docs/look-studio.md §5.1, §9.5). */
|
|
74
|
+
export const SAMPLE_FILES = { module: "src/samples.generated.ts", guide: "public/styles/style-guide.css" };
|
|
75
|
+
/** Every Workbench package the project at `dir` depends on whose package.json exports "./samples", sorted. */
|
|
76
|
+
export function samplePackages(dir) {
|
|
77
|
+
if (!existsSync(join(dir, "package.json")))
|
|
78
|
+
return [];
|
|
79
|
+
const req = createRequire(join(dir, "package.json"));
|
|
80
|
+
const project = JSON.parse(read(join(dir, "package.json")));
|
|
81
|
+
const names = [...new Set([...Object.keys(project.dependencies ?? {}), ...Object.keys(project.devDependencies ?? {})])].filter((n) => n.startsWith(STYLED_SCOPE)).sort();
|
|
82
|
+
return names.filter((name) => {
|
|
83
|
+
const at = (req.resolve.paths(name) ?? []).map((d) => join(d, name, "package.json")).find((f) => existsSync(f));
|
|
84
|
+
if (!at)
|
|
85
|
+
return false;
|
|
86
|
+
const manifest = JSON.parse(read(at));
|
|
87
|
+
return !!manifest.exports && Object.hasOwn(manifest.exports, "./samples");
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
/** The generated samples module: one import per package, every ToolSamples it exports, in package order. */
|
|
91
|
+
export function samplesModule(packages) {
|
|
92
|
+
const id = (n) => n.replace(/^@mccaskie-labs\/workbench-/, "").replace(/[^a-z0-9]/g, "_");
|
|
93
|
+
return [
|
|
94
|
+
"// Written by `npx workbench-ui styles`: every installed Workbench tool's samples, for the style guide page",
|
|
95
|
+
"// (docs/look-studio.md §5.1). Don't edit; `npx workbench-ui styles --check` fails when it is out of date.",
|
|
96
|
+
"",
|
|
97
|
+
'import type { ToolSamples } from "@mccaskie-labs/workbench-ui";',
|
|
98
|
+
...packages.map((p) => `import * as ${id(p)} from "${p}/samples";`),
|
|
99
|
+
"",
|
|
100
|
+
"const of = (m: object): ToolSamples[] =>",
|
|
101
|
+
" Object.values(m).filter((x): x is ToolSamples => !!x && typeof x === \"object\" && (x as ToolSamples).format === 1 && Array.isArray((x as ToolSamples).samples));",
|
|
102
|
+
"",
|
|
103
|
+
`export const SAMPLES: readonly ToolSamples[] = [${packages.map((p) => `...of(${id(p)})`).join(", ")}];`,
|
|
104
|
+
"",
|
|
105
|
+
].join("\n");
|
|
106
|
+
}
|
|
72
107
|
/** Where a project's own pages are written, from its root. */
|
|
73
108
|
export const MARKUP_DIRS = ["src", "public"];
|
|
74
109
|
const MARKUP = /\.(?:[cm]?[jt]sx?|html?)$/;
|
|
@@ -82,7 +117,7 @@ export function markupFiles(dir) {
|
|
|
82
117
|
if (!NOT_MARKUP.has(e.name) && !e.name.startsWith("."))
|
|
83
118
|
walk(join(at, e.name));
|
|
84
119
|
}
|
|
85
|
-
else if (e.isFile() && MARKUP.test(e.name) && !/\.(?:test|spec)\./.test(e.name) && !e.name.endsWith(".d.ts"))
|
|
120
|
+
else if (e.isFile() && MARKUP.test(e.name) && !/\.(?:test|spec|generated)\./.test(e.name) && !e.name.endsWith(".d.ts"))
|
|
86
121
|
out.push(join(at, e.name));
|
|
87
122
|
}
|
|
88
123
|
};
|
|
@@ -136,6 +171,25 @@ export function projectLibrary(dir) {
|
|
|
136
171
|
problems.push(...libraryProblems(library).map((p) => `${PROJECT_LOOKS_DIR}: ${p}`));
|
|
137
172
|
return { library, problems };
|
|
138
173
|
}
|
|
174
|
+
/** The brand.json colour behind each token a fix can change. */
|
|
175
|
+
const BRAND_FIELD = {
|
|
176
|
+
"--wb-accent": "primary",
|
|
177
|
+
"--wb-text": "text",
|
|
178
|
+
"--wb-background": "background",
|
|
179
|
+
"--wb-muted": "muted",
|
|
180
|
+
};
|
|
181
|
+
const brandFixes = (fixes) => fixes.flatMap((f) => {
|
|
182
|
+
const field = BRAND_FIELD[f.token];
|
|
183
|
+
return field ? [{ field, from: f.from, to: f.to, what: f.what }] : [];
|
|
184
|
+
});
|
|
185
|
+
/** brand.json's text with the fixes taken: only `colours.<field>` changes, everything else is kept as it was. */
|
|
186
|
+
export function applyBrandFixes(brandText, fixes) {
|
|
187
|
+
const brand = JSON.parse(brandText);
|
|
188
|
+
brand.colours = { ...brand.colours };
|
|
189
|
+
for (const f of fixes)
|
|
190
|
+
brand.colours[f.field] = f.to;
|
|
191
|
+
return `${JSON.stringify(brand, null, 2)}\n`;
|
|
192
|
+
}
|
|
139
193
|
/**
|
|
140
194
|
* The theme block for the project at `dir`, from its brand.json (Clean, light,
|
|
141
195
|
* when it has none), and what is wrong with it: a field of the wrong kind, or
|
|
@@ -143,14 +197,15 @@ export function projectLibrary(dir) {
|
|
|
143
197
|
*/
|
|
144
198
|
export function projectTheme(dir) {
|
|
145
199
|
const at = join(dir, BRAND_FILE);
|
|
200
|
+
const plain = () => familiesIn([resolveTheme().tokens["--wb-font-heading"], resolveTheme().tokens["--wb-font"]]);
|
|
146
201
|
if (!existsSync(at))
|
|
147
|
-
return { css: DEFAULT_THEME_CSS, problems: projectLibrary(dir).problems };
|
|
202
|
+
return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: projectLibrary(dir).problems, fixes: [], families: plain() };
|
|
148
203
|
let brand;
|
|
149
204
|
try {
|
|
150
205
|
brand = JSON.parse(read(at));
|
|
151
206
|
}
|
|
152
207
|
catch {
|
|
153
|
-
return { css: DEFAULT_THEME_CSS, problems: [`${BRAND_FILE} isn't valid JSON`] };
|
|
208
|
+
return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: [`${BRAND_FILE} isn't valid JSON`], fixes: [], families: plain() };
|
|
154
209
|
}
|
|
155
210
|
const own = projectLibrary(dir);
|
|
156
211
|
const { input, problems } = themeInput(brand, own.library);
|
|
@@ -158,7 +213,28 @@ export function projectTheme(dir) {
|
|
|
158
213
|
const site = resolveTheme(input, own.library);
|
|
159
214
|
const look = `${input.look ?? "clean"}, ${input.scheme ?? "light"}${input.colours?.primary || input.fonts || input.shape || input.palette || input.pairing ? ", with brand.json's own choices" : ""}`;
|
|
160
215
|
problems.push(...themeProblems(site).map((p) => `${BRAND_FILE}: ${p}`));
|
|
161
|
-
|
|
216
|
+
const fixes = brandFixes(themeFixes(site));
|
|
217
|
+
for (const f of fixes)
|
|
218
|
+
problems.push(`${BRAND_FILE}: your colours.${f.field} ${f.from} is too faint for ${f.what}. Nearest that reads: ${f.to} (\`npx workbench-ui styles --fix\` takes it)`);
|
|
219
|
+
for (const f of themeFixes(site).filter((x) => !BRAND_FIELD[x.token]))
|
|
220
|
+
problems.push(`${BRAND_FILE}: the look's ${f.token} ${f.from} doesn't read for ${f.what}: choose another look or palette`);
|
|
221
|
+
return { css: renderTheme(site, resolveAdminTheme(input, own.library), `${look} (from ${BRAND_FILE})`), guide: renderSchemes(input, own.library), problems, fixes, families: familiesIn([site.tokens["--wb-font-heading"], site.tokens["--wb-font"]]) };
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Rules in a project's own CSS that take the underline off plain links, so a
|
|
225
|
+
* link is told apart from text by colour alone (WCAG 1.4.1). A link styled as
|
|
226
|
+
* a button or pill carries a class, so only bare `a` selectors count.
|
|
227
|
+
*/
|
|
228
|
+
export function linkProblems(css) {
|
|
229
|
+
const out = [];
|
|
230
|
+
const text = css.replace(/\/\*[\s\S]*?\*\//g, "");
|
|
231
|
+
for (const m of text.matchAll(/([^{}]+)\{([^{}]*)\}/g)) {
|
|
232
|
+
const selectors = m[1].split(",").map((x) => x.trim());
|
|
233
|
+
const bare = selectors.filter((x) => /(^|[\s>+~])a(:link|:visited|:any-link)?$/.test(x));
|
|
234
|
+
if (bare.length && /text-decoration(-line)?\s*:\s*none/.test(m[2]))
|
|
235
|
+
out.push(`${bare.join(", ")} takes the underline off links, so they differ from text by colour alone: keep an underline (or another mark) on plain links`);
|
|
236
|
+
}
|
|
237
|
+
return out;
|
|
162
238
|
}
|
|
163
239
|
/** What `styles` writes in `dir`, and what --check would fail on, without touching the disk. */
|
|
164
240
|
export function planStyles(dir, tools, uiVersion) {
|
|
@@ -166,7 +242,22 @@ export function planStyles(dir, tools, uiVersion) {
|
|
|
166
242
|
const problems = [];
|
|
167
243
|
const theme = projectTheme(dir);
|
|
168
244
|
problems.push(...theme.problems);
|
|
169
|
-
const
|
|
245
|
+
const fonts = projectFonts(theme.families);
|
|
246
|
+
const generated = renderStyles(tools, uiVersion, theme.css, fonts.css);
|
|
247
|
+
for (const f of fonts.files) {
|
|
248
|
+
const bytes = readFileSync(f.from);
|
|
249
|
+
if (existsSync(join(dir, f.path)) && readFileSync(join(dir, f.path)).equals(bytes))
|
|
250
|
+
continue;
|
|
251
|
+
files[f.path] = bytes;
|
|
252
|
+
problems.push(`${f.path} is missing or out of date: the look's fonts are hosted by the project`);
|
|
253
|
+
}
|
|
254
|
+
const derived = { [SAMPLE_FILES.module]: samplesModule(samplePackages(dir)), [SAMPLE_FILES.guide]: theme.guide };
|
|
255
|
+
for (const [path, text] of Object.entries(derived)) {
|
|
256
|
+
if (existsSync(join(dir, path)) && read(join(dir, path)) === text)
|
|
257
|
+
continue;
|
|
258
|
+
files[path] = text;
|
|
259
|
+
problems.push(`${path} is out of date with the installed Workbench packages`);
|
|
260
|
+
}
|
|
170
261
|
const genPath = join(dir, STYLE_FILES.generated);
|
|
171
262
|
if (!existsSync(genPath) || read(genPath) !== generated) {
|
|
172
263
|
files[STYLE_FILES.generated] = generated;
|
|
@@ -178,6 +269,7 @@ export function planStyles(dir, tools, uiVersion) {
|
|
|
178
269
|
problems.push(`${STYLE_FILES.project} is missing`);
|
|
179
270
|
}
|
|
180
271
|
else {
|
|
272
|
+
problems.push(...linkProblems(read(projPath)).map((p) => `${STYLE_FILES.project}: ${p}`));
|
|
181
273
|
const unknown = unknownNames(read(projPath), tools);
|
|
182
274
|
if (unknown.length)
|
|
183
275
|
problems.push(`${STYLE_FILES.project} names ${unknown.join(", ")}, which no installed Workbench tool draws (renamed or removed: see workbench.css)`);
|
|
@@ -188,7 +280,7 @@ export function planStyles(dir, tools, uiVersion) {
|
|
|
188
280
|
if (unknown.length)
|
|
189
281
|
problems.push(`${at} puts ${unknown.join(", ")} on an element, which no installed Workbench tool draws (renamed or removed: see workbench.css)`);
|
|
190
282
|
}
|
|
191
|
-
return { files, problems };
|
|
283
|
+
return { files, problems, notes: fontGaps(theme.families) };
|
|
192
284
|
}
|
|
193
285
|
async function main(argv) {
|
|
194
286
|
const [command, ...rest] = argv;
|
|
@@ -197,14 +289,28 @@ async function main(argv) {
|
|
|
197
289
|
return 2;
|
|
198
290
|
}
|
|
199
291
|
const check = rest.includes("--check");
|
|
200
|
-
const
|
|
201
|
-
|
|
292
|
+
const fix = rest.includes("--fix");
|
|
293
|
+
const dirs = rest.filter((a) => a !== "--check" && a !== "--fix");
|
|
294
|
+
if ((check && fix) || dirs.length > 1 || dirs.some((d) => d.startsWith("-"))) {
|
|
202
295
|
console.error(USAGE);
|
|
203
296
|
return 2;
|
|
204
297
|
}
|
|
205
298
|
const dir = resolve(dirs[0] ?? ".");
|
|
206
299
|
const { tools, uiVersion } = await installedStyles(dir);
|
|
300
|
+
if (fix) {
|
|
301
|
+
// Takes the nearest colour that reads for each of brand.json's own colours that doesn't, then writes the styles as usual.
|
|
302
|
+
const { fixes } = projectTheme(dir);
|
|
303
|
+
if (fixes.length) {
|
|
304
|
+
writeFileSync(join(dir, BRAND_FILE), applyBrandFixes(read(join(dir, BRAND_FILE)), fixes));
|
|
305
|
+
for (const f of fixes)
|
|
306
|
+
console.log(`${BRAND_FILE}: colours.${f.field} ${f.from} -> ${f.to} (${f.what})`);
|
|
307
|
+
}
|
|
308
|
+
else
|
|
309
|
+
console.log(`${BRAND_FILE}: nothing to fix`);
|
|
310
|
+
}
|
|
207
311
|
const plan = planStyles(dir, tools, uiVersion);
|
|
312
|
+
for (const n of plan.notes)
|
|
313
|
+
console.log(`Note: ${n}`);
|
|
208
314
|
if (check) {
|
|
209
315
|
if (!plan.problems.length) {
|
|
210
316
|
console.log(`${STYLE_FILES.generated} is current (${tools.map((t) => `${t.styles.package} ${t.version}`).join(", ") || "no styled tools"})`);
|
|
@@ -222,7 +328,7 @@ async function main(argv) {
|
|
|
222
328
|
}
|
|
223
329
|
// Writing fixes workbench.css; project.css and the project's own pages are fixed by hand.
|
|
224
330
|
// brand.json's problems are fixed by hand too.
|
|
225
|
-
const left = plan.problems.filter((p) => !p.startsWith(STYLE_FILES.generated) && !(p.startsWith(STYLE_FILES.project) && plan.files[STYLE_FILES.project]));
|
|
331
|
+
const left = plan.problems.filter((p) => !p.startsWith(STYLE_FILES.generated) && !Object.values(SAMPLE_FILES).some((f) => p.startsWith(f)) && !p.startsWith("public/fonts/") && !(p.startsWith(STYLE_FILES.project) && plan.files[STYLE_FILES.project]));
|
|
226
332
|
for (const p of left)
|
|
227
333
|
console.error(p);
|
|
228
334
|
return left.length ? 1 : 0;
|
package/dist/fonts.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/** One hosted file: a subset of one family at a weight or a weight range. */
|
|
2
|
+
interface FontFile {
|
|
3
|
+
file: string;
|
|
4
|
+
subset: string;
|
|
5
|
+
/** "400", or "400 700" for a variable file. */
|
|
6
|
+
weight: string;
|
|
7
|
+
unicodeRange: string;
|
|
8
|
+
bytes: number;
|
|
9
|
+
sha256: string;
|
|
10
|
+
}
|
|
11
|
+
/** One hosted family, as fonts/fonts.json lists it. */
|
|
12
|
+
export interface HostedFamily {
|
|
13
|
+
family: string;
|
|
14
|
+
id: string;
|
|
15
|
+
licence: string;
|
|
16
|
+
licenceUrl: string;
|
|
17
|
+
from: string;
|
|
18
|
+
/** Characters of the letters check its files lack, shown in the fallback font. */
|
|
19
|
+
missing: string[];
|
|
20
|
+
files: FontFile[];
|
|
21
|
+
}
|
|
22
|
+
/** Where the package keeps its fonts: fonts/ beside dist/ and src/. */
|
|
23
|
+
export declare const FONTS_DIR: string;
|
|
24
|
+
/** Every family workbench-ui hosts. */
|
|
25
|
+
export declare function hostedFamilies(dir?: string): HostedFamily[];
|
|
26
|
+
/** The families a font stack names first (`"Inter",system-ui,…` is Inter), in order, once each. */
|
|
27
|
+
export declare function familiesIn(stacks: readonly string[]): string[];
|
|
28
|
+
/**
|
|
29
|
+
* The @font-face rules and files for the families a project uses that
|
|
30
|
+
* workbench-ui hosts. A family it doesn't host (an owner's own) is left to its
|
|
31
|
+
* fallback, as before.
|
|
32
|
+
*/
|
|
33
|
+
export declare function projectFonts(families: readonly string[], hosted?: readonly HostedFamily[]): {
|
|
34
|
+
css: string;
|
|
35
|
+
files: {
|
|
36
|
+
path: string;
|
|
37
|
+
from: string;
|
|
38
|
+
}[];
|
|
39
|
+
};
|
|
40
|
+
/** For each family a project uses that lacks letters of the letters check, one line saying which show in the fallback font. */
|
|
41
|
+
export declare function fontGaps(families: readonly string[], hosted?: readonly HostedFamily[]): string[];
|
|
42
|
+
export {};
|
package/dist/fonts.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
// Hosted fonts (docs/look-studio.md §5.3, Robert, 24 November 2026). The
|
|
2
|
+
// families the library's pairings name are kept in workbench-ui's fonts/
|
|
3
|
+
// folder (open licence only, fetched once by `npm run fonts`), and
|
|
4
|
+
// `npx workbench-ui styles` copies the ones a project uses into its own
|
|
5
|
+
// public/fonts/ with their @font-face rules, so no visitor's browser ever
|
|
6
|
+
// calls Google. Node only: read by the styles command, never by a Worker.
|
|
7
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
8
|
+
import { dirname, join, resolve } from "node:path";
|
|
9
|
+
import { fileURLToPath } from "node:url";
|
|
10
|
+
/** Where the package keeps its fonts: fonts/ beside dist/ and src/. */
|
|
11
|
+
export const FONTS_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../fonts");
|
|
12
|
+
/** Where a project's copies go, from its root, and the address they are served at. */
|
|
13
|
+
const PROJECT_FONTS = { dir: "public/fonts", url: "/fonts" };
|
|
14
|
+
/** Every family workbench-ui hosts. */
|
|
15
|
+
export function hostedFamilies(dir = FONTS_DIR) {
|
|
16
|
+
const at = join(dir, "fonts.json");
|
|
17
|
+
return existsSync(at) ? JSON.parse(readFileSync(at, "utf8")) : [];
|
|
18
|
+
}
|
|
19
|
+
/** The families a font stack names first (`"Inter",system-ui,…` is Inter), in order, once each. */
|
|
20
|
+
export function familiesIn(stacks) {
|
|
21
|
+
const out = [];
|
|
22
|
+
for (const s of stacks) {
|
|
23
|
+
const m = /^\s*"([^"]+)"/.exec(s);
|
|
24
|
+
if (m && !out.includes(m[1]))
|
|
25
|
+
out.push(m[1]);
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
const q = (s) => `"${s.replace(/["\\]/g, "")}"`;
|
|
30
|
+
/**
|
|
31
|
+
* The @font-face rules and files for the families a project uses that
|
|
32
|
+
* workbench-ui hosts. A family it doesn't host (an owner's own) is left to its
|
|
33
|
+
* fallback, as before.
|
|
34
|
+
*/
|
|
35
|
+
export function projectFonts(families, hosted = hostedFamilies()) {
|
|
36
|
+
const used = families.map((f) => hosted.find((h) => h.family === f)).filter((h) => !!h);
|
|
37
|
+
if (!used.length)
|
|
38
|
+
return { css: "", files: [] };
|
|
39
|
+
const rules = used.flatMap((h) => h.files.map((f) => `@font-face { font-family: ${q(h.family)}; font-style: normal; font-weight: ${f.weight}; font-display: swap; src: url("${PROJECT_FONTS.url}/${h.id}/${f.file}") format("woff2"); unicode-range: ${f.unicodeRange}; }`));
|
|
40
|
+
const credits = used.map((h) => `${h.family} (${h.licence}, ${h.licenceUrl || h.from})`).join("; ");
|
|
41
|
+
return {
|
|
42
|
+
css: [`/* Fonts, hosted by this project in ${PROJECT_FONTS.dir}/ so no browser calls a font service: ${credits.replace(/\*\//g, "* /")}. */`, ...rules, ""].join("\n"),
|
|
43
|
+
files: used.flatMap((h) => h.files.map((f) => ({ path: `${PROJECT_FONTS.dir}/${h.id}/${f.file}`, from: join(FONTS_DIR, h.id, f.file) }))),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** For each family a project uses that lacks letters of the letters check, one line saying which show in the fallback font. */
|
|
47
|
+
export function fontGaps(families, hosted = hostedFamilies()) {
|
|
48
|
+
return families
|
|
49
|
+
.map((f) => hosted.find((h) => h.family === f))
|
|
50
|
+
.filter((h) => !!h && h.missing.length > 0)
|
|
51
|
+
.map((h) => `${h.family} has no ${h.missing.join(" ")}, so ${h.missing.length === 1 ? "it shows" : "they show"} in the fallback font`);
|
|
52
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -3,8 +3,11 @@ export { confirmed, renderConfirm, type ConfirmOptions } from "./confirm.ts";
|
|
|
3
3
|
export { BASE_CSS, CSS, STYLE, UI_CSS } from "./style.ts";
|
|
4
4
|
export { UI_STYLES } from "./ui-styles.ts";
|
|
5
5
|
export { FORM_SCRIPT } from "./form-script.ts";
|
|
6
|
+
export { chooseLayout, LAYOUT_BREAKPOINT, LAYOUT_KIT, layoutProblems, type LayoutRule, type ToolLayout, type ToolLayouts } from "./layouts.ts";
|
|
7
|
+
export { classesIn, sampleProblems, type ToolSample, type ToolSamples } from "./samples.ts";
|
|
8
|
+
export { guideView, styleGuide, type GuideView, type StyleGuideOptions } from "./style-guide.ts";
|
|
6
9
|
export { DEFAULT_THEME_CSS, renderStyles, STYLE_FILES, STYLED_SCOPE, styleNames, styleProblems, STYLESHEETS, starterProjectCss, unknownMarkupNames, unknownNames, type InstalledStyles, type StyleClass, type StyleState, type StyleVariable, type ToolStyles, } from "./styles.ts";
|
|
7
|
-
export { contrast, DEFAULT_LOOK, LOOKS, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, paletteDistance, renderTheme, resolveAdminTheme, resolveSiteLayout, resolveTheme, THEME_TOKENS, themeInput, themeProblems, type Look, type LookId, type Palette, type Scheme, type Shape, type Theme, type ThemeInput, type ThemeToken, type TokenName, } from "./theme.ts";
|
|
10
|
+
export { contrast, DEFAULT_LOOK, LOOKS, fixPalette, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, nearestPassing, paletteDistance, renderSchemes, renderTheme, resolveAdminTheme, resolveSiteLayout, resolveTheme, THEME_TOKENS, themeFixes, themeInput, themeProblems, type Look, type LookId, type Palette, type Scheme, type Shape, type Theme, type ThemeFix, type ThemeInput, type ThemeToken, type TokenName, } from "./theme.ts";
|
|
8
11
|
export { findItem, itemProblems, LIBRARY, LIBRARY_KINDS, libraryOf, SITE_LAYOUT_CHOICES, usable, withProjectItems, type FontFallback, type ItemStatus, type LayoutItem, type Library, type LibraryItem, type LibraryKind, type LookItem, type PairingFont, type PairingItem, type PaletteItem, type SiteLayoutChoice, } from "./library.ts";
|
|
9
12
|
/** Plain text made safe to put in HTML, in text or in a quoted attribute. */
|
|
10
13
|
export declare const escape: (value: string) => string;
|
package/dist/index.js
CHANGED
|
@@ -8,8 +8,11 @@ export { confirmed, renderConfirm } from "./confirm.js";
|
|
|
8
8
|
export { BASE_CSS, CSS, STYLE, UI_CSS } from "./style.js";
|
|
9
9
|
export { UI_STYLES } from "./ui-styles.js";
|
|
10
10
|
export { FORM_SCRIPT } from "./form-script.js";
|
|
11
|
+
export { chooseLayout, LAYOUT_BREAKPOINT, LAYOUT_KIT, layoutProblems } from "./layouts.js";
|
|
12
|
+
export { classesIn, sampleProblems } from "./samples.js";
|
|
13
|
+
export { guideView, styleGuide } from "./style-guide.js";
|
|
11
14
|
export { DEFAULT_THEME_CSS, renderStyles, STYLE_FILES, STYLED_SCOPE, styleNames, styleProblems, STYLESHEETS, starterProjectCss, unknownMarkupNames, unknownNames, } from "./styles.js";
|
|
12
|
-
export { contrast, DEFAULT_LOOK, LOOKS, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, paletteDistance, renderTheme, resolveAdminTheme, resolveSiteLayout, resolveTheme, THEME_TOKENS, themeInput, themeProblems, } from "./theme.js";
|
|
15
|
+
export { contrast, DEFAULT_LOOK, LOOKS, fixPalette, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, nearestPassing, paletteDistance, renderSchemes, renderTheme, resolveAdminTheme, resolveSiteLayout, resolveTheme, THEME_TOKENS, themeFixes, themeInput, themeProblems, } from "./theme.js";
|
|
13
16
|
export { findItem, itemProblems, LIBRARY, LIBRARY_KINDS, libraryOf, SITE_LAYOUT_CHOICES, usable, withProjectItems, } from "./library.js";
|
|
14
17
|
/** Plain text made safe to put in HTML, in text or in a quoted attribute. */
|
|
15
18
|
export const escape = (value) => value.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/** One rule of a layout: a selector under the tool's outer element ("" for the element itself) and its declarations. */
|
|
2
|
+
export interface LayoutRule {
|
|
3
|
+
selector: string;
|
|
4
|
+
css: string;
|
|
5
|
+
}
|
|
6
|
+
/** One way of laying out a tool. */
|
|
7
|
+
export interface ToolLayout {
|
|
8
|
+
id: string;
|
|
9
|
+
title: string;
|
|
10
|
+
/** One line an owner reads: what it is good for. */
|
|
11
|
+
bestFor: string;
|
|
12
|
+
/** What the site must have for it to make sense, such as "pictures". */
|
|
13
|
+
needs?: readonly string[];
|
|
14
|
+
/** Its rules, from 600px wide. None for the stacked default. */
|
|
15
|
+
rules: readonly LayoutRule[];
|
|
16
|
+
/** Drawn by a render option of the tool, not by CSS alone (a form asking one question at a time). */
|
|
17
|
+
render?: boolean;
|
|
18
|
+
}
|
|
19
|
+
/** A tool's layouts: the outer element they hang on and the default. */
|
|
20
|
+
export interface ToolLayouts {
|
|
21
|
+
/** The class of the tool's outer element, which carries data-layout. */
|
|
22
|
+
root: string;
|
|
23
|
+
/** The layout when nothing chooses one. */
|
|
24
|
+
default: string;
|
|
25
|
+
layouts: readonly ToolLayout[];
|
|
26
|
+
}
|
|
27
|
+
/** Widest a phone is: under this every layout is the stacked one. */
|
|
28
|
+
export declare const LAYOUT_BREAKPOINT = "600px";
|
|
29
|
+
/** The layout kit: rule lists a tool's layouts are built from. `at` is a selector under the tool's outer element. */
|
|
30
|
+
export declare const LAYOUT_KIT: {
|
|
31
|
+
/** One thing under another, the gap between: the default, and what every layout becomes on a phone. */
|
|
32
|
+
readonly stack: (at?: string) => LayoutRule[];
|
|
33
|
+
/** `n` equal columns, filled in reading order. */
|
|
34
|
+
readonly columns: (n: number, at?: string) => LayoutRule[];
|
|
35
|
+
/** Two unequal columns: a narrow one (`narrow` of the width) and the rest. */
|
|
36
|
+
readonly split: (narrow?: string, wide?: string, at?: string) => LayoutRule[];
|
|
37
|
+
/** As many cards of at least `min` wide as fit. */
|
|
38
|
+
readonly cards: (min?: string, at?: string) => LayoutRule[];
|
|
39
|
+
/** Everything in one row that wraps, such as a one-line sign-up. */
|
|
40
|
+
readonly strip: (at?: string) => LayoutRule[];
|
|
41
|
+
/** A part across every column of the grid it is in. */
|
|
42
|
+
readonly full: (at: string) => LayoutRule[];
|
|
43
|
+
/** Steps one after another down the page, each set apart: a long form in sections. */
|
|
44
|
+
readonly steps: (at: string) => LayoutRule[];
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* What is wrong with a tool's layouts, in plain words: an unknown default, two
|
|
48
|
+
* with one id, a rule that sets anything but where things sit, a value that
|
|
49
|
+
* reverses the reading order, a selector that names something other than the
|
|
50
|
+
* tool's own parts, or a layout with no words for the owner.
|
|
51
|
+
*/
|
|
52
|
+
export declare function layoutProblems(tool: string, prefix: string, classes: ReadonlySet<string>, t: ToolLayouts): string[];
|
|
53
|
+
/** A tool's layout rules for the generated stylesheet, each under its data-layout, inside one wide-screen media query. */
|
|
54
|
+
export declare function renderLayouts(t: ToolLayouts, comment: (s: string) => string, decls: (css: string, indent: string) => string): string[];
|
|
55
|
+
/**
|
|
56
|
+
* The layout a tool draws in: the one asked for, if the tool has it, else the
|
|
57
|
+
* project's default for the tool, else the tool's own. The one resolver of §8
|
|
58
|
+
* grows from this in design mode.
|
|
59
|
+
*/
|
|
60
|
+
export declare function chooseLayout(t: ToolLayouts, asked?: string | null, projectDefault?: string | null): string;
|
package/dist/layouts.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// Tool layouts (docs/look-studio.md §7, Robert, 24 November 2026): several
|
|
2
|
+
// ways to lay out one tool's parts, distinct from the site's layout and from
|
|
3
|
+
// the look. A layout is data in the tool's ToolStyles: rules that only say
|
|
4
|
+
// where things sit (grid, flex, gaps, widths, alignment), drawn under the
|
|
5
|
+
// tool's outer element with data-layout="<id>" and only from 600px wide, so on
|
|
6
|
+
// a phone every layout is the stacked one. A layout arranges content and never
|
|
7
|
+
// owns it: it addresses kinds of part (a field, the send button), never one
|
|
8
|
+
// named item, so whatever the project adds flows into it.
|
|
9
|
+
//
|
|
10
|
+
// The kit below gives every tool the same few patterns (stack, columns, split,
|
|
11
|
+
// cards, strip, steps), so "two columns" behaves the same in a form and an
|
|
12
|
+
// order page.
|
|
13
|
+
/** Widest a phone is: under this every layout is the stacked one. */
|
|
14
|
+
export const LAYOUT_BREAKPOINT = "600px";
|
|
15
|
+
const GAP = "var(--wb-space, 1rem)";
|
|
16
|
+
/** The layout kit: rule lists a tool's layouts are built from. `at` is a selector under the tool's outer element. */
|
|
17
|
+
export const LAYOUT_KIT = {
|
|
18
|
+
/** One thing under another, the gap between: the default, and what every layout becomes on a phone. */
|
|
19
|
+
stack: (at = "") => [{ selector: at, css: `display:grid;grid-template-columns:minmax(0,1fr);gap:${GAP}` }],
|
|
20
|
+
/** `n` equal columns, filled in reading order. */
|
|
21
|
+
columns: (n, at = "") => [{ selector: at, css: `display:grid;grid-template-columns:repeat(${n},minmax(0,1fr));gap:${GAP}` }],
|
|
22
|
+
/** Two unequal columns: a narrow one (`narrow` of the width) and the rest. */
|
|
23
|
+
split: (narrow = "1fr", wide = "2fr", at = "") => [{ selector: at, css: `display:grid;grid-template-columns:minmax(0,${narrow}) minmax(0,${wide});gap:${GAP};align-items:start` }],
|
|
24
|
+
/** As many cards of at least `min` wide as fit. */
|
|
25
|
+
cards: (min = "14rem", at = "") => [{ selector: at, css: `display:grid;grid-template-columns:repeat(auto-fill,minmax(min(100%,${min}),1fr));gap:${GAP}` }],
|
|
26
|
+
/** Everything in one row that wraps, such as a one-line sign-up. */
|
|
27
|
+
strip: (at = "") => [{ selector: at, css: `display:flex;flex-wrap:wrap;align-items:end;gap:${GAP}` }],
|
|
28
|
+
/** A part across every column of the grid it is in. */
|
|
29
|
+
full: (at) => [{ selector: at, css: "grid-column:1/-1" }],
|
|
30
|
+
/** Steps one after another down the page, each set apart: a long form in sections. */
|
|
31
|
+
steps: (at) => [{ selector: at, css: `display:grid;gap:calc(${GAP} * 1.5)` }],
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* The properties a layout may set: where things sit and how far apart, never
|
|
35
|
+
* a colour, font, corner or anything that reorders what is read (no `order`,
|
|
36
|
+
* no reversed directions, no named grid areas).
|
|
37
|
+
*/
|
|
38
|
+
const LAYOUT_PROPERTIES = new Set([
|
|
39
|
+
"display",
|
|
40
|
+
"grid-template-columns",
|
|
41
|
+
"grid-auto-flow",
|
|
42
|
+
"grid-auto-rows",
|
|
43
|
+
"grid-column",
|
|
44
|
+
"gap",
|
|
45
|
+
"row-gap",
|
|
46
|
+
"column-gap",
|
|
47
|
+
"flex",
|
|
48
|
+
"flex-wrap",
|
|
49
|
+
"flex-basis",
|
|
50
|
+
"flex-grow",
|
|
51
|
+
"flex-shrink",
|
|
52
|
+
"flex-direction",
|
|
53
|
+
"align-items",
|
|
54
|
+
"align-self",
|
|
55
|
+
"align-content",
|
|
56
|
+
"justify-content",
|
|
57
|
+
"justify-items",
|
|
58
|
+
"justify-self",
|
|
59
|
+
"place-items",
|
|
60
|
+
"width",
|
|
61
|
+
"max-width",
|
|
62
|
+
"min-width",
|
|
63
|
+
"margin-inline",
|
|
64
|
+
"margin-block-start",
|
|
65
|
+
"margin-block-end",
|
|
66
|
+
"text-align",
|
|
67
|
+
]);
|
|
68
|
+
const LAYOUT_ID = /^[a-z][a-z0-9-]{1,39}$/;
|
|
69
|
+
/** The declarations of a rule's css, as [property, value] pairs. */
|
|
70
|
+
const declarationsOf = (css) => css
|
|
71
|
+
.split(";")
|
|
72
|
+
.map((d) => d.trim())
|
|
73
|
+
.filter(Boolean)
|
|
74
|
+
.map((d) => {
|
|
75
|
+
const i = d.indexOf(":");
|
|
76
|
+
return [d.slice(0, i).trim().toLowerCase(), d.slice(i + 1).trim()];
|
|
77
|
+
});
|
|
78
|
+
/**
|
|
79
|
+
* What is wrong with a tool's layouts, in plain words: an unknown default, two
|
|
80
|
+
* with one id, a rule that sets anything but where things sit, a value that
|
|
81
|
+
* reverses the reading order, a selector that names something other than the
|
|
82
|
+
* tool's own parts, or a layout with no words for the owner.
|
|
83
|
+
*/
|
|
84
|
+
export function layoutProblems(tool, prefix, classes, t) {
|
|
85
|
+
const out = [];
|
|
86
|
+
if (!classes.has(t.root))
|
|
87
|
+
out.push(`${tool}: layouts hang on ${t.root}, which isn't one of its classes`);
|
|
88
|
+
const ids = new Set();
|
|
89
|
+
for (const l of t.layouts) {
|
|
90
|
+
if (!LAYOUT_ID.test(l.id))
|
|
91
|
+
out.push(`${tool}: layout id ${l.id} must be lower case and hyphens`);
|
|
92
|
+
if (ids.has(l.id))
|
|
93
|
+
out.push(`${tool}: two layouts are called ${l.id}`);
|
|
94
|
+
ids.add(l.id);
|
|
95
|
+
if (!l.title.trim() || !l.bestFor.trim())
|
|
96
|
+
out.push(`${tool}: layout ${l.id} needs a title and what it is best for`);
|
|
97
|
+
for (const r of l.rules) {
|
|
98
|
+
// A selector may name the tool's own classes and plain structure (> * :first-child), nothing else.
|
|
99
|
+
for (const c of r.selector.matchAll(/\.([\w-]+)/g))
|
|
100
|
+
if (!c[1].startsWith(prefix) || !classes.has(c[1]))
|
|
101
|
+
out.push(`${tool}: layout ${l.id} names .${c[1]}, which isn't one of its parts`);
|
|
102
|
+
const rest = r.selector.replace(/\.[\w-]+/g, "").replace(/:(?:first-child|last-child|nth-child\([\dn+ -]+\)|not\()/g, "");
|
|
103
|
+
if (!/^[\s>*+~(),]*$/.test(rest))
|
|
104
|
+
out.push(`${tool}: layout ${l.id}'s selector "${r.selector}" may name only the tool's parts`);
|
|
105
|
+
for (const [prop, value] of declarationsOf(r.css)) {
|
|
106
|
+
if (!LAYOUT_PROPERTIES.has(prop))
|
|
107
|
+
out.push(`${tool}: layout ${l.id} sets ${prop}, which isn't layout: a layout only says where things sit`);
|
|
108
|
+
if (/reverse/.test(value) || prop === "order")
|
|
109
|
+
out.push(`${tool}: layout ${l.id} reverses the order things are read in`);
|
|
110
|
+
if (/#|rgb|hsl|url\(|!important/.test(value))
|
|
111
|
+
out.push(`${tool}: layout ${l.id} sets ${prop} to ${value}, which isn't layout`);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
if (!ids.has(t.default))
|
|
116
|
+
out.push(`${tool}: the default layout ${t.default} isn't one of its layouts`);
|
|
117
|
+
if (t.layouts.length > 7)
|
|
118
|
+
out.push(`${tool}: ${t.layouts.length} layouts is too many to choose from: 3 to 7`);
|
|
119
|
+
return out;
|
|
120
|
+
}
|
|
121
|
+
/** A tool's layout rules for the generated stylesheet, each under its data-layout, inside one wide-screen media query. */
|
|
122
|
+
export function renderLayouts(t, comment, decls) {
|
|
123
|
+
const withRules = t.layouts.filter((l) => l.rules.length);
|
|
124
|
+
if (!withRules.length)
|
|
125
|
+
return [];
|
|
126
|
+
const out = ["", ` ${comment(`Layouts: under ${t.root} with data-layout, from ${LAYOUT_BREAKPOINT} wide (a phone always stacks). ${t.default} is the default.`)}`, ` @media (min-width: ${LAYOUT_BREAKPOINT}) {`];
|
|
127
|
+
for (const l of withRules) {
|
|
128
|
+
out.push(` ${comment(`${l.title}: ${l.bestFor}`)}`);
|
|
129
|
+
for (const r of l.rules)
|
|
130
|
+
out.push(` .${t.root}[data-layout="${l.id}"]${r.selector ? (r.selector.startsWith(":") ? r.selector : ` ${r.selector.trim()}`) : ""} {`, decls(r.css, " "), " }");
|
|
131
|
+
}
|
|
132
|
+
out.push(" }");
|
|
133
|
+
return out;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* The layout a tool draws in: the one asked for, if the tool has it, else the
|
|
137
|
+
* project's default for the tool, else the tool's own. The one resolver of §8
|
|
138
|
+
* grows from this in design mode.
|
|
139
|
+
*/
|
|
140
|
+
export function chooseLayout(t, asked, projectDefault) {
|
|
141
|
+
const has = (id) => !!id && t.layouts.some((l) => l.id === id);
|
|
142
|
+
return has(asked) ? asked : has(projectDefault) ? projectDefault : t.default;
|
|
143
|
+
}
|