@ohmygame/plugin-ui 2.4.0 → 2.35.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/ui/1/data.js ADDED
@@ -0,0 +1,238 @@
1
+ import {
2
+ OmgElement,
3
+ css,
4
+ define,
5
+ text,
6
+ activeElement,
7
+ containsFocus,
8
+ } from "./base.js";
9
+
10
+ /**
11
+ * `<omg-table label>`: a semantic table with stable keyed rows and cells.
12
+ * Text is always inserted as text; a Node cell is slotted without cloning so
13
+ * its state, listeners and plugin styles survive updates. Callers own sorting,
14
+ * loading and actions. Wide tables scroll inside their labelled region.
15
+ *
16
+ * @attr {string} label - Table and scroll-region accessible name.
17
+ * @attr {boolean} loading - Marks data busy; existing rows remain visible.
18
+ * @attr {string} empty-label - Message with no rows; defaults to No data.
19
+ * @prop {Array<{ key: string; heading: string; align?: "left" | "right"; width?: string }>} columns - Unique column keys, headings, optional width and alignment.
20
+ * @prop {Array<{ key: string; cells: Record<string, string | Node> }>} rows - Unique row keys and cells by column key; Node cells belong to this table while mounted.
21
+ * @part region - Focusable horizontal scroll region.
22
+ * @part table - Native table.
23
+ * @part empty - Empty message.
24
+ */
25
+ class OmgTable extends OmgElement {
26
+ static observedAttributes = ["label", "loading", "empty-label"];
27
+ static styles = [
28
+ css(`
29
+ :host { display: block; min-width: 0; }
30
+ .region { overflow: auto; }
31
+ table { width: 100%; border-collapse: collapse; font: inherit; }
32
+ th { height: 34px; color: var(--color-text-tertiary); font-size: var(--text-sm); font-weight: var(--weight-medium); text-align: start; }
33
+ th, td { padding: var(--space-2) var(--space-3); border-bottom: 1px solid var(--color-border); }
34
+ td { height: 48px; overflow-wrap: anywhere; }
35
+ .empty { padding: var(--space-3); color: var(--color-text-secondary); }
36
+ `),
37
+ ];
38
+ static template = `<div class="region" part="region" role="region" tabindex="0"><table part="table"><thead><tr></tr></thead><tbody></tbody></table><div class="empty" part="empty" role="status"></div></div>`;
39
+ #columns = [];
40
+ #rows = [];
41
+ #mounted = new Map();
42
+ #nextSlot = 0;
43
+
44
+ get columns() {
45
+ return this.#columns;
46
+ }
47
+ set columns(value) {
48
+ this.#columns = value;
49
+ this.#draw();
50
+ }
51
+ get rows() {
52
+ return this.#rows;
53
+ }
54
+ set rows(value) {
55
+ this.#rows = value;
56
+ this.#draw();
57
+ }
58
+
59
+ #removeCell(cell) {
60
+ cell.node?.remove();
61
+ cell.element.remove();
62
+ }
63
+ #draw() {
64
+ const focused = containsFocus(this)
65
+ ? activeElement(this.ownerDocument)
66
+ : null;
67
+ const head = this.shadowRoot.querySelector("thead tr");
68
+ head.replaceChildren(
69
+ ...this.#columns.map((column) => {
70
+ const th = document.createElement("th");
71
+ th.scope = "col";
72
+ th.textContent = column.heading;
73
+ th.style.textAlign = column.align || "start";
74
+ th.style.width = column.width || "";
75
+ return th;
76
+ }),
77
+ );
78
+ const keys = new Set(this.#rows.map((row) => row.key));
79
+ for (const [key, row] of this.#mounted) {
80
+ if (keys.has(key)) continue;
81
+ for (const cell of row.cells.values()) this.#removeCell(cell);
82
+ row.element.remove();
83
+ this.#mounted.delete(key);
84
+ }
85
+ const body = this.shadowRoot.querySelector("tbody");
86
+ this.#rows.forEach((row, index) => {
87
+ let mounted = this.#mounted.get(row.key);
88
+ if (!mounted) {
89
+ mounted = { element: document.createElement("tr"), cells: new Map() };
90
+ this.#mounted.set(row.key, mounted);
91
+ }
92
+ for (const [key, cell] of mounted.cells) {
93
+ if (!this.#columns.some((column) => column.key === key)) {
94
+ this.#removeCell(cell);
95
+ mounted.cells.delete(key);
96
+ }
97
+ }
98
+ this.#columns.forEach((column, columnIndex) => {
99
+ let cell = mounted.cells.get(column.key);
100
+ if (!cell) {
101
+ cell = { element: document.createElement("td"), value: undefined };
102
+ mounted.cells.set(column.key, cell);
103
+ }
104
+ cell.element.style.textAlign = column.align || "start";
105
+ const value = row.cells[column.key] ?? "";
106
+ if (cell.value !== value) {
107
+ cell.node?.remove();
108
+ cell.node = undefined;
109
+ cell.value = value;
110
+ if (value instanceof Node) {
111
+ // A wrapper also supports Text nodes and avoids changing the caller's slot attribute.
112
+ const wrapper = document.createElement("span");
113
+ wrapper.slot = `cell-${++this.#nextSlot}`;
114
+ wrapper.append(value);
115
+ this.append(wrapper);
116
+ const slot = document.createElement("slot");
117
+ slot.name = wrapper.slot;
118
+ cell.element.replaceChildren(slot);
119
+ cell.node = wrapper;
120
+ } else cell.element.textContent = String(value);
121
+ }
122
+ if (mounted.element.children[columnIndex] !== cell.element)
123
+ mounted.element.insertBefore(
124
+ cell.element,
125
+ mounted.element.children[columnIndex] || null,
126
+ );
127
+ });
128
+ if (body.children[index] !== mounted.element)
129
+ body.insertBefore(mounted.element, body.children[index] || null);
130
+ });
131
+ this.update();
132
+ if (focused?.isConnected && activeElement(this.ownerDocument) !== focused)
133
+ focused.focus({ preventScroll: true });
134
+ }
135
+ update() {
136
+ const label = this.getAttribute("label") || "";
137
+ this.part("region").setAttribute("aria-label", label);
138
+ this.part("table").setAttribute("aria-label", label);
139
+ this.part("table").setAttribute(
140
+ "aria-busy",
141
+ String(this.hasAttribute("loading")),
142
+ );
143
+ text(
144
+ this.part("empty"),
145
+ this.#rows.length || this.hasAttribute("loading")
146
+ ? ""
147
+ : this.getAttribute("empty-label") || "No data",
148
+ );
149
+ }
150
+ }
151
+
152
+ /**
153
+ * `<omg-pagination page pages>`: controlled pagination. It requests a page;
154
+ * the caller updates `page` after success. Pending requests preserve focus.
155
+ *
156
+ * @attr {number} page - Current page, one-based; defaults to 1.
157
+ * @attr {number} pages - Number of pages; at least 1.
158
+ * @attr {boolean} disabled - Ignores requests while retaining focus.
159
+ * @attr {string} label - Navigation accessible name; defaults to Pagination.
160
+ * @attr {string} previous-label - Previous button text; defaults to Previous.
161
+ * @attr {string} next-label - Next button text; defaults to Next.
162
+ * @fires {{ page: number }} omg-page-change - Requested page; no state changes until the caller updates page.
163
+ * @part previous - Previous button.
164
+ * @part next - Next button.
165
+ * @part status - Current page and total.
166
+ */
167
+ class OmgPagination extends OmgElement {
168
+ static observedAttributes = [
169
+ "page",
170
+ "pages",
171
+ "disabled",
172
+ "label",
173
+ "previous-label",
174
+ "next-label",
175
+ ];
176
+ static styles = [
177
+ css(`
178
+ :host { display: block; }
179
+ nav { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: var(--space-2); }
180
+ button { min-height: 30px; padding: 0 var(--space-2); border: 0; border-radius: var(--radius-md); background: transparent; color: var(--color-text); font: inherit; cursor: pointer; }
181
+ button:not([aria-disabled="true"]):hover { background: var(--color-hover); }
182
+ button[aria-disabled="true"] { opacity: .4; cursor: default; }
183
+ .status { color: var(--color-text-secondary); font-variant-numeric: tabular-nums; }
184
+ `),
185
+ ];
186
+ static template = `<nav><button type="button" part="previous"></button><span class="status" part="status" role="status" aria-live="polite"></span><button type="button" part="next"></button></nav>`;
187
+ constructor() {
188
+ super();
189
+ for (const [part, delta] of [
190
+ ["previous", -1],
191
+ ["next", 1],
192
+ ])
193
+ this.part(part).addEventListener("click", () => {
194
+ if (this.hasAttribute("disabled")) return;
195
+ const [page, pages] = this.#range();
196
+ const next = page + delta;
197
+ if (next >= 1 && next <= pages)
198
+ this.dispatchEvent(
199
+ new CustomEvent("omg-page-change", {
200
+ bubbles: true,
201
+ composed: true,
202
+ detail: { page: next },
203
+ }),
204
+ );
205
+ });
206
+ }
207
+ #range() {
208
+ const number = (name) => {
209
+ const value = Number(this.getAttribute(name));
210
+ return Number.isFinite(value) ? Math.max(1, Math.floor(value)) : 1;
211
+ };
212
+ const pages = number("pages");
213
+ return [Math.min(pages, number("page")), pages];
214
+ }
215
+ update() {
216
+ const [page, pages] = this.#range();
217
+ this.shadowRoot
218
+ .querySelector("nav")
219
+ .setAttribute("aria-label", this.getAttribute("label") || "Pagination");
220
+ text(
221
+ this.part("previous"),
222
+ this.getAttribute("previous-label") || "Previous",
223
+ );
224
+ text(this.part("next"), this.getAttribute("next-label") || "Next");
225
+ text(this.part("status"), `${page} / ${pages}`);
226
+ this.part("previous").setAttribute(
227
+ "aria-disabled",
228
+ String(this.hasAttribute("disabled") || page <= 1),
229
+ );
230
+ this.part("next").setAttribute(
231
+ "aria-disabled",
232
+ String(this.hasAttribute("disabled") || page >= pages),
233
+ );
234
+ }
235
+ }
236
+
237
+ define("omg-table", OmgTable);
238
+ define("omg-pagination", OmgPagination);
package/ui/1/feedback.js CHANGED
@@ -2,9 +2,10 @@
2
2
  import { OmgElement, css, define, filled, text } from "./base.js";
3
3
 
4
4
  const TONES = css(`
5
- :host { --tone: var(--color-border-strong); }
5
+ :host { --tone: var(--color-border-strong); --tone-fill: var(--tone); }
6
6
  :host([tone="good"]) { --tone: var(--color-success); }
7
- :host([tone="warning"]) { --tone: var(--color-warning); }
7
+ /* Radix Amber 11 for foregrounds; Amber 9 for tinted surfaces. */
8
+ :host([tone="warning"]) { --tone: light-dark(#ab6400, #ffca16); --tone-fill: #ffc53d; }
8
9
  :host([tone="bad"]) { --tone: var(--color-danger); }
9
10
  /* The plugin's brand colour marks progress only; information is the app's accent. */
10
11
  :host([tone="info"]) { --tone: var(--color-accent); }
@@ -71,7 +72,7 @@ class OmgBanner extends OmgElement {
71
72
  css(`
72
73
  :host { --tone: var(--color-text-tertiary); display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3);
73
74
  padding: var(--space-3); border-radius: var(--radius-md);
74
- background: color-mix(in srgb, var(--tone) 12%, transparent); }
75
+ background: color-mix(in srgb, var(--tone-fill) 12%, transparent); }
75
76
  .content { flex: 1 1 240px; min-width: 0; display: grid; gap: var(--space-1); overflow-wrap: anywhere; }
76
77
  .heading { margin: 0; font-weight: var(--weight-semibold); color: var(--tone); }
77
78
  :host(:not([tone])) .heading { color: var(--color-text); }
@@ -271,7 +272,7 @@ class OmgBadge extends OmgElement {
271
272
  css(`
272
273
  :host {
273
274
  --tone: var(--color-text-secondary); display: inline-flex; align-items: center; height: 20px; padding: 0 7px; width: fit-content;
274
- border-radius: var(--radius-pill); background: color-mix(in srgb, var(--tone) 16%, transparent); color: var(--tone);
275
+ border-radius: var(--radius-pill); background: color-mix(in srgb, var(--tone-fill) 16%, transparent); color: var(--tone);
275
276
  font-size: var(--text-xs); font-weight: var(--weight-medium); white-space: nowrap;
276
277
  }
277
278
  `),
@@ -0,0 +1,111 @@
1
+ // Shared mechanics for panel-local overlays. The browser's top layer escapes
2
+ // clipping ancestors, but deliberately stays within the plugin's iframe.
3
+ import { css, containsFocus } from "./base.js";
4
+
5
+ export const FLOATING = css(`
6
+ .floating {
7
+ position: fixed; inset: auto; margin: 0; border: 0; padding: var(--space-1);
8
+ max-width: calc(100vw - 16px); max-height: min(420px, calc(100vh - 16px)); overflow: auto;
9
+ border-radius: var(--radius-lg); background: var(--color-surface-raised);
10
+ color: var(--color-text); box-shadow: var(--shadow-float); font: inherit;
11
+ }
12
+ `);
13
+
14
+ const opened = [];
15
+
16
+ /** One overlay's listeners and observers live exactly as long as it is open. */
17
+ export class Floating {
18
+ constructor(host, anchor, panel, dismiss, placement = () => "bottom-start") {
19
+ this.host = host;
20
+ this.anchor = anchor;
21
+ this.panel = panel;
22
+ this.dismiss = dismiss;
23
+ this.placement = placement;
24
+ }
25
+
26
+ show() {
27
+ if (this.stop) {
28
+ this.position();
29
+ return;
30
+ }
31
+ this.panel.hidden = false;
32
+ this.panel.showPopover();
33
+ opened.push(this);
34
+ const dismissOutside = (event) => {
35
+ if (opened.at(-1) === this && !event.composedPath().includes(this.host))
36
+ this.dismiss(false);
37
+ };
38
+ const key = (event) => {
39
+ if (opened.at(-1) === this && event.key === "Escape") {
40
+ event.preventDefault();
41
+ event.stopPropagation();
42
+ this.dismiss(containsFocus(this.host));
43
+ }
44
+ };
45
+ const position = () => this.position();
46
+ const win = this.host.ownerDocument.defaultView;
47
+ const resized = new ResizeObserver(position);
48
+ resized.observe(this.anchor);
49
+ resized.observe(this.panel);
50
+ win.addEventListener("resize", position);
51
+ win.addEventListener("scroll", position, true);
52
+ this.host.ownerDocument.addEventListener(
53
+ "pointerdown",
54
+ dismissOutside,
55
+ true,
56
+ );
57
+ this.host.ownerDocument.addEventListener("keydown", key, true);
58
+ // Observe the destination, not activeElement during focusout: Chromium can
59
+ // temporarily report body between a top-layer panel and its slotted input.
60
+ this.host.ownerDocument.addEventListener("focusin", dismissOutside, true);
61
+ this.stop = () => {
62
+ resized.disconnect();
63
+ win.removeEventListener("resize", position);
64
+ win.removeEventListener("scroll", position, true);
65
+ this.host.ownerDocument.removeEventListener(
66
+ "pointerdown",
67
+ dismissOutside,
68
+ true,
69
+ );
70
+ this.host.ownerDocument.removeEventListener("keydown", key, true);
71
+ this.host.ownerDocument.removeEventListener(
72
+ "focusin",
73
+ dismissOutside,
74
+ true,
75
+ );
76
+ const index = opened.indexOf(this);
77
+ if (index !== -1) opened.splice(index, 1);
78
+ };
79
+ this.position();
80
+ }
81
+
82
+ position() {
83
+ const panel = this.panel;
84
+ const win = this.host.ownerDocument.defaultView;
85
+ const a = this.anchor.getBoundingClientRect();
86
+ const placement = this.placement();
87
+ if (this.matchWidth)
88
+ panel.style.width = `${Math.min(a.width, Math.max(0, win.innerWidth - 16))}px`;
89
+ const below = Math.max(0, win.innerHeight - a.bottom - 14);
90
+ const above = Math.max(0, a.top - 14);
91
+ const up = placement.startsWith("top")
92
+ ? above >= panel.scrollHeight || above > below
93
+ : below < panel.scrollHeight && above > below;
94
+ panel.style.maxHeight = `${Math.min(420, up ? above : below)}px`;
95
+ const { width, height } = panel.getBoundingClientRect();
96
+ const left = placement.endsWith("end")
97
+ ? a.right - width
98
+ : placement === "top" || placement === "bottom"
99
+ ? a.left + (a.width - width) / 2
100
+ : a.left;
101
+ panel.style.left = `${Math.max(8, Math.min(left, win.innerWidth - width - 8))}px`;
102
+ panel.style.top = `${Math.max(8, Math.min(up ? a.top - height - 6 : a.bottom + 6, win.innerHeight - height - 8))}px`;
103
+ }
104
+
105
+ hide() {
106
+ this.stop?.();
107
+ this.stop = undefined;
108
+ if (this.panel.isConnected && !this.panel.hidden) this.panel.hidePopover();
109
+ this.panel.hidden = true;
110
+ }
111
+ }