@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/forms.js
ADDED
|
@@ -0,0 +1,464 @@
|
|
|
1
|
+
// Forms: what a person fills in. Each control is a native one in the app's
|
|
2
|
+
// look, and carries its own label, hint and error.
|
|
3
|
+
import { OmgElement, css, define, text } from "./base.js";
|
|
4
|
+
|
|
5
|
+
const FIELD = css(`
|
|
6
|
+
:host { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; max-width: 100%; }
|
|
7
|
+
.field { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; }
|
|
8
|
+
.label { color: var(--color-text-secondary); font-size: var(--text-sm); }
|
|
9
|
+
.hint, .error { margin: 0; font-size: var(--text-sm); overflow-wrap: anywhere; }
|
|
10
|
+
.hint { color: var(--color-text-secondary); }
|
|
11
|
+
.error { color: var(--color-danger); }
|
|
12
|
+
`);
|
|
13
|
+
|
|
14
|
+
/** The surface a person types or chooses on. */
|
|
15
|
+
const BOX = css(`
|
|
16
|
+
.box {
|
|
17
|
+
width: 100%; min-width: 0; padding: 0 var(--space-2); border: 0; border-radius: var(--radius-md);
|
|
18
|
+
background: var(--color-stage); color: var(--color-text); font: inherit;
|
|
19
|
+
box-shadow: inset 0 0 0 1px var(--color-border); transition: box-shadow var(--duration-fast);
|
|
20
|
+
}
|
|
21
|
+
.box:not(:disabled):hover { box-shadow: inset 0 0 0 1px var(--color-border-strong); }
|
|
22
|
+
.box[aria-invalid="true"], .box[aria-invalid="true"]:not(:disabled):hover { box-shadow: inset 0 0 0 1px var(--color-danger); }
|
|
23
|
+
.box:disabled { opacity: 0.4; cursor: default; }
|
|
24
|
+
.box::placeholder { color: var(--color-text-tertiary); }
|
|
25
|
+
`);
|
|
26
|
+
|
|
27
|
+
const AFTER_FIELD = `<p class="hint" id="hint" part="hint"></p><p class="error" id="error" part="error"></p>`;
|
|
28
|
+
|
|
29
|
+
/** A native control under its label, then a hint or an error. */
|
|
30
|
+
const field = (control) => `<label class="field" part="field"><span class="label" part="label"></span>${control}</label>${AFTER_FIELD}`;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* What every form control has.
|
|
34
|
+
*
|
|
35
|
+
* @attr {string} label - Shown with it.
|
|
36
|
+
* @attr {string} hint - A line under it; an error takes its place.
|
|
37
|
+
* @attr {string} error - What's wrong with its value.
|
|
38
|
+
* @attr {boolean} disabled - Can't be changed.
|
|
39
|
+
* @attr {string} aria-label - Its name, without a label.
|
|
40
|
+
* @fires change - The person changed it.
|
|
41
|
+
* @fires input - The person is changing it.
|
|
42
|
+
* @part field - The label around it.
|
|
43
|
+
* @part label - The label's text.
|
|
44
|
+
* @part hint - The hint; hidden when empty or when there is an error.
|
|
45
|
+
* @part error - The error; hidden when empty.
|
|
46
|
+
*/
|
|
47
|
+
class OmgField extends OmgElement {
|
|
48
|
+
static observedAttributes = ["label", "hint", "error", "disabled", "aria-label"];
|
|
49
|
+
static delegatesFocus = true;
|
|
50
|
+
/** The part that is the native control. */
|
|
51
|
+
static control = "";
|
|
52
|
+
|
|
53
|
+
constructor() {
|
|
54
|
+
super();
|
|
55
|
+
// `input` crosses the shadow boundary by itself; `change` doesn't. A box, a
|
|
56
|
+
// radio or an option is chosen by the time `input` fires, so the choice is
|
|
57
|
+
// recorded then: a page that changes the hint as it hears `input` mustn't
|
|
58
|
+
// put back the one before.
|
|
59
|
+
this.control.addEventListener("input", () => this.commit());
|
|
60
|
+
this.control.addEventListener("change", () => {
|
|
61
|
+
this.commit();
|
|
62
|
+
this.dispatchEvent(new Event("change", { bubbles: true }));
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
get control() {
|
|
67
|
+
return this.part(/** @type {typeof OmgField} */ (this.constructor).control);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Records what the person chose, before the page hears of it. */
|
|
71
|
+
commit() {}
|
|
72
|
+
|
|
73
|
+
update() {
|
|
74
|
+
const control = this.control;
|
|
75
|
+
text(this.part("label"), this.getAttribute("label"));
|
|
76
|
+
const name = this.getAttribute("aria-label");
|
|
77
|
+
if (name) control.setAttribute("aria-label", name);
|
|
78
|
+
else control.removeAttribute("aria-label");
|
|
79
|
+
control.disabled = this.hasAttribute("disabled");
|
|
80
|
+
const error = this.getAttribute("error");
|
|
81
|
+
const hint = error ? null : this.getAttribute("hint");
|
|
82
|
+
text(this.part("error"), error);
|
|
83
|
+
text(this.part("hint"), hint);
|
|
84
|
+
if (error) control.setAttribute("aria-invalid", "true");
|
|
85
|
+
else control.removeAttribute("aria-invalid");
|
|
86
|
+
if (error || hint) control.setAttribute("aria-describedby", error ? "error" : "hint");
|
|
87
|
+
else control.removeAttribute("aria-describedby");
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* What a field to type in has.
|
|
93
|
+
*
|
|
94
|
+
* @attr {string} value - Its text; setting it replaces what was typed.
|
|
95
|
+
* @attr {string} placeholder - Shown while it is empty.
|
|
96
|
+
* @attr {boolean} readonly - Can be read and copied, not changed.
|
|
97
|
+
* @prop {string} value - Its text.
|
|
98
|
+
*/
|
|
99
|
+
class OmgTextField extends OmgField {
|
|
100
|
+
static observedAttributes = [...OmgField.observedAttributes, "value", "placeholder", "readonly"];
|
|
101
|
+
static styles = [
|
|
102
|
+
FIELD,
|
|
103
|
+
BOX,
|
|
104
|
+
// The caret says where typing goes, however the field got focus.
|
|
105
|
+
css(`.box:focus-visible { outline: none; }`),
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
get value() {
|
|
109
|
+
return this.control.value;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
set value(value) {
|
|
113
|
+
this.control.value = String(value ?? "");
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
attributeChangedCallback(name, previous, value) {
|
|
117
|
+
// Only here: an error appearing mustn't put back the text from before typing.
|
|
118
|
+
if (name === "value") this.control.value = value ?? "";
|
|
119
|
+
super.attributeChangedCallback(name, previous, value);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
update() {
|
|
123
|
+
super.update();
|
|
124
|
+
const control = this.control;
|
|
125
|
+
control.placeholder = this.getAttribute("placeholder") ?? "";
|
|
126
|
+
control.readOnly = this.hasAttribute("readonly");
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* `<omg-input label value type placeholder hint error>`: one line of text.
|
|
132
|
+
*
|
|
133
|
+
* @attr {"text" | "search" | "password" | "number" | "email" | "url"} type - What it holds; text by default.
|
|
134
|
+
* @part input - The native input.
|
|
135
|
+
*/
|
|
136
|
+
class OmgInput extends OmgTextField {
|
|
137
|
+
static observedAttributes = [...OmgTextField.observedAttributes, "type"];
|
|
138
|
+
static control = "input";
|
|
139
|
+
static styles = [...OmgTextField.styles, css(`input { height: 32px; }`)];
|
|
140
|
+
static template = field(`<input class="box" part="input" />`);
|
|
141
|
+
|
|
142
|
+
update() {
|
|
143
|
+
super.update();
|
|
144
|
+
this.control.type = this.getAttribute("type") ?? "text";
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* `<omg-textarea label value rows placeholder hint error>`: several lines of text.
|
|
150
|
+
*
|
|
151
|
+
* @attr {number} rows - How many lines it shows; 3 by default.
|
|
152
|
+
* @part textarea - The native textarea.
|
|
153
|
+
*/
|
|
154
|
+
class OmgTextarea extends OmgTextField {
|
|
155
|
+
static observedAttributes = [...OmgTextField.observedAttributes, "rows"];
|
|
156
|
+
static control = "textarea";
|
|
157
|
+
static styles = [...OmgTextField.styles, css(`textarea { display: block; padding-block: var(--space-2); line-height: 1.5; resize: vertical; }`)];
|
|
158
|
+
static template = field(`<textarea class="box" part="textarea"></textarea>`);
|
|
159
|
+
|
|
160
|
+
update() {
|
|
161
|
+
super.update();
|
|
162
|
+
this.control.rows = Number(this.getAttribute("rows")) || 3;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* What a control that is on or off has.
|
|
168
|
+
*
|
|
169
|
+
* @attr {boolean} checked - It is on.
|
|
170
|
+
* @prop {boolean} checked - It is on.
|
|
171
|
+
*/
|
|
172
|
+
class OmgToggle extends OmgField {
|
|
173
|
+
static observedAttributes = [...OmgField.observedAttributes, "checked"];
|
|
174
|
+
static styles = [
|
|
175
|
+
FIELD,
|
|
176
|
+
css(`
|
|
177
|
+
.field { flex-direction: row; align-items: center; gap: var(--space-2); cursor: pointer; }
|
|
178
|
+
:host([disabled]) .field { cursor: default; }
|
|
179
|
+
.label { color: var(--color-text); font-size: inherit; }
|
|
180
|
+
input { flex: none; appearance: none; margin: 0; cursor: inherit; }
|
|
181
|
+
input:disabled { opacity: 0.4; }
|
|
182
|
+
`),
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
get checked() {
|
|
186
|
+
return this.control.checked;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
set checked(value) {
|
|
190
|
+
this.toggleAttribute("checked", Boolean(value));
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
commit() {
|
|
194
|
+
this.toggleAttribute("checked", this.control.checked);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
update() {
|
|
198
|
+
super.update();
|
|
199
|
+
this.control.checked = this.hasAttribute("checked");
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* `<omg-switch label checked hint>`: turns a setting on or off at once, with its
|
|
205
|
+
* label at the row's start and the switch at its end.
|
|
206
|
+
*
|
|
207
|
+
* @cssprop --omg-accent - The plugin's brand colour, for the switch when on.
|
|
208
|
+
* @cssprop --omg-on-accent - The colour of the thumb when on.
|
|
209
|
+
* @part switch - The native checkbox.
|
|
210
|
+
*/
|
|
211
|
+
class OmgSwitch extends OmgToggle {
|
|
212
|
+
static control = "switch";
|
|
213
|
+
static styles = [
|
|
214
|
+
...OmgToggle.styles,
|
|
215
|
+
css(`
|
|
216
|
+
.field { justify-content: space-between; }
|
|
217
|
+
input {
|
|
218
|
+
position: relative; width: 34px; height: 20px; border-radius: var(--radius-pill);
|
|
219
|
+
background: var(--color-text-tertiary); transition: background var(--duration-fast);
|
|
220
|
+
}
|
|
221
|
+
input::before {
|
|
222
|
+
content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%;
|
|
223
|
+
background: var(--color-control-thumb); box-shadow: 0 1px 2px rgb(0 0 0 / 30%);
|
|
224
|
+
transition: transform var(--duration-base) var(--ease-out);
|
|
225
|
+
}
|
|
226
|
+
input:checked { background: var(--omg-accent, var(--color-accent)); }
|
|
227
|
+
input:checked::before { background: var(--omg-on-accent, var(--color-on-accent)); transform: translateX(14px); }
|
|
228
|
+
`),
|
|
229
|
+
];
|
|
230
|
+
static template = field(`<input type="checkbox" role="switch" part="switch" />`);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* `<omg-checkbox label checked hint>`: a choice that takes effect with the rest
|
|
235
|
+
* of a form, with the box before its label.
|
|
236
|
+
*
|
|
237
|
+
* @cssprop --omg-accent - The plugin's brand colour, for the box when checked.
|
|
238
|
+
* @cssprop --omg-on-accent - The colour of the check.
|
|
239
|
+
* @part checkbox - The native checkbox.
|
|
240
|
+
*/
|
|
241
|
+
class OmgCheckbox extends OmgToggle {
|
|
242
|
+
static control = "checkbox";
|
|
243
|
+
static styles = [
|
|
244
|
+
...OmgToggle.styles,
|
|
245
|
+
css(`
|
|
246
|
+
.field { flex-direction: row-reverse; justify-content: flex-end; }
|
|
247
|
+
.hint, .error { padding-left: calc(16px + var(--space-2)); }
|
|
248
|
+
input {
|
|
249
|
+
display: grid; place-items: center; width: 16px; height: 16px; border-radius: 4px;
|
|
250
|
+
background: var(--color-stage); box-shadow: inset 0 0 0 1px var(--color-border-strong);
|
|
251
|
+
transition: background var(--duration-fast);
|
|
252
|
+
}
|
|
253
|
+
input[aria-invalid="true"] { box-shadow: inset 0 0 0 1px var(--color-danger); }
|
|
254
|
+
input:checked { background: var(--omg-accent, var(--color-accent)); box-shadow: none; }
|
|
255
|
+
input:checked::before {
|
|
256
|
+
content: ""; width: 4px; height: 8px; margin-top: -2px; rotate: 45deg;
|
|
257
|
+
border: solid var(--omg-on-accent, var(--color-on-accent)); border-width: 0 1.5px 1.5px 0;
|
|
258
|
+
}
|
|
259
|
+
`),
|
|
260
|
+
];
|
|
261
|
+
static template = field(`<input type="checkbox" part="checkbox" />`);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* What a choice among `<option>` children has. The `value` attribute chooses,
|
|
266
|
+
* and a choice sets it; without one, the option marked `selected` is chosen.
|
|
267
|
+
*
|
|
268
|
+
* @attr {string} value - The chosen option's value.
|
|
269
|
+
* @prop {string} value - The chosen option's value.
|
|
270
|
+
*/
|
|
271
|
+
class OmgChoice extends OmgField {
|
|
272
|
+
static observedAttributes = [...OmgField.observedAttributes, "value"];
|
|
273
|
+
|
|
274
|
+
// Options added, removed or changed; the host's own attributes aren't options.
|
|
275
|
+
#observer = new MutationObserver((records) => {
|
|
276
|
+
if (records.some((record) => record.type !== "attributes" || record.target !== this)) this.#read();
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
connectedCallback() {
|
|
280
|
+
this.#observer.observe(this, { childList: true, subtree: true, attributes: true, characterData: true });
|
|
281
|
+
this.#read();
|
|
282
|
+
super.connectedCallback();
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
disconnectedCallback() {
|
|
286
|
+
this.#observer.disconnect();
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/** The value to show: the attribute's, or the option marked `selected`'s. A parser may add options one at a time. */
|
|
290
|
+
#read() {
|
|
291
|
+
const sources = [...this.querySelectorAll("option")];
|
|
292
|
+
this.drawOptions(sources, this.getAttribute("value") ?? sources.find((source) => source.hasAttribute("selected"))?.value);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/** Copies the page's options into the control. */
|
|
296
|
+
drawOptions(_sources, _chosen) {}
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* `<omg-select label value hint error disabled>` with `<option>` children: the
|
|
301
|
+
* app's dropdown. Its list stays inside the panel and scrolls.
|
|
302
|
+
*
|
|
303
|
+
* @part select - The native select.
|
|
304
|
+
*/
|
|
305
|
+
class OmgSelect extends OmgChoice {
|
|
306
|
+
static control = "select";
|
|
307
|
+
static styles = [
|
|
308
|
+
FIELD,
|
|
309
|
+
BOX,
|
|
310
|
+
css(`
|
|
311
|
+
:host { display: inline-flex; min-width: 160px; }
|
|
312
|
+
/* Still a native select, so the browser keeps its keyboard, its form and its
|
|
313
|
+
screen reader behaviour; a browser without base-select draws its own list. */
|
|
314
|
+
select, ::picker(select) { appearance: base-select; }
|
|
315
|
+
select { height: 32px; display: flex; align-items: center; gap: var(--space-2); cursor: pointer; }
|
|
316
|
+
select::picker-icon {
|
|
317
|
+
content: ""; width: 6px; height: 6px; margin: -3px 2px 0 auto; rotate: 45deg;
|
|
318
|
+
border: solid var(--color-text-tertiary); border-width: 0 1.5px 1.5px 0; transition: none;
|
|
319
|
+
}
|
|
320
|
+
::picker(select) {
|
|
321
|
+
margin-block: var(--space-1); padding: var(--space-1); border: 0; border-radius: var(--radius-lg);
|
|
322
|
+
/* The browser opens it on the side with more room and limits it to that room. */
|
|
323
|
+
overflow: auto;
|
|
324
|
+
background: var(--color-surface-raised); color: var(--color-text); box-shadow: var(--shadow-float);
|
|
325
|
+
}
|
|
326
|
+
option {
|
|
327
|
+
display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
|
|
328
|
+
min-height: 28px; padding: var(--space-1) var(--space-2); border-radius: var(--radius-md); cursor: pointer;
|
|
329
|
+
}
|
|
330
|
+
option:not(:disabled):hover, option:focus-visible { outline: none; background: var(--color-hover); }
|
|
331
|
+
option:disabled { color: var(--color-text-tertiary); cursor: default; }
|
|
332
|
+
option::checkmark { order: 1; color: var(--color-text-secondary); }
|
|
333
|
+
`),
|
|
334
|
+
];
|
|
335
|
+
static template = field(`<select class="box" part="select"></select>`);
|
|
336
|
+
|
|
337
|
+
get value() {
|
|
338
|
+
return this.control.value;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
set value(value) {
|
|
342
|
+
this.setAttribute("value", String(value ?? ""));
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
commit() {
|
|
346
|
+
this.setAttribute("value", this.control.value);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
drawOptions(sources, chosen) {
|
|
350
|
+
const select = this.control;
|
|
351
|
+
select.replaceChildren(
|
|
352
|
+
...sources.map((source) => {
|
|
353
|
+
const option = document.createElement("option");
|
|
354
|
+
option.value = source.value;
|
|
355
|
+
option.textContent = source.textContent;
|
|
356
|
+
option.disabled = source.disabled;
|
|
357
|
+
return option;
|
|
358
|
+
}),
|
|
359
|
+
);
|
|
360
|
+
if (chosen !== undefined) select.value = chosen;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
update() {
|
|
364
|
+
super.update();
|
|
365
|
+
// As a native select: a value no option has leaves nothing chosen.
|
|
366
|
+
const value = this.getAttribute("value");
|
|
367
|
+
if (value !== null && this.control.value !== value) this.control.value = value;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* `<omg-segmented label value hint error disabled>` with `<option>` children: a
|
|
373
|
+
* few choices side by side, all in view. Arrow keys move between them.
|
|
374
|
+
*
|
|
375
|
+
* @part segments - The row of choices.
|
|
376
|
+
* @part segment - One choice.
|
|
377
|
+
*/
|
|
378
|
+
class OmgSegmented extends OmgChoice {
|
|
379
|
+
static control = "segments";
|
|
380
|
+
static styles = [
|
|
381
|
+
FIELD,
|
|
382
|
+
css(`
|
|
383
|
+
:host { align-items: flex-start; }
|
|
384
|
+
.segments { display: inline-flex; max-width: 100%; overflow-x: auto; scrollbar-width: none; gap: 2px; padding: 3px; border-radius: var(--radius-md); background: var(--color-hover); }
|
|
385
|
+
label.segment {
|
|
386
|
+
position: relative; display: inline-flex; align-items: center; height: 26px; padding: 0 10px; border-radius: var(--radius-sm);
|
|
387
|
+
color: var(--color-text-secondary); white-space: nowrap; cursor: pointer; transition: background var(--duration-fast), color var(--duration-fast);
|
|
388
|
+
}
|
|
389
|
+
label.segment:hover { color: var(--color-text); }
|
|
390
|
+
label.segment:has(:checked) { background: var(--color-surface-raised); box-shadow: var(--shadow-hairline); color: var(--color-text); font-weight: var(--weight-medium); }
|
|
391
|
+
label.segment:has(:focus-visible) { outline: 2px solid var(--color-focus); outline-offset: -2px; }
|
|
392
|
+
label.segment:has(:disabled) { opacity: 0.4; cursor: default; }
|
|
393
|
+
input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: inherit; }
|
|
394
|
+
`),
|
|
395
|
+
];
|
|
396
|
+
static template = `<div class="field" part="field"><span class="label" id="label" part="label"></span><div class="segments" part="segments" role="radiogroup"></div></div>${AFTER_FIELD}`;
|
|
397
|
+
|
|
398
|
+
#radios() {
|
|
399
|
+
return [.../** @type {NodeListOf<HTMLInputElement>} */ (this.control.querySelectorAll("input"))];
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
get value() {
|
|
403
|
+
return this.#radios().find((radio) => radio.checked)?.value ?? "";
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
set value(value) {
|
|
407
|
+
this.setAttribute("value", String(value ?? ""));
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
commit() {
|
|
411
|
+
this.setAttribute("value", this.value);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
drawOptions(sources, chosen) {
|
|
415
|
+
// The radios are drawn again, so one that had focus gives it to its successor.
|
|
416
|
+
const focused = /** @type {HTMLInputElement | null} */ (this.shadowRoot?.activeElement ?? null);
|
|
417
|
+
const had = focused?.type === "radio" ? focused.value : undefined;
|
|
418
|
+
this.control.replaceChildren(
|
|
419
|
+
...sources.map((source) => {
|
|
420
|
+
const label = document.createElement("label");
|
|
421
|
+
label.className = "segment";
|
|
422
|
+
label.setAttribute("part", "segment");
|
|
423
|
+
const radio = document.createElement("input");
|
|
424
|
+
radio.type = "radio";
|
|
425
|
+
radio.name = "segments";
|
|
426
|
+
radio.value = source.value;
|
|
427
|
+
radio.checked = source.value === chosen;
|
|
428
|
+
radio.dataset.disabled = source.disabled ? "true" : "";
|
|
429
|
+
const name = document.createElement("span");
|
|
430
|
+
name.textContent = source.textContent;
|
|
431
|
+
label.append(radio, name);
|
|
432
|
+
return label;
|
|
433
|
+
}),
|
|
434
|
+
);
|
|
435
|
+
this.#mark();
|
|
436
|
+
if (had !== undefined) {
|
|
437
|
+
const radios = this.#radios().filter((radio) => !radio.disabled);
|
|
438
|
+
(radios.find((radio) => radio.value === had) ?? radios.find((radio) => radio.checked) ?? radios[0])?.focus();
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
#mark() {
|
|
443
|
+
const value = this.getAttribute("value");
|
|
444
|
+
for (const radio of this.#radios()) {
|
|
445
|
+
radio.disabled = this.hasAttribute("disabled") || radio.dataset.disabled === "true";
|
|
446
|
+
if (value !== null) radio.checked = radio.value === value;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
update() {
|
|
451
|
+
super.update();
|
|
452
|
+
const control = this.control;
|
|
453
|
+
if (this.getAttribute("label")) control.setAttribute("aria-labelledby", "label");
|
|
454
|
+
else control.removeAttribute("aria-labelledby");
|
|
455
|
+
this.#mark();
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
define("omg-input", OmgInput);
|
|
460
|
+
define("omg-textarea", OmgTextarea);
|
|
461
|
+
define("omg-switch", OmgSwitch);
|
|
462
|
+
define("omg-checkbox", OmgCheckbox);
|
|
463
|
+
define("omg-select", OmgSelect);
|
|
464
|
+
define("omg-segmented", OmgSegmented);
|