ShtukbaseStandard
package file→ .shtukbase/packages/shtukbase-ui-html/lib/build.ts

lib/build.ts

88 lines, 4.6 KB.

  1. // What `build` writes: ui.css (every component's CSS joined, in level then id order) and demo.html (every component in
  2. // every state and control value, and a package map made of the components). Pure: text in, text out.
  3. import { readFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. import { type Component, type Control, LIB } from './components.ts';
  6. export function uiCss(root: string, comps: Component[]): string {
  7. const parts = [
  8. `/* ${LIB}/ui.css: every component's CSS joined, built by \`shtuk run ${LIB} build\`. Do not edit: edit the component's\n` +
  9. ` own .css under components/ and build again. Needs a style's tokens.css (the token contract v1) before it. */\n`,
  10. ];
  11. for (const c of comps) {
  12. if (!c.css) continue;
  13. parts.push(`\n/* ---- ${c.header.level}/${c.header.id} ---- */\n${readFileSync(join(root, c.css), 'utf8').trim()}\n`);
  14. }
  15. return parts.join('');
  16. }
  17. /** The root element of a template with one attribute set (null value: removed). */
  18. export function setAttr(markup: string, name: string, value: string | null): string {
  19. const m = /^<([a-z][a-z0-9-]*)\b([^>]*?)(\/?)>/i.exec(markup);
  20. if (!m) return markup;
  21. let attrs = m[2].replace(new RegExp(`\\s${name}(="[^"]*")?(?=[\\s/>]|$)`), '');
  22. if (value !== null) attrs += value === '' ? ` ${name}` : ` ${name}="${value}"`;
  23. return `<${m[1]}${attrs}${m[3]}>` + markup.slice(m[0].length);
  24. }
  25. /** The root element with one custom property set in its style attribute. */
  26. export function setProp(markup: string, prop: string, value: string): string {
  27. const m = /^<([a-z][a-z0-9-]*)\b([^>]*?)>/i.exec(markup);
  28. if (!m) return markup;
  29. const style = /\sstyle="([^"]*)"/.exec(m[2]);
  30. const decls = (style?.[1] ?? '').split(';').map((s) => s.trim()).filter((s) => s && !s.startsWith(`${prop}:`));
  31. decls.push(`${prop}: ${value}`);
  32. const attrs = (style ? m[2].replace(style[0], '') : m[2]) + ` style="${decls.join('; ')}"`;
  33. return `<${m[1]}${attrs}>` + markup.slice(m[0].length);
  34. }
  35. /** Card presets are bundles: the layout and size each preset goes with (the mock's PRESETS). */
  36. export const PRESET_BUNDLES: Record<string, { layout: string; size: string }> = {
  37. base: { layout: 'side', size: 'm' },
  38. compact: { layout: 'side', size: 's' },
  39. editorial: { layout: 'side', size: 'l' },
  40. cover: { layout: 'top', size: 'm' },
  41. tiles: { layout: 'badge', size: 'm' },
  42. };
  43. /** Controls the demo and the overflow test leave out: the grid's preset only sizes its columns, the cards keep their own. */
  44. export const CONTROL_SKIP: Record<string, string[]> = { 'package-grid': ['preset', 'layout', 'size'] };
  45. /** The picture of an icon button: an icon in place of the text, the text as its accessible name. */
  46. const PLUS = '<svg viewBox="0 0 16 16" aria-hidden="true"><path d="M8 3.5v9M3.5 8h9"/></svg>';
  47. function iconButton(markup: string): string {
  48. const m = /^(<button\b[^>]*?)>([^<]*)<\/button>/.exec(markup);
  49. return m ? `${m[1]} aria-label="${m[2].trim()}">${PLUS}</button>` + markup.slice(m[0].length) : markup;
  50. }
  51. /** One picture per value of a control: [label, markup]. */
  52. export function variants(c: Component, name: string, ctl: Control): [string, string][] {
  53. const id = c.header.id;
  54. let base = c.templates.default ?? Object.values(c.templates)[0] ?? '';
  55. if (id === 'package-card' && name === 'cover') base = setAttr(setAttr(base, 'data-preset', 'cover'), 'data-layout', 'top');
  56. if (id === 'package-card' && name.startsWith('glow')) base = setAttr(setAttr(base, 'data-fill', 'faint'), 'data-glow', name === 'glow' ? null : 'under');
  57. if (id === 'package-card' && name === 'glow') base = setAttr(base, 'data-fill', 'faint');
  58. const one = (v: string): string => {
  59. let m = setAttr(base, `data-${name}`, v);
  60. if (id === 'package-card' && name === 'preset' && PRESET_BUNDLES[v])
  61. m = setAttr(setAttr(m, 'data-layout', PRESET_BUNDLES[v].layout), 'data-size', PRESET_BUNDLES[v].size);
  62. if (id === 'button' && name === 'size' && v === 'icon') m = iconButton(m);
  63. return m;
  64. };
  65. switch (ctl.kind) {
  66. case 'select':
  67. case 'segment':
  68. return (ctl.options ?? []).map((o) => [String(o), one(String(o))]);
  69. case 'swatch':
  70. return (ctl.options ?? []).filter((_, i) => i % 3 === 0).map((o) => [String(o), one(String(o))]);
  71. case 'bool':
  72. return [['off', setAttr(base, `data-${name}`, null)], ['on', setAttr(base, `data-${name}`, '')]];
  73. case 'range': {
  74. const vals = [...new Set([ctl.min, ctl.default, ctl.max].filter((v) => v !== undefined))] as number[];
  75. return vals.map((v) => [`${v}${ctl.unit ?? ''}`, setProp(base, `--${id}-${name}`, `${v}${ctl.unit ?? ''}`)]);
  76. }
  77. default:
  78. return [];
  79. }
  80. }