@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/LICENSE +201 -0
- package/README.md +32 -2
- package/index.d.ts +170 -0
- package/index.js +127 -0
- package/package.json +28 -4
- package/plugin-ui.css +158 -0
- package/ui/1/base.js +107 -0
- package/ui/1/controls.js +231 -0
- package/ui/1/custom-elements.json +2472 -0
- package/ui/1/feedback.js +340 -0
- package/ui/1/forms.js +464 -0
- package/ui/1/index.d.ts +518 -0
- package/ui/1/index.js +9 -0
- package/ui/1/layout.js +419 -0
- package/ui/1/media.js +370 -0
- package/ui/1/navigation.js +161 -0
package/ui/1/media.js
ADDED
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
// Media: a picture, and a tile that shows one with its name.
|
|
2
|
+
import { connect } from "../../plugin-ui.js";
|
|
3
|
+
import { OmgElement, css, define, text } from "./base.js";
|
|
4
|
+
|
|
5
|
+
// Listens for the app's port from the start, for pictures of the project's files.
|
|
6
|
+
const connection = connect();
|
|
7
|
+
|
|
8
|
+
/** The `blob:` address each picture of a project file was given, released once it is drawn or failed. */
|
|
9
|
+
const addresses = new WeakMap();
|
|
10
|
+
|
|
11
|
+
function release(image) {
|
|
12
|
+
const address = addresses.get(image);
|
|
13
|
+
if (address === undefined) return;
|
|
14
|
+
addresses.delete(image);
|
|
15
|
+
URL.revokeObjectURL(address);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The `w/h` or number in a `ratio` attribute, as CSS `aspect-ratio`, when it is a positive one. */
|
|
19
|
+
function aspect(value) {
|
|
20
|
+
const match = /^\s*(\d+(?:\.\d+)?|\.\d+)\s*(?:\/\s*(\d+(?:\.\d+)?|\.\d+)\s*)?$/.exec(value ?? "");
|
|
21
|
+
if (!match) return undefined;
|
|
22
|
+
const width = Number(match[1]);
|
|
23
|
+
const height = match[2] === undefined ? 1 : Number(match[2]);
|
|
24
|
+
return width > 0 && height > 0 ? `${width} / ${height}` : undefined;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// A picture that couldn't load: a frame with a slash through it.
|
|
28
|
+
const BROKEN = `<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><rect x="3.5" y="4.5" width="17" height="15" rx="2.5"/><path d="m3.5 16 4.5-4.5 3 3"/><path d="m4 4 16 16"/></svg>`;
|
|
29
|
+
|
|
30
|
+
const IMAGE = css(`
|
|
31
|
+
:host { display: block; min-width: 0; max-width: 100%; }
|
|
32
|
+
.frame {
|
|
33
|
+
position: relative; display: grid; place-items: center; overflow: hidden;
|
|
34
|
+
border-radius: var(--omg-image-radius, var(--radius-lg)); background: var(--color-surface-raised);
|
|
35
|
+
}
|
|
36
|
+
/* Without a shape of its own, as tall as the element when something sizes it, as a list's thumbnail does. */
|
|
37
|
+
.frame:not(.sized) { height: 100%; }
|
|
38
|
+
:host([checker]) .frame {
|
|
39
|
+
background:
|
|
40
|
+
repeating-conic-gradient(var(--color-surface-raised) 0 25%, var(--color-surface) 0 50%) 0 0 / 16px 16px;
|
|
41
|
+
}
|
|
42
|
+
img { display: block; max-width: 100%; max-height: 100%; height: auto; transition: opacity var(--duration-fast); }
|
|
43
|
+
/* Out of the flow, so the frame's shape alone sets its height. */
|
|
44
|
+
.frame.sized img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
|
|
45
|
+
:host([fit="cover"]) .frame.sized img { object-fit: cover; }
|
|
46
|
+
:host([pixelated]) img { image-rendering: pixelated; }
|
|
47
|
+
/* Not yet drawn, or not drawable: kept in the page for its alt text, but not seen. */
|
|
48
|
+
.frame:not(.loaded) img { opacity: 0; }
|
|
49
|
+
.frame:not(.sized):not(.loaded) { min-height: 64px; }
|
|
50
|
+
.frame:not(.sized):not(.loaded) img { position: absolute; }
|
|
51
|
+
.broken { position: absolute; inset: 0; display: grid; place-items: center; color: var(--color-text-tertiary); }
|
|
52
|
+
`);
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* `<omg-image src file alt ratio fit pixelated checker>`: a picture, on a raised
|
|
56
|
+
* surface until it loads and with a mark when it can't. With `ratio` it fills
|
|
57
|
+
* a frame of that shape; without, it is drawn at its own size, at most as wide
|
|
58
|
+
* as its place.
|
|
59
|
+
*
|
|
60
|
+
* @attr {string} src - The picture's address: a file in the plugin's package, or a `data:` or `blob:` URL.
|
|
61
|
+
* @attr {string} file - A picture in the project, by its path there, in place of `src`; the plugin needs the `project.files` capability. Set it again to show the file as it is now.
|
|
62
|
+
* @attr {string} alt - What it shows, for people who can't see it; leave it empty for a decoration.
|
|
63
|
+
* @attr {string} ratio - The frame's shape, such as `16/9` or `1`; the picture's own by default.
|
|
64
|
+
* @attr {"contain" | "cover"} fit - How the picture fills a frame of another shape: whole, or cropped to fill it. Whole by default.
|
|
65
|
+
* @attr {boolean} pixelated - Scales pixel art without smoothing it.
|
|
66
|
+
* @attr {boolean} checker - Draws a checkerboard behind it, to show where it is transparent.
|
|
67
|
+
* @readonly {boolean} complete - It has loaded, or failed to.
|
|
68
|
+
* @readonly {number} naturalWidth - The picture's own width in pixels; 0 until it loads.
|
|
69
|
+
* @readonly {number} naturalHeight - The picture's own height in pixels; 0 until it loads.
|
|
70
|
+
* @fires load - The picture loaded.
|
|
71
|
+
* @fires error - The picture couldn't load.
|
|
72
|
+
* @part frame - The surface the picture is drawn on.
|
|
73
|
+
* @part image - The picture.
|
|
74
|
+
* @part broken - The mark shown when it couldn't load.
|
|
75
|
+
* @cssprop --omg-image-radius - The frame's corners; the app's large radius by default.
|
|
76
|
+
*/
|
|
77
|
+
class OmgImage extends OmgElement {
|
|
78
|
+
static observedAttributes = ["src", "file", "alt", "ratio"];
|
|
79
|
+
static styles = [IMAGE];
|
|
80
|
+
static template = `
|
|
81
|
+
<div class="frame" part="frame">
|
|
82
|
+
<img part="image" alt="" loading="lazy" draggable="false">
|
|
83
|
+
<span class="broken" part="broken" hidden>${BROKEN}</span>
|
|
84
|
+
</div>`;
|
|
85
|
+
|
|
86
|
+
#status = "empty";
|
|
87
|
+
/** The `src` or `file` attribute was set, to the same value too, which loads it again. */
|
|
88
|
+
#reload = true;
|
|
89
|
+
/**
|
|
90
|
+
* Its connectedCallback has run. Markup upgraded in a page reports each attribute
|
|
91
|
+
* first, already connected, and drawing then would read a file once per attribute.
|
|
92
|
+
*/
|
|
93
|
+
#inPage = false;
|
|
94
|
+
|
|
95
|
+
constructor() {
|
|
96
|
+
super();
|
|
97
|
+
this.#listen(this.#image());
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
attributeChangedCallback(name, previous, value) {
|
|
101
|
+
// While there's a file, `src` isn't the picture, so setting it changes nothing.
|
|
102
|
+
const source = name === "file" || (name === "src" && !this.getAttribute("file"));
|
|
103
|
+
if (source) this.#reload = true;
|
|
104
|
+
super.attributeChangedCallback(name, previous, value);
|
|
105
|
+
// Out of the page, the old picture goes at once, so its late load or error doesn't count; the new one loads once in the page.
|
|
106
|
+
if (source && !this.#inPage) this.#replace();
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
connectedCallback() {
|
|
110
|
+
this.#inPage = true;
|
|
111
|
+
super.connectedCallback();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
disconnectedCallback() {
|
|
115
|
+
this.#inPage = false;
|
|
116
|
+
// A file still loading might never finish out of the page, and would keep its address. It loads again if it comes back.
|
|
117
|
+
if (this.#status !== "loading" || !this.getAttribute("file")) return;
|
|
118
|
+
this.#reload = true;
|
|
119
|
+
this.#replace();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
get complete() {
|
|
123
|
+
return this.#status === "load" || this.#status === "error";
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
get naturalWidth() {
|
|
127
|
+
return this.#status === "load" ? this.#image().naturalWidth : 0;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get naturalHeight() {
|
|
131
|
+
return this.#status === "load" ? this.#image().naturalHeight : 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
#image() {
|
|
135
|
+
return /** @type {HTMLImageElement} */ (this.part("image"));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
update() {
|
|
139
|
+
if (!this.#inPage) return;
|
|
140
|
+
const image = this.#image();
|
|
141
|
+
const frame = this.part("frame");
|
|
142
|
+
const ratio = aspect(this.getAttribute("ratio"));
|
|
143
|
+
frame.classList.toggle("sized", Boolean(ratio));
|
|
144
|
+
frame.style.aspectRatio = ratio ?? "";
|
|
145
|
+
image.alt = this.getAttribute("alt") ?? "";
|
|
146
|
+
if (!this.#reload) return;
|
|
147
|
+
this.#reload = false;
|
|
148
|
+
const file = this.getAttribute("file") ?? "";
|
|
149
|
+
const src = this.getAttribute("src") ?? "";
|
|
150
|
+
const next = this.#replace();
|
|
151
|
+
if (!file && !src) return;
|
|
152
|
+
this.#show("loading");
|
|
153
|
+
if (!file) {
|
|
154
|
+
next.src = src;
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
void connection
|
|
158
|
+
// A picture replaced by now, as setting several attributes at once does, has nothing to read.
|
|
159
|
+
.then((host) => (next === this.#image() ? host.files.url(file) : undefined))
|
|
160
|
+
.then(
|
|
161
|
+
(address) => {
|
|
162
|
+
if (address === undefined) return;
|
|
163
|
+
addresses.set(next, address);
|
|
164
|
+
// Replaced while the file was read: its address isn't needed.
|
|
165
|
+
if (next !== this.#image()) release(next);
|
|
166
|
+
else next.src = address;
|
|
167
|
+
},
|
|
168
|
+
() => {
|
|
169
|
+
if (next === this.#image() && this.#status === "loading") this.#settle("error");
|
|
170
|
+
},
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Each address gets a picture of its own, so the old one's late load or error is told apart. */
|
|
175
|
+
#replace() {
|
|
176
|
+
const image = this.#image();
|
|
177
|
+
const next = /** @type {HTMLImageElement} */ (image.cloneNode(false));
|
|
178
|
+
next.removeAttribute("src");
|
|
179
|
+
this.#listen(next);
|
|
180
|
+
image.replaceWith(next);
|
|
181
|
+
release(image);
|
|
182
|
+
this.#show("empty");
|
|
183
|
+
return next;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
#listen(image) {
|
|
187
|
+
for (const type of ["load", "error"])
|
|
188
|
+
image.addEventListener(type, () => {
|
|
189
|
+
// Drawn or not, the picture no longer needs its file's address.
|
|
190
|
+
release(image);
|
|
191
|
+
if (image === this.#image() && this.#status === "loading") this.#settle(type);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
#settle(type) {
|
|
196
|
+
this.#show(type);
|
|
197
|
+
this.dispatchEvent(new Event(type));
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#show(status) {
|
|
201
|
+
this.#status = status;
|
|
202
|
+
this.part("frame").classList.toggle("loaded", status === "load");
|
|
203
|
+
this.part("broken").hidden = status !== "error";
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* `<omg-tile heading description badge src file ratio selected disabled>`: a
|
|
209
|
+
* picture with its name and a line under it, such as an asset in a grid. It
|
|
210
|
+
* is a button: a click on it is the person picking it, and `selected` marks
|
|
211
|
+
* the one picked.
|
|
212
|
+
*
|
|
213
|
+
* @attr {string} heading - Its name, on one line.
|
|
214
|
+
* @attr {string} description - A line under the name, such as its size or kind.
|
|
215
|
+
* @attr {string} badge - A few words over the picture's corner, such as a duration.
|
|
216
|
+
* @attr {string} src - The picture, as for `omg-image`.
|
|
217
|
+
* @attr {string} file - A picture in the project, as for `omg-image`.
|
|
218
|
+
* @attr {string} alt - What the picture shows, when the name doesn't say; empty by default.
|
|
219
|
+
* @attr {string} ratio - The picture's shape, as for `omg-image`; `4/3` by default.
|
|
220
|
+
* @attr {"contain" | "cover"} fit - As for `omg-image`; cropped to fill by default.
|
|
221
|
+
* @attr {boolean} pixelated - As for `omg-image`.
|
|
222
|
+
* @attr {boolean} checker - As for `omg-image`.
|
|
223
|
+
* @attr {boolean} selected - It is the one picked.
|
|
224
|
+
* @attr {boolean} disabled - It can't be picked.
|
|
225
|
+
* @prop {boolean} disabled - The `disabled` attribute.
|
|
226
|
+
* @prop {boolean} selected - The `selected` attribute.
|
|
227
|
+
* @slot media - Drawn in place of the picture, such as a video or a canvas.
|
|
228
|
+
* @part tile - The button.
|
|
229
|
+
* @part preview - The picture's frame.
|
|
230
|
+
* @part image - The `omg-image`, when there's no `media`.
|
|
231
|
+
* @part badge - The badge; hidden when empty.
|
|
232
|
+
* @part heading - The name.
|
|
233
|
+
* @part description - The line under the name; hidden when empty.
|
|
234
|
+
*/
|
|
235
|
+
class OmgTile extends OmgElement {
|
|
236
|
+
static observedAttributes = ["heading", "description", "badge", "src", "file", "alt", "ratio", "fit", "pixelated", "checker", "selected", "disabled"];
|
|
237
|
+
static delegatesFocus = true;
|
|
238
|
+
static styles = [
|
|
239
|
+
css(`
|
|
240
|
+
:host { display: block; min-width: 0; }
|
|
241
|
+
button {
|
|
242
|
+
display: flex; flex-direction: column; gap: var(--space-2); width: 100%; min-width: 0;
|
|
243
|
+
padding: 0; border: 0; border-radius: var(--radius-lg); background: none;
|
|
244
|
+
color: var(--color-text); font: inherit; text-align: left; cursor: pointer;
|
|
245
|
+
}
|
|
246
|
+
button:disabled { opacity: 0.4; cursor: default; }
|
|
247
|
+
.preview {
|
|
248
|
+
position: relative; display: block; overflow: hidden; border-radius: var(--radius-lg);
|
|
249
|
+
background: var(--color-surface-raised);
|
|
250
|
+
transition: box-shadow var(--duration-fast), filter var(--duration-fast);
|
|
251
|
+
}
|
|
252
|
+
button:not(:disabled):hover .preview { filter: brightness(1.06); }
|
|
253
|
+
:host([selected]) .preview { box-shadow: 0 0 0 2px var(--color-ink); }
|
|
254
|
+
/* The picture fills the preview, whose shape the tile sets. */
|
|
255
|
+
omg-image { position: absolute; inset: 0; --omg-image-radius: 0; }
|
|
256
|
+
omg-image::part(frame) { width: 100%; height: 100%; }
|
|
257
|
+
::slotted(*) { position: absolute; inset: 0; display: block; width: 100%; height: 100%; object-fit: cover; }
|
|
258
|
+
.badge {
|
|
259
|
+
position: absolute; top: var(--space-2); right: var(--space-2); max-width: calc(100% - 2 * var(--space-2));
|
|
260
|
+
overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
|
|
261
|
+
height: 18px; padding: 0 6px; border-radius: var(--radius-pill); line-height: 18px;
|
|
262
|
+
background: color-mix(in srgb, var(--color-app) 78%, transparent); color: var(--color-text);
|
|
263
|
+
font-family: var(--font-mono); font-size: var(--text-xs);
|
|
264
|
+
}
|
|
265
|
+
.text { display: flex; flex-direction: column; gap: 2px; min-width: 0; padding: 0 2px; }
|
|
266
|
+
.heading, .description { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
|
267
|
+
.heading { font-size: var(--text-md); font-weight: var(--weight-medium); }
|
|
268
|
+
.description { color: var(--color-text-tertiary); font-size: var(--text-sm); }
|
|
269
|
+
`),
|
|
270
|
+
];
|
|
271
|
+
static template = `
|
|
272
|
+
<button type="button" part="tile">
|
|
273
|
+
<span class="preview" part="preview">
|
|
274
|
+
<slot name="media"><omg-image part="image" fit="cover"></omg-image></slot>
|
|
275
|
+
<span class="badge" part="badge" hidden></span>
|
|
276
|
+
</span>
|
|
277
|
+
<span class="text">
|
|
278
|
+
<span class="heading" part="heading"></span>
|
|
279
|
+
<span class="description" part="description"></span>
|
|
280
|
+
</span>
|
|
281
|
+
</button>`;
|
|
282
|
+
|
|
283
|
+
/** The picture's attributes set since it was last drawn, to the same value too. */
|
|
284
|
+
#reload = new Set();
|
|
285
|
+
/** Its connectedCallback has run; see `omg-image`. */
|
|
286
|
+
#inPage = false;
|
|
287
|
+
|
|
288
|
+
constructor() {
|
|
289
|
+
super();
|
|
290
|
+
// A disabled tile reports no click, not even one a script sends it.
|
|
291
|
+
this.addEventListener(
|
|
292
|
+
"click",
|
|
293
|
+
(event) => {
|
|
294
|
+
if (this.disabled) {
|
|
295
|
+
event.preventDefault();
|
|
296
|
+
event.stopImmediatePropagation();
|
|
297
|
+
}
|
|
298
|
+
},
|
|
299
|
+
{ capture: true },
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
attributeChangedCallback(name, previous, value) {
|
|
304
|
+
if (name === "src" || name === "file") this.#reload.add(name);
|
|
305
|
+
super.attributeChangedCallback(name, previous, value);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
connectedCallback() {
|
|
309
|
+
this.#inPage = true;
|
|
310
|
+
super.connectedCallback();
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
disconnectedCallback() {
|
|
314
|
+
this.#inPage = false;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
get disabled() {
|
|
318
|
+
return this.hasAttribute("disabled");
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
set disabled(value) {
|
|
322
|
+
this.toggleAttribute("disabled", Boolean(value));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
get selected() {
|
|
326
|
+
return this.hasAttribute("selected");
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
set selected(value) {
|
|
330
|
+
this.toggleAttribute("selected", Boolean(value));
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
update() {
|
|
334
|
+
if (!this.#inPage) return;
|
|
335
|
+
const button = /** @type {HTMLButtonElement} */ (this.part("tile"));
|
|
336
|
+
button.disabled = this.disabled;
|
|
337
|
+
if (this.selected) button.setAttribute("aria-current", "true");
|
|
338
|
+
else button.removeAttribute("aria-current");
|
|
339
|
+
const heading = this.getAttribute("heading");
|
|
340
|
+
text(this.part("heading"), heading);
|
|
341
|
+
// The name may be cut short; its whole shows on hover.
|
|
342
|
+
if (heading) this.part("heading").title = heading;
|
|
343
|
+
else this.part("heading").removeAttribute("title");
|
|
344
|
+
text(this.part("description"), this.getAttribute("description"));
|
|
345
|
+
text(this.part("badge"), this.getAttribute("badge"));
|
|
346
|
+
this.part("preview").style.aspectRatio = aspect(this.getAttribute("ratio")) ?? "4 / 3";
|
|
347
|
+
const image = this.part("image");
|
|
348
|
+
// Only the source in use, so a tile with both reads its file once. It's set again
|
|
349
|
+
// only when it changed, or was set again, which loads it again.
|
|
350
|
+
const [source, unused] = this.getAttribute("file") ? ["file", "src"] : ["src", "file"];
|
|
351
|
+
image.removeAttribute(unused);
|
|
352
|
+
const value = this.getAttribute(source);
|
|
353
|
+
if (this.#reload.has(source) || image.getAttribute(source) !== value) {
|
|
354
|
+
if (value === null) image.removeAttribute(source);
|
|
355
|
+
else image.setAttribute(source, value);
|
|
356
|
+
}
|
|
357
|
+
this.#reload.clear();
|
|
358
|
+
for (const name of ["alt", "pixelated", "checker"]) {
|
|
359
|
+
const value = this.getAttribute(name);
|
|
360
|
+
if (value === null) image.removeAttribute(name);
|
|
361
|
+
else image.setAttribute(name, value);
|
|
362
|
+
}
|
|
363
|
+
image.setAttribute("fit", this.getAttribute("fit") === "contain" ? "contain" : "cover");
|
|
364
|
+
// A framed picture fills its frame, which fills the preview, whatever shape the tile gives it.
|
|
365
|
+
image.setAttribute("ratio", "1");
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
define("omg-image", OmgImage);
|
|
370
|
+
define("omg-tile", OmgTile);
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
// Navigation: moving between a panel's views.
|
|
2
|
+
import { OmgElement, css, define } from "./base.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* `<omg-tabs value aria-label>`: shows one of its `omg-tab` children, with a
|
|
6
|
+
* row of their headings above. Arrow keys, Home and End move between tabs.
|
|
7
|
+
* A page listens for `omg-tab-change`, since `change` would also come up from
|
|
8
|
+
* the controls inside a tab.
|
|
9
|
+
*
|
|
10
|
+
* @attr {string} value - The `name` of the open tab; the first that isn't disabled without one.
|
|
11
|
+
* @attr {string} aria-label - What the tabs choose between, for screen readers.
|
|
12
|
+
* @prop {string} value - The open tab's name.
|
|
13
|
+
* @fires {{ value: string }} omg-tab-change - The person opened a tab.
|
|
14
|
+
* @slot - The `omg-tab` children.
|
|
15
|
+
* @part list - The row of headings.
|
|
16
|
+
* @part tab - A heading's button.
|
|
17
|
+
*/
|
|
18
|
+
class OmgTabs extends OmgElement {
|
|
19
|
+
static observedAttributes = ["value", "aria-label"];
|
|
20
|
+
static styles = [
|
|
21
|
+
css(`
|
|
22
|
+
:host { display: block; min-width: 0; }
|
|
23
|
+
.list { display: flex; flex-wrap: wrap; gap: 4px; }
|
|
24
|
+
button {
|
|
25
|
+
height: 30px; padding: 0 var(--space-3); border: 0; border-radius: var(--radius-pill);
|
|
26
|
+
background: transparent; color: var(--color-text-secondary); font: inherit; font-size: var(--text-sm); white-space: nowrap; cursor: pointer;
|
|
27
|
+
transition: background var(--duration-fast), color var(--duration-fast);
|
|
28
|
+
}
|
|
29
|
+
button:not(:disabled):hover { background: color-mix(in srgb, var(--color-hover) 60%, transparent); color: var(--color-text); }
|
|
30
|
+
button:disabled { opacity: 0.4; cursor: default; }
|
|
31
|
+
button[aria-selected="true"], button[aria-selected="true"]:not(:disabled):hover { background: var(--color-hover); color: var(--color-text); font-weight: var(--weight-medium); }
|
|
32
|
+
.panels { padding-top: var(--space-4); }
|
|
33
|
+
`),
|
|
34
|
+
];
|
|
35
|
+
static template = `<div class="list" part="list" role="tablist"></div><div class="panels"><slot></slot></div>`;
|
|
36
|
+
|
|
37
|
+
// Tabs added or removed, or a heading changed; not what this component sets on them.
|
|
38
|
+
#observer = new MutationObserver((records) => {
|
|
39
|
+
if (records.some((record) => (record.type === "childList" ? record.target === this : record.target.parentElement === this))) this.update();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
constructor() {
|
|
43
|
+
super();
|
|
44
|
+
const list = this.part("list");
|
|
45
|
+
list.addEventListener("click", (event) => {
|
|
46
|
+
const button = /** @type {Element} */ (event.target).closest("button");
|
|
47
|
+
if (button) this.#open(button);
|
|
48
|
+
});
|
|
49
|
+
list.addEventListener("keydown", (event) => {
|
|
50
|
+
const enabled = [...list.querySelectorAll("button:not(:disabled)")];
|
|
51
|
+
const at = enabled.indexOf(/** @type {Element} */ (event.target));
|
|
52
|
+
if (at < 0) return;
|
|
53
|
+
const next = { ArrowRight: enabled[(at + 1) % enabled.length], ArrowLeft: enabled.at(at - 1), Home: enabled[0], End: enabled.at(-1) }[event.key];
|
|
54
|
+
if (!next) return;
|
|
55
|
+
event.preventDefault();
|
|
56
|
+
const name = /** @type {HTMLElement} */ (next).dataset.name;
|
|
57
|
+
this.#open(/** @type {HTMLElement} */ (next));
|
|
58
|
+
// Opening draws the row again.
|
|
59
|
+
this.#button(name)?.focus();
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get value() {
|
|
64
|
+
return this.#current()?.getAttribute("name") ?? "";
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
set value(value) {
|
|
68
|
+
this.setAttribute("value", String(value ?? ""));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
connectedCallback() {
|
|
72
|
+
this.#observer.observe(this, { childList: true, subtree: true, attributes: true, attributeFilter: ["name", "heading", "disabled"] });
|
|
73
|
+
super.connectedCallback();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
disconnectedCallback() {
|
|
77
|
+
this.#observer.disconnect();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
get #tabs() {
|
|
81
|
+
return [...this.children].filter((child) => child.localName === "omg-tab");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The tab `value` names, or the first that can open. */
|
|
85
|
+
#current() {
|
|
86
|
+
const tabs = this.#tabs;
|
|
87
|
+
const value = this.getAttribute("value");
|
|
88
|
+
return tabs.find((tab) => tab.getAttribute("name") === value && !tab.hasAttribute("disabled")) ?? tabs.find((tab) => !tab.hasAttribute("disabled"));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#button(name) {
|
|
92
|
+
return [...this.part("list").children].find((button) => /** @type {HTMLElement} */ (button).dataset.name === name);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
#open(button) {
|
|
96
|
+
if (button.getAttribute("aria-selected") === "true") return;
|
|
97
|
+
const value = button.dataset.name;
|
|
98
|
+
this.setAttribute("value", value);
|
|
99
|
+
this.dispatchEvent(new CustomEvent("omg-tab-change", { bubbles: true, detail: { value } }));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
update() {
|
|
103
|
+
const list = this.part("list");
|
|
104
|
+
const label = this.getAttribute("aria-label");
|
|
105
|
+
if (label) list.setAttribute("aria-label", label);
|
|
106
|
+
else list.removeAttribute("aria-label");
|
|
107
|
+
const current = this.#current();
|
|
108
|
+
const focused = /** @type {HTMLElement | null} */ (this.shadowRoot.activeElement)?.dataset.name;
|
|
109
|
+
list.replaceChildren(
|
|
110
|
+
...this.#tabs.map((tab) => {
|
|
111
|
+
const button = document.createElement("button");
|
|
112
|
+
const name = tab.getAttribute("name") ?? "";
|
|
113
|
+
button.type = "button";
|
|
114
|
+
button.dataset.name = name;
|
|
115
|
+
button.setAttribute("part", "tab");
|
|
116
|
+
button.setAttribute("role", "tab");
|
|
117
|
+
button.setAttribute("aria-selected", String(tab === current));
|
|
118
|
+
button.tabIndex = tab === current ? 0 : -1;
|
|
119
|
+
button.disabled = tab.hasAttribute("disabled");
|
|
120
|
+
button.textContent = tab.getAttribute("heading") ?? "";
|
|
121
|
+
return button;
|
|
122
|
+
}),
|
|
123
|
+
);
|
|
124
|
+
// A focused tab may have gone or been disabled; focus stays in the row.
|
|
125
|
+
if (focused !== undefined) {
|
|
126
|
+
const button = /** @type {HTMLButtonElement | undefined} */ (this.#button(focused));
|
|
127
|
+
(button && !button.disabled ? button : list.querySelector('[aria-selected="true"]'))?.focus();
|
|
128
|
+
}
|
|
129
|
+
for (const tab of this.#tabs) tab.hidden = tab !== current;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* `<omg-tab name heading disabled>`: one view of an `omg-tabs`.
|
|
135
|
+
*
|
|
136
|
+
* @attr {string} name - What `value` on the `omg-tabs` calls it; unique among its tabs.
|
|
137
|
+
* @attr {string} heading - The text of its heading, and its name for screen readers.
|
|
138
|
+
* @attr {boolean} disabled - It can't be opened.
|
|
139
|
+
* @slot - The view.
|
|
140
|
+
*/
|
|
141
|
+
class OmgTab extends OmgElement {
|
|
142
|
+
static observedAttributes = ["heading"];
|
|
143
|
+
static styles = [
|
|
144
|
+
css(`
|
|
145
|
+
:host { display: flex; flex-direction: column; gap: var(--space-6); min-width: 0; }
|
|
146
|
+
:host(:focus-visible) { outline: 2px solid var(--color-focus); outline-offset: 2px; }
|
|
147
|
+
`),
|
|
148
|
+
];
|
|
149
|
+
static template = `<slot></slot>`;
|
|
150
|
+
|
|
151
|
+
update() {
|
|
152
|
+
this.setAttribute("role", "tabpanel");
|
|
153
|
+
this.tabIndex = 0;
|
|
154
|
+
const heading = this.getAttribute("heading");
|
|
155
|
+
if (heading) this.setAttribute("aria-label", heading);
|
|
156
|
+
else this.removeAttribute("aria-label");
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
define("omg-tabs", OmgTabs);
|
|
161
|
+
define("omg-tab", OmgTab);
|