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