@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/README.md +7 -0
- package/index.d.ts +197 -2
- package/index.js +196 -0
- package/package.json +1 -1
- package/plugin-ui.css +5 -0
- package/ui/1/base.js +17 -0
- package/ui/1/controls.js +85 -2
- package/ui/1/custom-elements.json +2190 -746
- package/ui/1/data.js +238 -0
- package/ui/1/feedback.js +5 -4
- package/ui/1/floating.js +111 -0
- package/ui/1/forms.js +440 -39
- package/ui/1/index.d.ts +313 -1
- package/ui/1/index.js +2 -0
- package/ui/1/layout.js +36 -6
- package/ui/1/media.js +77 -0
- package/ui/1/overlays.js +465 -0
package/ui/1/forms.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Forms: what a person fills in. Each control is a native one in the app's
|
|
2
2
|
// look, and carries its own label, hint and error.
|
|
3
3
|
import { OmgElement, css, define, text } from "./base.js";
|
|
4
|
+
import { Floating, FLOATING } from "./floating.js";
|
|
4
5
|
|
|
5
6
|
const FIELD = css(`
|
|
6
7
|
:host { display: flex; flex-direction: column; gap: var(--space-1); min-width: 0; max-width: 100%; }
|
|
@@ -9,6 +10,10 @@ const FIELD = css(`
|
|
|
9
10
|
.hint, .error { margin: 0; font-size: var(--text-sm); overflow-wrap: anywhere; }
|
|
10
11
|
.hint { color: var(--color-text-secondary); }
|
|
11
12
|
.error { color: var(--color-danger); }
|
|
13
|
+
/* Editable inputs may match :focus-visible even after a pointer click.
|
|
14
|
+
Suppress only pointer focus, including a segmented control's label ring. */
|
|
15
|
+
.field.pointer-focus :focus-visible,
|
|
16
|
+
.field.pointer-focus label.choice:has(:focus-visible) { outline: none; }
|
|
12
17
|
`);
|
|
13
18
|
|
|
14
19
|
/** The surface a person types or chooses on. */
|
|
@@ -49,18 +54,30 @@ class OmgField extends OmgElement {
|
|
|
49
54
|
static delegatesFocus = true;
|
|
50
55
|
/** The part that is the native control. */
|
|
51
56
|
static control = "";
|
|
57
|
+
// Searchable choices emit value events only when an option is picked.
|
|
58
|
+
static nativeValueEvents = true;
|
|
52
59
|
|
|
53
60
|
constructor() {
|
|
54
61
|
super();
|
|
62
|
+
const field = this.part("field");
|
|
63
|
+
this.shadowRoot.addEventListener("pointerdown", () => field.classList.add("pointer-focus"), true);
|
|
64
|
+
this.shadowRoot.addEventListener("keydown", (event) => {
|
|
65
|
+
if (["Tab", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "Home", "End"].includes(event.key)) field.classList.remove("pointer-focus");
|
|
66
|
+
}, true);
|
|
67
|
+
this.shadowRoot.addEventListener("focusout", () => queueMicrotask(() => {
|
|
68
|
+
if (!this.shadowRoot.activeElement) field.classList.remove("pointer-focus");
|
|
69
|
+
}));
|
|
55
70
|
// `input` crosses the shadow boundary by itself; `change` doesn't. A box, a
|
|
56
71
|
// radio or an option is chosen by the time `input` fires, so the choice is
|
|
57
72
|
// recorded then: a page that changes the hint as it hears `input` mustn't
|
|
58
73
|
// put back the one before.
|
|
59
|
-
this.
|
|
60
|
-
|
|
61
|
-
this.
|
|
62
|
-
|
|
63
|
-
|
|
74
|
+
if (this.constructor.nativeValueEvents) {
|
|
75
|
+
this.control.addEventListener("input", () => this.commit());
|
|
76
|
+
this.control.addEventListener("change", () => {
|
|
77
|
+
this.commit();
|
|
78
|
+
this.dispatchEvent(new Event("change", { bubbles: true }));
|
|
79
|
+
});
|
|
80
|
+
}
|
|
64
81
|
}
|
|
65
82
|
|
|
66
83
|
get control() {
|
|
@@ -131,10 +148,14 @@ class OmgTextField extends OmgField {
|
|
|
131
148
|
* `<omg-input label value type placeholder hint error>`: one line of text.
|
|
132
149
|
*
|
|
133
150
|
* @attr {"text" | "search" | "password" | "number" | "email" | "url"} type - What it holds; text by default.
|
|
151
|
+
* @attr {number} min - Minimum for type=number; native validation does not clamp typed text.
|
|
152
|
+
* @attr {number} max - Maximum for type=number.
|
|
153
|
+
* @attr {number | "any"} step - Numeric increment; native default is 1.
|
|
154
|
+
* @method {boolean} checkValidity - Whether the native input satisfies its constraints.
|
|
134
155
|
* @part input - The native input.
|
|
135
156
|
*/
|
|
136
157
|
class OmgInput extends OmgTextField {
|
|
137
|
-
static observedAttributes = [...OmgTextField.observedAttributes, "type"];
|
|
158
|
+
static observedAttributes = [...OmgTextField.observedAttributes, "type", "min", "max", "step"];
|
|
138
159
|
static control = "input";
|
|
139
160
|
static styles = [...OmgTextField.styles, css(`input { height: 32px; }`)];
|
|
140
161
|
static template = field(`<input class="box" part="input" />`);
|
|
@@ -142,7 +163,14 @@ class OmgInput extends OmgTextField {
|
|
|
142
163
|
update() {
|
|
143
164
|
super.update();
|
|
144
165
|
this.control.type = this.getAttribute("type") ?? "text";
|
|
166
|
+
for (const name of ["min", "max", "step"]) {
|
|
167
|
+
const value = this.getAttribute(name);
|
|
168
|
+
if (value === null) this.control.removeAttribute(name);
|
|
169
|
+
else this.control.setAttribute(name, value);
|
|
170
|
+
}
|
|
145
171
|
}
|
|
172
|
+
|
|
173
|
+
checkValidity() { return this.control.checkValidity(); }
|
|
146
174
|
}
|
|
147
175
|
|
|
148
176
|
/**
|
|
@@ -154,7 +182,7 @@ class OmgInput extends OmgTextField {
|
|
|
154
182
|
class OmgTextarea extends OmgTextField {
|
|
155
183
|
static observedAttributes = [...OmgTextField.observedAttributes, "rows"];
|
|
156
184
|
static control = "textarea";
|
|
157
|
-
static styles = [...OmgTextField.styles, css(`textarea { display: block; padding-block: var(--space-2); line-height: 1.5; resize: vertical; }`)];
|
|
185
|
+
static styles = [...OmgTextField.styles, css(`textarea.box { display: block; padding-block: var(--space-2); line-height: 1.5; resize: vertical; }`)];
|
|
158
186
|
static template = field(`<textarea class="box" part="textarea"></textarea>`);
|
|
159
187
|
|
|
160
188
|
update() {
|
|
@@ -308,7 +336,7 @@ class OmgSelect extends OmgChoice {
|
|
|
308
336
|
FIELD,
|
|
309
337
|
BOX,
|
|
310
338
|
css(`
|
|
311
|
-
:host { display: inline-flex; min-width:
|
|
339
|
+
:host { display: inline-flex; min-width: 0; }
|
|
312
340
|
/* Still a native select, so the browser keeps its keyboard, its form and its
|
|
313
341
|
screen reader behaviour; a browser without base-select draws its own list. */
|
|
314
342
|
select, ::picker(select) { appearance: base-select; }
|
|
@@ -368,32 +396,13 @@ class OmgSelect extends OmgChoice {
|
|
|
368
396
|
}
|
|
369
397
|
}
|
|
370
398
|
|
|
371
|
-
/**
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
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}`;
|
|
399
|
+
/** Shared native radio selection, disabled state and focus retention. */
|
|
400
|
+
class OmgRadioChoice extends OmgChoice {
|
|
401
|
+
optionContent(source) {
|
|
402
|
+
const name = document.createElement("span");
|
|
403
|
+
name.textContent = source.textContent;
|
|
404
|
+
return name;
|
|
405
|
+
}
|
|
397
406
|
|
|
398
407
|
#radios() {
|
|
399
408
|
return [.../** @type {NodeListOf<HTMLInputElement>} */ (this.control.querySelectorAll("input"))];
|
|
@@ -418,17 +427,15 @@ class OmgSegmented extends OmgChoice {
|
|
|
418
427
|
this.control.replaceChildren(
|
|
419
428
|
...sources.map((source) => {
|
|
420
429
|
const label = document.createElement("label");
|
|
421
|
-
label.className = "
|
|
422
|
-
label.setAttribute("part",
|
|
430
|
+
label.className = "choice";
|
|
431
|
+
label.setAttribute("part", this.constructor.optionPart);
|
|
423
432
|
const radio = document.createElement("input");
|
|
424
433
|
radio.type = "radio";
|
|
425
434
|
radio.name = "segments";
|
|
426
435
|
radio.value = source.value;
|
|
427
436
|
radio.checked = source.value === chosen;
|
|
428
437
|
radio.dataset.disabled = source.disabled ? "true" : "";
|
|
429
|
-
|
|
430
|
-
name.textContent = source.textContent;
|
|
431
|
-
label.append(radio, name);
|
|
438
|
+
label.append(radio, this.optionContent(source));
|
|
432
439
|
return label;
|
|
433
440
|
}),
|
|
434
441
|
);
|
|
@@ -456,6 +463,400 @@ class OmgSegmented extends OmgChoice {
|
|
|
456
463
|
}
|
|
457
464
|
}
|
|
458
465
|
|
|
466
|
+
/**
|
|
467
|
+
* `<omg-segmented label value hint error disabled>` with `<option>` children: a
|
|
468
|
+
* few choices side by side, all in view. Arrow keys move between them.
|
|
469
|
+
*
|
|
470
|
+
* @part segments - The row of choices.
|
|
471
|
+
* @part segment - One choice.
|
|
472
|
+
*/
|
|
473
|
+
class OmgSegmented extends OmgRadioChoice {
|
|
474
|
+
static optionPart = "segment";
|
|
475
|
+
static control = "segments";
|
|
476
|
+
static styles = [
|
|
477
|
+
FIELD,
|
|
478
|
+
css(`
|
|
479
|
+
:host { align-items: flex-start; }
|
|
480
|
+
.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); }
|
|
481
|
+
label.choice {
|
|
482
|
+
position: relative; display: inline-flex; align-items: center; height: 26px; padding: 0 10px; border-radius: var(--radius-sm);
|
|
483
|
+
color: var(--color-text-secondary); white-space: nowrap; cursor: pointer; transition: background var(--duration-fast), color var(--duration-fast);
|
|
484
|
+
}
|
|
485
|
+
label.choice:hover { color: var(--color-text); }
|
|
486
|
+
label.choice:has(:checked) { background: var(--color-surface-raised); box-shadow: var(--shadow-hairline); color: var(--color-text); font-weight: var(--weight-medium); }
|
|
487
|
+
label.choice:has(:focus-visible) { outline: 2px solid var(--color-focus); outline-offset: -2px; }
|
|
488
|
+
label.choice:has(:disabled) { opacity: 0.4; cursor: default; }
|
|
489
|
+
input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: inherit; }
|
|
490
|
+
`),
|
|
491
|
+
];
|
|
492
|
+
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}`;
|
|
493
|
+
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* `<omg-radio-group label value>` with `<option>` children: visible choices,
|
|
498
|
+
* optionally explained by each option's `data-description`. Native arrows and
|
|
499
|
+
* Space select; separate groups keep independent selections.
|
|
500
|
+
*
|
|
501
|
+
* @part options - The radio group.
|
|
502
|
+
* @part option - One labelled choice.
|
|
503
|
+
*/
|
|
504
|
+
class OmgRadioGroup extends OmgRadioChoice {
|
|
505
|
+
static control = "options";
|
|
506
|
+
static optionPart = "option";
|
|
507
|
+
static styles = [FIELD, css(`
|
|
508
|
+
.options { display: flex; flex-direction: column; gap: var(--space-2); }
|
|
509
|
+
.choice { display: flex; align-items: flex-start; gap: var(--space-2); cursor: pointer; }
|
|
510
|
+
.choice:has(:disabled) { color: var(--color-text-tertiary); cursor: default; }
|
|
511
|
+
input { flex: none; appearance: none; width: 16px; height: 16px; margin: 1px 0 0; border-radius: 50%; background: var(--color-stage); box-shadow: inset 0 0 0 1px var(--color-border-strong); cursor: inherit; }
|
|
512
|
+
input:checked { box-shadow: inset 0 0 0 5px var(--omg-accent, var(--color-accent)); }
|
|
513
|
+
input:disabled { opacity: 0.4; }
|
|
514
|
+
.text { display: flex; flex-direction: column; gap: 2px; min-width: 0; overflow-wrap: anywhere; }
|
|
515
|
+
small { color: var(--color-text-tertiary); font-size: var(--text-sm); line-height: 1.45; }
|
|
516
|
+
`)];
|
|
517
|
+
static template = `<div class="field" part="field"><span class="label" id="label" part="label"></span><div class="options" part="options" role="radiogroup"></div></div>${AFTER_FIELD}`;
|
|
518
|
+
|
|
519
|
+
optionContent(source) {
|
|
520
|
+
const content = document.createElement("span");
|
|
521
|
+
content.className = "text";
|
|
522
|
+
content.append(super.optionContent(source));
|
|
523
|
+
if (source.dataset.description) {
|
|
524
|
+
const description = document.createElement("small");
|
|
525
|
+
description.textContent = source.dataset.description;
|
|
526
|
+
content.append(description);
|
|
527
|
+
}
|
|
528
|
+
return content;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* `<omg-slider label min max step value>`: a native range with a numeric readout.
|
|
534
|
+
* The browser bounds and steps the value. Input fires while adjusting; change
|
|
535
|
+
* fires when committed. Programmatic changes are silent.
|
|
536
|
+
*
|
|
537
|
+
* @attr {number} min - Range start; defaults to 0.
|
|
538
|
+
* @attr {number} max - Range end; defaults to 100.
|
|
539
|
+
* @attr {number | "any"} step - Increment; defaults to 1.
|
|
540
|
+
* @attr {number} value - Current value; defaults to the native range midpoint.
|
|
541
|
+
* @prop {number} value - Current bounded numeric value.
|
|
542
|
+
* @part input - Native range control.
|
|
543
|
+
* @part output - Current numeric value.
|
|
544
|
+
*/
|
|
545
|
+
class OmgSlider extends OmgField {
|
|
546
|
+
static observedAttributes = [...OmgField.observedAttributes, "min", "max", "step", "value"];
|
|
547
|
+
static control = "input";
|
|
548
|
+
static styles = [FIELD, css(`
|
|
549
|
+
.slider { display: flex; align-items: center; gap: var(--space-3); min-height: 28px; }
|
|
550
|
+
input { flex: 1; min-width: 0; height: 4px; margin: 0; border-radius: 2px; appearance: none; cursor: pointer; background: linear-gradient(to right, var(--omg-accent, var(--color-accent)) var(--fill), var(--color-border-strong) var(--fill)); }
|
|
551
|
+
input::-webkit-slider-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--omg-accent, var(--color-accent)); box-shadow: 0 0 0 3px var(--color-surface); appearance: none; }
|
|
552
|
+
input::-moz-range-thumb { width: 14px; height: 14px; border: 0; border-radius: 50%; background: var(--omg-accent, var(--color-accent)); box-shadow: 0 0 0 3px var(--color-surface); }
|
|
553
|
+
input:disabled { cursor: default; opacity: 0.4; }
|
|
554
|
+
output { min-width: 36px; color: var(--color-text-secondary); font-family: var(--font-mono); font-size: var(--text-sm); text-align: right; }
|
|
555
|
+
`)];
|
|
556
|
+
static template = field(`<span class="slider"><input type="range" id="range" part="input"><output for="range" part="output" aria-hidden="true"></output></span>`);
|
|
557
|
+
|
|
558
|
+
get value() { return Number(this.control.value); }
|
|
559
|
+
set value(value) { this.setAttribute("value", String(value)); }
|
|
560
|
+
commit() { this.setAttribute("value", this.control.value); }
|
|
561
|
+
|
|
562
|
+
update() {
|
|
563
|
+
super.update();
|
|
564
|
+
const input = this.control;
|
|
565
|
+
input.min = this.getAttribute("min") ?? "0";
|
|
566
|
+
input.max = this.getAttribute("max") ?? "100";
|
|
567
|
+
input.step = this.getAttribute("step") ?? "1";
|
|
568
|
+
input.value = this.getAttribute("value") ?? "";
|
|
569
|
+
const min = Number.isFinite(Number(input.min)) ? Number(input.min) : 0;
|
|
570
|
+
const max = input.max.trim() && Number.isFinite(Number(input.max)) ? Number(input.max) : 100;
|
|
571
|
+
const percent = max > min ? Math.max(0, Math.min(100, (this.value - min) / (max - min) * 100)) : 0;
|
|
572
|
+
input.style.setProperty("--fill", `${percent}%`);
|
|
573
|
+
this.part("output").textContent = input.value;
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
|
|
578
|
+
/**
|
|
579
|
+
* `<omg-combobox label value>` with `<option>` children: a searchable choice.
|
|
580
|
+
* Query text never changes the committed value until an enabled option is
|
|
581
|
+
* picked. Closing discards the query. Options can be replaced asynchronously;
|
|
582
|
+
* set `manual-filter` when the caller filters them remotely.
|
|
583
|
+
*
|
|
584
|
+
* @attr {string} placeholder - Text shown with no selection.
|
|
585
|
+
* @attr {boolean} loading - Shows loading-label; disables picking stale options.
|
|
586
|
+
* @attr {boolean} manual-filter - The caller filters options using omg-query.
|
|
587
|
+
* @attr {string} empty-label - Empty list message; defaults to No results.
|
|
588
|
+
* @attr {string} loading-label - Loading message; defaults to Loading….
|
|
589
|
+
* @readonly {string} query - Current uncommitted search text.
|
|
590
|
+
* @fires {{ query: string }} omg-query - Search text changed, or a manual-filter list opened; caller owns fetching and stale-result protection.
|
|
591
|
+
* @part input - Combobox input, with active-descendant keyboard navigation.
|
|
592
|
+
* @part panel - Floating list surface.
|
|
593
|
+
* @part options - Listbox.
|
|
594
|
+
* @part status - Loading or empty message.
|
|
595
|
+
*/
|
|
596
|
+
class OmgCombobox extends OmgChoice {
|
|
597
|
+
static nativeValueEvents = false;
|
|
598
|
+
static observedAttributes = [...OmgChoice.observedAttributes, "placeholder", "loading", "manual-filter", "empty-label", "loading-label"];
|
|
599
|
+
static control = "input";
|
|
600
|
+
static styles = [FIELD, BOX, FLOATING, css(`
|
|
601
|
+
.input-wrap { position: relative; }
|
|
602
|
+
input { height: 32px; padding-inline-end: 28px !important; }
|
|
603
|
+
.caret { position: absolute; right: 11px; top: 12px; width: 6px; height: 6px; rotate: 45deg; border: solid var(--color-text-tertiary); border-width: 0 1.5px 1.5px 0; pointer-events: none; }
|
|
604
|
+
.option { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); min-height: 30px; padding: 5px var(--space-2); border-radius: var(--radius-sm); cursor: pointer; overflow-wrap: anywhere; }
|
|
605
|
+
.option.active { background: var(--color-hover); }
|
|
606
|
+
.option[aria-disabled="true"] { color: var(--color-text-tertiary); cursor: default; }
|
|
607
|
+
.status { padding: var(--space-2); color: var(--color-text-tertiary); font-size: var(--text-sm); }
|
|
608
|
+
`)];
|
|
609
|
+
static template = field(`<span class="input-wrap"><input class="box" part="input" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-controls="options" autocomplete="off"><span class="caret" aria-hidden="true"></span></span>`) + `<div class="floating" part="panel" popover="manual" hidden><div id="options" part="options" role="listbox"></div><div class="status" part="status" role="status" hidden></div></div>`;
|
|
610
|
+
#sources = [];
|
|
611
|
+
#filtered = [];
|
|
612
|
+
#chosenLabel;
|
|
613
|
+
#query = "";
|
|
614
|
+
#open = false;
|
|
615
|
+
#active = -1;
|
|
616
|
+
|
|
617
|
+
constructor() {
|
|
618
|
+
super();
|
|
619
|
+
this.floating = new Floating(this, this.control, this.part("panel"), () => this.#close());
|
|
620
|
+
this.floating.matchWidth = true;
|
|
621
|
+
this.control.addEventListener("focus", () => this.#show());
|
|
622
|
+
this.control.addEventListener("click", () => this.#show());
|
|
623
|
+
this.control.addEventListener("input", (event) => {
|
|
624
|
+
// The field base does not commit a search; only a picked option commits.
|
|
625
|
+
event.stopPropagation();
|
|
626
|
+
this.#query = this.control.value;
|
|
627
|
+
this.#active = -1;
|
|
628
|
+
this.#show();
|
|
629
|
+
this.dispatchEvent(new CustomEvent("omg-query", { bubbles: true, composed: true, detail: { query: this.#query } }));
|
|
630
|
+
});
|
|
631
|
+
this.control.addEventListener("keydown", (event) => {
|
|
632
|
+
if (event.isComposing) return;
|
|
633
|
+
if (event.key === "Tab") { this.#close(); return; }
|
|
634
|
+
if (event.key === "Enter" && this.#open) { event.preventDefault(); this.#pick(this.#active); return; }
|
|
635
|
+
if (!["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) return;
|
|
636
|
+
if (!this.#open && ["Home", "End"].includes(event.key)) return;
|
|
637
|
+
event.preventDefault();
|
|
638
|
+
this.#show();
|
|
639
|
+
const enabled = this.#filtered.map((item, index) => item.disabled ? -1 : index).filter((index) => index !== -1);
|
|
640
|
+
const current = enabled.indexOf(this.#active);
|
|
641
|
+
this.#active = event.key === "Home" ? enabled[0] : event.key === "End" ? enabled.at(-1) : enabled[(current + (event.key === "ArrowDown" ? 1 : -1) + enabled.length) % enabled.length] ?? -1;
|
|
642
|
+
this.#mark();
|
|
643
|
+
this.part("options").children[this.#active]?.scrollIntoView({ block: "nearest" });
|
|
644
|
+
});
|
|
645
|
+
// Mouse selection keeps input focus, so active-descendant remains authoritative.
|
|
646
|
+
this.part("panel").addEventListener("mousedown", (event) => event.preventDefault());
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
get value() { return this.getAttribute("value") ?? this.#sources.find((item) => item.selected)?.value ?? ""; }
|
|
650
|
+
set value(value) { this.setAttribute("value", String(value ?? "")); }
|
|
651
|
+
get query() { return this.#query; }
|
|
652
|
+
|
|
653
|
+
drawOptions(sources) { this.#sources = sources; this.#draw(); this.#display(); }
|
|
654
|
+
#display() {
|
|
655
|
+
const selected = this.#sources.find((item) => item.value === this.value);
|
|
656
|
+
if (selected) this.#chosenLabel = { value: selected.value, label: selected.textContent };
|
|
657
|
+
if (!this.#open) this.control.value = this.#chosenLabel?.value === this.value ? this.#chosenLabel.label : this.value;
|
|
658
|
+
}
|
|
659
|
+
#show() {
|
|
660
|
+
if (this.hasAttribute("disabled")) return;
|
|
661
|
+
const opening = !this.#open;
|
|
662
|
+
if (opening) { this.#open = true; this.control.value = this.#query; }
|
|
663
|
+
this.control.setAttribute("aria-expanded", "true");
|
|
664
|
+
this.#draw();
|
|
665
|
+
this.floating.show();
|
|
666
|
+
if (opening && this.hasAttribute("manual-filter")) this.dispatchEvent(new CustomEvent("omg-query", { bubbles: true, composed: true, detail: { query: this.#query } }));
|
|
667
|
+
}
|
|
668
|
+
#close() {
|
|
669
|
+
this.#open = false;
|
|
670
|
+
this.#query = "";
|
|
671
|
+
this.#active = -1;
|
|
672
|
+
this.control.setAttribute("aria-expanded", "false");
|
|
673
|
+
this.control.removeAttribute("aria-activedescendant");
|
|
674
|
+
this.floating.hide();
|
|
675
|
+
this.#display();
|
|
676
|
+
}
|
|
677
|
+
#pick(index) {
|
|
678
|
+
const option = this.#filtered[index];
|
|
679
|
+
if (!option || option.disabled || this.hasAttribute("disabled") || this.hasAttribute("loading")) return;
|
|
680
|
+
this.value = option.value;
|
|
681
|
+
this.#close();
|
|
682
|
+
this.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
|
|
683
|
+
this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
|
|
684
|
+
}
|
|
685
|
+
#draw() {
|
|
686
|
+
const oldValue = this.#filtered[this.#active]?.value;
|
|
687
|
+
const needle = this.#query.trim().toLowerCase();
|
|
688
|
+
this.#filtered = this.hasAttribute("loading") ? [] : this.#sources.filter((item) => this.hasAttribute("manual-filter") || `${item.textContent} ${item.value} ${item.getAttribute("data-keywords") || ""}`.toLowerCase().includes(needle));
|
|
689
|
+
this.#active = this.#filtered.findIndex((item) => item.value === oldValue && !item.disabled);
|
|
690
|
+
this.part("options").replaceChildren(...this.#filtered.map((item, index) => {
|
|
691
|
+
const option = document.createElement("div");
|
|
692
|
+
option.id = `option-${index}`;
|
|
693
|
+
option.className = "option";
|
|
694
|
+
option.setAttribute("role", "option");
|
|
695
|
+
option.setAttribute("aria-selected", String(item.value === this.value));
|
|
696
|
+
option.setAttribute("aria-disabled", String(item.disabled));
|
|
697
|
+
const label = document.createElement("span");
|
|
698
|
+
label.textContent = item.textContent;
|
|
699
|
+
option.append(label);
|
|
700
|
+
if (item.value === this.value) { const check = document.createElement("span"); check.textContent = "✓"; check.setAttribute("aria-hidden", "true"); option.append(check); }
|
|
701
|
+
option.addEventListener("click", () => this.#pick(index));
|
|
702
|
+
option.addEventListener("pointermove", () => { if (!item.disabled) { this.#active = index; this.#mark(); } });
|
|
703
|
+
return option;
|
|
704
|
+
}));
|
|
705
|
+
this.part("options").setAttribute("aria-label", this.getAttribute("label") || this.getAttribute("aria-label") || "");
|
|
706
|
+
this.part("options").setAttribute("aria-busy", String(this.hasAttribute("loading")));
|
|
707
|
+
text(this.part("status"), this.hasAttribute("loading") ? this.getAttribute("loading-label") || "Loading…" : !this.#filtered.length ? this.getAttribute("empty-label") || "No results" : "");
|
|
708
|
+
this.#mark();
|
|
709
|
+
if (this.#open) this.floating.position();
|
|
710
|
+
}
|
|
711
|
+
#mark() {
|
|
712
|
+
[...this.part("options").children].forEach((option, index) => option.classList.toggle("active", index === this.#active));
|
|
713
|
+
if (this.#open && this.#active >= 0) this.control.setAttribute("aria-activedescendant", `option-${this.#active}`);
|
|
714
|
+
else this.control.removeAttribute("aria-activedescendant");
|
|
715
|
+
}
|
|
716
|
+
disconnectedCallback() { super.disconnectedCallback(); this.#close(); }
|
|
717
|
+
update() {
|
|
718
|
+
super.update();
|
|
719
|
+
this.control.placeholder = this.getAttribute("placeholder") || "";
|
|
720
|
+
if (this.hasAttribute("disabled")) this.#close();
|
|
721
|
+
this.#draw();
|
|
722
|
+
this.#display();
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/**
|
|
727
|
+
* `<omg-file-picker label accept multiple>`: choose local files or drop them.
|
|
728
|
+
* Selection stays local; callers own validation, persistence and uploads.
|
|
729
|
+
* Accept checks filenames/MIME hints, never file contents. Invalid drops retain
|
|
730
|
+
* the prior selection. Cancelling the native chooser also retains it.
|
|
731
|
+
*
|
|
732
|
+
* @attr {string} accept - Comma-separated extensions or MIME types, including image/*.
|
|
733
|
+
* @attr {boolean} multiple - Allows more than one file.
|
|
734
|
+
* @attr {string} choose-label - Picker action; defaults to Choose files.
|
|
735
|
+
* @attr {string} drop-label - Drop hint; defaults to or drop files here.
|
|
736
|
+
* @attr {string} clear-label - Clear action; defaults to Clear.
|
|
737
|
+
* @attr {string} rejected-label - Rejection feedback; defaults to Check the file type and number of files.
|
|
738
|
+
* @readonly {File[]} files - A copy of the currently selected local files.
|
|
739
|
+
* @method {void} clear - Clears selection silently; user clearing emits change.
|
|
740
|
+
* @fires {{ files: File[], reason: "accept" | "multiple" }} omg-reject - Rejected selection; previous files remain available.
|
|
741
|
+
* @part input - Native file input.
|
|
742
|
+
* @part dropzone - Clickable drop surface.
|
|
743
|
+
* @part files - Selected filenames.
|
|
744
|
+
* @part clear - Clear button.
|
|
745
|
+
*/
|
|
746
|
+
class OmgFilePicker extends OmgField {
|
|
747
|
+
static observedAttributes = [...OmgField.observedAttributes, "accept", "multiple", "choose-label", "drop-label", "clear-label", "rejected-label"];
|
|
748
|
+
static control = "input";
|
|
749
|
+
static nativeValueEvents = false;
|
|
750
|
+
static styles = [FIELD, css(`
|
|
751
|
+
.dropzone { position: relative; display: flex; flex-direction: column; align-items: center; gap: var(--space-1); padding: var(--space-4); border: 1px dashed var(--color-border-strong); border-radius: var(--radius-lg); cursor: pointer; text-align: center; }
|
|
752
|
+
.dropzone:hover, .dropzone.dragging { background: var(--color-hover); }
|
|
753
|
+
.dropzone:has(:focus-visible) { outline: 2px solid var(--color-focus); outline-offset: 2px; }
|
|
754
|
+
.field.pointer-focus .dropzone:has(:focus-visible) { outline: none; }
|
|
755
|
+
input { position: absolute; width: 1px; height: 1px; opacity: 0; }
|
|
756
|
+
.choose { font-weight: var(--weight-medium); }
|
|
757
|
+
.drop { color: var(--color-text-tertiary); font-size: var(--text-sm); }
|
|
758
|
+
:host([disabled]) .dropzone { opacity: 0.4; cursor: default; background: transparent; }
|
|
759
|
+
.selection { display: flex; align-items: flex-start; gap: var(--space-2); }
|
|
760
|
+
ul { flex: 1; min-width: 0; margin: 0; padding: 0; list-style: none; font-size: var(--text-sm); overflow-wrap: anywhere; }
|
|
761
|
+
button { border: 0; border-radius: var(--radius-md); padding: 3px var(--space-2); background: transparent; color: var(--color-text-secondary); font: inherit; font-size: var(--text-sm); cursor: pointer; }
|
|
762
|
+
button:hover { background: var(--color-hover); color: var(--color-text); }
|
|
763
|
+
button:disabled { opacity: 0.4; cursor: default; }
|
|
764
|
+
`)];
|
|
765
|
+
static template = `<div class="field" part="field"><span class="label" id="label" part="label"></span><label class="dropzone" part="dropzone"><input type="file" part="input"><span class="choose"></span><span class="drop"></span></label><div class="selection" hidden><ul part="files"></ul><button type="button" part="clear"></button></div></div>${AFTER_FIELD}`;
|
|
766
|
+
#files = [];
|
|
767
|
+
#rejected = false;
|
|
768
|
+
#dragDepth = 0;
|
|
769
|
+
|
|
770
|
+
constructor() {
|
|
771
|
+
super();
|
|
772
|
+
this.control.addEventListener("change", (event) => {
|
|
773
|
+
event.stopPropagation();
|
|
774
|
+
this.#choose([...this.control.files]);
|
|
775
|
+
this.control.value = ""; // Picking the same file again still reports a change.
|
|
776
|
+
});
|
|
777
|
+
this.control.addEventListener("input", (event) => event.stopPropagation());
|
|
778
|
+
this.part("clear").addEventListener("click", () => {
|
|
779
|
+
if (this.hasAttribute("disabled")) return;
|
|
780
|
+
this.clear();
|
|
781
|
+
this.control.focus();
|
|
782
|
+
this.#changed();
|
|
783
|
+
});
|
|
784
|
+
const zone = this.part("dropzone");
|
|
785
|
+
zone.addEventListener("dragenter", (event) => {
|
|
786
|
+
if (!event.dataTransfer?.types.includes("Files") || this.hasAttribute("disabled")) return;
|
|
787
|
+
event.preventDefault();
|
|
788
|
+
this.#dragDepth++;
|
|
789
|
+
zone.classList.add("dragging");
|
|
790
|
+
});
|
|
791
|
+
zone.addEventListener("dragover", (event) => {
|
|
792
|
+
if (!event.dataTransfer?.types.includes("Files")) return;
|
|
793
|
+
event.preventDefault();
|
|
794
|
+
event.dataTransfer.dropEffect = this.hasAttribute("disabled") ? "none" : "copy";
|
|
795
|
+
});
|
|
796
|
+
zone.addEventListener("dragleave", () => {
|
|
797
|
+
this.#dragDepth = Math.max(0, this.#dragDepth - 1);
|
|
798
|
+
if (!this.#dragDepth) zone.classList.remove("dragging");
|
|
799
|
+
});
|
|
800
|
+
zone.addEventListener("drop", (event) => {
|
|
801
|
+
event.preventDefault();
|
|
802
|
+
this.#dragDepth = 0;
|
|
803
|
+
zone.classList.remove("dragging");
|
|
804
|
+
if (event.dataTransfer) this.#choose([...event.dataTransfer.files]);
|
|
805
|
+
});
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
get files() { return [...this.#files]; }
|
|
809
|
+
clear() { this.#files = []; this.#rejected = false; this.control.value = ""; this.update(); }
|
|
810
|
+
#changed() {
|
|
811
|
+
this.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
|
|
812
|
+
this.dispatchEvent(new Event("change", { bubbles: true, composed: true }));
|
|
813
|
+
}
|
|
814
|
+
#choose(files) {
|
|
815
|
+
if (!files.length || this.hasAttribute("disabled")) return;
|
|
816
|
+
const types = (this.getAttribute("accept") || "").toLowerCase().split(",").map((type) => type.trim()).filter(Boolean);
|
|
817
|
+
const accepts = (file) => !types.length || types.some((type) => type.startsWith(".") ? file.name.toLowerCase().endsWith(type) : type.endsWith("/*") ? file.type.toLowerCase().startsWith(type.slice(0, -1)) : file.type.toLowerCase() === type);
|
|
818
|
+
const reason = !this.hasAttribute("multiple") && files.length > 1 ? "multiple" : files.some((file) => !accepts(file)) ? "accept" : null;
|
|
819
|
+
this.#rejected = Boolean(reason);
|
|
820
|
+
if (reason) {
|
|
821
|
+
this.update();
|
|
822
|
+
this.dispatchEvent(new CustomEvent("omg-reject", { bubbles: true, composed: true, detail: { files, reason } }));
|
|
823
|
+
return;
|
|
824
|
+
}
|
|
825
|
+
this.#files = files;
|
|
826
|
+
this.update();
|
|
827
|
+
this.#changed();
|
|
828
|
+
}
|
|
829
|
+
disconnectedCallback() { this.#dragDepth = 0; this.part("dropzone").classList.remove("dragging"); }
|
|
830
|
+
update() {
|
|
831
|
+
super.update();
|
|
832
|
+
this.control.accept = this.getAttribute("accept") || "";
|
|
833
|
+
this.control.multiple = this.hasAttribute("multiple");
|
|
834
|
+
if (this.getAttribute("label")) this.control.setAttribute("aria-labelledby", "label");
|
|
835
|
+
else this.control.removeAttribute("aria-labelledby");
|
|
836
|
+
text(this.shadowRoot.querySelector(".choose"), this.getAttribute("choose-label") || "Choose files");
|
|
837
|
+
text(this.shadowRoot.querySelector(".drop"), this.getAttribute("drop-label") || "or drop files here");
|
|
838
|
+
text(this.part("clear"), this.getAttribute("clear-label") || "Clear");
|
|
839
|
+
this.part("clear").disabled = this.hasAttribute("disabled");
|
|
840
|
+
this.shadowRoot.querySelector(".selection").hidden = !this.#files.length;
|
|
841
|
+
this.part("files").replaceChildren(...this.#files.map((file) => {
|
|
842
|
+
const name = document.createElement("li");
|
|
843
|
+
name.textContent = file.name;
|
|
844
|
+
return name;
|
|
845
|
+
}));
|
|
846
|
+
if (this.#rejected && !this.getAttribute("error")) {
|
|
847
|
+
text(this.part("hint"), "");
|
|
848
|
+
text(this.part("error"), this.getAttribute("rejected-label") || "Check the file type and number of files.");
|
|
849
|
+
this.control.setAttribute("aria-invalid", "true");
|
|
850
|
+
this.control.setAttribute("aria-describedby", "error");
|
|
851
|
+
}
|
|
852
|
+
if (this.hasAttribute("disabled")) { this.#dragDepth = 0; this.part("dropzone").classList.remove("dragging"); }
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
define("omg-file-picker", OmgFilePicker);
|
|
857
|
+
define("omg-slider", OmgSlider);
|
|
858
|
+
define("omg-radio-group", OmgRadioGroup);
|
|
859
|
+
define("omg-combobox", OmgCombobox);
|
|
459
860
|
define("omg-input", OmgInput);
|
|
460
861
|
define("omg-textarea", OmgTextarea);
|
|
461
862
|
define("omg-switch", OmgSwitch);
|