@lionlang/ui 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/dom.mjs ADDED
@@ -0,0 +1,679 @@
1
+ import { isUiNode } from "./types.mjs";
2
+ //#region src/dom.ts
3
+ /**
4
+ * Zero-dependency, XSS-safe DOM renderer and in-place patcher for Lion UI
5
+ * trees (`@lionlang/ui/dom`).
6
+ *
7
+ * Every string is inserted via `textContent` (never `innerHTML`). Interactive
8
+ * controls (`input`, `number`, `slider`, `select`, `toggle`, `button`) keep
9
+ * their DOM elements and focus across re-renders by matching on `data-ui-kind`
10
+ * and `data-ui-key`.
11
+ *
12
+ * Styling is fully configurable:
13
+ * - Import `@lionlang/ui/styles.css` and override `--lion-ui-*` CSS variables
14
+ * (or set `theme: "light" | "dark"`), OR
15
+ * - Omit the default stylesheet and pass `classes: { card: "...", ... }` for
16
+ * Tailwind / CSS Modules / custom design systems.
17
+ */
18
+ const ACTION_MAP = /* @__PURE__ */ new WeakMap();
19
+ const WIRED = /* @__PURE__ */ new WeakSet();
20
+ const OPEN_SELECTS = /* @__PURE__ */ new Set();
21
+ const OUTSIDE_WIRED = /* @__PURE__ */ new WeakSet();
22
+ const str = (value, fallback = "") => {
23
+ if (typeof value === "string") return value;
24
+ if (typeof value === "number" || typeof value === "boolean") return String(value);
25
+ return fallback;
26
+ };
27
+ const num = (value, fallback) => typeof value === "number" && Number.isFinite(value) ? value : fallback;
28
+ const bool = (value, fallback = false) => typeof value === "boolean" ? value : fallback;
29
+ const keyOf = (node) => {
30
+ const key = node.props.key ?? node.props.id ?? node.props.label;
31
+ return typeof key === "string" || typeof key === "number" ? String(key) : "";
32
+ };
33
+ const TONES = new Set([
34
+ "ok",
35
+ "warn",
36
+ "error",
37
+ "muted",
38
+ "accent"
39
+ ]);
40
+ const toneClass = (tone) => {
41
+ const value = str(tone);
42
+ return TONES.has(value) ? `is-${value}` : "";
43
+ };
44
+ const cx = (ctx, slot, base, extra) => {
45
+ const parts = [];
46
+ if (!ctx.unstyled && base) parts.push(base);
47
+ const custom = ctx.classes?.[slot];
48
+ if (custom) parts.push(custom);
49
+ if (extra) parts.push(extra);
50
+ return parts.join(" ").trim();
51
+ };
52
+ const nodeClass = (ctx, slot, base, node, extra) => {
53
+ return cx(ctx, slot, base, [extra, str(node.props.className)].filter(Boolean).join(" "));
54
+ };
55
+ const matchesExisting = (existing, kind, key) => existing !== void 0 && existing.dataset.uiKind === kind && (existing.dataset.uiKey ?? "") === key;
56
+ const makeEl = (doc, tag, className) => {
57
+ const el = doc.createElement(tag);
58
+ if (className) el.className = className;
59
+ return el;
60
+ };
61
+ const appendText = (doc, parent, tag, text, className) => {
62
+ const el = makeEl(doc, tag, className);
63
+ el.textContent = text;
64
+ parent.append(el);
65
+ return el;
66
+ };
67
+ const formatAtom = (value) => {
68
+ if (Array.isArray(value)) return `[${value.map((item) => JSON.stringify(item)).join(", ")}]`;
69
+ return typeof value === "string" ? value : JSON.stringify(value);
70
+ };
71
+ const closeSelect = (root) => {
72
+ root.classList.remove("is-open");
73
+ const trigger = root.querySelector("[data-select-trigger]");
74
+ const menu = root.querySelector("[data-select-menu]");
75
+ trigger?.setAttribute("aria-expanded", "false");
76
+ if (menu) menu.hidden = true;
77
+ OPEN_SELECTS.delete(root);
78
+ };
79
+ const ensureOutsideDismiss = (doc) => {
80
+ if (OUTSIDE_WIRED.has(doc)) return;
81
+ OUTSIDE_WIRED.add(doc);
82
+ doc.addEventListener("pointerdown", (event) => {
83
+ const target = event.target;
84
+ for (const root of [...OPEN_SELECTS]) if (!(target && root.contains(target))) closeSelect(root);
85
+ });
86
+ doc.addEventListener("keydown", (event) => {
87
+ if (event.key === "Escape") for (const root of [...OPEN_SELECTS]) closeSelect(root);
88
+ });
89
+ };
90
+ const renderWhyHeader = (doc, rec) => {
91
+ const header = makeEl(doc, "div", "lion-ui-why-head");
92
+ if ("fact" in rec) {
93
+ const tag = rec.given === true ? "given" : "derived";
94
+ appendText(doc, header, "span", tag, `lion-ui-why-tag ${tag === "given" ? "is-ok" : "is-accent"}`);
95
+ appendText(doc, header, "code", formatAtom(rec.fact), "lion-ui-why-atom");
96
+ } else if ("absent" in rec) {
97
+ appendText(doc, header, "span", "absent", "lion-ui-why-tag is-error");
98
+ appendText(doc, header, "code", formatAtom(rec.absent), "lion-ui-why-atom");
99
+ } else if ("check" in rec) {
100
+ appendText(doc, header, "span", "check", "lion-ui-why-tag is-ok");
101
+ appendText(doc, header, "code", formatAtom(rec.check), "lion-ui-why-atom");
102
+ } else if ("rule" in rec) {
103
+ appendText(doc, header, "span", "rule", "lion-ui-why-tag is-muted");
104
+ appendText(doc, header, "code", formatAtom(rec.rule), "lion-ui-why-atom");
105
+ } else appendText(doc, header, "code", JSON.stringify(rec), "lion-ui-why-atom");
106
+ return header;
107
+ };
108
+ const renderWhyTree = (doc, data, depth = 0) => {
109
+ const root = makeEl(doc, "div", `lion-ui-why-node depth-${Math.min(depth, 4)}`);
110
+ if (typeof data !== "object" || data === null) {
111
+ appendText(doc, root, "span", str(data, "No explanation"), "lion-ui-why-empty");
112
+ return root;
113
+ }
114
+ const rec = data;
115
+ root.append(renderWhyHeader(doc, rec));
116
+ if ("but" in rec && rec.but !== void 0) {
117
+ const blocked = makeEl(doc, "div", "lion-ui-why-but");
118
+ appendText(doc, blocked, "span", "blocked by", "lion-ui-why-label");
119
+ blocked.append(renderWhyTree(doc, rec.but, depth + 1));
120
+ root.append(blocked);
121
+ }
122
+ if (Array.isArray(rec.because) && rec.because.length > 0) {
123
+ const list = makeEl(doc, "div", "lion-ui-why-children");
124
+ for (const item of rec.because) list.append(renderWhyTree(doc, item, depth + 1));
125
+ root.append(list);
126
+ }
127
+ return root;
128
+ };
129
+ const proofBadge = (props, data) => {
130
+ if (props.valid === true) return ["kernel verified", "is-ok"];
131
+ if (data?.proved === true) return ["proved", "is-ok"];
132
+ if (data?.proved === false) return ["refuted", "is-error"];
133
+ if (data && "min" in data && "max" in data) return props.valid === false ? ["bound failed", "is-error"] : ["exact bounds", "is-ok"];
134
+ return ["unchecked", "is-muted"];
135
+ };
136
+ const renderProofBody = (doc, card, props, data) => {
137
+ if (data && "min" in data && "max" in data) {
138
+ const row = makeEl(doc, "div", "lion-ui-proof-bounds");
139
+ appendText(doc, row, "span", `min ${str(data.min)}`, "lion-ui-proof-stat");
140
+ appendText(doc, row, "span", `max ${str(data.max)}`, "lion-ui-proof-stat");
141
+ card.append(row);
142
+ if (typeof data.witnessMin === "object" && data.witnessMin !== null) appendText(doc, card, "p", `Worst-case witness: ${JSON.stringify(data.witnessMin)}`, "lion-ui-proof-note");
143
+ return;
144
+ }
145
+ if (data && "counterexample" in data && data.counterexample !== null) {
146
+ appendText(doc, card, "p", `Counterexample: ${JSON.stringify(data.counterexample)}`, "lion-ui-proof-note is-error");
147
+ return;
148
+ }
149
+ if (props.summary) appendText(doc, card, "p", str(props.summary), "lion-ui-proof-note");
150
+ };
151
+ const renderProof = (node, ctx) => {
152
+ const { doc } = ctx;
153
+ const card = makeEl(doc, "div", nodeClass(ctx, "proof", "lion-ui-proof", node));
154
+ card.dataset.uiKind = node.$ui;
155
+ card.dataset.uiKey = keyOf(node);
156
+ const data = typeof node.props.data === "object" && node.props.data !== null ? node.props.data : null;
157
+ const head = makeEl(doc, "div", "lion-ui-proof-head");
158
+ appendText(doc, head, "span", str(node.props.title, "Formal proof"), "lion-ui-proof-title");
159
+ const [badgeText, badgeTone] = proofBadge(node.props, data);
160
+ appendText(doc, head, "span", badgeText, cx(ctx, "badge", "lion-ui-badge", badgeTone));
161
+ card.append(head);
162
+ renderProofBody(doc, card, node.props, data);
163
+ return card;
164
+ };
165
+ const renderButton = (node, ctx, existing) => {
166
+ const { doc, onAction } = ctx;
167
+ const key = keyOf(node);
168
+ const button = matchesExisting(existing, "button", key) ? existing : makeEl(doc, "button");
169
+ button.type = "button";
170
+ button.dataset.uiKind = "button";
171
+ button.dataset.uiKey = key;
172
+ button.className = nodeClass(ctx, "button", "lion-ui-button", node, toneClass(node.props.tone));
173
+ button.disabled = bool(node.props.disabled, false);
174
+ button.textContent = str(node.props.label) || node.children.map((child) => str(child)).join(" ") || "Run";
175
+ ACTION_MAP.set(button, node.props.onClick ?? node.props.action);
176
+ if (!WIRED.has(button)) {
177
+ WIRED.add(button);
178
+ button.addEventListener("click", () => {
179
+ const action = ACTION_MAP.get(button);
180
+ if (action !== void 0) onAction?.(action);
181
+ });
182
+ }
183
+ return button;
184
+ };
185
+ const renderToggle = (node, ctx, existing) => {
186
+ const { doc, onAction } = ctx;
187
+ const key = keyOf(node);
188
+ const wrapper = matchesExisting(existing, "toggle", key) ? existing : makeEl(doc, "label");
189
+ wrapper.dataset.uiKind = "toggle";
190
+ wrapper.dataset.uiKey = key;
191
+ wrapper.className = nodeClass(ctx, "toggle", "lion-ui-toggle", node);
192
+ let input = wrapper.querySelector("input");
193
+ let text = wrapper.querySelector(".lion-ui-toggle-label");
194
+ if (!(input && text)) {
195
+ wrapper.textContent = "";
196
+ input = doc.createElement("input");
197
+ input.type = "checkbox";
198
+ input.className = "lion-ui-toggle-input";
199
+ const track = makeEl(doc, "span", "lion-ui-toggle-track");
200
+ text = makeEl(doc, "span", "lion-ui-toggle-label");
201
+ wrapper.append(input, track, text);
202
+ }
203
+ input.checked = bool(node.props.checked ?? node.props.value, false);
204
+ text.textContent = str(node.props.label);
205
+ ACTION_MAP.set(input, node.props.onChange ?? node.props.action);
206
+ if (!WIRED.has(input)) {
207
+ WIRED.add(input);
208
+ input.addEventListener("change", () => {
209
+ const target = input;
210
+ const action = ACTION_MAP.get(target);
211
+ if (action !== void 0) onAction?.(action, target.checked);
212
+ });
213
+ }
214
+ return wrapper;
215
+ };
216
+ const ensureFieldShell = (ctx, kind, key, label, node, tag, existing) => {
217
+ const { doc } = ctx;
218
+ const field = matchesExisting(existing, kind, key) ? existing : makeEl(doc, tag);
219
+ field.dataset.uiKind = kind;
220
+ field.dataset.uiKey = key;
221
+ field.className = nodeClass(ctx, "field", `lion-ui-field is-${kind}`, node);
222
+ let head = field.querySelector(".lion-ui-field-head");
223
+ let labelEl = field.querySelector(".lion-ui-field-label");
224
+ let valueBadge = field.querySelector(".lion-ui-field-value");
225
+ if (!(head && labelEl)) {
226
+ field.textContent = "";
227
+ head = makeEl(doc, "span", "lion-ui-field-head");
228
+ labelEl = makeEl(doc, "span", "lion-ui-field-label");
229
+ head.append(labelEl);
230
+ if (kind === "slider") {
231
+ valueBadge = makeEl(doc, "span", "lion-ui-field-value");
232
+ head.append(valueBadge);
233
+ }
234
+ field.append(head);
235
+ }
236
+ labelEl.textContent = label;
237
+ return {
238
+ field,
239
+ valueBadge
240
+ };
241
+ };
242
+ const normalizeOptions = (raw) => {
243
+ if (!Array.isArray(raw)) return [];
244
+ return raw.map((opt) => typeof opt === "object" && opt !== null ? {
245
+ label: str(opt.label),
246
+ value: str(opt.value)
247
+ } : {
248
+ label: str(opt),
249
+ value: str(opt)
250
+ });
251
+ };
252
+ const makeChevronSvg = (doc) => {
253
+ const svg = doc.createElementNS("http://www.w3.org/2000/svg", "svg");
254
+ svg.setAttribute("aria-hidden", "true");
255
+ svg.setAttribute("width", "14");
256
+ svg.setAttribute("height", "14");
257
+ svg.setAttribute("viewBox", "0 0 24 24");
258
+ svg.setAttribute("fill", "none");
259
+ svg.setAttribute("stroke", "currentColor");
260
+ svg.setAttribute("stroke-width", "2");
261
+ svg.setAttribute("stroke-linecap", "round");
262
+ svg.setAttribute("stroke-linejoin", "round");
263
+ svg.setAttribute("class", "lion-ui-select-chevron");
264
+ const path = doc.createElementNS("http://www.w3.org/2000/svg", "polyline");
265
+ path.setAttribute("points", "6 9 12 15 18 9");
266
+ svg.append(path);
267
+ return svg;
268
+ };
269
+ const populateSelectMenu = (menu, root, options, currentValue, ctx) => {
270
+ const { doc, onAction } = ctx;
271
+ menu.textContent = "";
272
+ for (const opt of options) {
273
+ const selected = opt.value === currentValue;
274
+ const item = makeEl(doc, "button", cx(ctx, "selectOption", "lion-ui-select-option", selected ? "is-selected" : ""));
275
+ item.type = "button";
276
+ item.setAttribute("role", "option");
277
+ item.setAttribute("aria-selected", String(selected));
278
+ const check = makeEl(doc, "span", "lion-ui-select-check");
279
+ check.textContent = selected ? "✓" : "";
280
+ const text = makeEl(doc, "span", "lion-ui-select-option-label");
281
+ text.textContent = opt.label;
282
+ item.append(text, check);
283
+ item.addEventListener("click", () => {
284
+ closeSelect(root);
285
+ const action = ACTION_MAP.get(root);
286
+ if (action !== void 0) onAction?.(action, opt.value);
287
+ });
288
+ menu.append(item);
289
+ }
290
+ };
291
+ const renderSelect = (node, ctx, existing) => {
292
+ const { doc } = ctx;
293
+ ensureOutsideDismiss(doc);
294
+ const { field } = ensureFieldShell(ctx, "select", keyOf(node), str(node.props.label), node, "div", existing);
295
+ let root = field.querySelector("[data-select-root]");
296
+ let trigger = field.querySelector("[data-select-trigger]");
297
+ let triggerLabel = field.querySelector("[data-select-value]");
298
+ let menu = field.querySelector("[data-select-menu]");
299
+ if (!(root && trigger && triggerLabel && menu)) {
300
+ root = makeEl(doc, "div", cx(ctx, "select", "lion-ui-select-root"));
301
+ root.dataset.selectRoot = "";
302
+ trigger = makeEl(doc, "button", cx(ctx, "selectTrigger", "lion-ui-select-trigger"));
303
+ trigger.type = "button";
304
+ trigger.dataset.selectTrigger = "";
305
+ trigger.setAttribute("aria-haspopup", "listbox");
306
+ trigger.setAttribute("aria-expanded", "false");
307
+ triggerLabel = makeEl(doc, "span", "lion-ui-select-value");
308
+ triggerLabel.dataset.selectValue = "";
309
+ trigger.append(triggerLabel, makeChevronSvg(doc));
310
+ menu = makeEl(doc, "div", cx(ctx, "selectMenu", "lion-ui-select-menu"));
311
+ menu.dataset.selectMenu = "";
312
+ menu.setAttribute("role", "listbox");
313
+ menu.hidden = true;
314
+ root.append(trigger, menu);
315
+ field.append(root);
316
+ }
317
+ const options = normalizeOptions(node.props.options);
318
+ const currentValue = str(node.props.value);
319
+ const currentOption = options.find((opt) => opt.value === currentValue);
320
+ triggerLabel.textContent = currentOption?.label ?? currentValue;
321
+ ACTION_MAP.set(root, node.props.onChange ?? node.props.action);
322
+ populateSelectMenu(menu, root, options, currentValue, ctx);
323
+ if (!WIRED.has(trigger)) {
324
+ WIRED.add(trigger);
325
+ const selectRoot = root;
326
+ const selectTrigger = trigger;
327
+ const selectMenu = menu;
328
+ selectTrigger.addEventListener("click", () => {
329
+ const opening = selectMenu.hidden;
330
+ for (const other of [...OPEN_SELECTS]) if (other !== selectRoot) closeSelect(other);
331
+ selectMenu.hidden = !opening;
332
+ selectRoot.classList.toggle("is-open", opening);
333
+ selectTrigger.setAttribute("aria-expanded", String(opening));
334
+ if (opening) OPEN_SELECTS.add(selectRoot);
335
+ else OPEN_SELECTS.delete(selectRoot);
336
+ });
337
+ }
338
+ return field;
339
+ };
340
+ const syncInputAttributes = (input, kind, props, doc, valueBadge) => {
341
+ if (kind === "slider") {
342
+ input.type = "range";
343
+ input.min = String(num(props.min, 0));
344
+ input.max = String(num(props.max, 100));
345
+ input.step = String(num(props.step, 1));
346
+ const current = num(props.value, num(props.min, 0));
347
+ if (input.value !== String(current)) input.value = String(current);
348
+ if (valueBadge) valueBadge.textContent = `${str(props.prefix)}${current}${str(props.suffix)}`;
349
+ return;
350
+ }
351
+ input.type = kind === "number" ? "number" : "text";
352
+ if (kind === "number") {
353
+ if (props.min !== void 0) input.min = String(num(props.min, 0));
354
+ if (props.max !== void 0) input.max = String(num(props.max, 100));
355
+ if (props.step !== void 0) input.step = String(num(props.step, 1));
356
+ } else input.placeholder = str(props.placeholder);
357
+ const nextVal = str(props.value);
358
+ if (doc.activeElement !== input && input.value !== nextVal) input.value = nextVal;
359
+ };
360
+ const renderInputControl = (kind) => (node, ctx, existing) => {
361
+ const { doc, onAction } = ctx;
362
+ const { field, valueBadge } = ensureFieldShell(ctx, kind, keyOf(node), str(node.props.label), node, "label", existing);
363
+ let input = field.querySelector("input");
364
+ if (!input) {
365
+ input = doc.createElement("input");
366
+ input.className = cx(ctx, kind, kind === "slider" ? "lion-ui-slider" : "lion-ui-input");
367
+ field.append(input);
368
+ }
369
+ syncInputAttributes(input, kind, node.props, doc, valueBadge);
370
+ ACTION_MAP.set(input, node.props.onChange ?? node.props.action);
371
+ if (!WIRED.has(input)) {
372
+ WIRED.add(input);
373
+ input.addEventListener("input", () => {
374
+ const target = input;
375
+ const action = ACTION_MAP.get(target);
376
+ if (action === void 0) return;
377
+ if (kind === "slider" || kind === "number") {
378
+ const parsed = Number(target.value);
379
+ if (Number.isFinite(parsed)) onAction?.(action, parsed);
380
+ } else onAction?.(action, target.value);
381
+ });
382
+ }
383
+ return field;
384
+ };
385
+ const renderChild = (child, ctx, existing) => {
386
+ if (!isUiNode(child)) {
387
+ const text = String(child);
388
+ if (existing?.nodeType === 3) {
389
+ if (existing.textContent !== text) existing.textContent = text;
390
+ return existing;
391
+ }
392
+ return ctx.doc.createTextNode(text);
393
+ }
394
+ return renderElement(child, ctx, existing?.nodeType === 1 ? existing : void 0);
395
+ };
396
+ const patchChildren = (container, children, ctx) => {
397
+ for (let i = 0; i < children.length; i += 1) {
398
+ const child = children[i];
399
+ if (child === void 0) continue;
400
+ const existing = container.childNodes[i];
401
+ const next = renderChild(child, ctx, existing);
402
+ if (!existing) container.append(next);
403
+ else if (next !== existing) container.replaceChild(next, existing);
404
+ }
405
+ while (container.childNodes.length > children.length) container.lastChild?.remove();
406
+ };
407
+ const renderLayout = (kind) => (node, ctx, existing) => {
408
+ const key = keyOf(node);
409
+ const el = matchesExisting(existing, kind, key) ? existing : makeEl(ctx.doc, "div");
410
+ el.dataset.uiKind = kind;
411
+ el.dataset.uiKey = key;
412
+ const gap = str(node.props.gap, "md");
413
+ const align = str(node.props.align);
414
+ el.className = nodeClass(ctx, kind, `lion-ui-${kind}`, node, [`gap-${gap}`, align ? `align-${align}` : ""].filter(Boolean).join(" "));
415
+ if (kind === "grid") el.style.setProperty("--ui-cols", String(num(node.props.columns, 2)));
416
+ patchChildren(el, node.children, ctx);
417
+ return el;
418
+ };
419
+ const renderCard = (node, ctx, existing) => {
420
+ const { doc } = ctx;
421
+ const key = keyOf(node);
422
+ const el = matchesExisting(existing, "card", key) ? existing : makeEl(doc, "section");
423
+ el.dataset.uiKind = "card";
424
+ el.dataset.uiKey = key;
425
+ el.className = nodeClass(ctx, "card", "lion-ui-card", node, toneClass(node.props.tone));
426
+ const title = str(node.props.title);
427
+ const subtitle = str(node.props.subtitle);
428
+ let body = el.querySelector(":scope > .lion-ui-card-body");
429
+ if (body) {
430
+ const titleEl = el.querySelector(":scope > .lion-ui-card-head .lion-ui-card-title");
431
+ if (titleEl && titleEl.textContent !== title) titleEl.textContent = title;
432
+ const subEl = el.querySelector(":scope > .lion-ui-card-head .lion-ui-card-subtitle");
433
+ if (subEl && subEl.textContent !== subtitle) subEl.textContent = subtitle;
434
+ } else {
435
+ el.textContent = "";
436
+ if (title || subtitle) {
437
+ const header = makeEl(doc, "header", "lion-ui-card-head");
438
+ if (title) appendText(doc, header, "h4", title, "lion-ui-card-title");
439
+ if (subtitle) appendText(doc, header, "p", subtitle, "lion-ui-card-subtitle");
440
+ el.append(header);
441
+ }
442
+ body = makeEl(doc, "div", "lion-ui-card-body");
443
+ el.append(body);
444
+ }
445
+ patchChildren(body, node.children, ctx);
446
+ return el;
447
+ };
448
+ const renderMetric = (node, ctx) => {
449
+ const { doc } = ctx;
450
+ const el = makeEl(doc, "div", nodeClass(ctx, "metric", "lion-ui-metric", node, toneClass(node.props.tone)));
451
+ el.dataset.uiKind = "metric";
452
+ el.dataset.uiKey = keyOf(node);
453
+ appendText(doc, el, "span", str(node.props.label), "lion-ui-metric-label");
454
+ appendText(doc, el, "strong", str(node.props.value), "lion-ui-metric-value");
455
+ if (node.props.delta !== void 0 && node.props.delta !== null && node.props.delta !== "") appendText(doc, el, "span", str(node.props.delta), "lion-ui-metric-delta");
456
+ return el;
457
+ };
458
+ const renderAlert = (node, ctx) => {
459
+ const el = makeEl(ctx.doc, "div", nodeClass(ctx, "alert", "lion-ui-alert", node, toneClass(node.props.tone)));
460
+ el.dataset.uiKind = "alert";
461
+ el.dataset.uiKey = keyOf(node);
462
+ if (node.props.title) appendText(ctx.doc, el, "strong", str(node.props.title), "lion-ui-alert-title");
463
+ if (node.children.length > 0) {
464
+ const body = makeEl(ctx.doc, "div", "lion-ui-alert-body");
465
+ patchChildren(body, node.children, ctx);
466
+ el.append(body);
467
+ }
468
+ return el;
469
+ };
470
+ const renderProgress = (node, ctx) => {
471
+ const { doc } = ctx;
472
+ const el = makeEl(doc, "div", nodeClass(ctx, "progress", "lion-ui-progress", node, toneClass(node.props.tone)));
473
+ el.dataset.uiKind = "progress";
474
+ el.dataset.uiKey = keyOf(node);
475
+ const value = num(node.props.value, 0);
476
+ const max = Math.max(1, num(node.props.max, 100));
477
+ const pct = Math.max(0, Math.min(100, value / max * 100));
478
+ const head = makeEl(doc, "div", "lion-ui-progress-head");
479
+ appendText(doc, head, "span", str(node.props.label), "lion-ui-progress-label");
480
+ appendText(doc, head, "span", str(node.props.display, `${Math.round(pct)}%`), "lion-ui-progress-value");
481
+ const track = makeEl(doc, "div", "lion-ui-progress-track");
482
+ const fill = makeEl(doc, "div", "lion-ui-progress-fill");
483
+ fill.style.width = `${pct}%`;
484
+ track.append(fill);
485
+ el.append(head, track);
486
+ return el;
487
+ };
488
+ const renderTable = (node, ctx) => {
489
+ const { doc } = ctx;
490
+ const wrap = makeEl(doc, "div", nodeClass(ctx, "tableWrap", "lion-ui-table-wrap", node));
491
+ wrap.dataset.uiKind = "table";
492
+ wrap.dataset.uiKey = keyOf(node);
493
+ const table = makeEl(doc, "table", cx(ctx, "table", "lion-ui-table"));
494
+ const columns = Array.isArray(node.props.columns) ? node.props.columns : [];
495
+ const rows = Array.isArray(node.props.rows) ? node.props.rows : [];
496
+ if (columns.length > 0) {
497
+ const thead = doc.createElement("thead");
498
+ const tr = doc.createElement("tr");
499
+ for (const col of columns) appendText(doc, tr, "th", str(col));
500
+ thead.append(tr);
501
+ table.append(thead);
502
+ }
503
+ const tbody = doc.createElement("tbody");
504
+ for (const row of rows) {
505
+ const tr = doc.createElement("tr");
506
+ const cells = Array.isArray(row) ? row : [row];
507
+ for (const cell of cells) {
508
+ const td = doc.createElement("td");
509
+ if (isUiNode(cell)) td.append(renderElement(cell, ctx));
510
+ else td.textContent = str(cell);
511
+ tr.append(td);
512
+ }
513
+ tbody.append(tr);
514
+ }
515
+ table.append(tbody);
516
+ wrap.append(table);
517
+ return wrap;
518
+ };
519
+ const renderWhy = (node, ctx) => {
520
+ const { doc } = ctx;
521
+ const el = makeEl(doc, "div", nodeClass(ctx, "why", "lion-ui-why", node));
522
+ el.dataset.uiKind = "why";
523
+ el.dataset.uiKey = keyOf(node);
524
+ if (node.props.title) appendText(doc, el, "div", str(node.props.title), "lion-ui-why-title");
525
+ el.append(renderWhyTree(doc, node.props.data));
526
+ return el;
527
+ };
528
+ const renderSimple = (tag, slot, baseClass) => (node, ctx) => {
529
+ const el = makeEl(ctx.doc, tag, nodeClass(ctx, slot, baseClass, node, toneClass(node.props.tone)));
530
+ el.dataset.uiKind = node.$ui;
531
+ el.dataset.uiKey = keyOf(node);
532
+ patchChildren(el, node.children, ctx);
533
+ return el;
534
+ };
535
+ const renderHeading = (node, ctx) => {
536
+ const level = Math.min(4, Math.max(1, num(node.props.level, 3)));
537
+ const el = makeEl(ctx.doc, `h${level}`, nodeClass(ctx, "heading", "lion-ui-heading", node, `level-${level}`));
538
+ el.dataset.uiKind = "heading";
539
+ el.dataset.uiKey = keyOf(node);
540
+ patchChildren(el, node.children, ctx);
541
+ return el;
542
+ };
543
+ const renderCode = (node, ctx) => {
544
+ const { doc } = ctx;
545
+ const pre = makeEl(doc, "pre", nodeClass(ctx, "code", "lion-ui-code", node));
546
+ pre.dataset.uiKind = "code";
547
+ const code = doc.createElement("code");
548
+ code.textContent = node.children.map((child) => str(child)).join("");
549
+ pre.append(code);
550
+ return pre;
551
+ };
552
+ const renderDivider = (node, ctx) => {
553
+ const hr = makeEl(ctx.doc, "hr", nodeClass(ctx, "divider", "lion-ui-divider", node));
554
+ hr.dataset.uiKind = "divider";
555
+ return hr;
556
+ };
557
+ const renderChart = (node, ctx) => {
558
+ const { doc } = ctx;
559
+ const wrap = makeEl(doc, "div", nodeClass(ctx, "chart", "lion-ui-chart", node));
560
+ wrap.dataset.uiKind = "chart";
561
+ wrap.dataset.uiKey = keyOf(node);
562
+ if (node.props.title) appendText(doc, wrap, "div", str(node.props.title), "lion-ui-chart-title");
563
+ const rows = Array.isArray(node.props.data) ? node.props.data : [];
564
+ const maxVal = Math.max(1, ...rows.map((r) => Math.abs(num(r.max ?? r.value, 0))));
565
+ const list = makeEl(doc, "div", "lion-ui-chart-bars");
566
+ for (const row of rows) {
567
+ const item = makeEl(doc, "div", "lion-ui-chart-row");
568
+ appendText(doc, item, "span", str(row.label), "lion-ui-chart-label");
569
+ const track = makeEl(doc, "div", "lion-ui-chart-track");
570
+ const fill = makeEl(doc, "div", `lion-ui-chart-fill ${toneClass(row.tone)}`.trim());
571
+ const val = num(row.value, 0);
572
+ fill.style.width = `${Math.min(100, Math.max(0, Math.abs(val) / maxVal * 100))}%`;
573
+ track.append(fill);
574
+ item.append(track);
575
+ appendText(doc, item, "span", str(row.display ?? val), "lion-ui-chart-value");
576
+ list.append(item);
577
+ }
578
+ wrap.append(list);
579
+ return wrap;
580
+ };
581
+ const renderTimeline = (node, ctx) => {
582
+ const { doc } = ctx;
583
+ const wrap = makeEl(doc, "div", nodeClass(ctx, "timeline", "lion-ui-timeline", node));
584
+ wrap.dataset.uiKind = "timeline";
585
+ wrap.dataset.uiKey = keyOf(node);
586
+ if (node.props.title) appendText(doc, wrap, "div", str(node.props.title), "lion-ui-timeline-title");
587
+ const list = makeEl(doc, "ol", "lion-ui-timeline-list");
588
+ const items = Array.isArray(node.props.items) ? node.props.items : [];
589
+ for (const entry of items) {
590
+ const li = makeEl(doc, "li", `lion-ui-timeline-item ${toneClass(entry.tone)}`.trim());
591
+ if (entry.time !== void 0) appendText(doc, li, "time", str(entry.time), "lion-ui-timeline-time");
592
+ appendText(doc, li, "div", str(entry.title), "lion-ui-timeline-heading");
593
+ if (entry.detail !== void 0) appendText(doc, li, "p", str(entry.detail), "lion-ui-timeline-detail");
594
+ list.append(li);
595
+ }
596
+ wrap.append(list);
597
+ return wrap;
598
+ };
599
+ const renderDiff = (node, ctx) => {
600
+ const { doc } = ctx;
601
+ const wrap = makeEl(doc, "div", nodeClass(ctx, "diff", "lion-ui-diff", node));
602
+ wrap.dataset.uiKind = "diff";
603
+ wrap.dataset.uiKey = keyOf(node);
604
+ if (node.props.title) appendText(doc, wrap, "div", str(node.props.title), "lion-ui-diff-title");
605
+ const removed = Array.isArray(node.props.removed) ? node.props.removed : [];
606
+ const added = Array.isArray(node.props.added) ? node.props.added : [];
607
+ for (const item of removed) appendText(doc, wrap, "div", `- ${typeof item === "string" ? item : JSON.stringify(item)}`, "lion-ui-diff-line is-removed");
608
+ for (const item of added) appendText(doc, wrap, "div", `+ ${typeof item === "string" ? item : JSON.stringify(item)}`, "lion-ui-diff-line is-added");
609
+ return wrap;
610
+ };
611
+ const renderSource = (node, ctx) => {
612
+ const { doc } = ctx;
613
+ const wrap = makeEl(doc, "blockquote", nodeClass(ctx, "source", "lion-ui-source", node));
614
+ wrap.dataset.uiKind = "source";
615
+ wrap.dataset.uiKey = keyOf(node);
616
+ if (node.props.title || node.props.publisher) appendText(doc, wrap, "div", [str(node.props.publisher), str(node.props.title)].filter(Boolean).join(" — "), "lion-ui-source-header");
617
+ const text = str(node.props.text);
618
+ const quote = str(node.props.quote);
619
+ const body = makeEl(doc, "p", "lion-ui-source-body");
620
+ if (quote && text.includes(quote)) {
621
+ const at = text.indexOf(quote);
622
+ body.append(doc.createTextNode(text.slice(0, at)));
623
+ const mark = makeEl(doc, "mark", "lion-ui-source-quote");
624
+ mark.textContent = quote;
625
+ body.append(mark);
626
+ body.append(doc.createTextNode(text.slice(at + quote.length)));
627
+ } else body.textContent = text || quote;
628
+ wrap.append(body);
629
+ return wrap;
630
+ };
631
+ const RENDERERS = {
632
+ alert: renderAlert,
633
+ badge: renderSimple("span", "badge", "lion-ui-badge"),
634
+ button: renderButton,
635
+ card: renderCard,
636
+ chart: renderChart,
637
+ code: renderCode,
638
+ diff: renderDiff,
639
+ divider: renderDivider,
640
+ grid: renderLayout("grid"),
641
+ heading: renderHeading,
642
+ input: renderInputControl("input"),
643
+ metric: renderMetric,
644
+ number: renderInputControl("number"),
645
+ progress: renderProgress,
646
+ proof: renderProof,
647
+ row: renderLayout("row"),
648
+ select: renderSelect,
649
+ slider: renderInputControl("slider"),
650
+ source: renderSource,
651
+ stack: renderLayout("stack"),
652
+ table: renderTable,
653
+ text: renderSimple("p", "text", "lion-ui-text"),
654
+ timeline: renderTimeline,
655
+ toggle: renderToggle,
656
+ why: renderWhy
657
+ };
658
+ const renderElement = (node, ctx, existing) => RENDERERS[node.$ui](node, ctx, existing);
659
+ /**
660
+ * Renders (or patches in place) a Lion UI node into `container`.
661
+ */
662
+ const renderUi = (node, container, options = {}) => {
663
+ if (options.theme) container.dataset.lionTheme = options.theme;
664
+ const ctx = {
665
+ classes: options.classes,
666
+ doc: container.ownerDocument,
667
+ onAction: options.onAction,
668
+ unstyled: options.unstyled ?? false
669
+ };
670
+ const existing = container.firstElementChild;
671
+ const next = renderElement(node, ctx, existing ?? void 0);
672
+ if (!existing) container.append(next);
673
+ else if (next !== existing) container.replaceChild(next, existing);
674
+ while (container.children.length > 1) container.lastElementChild?.remove();
675
+ };
676
+ //#endregion
677
+ export { renderUi };
678
+
679
+ //# sourceMappingURL=dom.mjs.map