@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
|
@@ -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 [
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
// workbench-ui's own samples (docs/look-studio.md §5.1): every page part a
|
|
2
|
+
// project's pages are built from, drawn by the parts themselves with made-up
|
|
3
|
+
// words. Exported at ./samples beside ./styles.
|
|
4
|
+
import { actions, body, button, card, check, chip, code, confirm, facts, field, input, jumpNav, message, more, note, pill, select, small, table, textarea, topBar } from "./index.js";
|
|
5
|
+
import { styleGuide } from "./style-guide.js";
|
|
6
|
+
export const UI_SAMPLES = {
|
|
7
|
+
format: 1,
|
|
8
|
+
package: "@mccaskie-labs/workbench-ui",
|
|
9
|
+
title: "Page parts",
|
|
10
|
+
prefix: "wb-ui-",
|
|
11
|
+
samples: [
|
|
12
|
+
{
|
|
13
|
+
id: "page",
|
|
14
|
+
title: "A page: the bar on top, the heading and a card",
|
|
15
|
+
html: () => topBar({ name: "Saltbush Yoga", href: "/", env: "staging", links: [["Classes", "/classes"], ["Contact", "/contact"]], below: jumpNav([{ id: "today", label: "Today", chip: { tone: "pass", text: "All booked" } }]) }) +
|
|
16
|
+
`<main class="wb-ui-main"><h1 class="wb-ui-title">Admin</h1>${card("Today", body("<p>Three classes, 24 people booked.</p>"), { id: "today", about: "What is on today.", counts: pill("pass", "3 classes") })}</main>`,
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
id: "words",
|
|
20
|
+
title: "Words: a note, messages and small print",
|
|
21
|
+
html: () => note("<p>Bookings close an hour before each class.</p>") +
|
|
22
|
+
message("pass", "Saved.") +
|
|
23
|
+
message("warn", "Two places left.") +
|
|
24
|
+
message("fail", "That didn't save: try again.") +
|
|
25
|
+
small("Prices include GST."),
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
id: "outcomes",
|
|
29
|
+
title: "Outcome pills and chips",
|
|
30
|
+
html: () => ["pass", "refused", "fail", "skip", "none", "running"].map((t) => `${pill(t, t)} ${chip(t, "1 waiting")}`).join(" "),
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
id: "form",
|
|
34
|
+
title: "Fields, a tick box and buttons",
|
|
35
|
+
html: () => field("Your name", "s-name", input({ id: "s-name", name: "name", value: "Alex Rivera" }), { hint: "As it should appear on your booking" }) +
|
|
36
|
+
field("Class", "s-class", select({ id: "s-class", name: "class" }, [["flow", "Morning flow"], ["yin", "Evening yin"]], "yin")) +
|
|
37
|
+
field("Anything we should know?", "s-notes", textarea({ id: "s-notes", name: "notes" }, "First class in a while.")) +
|
|
38
|
+
check("Send me the timetable each month", { name: "news", checked: true }) +
|
|
39
|
+
actions(button("Book") + button("Back", { kind: "secondary", type: "button" }) + button("Not now", { kind: "link", type: "button" })),
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
id: "field-widths",
|
|
43
|
+
title: "Field widths, from what each answer holds",
|
|
44
|
+
html: () => [4, 5, 7, 9, 11, 13, 15, 19, 23, 29, 39]
|
|
45
|
+
.map((n) => field(`Up to ${n} characters`, `s-w${n}`, input({ id: `s-w${n}`, name: `w${n}`, maxlength: n })))
|
|
46
|
+
.join(""),
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
id: "table",
|
|
50
|
+
title: "A table and facts",
|
|
51
|
+
html: () => table(["Class", "Booked", "State"], [["Morning flow", "12", pill("pass", "Full")], { cells: ["Evening yin", "4", pill("fail", "Cancelled")], tone: "fail" }]) +
|
|
52
|
+
facts([["Teacher", "Sam"], ["Room", "Studio 2"]]),
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
id: "more",
|
|
56
|
+
title: "Folded detail and exact text",
|
|
57
|
+
html: () => more("Show the email", code("Hi Alex, see you on Tuesday.")),
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
id: "confirm",
|
|
61
|
+
title: "Are you sure?",
|
|
62
|
+
html: () => confirm({ endpoint: "/admin/classes/cancel", fields: { id: "yin-1" }, ask: "Cancel this class", question: "Cancel Evening yin on Tuesday?", consequence: "The 4 people booked are emailed and refunded.", yes: "Yes, cancel it", no: "Keep it", reason: { label: "Why (sent to them)", name: "reason" } }),
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
id: "style-guide",
|
|
66
|
+
title: "The style guide itself, with one sample",
|
|
67
|
+
html: () => styleGuide([{ format: 1, package: "@mccaskie-labs/workbench-ui", title: "Page parts", prefix: "wb-ui-", samples: [{ id: "note", title: "A note", layout: "stacked", html: () => note("<p>A note.</p>") }] }]) +
|
|
68
|
+
styleGuide([], { view: "dark" }),
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
};
|
package/dist/ui-styles.js
CHANGED
|
@@ -313,5 +313,25 @@ export const UI_STYLES = {
|
|
|
313
313
|
css: `font:.85rem/1.5 ${v("mono")};white-space:pre-wrap;overflow-wrap:anywhere;padding:.75rem 1rem;margin:.75rem 0;background:${v("surface")};border:1px solid ${v("line")};border-radius:8px;max-height:28rem;overflow:auto`,
|
|
314
314
|
},
|
|
315
315
|
...confirmStyles("wb-ui", (n) => v(CONFIRM_VARS[n] ?? n), { reason: true }),
|
|
316
|
+
// The style guide page (docs/look-studio.md §9.5): every tool's samples in the project's look.
|
|
317
|
+
{ name: "wb-ui-guide", element: "div", about: "The style guide: every part of every tool the project uses", css: "display:grid;gap:2rem" },
|
|
318
|
+
{ name: "wb-ui-guide-views", element: "nav", about: "The switch between light, dark and the admin look", css: "display:flex;flex-wrap:wrap;gap:.5rem" },
|
|
319
|
+
{
|
|
320
|
+
name: "wb-ui-guide-view",
|
|
321
|
+
element: "a",
|
|
322
|
+
about: "One view in the switch",
|
|
323
|
+
css: `padding:.3rem .8rem;border:1px solid ${v("line")};border-radius:999px;color:${v("text")};text-decoration:none`,
|
|
324
|
+
states: [{ selector: "[aria-current=page]", about: "The view shown now", css: `background:${v("accent")};border-color:${v("accent")};color:${v("accent-text")}` }, { selector: ":focus-visible", about: "Has focus", css: FOCUS }],
|
|
325
|
+
},
|
|
326
|
+
{ name: "wb-ui-guide-index", element: "nav", about: "Links to each tool's section", css: "display:flex;flex-wrap:wrap;gap:.25rem 1rem;font-size:.9rem" },
|
|
327
|
+
{ name: "wb-ui-guide-jump", element: "a", about: "A link to one tool's section", css: `color:${v("accent")}`, states: [{ selector: ":focus-visible", about: "Has focus", css: FOCUS }] },
|
|
328
|
+
{ name: "wb-ui-guide-tool", element: "section", about: "One tool's samples", css: "display:grid;gap:1rem" },
|
|
329
|
+
{ name: "wb-ui-guide-tool-title", element: "h2", about: "The tool's name", css: `margin:0;font-size:1.3rem;font-weight:700` },
|
|
330
|
+
{ name: "wb-ui-guide-sample", element: "figure", about: "One sample: a part or a layout", css: `margin:0;border:1px solid ${v("line")};border-radius:${v("corner")};overflow:hidden` },
|
|
331
|
+
{ name: "wb-ui-guide-caption", element: "figcaption", about: "What the sample shows", css: `display:flex;flex-wrap:wrap;gap:.5rem;justify-content:space-between;padding:.5rem .9rem;font-size:.85rem;color:${v("muted")};border-bottom:1px solid ${v("line")}` },
|
|
332
|
+
{ name: "wb-ui-guide-title", element: "span", about: "The sample's name", css: `color:${v("text")}` },
|
|
333
|
+
{ name: "wb-ui-guide-layout", element: "span", about: "The tool layout a sample shows", css: "font-weight:600" },
|
|
334
|
+
{ name: "wb-ui-guide-frame", element: "div", about: "The sample itself, drawn on the page's own background", css: "padding:1.25rem" },
|
|
335
|
+
{ name: "wb-ui-guide-empty", element: "p", about: "Said when no tool has samples yet", css: `color:${v("muted")}` },
|
|
316
336
|
],
|
|
317
337
|
};
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|