@y14e/accordion 1.2.12 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +542 -22
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +542 -22
- package/package.json +2 -1
package/dist/index.cjs
CHANGED
|
@@ -50,6 +50,494 @@ function saveAttributes(elements, attributes) {
|
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
52
|
|
|
53
|
+
// node_modules/@y14e/roving-tabindex/dist/index.js
|
|
54
|
+
var defaultParser2 = (value) => value.split(/\s+/);
|
|
55
|
+
var defaultSerializer2 = (tokens) => tokens.join(" ");
|
|
56
|
+
function addTokenToAttribute2(element, attribute, token, options = {}) {
|
|
57
|
+
const {
|
|
58
|
+
caseInsensitive = false,
|
|
59
|
+
parse = defaultParser2,
|
|
60
|
+
serialize = defaultSerializer2
|
|
61
|
+
} = options;
|
|
62
|
+
const value = element.getAttribute(attribute)?.trim();
|
|
63
|
+
const tokens = value ? parse(value).filter(Boolean) : [];
|
|
64
|
+
if (caseInsensitive) {
|
|
65
|
+
const lower = token.toLowerCase();
|
|
66
|
+
if (tokens.some((token2) => token2.toLowerCase() === lower)) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
tokens.push(token);
|
|
70
|
+
element.setAttribute(attribute, serialize(tokens));
|
|
71
|
+
return;
|
|
72
|
+
}
|
|
73
|
+
const set = new Set(tokens);
|
|
74
|
+
set.add(token);
|
|
75
|
+
element.setAttribute(attribute, serialize([...set]));
|
|
76
|
+
}
|
|
77
|
+
var snapshots2 = /* @__PURE__ */ new WeakMap();
|
|
78
|
+
function restoreAttributes2(elements) {
|
|
79
|
+
for (const element of elements) {
|
|
80
|
+
const snapshot = snapshots2.get(element);
|
|
81
|
+
if (!snapshot) {
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
for (const [attribute, value] of snapshot.entries()) {
|
|
85
|
+
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
86
|
+
}
|
|
87
|
+
snapshots2.delete(element);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
function saveAttributes2(elements, attributes) {
|
|
91
|
+
elements.forEach((element) => {
|
|
92
|
+
let snapshot = snapshots2.get(element);
|
|
93
|
+
if (!snapshot) {
|
|
94
|
+
snapshot = /* @__PURE__ */ new Map();
|
|
95
|
+
snapshots2.set(element, snapshot);
|
|
96
|
+
}
|
|
97
|
+
attributes.forEach((attribute) => {
|
|
98
|
+
snapshot.set(attribute, element.getAttribute(attribute));
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
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"])`;
|
|
103
|
+
function getFocusables(container = document.body, options = {}) {
|
|
104
|
+
if (!(container instanceof Element)) {
|
|
105
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
106
|
+
container = document.body;
|
|
107
|
+
}
|
|
108
|
+
let { composed = false, filter, include } = options;
|
|
109
|
+
if (typeof composed !== "boolean") {
|
|
110
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
111
|
+
composed = false;
|
|
112
|
+
}
|
|
113
|
+
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
114
|
+
console.warn(
|
|
115
|
+
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
116
|
+
);
|
|
117
|
+
filter = void 0;
|
|
118
|
+
}
|
|
119
|
+
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
120
|
+
console.warn(
|
|
121
|
+
"Invalid include function. Fallback: no include function (undefined)."
|
|
122
|
+
);
|
|
123
|
+
include = void 0;
|
|
124
|
+
}
|
|
125
|
+
const elements = [];
|
|
126
|
+
if (composed || include) {
|
|
127
|
+
let traverse2 = function(node) {
|
|
128
|
+
if (node instanceof Element) {
|
|
129
|
+
if (isFocusable(node) || include?.(node)) {
|
|
130
|
+
elements[elements.length] = node;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
const children = getComposedChildren(node);
|
|
134
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
135
|
+
const child = children[i];
|
|
136
|
+
if (!child) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
traverse2(child);
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
traverse2(container);
|
|
143
|
+
} else {
|
|
144
|
+
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
145
|
+
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
146
|
+
const candidate = candidates[i];
|
|
147
|
+
if (!(candidate instanceof Element)) {
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (isFocusable(candidate)) {
|
|
151
|
+
elements[elements.length] = candidate;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
156
|
+
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
157
|
+
}
|
|
158
|
+
function isFocusable(element) {
|
|
159
|
+
if (!(element instanceof Element)) {
|
|
160
|
+
console.warn("Invalid element");
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
if (getTabIndex(element) < 0) {
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
if (!element.matches(FOCUSABLE_SELECTOR)) {
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
if (isDisabledDeep(element)) {
|
|
173
|
+
return false;
|
|
174
|
+
}
|
|
175
|
+
if (!element.checkVisibility({
|
|
176
|
+
contentVisibilityAuto: true,
|
|
177
|
+
opacityProperty: true,
|
|
178
|
+
visibilityProperty: true
|
|
179
|
+
})) {
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
function isDisabledDeep(element) {
|
|
185
|
+
let current = element;
|
|
186
|
+
while (current) {
|
|
187
|
+
if (current instanceof ShadowRoot) {
|
|
188
|
+
if (current.mode !== "open") {
|
|
189
|
+
return false;
|
|
190
|
+
}
|
|
191
|
+
current = current.host;
|
|
192
|
+
continue;
|
|
193
|
+
}
|
|
194
|
+
if (!(current instanceof Element)) {
|
|
195
|
+
current = current.parentNode;
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
if (isInert(current)) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
205
|
+
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
current = current.parentNode;
|
|
210
|
+
}
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
function normalizeRadioGroup(elements) {
|
|
214
|
+
let map = null;
|
|
215
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
216
|
+
const element = elements[i];
|
|
217
|
+
if (!(element instanceof HTMLInputElement)) {
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (!isUngroupedRadio(element)) {
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
if (!map) {
|
|
224
|
+
map = /* @__PURE__ */ new Map();
|
|
225
|
+
}
|
|
226
|
+
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
227
|
+
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
228
|
+
if (group) {
|
|
229
|
+
group[group.length] = element;
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (!map) {
|
|
233
|
+
return elements;
|
|
234
|
+
}
|
|
235
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
236
|
+
for (const group of map.values()) {
|
|
237
|
+
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
238
|
+
}
|
|
239
|
+
return elements.filter((element) => {
|
|
240
|
+
if (isUngroupedRadio(element)) {
|
|
241
|
+
return placeholder.has(element);
|
|
242
|
+
}
|
|
243
|
+
return true;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
function sortByTabIndex(elements) {
|
|
247
|
+
const ordered = [];
|
|
248
|
+
const natural = [];
|
|
249
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
250
|
+
const element = elements[i];
|
|
251
|
+
if (!element) {
|
|
252
|
+
continue;
|
|
253
|
+
}
|
|
254
|
+
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
255
|
+
target[target.length] = element;
|
|
256
|
+
}
|
|
257
|
+
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
258
|
+
let count = 0;
|
|
259
|
+
const sorted = new Array(ordered.length + natural.length);
|
|
260
|
+
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
261
|
+
sorted[count++] = ordered[i];
|
|
262
|
+
}
|
|
263
|
+
for (let i = 0, l = natural.length; i < l; i++) {
|
|
264
|
+
sorted[count++] = natural[i];
|
|
265
|
+
}
|
|
266
|
+
return sorted;
|
|
267
|
+
}
|
|
268
|
+
function getComposedChildren(node) {
|
|
269
|
+
if (node instanceof ShadowRoot) {
|
|
270
|
+
return getChildren(node);
|
|
271
|
+
}
|
|
272
|
+
if (!(node instanceof Element)) {
|
|
273
|
+
return [];
|
|
274
|
+
}
|
|
275
|
+
if (node instanceof HTMLSlotElement) {
|
|
276
|
+
const assigned = node.assignedElements({ flatten: true });
|
|
277
|
+
if (assigned.length) {
|
|
278
|
+
return assigned;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
282
|
+
return getChildren(node.shadowRoot);
|
|
283
|
+
}
|
|
284
|
+
return getChildren(node);
|
|
285
|
+
}
|
|
286
|
+
function getChildren(node) {
|
|
287
|
+
const elements = [];
|
|
288
|
+
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
289
|
+
elements[elements.length] = child;
|
|
290
|
+
}
|
|
291
|
+
return elements;
|
|
292
|
+
}
|
|
293
|
+
function getTabIndex(element) {
|
|
294
|
+
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
295
|
+
}
|
|
296
|
+
function isDisabled(element) {
|
|
297
|
+
return "disabled" in element && !!element.disabled;
|
|
298
|
+
}
|
|
299
|
+
function isFormControl(element) {
|
|
300
|
+
const name = element.tagName;
|
|
301
|
+
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
302
|
+
}
|
|
303
|
+
function isInert(element) {
|
|
304
|
+
return "inert" in element && !!element.inert;
|
|
305
|
+
}
|
|
306
|
+
function isUngroupedRadio(element) {
|
|
307
|
+
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
308
|
+
}
|
|
309
|
+
function createRovingTabIndex(container, options = {}) {
|
|
310
|
+
if (!(container instanceof Element)) {
|
|
311
|
+
console.warn("Invalid container element");
|
|
312
|
+
return () => {
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
let {
|
|
316
|
+
direction,
|
|
317
|
+
navigationOnly = false,
|
|
318
|
+
selector,
|
|
319
|
+
typeahead = false,
|
|
320
|
+
wrap = false
|
|
321
|
+
} = options;
|
|
322
|
+
if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
|
|
323
|
+
console.warn("Invalid direction option. Fallback: both (undefined).");
|
|
324
|
+
direction = void 0;
|
|
325
|
+
}
|
|
326
|
+
if (typeof navigationOnly !== "boolean") {
|
|
327
|
+
console.warn("Invalid navigationOnly option. Fallback: false.");
|
|
328
|
+
navigationOnly = false;
|
|
329
|
+
}
|
|
330
|
+
if (typeof selector !== "undefined" && typeof selector !== "string") {
|
|
331
|
+
console.warn(
|
|
332
|
+
"Invalid selector. Fallback: all focusable elements (undefined)."
|
|
333
|
+
);
|
|
334
|
+
selector = void 0;
|
|
335
|
+
}
|
|
336
|
+
if (typeof typeahead !== "boolean") {
|
|
337
|
+
console.warn("Invalid typeahead option. Fallback: false.");
|
|
338
|
+
typeahead = false;
|
|
339
|
+
}
|
|
340
|
+
if (typeof wrap !== "boolean") {
|
|
341
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
342
|
+
wrap = false;
|
|
343
|
+
}
|
|
344
|
+
const roving = new RovingTabIndex(container, {
|
|
345
|
+
direction,
|
|
346
|
+
navigationOnly,
|
|
347
|
+
selector,
|
|
348
|
+
typeahead,
|
|
349
|
+
wrap
|
|
350
|
+
});
|
|
351
|
+
return () => roving.destroy();
|
|
352
|
+
}
|
|
353
|
+
var RovingTabIndex = class {
|
|
354
|
+
#container;
|
|
355
|
+
#options;
|
|
356
|
+
#focusables = /* @__PURE__ */ new Set();
|
|
357
|
+
#focusablesByFirstChar = /* @__PURE__ */ new Map();
|
|
358
|
+
#selectorFilter;
|
|
359
|
+
#controller = null;
|
|
360
|
+
#isDestroyed = false;
|
|
361
|
+
constructor(container, options = {}) {
|
|
362
|
+
this.#container = container;
|
|
363
|
+
this.#options = options;
|
|
364
|
+
this.#selectorFilter = this.#createSelectorFilter();
|
|
365
|
+
this.#initialize();
|
|
366
|
+
}
|
|
367
|
+
destroy() {
|
|
368
|
+
if (this.#isDestroyed) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
this.#isDestroyed = true;
|
|
372
|
+
this.#controller?.abort();
|
|
373
|
+
this.#controller = null;
|
|
374
|
+
restoreAttributes2([...this.#focusables]);
|
|
375
|
+
this.#focusables.clear();
|
|
376
|
+
this.#focusablesByFirstChar.clear();
|
|
377
|
+
this.#container.removeAttribute("data-roving-tabindex-initialized");
|
|
378
|
+
}
|
|
379
|
+
#initialize() {
|
|
380
|
+
this.#update(document.activeElement);
|
|
381
|
+
this.#controller = new AbortController();
|
|
382
|
+
document.addEventListener("keydown", this.#onKeyDown, {
|
|
383
|
+
capture: true,
|
|
384
|
+
signal: this.#controller.signal
|
|
385
|
+
});
|
|
386
|
+
this.#container.setAttribute("data-roving-tabindex-initialized", "");
|
|
387
|
+
}
|
|
388
|
+
#onKeyDown = (event) => {
|
|
389
|
+
if (!event.composedPath().includes(this.#container)) {
|
|
390
|
+
return;
|
|
391
|
+
}
|
|
392
|
+
const { key, altKey, ctrlKey, metaKey } = event;
|
|
393
|
+
if (altKey || ctrlKey || metaKey) {
|
|
394
|
+
return;
|
|
395
|
+
}
|
|
396
|
+
const { direction, typeahead, wrap } = this.#options;
|
|
397
|
+
const isBoth = !direction;
|
|
398
|
+
const isHorizontal = direction === "horizontal";
|
|
399
|
+
if (![
|
|
400
|
+
"End",
|
|
401
|
+
"Home",
|
|
402
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
403
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
404
|
+
].includes(key)) {
|
|
405
|
+
if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
|
|
406
|
+
return;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
const active = getActiveElement();
|
|
410
|
+
if (!(active instanceof HTMLElement)) {
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const current = this.#getFocusables();
|
|
414
|
+
if (!current.includes(active)) {
|
|
415
|
+
return;
|
|
416
|
+
}
|
|
417
|
+
event.preventDefault();
|
|
418
|
+
event.stopPropagation();
|
|
419
|
+
const currentIndex = current.indexOf(active);
|
|
420
|
+
let rawIndex;
|
|
421
|
+
let newIndex = currentIndex;
|
|
422
|
+
let target = current;
|
|
423
|
+
switch (key) {
|
|
424
|
+
case "End":
|
|
425
|
+
newIndex = -1;
|
|
426
|
+
break;
|
|
427
|
+
case "Home":
|
|
428
|
+
newIndex = 0;
|
|
429
|
+
break;
|
|
430
|
+
case "ArrowLeft":
|
|
431
|
+
case "ArrowUp":
|
|
432
|
+
rawIndex = currentIndex - 1;
|
|
433
|
+
newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
|
|
434
|
+
break;
|
|
435
|
+
case "ArrowRight":
|
|
436
|
+
case "ArrowDown":
|
|
437
|
+
rawIndex = currentIndex + 1;
|
|
438
|
+
newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
|
|
439
|
+
break;
|
|
440
|
+
default: {
|
|
441
|
+
target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
|
|
442
|
+
const foundIndex = target.findIndex(
|
|
443
|
+
(focusable2) => current.indexOf(focusable2) > currentIndex
|
|
444
|
+
);
|
|
445
|
+
newIndex = foundIndex >= 0 ? foundIndex : 0;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
const focusable = target.at(newIndex);
|
|
449
|
+
if (!focusable) {
|
|
450
|
+
return;
|
|
451
|
+
}
|
|
452
|
+
this.#update(focusable);
|
|
453
|
+
focusElement(focusable);
|
|
454
|
+
};
|
|
455
|
+
#update(active) {
|
|
456
|
+
const current = /* @__PURE__ */ new Set([
|
|
457
|
+
...this.#getFocusables(),
|
|
458
|
+
...getFocusables(this.#container, {
|
|
459
|
+
composed: true,
|
|
460
|
+
filter: this.#selectorFilter
|
|
461
|
+
})
|
|
462
|
+
]);
|
|
463
|
+
for (const focusable of this.#focusables) {
|
|
464
|
+
if (current.has(focusable)) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
focusable.isConnected && restoreAttributes2([focusable]);
|
|
468
|
+
this.#focusables.delete(focusable);
|
|
469
|
+
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
470
|
+
const index = focusables.indexOf(focusable);
|
|
471
|
+
index >= 0 && focusables.splice(index, 1);
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
const { navigationOnly } = this.#options;
|
|
475
|
+
for (const focusable of current) {
|
|
476
|
+
if (this.#focusables.has(focusable)) {
|
|
477
|
+
continue;
|
|
478
|
+
}
|
|
479
|
+
this.#focusables.add(focusable);
|
|
480
|
+
if (!navigationOnly) {
|
|
481
|
+
saveAttributes2([focusable], ["tabindex"]);
|
|
482
|
+
focusable.setAttribute("tabindex", "-1");
|
|
483
|
+
}
|
|
484
|
+
if (!this.#options.typeahead) {
|
|
485
|
+
continue;
|
|
486
|
+
}
|
|
487
|
+
const value = focusable.ariaKeyShortcuts?.trim();
|
|
488
|
+
const keys = new Set(
|
|
489
|
+
value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
|
|
490
|
+
);
|
|
491
|
+
const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
|
|
492
|
+
if (char) {
|
|
493
|
+
keys.add(char);
|
|
494
|
+
saveAttributes2([focusable], ["aria-keyshortcuts"]);
|
|
495
|
+
addTokenToAttribute2(focusable, "aria-keyshortcuts", char, {
|
|
496
|
+
caseInsensitive: true
|
|
497
|
+
});
|
|
498
|
+
}
|
|
499
|
+
keys.forEach((key) => {
|
|
500
|
+
const focusables = this.#focusablesByFirstChar.get(key) ?? [];
|
|
501
|
+
focusables.push(focusable);
|
|
502
|
+
this.#focusablesByFirstChar.set(key, focusables);
|
|
503
|
+
});
|
|
504
|
+
}
|
|
505
|
+
if (navigationOnly) {
|
|
506
|
+
return;
|
|
507
|
+
}
|
|
508
|
+
if (active && this.#focusables.has(active)) {
|
|
509
|
+
this.#focusables.forEach((focusable) => {
|
|
510
|
+
focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
|
|
511
|
+
});
|
|
512
|
+
return;
|
|
513
|
+
}
|
|
514
|
+
[...this.#focusables].forEach((focusable, i) => {
|
|
515
|
+
focusable.setAttribute("tabindex", i ? "-1" : "0");
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
#createSelectorFilter() {
|
|
519
|
+
const { selector } = this.#options;
|
|
520
|
+
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
521
|
+
}
|
|
522
|
+
#getFocusables() {
|
|
523
|
+
return getFocusables(this.#container, {
|
|
524
|
+
composed: true,
|
|
525
|
+
filter: this.#selectorFilter,
|
|
526
|
+
include: (element) => this.#focusables.has(element)
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
function focusElement(element) {
|
|
531
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
532
|
+
}
|
|
533
|
+
function getActiveElement() {
|
|
534
|
+
let current = document.activeElement;
|
|
535
|
+
while (current?.shadowRoot?.activeElement) {
|
|
536
|
+
current = current.shadowRoot.activeElement;
|
|
537
|
+
}
|
|
538
|
+
return current;
|
|
539
|
+
}
|
|
540
|
+
|
|
53
541
|
// src/index.ts
|
|
54
542
|
var Accordion = class _Accordion {
|
|
55
543
|
static defaults = {};
|
|
@@ -67,6 +555,7 @@ var Accordion = class _Accordion {
|
|
|
67
555
|
#bindings = /* @__PURE__ */ new WeakMap();
|
|
68
556
|
#eventController = null;
|
|
69
557
|
#animationController = null;
|
|
558
|
+
#cleanupRovingTabIndex = null;
|
|
70
559
|
#isDestroyed = false;
|
|
71
560
|
constructor(root, options = {}) {
|
|
72
561
|
if (!(root instanceof HTMLElement)) {
|
|
@@ -109,6 +598,12 @@ var Accordion = class _Accordion {
|
|
|
109
598
|
this.#bindings.set(trigger2, binding);
|
|
110
599
|
this.#bindings.set(content2, binding);
|
|
111
600
|
});
|
|
601
|
+
this.#cleanupRovingTabIndex = createRovingTabIndex(this.#rootElement, {
|
|
602
|
+
direction: "vertical",
|
|
603
|
+
navigationOnly: true,
|
|
604
|
+
selector: `${trigger}${NOT_NESTED}`,
|
|
605
|
+
wrap: true
|
|
606
|
+
});
|
|
112
607
|
this.#initialize();
|
|
113
608
|
}
|
|
114
609
|
open(trigger) {
|
|
@@ -145,6 +640,8 @@ var Accordion = class _Accordion {
|
|
|
145
640
|
});
|
|
146
641
|
this.#animationController?.abort();
|
|
147
642
|
this.#animationController = null;
|
|
643
|
+
this.#cleanupRovingTabIndex?.();
|
|
644
|
+
this.#cleanupRovingTabIndex = null;
|
|
148
645
|
restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
149
646
|
this.#triggerElements.length = 0;
|
|
150
647
|
this.#contentElements.length = 0;
|
|
@@ -174,7 +671,7 @@ var Accordion = class _Accordion {
|
|
|
174
671
|
trigger.ariaExpanded === "true" ? "true" : "false"
|
|
175
672
|
);
|
|
176
673
|
trigger.id ||= `accordion-trigger-${id}`;
|
|
177
|
-
if (!
|
|
674
|
+
if (!isFocusable2(trigger)) {
|
|
178
675
|
trigger.setAttribute("aria-disabled", "true");
|
|
179
676
|
trigger.setAttribute("tabindex", "-1");
|
|
180
677
|
trigger.style.setProperty("pointer-events", "none");
|
|
@@ -202,36 +699,20 @@ var Accordion = class _Accordion {
|
|
|
202
699
|
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
203
700
|
return;
|
|
204
701
|
}
|
|
205
|
-
if (!["Enter", " "
|
|
702
|
+
if (!["Enter", " "].includes(key)) {
|
|
206
703
|
return;
|
|
207
704
|
}
|
|
208
|
-
const
|
|
209
|
-
const active = getActiveElement();
|
|
705
|
+
const active = getActiveElement2();
|
|
210
706
|
if (!(active instanceof HTMLElement)) {
|
|
211
707
|
return;
|
|
212
708
|
}
|
|
213
709
|
event.preventDefault();
|
|
214
|
-
const currentIndex = focusables.indexOf(active);
|
|
215
|
-
let newIndex = currentIndex;
|
|
216
710
|
switch (key) {
|
|
217
711
|
case "Enter":
|
|
218
712
|
case " ":
|
|
219
713
|
active.click();
|
|
220
714
|
return;
|
|
221
|
-
case "End":
|
|
222
|
-
newIndex = -1;
|
|
223
|
-
break;
|
|
224
|
-
case "Home":
|
|
225
|
-
newIndex = 0;
|
|
226
|
-
break;
|
|
227
|
-
case "ArrowUp":
|
|
228
|
-
newIndex = currentIndex - 1;
|
|
229
|
-
break;
|
|
230
|
-
case "ArrowDown":
|
|
231
|
-
newIndex = (currentIndex + 1) % focusables.length;
|
|
232
|
-
break;
|
|
233
715
|
}
|
|
234
|
-
focusables.at(newIndex)?.focus();
|
|
235
716
|
};
|
|
236
717
|
#onContentBeforeMatch = (event) => {
|
|
237
718
|
const content = event.currentTarget;
|
|
@@ -329,14 +810,14 @@ var Accordion = class _Accordion {
|
|
|
329
810
|
function createBinding(trigger, content) {
|
|
330
811
|
return { trigger, content, animation: null };
|
|
331
812
|
}
|
|
332
|
-
function
|
|
813
|
+
function getActiveElement2() {
|
|
333
814
|
let current = document.activeElement;
|
|
334
815
|
while (current?.shadowRoot?.activeElement) {
|
|
335
816
|
current = current.shadowRoot.activeElement;
|
|
336
817
|
}
|
|
337
818
|
return current;
|
|
338
819
|
}
|
|
339
|
-
function
|
|
820
|
+
function isFocusable2(element) {
|
|
340
821
|
return !element.hasAttribute("disabled") && element.tabIndex >= 0;
|
|
341
822
|
}
|
|
342
823
|
function waitAnimationFinish(animation) {
|
|
@@ -352,7 +833,7 @@ function waitAnimationFinish(animation) {
|
|
|
352
833
|
* Accordion
|
|
353
834
|
* WAI-ARIA compliant accordion pattern implementation in TypeScript.
|
|
354
835
|
*
|
|
355
|
-
* @version 1.
|
|
836
|
+
* @version 1.3.0
|
|
356
837
|
* @author Yusuke Kamiyamane
|
|
357
838
|
* @license MIT
|
|
358
839
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
@@ -370,6 +851,45 @@ function waitAnimationFinish(animation) {
|
|
|
370
851
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
371
852
|
* @see {@link https://github.com/y14e/attributes-utils}
|
|
372
853
|
*)
|
|
854
|
+
|
|
855
|
+
@y14e/roving-tabindex/dist/index.js:
|
|
856
|
+
(**
|
|
857
|
+
* Roving Tabindex
|
|
858
|
+
* Lightweight roving tabindex utility with fully focus management.
|
|
859
|
+
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
860
|
+
*
|
|
861
|
+
* @version 1.3.0
|
|
862
|
+
* @author Yusuke Kamiyamane
|
|
863
|
+
* @license MIT
|
|
864
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
865
|
+
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
866
|
+
*)
|
|
867
|
+
(*! Bundled license information:
|
|
868
|
+
|
|
869
|
+
@y14e/attributes-utils/dist/index.js:
|
|
870
|
+
(**
|
|
871
|
+
* Attributes Utils
|
|
872
|
+
*
|
|
873
|
+
* @version 1.0.5
|
|
874
|
+
* @author Yusuke Kamiyamane
|
|
875
|
+
* @license MIT
|
|
876
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
877
|
+
* @see {@link https://github.com/y14e/attributes-utils}
|
|
878
|
+
*)
|
|
879
|
+
|
|
880
|
+
power-focusable/dist/index.js:
|
|
881
|
+
(**
|
|
882
|
+
* Power Focusable
|
|
883
|
+
* High-precision focus management utility with full composed tree support.
|
|
884
|
+
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
885
|
+
*
|
|
886
|
+
* @version 4.1.8
|
|
887
|
+
* @author Yusuke Kamiyamane
|
|
888
|
+
* @license MIT
|
|
889
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
890
|
+
* @see {@link https://github.com/y14e/power-focusable}
|
|
891
|
+
*)
|
|
892
|
+
*)
|
|
373
893
|
*/
|
|
374
894
|
|
|
375
895
|
module.exports = Accordion;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -48,6 +48,494 @@ function saveAttributes(elements, attributes) {
|
|
|
48
48
|
});
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
+
// node_modules/@y14e/roving-tabindex/dist/index.js
|
|
52
|
+
var defaultParser2 = (value) => value.split(/\s+/);
|
|
53
|
+
var defaultSerializer2 = (tokens) => tokens.join(" ");
|
|
54
|
+
function addTokenToAttribute2(element, attribute, token, options = {}) {
|
|
55
|
+
const {
|
|
56
|
+
caseInsensitive = false,
|
|
57
|
+
parse = defaultParser2,
|
|
58
|
+
serialize = defaultSerializer2
|
|
59
|
+
} = options;
|
|
60
|
+
const value = element.getAttribute(attribute)?.trim();
|
|
61
|
+
const tokens = value ? parse(value).filter(Boolean) : [];
|
|
62
|
+
if (caseInsensitive) {
|
|
63
|
+
const lower = token.toLowerCase();
|
|
64
|
+
if (tokens.some((token2) => token2.toLowerCase() === lower)) {
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
tokens.push(token);
|
|
68
|
+
element.setAttribute(attribute, serialize(tokens));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const set = new Set(tokens);
|
|
72
|
+
set.add(token);
|
|
73
|
+
element.setAttribute(attribute, serialize([...set]));
|
|
74
|
+
}
|
|
75
|
+
var snapshots2 = /* @__PURE__ */ new WeakMap();
|
|
76
|
+
function restoreAttributes2(elements) {
|
|
77
|
+
for (const element of elements) {
|
|
78
|
+
const snapshot = snapshots2.get(element);
|
|
79
|
+
if (!snapshot) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
for (const [attribute, value] of snapshot.entries()) {
|
|
83
|
+
value === null ? element.removeAttribute(attribute) : element.setAttribute(attribute, value);
|
|
84
|
+
}
|
|
85
|
+
snapshots2.delete(element);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function saveAttributes2(elements, attributes) {
|
|
89
|
+
elements.forEach((element) => {
|
|
90
|
+
let snapshot = snapshots2.get(element);
|
|
91
|
+
if (!snapshot) {
|
|
92
|
+
snapshot = /* @__PURE__ */ new Map();
|
|
93
|
+
snapshots2.set(element, snapshot);
|
|
94
|
+
}
|
|
95
|
+
attributes.forEach((attribute) => {
|
|
96
|
+
snapshot.set(attribute, element.getAttribute(attribute));
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
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"])`;
|
|
101
|
+
function getFocusables(container = document.body, options = {}) {
|
|
102
|
+
if (!(container instanceof Element)) {
|
|
103
|
+
console.warn("Invalid container element. Fallback: <body> element.");
|
|
104
|
+
container = document.body;
|
|
105
|
+
}
|
|
106
|
+
let { composed = false, filter, include } = options;
|
|
107
|
+
if (typeof composed !== "boolean") {
|
|
108
|
+
console.warn("Invalid composed option. Fallback: false.");
|
|
109
|
+
composed = false;
|
|
110
|
+
}
|
|
111
|
+
if (typeof filter !== "undefined" && typeof filter !== "function") {
|
|
112
|
+
console.warn(
|
|
113
|
+
"Invalid filter function. Fallback: no filter function (undefined)."
|
|
114
|
+
);
|
|
115
|
+
filter = void 0;
|
|
116
|
+
}
|
|
117
|
+
if (typeof include !== "undefined" && typeof include !== "function") {
|
|
118
|
+
console.warn(
|
|
119
|
+
"Invalid include function. Fallback: no include function (undefined)."
|
|
120
|
+
);
|
|
121
|
+
include = void 0;
|
|
122
|
+
}
|
|
123
|
+
const elements = [];
|
|
124
|
+
if (composed || include) {
|
|
125
|
+
let traverse2 = function(node) {
|
|
126
|
+
if (node instanceof Element) {
|
|
127
|
+
if (isFocusable(node) || include?.(node)) {
|
|
128
|
+
elements[elements.length] = node;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const children = getComposedChildren(node);
|
|
132
|
+
for (let i = 0, l = children.length; i < l; i++) {
|
|
133
|
+
const child = children[i];
|
|
134
|
+
if (!child) {
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
traverse2(child);
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
traverse2(container);
|
|
141
|
+
} else {
|
|
142
|
+
const candidates = container.querySelectorAll(FOCUSABLE_SELECTOR);
|
|
143
|
+
for (let i = 0, l = candidates.length; i < l; i++) {
|
|
144
|
+
const candidate = candidates[i];
|
|
145
|
+
if (!(candidate instanceof Element)) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (isFocusable(candidate)) {
|
|
149
|
+
elements[elements.length] = candidate;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const unfiltered = normalizeRadioGroup(sortByTabIndex(elements));
|
|
154
|
+
return filter ? unfiltered.filter(filter) : unfiltered;
|
|
155
|
+
}
|
|
156
|
+
function isFocusable(element) {
|
|
157
|
+
if (!(element instanceof Element)) {
|
|
158
|
+
console.warn("Invalid element");
|
|
159
|
+
return false;
|
|
160
|
+
}
|
|
161
|
+
if (element.hasAttribute("hidden") || isInert(element)) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
if (getTabIndex(element) < 0) {
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
if (!element.matches(FOCUSABLE_SELECTOR)) {
|
|
168
|
+
return false;
|
|
169
|
+
}
|
|
170
|
+
if (isDisabledDeep(element)) {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
if (!element.checkVisibility({
|
|
174
|
+
contentVisibilityAuto: true,
|
|
175
|
+
opacityProperty: true,
|
|
176
|
+
visibilityProperty: true
|
|
177
|
+
})) {
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
182
|
+
function isDisabledDeep(element) {
|
|
183
|
+
let current = element;
|
|
184
|
+
while (current) {
|
|
185
|
+
if (current instanceof ShadowRoot) {
|
|
186
|
+
if (current.mode !== "open") {
|
|
187
|
+
return false;
|
|
188
|
+
}
|
|
189
|
+
current = current.host;
|
|
190
|
+
continue;
|
|
191
|
+
}
|
|
192
|
+
if (!(current instanceof Element)) {
|
|
193
|
+
current = current.parentNode;
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (current === element && isFormControl(current) && isDisabled(current)) {
|
|
197
|
+
return true;
|
|
198
|
+
}
|
|
199
|
+
if (isInert(current)) {
|
|
200
|
+
return true;
|
|
201
|
+
}
|
|
202
|
+
if (isFormControl(element) && current.tagName === "FIELDSET" && isDisabled(current)) {
|
|
203
|
+
if (!current.querySelector(":scope > legend:first-of-type")?.contains(element)) {
|
|
204
|
+
return true;
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
current = current.parentNode;
|
|
208
|
+
}
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
function normalizeRadioGroup(elements) {
|
|
212
|
+
let map = null;
|
|
213
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
214
|
+
const element = elements[i];
|
|
215
|
+
if (!(element instanceof HTMLInputElement)) {
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (!isUngroupedRadio(element)) {
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
if (!map) {
|
|
222
|
+
map = /* @__PURE__ */ new Map();
|
|
223
|
+
}
|
|
224
|
+
const key = `${element.form?.id ?? "no-form"}::${element.name}`;
|
|
225
|
+
const group = map.get(key) ?? map.set(key, []).get(key);
|
|
226
|
+
if (group) {
|
|
227
|
+
group[group.length] = element;
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
if (!map) {
|
|
231
|
+
return elements;
|
|
232
|
+
}
|
|
233
|
+
const placeholder = /* @__PURE__ */ new Set();
|
|
234
|
+
for (const group of map.values()) {
|
|
235
|
+
placeholder.add(group.find((radio) => radio.checked) ?? group[0]);
|
|
236
|
+
}
|
|
237
|
+
return elements.filter((element) => {
|
|
238
|
+
if (isUngroupedRadio(element)) {
|
|
239
|
+
return placeholder.has(element);
|
|
240
|
+
}
|
|
241
|
+
return true;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
function sortByTabIndex(elements) {
|
|
245
|
+
const ordered = [];
|
|
246
|
+
const natural = [];
|
|
247
|
+
for (let i = 0, l = elements.length; i < l; i++) {
|
|
248
|
+
const element = elements[i];
|
|
249
|
+
if (!element) {
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const target = getTabIndex(element) > 0 ? ordered : natural;
|
|
253
|
+
target[target.length] = element;
|
|
254
|
+
}
|
|
255
|
+
ordered.sort((a, b) => getTabIndex(a) - getTabIndex(b));
|
|
256
|
+
let count = 0;
|
|
257
|
+
const sorted = new Array(ordered.length + natural.length);
|
|
258
|
+
for (let i = 0, l = ordered.length; i < l; i++) {
|
|
259
|
+
sorted[count++] = ordered[i];
|
|
260
|
+
}
|
|
261
|
+
for (let i = 0, l = natural.length; i < l; i++) {
|
|
262
|
+
sorted[count++] = natural[i];
|
|
263
|
+
}
|
|
264
|
+
return sorted;
|
|
265
|
+
}
|
|
266
|
+
function getComposedChildren(node) {
|
|
267
|
+
if (node instanceof ShadowRoot) {
|
|
268
|
+
return getChildren(node);
|
|
269
|
+
}
|
|
270
|
+
if (!(node instanceof Element)) {
|
|
271
|
+
return [];
|
|
272
|
+
}
|
|
273
|
+
if (node instanceof HTMLSlotElement) {
|
|
274
|
+
const assigned = node.assignedElements({ flatten: true });
|
|
275
|
+
if (assigned.length) {
|
|
276
|
+
return assigned;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (node instanceof HTMLElement && node.shadowRoot?.mode === "open") {
|
|
280
|
+
return getChildren(node.shadowRoot);
|
|
281
|
+
}
|
|
282
|
+
return getChildren(node);
|
|
283
|
+
}
|
|
284
|
+
function getChildren(node) {
|
|
285
|
+
const elements = [];
|
|
286
|
+
for (let child = node.firstElementChild; child; child = child.nextElementSibling) {
|
|
287
|
+
elements[elements.length] = child;
|
|
288
|
+
}
|
|
289
|
+
return elements;
|
|
290
|
+
}
|
|
291
|
+
function getTabIndex(element) {
|
|
292
|
+
return "tabIndex" in element ? Number(element.tabIndex) : 0;
|
|
293
|
+
}
|
|
294
|
+
function isDisabled(element) {
|
|
295
|
+
return "disabled" in element && !!element.disabled;
|
|
296
|
+
}
|
|
297
|
+
function isFormControl(element) {
|
|
298
|
+
const name = element.tagName;
|
|
299
|
+
return name === "BUTTON" || name === "INPUT" || name === "SELECT" || name === "TEXTAREA";
|
|
300
|
+
}
|
|
301
|
+
function isInert(element) {
|
|
302
|
+
return "inert" in element && !!element.inert;
|
|
303
|
+
}
|
|
304
|
+
function isUngroupedRadio(element) {
|
|
305
|
+
return element instanceof HTMLInputElement && element.type === "radio" && !!element.name;
|
|
306
|
+
}
|
|
307
|
+
function createRovingTabIndex(container, options = {}) {
|
|
308
|
+
if (!(container instanceof Element)) {
|
|
309
|
+
console.warn("Invalid container element");
|
|
310
|
+
return () => {
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
let {
|
|
314
|
+
direction,
|
|
315
|
+
navigationOnly = false,
|
|
316
|
+
selector,
|
|
317
|
+
typeahead = false,
|
|
318
|
+
wrap = false
|
|
319
|
+
} = options;
|
|
320
|
+
if (typeof direction !== "undefined" && !["horizontal", "vertical"].includes(direction)) {
|
|
321
|
+
console.warn("Invalid direction option. Fallback: both (undefined).");
|
|
322
|
+
direction = void 0;
|
|
323
|
+
}
|
|
324
|
+
if (typeof navigationOnly !== "boolean") {
|
|
325
|
+
console.warn("Invalid navigationOnly option. Fallback: false.");
|
|
326
|
+
navigationOnly = false;
|
|
327
|
+
}
|
|
328
|
+
if (typeof selector !== "undefined" && typeof selector !== "string") {
|
|
329
|
+
console.warn(
|
|
330
|
+
"Invalid selector. Fallback: all focusable elements (undefined)."
|
|
331
|
+
);
|
|
332
|
+
selector = void 0;
|
|
333
|
+
}
|
|
334
|
+
if (typeof typeahead !== "boolean") {
|
|
335
|
+
console.warn("Invalid typeahead option. Fallback: false.");
|
|
336
|
+
typeahead = false;
|
|
337
|
+
}
|
|
338
|
+
if (typeof wrap !== "boolean") {
|
|
339
|
+
console.warn("Invalid wrap option. Fallback: false.");
|
|
340
|
+
wrap = false;
|
|
341
|
+
}
|
|
342
|
+
const roving = new RovingTabIndex(container, {
|
|
343
|
+
direction,
|
|
344
|
+
navigationOnly,
|
|
345
|
+
selector,
|
|
346
|
+
typeahead,
|
|
347
|
+
wrap
|
|
348
|
+
});
|
|
349
|
+
return () => roving.destroy();
|
|
350
|
+
}
|
|
351
|
+
var RovingTabIndex = class {
|
|
352
|
+
#container;
|
|
353
|
+
#options;
|
|
354
|
+
#focusables = /* @__PURE__ */ new Set();
|
|
355
|
+
#focusablesByFirstChar = /* @__PURE__ */ new Map();
|
|
356
|
+
#selectorFilter;
|
|
357
|
+
#controller = null;
|
|
358
|
+
#isDestroyed = false;
|
|
359
|
+
constructor(container, options = {}) {
|
|
360
|
+
this.#container = container;
|
|
361
|
+
this.#options = options;
|
|
362
|
+
this.#selectorFilter = this.#createSelectorFilter();
|
|
363
|
+
this.#initialize();
|
|
364
|
+
}
|
|
365
|
+
destroy() {
|
|
366
|
+
if (this.#isDestroyed) {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
this.#isDestroyed = true;
|
|
370
|
+
this.#controller?.abort();
|
|
371
|
+
this.#controller = null;
|
|
372
|
+
restoreAttributes2([...this.#focusables]);
|
|
373
|
+
this.#focusables.clear();
|
|
374
|
+
this.#focusablesByFirstChar.clear();
|
|
375
|
+
this.#container.removeAttribute("data-roving-tabindex-initialized");
|
|
376
|
+
}
|
|
377
|
+
#initialize() {
|
|
378
|
+
this.#update(document.activeElement);
|
|
379
|
+
this.#controller = new AbortController();
|
|
380
|
+
document.addEventListener("keydown", this.#onKeyDown, {
|
|
381
|
+
capture: true,
|
|
382
|
+
signal: this.#controller.signal
|
|
383
|
+
});
|
|
384
|
+
this.#container.setAttribute("data-roving-tabindex-initialized", "");
|
|
385
|
+
}
|
|
386
|
+
#onKeyDown = (event) => {
|
|
387
|
+
if (!event.composedPath().includes(this.#container)) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const { key, altKey, ctrlKey, metaKey } = event;
|
|
391
|
+
if (altKey || ctrlKey || metaKey) {
|
|
392
|
+
return;
|
|
393
|
+
}
|
|
394
|
+
const { direction, typeahead, wrap } = this.#options;
|
|
395
|
+
const isBoth = !direction;
|
|
396
|
+
const isHorizontal = direction === "horizontal";
|
|
397
|
+
if (![
|
|
398
|
+
"End",
|
|
399
|
+
"Home",
|
|
400
|
+
...isBoth ? ["ArrowLeft", "ArrowUp"] : [`Arrow${isHorizontal ? "Left" : "Up"}`],
|
|
401
|
+
...isBoth ? ["ArrowRight", "ArrowDown"] : [`Arrow${isHorizontal ? "Right" : "Down"}`]
|
|
402
|
+
].includes(key)) {
|
|
403
|
+
if (!typeahead || !/^\S$/i.test(key) || !this.#focusablesByFirstChar.has(key.toUpperCase())) {
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
const active = getActiveElement();
|
|
408
|
+
if (!(active instanceof HTMLElement)) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
const current = this.#getFocusables();
|
|
412
|
+
if (!current.includes(active)) {
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
event.preventDefault();
|
|
416
|
+
event.stopPropagation();
|
|
417
|
+
const currentIndex = current.indexOf(active);
|
|
418
|
+
let rawIndex;
|
|
419
|
+
let newIndex = currentIndex;
|
|
420
|
+
let target = current;
|
|
421
|
+
switch (key) {
|
|
422
|
+
case "End":
|
|
423
|
+
newIndex = -1;
|
|
424
|
+
break;
|
|
425
|
+
case "Home":
|
|
426
|
+
newIndex = 0;
|
|
427
|
+
break;
|
|
428
|
+
case "ArrowLeft":
|
|
429
|
+
case "ArrowUp":
|
|
430
|
+
rawIndex = currentIndex - 1;
|
|
431
|
+
newIndex = wrap ? rawIndex : Math.max(rawIndex, 0);
|
|
432
|
+
break;
|
|
433
|
+
case "ArrowRight":
|
|
434
|
+
case "ArrowDown":
|
|
435
|
+
rawIndex = currentIndex + 1;
|
|
436
|
+
newIndex = wrap ? rawIndex % current.length : Math.min(rawIndex, current.length - 1);
|
|
437
|
+
break;
|
|
438
|
+
default: {
|
|
439
|
+
target = this.#focusablesByFirstChar.get(key.toUpperCase()) ?? [];
|
|
440
|
+
const foundIndex = target.findIndex(
|
|
441
|
+
(focusable2) => current.indexOf(focusable2) > currentIndex
|
|
442
|
+
);
|
|
443
|
+
newIndex = foundIndex >= 0 ? foundIndex : 0;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
const focusable = target.at(newIndex);
|
|
447
|
+
if (!focusable) {
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
this.#update(focusable);
|
|
451
|
+
focusElement(focusable);
|
|
452
|
+
};
|
|
453
|
+
#update(active) {
|
|
454
|
+
const current = /* @__PURE__ */ new Set([
|
|
455
|
+
...this.#getFocusables(),
|
|
456
|
+
...getFocusables(this.#container, {
|
|
457
|
+
composed: true,
|
|
458
|
+
filter: this.#selectorFilter
|
|
459
|
+
})
|
|
460
|
+
]);
|
|
461
|
+
for (const focusable of this.#focusables) {
|
|
462
|
+
if (current.has(focusable)) {
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
focusable.isConnected && restoreAttributes2([focusable]);
|
|
466
|
+
this.#focusables.delete(focusable);
|
|
467
|
+
this.#focusablesByFirstChar.forEach((focusables) => {
|
|
468
|
+
const index = focusables.indexOf(focusable);
|
|
469
|
+
index >= 0 && focusables.splice(index, 1);
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
const { navigationOnly } = this.#options;
|
|
473
|
+
for (const focusable of current) {
|
|
474
|
+
if (this.#focusables.has(focusable)) {
|
|
475
|
+
continue;
|
|
476
|
+
}
|
|
477
|
+
this.#focusables.add(focusable);
|
|
478
|
+
if (!navigationOnly) {
|
|
479
|
+
saveAttributes2([focusable], ["tabindex"]);
|
|
480
|
+
focusable.setAttribute("tabindex", "-1");
|
|
481
|
+
}
|
|
482
|
+
if (!this.#options.typeahead) {
|
|
483
|
+
continue;
|
|
484
|
+
}
|
|
485
|
+
const value = focusable.ariaKeyShortcuts?.trim();
|
|
486
|
+
const keys = new Set(
|
|
487
|
+
value ? value.split(/\s+/).filter((key) => /^\S$/i.test(key)).map((key) => key.toUpperCase()) : []
|
|
488
|
+
);
|
|
489
|
+
const char = focusable.textContent?.trim()?.at(0)?.toUpperCase();
|
|
490
|
+
if (char) {
|
|
491
|
+
keys.add(char);
|
|
492
|
+
saveAttributes2([focusable], ["aria-keyshortcuts"]);
|
|
493
|
+
addTokenToAttribute2(focusable, "aria-keyshortcuts", char, {
|
|
494
|
+
caseInsensitive: true
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
keys.forEach((key) => {
|
|
498
|
+
const focusables = this.#focusablesByFirstChar.get(key) ?? [];
|
|
499
|
+
focusables.push(focusable);
|
|
500
|
+
this.#focusablesByFirstChar.set(key, focusables);
|
|
501
|
+
});
|
|
502
|
+
}
|
|
503
|
+
if (navigationOnly) {
|
|
504
|
+
return;
|
|
505
|
+
}
|
|
506
|
+
if (active && this.#focusables.has(active)) {
|
|
507
|
+
this.#focusables.forEach((focusable) => {
|
|
508
|
+
focusable.setAttribute("tabindex", focusable === active ? "0" : "-1");
|
|
509
|
+
});
|
|
510
|
+
return;
|
|
511
|
+
}
|
|
512
|
+
[...this.#focusables].forEach((focusable, i) => {
|
|
513
|
+
focusable.setAttribute("tabindex", i ? "-1" : "0");
|
|
514
|
+
});
|
|
515
|
+
}
|
|
516
|
+
#createSelectorFilter() {
|
|
517
|
+
const { selector } = this.#options;
|
|
518
|
+
return (element) => !selector || [...this.#container.querySelectorAll(selector)].includes(element);
|
|
519
|
+
}
|
|
520
|
+
#getFocusables() {
|
|
521
|
+
return getFocusables(this.#container, {
|
|
522
|
+
composed: true,
|
|
523
|
+
filter: this.#selectorFilter,
|
|
524
|
+
include: (element) => this.#focusables.has(element)
|
|
525
|
+
});
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
function focusElement(element) {
|
|
529
|
+
"focus" in element && typeof element.focus === "function" && element.focus();
|
|
530
|
+
}
|
|
531
|
+
function getActiveElement() {
|
|
532
|
+
let current = document.activeElement;
|
|
533
|
+
while (current?.shadowRoot?.activeElement) {
|
|
534
|
+
current = current.shadowRoot.activeElement;
|
|
535
|
+
}
|
|
536
|
+
return current;
|
|
537
|
+
}
|
|
538
|
+
|
|
51
539
|
// src/index.ts
|
|
52
540
|
var Accordion = class _Accordion {
|
|
53
541
|
static defaults = {};
|
|
@@ -65,6 +553,7 @@ var Accordion = class _Accordion {
|
|
|
65
553
|
#bindings = /* @__PURE__ */ new WeakMap();
|
|
66
554
|
#eventController = null;
|
|
67
555
|
#animationController = null;
|
|
556
|
+
#cleanupRovingTabIndex = null;
|
|
68
557
|
#isDestroyed = false;
|
|
69
558
|
constructor(root, options = {}) {
|
|
70
559
|
if (!(root instanceof HTMLElement)) {
|
|
@@ -107,6 +596,12 @@ var Accordion = class _Accordion {
|
|
|
107
596
|
this.#bindings.set(trigger2, binding);
|
|
108
597
|
this.#bindings.set(content2, binding);
|
|
109
598
|
});
|
|
599
|
+
this.#cleanupRovingTabIndex = createRovingTabIndex(this.#rootElement, {
|
|
600
|
+
direction: "vertical",
|
|
601
|
+
navigationOnly: true,
|
|
602
|
+
selector: `${trigger}${NOT_NESTED}`,
|
|
603
|
+
wrap: true
|
|
604
|
+
});
|
|
110
605
|
this.#initialize();
|
|
111
606
|
}
|
|
112
607
|
open(trigger) {
|
|
@@ -143,6 +638,8 @@ var Accordion = class _Accordion {
|
|
|
143
638
|
});
|
|
144
639
|
this.#animationController?.abort();
|
|
145
640
|
this.#animationController = null;
|
|
641
|
+
this.#cleanupRovingTabIndex?.();
|
|
642
|
+
this.#cleanupRovingTabIndex = null;
|
|
146
643
|
restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
147
644
|
this.#triggerElements.length = 0;
|
|
148
645
|
this.#contentElements.length = 0;
|
|
@@ -172,7 +669,7 @@ var Accordion = class _Accordion {
|
|
|
172
669
|
trigger.ariaExpanded === "true" ? "true" : "false"
|
|
173
670
|
);
|
|
174
671
|
trigger.id ||= `accordion-trigger-${id}`;
|
|
175
|
-
if (!
|
|
672
|
+
if (!isFocusable2(trigger)) {
|
|
176
673
|
trigger.setAttribute("aria-disabled", "true");
|
|
177
674
|
trigger.setAttribute("tabindex", "-1");
|
|
178
675
|
trigger.style.setProperty("pointer-events", "none");
|
|
@@ -200,36 +697,20 @@ var Accordion = class _Accordion {
|
|
|
200
697
|
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
201
698
|
return;
|
|
202
699
|
}
|
|
203
|
-
if (!["Enter", " "
|
|
700
|
+
if (!["Enter", " "].includes(key)) {
|
|
204
701
|
return;
|
|
205
702
|
}
|
|
206
|
-
const
|
|
207
|
-
const active = getActiveElement();
|
|
703
|
+
const active = getActiveElement2();
|
|
208
704
|
if (!(active instanceof HTMLElement)) {
|
|
209
705
|
return;
|
|
210
706
|
}
|
|
211
707
|
event.preventDefault();
|
|
212
|
-
const currentIndex = focusables.indexOf(active);
|
|
213
|
-
let newIndex = currentIndex;
|
|
214
708
|
switch (key) {
|
|
215
709
|
case "Enter":
|
|
216
710
|
case " ":
|
|
217
711
|
active.click();
|
|
218
712
|
return;
|
|
219
|
-
case "End":
|
|
220
|
-
newIndex = -1;
|
|
221
|
-
break;
|
|
222
|
-
case "Home":
|
|
223
|
-
newIndex = 0;
|
|
224
|
-
break;
|
|
225
|
-
case "ArrowUp":
|
|
226
|
-
newIndex = currentIndex - 1;
|
|
227
|
-
break;
|
|
228
|
-
case "ArrowDown":
|
|
229
|
-
newIndex = (currentIndex + 1) % focusables.length;
|
|
230
|
-
break;
|
|
231
713
|
}
|
|
232
|
-
focusables.at(newIndex)?.focus();
|
|
233
714
|
};
|
|
234
715
|
#onContentBeforeMatch = (event) => {
|
|
235
716
|
const content = event.currentTarget;
|
|
@@ -327,14 +808,14 @@ var Accordion = class _Accordion {
|
|
|
327
808
|
function createBinding(trigger, content) {
|
|
328
809
|
return { trigger, content, animation: null };
|
|
329
810
|
}
|
|
330
|
-
function
|
|
811
|
+
function getActiveElement2() {
|
|
331
812
|
let current = document.activeElement;
|
|
332
813
|
while (current?.shadowRoot?.activeElement) {
|
|
333
814
|
current = current.shadowRoot.activeElement;
|
|
334
815
|
}
|
|
335
816
|
return current;
|
|
336
817
|
}
|
|
337
|
-
function
|
|
818
|
+
function isFocusable2(element) {
|
|
338
819
|
return !element.hasAttribute("disabled") && element.tabIndex >= 0;
|
|
339
820
|
}
|
|
340
821
|
function waitAnimationFinish(animation) {
|
|
@@ -350,7 +831,7 @@ function waitAnimationFinish(animation) {
|
|
|
350
831
|
* Accordion
|
|
351
832
|
* WAI-ARIA compliant accordion pattern implementation in TypeScript.
|
|
352
833
|
*
|
|
353
|
-
* @version 1.
|
|
834
|
+
* @version 1.3.0
|
|
354
835
|
* @author Yusuke Kamiyamane
|
|
355
836
|
* @license MIT
|
|
356
837
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
@@ -368,6 +849,45 @@ function waitAnimationFinish(animation) {
|
|
|
368
849
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
369
850
|
* @see {@link https://github.com/y14e/attributes-utils}
|
|
370
851
|
*)
|
|
852
|
+
|
|
853
|
+
@y14e/roving-tabindex/dist/index.js:
|
|
854
|
+
(**
|
|
855
|
+
* Roving Tabindex
|
|
856
|
+
* Lightweight roving tabindex utility with fully focus management.
|
|
857
|
+
* Designed for accessible menus, tabs, toolbars, and composite widgets.
|
|
858
|
+
*
|
|
859
|
+
* @version 1.3.0
|
|
860
|
+
* @author Yusuke Kamiyamane
|
|
861
|
+
* @license MIT
|
|
862
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
863
|
+
* @see {@link https://github.com/y14e/roving-tabindex}
|
|
864
|
+
*)
|
|
865
|
+
(*! Bundled license information:
|
|
866
|
+
|
|
867
|
+
@y14e/attributes-utils/dist/index.js:
|
|
868
|
+
(**
|
|
869
|
+
* Attributes Utils
|
|
870
|
+
*
|
|
871
|
+
* @version 1.0.5
|
|
872
|
+
* @author Yusuke Kamiyamane
|
|
873
|
+
* @license MIT
|
|
874
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
875
|
+
* @see {@link https://github.com/y14e/attributes-utils}
|
|
876
|
+
*)
|
|
877
|
+
|
|
878
|
+
power-focusable/dist/index.js:
|
|
879
|
+
(**
|
|
880
|
+
* Power Focusable
|
|
881
|
+
* High-precision focus management utility with full composed tree support.
|
|
882
|
+
* Handles complex focus rules including tabindex ordering, radio groups, inert.
|
|
883
|
+
*
|
|
884
|
+
* @version 4.1.8
|
|
885
|
+
* @author Yusuke Kamiyamane
|
|
886
|
+
* @license MIT
|
|
887
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
888
|
+
* @see {@link https://github.com/y14e/power-focusable}
|
|
889
|
+
*)
|
|
890
|
+
*)
|
|
371
891
|
*/
|
|
372
892
|
|
|
373
893
|
export { Accordion as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@y14e/accordion",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "WAI-ARIA compliant accordion pattern implementation in TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
"homepage": "https://github.com/y14e/accordion#readme",
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@y14e/attributes-utils": "^1.0.5",
|
|
47
|
+
"@y14e/roving-tabindex": "^1.3.0",
|
|
47
48
|
"bun-types": "latest",
|
|
48
49
|
"tsup": "^8.0.0",
|
|
49
50
|
"typescript": "^5.6.0",
|