ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-react/tools/demo.ts

tools/demo.ts

45 lines, 2.9 KB.

  1. // A static demo page of the React library: every component in every state, drawn by React on the server and styled with
  2. // 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
  3. // demo page of shtukbase-ui-html (proof that the React markup draws the same look); nothing here is placed.
  4. // Usage, from the package folder:
  5. // bun --preload ./test/setup.ts tools/demo.ts <out.html> <ui.css> [tokens.css ...]
  6. // The CSS paths are written as given, so give them relative to the output file or as file:// URLs.
  7. import { writeFileSync } from 'node:fs';
  8. import { libraryComponents, load, renderState } from '../test/lib.ts';
  9. const [out, ...css] = process.argv.slice(2);
  10. if (!out || css.length === 0) {
  11. console.error('usage: bun --preload ./test/setup.ts tools/demo.ts <out.html> <ui.css> [tokens.css ...]');
  12. process.exit(1);
  13. }
  14. const [ui, ...tokens] = css;
  15. const ORDER = ['primitives', 'composites', 'blocks', 'shell', 'pages'];
  16. const esc = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  17. const sections: string[] = [];
  18. const comps = libraryComponents().sort((a, b) => ORDER.indexOf(a.dir) - ORDER.indexOf(b.dir) || (a.id < b.id ? -1 : 1));
  19. for (const { id } of comps) {
  20. const c = await load(id);
  21. const cells = c.states.names.map((name) => `<figure><figcaption>${esc(name)}</figcaption><div class="cell">${renderState(c, name)}</div></figure>`);
  22. 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>`);
  23. }
  24. const page = `<!doctype html>
  25. <html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1">
  26. <title>shtukbase-ui-react demo</title>
  27. ${[...tokens, ui].map((h) => `<link rel="stylesheet" href="${h}">`).join('\n')}
  28. <style>
  29. body{margin:0;padding:28px;background:var(--background,#fff);color:var(--foreground,#191919);font:14px/1.45 var(--font-sans,system-ui,sans-serif)}
  30. h1{font:600 24px/1.2 var(--font-display,var(--font-sans,system-ui));margin:0 0 4px}
  31. 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}
  32. p{margin:2px 0 12px;color:var(--muted-foreground,#777)}
  33. .states{display:flex;flex-wrap:wrap;gap:14px 22px;align-items:flex-start}
  34. 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}
  35. .cell{display:flex;flex-wrap:wrap;gap:8px;align-items:flex-start;max-width:900px}
  36. </style></head><body>
  37. <h1>shtukbase-ui-react</h1><p>The React components, drawn on the server, with the CSS of shtukbase-ui-html.</p>
  38. ${sections.join('\n')}
  39. </body></html>
  40. `;
  41. writeFileSync(out, page);
  42. console.log(`${comps.length} components written to ${out}`);