файл пакета→ .shtukbase/packages/shtukbase-ui-react/tools/demo.ts
tools/demo.ts
45 строк, 2.9 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // A static demo page of the React library: every component in every state, drawn by React on the server and styled with
- // the CSS of the HTML library and the tokens of a style. A maintainer's tool: the page is for a screenshot next to the
- // demo page of shtukbase-ui-html (proof that the React markup draws the same look); nothing here is placed.
- // Usage, from the package folder:
- // bun --preload ./test/setup.ts tools/demo.ts <out.html> <ui.css> [tokens.css ...]
- // The CSS paths are written as given, so give them relative to the output file or as file:// URLs.
- import { writeFileSync } from 'node:fs';
- import { libraryComponents, load, renderState } from '../test/lib.ts';
- const [out, ...css] = process.argv.slice(2);
- if (!out || css.length === 0) {
- console.error('usage: bun --preload ./test/setup.ts tools/demo.ts <out.html> <ui.css> [tokens.css ...]');
- process.exit(1);
- }
- const [ui, ...tokens] = css;
- const ORDER = ['primitives', 'composites', 'blocks', 'shell', 'pages'];
- const esc = (s: string) => s.replace(/&/g, '&').replace(/</g, '<');
- const sections: string[] = [];
- const comps = libraryComponents().sort((a, b) => ORDER.indexOf(a.dir) - ORDER.indexOf(b.dir) || (a.id < b.id ? -1 : 1));
- for (const { id } of comps) {
- const c = await load(id);
- const cells = c.states.names.map((name) => `<figure><figcaption>${esc(name)}</figcaption><div class="cell">${renderState(c, name)}</div></figure>`);
- sections.push(`<section><h2>${esc(c.header.title)} <small>${esc(c.header.level)} · ${esc(id)}</small></h2><p>${esc(c.header.description)}</p><div class="states">${cells.join('')}</div></section>`);
- }
- const page = `<!doctype html>
- <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
- <title>shtukbase-ui-react demo</title>
- ${[...tokens, ui].map((h) => `<link rel="stylesheet" href="${h}">`).join('\n')}
- <style>
- body{margin:0;padding:28px;background:var(--background,#fff);color:var(--foreground,#191919);font:14px/1.45 var(--font-sans,system-ui,sans-serif)}
- h1{font:600 24px/1.2 var(--font-display,var(--font-sans,system-ui));margin:0 0 4px}
- section{margin:28px 0}h2{font:600 17px/1.2 var(--font-sans,system-ui);margin:0}h2 small{font-weight:500;color:var(--muted-foreground,#777);margin-left:6px}
- p{margin:2px 0 12px;color:var(--muted-foreground,#777)}
- .states{display:flex;flex-wrap:wrap;gap:14px 22px;align-items:flex-start}
- figure{margin:0;min-width:0}figcaption{font:600 10px/1 var(--font-sans,system-ui);letter-spacing:.06em;text-transform:uppercase;color:var(--muted-foreground,#999);margin:0 0 8px}
- .cell{display:flex;flex-wrap:wrap;gap:8px;align-items:flex-start;max-width:900px}
- </style></head><body>
- <h1>shtukbase-ui-react</h1><p>The React components, drawn on the server, with the CSS of shtukbase-ui-html.</p>
- ${sections.join('\n')}
- </body></html>
- `;
- writeFileSync(out, page);
- console.log(`${comps.length} components written to ${out}`);