ShtukbaseStandard
package file→ .shtukbase/packages/keer-ui/tools/tokens-page.ts

tools/tokens-page.ts

83 lines, 4.4 KB.

  1. // Builds keer-style/pages/design/keer-style/tokens.html from tokens.css (groups of the first block).
  2. // Usage: bun tokens-page.ts <dir with tokens.css>
  3. import { readFileSync, writeFileSync } from 'node:fs';
  4. import { join } from 'node:path';
  5. const dir = process.argv[2];
  6. const css = readFileSync(join(dir, 'tokens.css'), 'utf8');
  7. const first = css.slice(0, css.indexOf('[data-theme="grain"]', css.indexOf(':root')));
  8. type G = { id: string; title: string; names: string[] };
  9. const groups: G[] = [];
  10. for (const line of first.split('\n')) {
  11. const g = line.match(/@group\s+(\S+)\s*\|\s*([^*]+?)\s*\*\//);
  12. if (g) { groups.push({ id: g[1], title: g[2], names: [] }); continue; }
  13. const d = line.match(/^\s*(--ds-[a-z0-9-]+)\s*:/);
  14. if (d && groups.length) groups[groups.length - 1].names.push(d[1]);
  15. }
  16. const esc = (s: string) => s.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  17. function sample(gid: string, n: string): string {
  18. if (gid.startsWith('colour/') && !/rgb$|alpha$|-sheen$|blur|width|opacity|size|offset|gradient-angle/.test(n))
  19. return `<div class="ks-swatch" style="background: var(${n})"></div>`;
  20. if (gid === 'radius') return `<div class="tk-box" style="border-radius: var(${n})"></div>`;
  21. if (gid === 'shadow') return `<div class="tk-box" style="box-shadow: var(${n})"></div>`;
  22. if (gid === 'space') return `<div class="tk-bar" style="width: var(${n})"></div>`;
  23. if (gid === 'type/scale' && n.startsWith('--ds-font-') && n.endsWith('-size')) return `<div class="tk-type" style="font-size: var(${n})">Aa Bb 123</div>`;
  24. if (gid === 'type/family') return `<div class="tk-type" style="font-family: var(${n})">The quick brown fox jumps over the lazy dog</div>`;
  25. return '';
  26. }
  27. const sections = groups.filter((g) => g.names.length).map((g) => `
  28. <section class="ks-section" id="${g.id.replace(/\//g, '-')}">
  29. <h2 class="ks-section-title">${esc(g.title)} <span class="ks-caption ks-mono">${g.id}</span></h2>
  30. <div class="tk-grid">${g.names.map((n) => `
  31. <div class="tk-item">${sample(g.id, n)}<div class="ks-mono">${n}</div><div class="ks-caption tk-val" data-var="${n}"></div></div>`).join('')}
  32. </div>
  33. </section>`).join('');
  34. const html = `<!doctype html>
  35. <html lang="en">
  36. <head>
  37. <meta charset="utf-8">
  38. <meta name="viewport" content="width=device-width, initial-scale=1">
  39. <title>keer-style — tokens</title>
  40. <link rel="stylesheet" href="tokens.css">
  41. <link rel="stylesheet" href="elements.css">
  42. <style>
  43. .tk-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: var(--ds-space-3); }
  44. .tk-item { display: flex; flex-direction: column; gap: var(--ds-space-1); min-width: 0; padding: var(--ds-space-3); background: var(--ds-bg-raised); border-radius: var(--ds-radius-lg); box-shadow: var(--ds-shadow-hair); }
  45. .tk-item .ks-mono, .tk-val { overflow-wrap: anywhere; }
  46. .tk-box { height: var(--ds-space-12); background: var(--ds-bg-raised); border-radius: var(--ds-radius-lg); box-shadow: var(--ds-shadow-hair); }
  47. .tk-bar { height: var(--ds-space-2); background: var(--ds-accent); border-radius: var(--ds-radius-round); }
  48. .tk-type { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  49. </style>
  50. </head>
  51. <body class="ks-page">
  52. <main class="ks-layout">
  53. <header class="ks-page-header">
  54. <div>
  55. <div class="ks-overline">keer-style</div>
  56. <h1 class="ks-page-title">Tokens</h1>
  57. <p>All style values from tokens.css: group, name, value in the chosen theme.</p>
  58. </div>
  59. <div class="ks-tabs" role="tablist" aria-label="Theme">
  60. <button role="tab" aria-selected="true" data-theme-pick="">Draft</button>
  61. <button role="tab" aria-selected="false" data-theme-pick="grain">Grain</button>
  62. </div>
  63. </header>${sections}
  64. </main>
  65. <script>
  66. function fill() {
  67. const cs = getComputedStyle(document.documentElement);
  68. for (const el of document.querySelectorAll('[data-var]')) el.textContent = cs.getPropertyValue(el.dataset.var).trim().replace(/\\s+/g, ' ');
  69. }
  70. for (const b of document.querySelectorAll('[data-theme-pick]')) b.addEventListener('click', () => {
  71. if (b.dataset.themePick) document.documentElement.dataset.theme = b.dataset.themePick; else delete document.documentElement.dataset.theme;
  72. for (const x of document.querySelectorAll('[data-theme-pick]')) x.setAttribute('aria-selected', String(x === b));
  73. fill();
  74. });
  75. fill();
  76. </script>
  77. </body>
  78. </html>
  79. `;
  80. writeFileSync(join(dir, 'tokens.html'), html);
  81. console.log(groups.map((g) => `${g.id}:${g.names.length}`).join(' '));