@y14e/accordion 1.2.5 → 1.2.7
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 +61 -13
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +61 -13
- package/package.json +4 -2
package/dist/index.cjs
CHANGED
|
@@ -1,5 +1,43 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// node_modules/@y14e/attributes-utils/dist/index.js
|
|
4
|
+
function addTokenToAttribute(element, attribute, token) {
|
|
5
|
+
const tokens = new Set(
|
|
6
|
+
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
7
|
+
);
|
|
8
|
+
tokens.add(token);
|
|
9
|
+
element.setAttribute(attribute, [...tokens].join(" "));
|
|
10
|
+
}
|
|
11
|
+
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
12
|
+
function restoreAttributes(elements) {
|
|
13
|
+
elements.forEach((element) => {
|
|
14
|
+
const snapshot = snapshots.get(element);
|
|
15
|
+
if (!snapshot) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const [attribute, value] of snapshot.entries()) {
|
|
19
|
+
if (value === null) {
|
|
20
|
+
element.removeAttribute(attribute);
|
|
21
|
+
} else {
|
|
22
|
+
element.setAttribute(attribute, value);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
snapshots.delete(element);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
function saveAttributes(elements, attributes) {
|
|
29
|
+
elements.forEach((element) => {
|
|
30
|
+
let snapshot = snapshots.get(element);
|
|
31
|
+
if (!snapshot) {
|
|
32
|
+
snapshot = /* @__PURE__ */ new Map();
|
|
33
|
+
snapshots.set(element, snapshot);
|
|
34
|
+
}
|
|
35
|
+
attributes.forEach((attribute) => {
|
|
36
|
+
snapshot.set(attribute, element.getAttribute(attribute));
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
3
41
|
// src/index.ts
|
|
4
42
|
var Accordion = class _Accordion {
|
|
5
43
|
static defaults = {};
|
|
@@ -95,6 +133,7 @@ var Accordion = class _Accordion {
|
|
|
95
133
|
});
|
|
96
134
|
this.#animationController?.abort();
|
|
97
135
|
this.#animationController = null;
|
|
136
|
+
restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
98
137
|
this.#triggerElements.length = 0;
|
|
99
138
|
this.#contentElements.length = 0;
|
|
100
139
|
this.#rootElement.removeAttribute("data-accordion-initialized");
|
|
@@ -102,12 +141,14 @@ var Accordion = class _Accordion {
|
|
|
102
141
|
#initialize() {
|
|
103
142
|
this.#eventController = new AbortController();
|
|
104
143
|
const { signal } = this.#eventController;
|
|
144
|
+
saveAttributes(this.#triggerElements, ["aria-controls", "id", "tabindex"]);
|
|
105
145
|
this.#triggerElements.forEach((trigger, i) => {
|
|
106
146
|
const id = Math.random().toString(36).slice(-8);
|
|
107
147
|
const content = this.#contentElements[i];
|
|
108
148
|
if (!content) {
|
|
109
149
|
return;
|
|
110
150
|
}
|
|
151
|
+
saveAttributes([content], ["aria-labelledby", "id", "role"]);
|
|
111
152
|
content.id ||= `accordion-content-${id}`;
|
|
112
153
|
addTokenToAttribute(trigger, "aria-controls", content.id);
|
|
113
154
|
trigger.setAttribute(
|
|
@@ -132,25 +173,26 @@ var Accordion = class _Accordion {
|
|
|
132
173
|
}
|
|
133
174
|
#onTriggerClick = (event) => {
|
|
134
175
|
event.preventDefault();
|
|
135
|
-
event.stopPropagation();
|
|
136
176
|
const trigger = event.currentTarget;
|
|
137
177
|
if (!(trigger instanceof HTMLElement)) {
|
|
138
178
|
return;
|
|
139
179
|
}
|
|
140
|
-
this.#toggle(trigger, trigger.ariaExpanded
|
|
180
|
+
this.#toggle(trigger, trigger.ariaExpanded === "false");
|
|
141
181
|
};
|
|
142
182
|
#onTriggerKeyDown = (event) => {
|
|
143
|
-
const { key } = event;
|
|
183
|
+
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
184
|
+
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
144
187
|
if (!["Enter", " ", "End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
|
|
145
188
|
return;
|
|
146
189
|
}
|
|
147
|
-
event.preventDefault();
|
|
148
|
-
event.stopPropagation();
|
|
149
190
|
const focusables = this.#triggerElements.filter(isFocusable);
|
|
150
191
|
const active = getActiveElement();
|
|
151
192
|
if (!(active instanceof HTMLElement)) {
|
|
152
193
|
return;
|
|
153
194
|
}
|
|
195
|
+
event.preventDefault();
|
|
154
196
|
const currentIndex = focusables.indexOf(active);
|
|
155
197
|
let newIndex = currentIndex;
|
|
156
198
|
switch (key) {
|
|
@@ -266,13 +308,6 @@ var Accordion = class _Accordion {
|
|
|
266
308
|
await Promise.allSettled(promises);
|
|
267
309
|
}
|
|
268
310
|
};
|
|
269
|
-
function addTokenToAttribute(element, attribute, token) {
|
|
270
|
-
const tokens = new Set(
|
|
271
|
-
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
272
|
-
);
|
|
273
|
-
tokens.add(token);
|
|
274
|
-
element.setAttribute(attribute, [...tokens].join(" "));
|
|
275
|
-
}
|
|
276
311
|
function createBinding(trigger, content) {
|
|
277
312
|
return { trigger, content, animation: null };
|
|
278
313
|
}
|
|
@@ -299,11 +334,24 @@ function waitAnimationFinish(animation) {
|
|
|
299
334
|
* Accordion
|
|
300
335
|
* WAI-ARIA compliant accordion pattern implementation in TypeScript.
|
|
301
336
|
*
|
|
302
|
-
* @version 1.2.
|
|
337
|
+
* @version 1.2.7
|
|
303
338
|
* @author Yusuke Kamiyamane
|
|
304
339
|
* @license MIT
|
|
305
340
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
306
341
|
* @see {@link https://github.com/y14e/accordion}
|
|
307
342
|
*/
|
|
343
|
+
/*! Bundled license information:
|
|
344
|
+
|
|
345
|
+
@y14e/attributes-utils/dist/index.js:
|
|
346
|
+
(**
|
|
347
|
+
* Attributes Utils
|
|
348
|
+
*
|
|
349
|
+
* @version 1.0.0
|
|
350
|
+
* @author Yusuke Kamiyamane
|
|
351
|
+
* @license MIT
|
|
352
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
353
|
+
* @see {@link https://github.com/y14e/attributes-utils}
|
|
354
|
+
*)
|
|
355
|
+
*/
|
|
308
356
|
|
|
309
357
|
module.exports = Accordion;
|
package/dist/index.d.cts
CHANGED
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,3 +1,41 @@
|
|
|
1
|
+
// node_modules/@y14e/attributes-utils/dist/index.js
|
|
2
|
+
function addTokenToAttribute(element, attribute, token) {
|
|
3
|
+
const tokens = new Set(
|
|
4
|
+
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
5
|
+
);
|
|
6
|
+
tokens.add(token);
|
|
7
|
+
element.setAttribute(attribute, [...tokens].join(" "));
|
|
8
|
+
}
|
|
9
|
+
var snapshots = /* @__PURE__ */ new WeakMap();
|
|
10
|
+
function restoreAttributes(elements) {
|
|
11
|
+
elements.forEach((element) => {
|
|
12
|
+
const snapshot = snapshots.get(element);
|
|
13
|
+
if (!snapshot) {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
for (const [attribute, value] of snapshot.entries()) {
|
|
17
|
+
if (value === null) {
|
|
18
|
+
element.removeAttribute(attribute);
|
|
19
|
+
} else {
|
|
20
|
+
element.setAttribute(attribute, value);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
snapshots.delete(element);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function saveAttributes(elements, attributes) {
|
|
27
|
+
elements.forEach((element) => {
|
|
28
|
+
let snapshot = snapshots.get(element);
|
|
29
|
+
if (!snapshot) {
|
|
30
|
+
snapshot = /* @__PURE__ */ new Map();
|
|
31
|
+
snapshots.set(element, snapshot);
|
|
32
|
+
}
|
|
33
|
+
attributes.forEach((attribute) => {
|
|
34
|
+
snapshot.set(attribute, element.getAttribute(attribute));
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
1
39
|
// src/index.ts
|
|
2
40
|
var Accordion = class _Accordion {
|
|
3
41
|
static defaults = {};
|
|
@@ -93,6 +131,7 @@ var Accordion = class _Accordion {
|
|
|
93
131
|
});
|
|
94
132
|
this.#animationController?.abort();
|
|
95
133
|
this.#animationController = null;
|
|
134
|
+
restoreAttributes([...this.#triggerElements, ...this.#contentElements]);
|
|
96
135
|
this.#triggerElements.length = 0;
|
|
97
136
|
this.#contentElements.length = 0;
|
|
98
137
|
this.#rootElement.removeAttribute("data-accordion-initialized");
|
|
@@ -100,12 +139,14 @@ var Accordion = class _Accordion {
|
|
|
100
139
|
#initialize() {
|
|
101
140
|
this.#eventController = new AbortController();
|
|
102
141
|
const { signal } = this.#eventController;
|
|
142
|
+
saveAttributes(this.#triggerElements, ["aria-controls", "id", "tabindex"]);
|
|
103
143
|
this.#triggerElements.forEach((trigger, i) => {
|
|
104
144
|
const id = Math.random().toString(36).slice(-8);
|
|
105
145
|
const content = this.#contentElements[i];
|
|
106
146
|
if (!content) {
|
|
107
147
|
return;
|
|
108
148
|
}
|
|
149
|
+
saveAttributes([content], ["aria-labelledby", "id", "role"]);
|
|
109
150
|
content.id ||= `accordion-content-${id}`;
|
|
110
151
|
addTokenToAttribute(trigger, "aria-controls", content.id);
|
|
111
152
|
trigger.setAttribute(
|
|
@@ -130,25 +171,26 @@ var Accordion = class _Accordion {
|
|
|
130
171
|
}
|
|
131
172
|
#onTriggerClick = (event) => {
|
|
132
173
|
event.preventDefault();
|
|
133
|
-
event.stopPropagation();
|
|
134
174
|
const trigger = event.currentTarget;
|
|
135
175
|
if (!(trigger instanceof HTMLElement)) {
|
|
136
176
|
return;
|
|
137
177
|
}
|
|
138
|
-
this.#toggle(trigger, trigger.ariaExpanded
|
|
178
|
+
this.#toggle(trigger, trigger.ariaExpanded === "false");
|
|
139
179
|
};
|
|
140
180
|
#onTriggerKeyDown = (event) => {
|
|
141
|
-
const { key } = event;
|
|
181
|
+
const { key, altKey, ctrlKey, metaKey, shiftKey } = event;
|
|
182
|
+
if (altKey || ctrlKey || metaKey || shiftKey) {
|
|
183
|
+
return;
|
|
184
|
+
}
|
|
142
185
|
if (!["Enter", " ", "End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
|
|
143
186
|
return;
|
|
144
187
|
}
|
|
145
|
-
event.preventDefault();
|
|
146
|
-
event.stopPropagation();
|
|
147
188
|
const focusables = this.#triggerElements.filter(isFocusable);
|
|
148
189
|
const active = getActiveElement();
|
|
149
190
|
if (!(active instanceof HTMLElement)) {
|
|
150
191
|
return;
|
|
151
192
|
}
|
|
193
|
+
event.preventDefault();
|
|
152
194
|
const currentIndex = focusables.indexOf(active);
|
|
153
195
|
let newIndex = currentIndex;
|
|
154
196
|
switch (key) {
|
|
@@ -264,13 +306,6 @@ var Accordion = class _Accordion {
|
|
|
264
306
|
await Promise.allSettled(promises);
|
|
265
307
|
}
|
|
266
308
|
};
|
|
267
|
-
function addTokenToAttribute(element, attribute, token) {
|
|
268
|
-
const tokens = new Set(
|
|
269
|
-
element.getAttribute(attribute)?.trim().split(/\s+/) ?? []
|
|
270
|
-
);
|
|
271
|
-
tokens.add(token);
|
|
272
|
-
element.setAttribute(attribute, [...tokens].join(" "));
|
|
273
|
-
}
|
|
274
309
|
function createBinding(trigger, content) {
|
|
275
310
|
return { trigger, content, animation: null };
|
|
276
311
|
}
|
|
@@ -297,11 +332,24 @@ function waitAnimationFinish(animation) {
|
|
|
297
332
|
* Accordion
|
|
298
333
|
* WAI-ARIA compliant accordion pattern implementation in TypeScript.
|
|
299
334
|
*
|
|
300
|
-
* @version 1.2.
|
|
335
|
+
* @version 1.2.7
|
|
301
336
|
* @author Yusuke Kamiyamane
|
|
302
337
|
* @license MIT
|
|
303
338
|
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
304
339
|
* @see {@link https://github.com/y14e/accordion}
|
|
305
340
|
*/
|
|
341
|
+
/*! Bundled license information:
|
|
342
|
+
|
|
343
|
+
@y14e/attributes-utils/dist/index.js:
|
|
344
|
+
(**
|
|
345
|
+
* Attributes Utils
|
|
346
|
+
*
|
|
347
|
+
* @version 1.0.0
|
|
348
|
+
* @author Yusuke Kamiyamane
|
|
349
|
+
* @license MIT
|
|
350
|
+
* @copyright Copyright (c) Yusuke Kamiyamane
|
|
351
|
+
* @see {@link https://github.com/y14e/attributes-utils}
|
|
352
|
+
*)
|
|
353
|
+
*/
|
|
306
354
|
|
|
307
355
|
export { Accordion as default };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@y14e/accordion",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.7",
|
|
4
4
|
"description": "WAI-ARIA compliant accordion pattern implementation in TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -43,9 +43,11 @@
|
|
|
43
43
|
},
|
|
44
44
|
"homepage": "https://github.com/y14e/accordion#readme",
|
|
45
45
|
"devDependencies": {
|
|
46
|
+
"@y14e/attributes-utils": "^1.0.0",
|
|
46
47
|
"bun-types": "latest",
|
|
47
48
|
"tsup": "^8.0.0",
|
|
48
|
-
"typescript": "^5.6.0"
|
|
49
|
+
"typescript": "^5.6.0",
|
|
50
|
+
"utility-types": "^3.11.0"
|
|
49
51
|
},
|
|
50
52
|
"engines": {
|
|
51
53
|
"node": ">=18"
|