@ohmygame/plugin-ui 0.0.0-stage → 2.3.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.
@@ -0,0 +1,340 @@
1
+ // Feedback: a status line, a banner, a log and a list of steps.
2
+ import { OmgElement, css, define, filled, text } from "./base.js";
3
+
4
+ const TONES = css(`
5
+ :host { --tone: var(--color-border-strong); }
6
+ :host([tone="good"]) { --tone: var(--color-success); }
7
+ :host([tone="warning"]) { --tone: var(--color-warning); }
8
+ :host([tone="bad"]) { --tone: var(--color-danger); }
9
+ /* The plugin's brand colour marks progress only; information is the app's accent. */
10
+ :host([tone="info"]) { --tone: var(--color-accent); }
11
+ :host([tone="busy"]) { --tone: var(--omg-accent, var(--color-accent)); }
12
+ `);
13
+
14
+ /** Gives an element a live-region role unless the page chose one. */
15
+ function announce(element, role) {
16
+ if (!element.hasAttribute("role") || element.dataset.omgRole === element.getAttribute("role")) {
17
+ element.setAttribute("role", role);
18
+ element.dataset.omgRole = role;
19
+ }
20
+ }
21
+
22
+ /**
23
+ * `<omg-status tone="neutral|good|warning|bad|busy">`: a short state, such as
24
+ * "Running", after a coloured dot, or a spinner while busy. It is a live
25
+ * region, so a change is read out.
26
+ *
27
+ * @attr {"neutral" | "good" | "warning" | "bad" | "busy"} tone - Neutral by default.
28
+ * @slot - The state, in a few words.
29
+ * @part indicator - The dot.
30
+ * @part spinner - The spinner shown while busy.
31
+ * @part label - The state.
32
+ * @cssprop --omg-accent - The plugin's brand colour, for the busy spinner.
33
+ */
34
+ class OmgStatus extends OmgElement {
35
+ static observedAttributes = ["tone"];
36
+ static styles = [
37
+ TONES,
38
+ css(`
39
+ :host { display: inline-flex; align-items: center; gap: var(--space-2); min-width: 0; }
40
+ .dot { width: 8px; height: 8px; flex: none; border-radius: var(--radius-pill); background: var(--tone); }
41
+ .spinner { color: var(--tone); }
42
+ `),
43
+ ];
44
+ static template = `<span class="dot" part="indicator"></span><span class="spinner" part="spinner" hidden></span><span part="label"><slot></slot></span>`;
45
+
46
+ update() {
47
+ announce(this, "status");
48
+ const busy = this.getAttribute("tone") === "busy";
49
+ this.part("indicator").hidden = busy;
50
+ this.part("spinner").hidden = !busy;
51
+ }
52
+ }
53
+
54
+ /**
55
+ * `<omg-banner tone="info|good|warning|bad" heading>`: a message across the
56
+ * page, such as a failure and what to do about it. Slots: the message, and
57
+ * `actions`. A `bad` banner is an alert; the others are a status.
58
+ *
59
+ * @attr {"info" | "good" | "warning" | "bad"} tone - Plain by default.
60
+ * @attr {string} heading - A title above the message.
61
+ * @slot - The message.
62
+ * @slot actions - Buttons after the message.
63
+ * @part heading - The title.
64
+ * @part message - The message.
65
+ * @part actions - The buttons' row; hidden when empty.
66
+ */
67
+ class OmgBanner extends OmgElement {
68
+ static observedAttributes = ["tone", "heading"];
69
+ static styles = [
70
+ TONES,
71
+ css(`
72
+ :host { --tone: var(--color-text-tertiary); display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2) var(--space-3);
73
+ padding: var(--space-3); border-radius: var(--radius-md);
74
+ background: color-mix(in srgb, var(--tone) 12%, transparent); }
75
+ .content { flex: 1 1 240px; min-width: 0; display: grid; gap: var(--space-1); overflow-wrap: anywhere; }
76
+ .heading { margin: 0; font-weight: var(--weight-semibold); color: var(--tone); }
77
+ :host(:not([tone])) .heading { color: var(--color-text); }
78
+ .actions { display: flex; flex-wrap: wrap; gap: var(--space-2); }
79
+ /* A see-through button turns muddy on the banner's colour. */
80
+ ::slotted(omg-button:not([variant])), ::slotted(omg-command-button:not([variant])) { --_button-background: var(--color-surface); }
81
+ `),
82
+ ];
83
+ static template = `
84
+ <div class="content"><p class="heading" part="heading"></p><div class="message" part="message"><slot></slot></div></div>
85
+ <div class="actions" part="actions"><slot name="actions"></slot></div>`;
86
+
87
+ constructor() {
88
+ super();
89
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
90
+ }
91
+
92
+ update() {
93
+ announce(this, this.getAttribute("tone") === "bad" ? "alert" : "status");
94
+ text(this.part("heading"), this.getAttribute("heading"));
95
+ this.part("actions").hidden = !filled(this.shadowRoot.querySelector('slot[name="actions"]'));
96
+ }
97
+ }
98
+
99
+ /**
100
+ * `<omg-log empty="No lines yet." follow>`: lines of output in the app's
101
+ * monospace type, scrolling past `--omg-log-height` (280px). Its text is
102
+ * its content, or set `text`. With `follow`, it keeps the latest line in
103
+ * view unless the person scrolled up.
104
+ *
105
+ * @attr {string} empty - What it says with no lines.
106
+ * @attr {boolean} follow - Keeps the latest line in view.
107
+ * @prop {string} text - Its lines.
108
+ * @slot - Its lines, as text.
109
+ * @part log - The lines; hidden with none.
110
+ * @part empty - The text shown with no lines.
111
+ * @cssprop [--omg-log-height=280px] - How tall it grows before it scrolls.
112
+ */
113
+ class OmgLog extends OmgElement {
114
+ static observedAttributes = ["empty"];
115
+ static styles = [
116
+ css(`
117
+ :host { display: block; }
118
+ pre {
119
+ margin: 0; max-height: var(--omg-log-height, 280px); overflow: auto; padding: var(--space-2) var(--space-3);
120
+ border-radius: var(--radius-md); background: var(--color-stage); box-shadow: inset 0 0 0 1px var(--color-border);
121
+ font-family: var(--font-mono); font-size: var(--text-sm); line-height: 1.45; white-space: pre-wrap; overflow-wrap: anywhere;
122
+ }
123
+ .empty { margin: 0; padding: var(--space-2) 0; color: var(--color-text-tertiary); }
124
+ `),
125
+ ];
126
+ static template = `<pre part="log" tabindex="0"><slot></slot></pre><p class="empty" part="empty"></p>`;
127
+
128
+ #observer = new MutationObserver(() => this.update());
129
+ #atEnd = true;
130
+
131
+ constructor() {
132
+ super();
133
+ const log = this.part("log");
134
+ log.addEventListener("scroll", () => {
135
+ this.#atEnd = log.scrollHeight - log.scrollTop - log.clientHeight < 8;
136
+ });
137
+ }
138
+
139
+ get text() {
140
+ return this.textContent;
141
+ }
142
+
143
+ set text(value) {
144
+ this.textContent = value ?? "";
145
+ }
146
+
147
+ connectedCallback() {
148
+ this.#observer.observe(this, { childList: true, characterData: true, subtree: true });
149
+ super.connectedCallback();
150
+ }
151
+
152
+ disconnectedCallback() {
153
+ this.#observer.disconnect();
154
+ }
155
+
156
+ update() {
157
+ const empty = !this.textContent;
158
+ this.part("log").hidden = empty;
159
+ text(this.part("empty"), empty ? this.getAttribute("empty") : "");
160
+ if (!empty && this.hasAttribute("follow") && this.#atEnd) {
161
+ const log = this.part("log");
162
+ log.scrollTop = log.scrollHeight;
163
+ }
164
+ }
165
+ }
166
+
167
+ const CHECK = `<svg viewBox="0 0 16 16" width="12" height="12" aria-hidden="true"><path d="M3.5 8.5l3 3 6-7" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>`;
168
+
169
+ /**
170
+ * `<omg-steps>`: the steps of a flow, numbered in order. Each is an
171
+ * `<omg-step heading state="pending|current|done|error" open>`; a current or
172
+ * failed step shows its content, and `open` shows it in any state. Slots of
173
+ * a step: `summary`, always shown under its heading, and its content.
174
+ *
175
+ * @slot - Its `omg-step` elements.
176
+ */
177
+ class OmgSteps extends OmgElement {
178
+ static styles = [css(`:host { display: flex; flex-direction: column; }`)];
179
+ static template = `<slot></slot>`;
180
+
181
+ constructor() {
182
+ super();
183
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
184
+ }
185
+
186
+ update() {
187
+ this.setAttribute("role", "list");
188
+ const steps = [...this.children].filter((child) => child.localName === "omg-step");
189
+ steps.forEach((step, index) => step.setAttribute("number", String(index + 1)));
190
+ }
191
+ }
192
+
193
+ /**
194
+ * `<omg-step heading state open>`: one step of an `omg-steps` flow.
195
+ *
196
+ * @attr {string} heading - What the step does.
197
+ * @attr {"pending" | "current" | "done" | "error"} state - Pending by default.
198
+ * @attr {boolean} open - Shows its content in any state.
199
+ * @attr {string} number - Its place in the flow, which `omg-steps` sets.
200
+ * @slot summary - Always shown under the heading, such as what a finished step did.
201
+ * @slot - What the step shows while it is current or failed.
202
+ * @part marker - The number, check or failure mark.
203
+ * @part heading - The heading.
204
+ * @part summary - The summary; hidden when empty.
205
+ * @part body - The content.
206
+ */
207
+ class OmgStep extends OmgElement {
208
+ static observedAttributes = ["heading", "state", "open", "number"];
209
+ static styles = [
210
+ css(`
211
+ :host { display: grid; grid-template-columns: 24px 1fr; column-gap: var(--space-3); }
212
+ .marker {
213
+ position: relative; display: flex; justify-content: center;
214
+ }
215
+ .badge {
216
+ position: relative; z-index: 1; width: 24px; height: 24px; display: grid; place-items: center;
217
+ border-radius: var(--radius-pill); box-shadow: inset 0 0 0 1px var(--color-border); color: var(--color-text-secondary);
218
+ font-size: var(--text-xs); font-weight: var(--weight-semibold); font-variant-numeric: tabular-nums;
219
+ }
220
+ .marker::after {
221
+ content: ""; position: absolute; top: 28px; bottom: 4px; width: 1px; background: var(--color-border);
222
+ }
223
+ :host(:last-of-type) .marker::after { display: none; }
224
+ :host([state="current"]) .badge { background: var(--color-ink); box-shadow: none; color: var(--color-on-ink); }
225
+ :host([state="done"]) .badge { background: color-mix(in srgb, var(--color-success) 16%, transparent); box-shadow: none; color: var(--color-success); }
226
+ :host([state="error"]) .badge { background: color-mix(in srgb, var(--color-danger) 16%, transparent); box-shadow: none; color: var(--color-danger); }
227
+ .content { min-width: 0; display: grid; gap: var(--space-1); padding-bottom: var(--space-6); }
228
+ :host(:last-of-type) .content { padding-bottom: 0; }
229
+ .heading { margin: 0; min-height: 24px; display: flex; align-items: center; font-weight: var(--weight-medium); }
230
+ :host([state="pending"]) .heading, :host(:not([state])) .heading { color: var(--color-text-secondary); }
231
+ .summary { color: var(--color-text-secondary); }
232
+ .body { display: flex; flex-direction: column; gap: var(--space-3); padding-top: var(--space-2); }
233
+ `),
234
+ ];
235
+ static template = `
236
+ <div class="marker"><span class="badge" part="marker"></span></div>
237
+ <div class="content">
238
+ <p class="heading" part="heading"></p>
239
+ <div class="summary" part="summary"><slot name="summary"></slot></div>
240
+ <div class="body" part="body"><slot></slot></div>
241
+ </div>`;
242
+
243
+ constructor() {
244
+ super();
245
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
246
+ }
247
+
248
+ update() {
249
+ this.setAttribute("role", "listitem");
250
+ const state = this.getAttribute("state") ?? "pending";
251
+ if (state === "current") this.setAttribute("aria-current", "step");
252
+ else this.removeAttribute("aria-current");
253
+ const badge = this.part("marker");
254
+ if (state === "done") badge.innerHTML = CHECK;
255
+ else badge.textContent = state === "error" ? "!" : (this.getAttribute("number") ?? "");
256
+ text(this.part("heading"), this.getAttribute("heading"));
257
+ this.part("summary").hidden = !filled(this.shadowRoot.querySelector('slot[name="summary"]'));
258
+ this.part("body").hidden = !(state === "current" || state === "error" || this.hasAttribute("open"));
259
+ }
260
+ }
261
+
262
+ /**
263
+ * `<omg-badge tone>`: a short label for what something is or how it stands.
264
+ *
265
+ * @attr {"neutral" | "info" | "good" | "warning" | "bad"} tone - Its colour; neutral by default.
266
+ * @slot - The label.
267
+ */
268
+ class OmgBadge extends OmgElement {
269
+ static styles = [
270
+ TONES,
271
+ css(`
272
+ :host {
273
+ --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
+ font-size: var(--text-xs); font-weight: var(--weight-medium); white-space: nowrap;
276
+ }
277
+ `),
278
+ ];
279
+ static template = `<slot></slot>`;
280
+ }
281
+
282
+ /**
283
+ * `<omg-progress value max label>`: how far something has got, or a moving bar
284
+ * while nobody knows yet.
285
+ *
286
+ * @attr {number} value - How far it has got; without one, the bar just moves.
287
+ * @attr {number} max - What finished means; 100 by default.
288
+ * @attr {string} label - What is in progress, above the bar and for screen readers.
289
+ * @part label - The label; hidden when empty.
290
+ * @part track - The whole bar.
291
+ * @part bar - The part that is done.
292
+ */
293
+ class OmgProgress extends OmgElement {
294
+ static observedAttributes = ["value", "max", "label"];
295
+ static styles = [
296
+ css(`
297
+ :host { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
298
+ .label { color: var(--color-text-secondary); font-size: var(--text-sm); }
299
+ .track { height: 4px; overflow: hidden; border-radius: 2px; background: var(--color-border-strong); }
300
+ .bar { height: 100%; border-radius: inherit; background: var(--color-ink); transition: width var(--duration-base) var(--ease-out); }
301
+ @keyframes omg-progress { from { translate: -100%; } to { translate: 250%; } }
302
+ .bar.moving { width: 40%; animation: omg-progress 1.4s ease-in-out infinite; }
303
+ `),
304
+ ];
305
+ static template = `<span class="label" part="label"></span><div class="track" part="track" role="progressbar"><div class="bar" part="bar"></div></div>`;
306
+
307
+ update() {
308
+ const track = this.part("track");
309
+ const bar = this.part("bar");
310
+ const label = this.getAttribute("label");
311
+ text(this.part("label"), label);
312
+ if (label) track.setAttribute("aria-label", label);
313
+ else track.removeAttribute("aria-label");
314
+ const given = this.getAttribute("value");
315
+ const value = given === null || given === "" ? NaN : Number(given);
316
+ // A maximum that is no finite positive number counts as the default.
317
+ const givenMax = Number(this.getAttribute("max"));
318
+ const max = Number.isFinite(givenMax) && givenMax > 0 ? givenMax : 100;
319
+ const known = Number.isFinite(value);
320
+ bar.classList.toggle("moving", !known);
321
+ if (known) {
322
+ const done = Math.min(Math.max(value, 0), max);
323
+ bar.style.width = `${(done / max) * 100}%`;
324
+ track.setAttribute("aria-valuenow", String(done));
325
+ track.setAttribute("aria-valuemax", String(max));
326
+ } else {
327
+ bar.style.removeProperty("width");
328
+ track.removeAttribute("aria-valuenow");
329
+ track.removeAttribute("aria-valuemax");
330
+ }
331
+ }
332
+ }
333
+
334
+ define("omg-status", OmgStatus);
335
+ define("omg-banner", OmgBanner);
336
+ define("omg-log", OmgLog);
337
+ define("omg-steps", OmgSteps);
338
+ define("omg-step", OmgStep);
339
+ define("omg-badge", OmgBadge);
340
+ define("omg-progress", OmgProgress);