@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.
package/ui/1/layout.js ADDED
@@ -0,0 +1,419 @@
1
+ // Layout: a page, its sections, and how their contents are arranged.
2
+ import { OmgElement, css, define, filled, text } from "./base.js";
3
+
4
+ const HEADING = css(`
5
+ .header { display: flex; align-items: flex-start; gap: var(--space-3); }
6
+ .titles { flex: 1; min-width: 0; display: grid; gap: var(--space-1); }
7
+ .heading { margin: 0; font-weight: var(--weight-semibold); line-height: 1.3; overflow-wrap: anywhere; }
8
+ .description { margin: 0; color: var(--color-text-secondary); }
9
+ .actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
10
+ `);
11
+
12
+ /** Hides a header that has no titles and nothing in its slots. */
13
+ function updateHeader(element) {
14
+ const heading = element.getAttribute("heading");
15
+ const description = element.getAttribute("description");
16
+ text(element.part("heading"), heading);
17
+ text(element.part("description"), description);
18
+ const slots = [...element.shadowRoot.querySelectorAll(".header slot")];
19
+ element.part("header").hidden = !heading && !description && !slots.some(filled);
20
+ }
21
+
22
+ /**
23
+ * `<omg-page heading description wide>`: the page's column, centred and at
24
+ * most 720px wide, or the panel's whole width when `wide`. Slots: `brand` (a
25
+ * logo before the heading), `actions`, and the page's sections.
26
+ *
27
+ * @attr {string} heading - The page's title.
28
+ * @attr {string} description - A line under the title.
29
+ * @attr {boolean} wide - Fills the panel's width, for a page with a sidebar or a grid.
30
+ * @slot brand - A logo before the heading.
31
+ * @slot actions - Buttons beside the heading.
32
+ * @slot - The page's sections.
33
+ * @part header - The row of the brand, titles and actions; hidden when empty.
34
+ * @part heading - The title.
35
+ * @part description - The line under the title.
36
+ * @part body - The column of sections.
37
+ */
38
+ class OmgPage extends OmgElement {
39
+ static observedAttributes = ["heading", "description"];
40
+ static styles = [
41
+ HEADING,
42
+ css(`
43
+ :host { display: block; max-width: 720px; margin: 0 auto; }
44
+ :host([wide]) { max-width: none; }
45
+ .header { margin-bottom: var(--space-6); }
46
+ .heading { font-size: var(--text-lg); }
47
+ .body { display: flex; flex-direction: column; gap: var(--space-6); }
48
+ `),
49
+ ];
50
+ static template = `
51
+ <header class="header" part="header">
52
+ <slot name="brand"></slot>
53
+ <div class="titles"><h1 class="heading" part="heading"></h1><p class="description" part="description"></p></div>
54
+ <div class="actions"><slot name="actions"></slot></div>
55
+ </header>
56
+ <div class="body" part="body"><slot></slot></div>`;
57
+
58
+ constructor() {
59
+ super();
60
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
61
+ }
62
+
63
+ update() {
64
+ updateHeader(this);
65
+ }
66
+ }
67
+
68
+ /**
69
+ * `<omg-section heading description>`: a titled group of content. Slots:
70
+ * `actions` beside the heading, and the section's content.
71
+ *
72
+ * @attr {string} heading - The section's title, which names it.
73
+ * @attr {string} description - A line under the title.
74
+ * @slot actions - Buttons beside the heading.
75
+ * @slot - The section's content.
76
+ * @part section - The section.
77
+ * @part header - The titles and actions; hidden when empty.
78
+ * @part heading - The title.
79
+ * @part description - The line under the title.
80
+ * @part body - The column of content.
81
+ */
82
+ class OmgSection extends OmgElement {
83
+ static observedAttributes = ["heading", "description"];
84
+ static styles = [
85
+ HEADING,
86
+ css(`
87
+ :host { display: block; }
88
+ section { display: flex; flex-direction: column; gap: var(--space-3); }
89
+ .heading { font-size: var(--text-md); }
90
+ .body { display: flex; flex-direction: column; gap: var(--space-2); }
91
+ `),
92
+ ];
93
+ static template = `
94
+ <section part="section">
95
+ <header class="header" part="header">
96
+ <div class="titles"><h2 class="heading" part="heading"></h2><p class="description" part="description"></p></div>
97
+ <div class="actions"><slot name="actions"></slot></div>
98
+ </header>
99
+ <div class="body" part="body"><slot></slot></div>
100
+ </section>`;
101
+
102
+ constructor() {
103
+ super();
104
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
105
+ }
106
+
107
+ update() {
108
+ updateHeader(this);
109
+ const heading = this.part("heading");
110
+ const section = this.part("section");
111
+ if (heading.hidden) section.removeAttribute("aria-labelledby");
112
+ else {
113
+ heading.id = "heading";
114
+ section.setAttribute("aria-labelledby", "heading");
115
+ }
116
+ }
117
+ }
118
+
119
+ const SPACES = ["1", "2", "3", "4", "6"];
120
+
121
+ /**
122
+ * `<omg-stack direction="row|column" gap="1|2|3|4|6" align justify wrap>`:
123
+ * lays its children out in a row or a column, spaced by the app's steps.
124
+ *
125
+ * @attr {"column" | "row"} direction - Column by default.
126
+ * @attr {"0" | "1" | "2" | "3" | "4" | "6"} gap - A space token; 2 by default.
127
+ * @attr {"start" | "center" | "end" | "stretch" | "baseline"} align - How children line up across it.
128
+ * @attr {"start" | "center" | "end" | "between"} justify - How children are spread along it.
129
+ * @attr {boolean} wrap - Lets a row wrap.
130
+ * @slot - The children.
131
+ */
132
+ class OmgStack extends OmgElement {
133
+ static styles = [
134
+ css(`
135
+ :host { display: flex; flex-direction: column; gap: var(--space-2); min-width: 0; }
136
+ :host([direction="row"]) { flex-direction: row; align-items: center; }
137
+ :host([wrap]) { flex-wrap: wrap; }
138
+ ${SPACES.map((step) => `:host([gap="${step}"]) { gap: var(--space-${step}); }`).join("\n")}
139
+ :host([gap="0"]) { gap: 0; }
140
+ ${["start", "center", "end", "stretch", "baseline"].map((value) => `:host([align="${value}"]) { align-items: ${value === "start" || value === "end" ? `flex-${value}` : value}; }`).join("\n")}
141
+ ${["start", "center", "end"].map((value) => `:host([justify="${value}"]) { justify-content: ${value === "center" ? value : `flex-${value}`}; }`).join("\n")}
142
+ :host([justify="between"]) { justify-content: space-between; }
143
+ `),
144
+ ];
145
+ static template = `<slot></slot>`;
146
+ }
147
+
148
+ /**
149
+ * `<omg-empty heading description>`: what a page shows when it has nothing
150
+ * yet. Slots: `icon`, and actions as its content.
151
+ *
152
+ * @attr {string} heading - What is missing.
153
+ * @attr {string} description - What to do about it.
154
+ * @slot icon - An icon above the heading.
155
+ * @slot - Actions.
156
+ * @part icon - The icon's place; hidden when empty.
157
+ * @part heading - The heading.
158
+ * @part description - The description.
159
+ * @part actions - The actions' row; hidden when empty.
160
+ */
161
+ class OmgEmpty extends OmgElement {
162
+ static observedAttributes = ["heading", "description"];
163
+ static styles = [
164
+ css(`
165
+ :host { display: flex; flex-direction: column; align-items: center; gap: var(--space-2); padding: var(--space-6) var(--space-4); text-align: center; color: var(--color-text-secondary); }
166
+ .icon { color: var(--color-text-tertiary); }
167
+ .heading { margin: 0; font-size: var(--text-md); font-weight: var(--weight-medium); color: var(--color-text); }
168
+ .description { margin: 0; max-width: 48ch; }
169
+ .actions { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--space-2); margin-top: var(--space-2); }
170
+ `),
171
+ ];
172
+ static template = `
173
+ <div class="icon" part="icon"><slot name="icon"></slot></div>
174
+ <p class="heading" part="heading"></p>
175
+ <p class="description" part="description"></p>
176
+ <div class="actions" part="actions"><slot></slot></div>`;
177
+
178
+ constructor() {
179
+ super();
180
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
181
+ }
182
+
183
+ update() {
184
+ text(this.part("heading"), this.getAttribute("heading"));
185
+ text(this.part("description"), this.getAttribute("description"));
186
+ const [icon, actions] = this.shadowRoot.querySelectorAll("slot");
187
+ this.part("icon").hidden = !filled(icon);
188
+ this.part("actions").hidden = !filled(actions);
189
+ }
190
+ }
191
+
192
+ /** The number in an attribute, when it is a positive one. */
193
+ function positive(element, name) {
194
+ const value = Number(element.getAttribute(name));
195
+ return Number.isFinite(value) && value > 0 ? value : undefined;
196
+ }
197
+
198
+ /**
199
+ * `<omg-grid min columns gap>`: lays its children out in equal columns, as many
200
+ * as fit at `min` pixels wide each, or exactly `columns`.
201
+ *
202
+ * @attr {number} min - The narrowest a column gets, in pixels; 240 by default.
203
+ * @attr {number} columns - A fixed number of columns, in place of as many as fit.
204
+ * @attr {"0" | "1" | "2" | "3" | "4" | "6"} gap - A space token; 3 by default.
205
+ * @slot - The children.
206
+ * @part grid - The grid.
207
+ */
208
+ class OmgGrid extends OmgElement {
209
+ static observedAttributes = ["min", "columns"];
210
+ static styles = [
211
+ css(`
212
+ :host { display: block; min-width: 0; }
213
+ .grid { display: grid; gap: var(--space-3); grid-template-columns: var(--_columns); }
214
+ :host([gap="0"]) .grid { gap: 0; }
215
+ ${SPACES.map((step) => `:host([gap="${step}"]) .grid { gap: var(--space-${step}); }`).join("\n")}
216
+ `),
217
+ ];
218
+ static template = `<div class="grid" part="grid"><slot></slot></div>`;
219
+
220
+ update() {
221
+ const columns = positive(this, "columns");
222
+ this.part("grid").style.setProperty(
223
+ "--_columns",
224
+ columns ? `repeat(${Math.floor(columns) || 1}, minmax(0, 1fr))` : `repeat(auto-fill, minmax(min(${positive(this, "min") ?? 240}px, 100%), 1fr))`,
225
+ );
226
+ }
227
+ }
228
+
229
+ /**
230
+ * `<omg-split side side-width>`: a sidebar beside the main content, which go
231
+ * one above the other when there isn't room for both.
232
+ *
233
+ * @attr {"start" | "end"} side - Which side the sidebar is on; the start by default.
234
+ * @attr {number} side-width - The sidebar's width in pixels; 240 by default.
235
+ * @slot sidebar - The sidebar.
236
+ * @slot - The main content.
237
+ * @part split - The two places together.
238
+ * @part sidebar - The sidebar's place.
239
+ * @part main - The main content's place.
240
+ */
241
+ class OmgSplit extends OmgElement {
242
+ static observedAttributes = ["side-width"];
243
+ static styles = [
244
+ css(`
245
+ :host { display: block; min-width: 0; }
246
+ /* They wrap once the content would have less than half the width, whatever the sidebar's. */
247
+ .split { display: flex; flex-wrap: wrap; gap: var(--space-4); }
248
+ .sidebar { flex: 1 1 var(--_side); min-width: 0; max-width: 100%; }
249
+ .main { flex: 999 1 0; min-width: 50%; }
250
+ :host([side="end"]) .sidebar { order: 1; }
251
+ `),
252
+ ];
253
+ static template = `
254
+ <div class="split" part="split">
255
+ <div class="sidebar" part="sidebar"><slot name="sidebar"></slot></div>
256
+ <div class="main" part="main"><slot></slot></div>
257
+ </div>`;
258
+
259
+ update() {
260
+ this.part("split").style.setProperty("--_side", `${positive(this, "side-width") ?? 240}px`);
261
+ }
262
+ }
263
+
264
+ // A slot hears when nodes come and go, but not when a text node's words change.
265
+ class OmgTextWatcher extends OmgElement {
266
+ #observer = new MutationObserver(() => this.update());
267
+
268
+ connectedCallback() {
269
+ this.#observer.observe(this, { characterData: true, subtree: true });
270
+ super.connectedCallback();
271
+ }
272
+
273
+ disconnectedCallback() {
274
+ this.#observer.disconnect();
275
+ }
276
+ }
277
+
278
+ /**
279
+ * `<omg-card heading description>`: a raised surface that groups its content.
280
+ *
281
+ * @attr {string} heading - The card's title.
282
+ * @attr {string} description - A line under the title.
283
+ * @slot actions - Buttons beside the heading.
284
+ * @slot - The content.
285
+ * @part header - The row of the titles and actions; hidden when empty.
286
+ * @part heading - The title.
287
+ * @part description - The line under the title.
288
+ * @part body - The content.
289
+ */
290
+ class OmgCard extends OmgTextWatcher {
291
+ static observedAttributes = ["heading", "description"];
292
+ static styles = [
293
+ HEADING,
294
+ css(`
295
+ :host { display: flex; flex-direction: column; gap: var(--space-3); min-width: 0; padding: var(--space-4); border-radius: var(--radius-lg); background: var(--color-surface-raised); }
296
+ .heading { font-size: var(--text-md); }
297
+ .body { display: flex; flex-direction: column; gap: var(--space-2); }
298
+ `),
299
+ ];
300
+ static template = `
301
+ <header class="header" part="header">
302
+ <div class="titles"><h3 class="heading" part="heading"></h3><p class="description" part="description"></p></div>
303
+ <div class="actions"><slot name="actions"></slot></div>
304
+ </header>
305
+ <div class="body" part="body"><slot></slot></div>`;
306
+
307
+ constructor() {
308
+ super();
309
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
310
+ }
311
+
312
+ update() {
313
+ updateHeader(this);
314
+ this.part("body").hidden = !filled(this.shadowRoot.querySelector("slot:not([name])"));
315
+ }
316
+ }
317
+
318
+ /**
319
+ * `<omg-list>`: rows of `omg-list-item`, with a hairline between them.
320
+ *
321
+ * @slot - The `omg-list-item` children.
322
+ */
323
+ class OmgList extends OmgElement {
324
+ static styles = [css(`:host { display: flex; flex-direction: column; min-width: 0; }`)];
325
+ static template = `<slot></slot>`;
326
+
327
+ update() {
328
+ this.setAttribute("role", "list");
329
+ }
330
+ }
331
+
332
+ /**
333
+ * `<omg-list-item heading description>`: a row: an icon, a title and a line
334
+ * under it, then a value and buttons at its end.
335
+ *
336
+ * @attr {string} heading - The row's title.
337
+ * @attr {string} description - A line under the title.
338
+ * @slot leading - An icon or picture before the title; an `omg-image` is drawn as a 40px thumbnail.
339
+ * @slot - The row's value, at its end.
340
+ * @slot actions - Buttons at its end, after the value.
341
+ * @part leading - The leading place; hidden when empty.
342
+ * @part heading - The title.
343
+ * @part description - The line under the title.
344
+ * @part value - The value's place; hidden when empty.
345
+ * @part actions - The buttons' place; hidden when empty.
346
+ */
347
+ class OmgListItem extends OmgTextWatcher {
348
+ static observedAttributes = ["heading", "description"];
349
+ static styles = [
350
+ css(`
351
+ :host { display: flex; align-items: center; flex-wrap: wrap; gap: var(--space-2) var(--space-3); min-width: 0; padding: var(--space-3) 0; }
352
+ :host(:not(:last-child)) { box-shadow: inset 0 -1px 0 var(--color-border); }
353
+ .leading { flex: none; display: flex; }
354
+ /* An omg-image before the title is a thumbnail, whatever its picture's size; what doesn't fit is cut off. */
355
+ ::slotted(omg-image[slot="leading"]) {
356
+ width: 40px; height: 40px; flex: none; overflow: hidden; border-radius: var(--radius-md); --omg-image-radius: var(--radius-md);
357
+ }
358
+ .titles { flex: 1 1 160px; min-width: 0; display: grid; gap: 2px; overflow-wrap: anywhere; }
359
+ .heading { margin: 0; font-weight: var(--weight-medium); }
360
+ .description { margin: 0; color: var(--color-text-secondary); font-size: var(--text-sm); }
361
+ .value { min-width: 0; color: var(--color-text-secondary); overflow-wrap: anywhere; }
362
+ .actions { display: flex; flex-wrap: wrap; align-items: center; gap: var(--space-2); }
363
+ `),
364
+ ];
365
+ static template = `
366
+ <div class="leading" part="leading"><slot name="leading"></slot></div>
367
+ <div class="titles"><p class="heading" part="heading"></p><p class="description" part="description"></p></div>
368
+ <div class="value" part="value"><slot></slot></div>
369
+ <div class="actions" part="actions"><slot name="actions"></slot></div>`;
370
+
371
+ constructor() {
372
+ super();
373
+ this.shadowRoot.addEventListener("slotchange", () => this.update());
374
+ }
375
+
376
+ update() {
377
+ this.setAttribute("role", "listitem");
378
+ text(this.part("heading"), this.getAttribute("heading"));
379
+ text(this.part("description"), this.getAttribute("description"));
380
+ this.part("leading").hidden = !filled(this.shadowRoot.querySelector('slot[name="leading"]'));
381
+ this.part("value").hidden = !filled(this.shadowRoot.querySelector("slot:not([name])"));
382
+ this.part("actions").hidden = !filled(this.shadowRoot.querySelector('slot[name="actions"]'));
383
+ }
384
+ }
385
+
386
+ /**
387
+ * `<omg-divider label>`: a hairline between groups, with an optional label on it.
388
+ *
389
+ * @attr {string} label - Text on the line.
390
+ * @part label - The label; hidden when empty.
391
+ */
392
+ class OmgDivider extends OmgElement {
393
+ static observedAttributes = ["label"];
394
+ static styles = [
395
+ css(`
396
+ :host { display: flex; align-items: center; gap: var(--space-3); color: var(--color-text-tertiary); font-size: var(--text-sm); }
397
+ :host::before, :host::after { content: ""; flex: 1; height: 1px; background: var(--color-border); }
398
+ .label:not([hidden]) { white-space: nowrap; }
399
+ :host(:not([label]))::after { display: none; }
400
+ `),
401
+ ];
402
+ static template = `<span class="label" part="label"></span>`;
403
+
404
+ update() {
405
+ this.setAttribute("role", "separator");
406
+ text(this.part("label"), this.getAttribute("label"));
407
+ }
408
+ }
409
+
410
+ define("omg-page", OmgPage);
411
+ define("omg-section", OmgSection);
412
+ define("omg-stack", OmgStack);
413
+ define("omg-empty", OmgEmpty);
414
+ define("omg-grid", OmgGrid);
415
+ define("omg-split", OmgSplit);
416
+ define("omg-card", OmgCard);
417
+ define("omg-list", OmgList);
418
+ define("omg-list-item", OmgListItem);
419
+ define("omg-divider", OmgDivider);