@mccaskie-labs/workbench-ui 0.12.0 → 0.13.1
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 +15 -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/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
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { ToolStyles } from "./styles.ts";
|
|
2
|
+
/** One sample: a part of a tool, or one of its layouts, drawn with sample data. */
|
|
3
|
+
export interface ToolSample {
|
|
4
|
+
/** Unique within the tool: lower case and hyphens. */
|
|
5
|
+
id: string;
|
|
6
|
+
/** What it shows, in the owner's words ("A form with a problem"). */
|
|
7
|
+
title: string;
|
|
8
|
+
/** The tool layout it shows, when it shows one (look studio §7). */
|
|
9
|
+
layout?: string;
|
|
10
|
+
/** The HTML, drawn by the tool's own renderers. */
|
|
11
|
+
html: () => string;
|
|
12
|
+
}
|
|
13
|
+
/** Every sample of one tool, matched to its ToolStyles by `package` and `prefix`. */
|
|
14
|
+
export interface ToolSamples {
|
|
15
|
+
format: 1;
|
|
16
|
+
package: string;
|
|
17
|
+
/** The tool's name as the style guide heads its section ("Forms"). */
|
|
18
|
+
title: string;
|
|
19
|
+
prefix: string;
|
|
20
|
+
samples: readonly ToolSample[];
|
|
21
|
+
}
|
|
22
|
+
/** Every class named in some HTML's class attributes. */
|
|
23
|
+
export declare function classesIn(html: string): Set<string>;
|
|
24
|
+
/**
|
|
25
|
+
* What is wrong with a tool's samples, in plain words: a class in its styles
|
|
26
|
+
* that no sample draws, a class a sample draws with the tool's prefix that its
|
|
27
|
+
* styles don't list, a sample that draws nothing or throws, or two samples
|
|
28
|
+
* with one id.
|
|
29
|
+
*/
|
|
30
|
+
export declare function sampleProblems(styles: ToolStyles, samples: ToolSamples): string[];
|
package/dist/samples.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
// Tool samples (docs/look-studio.md §5.1, Robert, 24 November 2026). Each tool
|
|
2
|
+
// that draws on a project's pages exports `./samples`: a ToolSamples per tool
|
|
3
|
+
// it draws, each sample drawn by the tool's own renderers with made-up data,
|
|
4
|
+
// one per part and one per tool layout. They feed the project's style guide
|
|
5
|
+
// page, the look previewer, the look studio's previews and the visual change
|
|
6
|
+
// check, so a new element can't skip the style guide any more than it can
|
|
7
|
+
// skip having a class: `sampleProblems` fails a class no sample draws.
|
|
8
|
+
const PAGE_ELEMENTS = new Set(["html", "body"]);
|
|
9
|
+
const ID = /^[a-z][a-z0-9-]{0,47}$/;
|
|
10
|
+
/** Every class named in some HTML's class attributes. */
|
|
11
|
+
export function classesIn(html) {
|
|
12
|
+
const out = new Set();
|
|
13
|
+
for (const m of html.matchAll(/\sclass\s*=\s*(?:"([^"]*)"|'([^']*)')/g))
|
|
14
|
+
for (const c of (m[1] ?? m[2] ?? "").split(/\s+/))
|
|
15
|
+
if (c)
|
|
16
|
+
out.add(c);
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* What is wrong with a tool's samples, in plain words: a class in its styles
|
|
21
|
+
* that no sample draws, a class a sample draws with the tool's prefix that its
|
|
22
|
+
* styles don't list, a sample that draws nothing or throws, or two samples
|
|
23
|
+
* with one id.
|
|
24
|
+
*/
|
|
25
|
+
export function sampleProblems(styles, samples) {
|
|
26
|
+
const out = [];
|
|
27
|
+
if (samples.package !== styles.package || samples.prefix !== styles.prefix)
|
|
28
|
+
out.push(`${styles.tool}: samples are for ${samples.package} ${samples.prefix}, styles for ${styles.package} ${styles.prefix}`);
|
|
29
|
+
const drawn = new Set();
|
|
30
|
+
const ids = new Set();
|
|
31
|
+
for (const s of samples.samples) {
|
|
32
|
+
if (!ID.test(s.id))
|
|
33
|
+
out.push(`${styles.tool}: sample id ${s.id} must be lower case and hyphens`);
|
|
34
|
+
if (ids.has(s.id))
|
|
35
|
+
out.push(`${styles.tool}: two samples are called ${s.id}`);
|
|
36
|
+
ids.add(s.id);
|
|
37
|
+
let html;
|
|
38
|
+
try {
|
|
39
|
+
html = s.html();
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
out.push(`${styles.tool}: sample ${s.id} throws: ${e.message}`);
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (!html.trim())
|
|
46
|
+
out.push(`${styles.tool}: sample ${s.id} draws nothing`);
|
|
47
|
+
for (const c of classesIn(html))
|
|
48
|
+
drawn.add(c);
|
|
49
|
+
}
|
|
50
|
+
const listed = new Set(styles.classes.map((c) => c.name));
|
|
51
|
+
// A class on the page itself (html, body) is shown by the page around the samples.
|
|
52
|
+
for (const c of styles.classes)
|
|
53
|
+
if (!PAGE_ELEMENTS.has(c.element) && !drawn.has(c.name))
|
|
54
|
+
out.push(`${styles.tool}: no sample draws ${c.name}: add one, so it is on the style guide`);
|
|
55
|
+
for (const l of styles.layouts?.layouts ?? [])
|
|
56
|
+
if (!samples.samples.some((x) => x.layout === l.id))
|
|
57
|
+
out.push(`${styles.tool}: no sample shows the layout ${l.id}: add one, so it is on the style guide`);
|
|
58
|
+
for (const x of samples.samples)
|
|
59
|
+
if (x.layout && !styles.layouts?.layouts.some((l) => l.id === x.layout))
|
|
60
|
+
out.push(`${styles.tool}: sample ${x.id} shows the layout ${x.layout}, which the tool doesn't have`);
|
|
61
|
+
for (const c of drawn)
|
|
62
|
+
if (c.startsWith(styles.prefix) && !listed.has(c))
|
|
63
|
+
out.push(`${styles.tool}: sample draws ${c}, which its styles don't list`);
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ToolSamples } from "./samples.ts";
|
|
2
|
+
/** Which look the guide shows: the project's light or dark, or its admin pages. */
|
|
3
|
+
export type GuideView = "light" | "dark" | "admin";
|
|
4
|
+
/** The view a guide address asks for (`?view=dark`), light when it names none or another. */
|
|
5
|
+
export declare function guideView(url: string | URL): GuideView;
|
|
6
|
+
export interface StyleGuideOptions {
|
|
7
|
+
view?: GuideView;
|
|
8
|
+
/** The switch's words, in the page's language. */
|
|
9
|
+
words?: {
|
|
10
|
+
views?: string;
|
|
11
|
+
light?: string;
|
|
12
|
+
dark?: string;
|
|
13
|
+
admin?: string;
|
|
14
|
+
empty?: string;
|
|
15
|
+
layout?: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
/** The guide's HTML. Sample HTML is the tools' own, drawn by their renderers. */
|
|
19
|
+
export declare function styleGuide(samples: readonly ToolSamples[], opts?: StyleGuideOptions): string;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The style guide page's body (docs/look-studio.md §9.5): every sample of
|
|
2
|
+
// every tool the project uses, in the project's look, with a switch between
|
|
3
|
+
// light, dark and the admin look. The page around it (the workbench-site
|
|
4
|
+
// recipe /admin/style-guide) sets data-wb-scheme or wb-ui-admin on <html>
|
|
5
|
+
// from the same query and links style-guide.css, which holds the project's
|
|
6
|
+
// tokens for both schemes.
|
|
7
|
+
const esc = (s) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
8
|
+
/** The view a guide address asks for (`?view=dark`), light when it names none or another. */
|
|
9
|
+
export function guideView(url) {
|
|
10
|
+
const v = new URL(url, "https://x.invalid").searchParams.get("view");
|
|
11
|
+
return v === "dark" || v === "admin" ? v : "light";
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Samples can't do anything here: every form is sent back to the guide as an
|
|
15
|
+
* ordinary address (GET, no action), so pressing "Yes, cancel it" in a sample
|
|
16
|
+
* changes nothing anywhere.
|
|
17
|
+
*/
|
|
18
|
+
function harmless(html) {
|
|
19
|
+
return html.replace(/<form\b([^>]*)>/gi, (_m, a) => `<form${a.replace(/\s(?:method|action)\s*=\s*("[^"]*"|'[^']*'|[^\s>]+)/gi, "")} method="get" action="">`);
|
|
20
|
+
}
|
|
21
|
+
/** The guide's HTML. Sample HTML is the tools' own, drawn by their renderers. */
|
|
22
|
+
export function styleGuide(samples, opts = {}) {
|
|
23
|
+
const view = opts.view ?? "light";
|
|
24
|
+
const w = { views: "Show", light: "Light", dark: "Dark", admin: "Admin pages", empty: "No tool on this site has samples yet.", layout: "Layout", ...opts.words };
|
|
25
|
+
const views = ["light", "dark", "admin"]
|
|
26
|
+
.map((v) => `<a class="wb-ui-guide-view" href="?view=${v}"${v === view ? ' aria-current="page"' : ""}>${esc(w[v])}</a>`)
|
|
27
|
+
.join("");
|
|
28
|
+
const tools = samples.filter((t) => t.samples.length);
|
|
29
|
+
if (!tools.length)
|
|
30
|
+
return `<div class="wb-ui-guide"><nav class="wb-ui-guide-views" aria-label="${esc(w.views)}">${views}</nav><p class="wb-ui-guide-empty">${esc(w.empty)}</p></div>`;
|
|
31
|
+
const id = (t) => `guide-${t.prefix.replace(/^wb-|-$/g, "")}`;
|
|
32
|
+
const index = `<nav class="wb-ui-guide-index" aria-label="Tools">${tools.map((t) => `<a class="wb-ui-guide-jump" href="#${id(t)}">${esc(t.title)}</a>`).join("")}</nav>`;
|
|
33
|
+
const sections = tools.map((t) => {
|
|
34
|
+
const figures = t.samples.map((s) => {
|
|
35
|
+
const layout = s.layout ? `<span class="wb-ui-guide-layout">${esc(w.layout)}: ${esc(s.layout)}</span>` : "";
|
|
36
|
+
return `<figure class="wb-ui-guide-sample" id="${id(t)}-${esc(s.id)}"><figcaption class="wb-ui-guide-caption"><span class="wb-ui-guide-title">${esc(s.title)}</span>${layout}</figcaption><div class="wb-ui-guide-frame">${harmless(s.html())}</div></figure>`;
|
|
37
|
+
});
|
|
38
|
+
return `<section class="wb-ui-guide-tool" id="${id(t)}"><h2 class="wb-ui-guide-tool-title">${esc(t.title)}</h2>${figures.join("")}</section>`;
|
|
39
|
+
});
|
|
40
|
+
return `<div class="wb-ui-guide"><nav class="wb-ui-guide-views" aria-label="${esc(w.views)}">${views}</nav>${index}${sections.join("")}</div>`;
|
|
41
|
+
}
|
package/dist/styles.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type ToolLayouts } from "./layouts.ts";
|
|
1
2
|
/** One CSS custom property a tool's rules read, with its default. */
|
|
2
3
|
export interface StyleVariable {
|
|
3
4
|
name: string;
|
|
@@ -27,6 +28,8 @@ export interface ToolStyles {
|
|
|
27
28
|
prefix: string;
|
|
28
29
|
variables: readonly StyleVariable[];
|
|
29
30
|
classes: readonly StyleClass[];
|
|
31
|
+
/** Its tool layouts (docs/look-studio.md §7), when it has more than one way of laying out its parts. */
|
|
32
|
+
layouts?: ToolLayouts;
|
|
30
33
|
}
|
|
31
34
|
/** A tool's styles with the version of the package installed. */
|
|
32
35
|
export interface InstalledStyles {
|
|
@@ -56,7 +59,7 @@ export declare const DEFAULT_THEME_CSS: string;
|
|
|
56
59
|
* basics (BASE_CSS) in `@layer workbench.page`, then each tool's classes in
|
|
57
60
|
* `@layer workbench.tools`, which wins over both.
|
|
58
61
|
*/
|
|
59
|
-
export declare function renderStyles(tools: readonly InstalledStyles[], uiVersion: string, themeCss?: string): string;
|
|
62
|
+
export declare function renderStyles(tools: readonly InstalledStyles[], uiVersion: string, themeCss?: string, fontCss?: string): string;
|
|
60
63
|
/** The starting project.css: empty rules the project fills, with every variable listed in a comment. */
|
|
61
64
|
export declare function starterProjectCss(tools: readonly InstalledStyles[]): string;
|
|
62
65
|
/** Every wb- class and variable the tools draw with: what a project's own CSS may name. */
|
package/dist/styles.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
// public/styles/workbench.css inside `@layer workbench`, so any rule in the
|
|
7
7
|
// project's own public/styles/project.css wins without !important. This file
|
|
8
8
|
// renders that stylesheet and lists the names a project may use.
|
|
9
|
+
import { layoutProblems, renderLayouts } from "./layouts.js";
|
|
9
10
|
import { BASE_CSS } from "./style.js";
|
|
10
11
|
import { renderTheme, resolveAdminTheme, resolveTheme, THEME_TOKENS } from "./theme.js";
|
|
11
12
|
/** Where the generated stylesheet and the project's own sit, from the project's root. */
|
|
@@ -98,6 +99,8 @@ export function styleProblems(s) {
|
|
|
98
99
|
out.push(`${s.tool}: ${c.name} reads ${m[1]}, which isn't listed`);
|
|
99
100
|
}
|
|
100
101
|
}
|
|
102
|
+
if (s.layouts)
|
|
103
|
+
out.push(...layoutProblems(s.tool, s.prefix, classes, s.layouts));
|
|
101
104
|
return out;
|
|
102
105
|
}
|
|
103
106
|
const comment = (text) => `/* ${text.replace(/\*\//g, "* /")} */`;
|
|
@@ -118,6 +121,8 @@ function toolBlock({ styles: s, version }) {
|
|
|
118
121
|
for (const st of c.states ?? [])
|
|
119
122
|
out.push(` ${comment(st.about)}`, ` .${c.name}${st.selector} {`, decls(st.css, " "), " }");
|
|
120
123
|
}
|
|
124
|
+
if (s.layouts)
|
|
125
|
+
out.push(...renderLayouts(s.layouts, comment, decls));
|
|
121
126
|
return out.join("\n");
|
|
122
127
|
}
|
|
123
128
|
const HEADER = [
|
|
@@ -137,7 +142,7 @@ export const DEFAULT_THEME_CSS = renderTheme(resolveTheme(), resolveAdminTheme()
|
|
|
137
142
|
* basics (BASE_CSS) in `@layer workbench.page`, then each tool's classes in
|
|
138
143
|
* `@layer workbench.tools`, which wins over both.
|
|
139
144
|
*/
|
|
140
|
-
export function renderStyles(tools, uiVersion, themeCss = DEFAULT_THEME_CSS) {
|
|
145
|
+
export function renderStyles(tools, uiVersion, themeCss = DEFAULT_THEME_CSS, fontCss = "") {
|
|
141
146
|
const problems = tools.flatMap((t) => styleProblems(t.styles));
|
|
142
147
|
if (problems.length)
|
|
143
148
|
throw new Error(`workbench-ui styles: ${problems.join("; ")}`);
|
|
@@ -146,6 +151,7 @@ export function renderStyles(tools, uiVersion, themeCss = DEFAULT_THEME_CSS) {
|
|
|
146
151
|
...HEADER,
|
|
147
152
|
"@layer workbench.theme, workbench.page, workbench.tools;",
|
|
148
153
|
"",
|
|
154
|
+
...(fontCss ? [fontCss] : []),
|
|
149
155
|
themeCss,
|
|
150
156
|
"",
|
|
151
157
|
comment(`Page basics: @mccaskie-labs/workbench-ui ${uiVersion}, only on a page built with shell (html.wb-ui), and of no weight. Everything its helpers draw (cards, notes, buttons, fields, tables) is wb-ui- below.`),
|
package/dist/theme.d.ts
CHANGED
|
@@ -85,6 +85,13 @@ export interface ThemeInput {
|
|
|
85
85
|
export declare function mix(a: string, b: string, amount: number): string;
|
|
86
86
|
/** The WCAG contrast ratio of two colours, 1 to 21. */
|
|
87
87
|
export declare function contrast(a: string, b: string): number;
|
|
88
|
+
/**
|
|
89
|
+
* The colour nearest `colour` that reaches `ratio` against `against`: same hue,
|
|
90
|
+
* lightness moved as little as it takes (lighter or darker, whichever is
|
|
91
|
+
* nearer), and saturation lowered only when lightness alone can't get there.
|
|
92
|
+
* A colour that already reads comes back as it is.
|
|
93
|
+
*/
|
|
94
|
+
export declare function nearestPassing(colour: string, against: string, ratio: number): string;
|
|
88
95
|
/**
|
|
89
96
|
* A project's public look from what brand.json says: the named look (Clean
|
|
90
97
|
* unless it names another) in its scheme (light unless dark), with brand.json's
|
|
@@ -104,11 +111,35 @@ export declare function resolveAdminTheme(input?: ThemeInput, lib?: Library): Th
|
|
|
104
111
|
export declare function resolveSiteLayout(input?: ThemeInput, lib?: Library): LayoutItem;
|
|
105
112
|
/** Every pair of a theme's colours that doesn't read, named in plain words. */
|
|
106
113
|
export declare function themeProblems(theme: Theme): string[];
|
|
114
|
+
/** One suggested fix: the colour to change, what to, and why. */
|
|
115
|
+
export interface ThemeFix {
|
|
116
|
+
token: TokenName;
|
|
117
|
+
from: string;
|
|
118
|
+
to: string;
|
|
119
|
+
against: string;
|
|
120
|
+
ratio: number;
|
|
121
|
+
what: string;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* For each pair of a theme's colours that doesn't read, the nearest colour that
|
|
125
|
+
* does (docs/look-studio.md §5.2), changing the colour that is set (the accent,
|
|
126
|
+
* not the text worked out to sit on it). One fix per colour, the strictest.
|
|
127
|
+
* Nothing is changed: an owner's own colour is only ever suggested.
|
|
128
|
+
*/
|
|
129
|
+
export declare function themeFixes(theme: Theme): ThemeFix[];
|
|
130
|
+
/** A palette with every colour that doesn't read on its background moved to the nearest that does: what an AI proposal gets before the unchanged check runs. */
|
|
131
|
+
export declare function fixPalette(p: Palette, scheme: Scheme): Palette;
|
|
107
132
|
/** What brand.json says about the look, read loosely: a field of the wrong kind is named, never guessed. */
|
|
108
133
|
export declare function themeInput(brand: unknown, lib?: Library): {
|
|
109
134
|
input: ThemeInput;
|
|
110
135
|
problems: string[];
|
|
111
136
|
};
|
|
137
|
+
/**
|
|
138
|
+
* The style guide's own sheet (public/styles/style-guide.css): the project's
|
|
139
|
+
* look in light and in dark, switched by data-wb-scheme on <html>, so the
|
|
140
|
+
* guide shows both without the project changing its scheme.
|
|
141
|
+
*/
|
|
142
|
+
export declare function renderSchemes(input: ThemeInput, lib?: Library): string;
|
|
112
143
|
/** The project's `@layer workbench.theme`: the public look on :root, the admin look on html.wb-ui-admin. */
|
|
113
144
|
export declare function renderTheme(site: Theme, admin: Theme, source: string): string;
|
|
114
145
|
/** How far apart two palettes are: the mean distance of their background, text and accent in light and dark, 0 to 441. */
|
package/dist/theme.js
CHANGED
|
@@ -119,7 +119,63 @@ export function contrast(a, b) {
|
|
|
119
119
|
const [l1, l2] = [luminance(a), luminance(b)].sort((p, q) => q - p);
|
|
120
120
|
return (l1 + 0.05) / (l2 + 0.05);
|
|
121
121
|
}
|
|
122
|
-
|
|
122
|
+
// ---- the fixer (docs/look-studio.md §5.2): the nearest colour that reads ---------
|
|
123
|
+
function hsl(hex) {
|
|
124
|
+
const [r, g, b] = rgb(hex).map((v) => v / 255);
|
|
125
|
+
const max = Math.max(r, g, b);
|
|
126
|
+
const min = Math.min(r, g, b);
|
|
127
|
+
const l = (max + min) / 2;
|
|
128
|
+
if (max === min)
|
|
129
|
+
return [0, 0, l];
|
|
130
|
+
const d = max - min;
|
|
131
|
+
const s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
|
|
132
|
+
const h = max === r ? (g - b) / d + (g < b ? 6 : 0) : max === g ? (b - r) / d + 2 : (r - g) / d + 4;
|
|
133
|
+
return [h / 6, s, l];
|
|
134
|
+
}
|
|
135
|
+
function fromHsl(h, s, l) {
|
|
136
|
+
if (s === 0)
|
|
137
|
+
return toHex([l * 255, l * 255, l * 255]);
|
|
138
|
+
const q = l < 0.5 ? l * (1 + s) : l + s - l * s;
|
|
139
|
+
const p = 2 * l - q;
|
|
140
|
+
const ch = (t) => {
|
|
141
|
+
const u = t < 0 ? t + 1 : t > 1 ? t - 1 : t;
|
|
142
|
+
if (u < 1 / 6)
|
|
143
|
+
return p + (q - p) * 6 * u;
|
|
144
|
+
if (u < 1 / 2)
|
|
145
|
+
return q;
|
|
146
|
+
if (u < 2 / 3)
|
|
147
|
+
return p + (q - p) * (2 / 3 - u) * 6;
|
|
148
|
+
return p;
|
|
149
|
+
};
|
|
150
|
+
return toHex([ch(h + 1 / 3) * 255, ch(h) * 255, ch(h - 1 / 3) * 255]);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* The colour nearest `colour` that reaches `ratio` against `against`: same hue,
|
|
154
|
+
* lightness moved as little as it takes (lighter or darker, whichever is
|
|
155
|
+
* nearer), and saturation lowered only when lightness alone can't get there.
|
|
156
|
+
* A colour that already reads comes back as it is.
|
|
157
|
+
*/
|
|
158
|
+
export function nearestPassing(colour, against, ratio) {
|
|
159
|
+
const c = colour.toLowerCase();
|
|
160
|
+
if (contrast(c, against) >= ratio)
|
|
161
|
+
return c;
|
|
162
|
+
const [h, s, l] = hsl(c);
|
|
163
|
+
for (const keep of [1, 0.75, 0.5, 0.25, 0]) {
|
|
164
|
+
for (let step = 1; step <= 200; step++) {
|
|
165
|
+
const d = step / 200;
|
|
166
|
+
for (const lit of [l - d, l + d]) {
|
|
167
|
+
if (lit < 0 || lit > 1)
|
|
168
|
+
continue;
|
|
169
|
+
const candidate = fromHsl(h, s * keep, lit);
|
|
170
|
+
if (contrast(candidate, against) >= ratio)
|
|
171
|
+
return candidate;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
return contrast("#000000", against) >= contrast("#ffffff", against) ? "#000000" : "#ffffff";
|
|
176
|
+
}
|
|
177
|
+
/** The keyboard focus ring: the accent where it stands out from the page by 3:1, else the nearest colour that does. */
|
|
178
|
+
const focusRing = (accent, background) => nearestPassing(accent, background, 3);
|
|
123
179
|
/** `colour` moved towards `towards` in small steps until it reaches `ratio` against `against`. */
|
|
124
180
|
function untilContrast(colour, against, towards, ratio) {
|
|
125
181
|
let c = colour;
|
|
@@ -129,7 +185,7 @@ function untilContrast(colour, against, towards, ratio) {
|
|
|
129
185
|
}
|
|
130
186
|
// ---- the theme ---------------------------------------------------------------------
|
|
131
187
|
/** Text on a filled accent button: near-black or white, whichever reads better. */
|
|
132
|
-
const
|
|
188
|
+
const onAccentOf = (accent) => (contrast(accent, "#111111") >= contrast(accent, "#ffffff") ? "#111111" : "#ffffff");
|
|
133
189
|
/** What a site layout's width and spacing words come to (docs/look-studio.md §6). */
|
|
134
190
|
const PAGE_WIDTHS = { standard: "72rem", narrow: "56rem", wide: "90rem" };
|
|
135
191
|
const SECTION_SPACES = { standard: "2.5rem", compact: "1.5rem", airy: "4rem" };
|
|
@@ -145,8 +201,8 @@ function themeFrom(p, scheme, fonts, shape, layout = { width: "standard", spacin
|
|
|
145
201
|
"--wb-line": mix(p.background, p.text, dark ? 0.22 : 0.18),
|
|
146
202
|
"--wb-accent": p.accent,
|
|
147
203
|
"--wb-accent-hover": mix(p.accent, dark ? "#ffffff" : "#000000", 0.15),
|
|
148
|
-
"--wb-on-accent":
|
|
149
|
-
"--wb-focus":
|
|
204
|
+
"--wb-on-accent": onAccentOf(p.accent),
|
|
205
|
+
"--wb-focus": focusRing(p.accent, p.background),
|
|
150
206
|
"--wb-danger": p.danger,
|
|
151
207
|
"--wb-danger-bg": mix(p.background, p.danger, dark ? 0.16 : 0.08),
|
|
152
208
|
"--wb-success": p.success,
|
|
@@ -220,11 +276,61 @@ const PAIRS = [
|
|
|
220
276
|
["--wb-danger", "--wb-danger-bg", 4.5, "a problem message in its box"],
|
|
221
277
|
["--wb-success", "--wb-background", 3, "a done mark"],
|
|
222
278
|
["--wb-warning", "--wb-background", 3, "a take-care mark"],
|
|
279
|
+
["--wb-focus", "--wb-background", 3, "the keyboard focus ring"],
|
|
223
280
|
];
|
|
281
|
+
/** A size in rem or px, in pixels at the browser's usual 16px, or null when it is something else. */
|
|
282
|
+
const pixels = (size) => {
|
|
283
|
+
const m = /^(\d*\.?\d+)(rem|px)$/.exec(size.trim());
|
|
284
|
+
return m ? Number(m[1]) * (m[2] === "rem" ? 16 : 1) : null;
|
|
285
|
+
};
|
|
224
286
|
/** Every pair of a theme's colours that doesn't read, named in plain words. */
|
|
225
287
|
export function themeProblems(theme) {
|
|
226
288
|
const t = theme.tokens;
|
|
227
|
-
|
|
289
|
+
const out = PAIRS.filter(([a, b, ratio]) => contrast(t[a], t[b]) < ratio).map(([a, b, ratio, what]) => `${what} doesn't read: ${a} ${t[a]} on ${b} ${t[b]} is ${contrast(t[a], t[b]).toFixed(2)}:1, needs ${ratio}:1`);
|
|
290
|
+
const size = pixels(t["--wb-text-size"] ?? "1rem");
|
|
291
|
+
if (size !== null && size < 16)
|
|
292
|
+
out.push(`text is too small to read comfortably: --wb-text-size ${t["--wb-text-size"]} is ${size}px, needs at least 16px`);
|
|
293
|
+
return out;
|
|
294
|
+
}
|
|
295
|
+
/** The colour a pair's fix changes: the one an owner or a palette sets, never one worked out from it. */
|
|
296
|
+
const FIX_TARGET = { "--wb-on-accent": "--wb-accent", "--wb-focus": "--wb-accent" };
|
|
297
|
+
/**
|
|
298
|
+
* For each pair of a theme's colours that doesn't read, the nearest colour that
|
|
299
|
+
* does (docs/look-studio.md §5.2), changing the colour that is set (the accent,
|
|
300
|
+
* not the text worked out to sit on it). One fix per colour, the strictest.
|
|
301
|
+
* Nothing is changed: an owner's own colour is only ever suggested.
|
|
302
|
+
*/
|
|
303
|
+
export function themeFixes(theme) {
|
|
304
|
+
const t = theme.tokens;
|
|
305
|
+
const fixes = new Map();
|
|
306
|
+
for (const [a, b, ratio, what] of PAIRS) {
|
|
307
|
+
if (contrast(t[a], t[b]) >= ratio)
|
|
308
|
+
continue;
|
|
309
|
+
const target = FIX_TARGET[a] ?? a;
|
|
310
|
+
const against = target === a ? t[b] : t["--wb-background"];
|
|
311
|
+
// A filled button's text is near-black or white: the accent must read against whichever it gets, and stay a link colour on the page.
|
|
312
|
+
const need = target === "--wb-accent" ? Math.max(ratio, 4.5) : ratio;
|
|
313
|
+
let to = nearestPassing(t[target], against, need);
|
|
314
|
+
if (target === "--wb-accent" && contrast(onAccentOf(to), to) < 4.5)
|
|
315
|
+
to = nearestPassing(to, onAccentOf(to), 4.5);
|
|
316
|
+
const prev = fixes.get(target);
|
|
317
|
+
if (!prev || need > prev.ratio)
|
|
318
|
+
fixes.set(target, { token: target, from: t[target], to, against, ratio: need, what });
|
|
319
|
+
}
|
|
320
|
+
return [...fixes.values()];
|
|
321
|
+
}
|
|
322
|
+
/** A palette with every colour that doesn't read on its background moved to the nearest that does: what an AI proposal gets before the unchanged check runs. */
|
|
323
|
+
export function fixPalette(p, scheme) {
|
|
324
|
+
const out = { ...p };
|
|
325
|
+
for (const k of ["text", "muted", "accent", "danger"])
|
|
326
|
+
out[k] = nearestPassing(out[k], p.background, 4.5);
|
|
327
|
+
for (const k of ["success", "warning"])
|
|
328
|
+
out[k] = nearestPassing(out[k], p.background, 3);
|
|
329
|
+
// Text in a box, and a problem message in its tinted box, sit on slightly different backgrounds.
|
|
330
|
+
const t = themeFrom(out, scheme, { heading: SANS, body: SANS }, "soft").tokens;
|
|
331
|
+
out.text = nearestPassing(out.text, t["--wb-surface"], 4.5);
|
|
332
|
+
out.danger = nearestPassing(out.danger, t["--wb-danger-bg"], 4.5);
|
|
333
|
+
return out;
|
|
228
334
|
}
|
|
229
335
|
/** What brand.json says about the look, read loosely: a field of the wrong kind is named, never guessed. */
|
|
230
336
|
export function themeInput(brand, lib = LIBRARY) {
|
|
@@ -297,6 +403,23 @@ function declarations(theme, indent) {
|
|
|
297
403
|
out.push(`${indent}${comment(t.about)}`, `${indent}${t.name}: ${theme.tokens[t.name]};`);
|
|
298
404
|
return out;
|
|
299
405
|
}
|
|
406
|
+
/**
|
|
407
|
+
* The style guide's own sheet (public/styles/style-guide.css): the project's
|
|
408
|
+
* look in light and in dark, switched by data-wb-scheme on <html>, so the
|
|
409
|
+
* guide shows both without the project changing its scheme.
|
|
410
|
+
*/
|
|
411
|
+
export function renderSchemes(input, lib = LIBRARY) {
|
|
412
|
+
const block = (scheme) => [` :root[data-wb-scheme="${scheme}"] {`, ...declarations(resolveTheme({ ...input, scheme }, lib), " "), " }"];
|
|
413
|
+
return [
|
|
414
|
+
comment("Written by `npx workbench-ui styles` for the style guide page only: the project's look in light and dark. Don't edit."),
|
|
415
|
+
"@layer workbench.theme {",
|
|
416
|
+
...block("light"),
|
|
417
|
+
"",
|
|
418
|
+
...block("dark"),
|
|
419
|
+
"}",
|
|
420
|
+
"",
|
|
421
|
+
].join("\n");
|
|
422
|
+
}
|
|
300
423
|
/** The project's `@layer workbench.theme`: the public look on :root, the admin look on html.wb-ui-admin. */
|
|
301
424
|
export function renderTheme(site, admin, source) {
|
|
302
425
|
return [
|