@mccaskie-labs/workbench-ui 0.13.1 → 0.13.3

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 CHANGED
@@ -1,5 +1,17 @@
1
1
  # @mccaskie-labs/workbench-ui
2
2
 
3
+ ## 0.13.3
4
+
5
+ ### Patch Changes
6
+
7
+ - The look previewer (docs/look-studio.md §9.1, step 7a): `lookPreview({ pages, url, current, live, words })` draws the project's own pages framed side by side, as the site is now and in every look it may choose, with a switch for the page and for light or dark (`lookPreviewView` reads them from the address, kept to the site's own paths). On the live site (`live: true`) it shows only the site as it is, with a note that the other looks show on the test site. New `wb-ui-look*` classes, each with its default style and a sample. The preview fonts are now each look's families in full, not only the latin letters, so a look previewed in another language shows its own letters. Additive only.
8
+
9
+ ## 0.13.2
10
+
11
+ ### Patch Changes
12
+
13
+ - Preview styles (docs/look-studio.md §5.3, step 7a). `npx workbench-ui styles` also writes `public/styles/looks.css`: every look a project may choose (the package's and its own `workbench/looks/`), in light and dark, as token blocks under `:root[data-wb-look][data-wb-scheme]`, with `@font-face` rules for each look's fonts in the latin letters. Those font files are copied into `public/fonts/` beside the project's own, so a preview never calls a font service. `previewAttributes(url, allowed)` reads `?wb-look=` and `?wb-scheme=` for the page frame, only when the caller allows it (a signed-in admin, or outside production), and refuses anything that isn't an id. It also writes `src/site-layout.generated.ts`, whose `SITE_LAYOUT` is the site layout brand.json resolves to, a project's own from `workbench/looks/` too, so the Worker's page frame can use one it can't read at run time. `chooseLayout` now takes any number of choices in order (design mode's trial, the kept setting, the code's option, the answers file) and draws the first the tool has. Additive only.
14
+
3
15
  ## 0.13.1
4
16
 
5
17
  ### Patch Changes
package/dist/cli.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { type Library } from "./library.ts";
2
+ import { type LayoutItem, type Library } from "./library.ts";
3
3
  import { type InstalledStyles } from "./styles.ts";
4
4
  /** The styles of each Workbench tool the project at `dir` has installed, and the workbench-ui version. */
5
5
  export declare function installedStyles(dir: string): Promise<{
@@ -11,6 +11,10 @@ export declare const SAMPLE_FILES: {
11
11
  readonly module: "src/samples.generated.ts";
12
12
  readonly guide: "public/styles/style-guide.css";
13
13
  };
14
+ /** The project's resolved site layout, for its Worker, which can't read workbench/looks/ at run time. */
15
+ export declare const SITE_LAYOUT_FILE = "src/site-layout.generated.ts";
16
+ /** The generated module holding a project's site layout as data (docs/look-studio.md §6). */
17
+ export declare function siteLayoutModule(layout: LayoutItem): string;
14
18
  /** Every Workbench package the project at `dir` depends on whose package.json exports "./samples", sorted. */
15
19
  export declare function samplePackages(dir: string): string[];
16
20
  /** The generated samples module: one import per package, every ToolSamples it exports, in package order. */
@@ -52,6 +56,8 @@ export declare function projectTheme(dir: string): {
52
56
  problems: string[];
53
57
  fixes: BrandFix[];
54
58
  families: string[];
59
+ library: Library;
60
+ layout: LayoutItem;
55
61
  };
56
62
  /**
57
63
  * Rules in a project's own CSS that take the underline off plain links, so a
package/dist/cli.js CHANGED
@@ -23,7 +23,7 @@ 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, renderSchemes, renderTheme, resolveAdminTheme, resolveTheme, themeFixes, themeInput, themeProblems } from "./theme.js";
26
+ import { libraryProblems, PREVIEW_FILE, previewFamilies, renderLooks, renderSchemes, resolveSiteLayout, renderTheme, resolveAdminTheme, resolveTheme, themeFixes, themeInput, themeProblems } from "./theme.js";
27
27
  import { familiesIn, fontGaps, projectFonts } from "./fonts.js";
28
28
  import { findItem, itemProblems, LIBRARY, withProjectItems } from "./library.js";
29
29
  import { DEFAULT_THEME_CSS, renderStyles, STYLE_FILES, STYLED_SCOPE, starterProjectCss, unknownMarkupNames, unknownNames } from "./styles.js";
@@ -72,6 +72,19 @@ export async function installedStyles(dir) {
72
72
  }
73
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
74
  export const SAMPLE_FILES = { module: "src/samples.generated.ts", guide: "public/styles/style-guide.css" };
75
+ /** The project's resolved site layout, for its Worker, which can't read workbench/looks/ at run time. */
76
+ export const SITE_LAYOUT_FILE = "src/site-layout.generated.ts";
77
+ /** The generated module holding a project's site layout as data (docs/look-studio.md §6). */
78
+ export function siteLayoutModule(layout) {
79
+ const { id, header, footer, width, spacing, hero } = layout;
80
+ return [
81
+ "// Written by `npx workbench-ui styles`: the site layout brand.json resolves to (its `layout`, else its look's),",
82
+ "// including one from workbench/looks/. Don't edit; `npx workbench-ui styles --check` fails when it is out of date.",
83
+ "",
84
+ `export const SITE_LAYOUT = ${JSON.stringify({ id, header, footer, width, spacing, hero }, null, 2)} as const;`,
85
+ "",
86
+ ].join("\n");
87
+ }
75
88
  /** Every Workbench package the project at `dir` depends on whose package.json exports "./samples", sorted. */
76
89
  export function samplePackages(dir) {
77
90
  if (!existsSync(join(dir, "package.json")))
@@ -198,14 +211,16 @@ export function applyBrandFixes(brandText, fixes) {
198
211
  export function projectTheme(dir) {
199
212
  const at = join(dir, BRAND_FILE);
200
213
  const plain = () => familiesIn([resolveTheme().tokens["--wb-font-heading"], resolveTheme().tokens["--wb-font"]]);
201
- if (!existsSync(at))
202
- return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: projectLibrary(dir).problems, fixes: [], families: plain() };
214
+ if (!existsSync(at)) {
215
+ const own = projectLibrary(dir);
216
+ return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: own.problems, fixes: [], families: plain(), library: own.library, layout: resolveSiteLayout({}, own.library) };
217
+ }
203
218
  let brand;
204
219
  try {
205
220
  brand = JSON.parse(read(at));
206
221
  }
207
222
  catch {
208
- return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: [`${BRAND_FILE} isn't valid JSON`], fixes: [], families: plain() };
223
+ return { css: DEFAULT_THEME_CSS, guide: renderSchemes({}), problems: [`${BRAND_FILE} isn't valid JSON`], fixes: [], families: plain(), library: projectLibrary(dir).library, layout: resolveSiteLayout() };
209
224
  }
210
225
  const own = projectLibrary(dir);
211
226
  const { input, problems } = themeInput(brand, own.library);
@@ -218,7 +233,7 @@ export function projectTheme(dir) {
218
233
  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
234
  for (const f of themeFixes(site).filter((x) => !BRAND_FIELD[x.token]))
220
235
  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"]]) };
236
+ 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"]]), library: own.library, layout: resolveSiteLayout(input, own.library) };
222
237
  }
223
238
  /**
224
239
  * Rules in a project's own CSS that take the underline off plain links, so a
@@ -244,14 +259,23 @@ export function planStyles(dir, tools, uiVersion) {
244
259
  problems.push(...theme.problems);
245
260
  const fonts = projectFonts(theme.families);
246
261
  const generated = renderStyles(tools, uiVersion, theme.css, fonts.css);
247
- for (const f of fonts.files) {
262
+ // Every look the project may choose, with each look's fonts in full (docs/look-studio.md §5.3): the previewer and
263
+ // design mode switch looks with no redeploy, and a project started in any look already holds its fonts.
264
+ const preview = projectFonts(previewFamilies(theme.library));
265
+ const fontFiles = [...fonts.files, ...preview.files.filter((p) => !fonts.files.some((f) => f.path === p.path))];
266
+ for (const f of fontFiles) {
248
267
  const bytes = readFileSync(f.from);
249
268
  if (existsSync(join(dir, f.path)) && readFileSync(join(dir, f.path)).equals(bytes))
250
269
  continue;
251
270
  files[f.path] = bytes;
252
271
  problems.push(`${f.path} is missing or out of date: the look's fonts are hosted by the project`);
253
272
  }
254
- const derived = { [SAMPLE_FILES.module]: samplesModule(samplePackages(dir)), [SAMPLE_FILES.guide]: theme.guide };
273
+ const derived = {
274
+ [SAMPLE_FILES.module]: samplesModule(samplePackages(dir)),
275
+ [SAMPLE_FILES.guide]: theme.guide,
276
+ [PREVIEW_FILE.path]: renderLooks(theme.library, preview.css),
277
+ [SITE_LAYOUT_FILE]: siteLayoutModule(theme.layout),
278
+ };
255
279
  for (const [path, text] of Object.entries(derived)) {
256
280
  if (existsSync(join(dir, path)) && read(join(dir, path)) === text)
257
281
  continue;
@@ -328,7 +352,7 @@ async function main(argv) {
328
352
  }
329
353
  // Writing fixes workbench.css; project.css and the project's own pages are fixed by hand.
330
354
  // brand.json's problems are fixed by hand too.
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]));
355
+ const left = plan.problems.filter((p) => !p.startsWith(STYLE_FILES.generated) && !Object.values(SAMPLE_FILES).some((f) => p.startsWith(f)) && !p.startsWith(PREVIEW_FILE.path) && !p.startsWith(SITE_LAYOUT_FILE) && !p.startsWith("public/fonts/") && !(p.startsWith(STYLE_FILES.project) && plan.files[STYLE_FILES.project]));
332
356
  for (const p of left)
333
357
  console.error(p);
334
358
  return left.length ? 1 : 0;
package/dist/fonts.d.ts CHANGED
@@ -30,7 +30,9 @@ export declare function familiesIn(stacks: readonly string[]): string[];
30
30
  * workbench-ui hosts. A family it doesn't host (an owner's own) is left to its
31
31
  * fallback, as before.
32
32
  */
33
- export declare function projectFonts(families: readonly string[], hosted?: readonly HostedFamily[]): {
33
+ export declare function projectFonts(families: readonly string[], hosted?: readonly HostedFamily[],
34
+ /** Only these subsets (the preview styles take latin alone); every one when left out. */
35
+ subsets?: readonly string[]): {
34
36
  css: string;
35
37
  files: {
36
38
  path: string;
package/dist/fonts.js CHANGED
@@ -32,8 +32,14 @@ const q = (s) => `"${s.replace(/["\\]/g, "")}"`;
32
32
  * workbench-ui hosts. A family it doesn't host (an owner's own) is left to its
33
33
  * fallback, as before.
34
34
  */
35
- export function projectFonts(families, hosted = hostedFamilies()) {
36
- const used = families.map((f) => hosted.find((h) => h.family === f)).filter((h) => !!h);
35
+ export function projectFonts(families, hosted = hostedFamilies(),
36
+ /** Only these subsets (the preview styles take latin alone); every one when left out. */
37
+ subsets) {
38
+ const used = families
39
+ .map((f) => hosted.find((h) => h.family === f))
40
+ .filter((h) => !!h)
41
+ .map((h) => (subsets ? { ...h, files: h.files.filter((f) => subsets.includes(f.subset)) } : h))
42
+ .filter((h) => h.files.length > 0);
37
43
  if (!used.length)
38
44
  return { css: "", files: [] };
39
45
  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}; }`));
package/dist/index.d.ts CHANGED
@@ -6,8 +6,9 @@ export { FORM_SCRIPT } from "./form-script.ts";
6
6
  export { chooseLayout, LAYOUT_BREAKPOINT, LAYOUT_KIT, layoutProblems, type LayoutRule, type ToolLayout, type ToolLayouts } from "./layouts.ts";
7
7
  export { classesIn, sampleProblems, type ToolSample, type ToolSamples } from "./samples.ts";
8
8
  export { guideView, styleGuide, type GuideView, type StyleGuideOptions } from "./style-guide.ts";
9
+ export { lookPreview, lookPreviewView, type LookPreviewOptions, type PreviewPage } from "./look-preview.ts";
9
10
  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";
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";
11
+ export { contrast, DEFAULT_LOOK, LOOKS, fixPalette, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, nearestPassing, paletteDistance, PREVIEW_FILE, previewAttributes, previewFamilies, renderLooks, 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";
11
12
  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";
12
13
  /** Plain text made safe to put in HTML, in text or in a quoted attribute. */
13
14
  export declare const escape: (value: string) => string;
package/dist/index.js CHANGED
@@ -11,8 +11,9 @@ export { FORM_SCRIPT } from "./form-script.js";
11
11
  export { chooseLayout, LAYOUT_BREAKPOINT, LAYOUT_KIT, layoutProblems } from "./layouts.js";
12
12
  export { classesIn, sampleProblems } from "./samples.js";
13
13
  export { guideView, styleGuide } from "./style-guide.js";
14
+ export { lookPreview, lookPreviewView } from "./look-preview.js";
14
15
  export { DEFAULT_THEME_CSS, renderStyles, STYLE_FILES, STYLED_SCOPE, styleNames, styleProblems, STYLESHEETS, starterProjectCss, unknownMarkupNames, unknownNames, } from "./styles.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";
16
+ export { contrast, DEFAULT_LOOK, LOOKS, fixPalette, libraryProblems, lookById, lookFor, lookOf, looksIn, MIN_PALETTE_DISTANCE, mix, nearestPassing, paletteDistance, PREVIEW_FILE, previewAttributes, previewFamilies, renderLooks, renderSchemes, renderTheme, resolveAdminTheme, resolveSiteLayout, resolveTheme, THEME_TOKENS, themeFixes, themeInput, themeProblems, } from "./theme.js";
16
17
  export { findItem, itemProblems, LIBRARY, LIBRARY_KINDS, libraryOf, SITE_LAYOUT_CHOICES, usable, withProjectItems, } from "./library.js";
17
18
  /** Plain text made safe to put in HTML, in text or in a quoted attribute. */
18
19
  export const escape = (value) => value.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
package/dist/layouts.d.ts CHANGED
@@ -53,8 +53,10 @@ export declare function layoutProblems(tool: string, prefix: string, classes: Re
53
53
  /** A tool's layout rules for the generated stylesheet, each under its data-layout, inside one wide-screen media query. */
54
54
  export declare function renderLayouts(t: ToolLayouts, comment: (s: string) => string, decls: (css: string, indent: string) => string): string[];
55
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.
56
+ * The layout a tool draws in: the first of `choices` the tool has, else the
57
+ * tool's own default. This is the one resolver of docs/look-studio.md §7.1
58
+ * and §8, so callers pass, in this order: design mode's trial, the kept
59
+ * setting, the code's own per-use option, the tool's answers file. An id the
60
+ * tool doesn't have (a layout since removed) is passed over, never drawn.
59
61
  */
60
- export declare function chooseLayout(t: ToolLayouts, asked?: string | null, projectDefault?: string | null): string;
62
+ export declare function chooseLayout(t: ToolLayouts, ...choices: (string | null | undefined)[]): string;
package/dist/layouts.js CHANGED
@@ -133,11 +133,12 @@ export function renderLayouts(t, comment, decls) {
133
133
  return out;
134
134
  }
135
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.
136
+ * The layout a tool draws in: the first of `choices` the tool has, else the
137
+ * tool's own default. This is the one resolver of docs/look-studio.md §7.1
138
+ * and §8, so callers pass, in this order: design mode's trial, the kept
139
+ * setting, the code's own per-use option, the tool's answers file. An id the
140
+ * tool doesn't have (a layout since removed) is passed over, never drawn.
139
141
  */
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;
142
+ export function chooseLayout(t, ...choices) {
143
+ return choices.find((id) => !!id && t.layouts.some((l) => l.id === id)) ?? t.default;
143
144
  }
@@ -0,0 +1,43 @@
1
+ import { type Library } from "./library.ts";
2
+ import type { Scheme } from "./library.ts";
3
+ /** One of the project's pages the previewer can show. */
4
+ export interface PreviewPage {
5
+ path: string;
6
+ label: string;
7
+ }
8
+ export interface LookPreviewOptions {
9
+ /** The pages to choose between; the first is shown first. Paths only, on the project's own site. */
10
+ pages: readonly PreviewPage[];
11
+ /** The page and scheme asked for (`?page=` and `?scheme=`); see lookPreviewView. */
12
+ page?: string;
13
+ scheme?: Scheme;
14
+ /** The previewer's own address, read for `?page=` and `?scheme=` instead of page and scheme. */
15
+ url?: string | URL;
16
+ /**
17
+ * True on the live site, where a page shows only its own look (the frame
18
+ * reads ?wb-look= only outside production): just the site as it is, with a
19
+ * note that the other looks show on the test site.
20
+ */
21
+ live?: boolean;
22
+ /** The project's own look, marked as the one in use. */
23
+ current?: string;
24
+ library?: Library;
25
+ /** The switch's and captions' words, in the page's language. */
26
+ words?: {
27
+ pages?: string;
28
+ schemes?: string;
29
+ light?: string;
30
+ dark?: string;
31
+ yours?: string;
32
+ inUse?: string;
33
+ open?: string;
34
+ live?: string;
35
+ };
36
+ }
37
+ /** The page and scheme a previewer address asks for, kept to the pages offered. */
38
+ export declare function lookPreviewView(url: string | URL, pages: readonly PreviewPage[]): {
39
+ page: string;
40
+ scheme: Scheme;
41
+ };
42
+ /** The previewer's HTML: a switch for the page and the scheme, then one framed page per look. */
43
+ export declare function lookPreview(o: LookPreviewOptions): string;
@@ -0,0 +1,36 @@
1
+ // The look previewer's body (docs/look-studio.md §9.1): the project's own
2
+ // pages in every look it may choose, side by side, light or dark. Each page
3
+ // is the real one, drawn by the project's Worker with ?wb-look= and
4
+ // ?wb-scheme= (previewAttributes), so its words, pictures and tools are the
5
+ // project's own. The page around it is the workbench-site recipe /admin/look.
6
+ import { LIBRARY, usable } from "./library.js";
7
+ const esc = (s) => s.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
8
+ // A page path the previewer may frame: the site's own, never another site's or a script.
9
+ const SAFE_PATH = /^\/(?!\/)[A-Za-z0-9/_.~-]*$/;
10
+ /** The page and scheme a previewer address asks for, kept to the pages offered. */
11
+ export function lookPreviewView(url, pages) {
12
+ const q = new URL(url, "https://x.invalid").searchParams;
13
+ const own = pages.filter((p) => SAFE_PATH.test(p.path));
14
+ const page = own.find((p) => p.path === q.get("page"))?.path ?? own[0]?.path ?? "/";
15
+ return { page, scheme: q.get("scheme") === "dark" ? "dark" : "light" };
16
+ }
17
+ /** The previewer's HTML: a switch for the page and the scheme, then one framed page per look. */
18
+ export function lookPreview(o) {
19
+ const w = { pages: "Page", schemes: "Show", light: "Light", dark: "Dark", yours: "Your site now", inUse: "In use", open: "Open on its own", live: "The other looks show on your test site.", ...o.words };
20
+ const pages = o.pages.filter((p) => SAFE_PATH.test(p.path));
21
+ const asked = o.url !== undefined ? lookPreviewView(o.url, pages) : o;
22
+ const page = pages.find((p) => p.path === asked.page)?.path ?? pages[0]?.path ?? "/";
23
+ const scheme = asked.scheme === "dark" ? "dark" : "light";
24
+ const query = (p, s) => `?page=${encodeURIComponent(p)}&amp;scheme=${s}`;
25
+ const pageSwitch = pages.length > 1 ? `<nav class="wb-ui-look-switch" aria-label="${esc(w.pages)}">${pages.map((p) => `<a class="wb-ui-look-choice" href="${query(p.path, scheme)}"${p.path === page ? ' aria-current="page"' : ""}>${esc(p.label)}</a>`).join("")}</nav>` : "";
26
+ const schemeSwitch = `<nav class="wb-ui-look-switch" aria-label="${esc(w.schemes)}">${["light", "dark"].map((s) => `<a class="wb-ui-look-choice" href="${query(page, s)}"${s === scheme ? ' aria-current="page"' : ""}>${esc(w[s])}</a>`).join("")}</nav>`;
27
+ const sep = page.includes("?") ? "&amp;" : "?";
28
+ const figure = (title, about, src, inUse) => `<figure class="wb-ui-look"><figcaption class="wb-ui-look-caption"><span class="wb-ui-look-title">${esc(title)}</span>${inUse ? `<span class="wb-ui-look-in-use">${esc(w.inUse)}</span>` : ""}<span class="wb-ui-look-about">${esc(about)}</span></figcaption><iframe class="wb-ui-look-frame" src="${src}" title="${esc(title)}" loading="lazy" sandbox="allow-same-origin"></iframe><a class="wb-ui-look-open" href="${src}" target="_blank" rel="noopener">${esc(w.open)}</a></figure>`;
29
+ const looks = o.live ? [] : usable((o.library ?? LIBRARY).looks);
30
+ const figures = [
31
+ figure(w.yours, "", esc(page), false),
32
+ ...looks.map((l) => figure(l.title, l.feel, `${esc(page)}${sep}wb-look=${esc(l.id)}&amp;wb-scheme=${scheme}`, l.id === o.current)),
33
+ ];
34
+ const live = o.live ? `<p class="wb-ui-look-live">${esc(w.live)}</p>` : "";
35
+ return `<div class="wb-ui-looks">${pageSwitch}${o.live ? "" : schemeSwitch}${live}<div class="wb-ui-look-grid">${figures.join("")}</div></div>`;
36
+ }
package/dist/theme.d.ts CHANGED
@@ -140,6 +140,36 @@ export declare function themeInput(brand: unknown, lib?: Library): {
140
140
  * guide shows both without the project changing its scheme.
141
141
  */
142
142
  export declare function renderSchemes(input: ThemeInput, lib?: Library): string;
143
+ /** Where the preview styles go, from a project's root, and their address. */
144
+ export declare const PREVIEW_FILE: {
145
+ readonly path: "public/styles/looks.css";
146
+ readonly url: "/styles/looks.css";
147
+ };
148
+ /**
149
+ * The preview styles (docs/look-studio.md §5.3): every look a project may
150
+ * choose (the package's released and testing ones, then its own), each in
151
+ * light and dark, under `:root[data-wb-look][data-wb-scheme]`. Loaded beside
152
+ * workbench.css, they show the project's real pages in any look once the
153
+ * page frame sets those attributes (previewAttributes), with nothing else
154
+ * changed. Each look is the library's, without brand.json's own colours or
155
+ * fonts: the project's own look is workbench.css itself.
156
+ */
157
+ export declare function renderLooks(lib?: Library, fontCss?: string): string;
158
+ /** The families a project's preview styles name: every choosable look's heading and body, once each. */
159
+ export declare function previewFamilies(lib?: Library): string[];
160
+ /**
161
+ * The attributes the page frame puts on `<html>` to preview a look, from the
162
+ * page's address (`?wb-look=calm&wb-scheme=dark`). Only when `allowed`: the
163
+ * caller passes true for a signed-in admin or outside production, never for
164
+ * a visitor to the live site. An id that is no look simply matches no block
165
+ * in looks.css, so nothing else is checked. Empty when there is nothing to
166
+ * preview.
167
+ */
168
+ export declare function previewAttributes(url: URL | string, allowed: boolean): {
169
+ look: string;
170
+ scheme: Scheme;
171
+ html: string;
172
+ } | null;
143
173
  /** The project's `@layer workbench.theme`: the public look on :root, the admin look on html.wb-ui-admin. */
144
174
  export declare function renderTheme(site: Theme, admin: Theme, source: string): string;
145
175
  /** 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
@@ -420,6 +420,67 @@ export function renderSchemes(input, lib = LIBRARY) {
420
420
  "",
421
421
  ].join("\n");
422
422
  }
423
+ /** Where the preview styles go, from a project's root, and their address. */
424
+ export const PREVIEW_FILE = { path: "public/styles/looks.css", url: "/styles/looks.css" };
425
+ /**
426
+ * The preview styles (docs/look-studio.md §5.3): every look a project may
427
+ * choose (the package's released and testing ones, then its own), each in
428
+ * light and dark, under `:root[data-wb-look][data-wb-scheme]`. Loaded beside
429
+ * workbench.css, they show the project's real pages in any look once the
430
+ * page frame sets those attributes (previewAttributes), with nothing else
431
+ * changed. Each look is the library's, without brand.json's own colours or
432
+ * fonts: the project's own look is workbench.css itself.
433
+ */
434
+ export function renderLooks(lib = LIBRARY, fontCss = "") {
435
+ const ids = lib.looks.filter((l) => l.status !== "withdrawn").map((l) => l.id);
436
+ const out = [
437
+ comment("Written by `npx workbench-ui styles` for previewing looks (the previewer and design mode). Don't edit."),
438
+ ...(fontCss ? [fontCss] : []),
439
+ "@layer workbench.theme {",
440
+ ];
441
+ for (const id of ids) {
442
+ for (const scheme of ["light", "dark"]) {
443
+ out.push(` :root[data-wb-look="${id}"][data-wb-scheme="${scheme}"] {`, ...declarations(resolveTheme({ look: id, scheme }, lib), " "), " }", "");
444
+ }
445
+ }
446
+ if (out.at(-1) === "")
447
+ out.pop();
448
+ out.push("}", "");
449
+ return out.join("\n");
450
+ }
451
+ /** The families a project's preview styles name: every choosable look's heading and body, once each. */
452
+ export function previewFamilies(lib = LIBRARY) {
453
+ const stacks = lib.looks.filter((l) => l.status !== "withdrawn").flatMap((l) => {
454
+ const t = resolveTheme({ look: l.id }, lib).tokens;
455
+ return [t["--wb-font-heading"], t["--wb-font"]];
456
+ });
457
+ const out = [];
458
+ for (const s of stacks) {
459
+ const m = /^\s*"([^"]+)"/.exec(s);
460
+ if (m && !out.includes(m[1]))
461
+ out.push(m[1]);
462
+ }
463
+ return out;
464
+ }
465
+ const PREVIEW_ID = /^[a-z][a-z0-9-]{0,39}$/;
466
+ /**
467
+ * The attributes the page frame puts on `<html>` to preview a look, from the
468
+ * page's address (`?wb-look=calm&wb-scheme=dark`). Only when `allowed`: the
469
+ * caller passes true for a signed-in admin or outside production, never for
470
+ * a visitor to the live site. An id that is no look simply matches no block
471
+ * in looks.css, so nothing else is checked. Empty when there is nothing to
472
+ * preview.
473
+ */
474
+ export function previewAttributes(url, allowed) {
475
+ if (!allowed)
476
+ return null;
477
+ const u = typeof url === "string" ? new URL(url) : url;
478
+ const look = u.searchParams.get("wb-look");
479
+ if (!look || !PREVIEW_ID.test(look))
480
+ return null;
481
+ const scheme = u.searchParams.get("wb-scheme") === "dark" ? "dark" : "light";
482
+ return { look, scheme, html: ` data-wb-look="${look}" data-wb-scheme="${scheme}"` };
483
+ }
423
484
  /** The project's `@layer workbench.theme`: the public look on :root, the admin look on html.wb-ui-admin. */
424
485
  export function renderTheme(site, admin, source) {
425
486
  return [
@@ -2,6 +2,7 @@
2
2
  // project's pages are built from, drawn by the parts themselves with made-up
3
3
  // words. Exported at ./samples beside ./styles.
4
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 { lookPreview } from "./look-preview.js";
5
6
  import { styleGuide } from "./style-guide.js";
6
7
  export const UI_SAMPLES = {
7
8
  format: 1,
@@ -67,5 +68,12 @@ export const UI_SAMPLES = {
67
68
  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
69
  styleGuide([], { view: "dark" }),
69
70
  },
71
+ {
72
+ id: "look-preview",
73
+ title: "The look previewer, with its pages left blank, then as the live site shows it",
74
+ // The real one frames the site's own pages; here the frames are empty, so the style guide doesn't load the site inside itself.
75
+ html: () => lookPreview({ pages: [{ path: "/", label: "Home" }, { path: "/classes", label: "Classes" }], current: "calm" }).replace(/ src="[^"]*"/g, ' src="about:blank"').replace(/ href="\/[^"]*wb-look[^"]*"/g, ' href="#"') +
76
+ lookPreview({ pages: [{ path: "/", label: "Home" }], live: true }).replace(/ src="[^"]*"/g, ' src="about:blank"'),
77
+ },
70
78
  ],
71
79
  };
package/dist/ui-styles.js CHANGED
@@ -333,5 +333,24 @@ export const UI_STYLES = {
333
333
  { name: "wb-ui-guide-layout", element: "span", about: "The tool layout a sample shows", css: "font-weight:600" },
334
334
  { name: "wb-ui-guide-frame", element: "div", about: "The sample itself, drawn on the page's own background; a part fixed to the screen (the ask button) stays inside it", css: "padding:1.25rem;contain:layout;min-height:6rem" },
335
335
  { name: "wb-ui-guide-empty", element: "p", about: "Said when no tool has samples yet", css: `color:${v("muted")}` },
336
+ // The look previewer (docs/look-studio.md §9.1): the project's own pages in every look.
337
+ { name: "wb-ui-looks", element: "div", about: "The look previewer: the site's pages in every look", css: "display:grid;gap:1rem" },
338
+ { name: "wb-ui-look-switch", element: "nav", about: "A switch: which page, or light and dark", css: "display:flex;flex-wrap:wrap;gap:.5rem" },
339
+ {
340
+ name: "wb-ui-look-choice",
341
+ element: "a",
342
+ about: "One choice in a switch",
343
+ css: `padding:.3rem .8rem;border:1px solid ${v("line")};border-radius:999px;color:${v("text")};text-decoration:none`,
344
+ states: [{ selector: "[aria-current=page]", about: "The choice shown now", css: `background:${v("accent")};border-color:${v("accent")};color:${v("accent-text")}` }, { selector: ":focus-visible", about: "Has focus", css: FOCUS }],
345
+ },
346
+ { name: "wb-ui-look-live", element: "p", about: "On the live site: where the other looks show", css: `margin:0;color:${v("muted")}` },
347
+ { name: "wb-ui-look-grid", element: "div", about: "The looks side by side, as many as fit", css: "display:grid;grid-template-columns:repeat(auto-fill,minmax(min(100%,22rem),1fr));gap:1rem" },
348
+ { name: "wb-ui-look", element: "figure", about: "One look: its name and the page drawn in it", css: `margin:0;display:grid;border:1px solid ${v("line")};border-radius:${v("corner")};overflow:hidden` },
349
+ { name: "wb-ui-look-caption", element: "figcaption", about: "The look's name and feel", css: `display:flex;flex-wrap:wrap;gap:.25rem .5rem;align-items:baseline;padding:.5rem .9rem;border-bottom:1px solid ${v("line")}` },
350
+ { name: "wb-ui-look-title", element: "span", about: "The look's name", css: "font-weight:700" },
351
+ { name: "wb-ui-look-in-use", element: "span", about: "Marks the look the site uses now", css: `font-size:.8rem;padding:.05rem .5rem;border-radius:999px;background:${v("accent")};color:${v("accent-text")}` },
352
+ { name: "wb-ui-look-about", element: "span", about: "How the look feels", css: `flex-basis:100%;font-size:.85rem;color:${v("muted")}` },
353
+ { name: "wb-ui-look-frame", element: "iframe", about: "The page itself, in the look", css: "display:block;width:100%;height:26rem;border:0;background:#fff" },
354
+ { name: "wb-ui-look-open", element: "a", about: "Opens the page in the look on its own", css: `padding:.5rem .9rem;font-size:.85rem;color:${v("accent")}`, states: [{ selector: ":focus-visible", about: "Has focus", css: FOCUS }] },
336
355
  ],
337
356
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mccaskie-labs/workbench-ui",
3
- "version": "0.13.1",
3
+ "version": "0.13.3",
4
4
  "description": "Workbench pages: one dark look, a top bar with the environment tag, cards, outcome pills, and one format for times, money and durations.",
5
5
  "license": "UNLICENSED",
6
6
  "type": "module",