taga11y 1.0.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 (64) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +309 -0
  3. package/dist/a11y.d.ts +8 -0
  4. package/dist/a11y.d.ts.map +1 -0
  5. package/dist/core/dropdown.d.ts +63 -0
  6. package/dist/core/dropdown.d.ts.map +1 -0
  7. package/dist/core/input.d.ts +42 -0
  8. package/dist/core/input.d.ts.map +1 -0
  9. package/dist/core/keyboard.d.ts +37 -0
  10. package/dist/core/keyboard.d.ts.map +1 -0
  11. package/dist/core/selection.d.ts +24 -0
  12. package/dist/core/selection.d.ts.map +1 -0
  13. package/dist/dom/chips.d.ts +6 -0
  14. package/dist/dom/chips.d.ts.map +1 -0
  15. package/dist/dom/listbox.d.ts +5 -0
  16. package/dist/dom/listbox.d.ts.map +1 -0
  17. package/dist/dom/render.d.ts +24 -0
  18. package/dist/dom/render.d.ts.map +1 -0
  19. package/dist/i18n.d.ts +44 -0
  20. package/dist/i18n.d.ts.map +1 -0
  21. package/dist/index.d.ts +212 -0
  22. package/dist/index.d.ts.map +1 -0
  23. package/dist/locales/ar.json +28 -0
  24. package/dist/locales/pt.json +21 -0
  25. package/dist/react/index.d.ts +46 -0
  26. package/dist/react/index.d.ts.map +1 -0
  27. package/dist/react.cjs +1 -0
  28. package/dist/react.mjs +83 -0
  29. package/dist/taga11y.cjs +1 -0
  30. package/dist/taga11y.css +2 -0
  31. package/dist/taga11y.iife.js +1 -0
  32. package/dist/taga11y.mjs +829 -0
  33. package/dist/types.d.ts +201 -0
  34. package/dist/types.d.ts.map +1 -0
  35. package/dist/utils/debounce.d.ts +5 -0
  36. package/dist/utils/debounce.d.ts.map +1 -0
  37. package/dist/utils/filter.d.ts +5 -0
  38. package/dist/utils/filter.d.ts.map +1 -0
  39. package/dist/utils/id.d.ts +11 -0
  40. package/dist/utils/id.d.ts.map +1 -0
  41. package/dist/vue/index.d.ts +155 -0
  42. package/dist/vue/index.d.ts.map +1 -0
  43. package/dist/vue.cjs +1 -0
  44. package/dist/vue.mjs +127 -0
  45. package/package.json +111 -0
  46. package/src/a11y.ts +85 -0
  47. package/src/aria-notify.d.ts +9 -0
  48. package/src/core/dropdown.ts +331 -0
  49. package/src/core/input.ts +172 -0
  50. package/src/core/keyboard.ts +200 -0
  51. package/src/core/selection.ts +78 -0
  52. package/src/dom/chips.ts +80 -0
  53. package/src/dom/listbox.ts +49 -0
  54. package/src/dom/render.ts +154 -0
  55. package/src/i18n.ts +131 -0
  56. package/src/index.ts +900 -0
  57. package/src/react/index.tsx +223 -0
  58. package/src/styles/index.css +367 -0
  59. package/src/types.ts +224 -0
  60. package/src/utils/debounce.ts +27 -0
  61. package/src/utils/filter.ts +14 -0
  62. package/src/utils/id.ts +30 -0
  63. package/src/vite-env.d.ts +1 -0
  64. package/src/vue/index.ts +145 -0
@@ -0,0 +1,829 @@
1
+ //#region src/i18n.ts
2
+ var e = {
3
+ "a11y.tagAdded": "Tag added: {label}",
4
+ "a11y.tagRemoved": "Tag removed: {label}",
5
+ "a11y.tagsCleared": "Tags cleared",
6
+ "a11y.noResults": "No results",
7
+ "a11y.resultsCount": {
8
+ one: "{n} result available",
9
+ other: "{n} results available"
10
+ },
11
+ "a11y.tagsSummary": {
12
+ one: "{n} tag selected: {labels}",
13
+ other: "{n} tags selected: {labels}"
14
+ },
15
+ "a11y.selectedTags": "Selected tags",
16
+ "a11y.removeTag": "Remove {label}",
17
+ "ui.loading": "Loading suggestions…",
18
+ "error.duplicate": "Already added: {label}",
19
+ "error.maxReached": "Maximum tags reached",
20
+ "error.notInList": "Not in the list",
21
+ "error.loadError": "Failed to load suggestions"
22
+ }, t = Object.keys(e);
23
+ function n(n) {
24
+ if (!n) return { ...e };
25
+ let r = {
26
+ ...e,
27
+ ...n
28
+ }, i = t.filter((e) => !(e in n));
29
+ return i.length > 0 && console.warn(`taga11y: i18n strings missing ${i.length} key(s), falling back to English: ${i.join(", ")}`), r;
30
+ }
31
+ function r(e) {
32
+ return typeof e == "object" && !!e;
33
+ }
34
+ function i(e, t) {
35
+ return t ? e.replace(/\{(\w+)\}/g, (e, n) => n in t ? String(t[n]) : e) : e;
36
+ }
37
+ function a(e, t, n, a) {
38
+ let o = e[n];
39
+ return r(o) ? a && "n" in a ? i(o[new Intl.PluralRules(t, { type: "cardinal" }).select(Number(a.n))] ?? o.other, a) : i(o.other, a) : i(o, a);
40
+ }
41
+ function o(e) {
42
+ return e.split("-")[0].toLowerCase();
43
+ }
44
+ function s(e, t, n) {
45
+ let r = document.documentElement.getAttribute("lang");
46
+ (!r || o(r) !== o(t)) && e.setAttribute("lang", t), n && e.setAttribute("dir", n);
47
+ }
48
+ //#endregion
49
+ //#region src/utils/id.ts
50
+ function c() {
51
+ let e = new Uint8Array(4);
52
+ return crypto.getRandomValues(e), Array.from(e, (e) => e.toString(16).padStart(2, "0")).join("");
53
+ }
54
+ function l(e) {
55
+ let t = e.id.trim();
56
+ return t ? `taga11y-${t}` : `taga11y-${c()}`;
57
+ }
58
+ function u(e) {
59
+ return {
60
+ base: e,
61
+ label: `${e}-label`,
62
+ listbox: `${e}-listbox`,
63
+ option: (t) => `${e}-option-${t}`,
64
+ error: `${e}-error`,
65
+ announcer: `${e}-announcer`
66
+ };
67
+ }
68
+ //#endregion
69
+ //#region src/utils/filter.ts
70
+ function d(e) {
71
+ return typeof e == "string" ? e : e.label;
72
+ }
73
+ function f(e) {
74
+ return typeof e == "string" ? e : e.value;
75
+ }
76
+ function p(e, t) {
77
+ let n = t.toLowerCase();
78
+ return e.filter((e) => d(e).toLowerCase().includes(n));
79
+ }
80
+ //#endregion
81
+ //#region src/core/selection.ts
82
+ var m = class {
83
+ constructor(e, t) {
84
+ this.tags = [], this.nextId = 0, this.base = e, this.maxTags = t;
85
+ }
86
+ addTag(e, t) {
87
+ if (this.maxTags !== void 0 && this.tags.length >= this.maxTags) return {
88
+ ok: !1,
89
+ reason: "max-reached"
90
+ };
91
+ if (this.tags.some((t) => t.value.toLowerCase() === e.toLowerCase())) return {
92
+ ok: !1,
93
+ reason: "duplicate"
94
+ };
95
+ let n = t ?? e, r = {
96
+ id: `${this.base}-tag-${this.nextId++}`,
97
+ label: n,
98
+ value: e
99
+ };
100
+ return this.tags.push(r), {
101
+ ok: !0,
102
+ tag: r
103
+ };
104
+ }
105
+ removeTag(e) {
106
+ let t = this.tags.findIndex((t) => t.id === e);
107
+ if (t === -1) return null;
108
+ let [n] = this.tags.splice(t, 1);
109
+ return n ?? null;
110
+ }
111
+ clearTags() {
112
+ let e = [...this.tags];
113
+ return this.tags = [], this.nextId = 0, e;
114
+ }
115
+ getTags() {
116
+ return [...this.tags];
117
+ }
118
+ addTags(e) {
119
+ let t = [];
120
+ for (let n of e) {
121
+ let e = this.addTag(f(n), d(n));
122
+ e.ok && t.push(e.tag);
123
+ }
124
+ return t;
125
+ }
126
+ setTags(e) {
127
+ return this.clearTags(), this.addTags(e);
128
+ }
129
+ isTagged(e) {
130
+ return this.tags.some((t) => t.value.toLowerCase() === e.toLowerCase());
131
+ }
132
+ setMaxTags(e) {
133
+ this.maxTags = e;
134
+ }
135
+ };
136
+ //#endregion
137
+ //#region src/utils/debounce.ts
138
+ function h(e, t) {
139
+ let n, r = (...r) => {
140
+ n !== void 0 && clearTimeout(n), n = setTimeout(() => {
141
+ n = void 0, e(...r);
142
+ }, t);
143
+ };
144
+ return r.cancel = () => {
145
+ n !== void 0 && (clearTimeout(n), n = void 0);
146
+ }, r;
147
+ }
148
+ //#endregion
149
+ //#region src/dom/listbox.ts
150
+ function g(e) {
151
+ let t = document.createElement("ul");
152
+ return t.className = "taga11y__listbox", t.setAttribute("role", "listbox"), t.id = e.listbox, t.hidden = !0, t;
153
+ }
154
+ function _(e, t, n) {
155
+ e.innerHTML = "", t.forEach((t, r) => {
156
+ let i = document.createElement("li");
157
+ i.className = "taga11y__option", i.setAttribute("role", "option"), i.id = n.option(r), i.setAttribute("aria-selected", "false"), i.textContent = d(t), e.appendChild(i);
158
+ });
159
+ }
160
+ function v(e, t, n, r, i, a) {
161
+ if (e.querySelectorAll(".taga11y__option").forEach((e) => e.remove()), n.removeAttribute("aria-activedescendant"), i) {
162
+ t.hidden = !1;
163
+ return;
164
+ }
165
+ t.hidden = !0, _(e, r, a);
166
+ }
167
+ //#endregion
168
+ //#region src/core/dropdown.ts
169
+ var y = class {
170
+ constructor(e, t) {
171
+ if (this.isOpen = !1, this.activeIndex = -1, this.filteredSuggestions = [], this.isLoading = !1, this.cachedItems = null, this._isPrefetching = !1, this.abortController = null, this.debouncedQuery = null, this.listbox = e.listbox, this.input = e.input, this.loading = e.loading, this.wrapper = e.wrapper, this.ids = t.ids, this.strings = t.strings, this.locale = t.locale, this.maxSuggestions = t.maxSuggestions, this.source = t.suggestions, this.getSelectedValues = t.getSelectedValues, this.onAnnounce = t.onAnnounce ?? (() => void 0), this.onLoadError = t.onLoadError ?? (() => void 0), this.onOptionSelect = t.onOptionSelect, this.source && "query" in this.source) {
172
+ let e = this.source;
173
+ this.debouncedQuery = h((t) => {
174
+ this.executeDynamicQuery(e, t);
175
+ }, t.debounceMs);
176
+ }
177
+ this.source && "once" in this.source && this.startPrefetch(this.source), this.resizeObserver = new ResizeObserver(() => {}), this.resizeObserver.observe(this.wrapper), this.listbox.addEventListener("mousedown", (e) => {
178
+ let t = e.target.closest("[role=\"option\"]");
179
+ if (!t) return;
180
+ e.preventDefault();
181
+ let n = Array.from(this.listbox.querySelectorAll("[role=\"option\"]")).indexOf(t);
182
+ if (n === -1) return;
183
+ let r = this.filteredSuggestions[n];
184
+ r !== void 0 && this.onOptionSelect?.(r);
185
+ });
186
+ }
187
+ async startPrefetch(e) {
188
+ this._isPrefetching = !0;
189
+ try {
190
+ this.cachedItems = await e.once(), this._isPrefetching = !1, this.isLoading && (this.setLoading(!1), this.filter(this.input.value));
191
+ } catch {
192
+ this._isPrefetching = !1, this.isLoading && this.setLoading(!1), this.onLoadError(a(this.strings, this.locale, "error.loadError"));
193
+ }
194
+ }
195
+ async executeDynamicQuery(e, t) {
196
+ this.abortController?.abort(), this.abortController = new AbortController();
197
+ let { signal: n } = this.abortController;
198
+ this.setLoading(!0);
199
+ try {
200
+ let r = await e.query(t, n);
201
+ if (n.aborted) return;
202
+ let i = this.getSelectedValues(), o = p(r, t).filter((e) => !i.includes(f(e))).slice(0, this.maxSuggestions);
203
+ this.filteredSuggestions = o, this.setLoading(!1), o.length === 0 ? (this.close(), this.onAnnounce(a(this.strings, this.locale, "a11y.noResults"))) : (v(this.listbox, this.loading, this.input, o, !1, this.ids), this.isOpen || this.open(), this.onAnnounce(a(this.strings, this.locale, "a11y.resultsCount", { n: o.length })), this.highlightFirst());
204
+ } catch (e) {
205
+ if (e instanceof Error && e.name === "AbortError") return;
206
+ this.setLoading(!1), this.close(), this.onLoadError(a(this.strings, this.locale, "error.loadError"));
207
+ }
208
+ }
209
+ open() {
210
+ this.listbox.hidden = !1, this.input.setAttribute("aria-expanded", "true"), this.isOpen = !0;
211
+ }
212
+ close() {
213
+ this.debouncedQuery?.cancel(), this.abortController?.abort(), this.listbox.hidden = !0, this.input.setAttribute("aria-expanded", "false"), this.input.removeAttribute("aria-activedescendant"), this.activeIndex = -1, this.loading.hidden = !0, this.isLoading = !1, this.isOpen = !1;
214
+ }
215
+ toggle() {
216
+ this.isOpen ? this.close() : this.open();
217
+ }
218
+ filter(e) {
219
+ if (!this.source) return;
220
+ let t = e.trim();
221
+ if ("query" in this.source) {
222
+ if (!t) return;
223
+ this.debouncedQuery(t);
224
+ return;
225
+ }
226
+ if ("once" in this.source && this.cachedItems === null && this._isPrefetching) {
227
+ this.setLoading(!0);
228
+ return;
229
+ }
230
+ let n = "once" in this.source ? this.cachedItems ?? [] : this.source, r = this.getSelectedValues(), i = p(n, t).filter((e) => !r.includes(f(e))).slice(0, this.maxSuggestions);
231
+ this.filteredSuggestions = i, v(this.listbox, this.loading, this.input, i, !1, this.ids), i.length === 0 ? (this.close(), this.onAnnounce(a(this.strings, this.locale, "a11y.noResults"))) : (this.isOpen || this.open(), this.onAnnounce(a(this.strings, this.locale, "a11y.resultsCount", { n: i.length })), this.highlightFirst());
232
+ }
233
+ setActiveIndex(e) {
234
+ let t = Array.from(this.listbox.querySelectorAll("[role=\"option\"]"));
235
+ if (t.forEach((e) => e.setAttribute("aria-selected", "false")), e < 0 || e >= t.length) {
236
+ this.activeIndex = -1, this.input.removeAttribute("aria-activedescendant");
237
+ return;
238
+ }
239
+ this.activeIndex = e;
240
+ let n = t[e];
241
+ n && (n.setAttribute("aria-selected", "true"), this.input.setAttribute("aria-activedescendant", n.id));
242
+ }
243
+ highlightFirst() {
244
+ this.setActiveIndex(0);
245
+ }
246
+ highlightPrev() {
247
+ let e = this.filteredSuggestions.length;
248
+ if (e === 0) return;
249
+ let t = this.activeIndex <= 0 ? e - 1 : this.activeIndex - 1;
250
+ this.setActiveIndex(t);
251
+ }
252
+ highlightNext() {
253
+ let e = this.filteredSuggestions.length;
254
+ if (e === 0) return;
255
+ let t = this.activeIndex >= e - 1 ? 0 : this.activeIndex + 1;
256
+ this.setActiveIndex(t);
257
+ }
258
+ highlightHome() {
259
+ this.setActiveIndex(0);
260
+ }
261
+ highlightEnd() {
262
+ this.setActiveIndex(this.filteredSuggestions.length - 1);
263
+ }
264
+ selectActive() {
265
+ return this.activeIndex < 0 ? null : this.filteredSuggestions[this.activeIndex] ?? null;
266
+ }
267
+ setLoading(e) {
268
+ this.isLoading = e, v(this.listbox, this.loading, this.input, this.filteredSuggestions, e, this.ids), e && (this.isOpen || this.open(), this.onAnnounce(a(this.strings, this.locale, "ui.loading")));
269
+ }
270
+ setSource(e, t) {
271
+ if (this.debouncedQuery?.cancel(), this.abortController?.abort(), this.abortController = null, this.cachedItems = null, this._isPrefetching = !1, this.source = e, e && "query" in e) {
272
+ let n = e;
273
+ this.debouncedQuery = h((e) => {
274
+ this.executeDynamicQuery(n, e);
275
+ }, t);
276
+ } else this.debouncedQuery = null;
277
+ e && "once" in e && this.startPrefetch(e);
278
+ }
279
+ setDebounceMs(e) {
280
+ if (!this.source || !("query" in this.source)) return;
281
+ let t = this.source;
282
+ this.debouncedQuery?.cancel(), this.debouncedQuery = h((e) => {
283
+ this.executeDynamicQuery(t, e);
284
+ }, e);
285
+ }
286
+ setMaxSuggestions(e) {
287
+ this.maxSuggestions = e;
288
+ }
289
+ getAllSuggestions() {
290
+ return this.source ? Array.isArray(this.source) ? this.source : "once" in this.source ? this.cachedItems ?? [] : this.filteredSuggestions : [];
291
+ }
292
+ destroy() {
293
+ this.debouncedQuery?.cancel(), this.abortController?.abort(), this.resizeObserver.disconnect();
294
+ }
295
+ }, b = class {
296
+ constructor(e, t) {
297
+ this.currentValue = "", this.input = e, this.onInput = t.onInput, this.onBlurCommit = t.onBlurCommit, this.onBlurClose = t.onBlurClose, this.onFocus = t.onFocus, this.getDelimiters = t.getDelimiters ?? (() => []), this.isEnforceSuggestions = t.isEnforceSuggestions ?? (() => !1), this.getLoadedSuggestions = t.getLoadedSuggestions ?? (() => []), this.onCommit = t.onCommit ?? (() => void 0), this.onCommitRejected = t.onCommitRejected ?? (() => void 0), this.onPaste = t.onPaste ?? (() => void 0), this.onCloseDropdown = t.onCloseDropdown ?? (() => void 0), this.handleInput = () => {
298
+ this.currentValue = this.input.value;
299
+ let e = this.getCharDelimiters();
300
+ if (e.length > 0 && this.containsAny(this.currentValue, e)) {
301
+ this.extractAndCommit(e);
302
+ return;
303
+ }
304
+ this.onInput(this.currentValue);
305
+ }, this.handleFocus = () => {
306
+ this.onFocus?.();
307
+ }, this.handleBlur = () => {
308
+ clearTimeout(this.blurTimer), this.blurTimer = setTimeout(() => {
309
+ this.currentValue && this.onBlurCommit(this.currentValue), this.onBlurClose();
310
+ }, 150);
311
+ }, this.handlePaste = (e) => {
312
+ let t = e.clipboardData?.getData("text") ?? "";
313
+ if (!t) return;
314
+ let n = this.getCharDelimiters();
315
+ if (n.length === 0 || !this.containsAny(t, n)) return;
316
+ e.preventDefault();
317
+ let r = RegExp(`[${x(n.join(""))}]`), i = t.split(r).map((e) => e.trim()).filter((e) => e !== "");
318
+ this.input.value = "", this.currentValue = "", this.onPaste(i), this.onCloseDropdown();
319
+ }, this.input.addEventListener("input", this.handleInput), this.input.addEventListener("focus", this.handleFocus), this.input.addEventListener("blur", this.handleBlur), this.input.addEventListener("paste", this.handlePaste);
320
+ }
321
+ clearValue() {
322
+ this.input.value = "", this.currentValue = "";
323
+ }
324
+ getValue() {
325
+ return this.currentValue;
326
+ }
327
+ destroy() {
328
+ clearTimeout(this.blurTimer), this.input.removeEventListener("input", this.handleInput), this.input.removeEventListener("focus", this.handleFocus), this.input.removeEventListener("blur", this.handleBlur), this.input.removeEventListener("paste", this.handlePaste);
329
+ }
330
+ getCharDelimiters() {
331
+ return this.getDelimiters().filter((e) => e.length === 1);
332
+ }
333
+ containsAny(e, t) {
334
+ for (let n of t) if (e.indexOf(n) !== -1) return !0;
335
+ return !1;
336
+ }
337
+ extractAndCommit(e) {
338
+ let t = this.currentValue, n = RegExp(`[${x(e.join(""))}]`), r = t.split(n).map((e) => e.trim()), i = r[r.length - 1] ?? "", a = r.slice(0, -1).filter((e) => e !== "");
339
+ for (let e of a) this.commitChunk(e);
340
+ this.input.value = i, this.currentValue = i, i ? this.onInput(i) : this.onCloseDropdown();
341
+ }
342
+ commitChunk(e) {
343
+ if (this.isEnforceSuggestions()) {
344
+ let t = e.toLowerCase(), n = this.getLoadedSuggestions().find((e) => d(e).toLowerCase() === t);
345
+ if (!n) {
346
+ this.onCommitRejected(e);
347
+ return;
348
+ }
349
+ this.onCommit(f(n));
350
+ } else this.onCommit(e);
351
+ }
352
+ };
353
+ function x(e) {
354
+ return e.replace(/[\\\]^-]/g, (e) => `\\${e}`);
355
+ }
356
+ //#endregion
357
+ //#region src/core/keyboard.ts
358
+ var S = class {
359
+ constructor(e, t) {
360
+ this.input = e, this.onCommit = t.onCommit, this.onCommitRejected = t.onCommitRejected, this.onRemoveLast = t.onRemoveLast, this.clearInput = t.clearInput, this.hasChips = t.hasChips, this.getDropdown = t.getDropdown, this.getDelimiters = t.getDelimiters, this.isEnforceSuggestions = t.isEnforceSuggestions, this.getSuggestionSource = t.getSuggestionSource, this.getLoadedSuggestions = t.getLoadedSuggestions, this.handleKeydown = (e) => this.routeKey(e), this.input.addEventListener("keydown", this.handleKeydown);
361
+ }
362
+ routeKey(e) {
363
+ let t = this.getDropdown(), n = this.getDelimiters();
364
+ switch (e.key) {
365
+ case "ArrowDown":
366
+ e.preventDefault(), this.handleArrowDown(t);
367
+ break;
368
+ case "ArrowUp":
369
+ e.preventDefault(), this.handleArrowUp(t);
370
+ break;
371
+ case "Home":
372
+ t.isOpen && (e.preventDefault(), t.highlightHome());
373
+ break;
374
+ case "End":
375
+ t.isOpen && (e.preventDefault(), t.highlightEnd());
376
+ break;
377
+ case "Enter":
378
+ this.handleEnter(t, e);
379
+ break;
380
+ case "Escape":
381
+ t.isOpen && (e.preventDefault(), t.close());
382
+ break;
383
+ case "Tab":
384
+ this.handleTab(t);
385
+ break;
386
+ case "Backspace":
387
+ this.handleBackspace(e);
388
+ break;
389
+ default: e.key !== "Enter" && e.key !== "Tab" && n.includes(e.key) && this.input.value && (e.preventDefault(), this.commit(this.input.value));
390
+ }
391
+ }
392
+ handleArrowDown(e) {
393
+ if (e.isOpen) {
394
+ e.highlightNext();
395
+ return;
396
+ }
397
+ let t = this.getSuggestionSource(), n = !this.input.value;
398
+ n && t && "query" in t || (n && t && e.filter(""), e.isOpen || e.open(), e.highlightFirst());
399
+ }
400
+ handleArrowUp(e) {
401
+ e.isOpen && e.highlightPrev();
402
+ }
403
+ handleEnter(e, t) {
404
+ if (!e.isOpen) {
405
+ t.preventDefault(), this.input.value && this.commit(this.input.value);
406
+ return;
407
+ }
408
+ t.preventDefault();
409
+ let n = e.selectActive();
410
+ n === null ? (e.close(), this.input.value && this.commit(this.input.value)) : (e.close(), this.onCommit(f(n)), this.clearInput());
411
+ }
412
+ handleTab(e) {
413
+ this.getDelimiters().includes("Tab") && this.input.value && this.commit(this.input.value), e.close();
414
+ }
415
+ handleBackspace(e) {
416
+ this.input.value || this.hasChips() && (e.preventDefault(), this.onRemoveLast());
417
+ }
418
+ commit(e) {
419
+ if (this.isEnforceSuggestions()) {
420
+ let t = e.toLowerCase(), n = this.getLoadedSuggestions().find((e) => d(e).toLowerCase() === t);
421
+ if (!n) {
422
+ this.onCommitRejected(e);
423
+ return;
424
+ }
425
+ this.onCommit(f(n));
426
+ } else this.onCommit(e);
427
+ this.clearInput(), this.getDropdown().close();
428
+ }
429
+ destroy() {
430
+ this.input.removeEventListener("keydown", this.handleKeydown);
431
+ }
432
+ };
433
+ //#endregion
434
+ //#region src/dom/chips.ts
435
+ function C() {
436
+ let e = document.createElement("div");
437
+ return e.className = "taga11y__tags", e;
438
+ }
439
+ function w(e, t) {
440
+ let n = document.createElement("ul");
441
+ return n.className = "taga11y__tag-list", n.setAttribute("aria-label", a(e, t, "a11y.selectedTags")), n;
442
+ }
443
+ function T(e, t, n, r, i) {
444
+ let o = document.createElement("li");
445
+ o.className = "taga11y__tag", o.dataset.tagId = e.id;
446
+ let s = document.createElement("span");
447
+ s.className = "taga11y__tag-label", s.textContent = e.label;
448
+ let c = document.createElement("button");
449
+ return c.className = "taga11y__tag-remove", c.type = "button", c.setAttribute("aria-label", a(n, r, "a11y.removeTag", { label: e.label })), c.textContent = "Ă—", c.addEventListener("click", () => {
450
+ t(e.id), i?.();
451
+ }), o.appendChild(s), o.appendChild(c), o;
452
+ }
453
+ function E(e) {
454
+ let t = document.createElement("li");
455
+ t.className = "taga11y__tag taga11y__tag--error";
456
+ let n = document.createElement("span");
457
+ return n.className = "taga11y__tag-label", n.textContent = e, t.appendChild(n), t;
458
+ }
459
+ function D(e, t, n, r, i, a, o) {
460
+ Array.from(e.children).forEach((e) => e.remove());
461
+ for (let a of t) e.appendChild(T(a, n, r, i, o));
462
+ a && e.appendChild(a);
463
+ }
464
+ //#endregion
465
+ //#region src/dom/render.ts
466
+ function O(e, t) {
467
+ let n = document.createElement("li");
468
+ n.className = "taga11y__loading", n.setAttribute("aria-hidden", "true"), n.hidden = !0;
469
+ let r = document.createElement("span");
470
+ r.className = "taga11y__loading-spinner";
471
+ let i = document.createElement("span");
472
+ return i.className = "taga11y__loading-text", i.textContent = a(e, t, "ui.loading"), n.appendChild(r), n.appendChild(i), n;
473
+ }
474
+ function k(e) {
475
+ let t = document.createElement("div");
476
+ return t.className = "taga11y__error", N() || t.setAttribute("role", "alert"), t.id = e.error, t.hidden = !0, t;
477
+ }
478
+ function A(e) {
479
+ let t = document.createElement("div");
480
+ return t.className = "taga11y__announcer", t.setAttribute("aria-live", "polite"), t.setAttribute("aria-atomic", "true"), t.id = e.announcer, t;
481
+ }
482
+ function j(e, t) {
483
+ e.id ||= t.base, e.classList.add("taga11y__input"), e.setAttribute("role", "combobox"), e.setAttribute("aria-autocomplete", "list"), e.setAttribute("aria-haspopup", "listbox"), e.setAttribute("aria-expanded", "false"), e.setAttribute("aria-controls", t.listbox), e.removeAttribute("aria-activedescendant"), e.setAttribute("autocomplete", "off"), e.removeAttribute("name");
484
+ }
485
+ function M(e, t) {
486
+ let n = document.createElement("input");
487
+ return n.type = "hidden", n.className = "taga11y__hidden", n.name = e, n.value = t, n;
488
+ }
489
+ function N() {
490
+ return typeof document.ariaNotify == "function";
491
+ }
492
+ function P(e, t, n = "normal") {
493
+ if (N()) {
494
+ document.ariaNotify(t, { priority: n });
495
+ return;
496
+ }
497
+ e.textContent = "", setTimeout(() => {
498
+ e.textContent = t;
499
+ }, 0);
500
+ }
501
+ function F(e, t, n) {
502
+ let r = document.createElement("div");
503
+ if (r.className = "taga11y", n.label) {
504
+ let t = document.createElement("label");
505
+ t.className = "taga11y__label", t.htmlFor = e.id, t.textContent = n.label, r.appendChild(t);
506
+ }
507
+ let i = C(), a = w(n.strings, n.locale);
508
+ i.appendChild(a), i.appendChild(e), r.appendChild(i);
509
+ let o = g(t), s = O(n.strings, n.locale);
510
+ o.appendChild(s), r.appendChild(o);
511
+ let c = k(t);
512
+ r.appendChild(c);
513
+ let l = A(t);
514
+ r.appendChild(l);
515
+ let u = M(n.name, n.initialValue);
516
+ return r.appendChild(u), {
517
+ wrapper: r,
518
+ tagsContainer: i,
519
+ tagList: a,
520
+ listbox: o,
521
+ loading: s,
522
+ error: c,
523
+ announcer: l,
524
+ hiddenInput: u
525
+ };
526
+ }
527
+ //#endregion
528
+ //#region src/a11y.ts
529
+ function I(e, t, n) {
530
+ e.textContent = t, e.hidden = !1, N() && P(e, t, "high"), setTimeout(() => {
531
+ L(e), n?.remove();
532
+ }, 3e3);
533
+ }
534
+ function L(e) {
535
+ e.textContent = "", e.hidden = !0;
536
+ }
537
+ function R(e, t, n, r) {
538
+ P(e, a(t, n, "a11y.tagAdded", { label: r }));
539
+ }
540
+ function z(e, t, n, r) {
541
+ P(e, a(t, n, "a11y.tagRemoved", { label: r }));
542
+ }
543
+ function B(e, t, n) {
544
+ P(e, a(t, n, "a11y.tagsCleared"));
545
+ }
546
+ function V(e, t, n, r) {
547
+ if (r.length === 0) return;
548
+ let i = new Intl.ListFormat(n, {
549
+ type: "conjunction",
550
+ style: "long"
551
+ }).format(r.map((e) => e.label));
552
+ P(e, a(t, n, "a11y.tagsSummary", {
553
+ n: r.length,
554
+ labels: i
555
+ }));
556
+ }
557
+ function H(e, t, n, r, i, o) {
558
+ I(e, r === "duplicate" ? a(t, n, "error.duplicate", { label: i }) : r === "max-reached" ? a(t, n, "error.maxReached") : a(t, n, "error.notInList"), o);
559
+ }
560
+ //#endregion
561
+ //#region src/index.ts
562
+ function U(e) {
563
+ if (e == null) return null;
564
+ if (e === "dark" || e === "light") return e;
565
+ throw TypeError(`taga11y: theme must be "dark", "light", or null — received "${e}"`);
566
+ }
567
+ var W = class {
568
+ constructor(e, t = {}) {
569
+ this._initialSnapshot = [], this._formResetHandler = null, this._input = e, this._originalName = e.name, this._originalId = e.id, this._originalValue = e.value, this._originalParent = e.parentNode, this._originalNextSibling = e.nextSibling, this._ids = u(l(e)), this._opts = this._parseOptions(t), this._strings = n(this._opts.i18n?.strings), this._locale = this._opts.i18n?.locale ?? "en", this.init();
570
+ }
571
+ init() {
572
+ this._initialSnapshot = this._opts.deserialize(this._input.value), this._input.value = "", this._input.defaultValue = "", j(this._input, this._ids), this._dom = F(this._input, this._ids, {
573
+ ...this._opts.label === void 0 ? {} : { label: this._opts.label },
574
+ name: this._opts.name,
575
+ initialValue: "",
576
+ strings: this._strings,
577
+ locale: this._locale
578
+ }), this._opts.theme && this._dom.wrapper.setAttribute("data-theme", this._opts.theme), this._opts.disabled && (this._dom.wrapper.classList.add("taga11y--disabled"), this._input.disabled = !0, this._dom.hiddenInput.disabled = !0), this._originalParent && this._originalParent.insertBefore(this._dom.wrapper, this._originalNextSibling), this._opts.i18n && s(this._dom.wrapper, this._opts.i18n.locale, this._opts.i18n.dir), this._selection = new m(this._ids.base, this._opts.maxTags), this._dropdown = new y({
579
+ listbox: this._dom.listbox,
580
+ input: this._input,
581
+ loading: this._dom.loading,
582
+ wrapper: this._dom.wrapper
583
+ }, {
584
+ ...this._opts.suggestions === void 0 ? {} : { suggestions: this._opts.suggestions },
585
+ debounceMs: this._opts.debounceMs,
586
+ maxSuggestions: this._opts.maxSuggestions,
587
+ getSelectedValues: () => this._selection.getTags().map((e) => e.value),
588
+ ids: this._ids,
589
+ strings: this._strings,
590
+ locale: this._locale,
591
+ onOptionSelect: (e) => {
592
+ this._dropdown.close(), this._handleCommit(f(e)), this._inputMgr.clearValue();
593
+ },
594
+ onAnnounce: (e) => P(this._dom.announcer, e),
595
+ onLoadError: (e) => I(this._dom.error, e)
596
+ }), this._inputMgr = new b(this._input, {
597
+ onInput: (e) => this._dropdown.filter(e),
598
+ onBlurCommit: (e) => this._handleBlurCommit(e),
599
+ onBlurClose: () => this._dropdown.close(),
600
+ onFocus: () => this._announceTagsSummary(),
601
+ getDelimiters: () => this._opts.delimiter,
602
+ isEnforceSuggestions: () => this._opts.enforceSuggestions,
603
+ getLoadedSuggestions: () => this._dropdown.getAllSuggestions(),
604
+ onCommit: (e) => this._handleCommit(e),
605
+ onCommitRejected: (e) => this._showRejection(e, "not-in-list"),
606
+ onPaste: (e) => this._handlePaste(e),
607
+ onCloseDropdown: () => this._dropdown.close()
608
+ }), this._keyboard = new S(this._input, {
609
+ onCommit: (e) => this._handleCommit(e),
610
+ onCommitRejected: (e) => this._showRejection(e, "not-in-list"),
611
+ onRemoveLast: () => this._handleRemoveLast(),
612
+ clearInput: () => this._inputMgr.clearValue(),
613
+ hasChips: () => this._selection.getTags().length > 0,
614
+ getDropdown: () => this._dropdown,
615
+ getDelimiters: () => this._opts.delimiter,
616
+ isEnforceSuggestions: () => this._opts.enforceSuggestions,
617
+ getSuggestionSource: () => this._opts.suggestions,
618
+ getLoadedSuggestions: () => this._dropdown.getAllSuggestions()
619
+ });
620
+ for (let e of this._initialSnapshot) {
621
+ let t = f(e), n = typeof e == "string" ? this._resolveLabelFromAll(t) : d(e);
622
+ this._selection.addTag(t, n);
623
+ }
624
+ this._syncChips(), this._syncHiddenInput();
625
+ let e = this._input.closest("form");
626
+ e && (this._formResetHandler = () => this._handleFormReset(), e.addEventListener("reset", this._formResetHandler));
627
+ }
628
+ destroy() {
629
+ this._keyboard.destroy(), this._inputMgr.destroy(), this._dropdown.destroy();
630
+ let e = this._input.closest("form");
631
+ e && this._formResetHandler && (e.removeEventListener("reset", this._formResetHandler), this._formResetHandler = null), this._originalParent && this._originalParent.insertBefore(this._input, this._originalNextSibling), this._originalName && this._input.setAttribute("name", this._originalName), this._originalId || this._input.removeAttribute("id"), this._input.value = this._originalValue, this._input.defaultValue = this._originalValue, this._input.removeAttribute("role"), this._input.removeAttribute("aria-autocomplete"), this._input.removeAttribute("aria-haspopup"), this._input.removeAttribute("aria-expanded"), this._input.removeAttribute("aria-controls"), this._input.removeAttribute("aria-activedescendant"), this._input.disabled = !1, this._dom.wrapper.remove(), this.emit("taga11y:destroy", {});
632
+ }
633
+ addTag(e) {
634
+ let t = this._resolveLabelFromAll(e), n = this._selection.addTag(e, t);
635
+ return n.ok ? (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:add", { tag: n.tag }), this.emit("taga11y:change", { tags: this._selection.getTags() }), R(this._dom.announcer, this._strings, this._locale, n.tag.label), !0) : (this._showRejection(e, n.reason), !1);
636
+ }
637
+ removeTag(e) {
638
+ let t = this._selection.getTags().find((t) => t.value === e);
639
+ if (!t) return null;
640
+ let n = this._selection.removeTag(t.id);
641
+ return n ? (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:remove", { tag: n }), this.emit("taga11y:change", { tags: this._selection.getTags() }), z(this._dom.announcer, this._strings, this._locale, n.label), this._input.focus(), n) : null;
642
+ }
643
+ clearTags() {
644
+ let e = this._selection.clearTags();
645
+ this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:clear", { tags: e }), this.emit("taga11y:change", { tags: [] }), B(this._dom.announcer, this._strings, this._locale);
646
+ }
647
+ getTags() {
648
+ return this._selection.getTags();
649
+ }
650
+ addTags(e) {
651
+ let t = !1;
652
+ for (let n of e) {
653
+ let e = f(n), r = this._resolveLabelFromAll(e) || d(n), i = this._selection.addTag(e, r);
654
+ i.ok && (t = !0, this.emit("taga11y:add", { tag: i.tag }));
655
+ }
656
+ t && (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:change", { tags: this._selection.getTags() }));
657
+ }
658
+ setTags(e) {
659
+ let t = this._selection.clearTags();
660
+ this.emit("taga11y:clear", { tags: t });
661
+ for (let t of e) {
662
+ let e = f(t), n = this._resolveLabelFromAll(e) || d(t), r = this._selection.addTag(e, n);
663
+ r.ok && this.emit("taga11y:add", { tag: r.tag });
664
+ }
665
+ this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:change", { tags: this._selection.getTags() });
666
+ }
667
+ isTagged(e) {
668
+ return this._selection.isTagged(e);
669
+ }
670
+ open() {
671
+ this._opts.disabled || this._dropdown.isOpen || this._dropdown.open();
672
+ }
673
+ close() {
674
+ this._dropdown.isOpen && this._dropdown.close();
675
+ }
676
+ focus() {
677
+ this._input.focus();
678
+ }
679
+ blur() {
680
+ this._input.blur();
681
+ }
682
+ settings(e) {
683
+ if ("i18n" in e && console.warn("taga11y: i18n is init-only and cannot be changed via settings(); use destroy() + new Taga11y() to switch locale. Ignoring i18n; other provided keys are still applied."), "disabled" in e) {
684
+ let t = e.disabled ?? !1;
685
+ this._opts.disabled = t, this._input.disabled = t, this._dom.hiddenInput.disabled = t, this._dom.tagList.querySelectorAll(".taga11y__tag-remove").forEach((e) => {
686
+ e.hidden = t;
687
+ }), this._dom.wrapper.classList.toggle("taga11y--disabled", t);
688
+ }
689
+ if ("theme" in e) {
690
+ let t = U(e.theme);
691
+ this._opts.theme = t, t ? this._dom.wrapper.setAttribute("data-theme", t) : this._dom.wrapper.removeAttribute("data-theme");
692
+ }
693
+ if ("maxTags" in e && (this._opts.maxTags = e.maxTags, this._selection.setMaxTags(e.maxTags)), "maxSuggestions" in e && (this._opts.maxSuggestions = Math.max(0, e.maxSuggestions ?? 10), this._dropdown.setMaxSuggestions(this._opts.maxSuggestions)), "delimiter" in e) {
694
+ let t = e.delimiter;
695
+ this._opts.delimiter = Array.isArray(t) ? t : t ? [t] : [",", "Enter"];
696
+ }
697
+ if ("enforceSuggestions" in e && (this._opts.enforceSuggestions = e.enforceSuggestions ?? !1), "label" in e) {
698
+ let t = e.label ?? null, n = this._dom.wrapper.querySelector(".taga11y__label");
699
+ t == null ? n?.remove() : (n || (n = document.createElement("label"), n.className = "taga11y__label", n.htmlFor = this._input.id, this._dom.wrapper.insertBefore(n, this._dom.wrapper.firstChild)), n.textContent = t), this._opts.label = t ?? void 0;
700
+ }
701
+ if ("suggestions" in e && (this._opts.suggestions = e.suggestions, this._dropdown.setSource(e.suggestions, this._opts.debounceMs)), "debounceMs" in e && (this._opts.debounceMs = Math.max(0, e.debounceMs ?? 200), this._dropdown.setDebounceMs(this._opts.debounceMs)), "serialize" in e && e.serialize && (this._opts.serialize = e.serialize, this._syncHiddenInput()), "deserialize" in e && e.deserialize && (this._opts.deserialize = e.deserialize), "name" in e) {
702
+ let t = e.name ?? "";
703
+ this._opts.name = t, this._dom.hiddenInput.name = t;
704
+ }
705
+ }
706
+ on(e, t) {
707
+ this._input.addEventListener(e, t);
708
+ }
709
+ off(e, t) {
710
+ this._input.removeEventListener(e, t);
711
+ }
712
+ emit(e, t) {
713
+ this._input.dispatchEvent(new CustomEvent(e, {
714
+ detail: t,
715
+ bubbles: !0,
716
+ cancelable: !1
717
+ }));
718
+ }
719
+ get tags() {
720
+ return this._selection.getTags();
721
+ }
722
+ get isOpen() {
723
+ return this._dropdown.isOpen;
724
+ }
725
+ get maxTagsReached() {
726
+ return this._opts.maxTags === void 0 ? !1 : this._selection.getTags().length >= this._opts.maxTags;
727
+ }
728
+ _parseOptions(e) {
729
+ if (e.suggestions && !Array.isArray(e.suggestions)) {
730
+ let t = e.suggestions;
731
+ if ("once" in t && "query" in t) throw Error("suggestions source cannot define both 'once' and 'query'");
732
+ }
733
+ let t = e.delimiter;
734
+ return {
735
+ suggestions: e.suggestions,
736
+ maxTags: e.maxTags,
737
+ maxSuggestions: Math.max(0, e.maxSuggestions ?? 10),
738
+ delimiter: Array.isArray(t) ? t : t ? [t] : [",", "Enter"],
739
+ enforceSuggestions: e.enforceSuggestions ?? !1,
740
+ name: e.name ?? this._originalName,
741
+ label: e.label,
742
+ disabled: e.disabled ?? !1,
743
+ theme: U(e.theme),
744
+ serialize: e.serialize ?? ((e) => e.map((e) => e.value).join(",")),
745
+ deserialize: e.deserialize ?? ((e) => e ? e.split(",").map((e) => e.trim()).filter(Boolean) : []),
746
+ debounceMs: Math.max(0, e.debounceMs ?? 200),
747
+ i18n: e.i18n
748
+ };
749
+ }
750
+ _handleCommit(e) {
751
+ let t = this._resolveLabelFromAll(e), n = this._selection.addTag(e, t);
752
+ n.ok ? (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:add", { tag: n.tag }), this.emit("taga11y:change", { tags: this._selection.getTags() }), R(this._dom.announcer, this._strings, this._locale, n.tag.label)) : this._showRejection(e, n.reason);
753
+ }
754
+ _handlePaste(e) {
755
+ let t = [], n = [];
756
+ for (let r of e) {
757
+ let e, i;
758
+ if (this._opts.enforceSuggestions) {
759
+ let t = r.toLowerCase(), a = this._dropdown.getAllSuggestions().find((e) => d(e).toLowerCase() === t);
760
+ if (!a) {
761
+ n.push(r);
762
+ continue;
763
+ }
764
+ e = f(a), i = d(a);
765
+ } else e = r, i = r;
766
+ let a = this._selection.addTag(e, i);
767
+ a.ok ? (t.push(a.tag), this.emit("taga11y:add", { tag: a.tag })) : n.push(r);
768
+ }
769
+ t.length > 0 && (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:change", { tags: this._selection.getTags() })), this.emit("taga11y:paste", {
770
+ added: t,
771
+ skipped: n
772
+ });
773
+ }
774
+ _handleBlurCommit(e) {
775
+ if (this._opts.enforceSuggestions) {
776
+ let t = e.toLowerCase(), n = this._dropdown.getAllSuggestions().find((e) => d(e).toLowerCase() === t);
777
+ if (!n) {
778
+ this._showRejection(e, "not-in-list"), this._inputMgr.clearValue();
779
+ return;
780
+ }
781
+ let r = this._selection.addTag(f(n), d(n));
782
+ r.ok ? (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:add", { tag: r.tag }), this.emit("taga11y:change", { tags: this._selection.getTags() }), R(this._dom.announcer, this._strings, this._locale, r.tag.label)) : this._showRejection(e, r.reason);
783
+ } else {
784
+ let t = this._selection.addTag(e, e);
785
+ t.ok ? (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:add", { tag: t.tag }), this.emit("taga11y:change", { tags: this._selection.getTags() }), R(this._dom.announcer, this._strings, this._locale, t.tag.label)) : this._showRejection(e, t.reason);
786
+ }
787
+ this._inputMgr.clearValue();
788
+ }
789
+ _handleRemove(e) {
790
+ let t = this._selection.removeTag(e);
791
+ t && (this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:remove", { tag: t }), this.emit("taga11y:change", { tags: this._selection.getTags() }), z(this._dom.announcer, this._strings, this._locale, t.label));
792
+ }
793
+ _handleRemoveLast() {
794
+ let e = this._selection.getTags();
795
+ e.length !== 0 && this._handleRemove(e[e.length - 1].id);
796
+ }
797
+ _handleFormReset() {
798
+ let e = this._selection.clearTags();
799
+ this.emit("taga11y:clear", { tags: e });
800
+ for (let e of this._initialSnapshot) {
801
+ let t = f(e), n = typeof e == "string" ? this._resolveLabelFromAll(t) : d(e), r = this._selection.addTag(t, n);
802
+ r.ok && this.emit("taga11y:add", { tag: r.tag });
803
+ }
804
+ this._syncChips(), this._syncHiddenInput(), this.emit("taga11y:change", { tags: this._selection.getTags() });
805
+ }
806
+ _showRejection(e, t) {
807
+ this._dom.tagList.querySelector(".taga11y__tag--error")?.remove();
808
+ let n = E(e);
809
+ this._dom.tagList.appendChild(n), H(this._dom.error, this._strings, this._locale, t, e, n);
810
+ }
811
+ _syncChips() {
812
+ let e = this._dom.tagList.querySelector(".taga11y__tag--error") ?? void 0;
813
+ D(this._dom.tagList, this._selection.getTags(), (e) => this._handleRemove(e), this._strings, this._locale, e, () => this._input.focus()), this._opts.disabled && this._dom.tagList.querySelectorAll(".taga11y__tag-remove").forEach((e) => {
814
+ e.hidden = !0;
815
+ });
816
+ }
817
+ _announceTagsSummary() {
818
+ V(this._dom.announcer, this._strings, this._locale, this._selection.getTags());
819
+ }
820
+ _syncHiddenInput() {
821
+ this._dom.hiddenInput.value = this._opts.serialize(this._selection.getTags());
822
+ }
823
+ _resolveLabelFromAll(e) {
824
+ let t = this._dropdown.getAllSuggestions().find((t) => f(t) === e);
825
+ return t ? d(t) : e;
826
+ }
827
+ };
828
+ //#endregion
829
+ export { W as Taga11y };