ShtukbaseStandard
package page→ /design/components.html

pages/design/components.html

406 lines, 24.9 KB. Open as is (in a project other files lie next to it; they may be missing here).

  1. <!--
  2. ---
  3. type: Lens
  4. title: "Components"
  5. icon: "🧩"
  6. description: "The component catalog of the project's libraries: the chosen component on the stage; the right panel holds the components by level, its states and its settings."
  7. template: custom
  8. layout: catalog
  9. lensType: catalog
  10. search: true
  11. sources: ["/design/components.md", "/design/components/*.md", "/design/<library>/components/**"]
  12. generator: "shtuk run design-components sync"
  13. ---
  14. The component catalog of `design-lenses`, read from `lib/design/components.data.js` (`shtuk run design-components sync`).
  15. The right panel holds three sections: Components (the tree by level, built of the library's tree items), States (any state or data state, or all side by side) and Settings (every control).
  16. -->
  17. <!doctype html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="utf-8">
  21. <meta name="viewport" content="width=device-width, initial-scale=1">
  22. <title>Components</title>
  23. <link rel="stylesheet" href="../lib/kit.css">
  24. <link rel="stylesheet" href="../lib/explore.css">
  25. <script src="../lib/data.js"></script>
  26. <script src="../lib/design/style.data.js"></script>
  27. <script src="../lib/design/components.data.js"></script>
  28. <script src="../lib/explore.js"></script>
  29. <script>
  30. // The chosen style's CSS goes in before the kit's CSS, as in the style specimen: the lens wears the chosen style.
  31. (function () {
  32. const S = window.SB_STYLE, kit = document.querySelector('link[href$="lib/kit.css"]');
  33. if (!S || !kit) return;
  34. const root = new URL("../", kit.href).href;
  35. for (const p of S.css || []) {
  36. const l = document.createElement("link");
  37. l.rel = "stylesheet"; l.href = root + p.replace(/^\//, ""); l.dataset.style = S.id;
  38. kit.before(l);
  39. }
  40. })();
  41. </script>
  42. <script src="../lib/kit.js"></script>
  43. <style>
  44. /* The catalog: the stage fills the page (the shell's layout-catalog); the component tree is a section of the right panel,
  45. built of the library's tree items. No cards, no frames: the component stands on the white page; regions are separated
  46. by spacing and one hairline. Contract names only. */
  47. .cc { --cc-fg: var(--foreground, #191919); --cc-mute: var(--muted-foreground, #6f6e69); --cc-line: var(--border, #e9e9e7);
  48. --cc-row: var(--sidebar-accent, #f1f1ef);
  49. min-height: 100%; color: var(--cc-fg); box-sizing: border-box; }
  50. .cc *, .cc *::before, .cc *::after { box-sizing: border-box; }
  51. /* The tree scrolls with the States and Settings in the panel body. */
  52. .cc-tree { display: flex; flex-direction: column; gap: 1px; margin: 0 -8px; }
  53. .cc-tree [hidden] { display: none; }
  54. .cc-override { width: 6px; height: 6px; flex: none; border-radius: 50%; background: var(--primary); }
  55. .cc-stage { min-width: 0; padding: 48px 56px 96px; }
  56. .cc-comp { max-width: 960px; }
  57. .cc-comp h1 { margin: 0; font-size: 26px; line-height: 1.2; font-weight: 650; letter-spacing: -.015em; }
  58. .cc-desc { margin: 8px 0 0; max-width: 640px; color: var(--cc-mute); font-size: 14px; line-height: 1.5; }
  59. .cc-links { display: flex; flex-wrap: wrap; gap: 4px 16px; margin-top: 12px; font-size: 13px; color: var(--cc-mute); }
  60. .cc-links a { color: inherit; text-decoration: none; cursor: pointer; }
  61. .cc-links a:hover { color: var(--cc-fg); text-decoration: underline; text-underline-offset: 3px; }
  62. .cc-links .rel button { all: unset; cursor: pointer; color: var(--cc-fg); }
  63. .cc-links .rel button:hover { text-decoration: underline; text-underline-offset: 3px; }
  64. .cc-view { margin-top: 32px; border-top: 1px solid var(--cc-line); padding-top: 8px; }
  65. .cc-view iframe { display: block; width: 100%; border: 0; height: 240px; background: transparent; color-scheme: normal; }
  66. .cc-none { color: var(--cc-mute); font-size: 13.5px; padding: 24px 0; }
  67. .cc-warn { color: var(--destructive, #e85339); font-size: 13.5px; padding: 24px; }
  68. /* All components: a gallery of tiles, a picture and the name under it */
  69. .cc-gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 28px 24px; margin-top: 32px; }
  70. .cc-tile { all: unset; cursor: pointer; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
  71. .cc-pic { height: 130px; overflow: hidden; border-radius: 8px; box-shadow: inset 0 0 0 1px var(--cc-line); margin-bottom: 8px; display: flex; align-items: center; justify-content: center; }
  72. .cc-pic .cc-view { margin: 0; border: 0; padding: 0; width: 100%; }
  73. .cc-pic iframe { max-height: 130px; }
  74. .cc-tile:hover .cc-pic { background: var(--cc-row); }
  75. .cc-tile .t { font-size: 14px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  76. .cc-tile .l { font-size: 12.5px; color: var(--cc-mute); }
  77. .cc-tile:focus-visible { outline: 2px solid var(--ring, #e04a72); outline-offset: 4px; border-radius: 8px; }
  78. /* the States section of the right panel: a list of rows, the chosen one grey */
  79. .cc-states { display: flex; flex-direction: column; gap: 1px; margin: 0 -8px; }
  80. .cc-states .g { font-size: 12px; color: var(--cc-mute, #6f6e69); padding: 8px 8px 3px; }
  81. .cc-states .g:first-child { padding-top: 0; }
  82. .cc-states button { all: unset; display: flex; align-items: center; height: 28px; padding: 0 8px; border-radius: 6px; font-size: 13.5px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  83. .cc-states button:hover, .cc-states button.on { background: var(--sidebar-accent, #f1f1ef); }
  84. .cc-states button.on { font-weight: 500; }
  85. .cc-plain-row { display: grid; grid-template-columns: 96px minmax(0, 1fr); align-items: center; gap: 8px; margin: 6px 0; font-size: 13px; }
  86. .cc-plain-row select { width: 100%; }
  87. @media (max-width: 900px) { .cc-stage { padding: 32px 24px 80px; } }
  88. </style>
  89. <script>
  90. (function () {
  91. const { h } = Lens;
  92. const LEVELS = [["primitive", "Primitives"], ["composite", "Composites"], ["block", "Blocks"], ["shell", "Shell"], ["page", "Pages"]];
  93. const href = (p) => Lens.rootHref + String(p).replace(/^\//, "");
  94. const esc = (s) => String(s).replace(/[&<>"]/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" })[c]);
  95. const stateKeys = (comp) => {
  96. const keys = [...(comp.states || []), ...(comp.status || [])].filter((k, i, a) => a.indexOf(k) === i);
  97. for (const k of Object.keys((comp.html && comp.html.templates) || {})) if (!keys.includes(k)) keys.push(k);
  98. return keys;
  99. };
  100. /** The CSS a frame links: the chosen style's files, then the library's own (style files of any installed style are left out). */
  101. function frameCss(C, S, styleId) {
  102. const styles = (S && S.styles) || [];
  103. const chosen = styles.find((s) => s.id === styleId) || (S && S.id === styleId ? { css: S.css } : null);
  104. const isStyleFile = (p) => styles.some((s) => p.startsWith("/design/" + s.id + "/")) || (S && p.startsWith("/design/" + S.id + "/"));
  105. const own = (C.css || []).filter((p) => !isStyleFile(p));
  106. return chosen ? [...chosen.css, ...own] : C.css || [];
  107. }
  108. /** The document of a frame: the chosen state alone and large, or every state side by side with its name under it. */
  109. function frameDoc(comp, css, theme, state) {
  110. const T = (comp.html && comp.html.templates) || {};
  111. const thumb = state === "thumb";
  112. const keys = state === "all" ? stateKeys(comp) : [state in T ? state : stateKeys(comp)[0]];
  113. const one = keys.length === 1 && state !== "all";
  114. const cells = keys.map((k) => {
  115. const body = T[k] != null ? T[k] : '<span class="sb-missing">no template</span>';
  116. return '<figure class="sb-cell" data-key="' + esc(k) + '"><div class="sb-stage">' + body + "</div>" + (one ? "" : "<figcaption>" + esc(k) + "</figcaption>") + "</figure>";
  117. }).join("");
  118. return '<!doctype html><html lang="en" data-theme="' + theme + '"' + (theme === "dark" ? ' class="dark"' : "") + '><head><meta charset="utf-8"><base href="' + esc(Lens.rootHref) + '">'
  119. + css.map((p) => '<link rel="stylesheet" href="' + esc(p.replace(/^\//, "")) + '">').join("")
  120. + "<style>html,body{margin:0;background:var(--background,#fff);color:var(--foreground,#191919);font-family:var(--font-sans,system-ui,sans-serif)}"
  121. + "body{padding:" + (thumb ? "20px" : one ? "56px 24px" : "28px 2px 8px") + "}" + (thumb ? "html,body{background:transparent}body{zoom:.8;pointer-events:none}" : "")
  122. + ".sb-cells{display:" + (one ? "flex;justify-content:center" : "grid;grid-template-columns:repeat(auto-fill,minmax(min(100%," + (comp.level === "primitive" ? "160px" : "300px") + "),1fr));gap:40px 48px;align-items:start") + "}"
  123. + ".sb-cell{margin:0;display:flex;flex-direction:column;gap:12px;min-width:0;max-width:100%}"
  124. + ".sb-stage{min-height:40px;display:flex;flex-direction:column;justify-content:center;" + (one ? "align-items:center;" : "align-items:stretch;") + "max-width:100%}"
  125. + ".sb-stage>*{max-width:100%}" + (comp.level === "primitive" ? ".sb-stage{align-items:flex-start}" : "") + (one ? ".sb-cells>.sb-cell{width:" + (comp.level === "primitive" ? "auto" : "min(100%,420px)") + "}" : "")
  126. + "figcaption{font:12px/1.2 var(--font-mono,ui-monospace,monospace);color:var(--muted-foreground,#6f6e69)}"
  127. + ".sb-missing{font-size:12px;color:var(--muted-foreground,#6f6e69)}</style></head><body><div class=\"sb-cells\">" + cells + "</div></body></html>";
  128. }
  129. /** A control value on a component root (build contract, section 4). */
  130. function applyControls(doc, comp, values) {
  131. if (!doc) return;
  132. const roots = [...doc.querySelectorAll(".sb-stage")].map((s) => s.querySelector('[data-slot="' + comp.id + '"]') || s.firstElementChild).filter(Boolean);
  133. for (const [k, c] of Object.entries(comp.controls || {})) {
  134. if (!(k in values)) continue;
  135. const v = values[k];
  136. for (const r of roots) {
  137. if (c.kind === "range") r.style.setProperty("--" + comp.id + "-" + k, v + (c.unit || ""));
  138. else if (c.kind === "bool") v === true || v === "true" ? r.setAttribute("data-" + k, "") : r.removeAttribute("data-" + k);
  139. else r.setAttribute("data-" + k, String(v));
  140. }
  141. }
  142. }
  143. Lens.templates.custom = function (cfg) {
  144. const ui = Lens.page(Object.assign({ layout: "catalog" }, cfg));
  145. const C = window.SB_COMPONENTS, S = window.SB_STYLE;
  146. if (!C) {
  147. ui.main.append(h("p", { class: "cc-warn" }, "There is no component data. Build it from the project root: shtuk run design-components sync"));
  148. return;
  149. }
  150. const comps = C.components || [];
  151. const byId = Object.fromEntries(comps.map((c) => [c.id, c]));
  152. const provenance = (c) => [...new Set([c.html, c.react].filter((impl) => impl?.overrides)
  153. .map((impl) => "from " + impl.library + ", overrides " + impl.overrides))].join(" · ");
  154. const usedBy = (id) => comps.filter((c) => (c.uses || []).includes(id)).map((c) => c.id);
  155. const styleIds = ((S && S.styles) || []).map((s) => s.id);
  156. const defStyle = (S && S.id) || C.style || styleIds[0] || "";
  157. if (defStyle && !styleIds.includes(defStyle)) styleIds.unshift(defStyle);
  158. const pageTheme = () => (document.documentElement.dataset.theme === "dark" ? "dark" : "light");
  159. const look = { style: defStyle, values: {} };
  160. const params = new URLSearchParams(location.search);
  161. let current = null, state = params.get("state") || "all", settings = null;
  162. const frames = []; // {comp, iframe, state}
  163. // ---------- the frame of one component ----------
  164. function mountFrame(comp, holder, st) {
  165. if (!comp.html) { holder.append(h("p", { class: "cc-none" }, "No HTML templates: " + (comp.react ? "React only (" + comp.react.source + ")." : "not implemented yet."))); return; }
  166. const f = h("iframe", { title: comp.title, sandbox: "allow-same-origin", loading: "lazy" });
  167. const rec = { comp, iframe: f, state: st };
  168. const size = (d) => { f.style.height = Math.max(64, Math.ceil(d.documentElement.getBoundingClientRect().height)) + "px"; };
  169. const fit = () => {
  170. try {
  171. const d = f.contentDocument;
  172. if (!d || !d.body) return;
  173. if (comp.id === current) applyControls(d, comp, look.values);
  174. size(d);
  175. if (!f._ro && f.contentWindow.ResizeObserver) { f._ro = new f.contentWindow.ResizeObserver(() => size(d)); f._ro.observe(d.body); }
  176. } catch { f.style.height = "240px"; }
  177. };
  178. f.addEventListener("load", () => { f._ro = null; fit(); setTimeout(fit, 150); });
  179. f.srcdoc = frameDoc(comp, frameCss(C, S, look.style), pageTheme(), st);
  180. holder.append(h("div", { class: "cc-view" }, f));
  181. frames.push(rec);
  182. }
  183. const pageLink = (p) => (Lens.rec(p) ? h("a", { onclick: (e) => { e.preventDefault(); Lens.open(p); } }, "Page") : h("a", { href: href(p), title: p }, "Page"));
  184. const relList = (label, ids) => ids.length ? h("span", { class: "rel" }, label + " ",
  185. ids.map((x, i) => [i ? ", " : "", byId[x] ? h("button", { type: "button", onclick: () => select(x) }, byId[x].title) : x])) : null;
  186. function compView(comp, st) {
  187. const box = h("section", { class: "cc-comp", "data-id": comp.id },
  188. h("h1", {}, comp.title),
  189. comp.description ? h("p", { class: "cc-desc" }, comp.description) : null,
  190. h("div", { class: "cc-links" },
  191. provenance(comp) ? h("span", {}, provenance(comp)) : null,
  192. comp.page ? pageLink(comp.page) : null,
  193. comp.html && comp.html.source ? h("a", { href: href(comp.html.source), title: comp.html.source }, "HTML") : null,
  194. comp.html && comp.html.css ? h("a", { href: href(comp.html.css), title: comp.html.css }, "CSS") : null,
  195. comp.react && comp.react.source ? h("a", { href: href(comp.react.source), title: comp.react.source }, "React") : null,
  196. relList("Uses", comp.uses || []), relList("Used by", usedBy(comp.id))));
  197. mountFrame(comp, box, st);
  198. return box;
  199. }
  200. /** All components: a gallery, each in its first state with its name under it; a click opens it. */
  201. function overview() {
  202. const grid = h("div", { class: "cc-gallery" });
  203. for (const c of comps) {
  204. const cell = h("button", { type: "button", class: "cc-tile", title: c.description || c.title, onclick: () => select(c.id) });
  205. const pic = h("div", { class: "cc-pic" });
  206. cell.append(pic, h("span", { class: "t" }, c.title), h("span", { class: "l" }, (LEVELS.find(([x]) => x === c.level) || [c.level, c.level])[1]));
  207. mountFrame(c, pic, "thumb");
  208. grid.append(cell);
  209. }
  210. return grid;
  211. }
  212. // ---------- layout: the stage fills the page; the tree is the first section of the right panel ----------
  213. const stage = h("div", { class: "cc-stage", "data-slot": "layout-catalog-stage" });
  214. ui.main.append(h("div", { class: "cc" }, stage));
  215. // The tree is the library's tree items: "All components", then the levels as rows that fold, their components under them.
  216. const CHEV = '<svg viewBox="0 0 16 16"><path d="m6 4 4 4-4 4"/></svg>';
  217. const tree = h("div", { class: "cc-tree", role: "tree", "data-slot": "layout-catalog-tree", "aria-label": "Components by level" });
  218. const treeBody = ui.section("Components", tree);
  219. if (treeBody.setAnswer) treeBody.setAnswer(String(comps.length));
  220. const treeItem = (label, count, depth, expanded, source) => h("div", { "data-slot": "tree-item", class: "tree-item", role: "treeitem", "aria-level": String(depth + 1),
  221. "aria-selected": "false", tabindex: "-1", title: source ? label + " · " + source : label, "--tree-item-depth": String(depth), ...(expanded != null ? { "aria-expanded": String(expanded) } : {}) },
  222. h("span", { "data-slot": "tree-item-toggle", class: "tree-item-toggle", "aria-hidden": "true", ...(expanded != null ? { html: CHEV } : {}) }),
  223. h("span", { "data-slot": "tree-item-label", class: "tree-item-label" }, label),
  224. source ? h("span", { class: "cc-override", role: "img", "aria-label": source }) : null,
  225. h("span", { "data-slot": "tree-item-count", class: "tree-item-count" }, String(count)));
  226. const items = {}, lvRows = {};
  227. let query = "", openLevel = null;
  228. const matches = (c, q) => (c.title + " " + c.id + " " + (c.description || "")).toLowerCase().includes(q);
  229. function paintTree() {
  230. for (const L of Object.values(lvRows)) {
  231. const hits = L.ids.filter((id) => !query || matches(byId[id], query));
  232. const isOpen = query ? hits.length > 0 : openLevel === L.lv;
  233. L.row.hidden = !!query && !hits.length;
  234. L.row.setAttribute("aria-expanded", String(isOpen));
  235. L.row.querySelector(".tree-item-count").textContent = String(hits.length);
  236. for (const id of L.ids) items[id].hidden = !(isOpen && hits.includes(id));
  237. }
  238. const tab = tree.querySelector('.tree-item[tabindex="0"]');
  239. if (!tab || tab.hidden) {
  240. if (tab) tab.setAttribute("tabindex", "-1");
  241. (tree.querySelector('.tree-item[aria-selected="true"]:not([hidden])') || allItem).setAttribute("tabindex", "0");
  242. }
  243. }
  244. const toggleLv = (lv, v = openLevel !== lv) => { if (!query) { openLevel = v ? lv : null; paintTree(); } };
  245. const allItem = treeItem("All components", comps.length, 0, null);
  246. allItem._act = () => select("");
  247. tree.append(allItem);
  248. const levels = LEVELS.concat([...new Set(comps.map((c) => c.level))].filter((l) => !LEVELS.some(([x]) => x === l)).map((l) => [l, l]));
  249. for (const [lv, title] of levels) {
  250. const list = comps.filter((c) => c.level === lv);
  251. if (!list.length) continue;
  252. const row = treeItem(title, list.length, 0, false);
  253. row._lv = lv; row._act = () => toggleLv(lv);
  254. lvRows[lv] = { lv, row, ids: list.map((c) => c.id) };
  255. tree.append(row);
  256. for (const c of list) {
  257. const it = (items[c.id] = treeItem(c.title, stateKeys(c).length, 1, null, provenance(c)));
  258. it.hidden = true; it._parent = row; it._act = () => select(c.id);
  259. tree.append(it);
  260. }
  261. }
  262. tree.addEventListener("click", (e) => { const r = e.target.closest(".tree-item"); if (r && r._act) r._act(); });
  263. tree.addEventListener("focusin", (e) => { const r = e.target.closest(".tree-item"); if (!r) return; tree.querySelectorAll('.tree-item[tabindex="0"]').forEach((x) => x.setAttribute("tabindex", "-1")); r.setAttribute("tabindex", "0"); });
  264. tree.addEventListener("keydown", (e) => {
  265. const r = e.target.closest(".tree-item");
  266. if (!r) return;
  267. const vis = [...tree.querySelectorAll(".tree-item")].filter((x) => !x.hidden), i = vis.indexOf(r);
  268. const go = (j) => { const t = vis[Math.max(0, Math.min(vis.length - 1, j))]; if (t) { e.preventDefault(); t.focus(); } };
  269. if (e.key === "ArrowDown") go(i + 1);
  270. else if (e.key === "ArrowUp") go(i - 1);
  271. else if (e.key === "Home") go(0);
  272. else if (e.key === "End") go(vis.length - 1);
  273. else if (e.key === "Enter" || e.key === " ") { e.preventDefault(); if (r._act) r._act(); }
  274. else if (e.key === "ArrowRight" && r._lv && r.getAttribute("aria-expanded") === "false") { e.preventDefault(); toggleLv(r._lv, true); }
  275. else if (e.key === "ArrowLeft") {
  276. if (r._lv && r.getAttribute("aria-expanded") === "true") { e.preventDefault(); toggleLv(r._lv, false); }
  277. else if (r._parent) { e.preventDefault(); r._parent.focus(); }
  278. }
  279. });
  280. // ---------- the right panel: States, then Settings ----------
  281. const statesBody = ui.section("States");
  282. function drawStates() {
  283. const c = byId[current];
  284. statesBody.replaceChildren();
  285. if (statesBody.setAnswer) statesBody.setAnswer(c ? String(stateKeys(c).length) : "");
  286. const sec = statesBody.card || statesBody.closest("[data-slot=panel-section], .lx-sec");
  287. if (sec) sec.hidden = !c;
  288. if (!c) return;
  289. const row = (k, label) => h("button", { type: "button", class: state === k ? "on" : "", onclick: () => setState(k) }, label || k);
  290. const inter = (c.states || []).filter((k) => stateKeys(c).includes(k));
  291. const data = stateKeys(c).filter((k) => !inter.includes(k));
  292. statesBody.append(h("div", { class: "cc-states" }, row("all", "All, side by side"),
  293. inter.length ? [h("div", { class: "g" }, "Interaction"), inter.map((k) => row(k))] : null,
  294. data.length ? [h("div", { class: "g" }, "Data"), data.map((k) => row(k))] : null));
  295. }
  296. function setState(k) {
  297. state = k;
  298. const u = new URL(location.href);
  299. if (k === "all") u.searchParams.delete("state"); else u.searchParams.set("state", k);
  300. history.replaceState(null, "", u);
  301. drawStates();
  302. render();
  303. }
  304. let settingsHost = null;
  305. function openSettings(comp) {
  306. if (settings && settings.destroy) settings.destroy();
  307. if (settingsHost) { (settingsHost.card || settingsHost.closest("[data-slot=panel-section], .lx-sec") || settingsHost).remove(); settingsHost = null; }
  308. settings = null;
  309. look.values = {};
  310. const controls = Object.assign({}, (comp && comp.controls) || {});
  311. if (styleIds.length > 1) controls["preview-style"] = { kind: "select", options: styleIds, default: defStyle, label: "Style" };
  312. if (!comp || !Object.keys(controls).length) return;
  313. const opts = {
  314. title: "Settings",
  315. target: h("div", { hidden: true }),
  316. storageKey: "design-components:" + comp.id,
  317. sections: [{ title: "Settings", controls }],
  318. onChange: (values) => {
  319. look.values = values;
  320. const s = values["preview-style"] || defStyle;
  321. if (s !== look.style) { look.style = s; render(); return; }
  322. for (const r of frames) if (r.comp.id === comp.id) { try { applyControls(r.iframe.contentDocument, r.comp, values); } catch {} }
  323. },
  324. };
  325. if (ui.settings) { settings = ui.settings(opts); return; }
  326. // An older kit: the explore kit's panel stands in a bare block of the right panel, its title the section's.
  327. if (window.Explore && !window.Explore.standIn) {
  328. settingsHost = ui.section();
  329. settings = window.Explore.panel(Object.assign({}, opts, { sections: [{ title: "", controls }], container: settingsHost }));
  330. return;
  331. }
  332. settingsHost = ui.section("Settings");
  333. // No explore kit: plain selects with the same values.
  334. const vals = {};
  335. for (const [k, c] of Object.entries(controls)) {
  336. const o = (c.options || []).map((x) => (Array.isArray(x) ? x : [x, x]));
  337. vals[k] = c.default != null ? c.default : o.length ? o[0][0] : "";
  338. const inp = c.kind === "range" ? h("input", { type: "range", min: c.min, max: c.max, step: c.step || 1, value: vals[k] })
  339. : c.kind === "bool" ? h("input", { type: "checkbox", checked: !!vals[k] })
  340. : h("select", {}, o.map(([v, t]) => h("option", { value: v, selected: v === vals[k] }, t)));
  341. inp.addEventListener("input", () => { vals[k] = c.kind === "bool" ? inp.checked : inp.value; opts.onChange(Object.assign({}, vals)); });
  342. settingsHost.append(h("label", { class: "cc-plain-row" }, h("span", {}, c.label || k), inp));
  343. }
  344. opts.onChange(Object.assign({}, vals));
  345. }
  346. function render() {
  347. frames.length = 0;
  348. stage.replaceChildren();
  349. const c = byId[current];
  350. if (!c) {
  351. stage.append(comps.length ? h("div", { class: "cc-comp" }, h("h1", {}, "All components"),
  352. h("p", { class: "cc-desc" }, comps.length + " components by level; open one to see its states and settings."), overview())
  353. : h("p", { class: "cc-none" }, "No components in the data."));
  354. return;
  355. }
  356. stage.append(compView(c, state === "all" || stateKeys(c).includes(state) ? state : "all"));
  357. }
  358. function select(id) {
  359. if (id && !byId[id]) return;
  360. // the chosen component's level opens; the chosen row is the tree's one tab stop
  361. openLevel = id ? byId[id].level : null;
  362. allItem.setAttribute("aria-selected", String(!id));
  363. for (const [k, b] of Object.entries(items)) b.setAttribute("aria-selected", String(k === id));
  364. tree.querySelectorAll('.tree-item[tabindex="0"]').forEach((x) => x.setAttribute("tabindex", "-1"));
  365. (id ? items[id] : allItem).setAttribute("tabindex", "0");
  366. paintTree();
  367. const u = new URL(location.href);
  368. // Another component: the old one's control values and state leave the link (a shared link keeps them on the first open).
  369. if (current !== null && u.searchParams.get("component") !== id) {
  370. for (const k of [...u.searchParams.keys()]) if (!["component", "skin", "theme"].includes(k)) u.searchParams.delete(k);
  371. state = "all";
  372. }
  373. if (id) u.searchParams.set("component", id); else u.searchParams.delete("component");
  374. history.replaceState(null, "", u);
  375. current = id;
  376. if (byId[id] && state !== "all" && !stateKeys(byId[id]).includes(state)) state = "all";
  377. drawStates();
  378. openSettings(byId[id]);
  379. render();
  380. stage.scrollTop = 0;
  381. }
  382. Lens.on("search", (q) => {
  383. query = q || "";
  384. if (!query) openLevel = byId[current]?.level ?? null;
  385. paintTree();
  386. });
  387. // The frames follow the page's theme.
  388. new MutationObserver(() => render()).observe(document.documentElement, { attributes: true, attributeFilter: ["data-theme"] });
  389. const want = params.get("component");
  390. select(byId[want] ? want : want === "" || params.get("view") === "all" ? "" : comps[0] ? comps[0].id : "");
  391. };
  392. })();
  393. </script>
  394. </head>
  395. <body><p class="lx-missing">The lens "Components". There is no data yet: build it from the project root with <code>shtuk run lenses build</code> and <code>shtuk run design-components sync</code>.</p></body>
  396. </html>