@andamento/dom 0.0.0-stage → 0.2.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/index.js ADDED
@@ -0,0 +1,727 @@
1
+ import {
2
+ __privateAdd,
3
+ __privateGet,
4
+ __privateMethod,
5
+ __privateSet,
6
+ backdropStyle,
7
+ backgroundStyle,
8
+ containerStyle,
9
+ coverStyle,
10
+ frameBuilder,
11
+ keyAction,
12
+ layoutResolver,
13
+ overlayStyle,
14
+ resolvePerCell,
15
+ ringStrokes,
16
+ safeBoxStyle,
17
+ sameRect,
18
+ shadowShape,
19
+ tileSheet,
20
+ tileStyle,
21
+ tileViewBox,
22
+ viewRect,
23
+ watchView
24
+ } from "./chunk-RCXEXYRF.js";
25
+
26
+ // src/mount.ts
27
+ import { cellsInRect } from "@andamento/core";
28
+ var SVG = "http://www.w3.org/2000/svg";
29
+ var kebab = (k) => k.startsWith("--") ? k : k.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase());
30
+ var isEmpty = (c) => c == null || c === "";
31
+ var styleMemo = /* @__PURE__ */ new WeakMap();
32
+ function applyStyle(el, next) {
33
+ const prev = styleMemo.get(el) ?? {};
34
+ for (const k of Object.keys(prev)) if (next[k] === void 0 && prev[k] !== void 0) el.style.removeProperty(kebab(k));
35
+ for (const [k, v] of Object.entries(next)) if (v !== void 0 && prev[k] !== v) el.style.setProperty(kebab(k), String(v));
36
+ styleMemo.set(el, next);
37
+ }
38
+ function attr(el, name, value) {
39
+ if (value === void 0 || value === null || value === false) {
40
+ if (el.hasAttribute(name)) el.removeAttribute(name);
41
+ } else {
42
+ const v = value === true ? "" : String(value);
43
+ if (el.getAttribute(name) !== v) el.setAttribute(name, v);
44
+ }
45
+ }
46
+ function setChildren(parent, nodes) {
47
+ let at = parent.firstChild;
48
+ for (const n of nodes) {
49
+ if (n === at) at = at.nextSibling;
50
+ else parent.insertBefore(n, at);
51
+ }
52
+ while (at) {
53
+ const next = at.nextSibling;
54
+ parent.removeChild(at);
55
+ at = next;
56
+ }
57
+ }
58
+ function svg(tag, attrs, children = []) {
59
+ const el = document.createElementNS(SVG, tag);
60
+ for (const [k, v] of Object.entries(attrs)) if (v !== void 0) el.setAttribute(k, String(v));
61
+ for (const c of children) el.appendChild(c);
62
+ return el;
63
+ }
64
+ var nodeIds = /* @__PURE__ */ new WeakMap();
65
+ var nextNodeId = 1;
66
+ var nodeId = (n) => {
67
+ let id = nodeIds.get(n);
68
+ if (!id) nodeIds.set(n, id = nextNodeId++);
69
+ return id;
70
+ };
71
+ var uidCount = 0;
72
+ function mountTileLayout(target, initial) {
73
+ let config = { ...initial };
74
+ const uid = `andamento-dom${++uidCount}`;
75
+ const resolveLayout = layoutResolver("mountTileLayout");
76
+ const buildFrame = frameBuilder();
77
+ const container = document.createElement("div");
78
+ container.setAttribute("data-andamento", "");
79
+ target.appendChild(container);
80
+ const sheet = document.createElement("style");
81
+ const backgroundEl = document.createElement("div");
82
+ backgroundEl.setAttribute("aria-hidden", "true");
83
+ const blur = svg("feGaussianBlur", {});
84
+ const shadowPath = svg("path", { "fill-rule": "evenodd", fill: "#000", filter: `url(#${uid}-shadow)` });
85
+ const shadowEl = svg("svg", { "aria-hidden": "true", "data-andamento-shadow": "", preserveAspectRatio: "none" }, [
86
+ svg("filter", { id: `${uid}-shadow`, x: "-10%", y: "-10%", width: "120%", height: "120%" }, [blur]),
87
+ shadowPath
88
+ ]);
89
+ let layout;
90
+ let tiles = /* @__PURE__ */ new Map();
91
+ const leaving = /* @__PURE__ */ new Set();
92
+ const byElement = /* @__PURE__ */ new WeakMap();
93
+ let lastKeys = /* @__PURE__ */ new Set();
94
+ let mounted = false;
95
+ let activeId;
96
+ let pendingFocus;
97
+ let focusable = /* @__PURE__ */ new Set();
98
+ let view;
99
+ let stopWatching;
100
+ let watchedLayout;
101
+ const timers = /* @__PURE__ */ new Set();
102
+ let destroyed = false;
103
+ function render() {
104
+ if (destroyed) return;
105
+ const c = config;
106
+ const items = c.renderCell ? [] : c.items ?? [];
107
+ layout = resolveLayout(c.options, c.layout, {
108
+ fit: !c.renderCell && c.fit,
109
+ count: items.length,
110
+ cells: c.contentCells,
111
+ weights: c.renderCell ? void 0 : c.weights,
112
+ placement: c.placement
113
+ });
114
+ const frame = buildFrame(layout, {
115
+ count: c.renderCell ? Infinity : items.length,
116
+ aspects: c.renderCell ? void 0 : c.aspects,
117
+ contentCells: c.contentCells,
118
+ placement: c.placement,
119
+ choose: c.choose,
120
+ fill: c.fill,
121
+ relief: c.relief,
122
+ animate: c.animate
123
+ });
124
+ const { slots } = frame;
125
+ const focusRing = c.focusRing === void 0 ? "#1a56db" : c.focusRing;
126
+ const list = (c.role ?? "list") === "list";
127
+ const virtual = !!c.virtualize;
128
+ const keyOf = (i) => {
129
+ const slot = slots[i];
130
+ if (!c.renderCell && slot >= 0) {
131
+ const item = items[slot];
132
+ return item && typeof item === "object" ? `n${nodeId(item)}` : `i${slot}`;
133
+ }
134
+ return `c${layout.cells[i].id}`;
135
+ };
136
+ const hasContent = (i) => c.renderCell ? slots[i] >= 0 : slots[i] >= 0 && !isEmpty(items[slots[i]]);
137
+ focusable = new Set(c.navigable ? layout.cells.filter((cell) => hasContent(cell.index)).map((cell) => cell.id) : []);
138
+ const firstFocusable = layout.cells.find((cell) => focusable.has(cell.id))?.id;
139
+ const active = activeId !== void 0 && focusable.has(activeId) ? activeId : firstFocusable;
140
+ let shown;
141
+ if (!virtual) shown = layout.cells.map((_, i) => i);
142
+ else {
143
+ const s = new Set(view && view.width >= 0 ? cellsInRect(layout, view).map((cell) => cell.index) : []);
144
+ const a = active !== void 0 ? layout.cells.find((cell) => cell.id === active) : void 0;
145
+ if (a) s.add(a.index);
146
+ shown = [...s].sort((x, y) => x - y);
147
+ }
148
+ let positions;
149
+ if (virtual) {
150
+ positions = /* @__PURE__ */ new Map();
151
+ for (const cell of layout.cells) if (hasContent(cell.index)) positions.set(cell.index, positions.size + 1);
152
+ }
153
+ const allKeys = frame.anim || virtual ? new Set(layout.cells.map((_, i) => keyOf(i))) : void 0;
154
+ const next = /* @__PURE__ */ new Map();
155
+ for (const i of shown) {
156
+ const cell = layout.cells[i];
157
+ const slot = slots[i];
158
+ const key = keyOf(i);
159
+ let content = c.renderCell ? c.renderCell(cell, { slot }) : slot >= 0 ? items[slot] : void 0;
160
+ const empty = isEmpty(content);
161
+ const state = { empty, slot: empty && !c.renderCell ? -1 : slot };
162
+ if (empty) content = resolvePerCell(c.fallback, cell, state);
163
+ const silent = empty && isEmpty(content);
164
+ let tile = tiles.get(key);
165
+ if (!tile || tile.leaving) {
166
+ const el2 = document.createElement("div");
167
+ tile = { key, el: el2, cell, state, empty };
168
+ if (frame.anim && mounted && !lastKeys.has(key)) el2.setAttribute("data-entering", "");
169
+ }
170
+ tiles.delete(key);
171
+ next.set(key, tile);
172
+ Object.assign(tile, { cell, state, empty });
173
+ const el = tile.el;
174
+ byElement.set(el, tile);
175
+ const canFocus = focusable.has(cell.id);
176
+ attr(el, "role", silent ? void 0 : list ? "listitem" : void 0);
177
+ attr(el, "aria-hidden", silent ? "true" : void 0);
178
+ attr(el, "aria-label", silent ? void 0 : resolvePerCell(c.cellLabel, cell, state));
179
+ attr(el, "aria-setsize", positions && list && !silent ? positions.size : void 0);
180
+ attr(el, "aria-posinset", positions && list && !silent ? positions.get(i) : void 0);
181
+ attr(el, "tabindex", canFocus ? cell.id === active ? 0 : -1 : void 0);
182
+ attr(el, "class", resolvePerCell(c.cellClass, cell, state) || void 0);
183
+ attr(el, "data-cell-id", cell.id);
184
+ attr(el, "data-index", cell.index);
185
+ attr(el, "data-empty", empty);
186
+ attr(el, "data-boundary", !!cell.boundary);
187
+ const extra = resolvePerCell(c.cellAttributes, cell, state) ?? {};
188
+ for (const k of tile.extra ?? []) if (!(k in extra)) attr(el, k, void 0);
189
+ for (const [k, v] of Object.entries(extra)) attr(el, k, v);
190
+ tile.extra = Object.keys(extra);
191
+ applyStyle(el, {
192
+ ...tileStyle(frame, cell, {
193
+ empty,
194
+ safeArea: c.safeArea,
195
+ backdrop: c.backdrop != null,
196
+ clickable: !!c.onCellActivate && !empty,
197
+ focus: slot >= 0 ? c.focalPoints?.[slot] : void 0,
198
+ aspect: slot >= 0 ? c.aspects?.[slot] : void 0
199
+ }),
200
+ ...resolvePerCell(c.cellStyle, cell, state)
201
+ });
202
+ const kids = [];
203
+ if (c.backdrop != null) {
204
+ if (!tile.backdrop || tile.backdrop.source !== c.backdrop) {
205
+ const node2 = typeof c.backdrop === "function" ? c.backdrop() : c.backdrop.cloneNode(true);
206
+ const wrap = document.createElement("div");
207
+ wrap.setAttribute("aria-hidden", "true");
208
+ wrap.appendChild(node2);
209
+ tile.backdrop = { source: c.backdrop, node: wrap };
210
+ }
211
+ applyStyle(tile.backdrop.node, backdropStyle(frame, cell));
212
+ kids.push(tile.backdrop.node);
213
+ } else tile.backdrop = void 0;
214
+ let node;
215
+ if (typeof content === "string") {
216
+ if (!tile.text) tile.text = document.createTextNode(content);
217
+ else if (tile.text.data !== content) tile.text.data = content;
218
+ node = tile.text;
219
+ } else if (content) node = content;
220
+ if (node && c.safeArea) {
221
+ if (!tile.safe) {
222
+ tile.safe = document.createElement("div");
223
+ tile.safe.setAttribute("data-safe-area", "");
224
+ }
225
+ const aspect = !c.renderCell && !empty && c.aspects ? c.aspects[slot] ?? 1 : void 0;
226
+ applyStyle(tile.safe, safeBoxStyle(frame, cell, aspect, c.safeArea));
227
+ setChildren(tile.safe, [node]);
228
+ kids.push(tile.safe);
229
+ } else if (node) kids.push(node);
230
+ const bevel = frame.bevel(i);
231
+ if (bevel && bevel.length) {
232
+ const clip = frame.anim ? String(el.style.getPropertyValue("clip-path")) : "";
233
+ const bk = tileViewBox(cell) + clip + bevel.map((p) => p.d + p.fill + p.opacity).join();
234
+ if (tile.bevel?.key !== bk) {
235
+ const s = svg(
236
+ "svg",
237
+ { "aria-hidden": "true", "data-bevel": "", "data-late": frame.anim ? "" : void 0, viewBox: tileViewBox(cell), preserveAspectRatio: "none" },
238
+ bevel.map((p) => svg("path", { d: p.d, fill: p.fill, "fill-opacity": p.opacity }))
239
+ );
240
+ applyStyle(s, overlayStyle);
241
+ tile.bevel = { key: bk, node: s };
242
+ }
243
+ kids.push(tile.bevel.node);
244
+ } else tile.bevel = void 0;
245
+ if (canFocus && focusRing !== false) {
246
+ const strokes = ringStrokes(frame, cell, focusRing);
247
+ const rk = tileViewBox(cell) + strokes[0].d + focusRing;
248
+ if (tile.ring?.key !== rk) {
249
+ const s = svg(
250
+ "svg",
251
+ { "aria-hidden": "true", "data-focus-ring": "", viewBox: tileViewBox(cell), preserveAspectRatio: "none" },
252
+ strokes.map((p) => svg("path", { d: p.d, fill: "none", stroke: p.stroke, "stroke-width": p.width, "vector-effect": "non-scaling-stroke" }))
253
+ );
254
+ applyStyle(s, overlayStyle);
255
+ tile.ring = { key: rk, node: s };
256
+ }
257
+ kids.push(tile.ring.node);
258
+ } else tile.ring = void 0;
259
+ setChildren(el, kids);
260
+ }
261
+ const present = allKeys ?? new Set(next.keys());
262
+ for (const tile of tiles.values()) {
263
+ if (frame.anim && mounted && !present.has(tile.key)) startLeaving(tile, frame.anim.duration);
264
+ }
265
+ tiles = next;
266
+ attr(container, "role", c.role ?? "list");
267
+ attr(container, "aria-label", c.label);
268
+ attr(container, "class", c.className);
269
+ applyStyle(container, containerStyle(layout));
270
+ const css = tileSheet({ focusRing: !!c.navigable && focusRing !== false, anim: frame.anim });
271
+ if (sheet.textContent !== css) sheet.textContent = css;
272
+ const top = [];
273
+ if (css) top.push(sheet);
274
+ if (c.background) {
275
+ applyStyle(backgroundEl, backgroundStyle(frame, c.background));
276
+ top.push(backgroundEl);
277
+ }
278
+ const shadow = shadowShape(frame, shown.map((i) => layout.cells[i]));
279
+ if (shadow) {
280
+ attr(shadowEl, "viewBox", shadow.viewBox);
281
+ applyStyle(shadowEl, shadow.style);
282
+ attr(blur, "stdDeviation", shadow.blur);
283
+ attr(shadowPath, "d", shadow.d);
284
+ attr(shadowPath, "transform", shadow.transform);
285
+ attr(shadowPath, "fill-opacity", shadow.opacity);
286
+ top.push(shadowEl);
287
+ }
288
+ setChildren(container, [...top, ...[...tiles.values()].map((t) => t.el), ...[...leaving].map((t) => t.el)]);
289
+ lastKeys = present;
290
+ mounted = true;
291
+ if (pendingFocus) {
292
+ const t = [...tiles.values()].find((x) => x.cell.id === pendingFocus);
293
+ if (t) {
294
+ pendingFocus = void 0;
295
+ t.el.focus();
296
+ }
297
+ }
298
+ if (virtual && !stopWatching) {
299
+ let stop;
300
+ stopWatching = () => stop?.();
301
+ stop = watchView(container, measure);
302
+ } else if (!virtual && stopWatching) {
303
+ stopWatching();
304
+ stopWatching = void 0;
305
+ view = void 0;
306
+ } else if (virtual && watchedLayout !== layout) queueMicrotask(measure);
307
+ watchedLayout = layout;
308
+ }
309
+ function measure() {
310
+ if (destroyed || !config.virtualize) return;
311
+ const overscan = Math.max(0, (typeof config.virtualize === "object" ? config.virtualize.overscan : void 0) ?? 300);
312
+ const next = viewRect(container, layout, overscan);
313
+ if (!sameRect(next, view)) {
314
+ view = next;
315
+ render();
316
+ }
317
+ }
318
+ function startLeaving(tile, duration) {
319
+ tile.leaving = true;
320
+ const el = tile.el;
321
+ for (const a of ["role", "tabindex", "aria-label", "aria-setsize", "aria-posinset", "data-entering"]) el.removeAttribute(a);
322
+ el.setAttribute("aria-hidden", "true");
323
+ el.setAttribute("data-exiting", "");
324
+ leaving.add(tile);
325
+ const timer = setTimeout(() => {
326
+ timers.delete(timer);
327
+ leaving.delete(tile);
328
+ el.remove();
329
+ }, duration);
330
+ timers.add(timer);
331
+ }
332
+ const tileOf = (e) => {
333
+ for (const n of e.composedPath()) {
334
+ if (n === container) return void 0;
335
+ if (n instanceof Element && n.parentNode === container) {
336
+ const t = byElement.get(n);
337
+ return t && !t.leaving ? t : void 0;
338
+ }
339
+ }
340
+ return void 0;
341
+ };
342
+ const setActive = (id) => {
343
+ if (id === activeId) return;
344
+ activeId = id;
345
+ for (const t of tiles.values()) if (focusable.has(t.cell.id)) attr(t.el, "tabindex", t.cell.id === id ? 0 : -1);
346
+ };
347
+ const onClick = (e) => {
348
+ const t = tileOf(e);
349
+ if (t && !t.empty && config.onCellActivate) config.onCellActivate(t.cell, t.state);
350
+ };
351
+ const onFocusIn = (e) => {
352
+ const t = tileOf(e);
353
+ if (t && e.target === t.el && focusable.has(t.cell.id)) setActive(t.cell.id);
354
+ };
355
+ const onKeyDown = (e) => {
356
+ if (e.defaultPrevented || !config.navigable || e.altKey || e.ctrlKey || e.metaKey) return;
357
+ const t = tileOf(e);
358
+ if (!t || e.target !== t.el || !focusable.has(t.cell.id)) return;
359
+ const action = keyAction(layout, t.cell, e.key, (cell) => focusable.has(cell.id), !!config.onCellActivate);
360
+ if (!action) return;
361
+ e.preventDefault();
362
+ if ("activate" in action) config.onCellActivate(t.cell, t.state);
363
+ else if ("move" in action) {
364
+ const to = action.move;
365
+ setActive(to.id);
366
+ const shownTile = [...tiles.values()].find((x) => x.cell.id === to.id);
367
+ if (shownTile) shownTile.el.focus();
368
+ else {
369
+ pendingFocus = to.id;
370
+ render();
371
+ }
372
+ }
373
+ };
374
+ container.addEventListener("click", onClick);
375
+ container.addEventListener("focusin", onFocusIn);
376
+ container.addEventListener("keydown", onKeyDown);
377
+ render();
378
+ return {
379
+ element: container,
380
+ get layout() {
381
+ return layout;
382
+ },
383
+ update(changes) {
384
+ config = { ...config, ...changes };
385
+ render();
386
+ },
387
+ refresh: render,
388
+ tileElement(cellId) {
389
+ for (const t of tiles.values()) if (t.cell.id === cellId) return t.el;
390
+ return void 0;
391
+ },
392
+ destroy() {
393
+ destroyed = true;
394
+ stopWatching?.();
395
+ timers.forEach(clearTimeout);
396
+ container.removeEventListener("click", onClick);
397
+ container.removeEventListener("focusin", onFocusIn);
398
+ container.removeEventListener("keydown", onKeyDown);
399
+ for (const t of [...tiles.values(), ...leaving]) {
400
+ if (t.safe) setChildren(t.safe, []);
401
+ setChildren(t.el, []);
402
+ }
403
+ container.remove();
404
+ }
405
+ };
406
+ }
407
+
408
+ // src/element.ts
409
+ var Base = typeof HTMLElement === "undefined" ? class {
410
+ } : HTMLElement;
411
+ var BOOLEAN = ["navigable", "animate", "relief", "safe-area", "virtualize"];
412
+ var ATTRIBUTES = [...BOOLEAN, "options", "fit", "placement", "content-cells", "choose", "background", "fill", "focus-ring", "label"];
413
+ var json = (v) => {
414
+ try {
415
+ return JSON.parse(v);
416
+ } catch {
417
+ throw new SyntaxError(`<andamento-layout>: not valid JSON: ${v}`);
418
+ }
419
+ };
420
+ var slotCount = 0;
421
+ var _config, _props, _handle, _observer, _queued, _slots, _items, _backdrop, _AndamentoLayoutElement_instances, backdropFor_fn, set_fn, schedule_fn, render_fn, configure_fn;
422
+ var AndamentoLayoutElement = class extends Base {
423
+ constructor() {
424
+ super();
425
+ __privateAdd(this, _AndamentoLayoutElement_instances);
426
+ __privateAdd(this, _config, {});
427
+ __privateAdd(this, _props, {});
428
+ __privateAdd(this, _handle);
429
+ __privateAdd(this, _observer);
430
+ __privateAdd(this, _queued, false);
431
+ __privateAdd(this, _slots, /* @__PURE__ */ new WeakMap());
432
+ __privateAdd(this, _items, []);
433
+ __privateAdd(this, _backdrop);
434
+ const root = this.attachShadow({ mode: "open" });
435
+ const style = document.createElement("style");
436
+ style.textContent = ":host{display:block}:host([hidden]){display:none}";
437
+ root.appendChild(style);
438
+ }
439
+ /** Layout options (also the `options` attribute, as JSON). */
440
+ get options() {
441
+ return __privateGet(this, _props).options;
442
+ }
443
+ set options(v) {
444
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { options: v });
445
+ }
446
+ /** A precomputed layout, instead of `options`. */
447
+ get layout() {
448
+ return __privateGet(this, _handle)?.layout ?? __privateGet(this, _props).layout;
449
+ }
450
+ set layout(v) {
451
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { layout: v });
452
+ }
453
+ /** Color of empty tiles: a color, a palette or a function per cell. */
454
+ get fill() {
455
+ return __privateGet(this, _props).fill;
456
+ }
457
+ set fill(v) {
458
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { fill: v });
459
+ }
460
+ get relief() {
461
+ return __privateGet(this, _props).relief;
462
+ }
463
+ set relief(v) {
464
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { relief: v });
465
+ }
466
+ /** Animate changes (the `animate` attribute; `animate` itself is the Web Animations method). */
467
+ get animated() {
468
+ return __privateGet(this, _props).animate;
469
+ }
470
+ set animated(v) {
471
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { animate: v });
472
+ }
473
+ get virtualize() {
474
+ return __privateGet(this, _props).virtualize;
475
+ }
476
+ set virtualize(v) {
477
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { virtualize: v });
478
+ }
479
+ get navigable() {
480
+ return __privateGet(this, _props).navigable;
481
+ }
482
+ set navigable(v) {
483
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { navigable: v });
484
+ }
485
+ get safeArea() {
486
+ return __privateGet(this, _props).safeArea;
487
+ }
488
+ set safeArea(v) {
489
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { safeArea: v });
490
+ }
491
+ get fit() {
492
+ return __privateGet(this, _props).fit;
493
+ }
494
+ set fit(v) {
495
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { fit: v });
496
+ }
497
+ get placement() {
498
+ return __privateGet(this, _props).placement;
499
+ }
500
+ set placement(v) {
501
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { placement: v });
502
+ }
503
+ get contentCells() {
504
+ return __privateGet(this, _props).contentCells;
505
+ }
506
+ set contentCells(v) {
507
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { contentCells: v });
508
+ }
509
+ get choose() {
510
+ return __privateGet(this, _props).choose;
511
+ }
512
+ set choose(v) {
513
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { choose: v });
514
+ }
515
+ /** Width / height of each child; otherwise read from children's `data-aspect`. */
516
+ get aspects() {
517
+ return __privateGet(this, _props).aspects;
518
+ }
519
+ set aspects(v) {
520
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { aspects: v });
521
+ }
522
+ /** Tiles each child spans; otherwise read from children's `data-weight`. */
523
+ get weights() {
524
+ return __privateGet(this, _props).weights;
525
+ }
526
+ set weights(v) {
527
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { weights: v });
528
+ }
529
+ /** Focal point of each child; otherwise read from children's `data-focus`. */
530
+ get focalPoints() {
531
+ return __privateGet(this, _props).focalPoints;
532
+ }
533
+ set focalPoints(v) {
534
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { focalPoints: v });
535
+ }
536
+ get background() {
537
+ return __privateGet(this, _props).background;
538
+ }
539
+ set background(v) {
540
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { background: v });
541
+ }
542
+ get focusRing() {
543
+ return __privateGet(this, _props).focusRing;
544
+ }
545
+ set focusRing(v) {
546
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { focusRing: v });
547
+ }
548
+ /** Accessible name of each tile. */
549
+ get cellLabel() {
550
+ return __privateGet(this, _props).cellLabel;
551
+ }
552
+ set cellLabel(v) {
553
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { cellLabel: v });
554
+ }
555
+ /** Extra style for each tile. */
556
+ get cellStyle() {
557
+ return __privateGet(this, _props).cellStyle;
558
+ }
559
+ set cellStyle(v) {
560
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { cellStyle: v });
561
+ }
562
+ /** Content for empty tiles (rendered in the shadow root). */
563
+ get fallback() {
564
+ return __privateGet(this, _props).fallback;
565
+ }
566
+ set fallback(v) {
567
+ __privateMethod(this, _AndamentoLayoutElement_instances, set_fn).call(this, { fallback: v });
568
+ }
569
+ /** The tile element (in the shadow root) showing a cell, if it's rendered. */
570
+ tileElement(cellId) {
571
+ return __privateGet(this, _handle)?.tileElement(cellId);
572
+ }
573
+ connectedCallback() {
574
+ __privateSet(this, _observer, new MutationObserver((records) => {
575
+ if (records.some((r) => r.target === this || r.target.parentNode === this)) __privateMethod(this, _AndamentoLayoutElement_instances, schedule_fn).call(this);
576
+ }));
577
+ __privateGet(this, _observer).observe(this, { childList: true, subtree: true, attributes: true, attributeFilter: ["data-aspect", "data-focus", "data-weight", "slot"] });
578
+ __privateMethod(this, _AndamentoLayoutElement_instances, schedule_fn).call(this);
579
+ }
580
+ disconnectedCallback() {
581
+ __privateGet(this, _observer)?.disconnect();
582
+ __privateSet(this, _observer, void 0);
583
+ __privateGet(this, _handle)?.destroy();
584
+ __privateSet(this, _handle, void 0);
585
+ }
586
+ attributeChangedCallback() {
587
+ __privateMethod(this, _AndamentoLayoutElement_instances, schedule_fn).call(this);
588
+ }
589
+ /** Render now rather than at the end of the current task (e.g. before measuring). */
590
+ render() {
591
+ __privateMethod(this, _AndamentoLayoutElement_instances, render_fn).call(this);
592
+ }
593
+ };
594
+ _config = new WeakMap();
595
+ _props = new WeakMap();
596
+ _handle = new WeakMap();
597
+ _observer = new WeakMap();
598
+ _queued = new WeakMap();
599
+ _slots = new WeakMap();
600
+ _items = new WeakMap();
601
+ _backdrop = new WeakMap();
602
+ _AndamentoLayoutElement_instances = new WeakSet();
603
+ /** The same function for the same backdrop child, so tiles keep their copies. */
604
+ backdropFor_fn = function(el) {
605
+ if (__privateGet(this, _backdrop)?.el !== el) __privateSet(this, _backdrop, { el, make: () => copyBackdrop(el) });
606
+ return __privateGet(this, _backdrop).make;
607
+ };
608
+ set_fn = function(changes) {
609
+ __privateSet(this, _props, { ...__privateGet(this, _props), ...changes });
610
+ __privateMethod(this, _AndamentoLayoutElement_instances, schedule_fn).call(this);
611
+ };
612
+ /** Several changes in one go render once. */
613
+ schedule_fn = function() {
614
+ if (__privateGet(this, _queued)) return;
615
+ __privateSet(this, _queued, true);
616
+ queueMicrotask(() => {
617
+ __privateSet(this, _queued, false);
618
+ __privateMethod(this, _AndamentoLayoutElement_instances, render_fn).call(this);
619
+ });
620
+ };
621
+ render_fn = function() {
622
+ if (!this.isConnected) return;
623
+ const config = __privateMethod(this, _AndamentoLayoutElement_instances, configure_fn).call(this);
624
+ if (!config.options && !config.layout) return;
625
+ if (__privateGet(this, _handle)) __privateGet(this, _handle).update(config);
626
+ else {
627
+ __privateSet(this, _handle, mountTileLayout(this.shadowRoot, config));
628
+ __privateGet(this, _handle).element.setAttribute("part", "layout");
629
+ }
630
+ };
631
+ configure_fn = function() {
632
+ const a = (n) => this.getAttribute(n);
633
+ const flag = (n) => this.hasAttribute(n) && a(n) !== "false";
634
+ const fromAttrs = Object.fromEntries([...ATTRIBUTE_KEYS].map((k) => [k, void 0]));
635
+ if (a("options")) fromAttrs.options = json(a("options"));
636
+ for (const n of BOOLEAN) if (flag(n)) fromAttrs[n === "safe-area" ? "safeArea" : n] = true;
637
+ if (a("safe-area") === "tile" || a("safe-area") === "visible") fromAttrs.safeArea = a("safe-area");
638
+ if (a("fit")) fromAttrs.fit = a("fit") === "nearest" ? "nearest" : a("fit") === "false" ? false : true;
639
+ if (a("placement")) fromAttrs.placement = a("placement");
640
+ if (a("content-cells")) fromAttrs.contentCells = a("content-cells");
641
+ if (a("choose")) fromAttrs.choose = a("choose");
642
+ if (a("background")) fromAttrs.background = a("background");
643
+ if (a("fill")) fromAttrs.fill = a("fill").trim().startsWith("[") ? json(a("fill")) : a("fill");
644
+ if (a("focus-ring")) fromAttrs.focusRing = a("focus-ring") === "false" ? false : a("focus-ring");
645
+ if (a("label")) fromAttrs.label = a("label");
646
+ const children = [...this.children];
647
+ const backdrop = children.find((c) => c.getAttribute("slot") === "backdrop");
648
+ __privateSet(this, _items, children.filter((c) => c !== backdrop && c.localName !== "template"));
649
+ const items = __privateGet(this, _items).map((child) => {
650
+ let slot = __privateGet(this, _slots).get(child);
651
+ if (!slot) {
652
+ slot = document.createElement("slot");
653
+ slot.name = `andamento-${++slotCount}`;
654
+ __privateGet(this, _slots).set(child, slot);
655
+ }
656
+ if (child.getAttribute("slot") !== slot.name) child.setAttribute("slot", slot.name);
657
+ return slot;
658
+ });
659
+ const aspectAttrs = __privateGet(this, _items).map((c) => Number(c.getAttribute("data-aspect")));
660
+ const aspects = aspectAttrs.some((v) => v > 0) ? aspectAttrs.map((v) => v > 0 ? v : 1) : void 0;
661
+ const focusAttrs = __privateGet(this, _items).map((c) => {
662
+ const [x, y] = (c.getAttribute("data-focus") ?? "").trim().split(/[\s,]+/).map(Number);
663
+ return Number.isFinite(x) && Number.isFinite(y) && c.hasAttribute("data-focus") ? { x, y } : void 0;
664
+ });
665
+ const focalPoints = focusAttrs.some(Boolean) ? focusAttrs : void 0;
666
+ const weightAttrs = __privateGet(this, _items).map((c) => Number(c.getAttribute("data-weight")));
667
+ const weights = weightAttrs.some((v) => v > 1) ? weightAttrs.map((v) => v > 0 ? v : 1) : void 0;
668
+ __privateSet(this, _config, {
669
+ ...fromAttrs,
670
+ aspects,
671
+ focalPoints,
672
+ weights,
673
+ ...overrides(__privateGet(this, _props)),
674
+ items,
675
+ backdrop: backdrop ? __privateMethod(this, _AndamentoLayoutElement_instances, backdropFor_fn).call(this, backdrop) : void 0,
676
+ cellAttributes: (cell, state) => ({
677
+ part: ["tile", state.empty && "empty", cell.boundary && "boundary"].filter(Boolean).join(" ")
678
+ }),
679
+ onCellActivate: (cell, state) => {
680
+ const detail = { cell, slot: state.slot, item: __privateGet(this, _items)[state.slot] };
681
+ this.dispatchEvent(new CustomEvent("cellactivate", { detail, bubbles: true, composed: true }));
682
+ }
683
+ });
684
+ return __privateGet(this, _config);
685
+ };
686
+ AndamentoLayoutElement.observedAttributes = ATTRIBUTES;
687
+ var overrides = (props) => Object.fromEntries(Object.entries(props).filter(([k, v]) => v !== void 0 || !ATTRIBUTE_KEYS.has(k)));
688
+ var ATTRIBUTE_KEYS = /* @__PURE__ */ new Set(["options", "navigable", "animate", "relief", "safeArea", "virtualize", "fit", "placement", "contentCells", "choose", "background", "fill", "focusRing", "label", "aspects", "focalPoints", "weights"]);
689
+ function copyBackdrop(el) {
690
+ const copy = el.cloneNode(true);
691
+ copy.removeAttribute("slot");
692
+ if (/^(img|video|picture|canvas)$/.test(copy.localName)) {
693
+ for (const [k, v] of Object.entries(coverStyle)) copy.style.setProperty(k.replace(/[A-Z]/g, (m) => "-" + m.toLowerCase()), String(v));
694
+ }
695
+ return copy;
696
+ }
697
+ function defineTileLayoutElement(tagName = "andamento-layout") {
698
+ if (typeof customElements === "undefined") return AndamentoLayoutElement;
699
+ const existing = customElements.get(tagName);
700
+ if (existing) return existing;
701
+ const cls = class extends AndamentoLayoutElement {
702
+ };
703
+ customElements.define(tagName, cls);
704
+ return cls;
705
+ }
706
+
707
+ // src/pointer.ts
708
+ import { cellAt, screenToLayout } from "@andamento/core";
709
+ function cellFromPointer(layout, event, options = {}) {
710
+ const el = options.element ?? event.currentTarget;
711
+ if (!el || typeof el.getBoundingClientRect !== "function") return void 0;
712
+ const point = screenToLayout(layout, el.getBoundingClientRect(), event.clientX, event.clientY);
713
+ return cellAt(layout, point, options);
714
+ }
715
+
716
+ // src/index.ts
717
+ import { defaultPalette, neutralPalette } from "@andamento/svg";
718
+ export {
719
+ AndamentoLayoutElement,
720
+ cellFromPointer,
721
+ coverStyle,
722
+ defaultPalette,
723
+ defineTileLayoutElement,
724
+ mountTileLayout,
725
+ neutralPalette
726
+ };
727
+ //# sourceMappingURL=index.js.map