@raylang/elements 0.0.0-stage → 0.4.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.
Files changed (95) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +4 -0
  3. package/README.md +12 -2
  4. package/base.css +111 -0
  5. package/components/accordion/accordion.js +197 -0
  6. package/components/alert/alert.js +112 -0
  7. package/components/app-bar/app-bar.js +168 -0
  8. package/components/app-shell/app-shell.js +198 -0
  9. package/components/aspect-ratio/aspect-ratio.js +59 -0
  10. package/components/attachment/attachment.js +333 -0
  11. package/components/avatar/avatar.js +96 -0
  12. package/components/badge/badge.js +63 -0
  13. package/components/blockquote/blockquote.js +128 -0
  14. package/components/bottom-nav/bottom-nav.js +309 -0
  15. package/components/breadcrumbs/breadcrumbs.js +175 -0
  16. package/components/button/button.js +283 -0
  17. package/components/button-group/button-group.js +72 -0
  18. package/components/card/card.js +73 -0
  19. package/components/carousel/carousel.js +435 -0
  20. package/components/center/center.js +26 -0
  21. package/components/checkbox/checkbox.js +152 -0
  22. package/components/checkbox-group/checkbox-group.js +152 -0
  23. package/components/code/code.js +574 -0
  24. package/components/color-picker/color-picker.js +775 -0
  25. package/components/combobox/combobox.js +563 -0
  26. package/components/command-palette/command-palette.js +510 -0
  27. package/components/container/container.js +41 -0
  28. package/components/date-picker/date-picker.js +594 -0
  29. package/components/dialog/dialog.js +329 -0
  30. package/components/drawer/drawer.js +139 -0
  31. package/components/empty-state/empty-state.js +66 -0
  32. package/components/file-upload/file-upload.js +589 -0
  33. package/components/grid/grid.js +96 -0
  34. package/components/group/group.js +40 -0
  35. package/components/icon/icon.js +58 -0
  36. package/components/icon-button/icon-button.js +30 -0
  37. package/components/indicator/indicator.js +112 -0
  38. package/components/input/input.js +85 -0
  39. package/components/kbd/kbd.js +60 -0
  40. package/components/menu/menu.js +296 -0
  41. package/components/nav-link/nav-link.js +159 -0
  42. package/components/notification/notification.js +591 -0
  43. package/components/number-input/number-input.js +241 -0
  44. package/components/pagination/pagination.js +194 -0
  45. package/components/password-input/password-input.js +85 -0
  46. package/components/path-input/path-input.js +137 -0
  47. package/components/popover/popover.js +85 -0
  48. package/components/progress/progress.js +109 -0
  49. package/components/radio/radio.js +253 -0
  50. package/components/scroll-area/scroll-area.js +158 -0
  51. package/components/search/search.js +157 -0
  52. package/components/segmented-control/segmented-control.js +190 -0
  53. package/components/select/select.js +506 -0
  54. package/components/separator/separator.js +58 -0
  55. package/components/sidebar/sidebar.js +252 -0
  56. package/components/skeleton/skeleton.js +90 -0
  57. package/components/slider/slider.js +155 -0
  58. package/components/spinner/spinner.js +39 -0
  59. package/components/split/split.js +221 -0
  60. package/components/stack/stack.js +46 -0
  61. package/components/stat/stat.js +79 -0
  62. package/components/statusbar/statusbar.js +110 -0
  63. package/components/stepper/stepper.js +239 -0
  64. package/components/switch/switch.js +45 -0
  65. package/components/table/table.js +328 -0
  66. package/components/tabs/tabs.js +224 -0
  67. package/components/textarea/textarea.js +105 -0
  68. package/components/theme-toggle/theme-toggle.js +78 -0
  69. package/components/timeline/timeline.js +150 -0
  70. package/components/toggle/toggle.js +130 -0
  71. package/components/toggle-group/toggle-group.js +216 -0
  72. package/components/tooltip/tooltip.js +101 -0
  73. package/core/charts.js +107 -0
  74. package/core/color.js +110 -0
  75. package/core/core.js +556 -0
  76. package/core/dismiss.js +69 -0
  77. package/core/floating.js +195 -0
  78. package/core/focus.js +70 -0
  79. package/core/form.js +235 -0
  80. package/core/host.js +200 -0
  81. package/core/hotkey.js +40 -0
  82. package/core/icons.js +104 -0
  83. package/core/motion.js +73 -0
  84. package/core/position.js +91 -0
  85. package/core/raylang.js +167 -0
  86. package/core/scroll.js +58 -0
  87. package/core/styles.js +211 -0
  88. package/core.js +16 -0
  89. package/custom-elements.json +18545 -0
  90. package/index.js +74 -0
  91. package/package.json +312 -4
  92. package/ray-elements.d.ts +2570 -0
  93. package/tokens.css +796 -0
  94. package/tokens.json +2083 -0
  95. package/vscode.html-custom-data.json +5499 -0
@@ -0,0 +1,563 @@
1
+ // <ray-combobox>: a select you can type in (Mantine's Select searchable,
2
+ // MultiSelect and TagsInput in one). Typing filters the options; Enter or
3
+ // a click picks one. `multiple` keeps several as chips (Backspace removes
4
+ // the last); `creatable` adds what was typed when nothing matches;
5
+ // `clearable` adds a button that clears it; `max-values` caps a multiple.
6
+ //
7
+ // Options: child <option>/<optgroup> elements or `data` (strings or
8
+ // { value, label, disabled, group }). The value is a string, or an array
9
+ // with `multiple` (the attribute: "a,b"); the form gets one entry per value.
10
+ //
11
+ // The WAI-ARIA combobox pattern with a listbox popup: focus stays in the
12
+ // text field, the active option is aria-activedescendant.
13
+
14
+ import { css, define, uid } from "../../core/core.js";
15
+ import { sizeStyles, radiusStyles } from "../../core/styles.js";
16
+ import { place, autoUpdate } from "../../core/position.js";
17
+ import { dismissable } from "../../core/dismiss.js";
18
+ import { FieldControl, fieldStyles, boxStyles, fieldTemplate } from "../../core/form.js";
19
+
20
+ const styles = css`
21
+ .box { flex-wrap: wrap; gap: 0.25rem; padding-inline-start: calc(var(--_px) * 0.66 - 0.25rem); cursor: text; }
22
+ :host([multiple]) .box[data-has-chips] { padding-block: 0.25rem; }
23
+ .control { padding-inline: 0.25rem; min-width: 4rem; }
24
+ :host([multiple]) .box[data-has-chips] .control { min-height: calc(var(--_h) - 0.5rem - 2px); }
25
+ .chips { display: contents; }
26
+ .chip {
27
+ display: inline-flex;
28
+ align-items: center;
29
+ gap: 0.125rem;
30
+ max-width: 100%;
31
+ height: calc(var(--_h) - 0.75rem);
32
+ min-height: 1.375rem;
33
+ padding: 0 0.125rem 0 0.5rem;
34
+ font-size: calc(var(--_fs) - 0.0625rem);
35
+ color: var(--ray-foreground);
36
+ background: var(--ray-muted);
37
+ border: 1px solid var(--ray-border);
38
+ border-radius: var(--ray-radius-sm);
39
+ box-sizing: border-box;
40
+ }
41
+ .chip .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
42
+ .chip button, .clear {
43
+ display: inline-grid;
44
+ place-items: center;
45
+ flex: none;
46
+ width: 1.125rem;
47
+ height: 1.125rem;
48
+ margin: 0;
49
+ padding: 0;
50
+ color: var(--ray-muted-foreground);
51
+ background: none;
52
+ border: 0;
53
+ border-radius: var(--ray-radius-xs);
54
+ cursor: pointer;
55
+ --ray-icon-size: 0.75rem;
56
+ }
57
+ .chip button:hover, .clear:hover { color: var(--ray-foreground); background: var(--ray-default-hover); }
58
+ .chip button:focus-visible, .clear:focus-visible { outline: var(--ray-focus-ring-width) solid var(--ray-ring); outline-offset: 1px; }
59
+ .tail { display: inline-flex; align-items: center; gap: 0.125rem; margin-inline-start: auto; padding-inline-end: calc(var(--_px) * 0.5); align-self: stretch; }
60
+ .clear { width: 1.375rem; height: 1.375rem; }
61
+ .clear[hidden] { display: none; }
62
+ .chevron { display: inline-flex; color: var(--ray-muted-foreground); pointer-events: none; transition: transform var(--ray-duration-fast) var(--ray-easing-standard); --ray-icon-size: 1em; }
63
+ :host([open]) .chevron { transform: rotate(180deg); }
64
+ .options { display: none; }
65
+
66
+ .listbox {
67
+ box-sizing: border-box;
68
+ max-height: min(18rem, calc(100vh - 16px));
69
+ overflow: auto;
70
+ margin: 0;
71
+ padding: var(--ray-space-1);
72
+ color: var(--ray-popover-foreground);
73
+ background: var(--ray-popover);
74
+ border: 1px solid var(--ray-border);
75
+ border-radius: var(--ray-radius-default);
76
+ box-shadow: var(--ray-shadow-md);
77
+ font-size: var(--_fs);
78
+ overscroll-behavior: contain;
79
+ }
80
+ .listbox:popover-open { animation: ray-pop-in var(--ray-duration-fast) var(--ray-easing-out); }
81
+ @keyframes ray-pop-in { from { opacity: 0; transform: translateY(-2px); } }
82
+ .option {
83
+ display: flex;
84
+ align-items: center;
85
+ gap: var(--ray-space-2);
86
+ min-height: calc(var(--_h) - 0.25rem);
87
+ padding: 0 var(--ray-space-2);
88
+ border-radius: var(--ray-radius-sm);
89
+ cursor: pointer;
90
+ user-select: none;
91
+ }
92
+ .option .text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
93
+ .option .text mark { color: inherit; background: none; font-weight: var(--ray-font-weight-bold, 700); }
94
+ .option .mark { display: inline-flex; color: var(--ray-primary); visibility: hidden; --ray-icon-size: 1em; }
95
+ .option[aria-selected="true"] .mark { visibility: visible; }
96
+ .option[data-active] { background: var(--ray-accent); color: var(--ray-accent-foreground); }
97
+ .option[aria-disabled="true"] { color: var(--ray-muted-foreground); cursor: not-allowed; opacity: 0.6; }
98
+ .option.create { color: var(--ray-primary-light-color); }
99
+ .group-label { padding: var(--ray-space-2) var(--ray-space-2) var(--ray-space-1); font-size: var(--ray-font-size-xs); font-weight: var(--ray-font-weight-medium); color: var(--ray-muted-foreground); }
100
+ .empty, .more { padding: var(--ray-space-2); font-size: calc(var(--_fs) - 0.0625rem); color: var(--ray-muted-foreground); }
101
+ `;
102
+
103
+ /** "a, b" (an attribute) or an array, as an array of strings. */
104
+ const toList = (v) => (Array.isArray(v) ? v.map(String) : String(v ?? "").split(",").map((s) => s.trim()).filter(Boolean));
105
+
106
+ export class RayCombobox extends FieldControl {
107
+ static properties = {
108
+ ...FieldControl.fieldProperties,
109
+ value: { attribute: false },
110
+ data: { attribute: false },
111
+ multiple: { type: Boolean, reflect: true },
112
+ placeholder: { type: String },
113
+ creatable: { type: Boolean },
114
+ clearable: { type: Boolean },
115
+ maxValues: { type: Number, attribute: "max-values" },
116
+ limit: { type: Number },
117
+ nothingFound: { type: String, attribute: "nothing-found", default: "No matches" },
118
+ open: { type: Boolean, reflect: true },
119
+ };
120
+ static styles = [sizeStyles, radiusStyles, fieldStyles, boxStyles, styles];
121
+ static template = fieldTemplate(
122
+ `<span class="chips" part="chips"></span><input id="control" class="control" part="input" role="combobox" aria-autocomplete="list" aria-expanded="false" aria-controls="listbox" autocomplete="off" spellcheck="false">` +
123
+ `<span class="tail"><button type="button" class="clear" part="clear" aria-label="Clear" hidden tabindex="-1"><span data-ray-icon="close"></span></button><span class="chevron" aria-hidden="true" data-ray-icon="chevron-down"></span></span>`,
124
+ { sections: false },
125
+ ) + `<div class="listbox" id="listbox" part="dropdown" role="listbox" popover="manual" tabindex="-1"></div><div class="options"><slot></slot></div>`;
126
+
127
+ static get observedAttributes() {
128
+ return [...super.observedAttributes, "value"];
129
+ }
130
+
131
+ attributeChangedCallback(attr, old, v) {
132
+ if (attr === "value") {
133
+ this.value = this.multiple || this.hasAttribute("multiple") ? toList(v) : (v ?? "");
134
+ return;
135
+ }
136
+ super.attributeChangedCallback(attr, old, v);
137
+ }
138
+
139
+ #options = [];
140
+ #items = [];
141
+ #active = -1;
142
+ #query = "";
143
+ #filtering = false;
144
+ #undismiss = null;
145
+ #stopTracking = null;
146
+ #id = uid("cbx");
147
+
148
+ constructor() {
149
+ super();
150
+ this.listbox = this.$("#listbox");
151
+ }
152
+
153
+ wire() {
154
+ const c = this.control;
155
+ this._default = this.getAttribute("value");
156
+ this._observer = new MutationObserver(() => this._readOptions());
157
+ this._observer.observe(this, { childList: true, subtree: true, attributes: true, characterData: true, attributeFilter: ["value", "label", "disabled", "selected"] });
158
+ this._readOptions();
159
+ c.addEventListener("input", () => {
160
+ this.#query = c.value;
161
+ this.#filtering = true;
162
+ this.open = true;
163
+ this._renderList();
164
+ this._activate(this._firstEnabled());
165
+ });
166
+ c.addEventListener("keydown", (e) => this._key(e));
167
+ c.addEventListener("blur", () => {
168
+ if (this.open) return;
169
+ this._settleText();
170
+ });
171
+ c.addEventListener("click", () => this.toggle(true));
172
+ this.$(".box").addEventListener("pointerdown", (e) => {
173
+ // A click on the box (not on a button) focuses the text field.
174
+ if (e.target.closest?.("button")) return;
175
+ if (e.target !== c) {
176
+ e.preventDefault();
177
+ c.focus();
178
+ this.toggle(true);
179
+ }
180
+ });
181
+ this.$(".clear").addEventListener("click", () => {
182
+ this._setValue(this.multiple ? [] : "", true);
183
+ this.#query = "";
184
+ c.value = "";
185
+ c.focus();
186
+ });
187
+ this.$(".chips").addEventListener("click", (e) => {
188
+ const b = e.target.closest?.("button[data-value]");
189
+ if (!b) return;
190
+ this._remove(b.dataset.value);
191
+ c.focus();
192
+ });
193
+ this.listbox.addEventListener("pointerdown", (e) => e.preventDefault());
194
+ this.listbox.addEventListener("pointermove", (e) => {
195
+ const i = this.#items.findIndex((it) => it.el === e.target.closest?.(".option"));
196
+ if (i >= 0 && !this.#items[i].disabled) this._activate(i, false);
197
+ });
198
+ this.listbox.addEventListener("click", (e) => {
199
+ const i = this.#items.findIndex((it) => it.el === e.target.closest?.(".option"));
200
+ if (i >= 0) this._choose(i);
201
+ });
202
+ this.listbox.addEventListener("toggle", (e) => this._onToggle(e.newState === "open"));
203
+ }
204
+
205
+ disconnectedCallback() {
206
+ super.disconnectedCallback();
207
+ this.#stopTracking?.();
208
+ this.#stopTracking = null;
209
+ this.#undismiss?.();
210
+ this.#undismiss = null;
211
+ }
212
+
213
+ /** The current values as an array (one or none without `multiple`). */
214
+ get values() {
215
+ return this.multiple ? toList(this.value) : (this.value ? [String(this.value)] : []);
216
+ }
217
+
218
+ update(changed) {
219
+ if (changed.has("data")) this._readOptions();
220
+ const c = this.control;
221
+ c.placeholder = this.multiple && this.values.length ? "" : (this.placeholder ?? "");
222
+ c.readOnly = false;
223
+ this.syncField();
224
+ c.setAttribute("aria-expanded", String(Boolean(this.open)));
225
+ this.listbox.toggleAttribute("aria-multiselectable", Boolean(this.multiple));
226
+ if (this.multiple) this.listbox.setAttribute("aria-multiselectable", "true");
227
+ this._renderChips();
228
+ if (!this.#filtering && document.activeElement !== this) this._settleText();
229
+ this.$(".clear").hidden = !this.clearable || !this.values.length || this.effectivelyDisabled;
230
+ this._commit();
231
+ if (changed.has("open")) this._setOpen(Boolean(this.open));
232
+ if (this.open) this._renderList();
233
+ }
234
+
235
+ /** Opens or closes the dropdown. */
236
+ toggle(force) {
237
+ if (this.effectivelyDisabled) return;
238
+ this.open = force ?? !this.open;
239
+ }
240
+
241
+ // Options from `data`, else from the child option elements.
242
+ _readOptions() {
243
+ const out = [];
244
+ if (Array.isArray(this.data)) {
245
+ for (const d of this.data) {
246
+ if (typeof d === "string") out.push({ value: d, label: d, disabled: false, group: "" });
247
+ else out.push({ value: String(d.value), label: d.label ?? String(d.value), disabled: Boolean(d.disabled), group: d.group ?? "" });
248
+ }
249
+ } else {
250
+ const add = (o, group) => out.push({ value: o.value, label: o.label || o.textContent.trim(), disabled: o.disabled, group });
251
+ for (const child of this.children) {
252
+ if (child.localName === "option") add(child, "");
253
+ else if (child.localName === "optgroup") for (const o of child.querySelectorAll("option")) add(o, child.label);
254
+ }
255
+ // An option marked selected sets the first value.
256
+ if (this.value === undefined) {
257
+ const sel = [...this.querySelectorAll("option[selected]")].map((o) => o.value);
258
+ if (sel.length) this.commitProperty("value", this.multiple ? sel : sel[0]);
259
+ }
260
+ }
261
+ this.#options = out;
262
+ this.requestUpdate();
263
+ }
264
+
265
+ _labelOf(value) {
266
+ return this.#options.find((o) => o.value === value)?.label ?? value;
267
+ }
268
+
269
+ // The text field shows the chosen label (single) or nothing (multiple).
270
+ _settleText() {
271
+ const c = this.control;
272
+ if (!c) return;
273
+ this.#filtering = false;
274
+ this.#query = "";
275
+ if (this.multiple) {
276
+ if (c.value) c.value = "";
277
+ } else {
278
+ const text = this.value ? this._labelOf(String(this.value)) : "";
279
+ if (c.value !== text) c.value = text;
280
+ }
281
+ }
282
+
283
+ _renderChips() {
284
+ const box = this.$(".chips");
285
+ box.replaceChildren();
286
+ if (!this.multiple) {
287
+ this.$(".box").removeAttribute("data-has-chips");
288
+ return;
289
+ }
290
+ for (const v of this.values) {
291
+ const chip = document.createElement("span");
292
+ chip.className = "chip";
293
+ chip.setAttribute("part", "chip");
294
+ const t = document.createElement("span");
295
+ t.className = "t";
296
+ t.textContent = this._labelOf(v);
297
+ const b = document.createElement("button");
298
+ b.type = "button";
299
+ b.tabIndex = -1;
300
+ b.dataset.value = v;
301
+ b.setAttribute("aria-label", `Remove ${this._labelOf(v)}`);
302
+ b.innerHTML = `<span data-ray-icon="close"></span>`;
303
+ chip.append(t, b);
304
+ box.append(chip);
305
+ }
306
+ this.paintIcons(box);
307
+ this.$(".box").toggleAttribute("data-has-chips", this.values.length > 0);
308
+ }
309
+
310
+ // The options that match what was typed (all when not filtering).
311
+ _matches() {
312
+ const q = this.#filtering ? this.#query.trim().toLowerCase() : "";
313
+ return q ? this.#options.filter((o) => o.label.toLowerCase().includes(q)) : this.#options.slice();
314
+ }
315
+
316
+ _renderList() {
317
+ const lb = this.listbox;
318
+ lb.replaceChildren();
319
+ this.#items = [];
320
+ const values = this.values;
321
+ const full = this.multiple && this.maxValues > 0 && values.length >= this.maxValues;
322
+ const all = this._matches();
323
+ const shown = this.limit > 0 ? all.slice(0, this.limit) : all;
324
+ const q = this.#filtering ? this.#query.trim() : "";
325
+ let groupEl = null;
326
+ let group = null;
327
+ let n = 0;
328
+ for (const o of shown) {
329
+ if (o.group !== group) {
330
+ group = o.group;
331
+ if (group) {
332
+ groupEl = document.createElement("div");
333
+ groupEl.setAttribute("role", "group");
334
+ const gl = document.createElement("div");
335
+ gl.className = "group-label";
336
+ gl.id = `${this.#id}-g${n}`;
337
+ gl.textContent = group;
338
+ groupEl.setAttribute("aria-labelledby", gl.id);
339
+ groupEl.append(gl);
340
+ lb.append(groupEl);
341
+ } else {
342
+ groupEl = null;
343
+ }
344
+ }
345
+ const selected = values.includes(o.value);
346
+ const disabled = o.disabled || (full && !selected);
347
+ const el = document.createElement("div");
348
+ el.className = "option";
349
+ el.id = `${this.#id}-o${n++}`;
350
+ el.setAttribute("role", "option");
351
+ el.setAttribute("part", "option");
352
+ el.setAttribute("aria-selected", String(selected));
353
+ el.setAttribute("aria-disabled", String(disabled));
354
+ const text = document.createElement("span");
355
+ text.className = "text";
356
+ // The typed part of each match in bold.
357
+ const at = q ? o.label.toLowerCase().indexOf(q.toLowerCase()) : -1;
358
+ if (at >= 0) {
359
+ const mark = document.createElement("mark");
360
+ mark.textContent = o.label.slice(at, at + q.length);
361
+ text.append(o.label.slice(0, at), mark, o.label.slice(at + q.length));
362
+ } else {
363
+ text.textContent = o.label;
364
+ }
365
+ const check = document.createElement("span");
366
+ check.className = "mark";
367
+ check.setAttribute("data-ray-icon", "check");
368
+ el.append(text, check);
369
+ (groupEl ?? lb).append(el);
370
+ this.#items.push({ el, value: o.value, disabled, create: false });
371
+ }
372
+ if (this.creatable && q && !this.#options.some((o) => o.label.toLowerCase() === q.toLowerCase()) && !full) {
373
+ const el = document.createElement("div");
374
+ el.className = "option create";
375
+ el.id = `${this.#id}-create`;
376
+ el.setAttribute("role", "option");
377
+ el.setAttribute("aria-selected", "false");
378
+ el.textContent = `+ Create “${q}”`;
379
+ lb.append(el);
380
+ this.#items.push({ el, value: q, disabled: false, create: true });
381
+ }
382
+ if (all.length > shown.length) {
383
+ const more = document.createElement("div");
384
+ more.className = "more";
385
+ more.textContent = `${all.length - shown.length} more: keep typing`;
386
+ lb.append(more);
387
+ }
388
+ if (!this.#items.length) {
389
+ const empty = document.createElement("div");
390
+ empty.className = "empty";
391
+ empty.textContent = this.nothingFound ?? "No matches";
392
+ lb.append(empty);
393
+ }
394
+ this.paintIcons(lb);
395
+ if (this.#active >= this.#items.length) this.#active = -1;
396
+ if (this.#active >= 0) this._activate(this.#active, false);
397
+ }
398
+
399
+ _firstEnabled() {
400
+ return this.#items.findIndex((it) => !it.disabled);
401
+ }
402
+
403
+ _activate(i, scroll = true) {
404
+ for (const it of this.#items) it.el.removeAttribute("data-active");
405
+ this.#active = i;
406
+ const it = this.#items[i];
407
+ if (!it) {
408
+ this.control.removeAttribute("aria-activedescendant");
409
+ return;
410
+ }
411
+ it.el.setAttribute("data-active", "");
412
+ this.control.setAttribute("aria-activedescendant", it.el.id);
413
+ if (scroll) it.el.scrollIntoView({ block: "nearest" });
414
+ }
415
+
416
+ _step(dir) {
417
+ const n = this.#items.length;
418
+ if (!n) return;
419
+ for (let k = 1; k <= n; k++) {
420
+ const i = (this.#active + dir * k + n * 2) % n;
421
+ if (!this.#items[i].disabled) return this._activate(i);
422
+ }
423
+ }
424
+
425
+ _choose(i) {
426
+ const it = this.#items[i];
427
+ if (!it || it.disabled) return;
428
+ if (it.create) {
429
+ this.#options.push({ value: it.value, label: it.value, disabled: false, group: "" });
430
+ this.emit("ray-create", { value: it.value });
431
+ }
432
+ if (this.multiple) {
433
+ const values = this.values;
434
+ this._setValue(values.includes(it.value) ? values.filter((v) => v !== it.value) : [...values, it.value], true);
435
+ this.#query = "";
436
+ this.#filtering = false;
437
+ this.control.value = "";
438
+ this._renderList();
439
+ this._activate(Math.min(i, this.#items.length - 1), false);
440
+ } else {
441
+ this._setValue(it.value, true);
442
+ this.open = false;
443
+ this._settleText();
444
+ }
445
+ }
446
+
447
+ _remove(value) {
448
+ this._setValue(this.values.filter((v) => v !== value), true);
449
+ }
450
+
451
+ _setValue(v, user) {
452
+ const before = JSON.stringify(this.values);
453
+ this.commitProperty("value", v);
454
+ const after = JSON.stringify(this.multiple ? toList(v) : (v ? [String(v)] : []));
455
+ this.requestUpdate();
456
+ if (user && before !== after) {
457
+ this.dispatchEvent(new Event("input", { bubbles: true, composed: true }));
458
+ this.dispatchEvent(new Event("change", { bubbles: true }));
459
+ }
460
+ }
461
+
462
+ _key(e) {
463
+ const k = e.key;
464
+ if (k === "ArrowDown" || k === "ArrowUp") {
465
+ e.preventDefault();
466
+ if (!this.open) {
467
+ this.open = true;
468
+ queueMicrotask(() => this._activate(k === "ArrowUp" ? this.#items.length - 1 : this._firstEnabled()));
469
+ return;
470
+ }
471
+ this._step(k === "ArrowDown" ? 1 : -1);
472
+ } else if (k === "Enter") {
473
+ if (this.open && this.#active >= 0) {
474
+ e.preventDefault();
475
+ this._choose(this.#active);
476
+ } else if (this.open && this.creatable && this.#query.trim()) {
477
+ e.preventDefault();
478
+ const i = this.#items.findIndex((it) => it.create);
479
+ if (i >= 0) this._choose(i);
480
+ }
481
+ } else if (k === "Escape") {
482
+ if (this.open) {
483
+ e.preventDefault();
484
+ this.open = false;
485
+ this._settleText();
486
+ } else if (this.clearable && this.values.length) {
487
+ e.preventDefault();
488
+ this._setValue(this.multiple ? [] : "", true);
489
+ }
490
+ } else if (k === "Backspace" && this.multiple && !this.control.value && this.values.length) {
491
+ this._remove(this.values[this.values.length - 1]);
492
+ } else if (k === "Tab") {
493
+ this.open = false;
494
+ this._settleText();
495
+ }
496
+ }
497
+
498
+ _setOpen(open) {
499
+ const lb = this.listbox;
500
+ if (!lb.isConnected) return;
501
+ const shown = lb.matches(":popover-open");
502
+ if (open && !shown) {
503
+ try {
504
+ lb.showPopover();
505
+ } catch {
506
+ this.commitProperty("open", false);
507
+ return;
508
+ }
509
+ this.#undismiss ??= dismissable({ panel: lb, owner: this, trigger: this.$(".box"), close: () => { this.open = false; this._settleText(); } });
510
+ } else if (!open && shown) {
511
+ lb.hidePopover();
512
+ }
513
+ }
514
+
515
+ _onToggle(open) {
516
+ this.control.setAttribute("aria-expanded", String(open));
517
+ if (open) {
518
+ const box = this.$(".box");
519
+ const update = () => place(box.getBoundingClientRect(), this.listbox, { placement: "bottom-start", offset: 4, matchWidth: true });
520
+ update();
521
+ this.#stopTracking?.();
522
+ this.#stopTracking = autoUpdate(box, this.listbox, update);
523
+ this._renderList();
524
+ if (this.#active < 0) {
525
+ const sel = this.#items.findIndex((it) => this.values.includes(it.value));
526
+ this._activate(sel >= 0 ? sel : this._firstEnabled());
527
+ }
528
+ } else {
529
+ this.#stopTracking?.();
530
+ this.#stopTracking = null;
531
+ this.#undismiss?.();
532
+ this.#undismiss = null;
533
+ this._activate(-1);
534
+ this.#filtering = false;
535
+ }
536
+ if (Boolean(this.open) !== open) this.commitProperty("open", open);
537
+ this.emit(open ? "ray-open" : "ray-close");
538
+ }
539
+
540
+ _commit() {
541
+ const values = this.values;
542
+ if (this.multiple && this.name && values.length) {
543
+ const data = new FormData();
544
+ for (const v of values) data.append(this.name, v);
545
+ this.internals.setFormValue(data);
546
+ } else {
547
+ this.internals.setFormValue(values[0] ?? null);
548
+ }
549
+ if (this.required && !values.length) this.internals.setValidity({ valueMissing: true }, "Please choose an option.", this.control);
550
+ else this.internals.setValidity({});
551
+ }
552
+
553
+ formResetCallback() {
554
+ this.value = this.multiple ? toList(this._default) : (this._default ?? "");
555
+ }
556
+
557
+ formStateRestoreCallback(state) {
558
+ if (state instanceof FormData) this.value = state.getAll(this.name).map(String);
559
+ else this.value = state ?? "";
560
+ }
561
+ }
562
+
563
+ define("ray-combobox", RayCombobox);