@y14e/accordion 1.4.13 → 1.4.15
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 +4 -4
- package/dist/index.bundle.cjs +292 -0
- package/dist/index.bundle.js +286 -0
- package/dist/index.cjs +16 -636
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +6 -630
- package/package.json +6 -3
package/dist/index.cjs
CHANGED
|
@@ -1,580 +1,12 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var Button =
|
|
5
|
-
|
|
6
|
-
#controller = null;
|
|
7
|
-
#isDestroyed = false;
|
|
8
|
-
constructor(element) {
|
|
9
|
-
if (!(element instanceof HTMLElement)) {
|
|
10
|
-
throw new TypeError("Invalid element");
|
|
11
|
-
}
|
|
12
|
-
if (element.hasAttribute("data-button-initialized")) {
|
|
13
|
-
console.warn("Already initialized");
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
this.#element = element;
|
|
17
|
-
this.#initialize();
|
|
18
|
-
}
|
|
19
|
-
destroy() {
|
|
20
|
-
if (this.#isDestroyed) {
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
this.#isDestroyed = true;
|
|
24
|
-
this.#controller?.abort();
|
|
25
|
-
this.#controller = null;
|
|
26
|
-
this.#element.removeAttribute("data-button-initialized");
|
|
27
|
-
}
|
|
28
|
-
#initialize() {
|
|
29
|
-
this.#controller = new AbortController();
|
|
30
|
-
this.#element.addEventListener("keydown", this.#onKeyDown, {
|
|
31
|
-
signal: this.#controller.signal
|
|
32
|
-
});
|
|
33
|
-
this.#element.setAttribute("data-button-initialized", "");
|
|
34
|
-
}
|
|
35
|
-
#onKeyDown = (event) => {
|
|
36
|
-
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
37
|
-
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
38
|
-
return;
|
|
39
|
-
}
|
|
40
|
-
if (!["Enter", " "].includes(key)) {
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const active = getActiveElement();
|
|
44
|
-
if (!(active instanceof HTMLElement)) {
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
event.preventDefault();
|
|
48
|
-
active.click();
|
|
49
|
-
};
|
|
50
|
-
};
|
|
51
|
-
function getActiveElement() {
|
|
52
|
-
let current = document.activeElement;
|
|
53
|
-
while (current?.shadowRoot?.activeElement) {
|
|
54
|
-
current = current.shadowRoot.activeElement;
|
|
55
|
-
}
|
|
56
|
-
return current;
|
|
57
|
-
}
|
|
3
|
+
var attributesUtils = require('@y14e/attributes-utils');
|
|
4
|
+
var Button = require('@y14e/button');
|
|
5
|
+
var rovingTabindex = require('@y14e/roving-tabindex');
|
|
58
6
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
var
|
|
62
|
-
function addTokenToAttribute(element, attribute, token, options = {}) {
|
|
63
|
-
const {
|
|
64
|
-
caseInsensitive = false,
|
|
65
|
-
parse = DEFAULT_PARSER,
|
|
66
|
-
serialize = DEFAULT_SERIALIZER
|
|
67
|
-
} = options;
|
|
68
|
-
const value = element.getAttribute(attribute)?.trim();
|
|
69
|
-
const tokens = value ? parse(value).filter(Boolean) : [];
|
|
70
|
-
if (caseInsensitive) {
|
|
71
|
-
const lower = token.toLowerCase();
|
|
72
|
-
if (tokens.every((token2) => token2.toLowerCase() !== lower)) {
|
|
73
|
-
tokens.push(token);
|
|
74
|
-
element.setAttribute(attribute, serialize(tokens));
|
|
75
|
-
}
|
|
76
|
-
} else {
|
|
77
|
-
const set = new Set(tokens);
|
|
78
|
-
set.add(token);
|
|
79
|
-
element.setAttribute(attribute, serialize([...set]));
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
83
|
-
function restoreAttributes(elements) {
|
|
84
|
-
for (const element of elements) {
|
|
85
|
-
const snapshot = snapshots.get(element);
|
|
86
|
-
if (!snapshot) {
|
|
87
|
-
continue;
|
|
88
|
-
}
|
|
89
|
-
for (const [attribute, value] of snapshot.entries()) {
|
|
90
|
-
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
91
|
-
}
|
|
92
|
-
snapshots.delete(element);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
function saveAttributes(elements, attributes) {
|
|
96
|
-
elements.forEach((element) => {
|
|
97
|
-
let snapshot = snapshots.get(element);
|
|
98
|
-
if (!snapshot) {
|
|
99
|
-
snapshot = /* @__PURE__ */ new Map();
|
|
100
|
-
snapshots.set(element, snapshot);
|
|
101
|
-
}
|
|
102
|
-
attributes.forEach((attribute) => {
|
|
103
|
-
snapshot.set(attribute, element.getAttribute(attribute));
|
|
104
|
-
});
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
var FOCUSABLE_SELECTOR = `:is(a[href], area[href], button, embed, iframe, input:not([type="hidden" i]), object, select, details > summary:first-of-type, textarea, [contenteditable]:not([contenteditable="false" i]), [controls], [tabindex]):not(:disabled, [hidden], [inert], [tabindex="-1"])`;
|
|
108
|
-
function getFocusables(container = document.body, options = {}) {
|
|
109
|
-
if (!(container instanceof Element)) {
|
|
110
|
-
console.warn("Invalid container element. Fallback: <body> element.");
|
|
111
|
-
container = document.body;
|
|
112
|
-
}
|
|
113
|
-
let {
|
|
114
|
-
composed = false,
|
|
115
|
-
filter,
|
|
116
|
-
include,
|
|
117
|
-
skipNegativeTabIndexCheck = false,
|
|
118
|
-
skipVisibilityCheck = false
|
|
119
|
-
} = options;
|
|
120
|
-
if (typeof composed !== "boolean") {
|
|
121
|
-
console.warn("Invalid composed option. Fallback: false.");
|
|
122
|
-
composed = false;
|
|
123
|
-
}
|
|
124
|
-
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
125
|
-
console.warn(
|
|
126
|
-
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
127
|
-
);
|
|
128
|
-
filter = void 0;
|
|
129
|
-
}
|
|
130
|
-
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
131
|
-
console.warn(
|
|
132
|
-
"Invalid include function. Fallback: no include function (undefined)."
|
|
133
|
-
);
|
|
134
|
-
include = void 0;
|
|
135
|
-
}
|
|
136
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
137
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
138
|
-
skipNegativeTabIndexCheck = false;
|
|
139
|
-
}
|
|
140
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
141
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
142
|
-
skipVisibilityCheck = false;
|
|
143
|
-
}
|
|
144
|
-
const elements = [];
|
|
145
|
-
if (composed || include) {
|
|
146
|
-
let traverse2 = function(node) {
|
|
147
|
-
if (!(node instanceof Element)) {
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
if (isFocusable(node, { skipNegativeTabIndexCheck, skipVisibilityCheck }) || include?.(node)) {
|
|
151
|
-
elements[elements.length] = node;
|
|
152
|
-
}
|
|
153
|
-
const children = getComposedChildren(node);
|
|
154
|
-
for (let i = 0, l = children.length; i < l; i++) {
|
|
155
|
-
const child = children[i];
|
|
156
|
-
child && traverse2(child);
|
|
157
|
-
}
|
|
158
|
-
};
|
|
159
|
-
traverse2(container);
|
|
160
|
-
} else {
|
|
161
|
-
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
162
|
-
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
163
|
-
const candidate = candidates[i];
|
|
164
|
-
if (candidate && isFocusable(candidate, {
|
|
165
|
-
skipNegativeTabIndexCheck,
|
|
166
|
-
skipVisibilityCheck
|
|
167
|
-
})) {
|
|
168
|
-
elements[elements.length] = candidate;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
173
|
-
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
174
|
-
}
|
|
175
|
-
function isFocusable(element, options = {}) {
|
|
176
|
-
if (!(element instanceof Element)) {
|
|
177
|
-
console.warn("Invalid element");
|
|
178
|
-
return false;
|
|
179
|
-
}
|
|
180
|
-
let { skipNegativeTabIndexCheck = false, skipVisibilityCheck = false } = options;
|
|
181
|
-
if (typeof skipNegativeTabIndexCheck !== "boolean") {
|
|
182
|
-
console.warn("Invalid skipNegativeTabIndexCheck option. Fallback: false.");
|
|
183
|
-
skipNegativeTabIndexCheck = false;
|
|
184
|
-
}
|
|
185
|
-
if (typeof skipVisibilityCheck !== "boolean") {
|
|
186
|
-
console.warn("Invalid skipVisibilityCheck option. Fallback: false.");
|
|
187
|
-
skipVisibilityCheck = false;
|
|
188
|
-
}
|
|
189
|
-
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
190
|
-
return false;
|
|
191
|
-
}
|
|
192
|
-
if (!skipNegativeTabIndexCheck && getTabIndex(element) < 0) {
|
|
193
|
-
return false;
|
|
194
|
-
}
|
|
195
|
-
if (!element.matches(
|
|
196
|
-
skipNegativeTabIndexCheck ? FOCUSABLE_SELECTOR.replace(/(,\s*)?\[tabindex="-1"\]/g, "") : FOCUSABLE_SELECTOR
|
|
197
|
-
)) {
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
if (isDisabledDeep(element)) {
|
|
201
|
-
return false;
|
|
202
|
-
}
|
|
203
|
-
if (!skipVisibilityCheck && !element.checkVisibility({
|
|
204
|
-
contentVisibilityAuto: true,
|
|
205
|
-
opacityProperty: true,
|
|
206
|
-
visibilityProperty: true
|
|
207
|
-
})) {
|
|
208
|
-
return false;
|
|
209
|
-
}
|
|
210
|
-
return true;
|
|
211
|
-
}
|
|
212
|
-
function isDisabledDeep(element) {
|
|
213
|
-
let current = element;
|
|
214
|
-
while (current) {
|
|
215
|
-
if (current instanceof ShadowRoot) {
|
|
216
|
-
if (current.mode !== "open") {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
current = current.host;
|
|
220
|
-
continue;
|
|
221
|
-
}
|
|
222
|
-
if (!(current instanceof Element)) {
|
|
223
|
-
current = current.parentNode;
|
|
224
|
-
continue;
|
|
225
|
-
}
|
|
226
|
-
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
227
|
-
return true;
|
|
228
|
-
}
|
|
229
|
-
if (isInert(current)) {
|
|
230
|
-
return true;
|
|
231
|
-
}
|
|
232
|
-
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
233
|
-
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
234
|
-
return true;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
current = current.parentNode;
|
|
238
|
-
}
|
|
239
|
-
return false;
|
|
240
|
-
}
|
|
241
|
-
function normalizeRadioGroup(elements) {
|
|
242
|
-
let map = null;
|
|
243
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
244
|
-
const element = elements[i];
|
|
245
|
-
if (!(element instanceof HTMLInputElement)) {
|
|
246
|
-
continue;
|
|
247
|
-
}
|
|
248
|
-
if (!isUngroupedRadio(element)) {
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
if (!map) {
|
|
252
|
-
map = /* @__PURE__ */ new Map();
|
|
253
|
-
}
|
|
254
|
-
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
255
|
-
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
256
|
-
if (group) {
|
|
257
|
-
group[group.length] = element;
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
if (!map) {
|
|
261
|
-
return elements;
|
|
262
|
-
}
|
|
263
|
-
const placeholder = /* @__PURE__ */ new Set();
|
|
264
|
-
for (const group of map.values()) {
|
|
265
|
-
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
266
|
-
}
|
|
267
|
-
return elements.filter(
|
|
268
|
-
(element) => isUngroupedRadio(element) ? placeholder.has(element) : true
|
|
269
|
-
);
|
|
270
|
-
}
|
|
271
|
-
function sortByTabIndex(elements) {
|
|
272
|
-
const ordered = [];
|
|
273
|
-
const natural = [];
|
|
274
|
-
for (let i = 0, l = elements.length; i < l; i++) {
|
|
275
|
-
const element = elements[i];
|
|
276
|
-
if (element) {
|
|
277
|
-
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
278
|
-
target[target.length] = element;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
282
|
-
let count = 0;
|
|
283
|
-
const sorted = new Array(ordered.length + natural.length);
|
|
284
|
-
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
285
|
-
sorted[count++] = ordered[i];
|
|
286
|
-
}
|
|
287
|
-
for (let i = 0, l = natural.length; i < l; i++) {
|
|
288
|
-
sorted[count++] = natural[i];
|
|
289
|
-
}
|
|
290
|
-
return sorted;
|
|
291
|
-
}
|
|
292
|
-
function getComposedChildren(node) {
|
|
293
|
-
if (node instanceof ShadowRoot) {
|
|
294
|
-
return getChildren(node);
|
|
295
|
-
}
|
|
296
|
-
if (!(node instanceof Element)) {
|
|
297
|
-
return [];
|
|
298
|
-
}
|
|
299
|
-
if (node instanceof HTMLSlotElement) {
|
|
300
|
-
const assigned = node.assignedElements({ flatten: true });
|
|
301
|
-
if (assigned.length) {
|
|
302
|
-
return assigned;
|
|
303
|
-
}
|
|
304
|
-
}
|
|
305
|
-
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
306
|
-
return getChildren(node.shadowRoot);
|
|
307
|
-
}
|
|
308
|
-
return getChildren(node);
|
|
309
|
-
}
|
|
310
|
-
function getChildren(node) {
|
|
311
|
-
const elements = [];
|
|
312
|
-
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
313
|
-
elements[elements.length] = child;
|
|
314
|
-
}
|
|
315
|
-
return elements;
|
|
316
|
-
}
|
|
317
|
-
function getTabIndex(element) {
|
|
318
|
-
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
319
|
-
}
|
|
320
|
-
function isDisabled(element) {
|
|
321
|
-
return "disabled" in element && !!element.disabled;
|
|
322
|
-
}
|
|
323
|
-
function isFormControl(element) {
|
|
324
|
-
const name = element.tagName;
|
|
325
|
-
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
326
|
-
}
|
|
327
|
-
function isInert(element) {
|
|
328
|
-
return "inert" in element && !!element.inert;
|
|
329
|
-
}
|
|
330
|
-
function isUngroupedRadio(element) {
|
|
331
|
-
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
332
|
-
}
|
|
333
|
-
function createRovingTabIndex(container, options = {}) {
|
|
334
|
-
if (!(container instanceof Element)) {
|
|
335
|
-
console.warn("Invalid container element");
|
|
336
|
-
return () => {
|
|
337
|
-
};
|
|
338
|
-
}
|
|
339
|
-
const roving = new RovingTabIndex(container, options);
|
|
340
|
-
return () => roving.destroy();
|
|
341
|
-
}
|
|
342
|
-
var RovingTabIndex = class {
|
|
343
|
-
#container;
|
|
344
|
-
#settings;
|
|
345
|
-
#focusables = /* @__PURE__ */ new Set();
|
|
346
|
-
#focusablesByFirstChar = /* @__PURE__ */ new Map();
|
|
347
|
-
#selectorFilter;
|
|
348
|
-
#controller = null;
|
|
349
|
-
#isDestroyed = false;
|
|
350
|
-
constructor(container, options = {}) {
|
|
351
|
-
this.#container = container;
|
|
352
|
-
let {
|
|
353
|
-
direction,
|
|
354
|
-
navigationOnly = false,
|
|
355
|
-
noMemory = false,
|
|
356
|
-
noStart = false,
|
|
357
|
-
selector,
|
|
358
|
-
typeahead = false,
|
|
359
|
-
wrap = false
|
|
360
|
-
} = options;
|
|
361
|
-
if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
|
|
362
|
-
console.warn("Invalid direction option. Fallback: both (undefined).");
|
|
363
|
-
direction = void 0;
|
|
364
|
-
}
|
|
365
|
-
if (typeof navigationOnly !== "boolean") {
|
|
366
|
-
console.warn("Invalid navigationOnly option. Fallback: false.");
|
|
367
|
-
navigationOnly = false;
|
|
368
|
-
}
|
|
369
|
-
if (typeof noMemory !== "boolean") {
|
|
370
|
-
console.warn("Invalid noMemory option. Fallback: false.");
|
|
371
|
-
noMemory = false;
|
|
372
|
-
}
|
|
373
|
-
if (typeof noStart !== "boolean") {
|
|
374
|
-
console.warn("Invalid noStart option. Fallback: false.");
|
|
375
|
-
noStart = false;
|
|
376
|
-
}
|
|
377
|
-
if (typeof selector !== "undefined" && (typeof selector !== "string" || !selector.trim())) {
|
|
378
|
-
console.warn(
|
|
379
|
-
"Invalid selector. Fallback: all focusable elements (undefined)."
|
|
380
|
-
);
|
|
381
|
-
selector = void 0;
|
|
382
|
-
}
|
|
383
|
-
if (typeof typeahead !== "boolean") {
|
|
384
|
-
console.warn("Invalid typeahead option. Fallback: false.");
|
|
385
|
-
typeahead = false;
|
|
386
|
-
}
|
|
387
|
-
if (typeof wrap !== "boolean") {
|
|
388
|
-
console.warn("Invalid wrap option. Fallback: false.");
|
|
389
|
-
wrap = false;
|
|
390
|
-
}
|
|
391
|
-
this.#settings = {
|
|
392
|
-
navigationOnly,
|
|
393
|
-
noMemory,
|
|
394
|
-
noStart,
|
|
395
|
-
typeahead,
|
|
396
|
-
wrap
|
|
397
|
-
};
|
|
398
|
-
direction && Object.assign(this.#settings, { direction });
|
|
399
|
-
selector && Object.assign(this.#settings, { selector });
|
|
400
|
-
this.#selectorFilter = this.#createSelectorFilter();
|
|
401
|
-
this.#initialize();
|
|
402
|
-
}
|
|
403
|
-
destroy() {
|
|
404
|
-
if (this.#isDestroyed) {
|
|
405
|
-
return;
|
|
406
|
-
}
|
|
407
|
-
this.#isDestroyed = true;
|
|
408
|
-
this.#controller?.abort();
|
|
409
|
-
this.#controller = null;
|
|
410
|
-
restoreAttributes([...this.#focusables]);
|
|
411
|
-
this.#focusables.clear();
|
|
412
|
-
this.#focusablesByFirstChar.clear();
|
|
413
|
-
this.#container.removeAttribute("data-roving-tabindex-initialized");
|
|
414
|
-
}
|
|
415
|
-
#initialize() {
|
|
416
|
-
this.#update(document.activeElement);
|
|
417
|
-
this.#controller = new AbortController();
|
|
418
|
-
const { signal } = this.#controller;
|
|
419
|
-
document.addEventListener("focusin", this.#onFocusIn, {
|
|
420
|
-
capture: true,
|
|
421
|
-
signal
|
|
422
|
-
});
|
|
423
|
-
document.addEventListener("keydown", this.#onKeyDown, {
|
|
424
|
-
capture: true,
|
|
425
|
-
signal
|
|
426
|
-
});
|
|
427
|
-
this.#container.setAttribute("data-roving-tabindex-initialized", "");
|
|
428
|
-
}
|
|
429
|
-
#onFocusIn = (event) => {
|
|
430
|
-
const { target } = event;
|
|
431
|
-
if (!(target instanceof Element)) {
|
|
432
|
-
return;
|
|
433
|
-
}
|
|
434
|
-
const isFocusable22 = this.#focusables.has(target);
|
|
435
|
-
this.#settings.noMemory && !isFocusable22 ? this.#update(null) : isFocusable22 && this.#update(target);
|
|
436
|
-
};
|
|
437
|
-
#onKeyDown = (event) => {
|
|
438
|
-
if (!event.composedPath().includes(this.#container)) {
|
|
439
|
-
return;
|
|
440
|
-
}
|
|
441
|
-
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
442
|
-
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
const { direction, typeahead, wrap } = this.#settings;
|
|
446
|
-
const isBoth = !direction;
|
|
447
|
-
const isHorizontal = direction === "horizontal";
|
|
448
|
-
if (![
|
|
449
|
-
"End",
|
|
450
|
-
"Home",
|
|
451
|
-
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
452
|
-
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
453
|
-
].includes(key)) {
|
|
454
|
-
if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
|
|
455
|
-
return;
|
|
456
|
-
}
|
|
457
|
-
}
|
|
458
|
-
const active = getActiveElement2();
|
|
459
|
-
if (!(active instanceof HTMLElement)) {
|
|
460
|
-
return;
|
|
461
|
-
}
|
|
462
|
-
const current = this.#getFocusables();
|
|
463
|
-
if (!current.includes(active)) {
|
|
464
|
-
return;
|
|
465
|
-
}
|
|
466
|
-
event.preventDefault();
|
|
467
|
-
const currentIndex = current.indexOf(active);
|
|
468
|
-
let newIndex;
|
|
469
|
-
let target = current;
|
|
470
|
-
switch (key) {
|
|
471
|
-
case "End":
|
|
472
|
-
newIndex = -1;
|
|
473
|
-
break;
|
|
474
|
-
case "Home":
|
|
475
|
-
newIndex = 0;
|
|
476
|
-
break;
|
|
477
|
-
case "ArrowLeft":
|
|
478
|
-
case "ArrowUp": {
|
|
479
|
-
const rawIndex = currentIndex - 1;
|
|
480
|
-
newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
|
|
481
|
-
break;
|
|
482
|
-
}
|
|
483
|
-
case "ArrowRight":
|
|
484
|
-
case "ArrowDown": {
|
|
485
|
-
const rawIndex = currentIndex + 1;
|
|
486
|
-
newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
|
|
487
|
-
break;
|
|
488
|
-
}
|
|
489
|
-
default: {
|
|
490
|
-
target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
|
|
491
|
-
const foundIndex = target.findIndex(
|
|
492
|
-
(focusable2) => current.indexOf(focusable2) > currentIndex
|
|
493
|
-
);
|
|
494
|
-
newIndex = foundIndex >= 0 ? foundIndex : 0;
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
const focusable = target.at(newIndex);
|
|
498
|
-
focusable && focusElement(focusable);
|
|
499
|
-
};
|
|
500
|
-
#update(active) {
|
|
501
|
-
const current = new Set(this.#getFocusables());
|
|
502
|
-
for (const focusable of this.#focusables) {
|
|
503
|
-
if (!current.has(focusable)) {
|
|
504
|
-
focusable.isConnected && restoreAttributes([focusable]);
|
|
505
|
-
this.#focusables.delete(focusable);
|
|
506
|
-
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
507
|
-
const index = focusables.indexOf(focusable);
|
|
508
|
-
index >= 0 && focusables.splice(index, 1);
|
|
509
|
-
});
|
|
510
|
-
}
|
|
511
|
-
}
|
|
512
|
-
const { navigationOnly, noStart, typeahead } = this.#settings;
|
|
513
|
-
for (const focusable of current) {
|
|
514
|
-
if (this.#focusables.has(focusable)) {
|
|
515
|
-
continue;
|
|
516
|
-
}
|
|
517
|
-
this.#focusables.add(focusable);
|
|
518
|
-
if (!navigationOnly) {
|
|
519
|
-
saveAttributes([focusable], ["tabindex"]);
|
|
520
|
-
focusable.setAttribute("tabindex", "-1");
|
|
521
|
-
}
|
|
522
|
-
if (!typeahead) {
|
|
523
|
-
continue;
|
|
524
|
-
}
|
|
525
|
-
const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
|
|
526
|
-
const value = focusable.ariaKeyShortcuts?.trim();
|
|
527
|
-
const keys = new Set(
|
|
528
|
-
value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
|
|
529
|
-
);
|
|
530
|
-
if (char) {
|
|
531
|
-
keys.add(char);
|
|
532
|
-
saveAttributes([focusable], ["aria-keyshortcuts"]);
|
|
533
|
-
addTokenToAttribute(focusable, "aria-keyshortcuts", char, {
|
|
534
|
-
caseInsensitive: true
|
|
535
|
-
});
|
|
536
|
-
}
|
|
537
|
-
keys.forEach((key) => {
|
|
538
|
-
const focusables = this.#focusablesByFirstChar.get(key) ?? [];
|
|
539
|
-
focusables.push(focusable);
|
|
540
|
-
this.#focusablesByFirstChar.set(key, focusables);
|
|
541
|
-
});
|
|
542
|
-
}
|
|
543
|
-
if (!navigationOnly) {
|
|
544
|
-
if (active && this.#focusables.has(active)) {
|
|
545
|
-
this.#focusables.forEach((focusable) => {
|
|
546
|
-
focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
|
|
547
|
-
});
|
|
548
|
-
} else {
|
|
549
|
-
[...this.#focusables].forEach((focusable, i) => {
|
|
550
|
-
focusable.setAttribute("tabindex", i || noStart ? "-1" : "0");
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
}
|
|
554
|
-
}
|
|
555
|
-
#createSelectorFilter() {
|
|
556
|
-
const { selector } = this.#settings;
|
|
557
|
-
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
558
|
-
}
|
|
559
|
-
#getFocusables() {
|
|
560
|
-
return getFocusables(this.#container, {
|
|
561
|
-
composed: true,
|
|
562
|
-
filter: this.#selectorFilter,
|
|
563
|
-
skipNegativeTabIndexCheck: !this.#settings.navigationOnly,
|
|
564
|
-
skipVisibilityCheck: true
|
|
565
|
-
});
|
|
566
|
-
}
|
|
567
|
-
};
|
|
568
|
-
function focusElement(element) {
|
|
569
|
-
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
570
|
-
}
|
|
571
|
-
function getActiveElement2() {
|
|
572
|
-
let current = document.activeElement;
|
|
573
|
-
while (current?.shadowRoot?.activeElement) {
|
|
574
|
-
current = current.shadowRoot.activeElement;
|
|
575
|
-
}
|
|
576
|
-
return current;
|
|
577
|
-
}
|
|
7
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
8
|
+
|
|
9
|
+
var Button__default = /*#__PURE__*/_interopDefault(Button);
|
|
578
10
|
|
|
579
11
|
// src/index.ts
|
|
580
12
|
var Accordion = class _Accordion {
|
|
@@ -669,7 +101,7 @@ var Accordion = class _Accordion {
|
|
|
669
101
|
});
|
|
670
102
|
this.#animationController?.abort();
|
|
671
103
|
this.#animationController = null;
|
|
672
|
-
restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
104
|
+
attributesUtils.restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
673
105
|
this.#triggerElements.length = 0;
|
|
674
106
|
this.#contentElements.length = 0;
|
|
675
107
|
this.#rootElement.removeAttribute("data-accordion-initialized");
|
|
@@ -685,14 +117,14 @@ var Accordion = class _Accordion {
|
|
|
685
117
|
this.#toggle(trigger, true);
|
|
686
118
|
}
|
|
687
119
|
#initialize() {
|
|
688
|
-
saveAttributes(this.#triggerElements, [
|
|
120
|
+
attributesUtils.saveAttributes(this.#triggerElements, [
|
|
689
121
|
"aria-controls",
|
|
690
122
|
"aria-disabled",
|
|
691
123
|
"id",
|
|
692
124
|
"style",
|
|
693
125
|
"tabindex"
|
|
694
126
|
]);
|
|
695
|
-
saveAttributes(this.#contentElements, ["aria-labelledby", "id", "role"]);
|
|
127
|
+
attributesUtils.saveAttributes(this.#contentElements, ["aria-labelledby", "id", "role"]);
|
|
696
128
|
this.#eventController = new AbortController();
|
|
697
129
|
const { signal } = this.#eventController;
|
|
698
130
|
this.#triggerElements.forEach((trigger2, i) => {
|
|
@@ -702,27 +134,27 @@ var Accordion = class _Accordion {
|
|
|
702
134
|
return;
|
|
703
135
|
}
|
|
704
136
|
content2.id ||= `accordion-content-${id}`;
|
|
705
|
-
addTokenToAttribute(trigger2, "aria-controls", content2.id);
|
|
137
|
+
attributesUtils.addTokenToAttribute(trigger2, "aria-controls", content2.id);
|
|
706
138
|
trigger2.setAttribute(
|
|
707
139
|
"aria-expanded",
|
|
708
140
|
String(trigger2.ariaExpanded === "true")
|
|
709
141
|
);
|
|
710
142
|
trigger2.id ||= `accordion-trigger-${id}`;
|
|
711
|
-
if (!
|
|
143
|
+
if (!isFocusable(trigger2)) {
|
|
712
144
|
trigger2.setAttribute("aria-disabled", "true");
|
|
713
145
|
trigger2.setAttribute("tabindex", "-1");
|
|
714
146
|
trigger2.style.setProperty("pointer-events", "none");
|
|
715
147
|
}
|
|
716
148
|
trigger2.addEventListener("click", this.#onTriggerClick, { signal });
|
|
717
|
-
addTokenToAttribute(content2, "aria-labelledby", trigger2.id);
|
|
149
|
+
attributesUtils.addTokenToAttribute(content2, "aria-labelledby", trigger2.id);
|
|
718
150
|
content2.setAttribute("role", "region");
|
|
719
151
|
content2.addEventListener("beforematch", this.#onContentBeforeMatch, {
|
|
720
152
|
signal
|
|
721
153
|
});
|
|
722
|
-
this.#buttons.push(new
|
|
154
|
+
this.#buttons.push(new Button__default.default(trigger2));
|
|
723
155
|
});
|
|
724
156
|
const { trigger, content } = this.#settings.selector;
|
|
725
|
-
this.#cleanupRovingTabIndex = createRovingTabIndex(this.#rootElement, {
|
|
157
|
+
this.#cleanupRovingTabIndex = rovingTabindex.createRovingTabIndex(this.#rootElement, {
|
|
726
158
|
direction: "vertical",
|
|
727
159
|
navigationOnly: true,
|
|
728
160
|
selector: `${trigger}:not(:scope ${content} *)`,
|
|
@@ -834,7 +266,7 @@ var Accordion = class _Accordion {
|
|
|
834
266
|
function createBinding(trigger, content) {
|
|
835
267
|
return { trigger, content, animation: null };
|
|
836
268
|
}
|
|
837
|
-
function
|
|
269
|
+
function isFocusable(element) {
|
|
838
270
|
return !element.hasAttribute("disabled") && element.tabIndex >= 0;
|
|
839
271
|
}
|
|
840
272
|
function waitAnimationFinish(animation) {
|
|
@@ -850,63 +282,11 @@ function waitAnimationFinish(animation) {
|
|
|
850
282
|
* Accordion
|
|
851
283
|
* WAI-ARIA compliant accordion pattern implementation in TypeScript.
|
|
852
284
|
*
|
|
853
|
-
* @version 1.4.
|
|
285
|
+
* @version 1.4.15
|
|
854
286
|
* @author Yusuke Kamiyamane
|
|
855
287
|
* @license MIT
|
|
856
288
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
857
289
|
* @see {@link https://github.com/y14e/accordion}
|
|
858
290
|
*/
|
|
859
|
-
/*! Bundled license information:
|
|
860
|
-
|
|
861
|
-
@y14e/button/dist/index.js:
|
|
862
|
-
(**
|
|
863
|
-
* Button
|
|
864
|
-
*
|
|
865
|
-
* @version 1.0.2
|
|
866
|
-
* @author Yusuke Kamiyamane
|
|
867
|
-
* @license MIT
|
|
868
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
869
|
-
* @see {@link https://github.com/y14e/button}
|
|
870
|
-
*)
|
|
871
|
-
|
|
872
|
-
@y14e/roving-tabindex/dist/index.js:
|
|
873
|
-
(**
|
|
874
|
-
* Roving Tabindex
|
|
875
|
-
* Lightweight roving tabindex utility with fully focus management.
|
|
876
|
-
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
877
|
-
*
|
|
878
|
-
* @version 3.0.7
|
|
879
|
-
* @author Yusuke Kamiyamane
|
|
880
|
-
* @license MIT
|
|
881
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
882
|
-
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
883
|
-
*)
|
|
884
|
-
(*! Bundled license information:
|
|
885
|
-
|
|
886
|
-
@y14e/attributes-utils/dist/index.js:
|
|
887
|
-
(**
|
|
888
|
-
* Attributes Utils
|
|
889
|
-
*
|
|
890
|
-
* @version 1.1.2
|
|
891
|
-
* @author Yusuke Kamiyamane
|
|
892
|
-
* @license MIT
|
|
893
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
894
|
-
* @see {@link https://github.com/y14e/attributes-utils}
|
|
895
|
-
*)
|
|
896
|
-
|
|
897
|
-
power-focusable/dist/index.js:
|
|
898
|
-
(**
|
|
899
|
-
* Power Focusable
|
|
900
|
-
* High-precision focus management utility with full composed tree support.
|
|
901
|
-
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
902
|
-
*
|
|
903
|
-
* @version 4.3.3
|
|
904
|
-
* @author Yusuke Kamiyamane
|
|
905
|
-
* @license MIT
|
|
906
|
-
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
907
|
-
* @see {@link https://github.com/y14e/power-focusable}
|
|
908
|
-
*)
|
|
909
|
-
*)
|
|
910
|
-
*/
|
|
911
291
|
|
|
912
292
|
module.exports = Accordion;
|