@y14e/disclosure 1.2.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yusuke Kamiyamane
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,94 @@
1
+ # Disclosure
2
+
3
+ WAI-ARIA compliant [disclosure](https://www.w3.org/WAI/ARIA/apg/patterns/disclosure/) pattern implementation in TypeScript. Using the `<details>` and `<summary>` element.
4
+
5
+ ## Install
6
+
7
+ ```bash
8
+ npm i @y14e/disclosure
9
+ ```
10
+
11
+ ```ts
12
+ // npm
13
+ import Disclosure from '@y14e/disclosure';
14
+
15
+ // CDNs
16
+ import Disclosure from 'https://esm.sh/@y14e/disclosure'
17
+ // or
18
+ import Disclosure from 'https://cdn.jsdelivr.net/npm/@y14e/disclosure/+esm';
19
+ // or
20
+ import Disclosure from 'https://unpkg.com/@y14e/disclosure/dist/index.js';
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ new Disclosure(root, options);
27
+ // => Disclosure
28
+ //
29
+ // root: HTMLElement
30
+ // options (optional): DisclosureOptions
31
+
32
+ ```
33
+
34
+ ## 🪄 Options
35
+
36
+ ```ts
37
+ interface DisclosureOptions {
38
+ animation?: {
39
+ duration?: number; // ms (default: 300)
40
+ easing?: string; // <easing-function> (default: 'ease')
41
+ };
42
+ }
43
+ ```
44
+
45
+ ### ⚙️ Customize defaults
46
+
47
+ Override the global default settings applied to all disclosure instances.
48
+
49
+ ```ts
50
+ import Disclosure from '@y14e/disclosure';
51
+
52
+ Disclosure.defaults = {
53
+ animation: {
54
+ duration: 1000,
55
+ },
56
+ };
57
+
58
+ new Disclosure(root);
59
+ ```
60
+
61
+ ## 📦 APIs
62
+
63
+ ### `open`
64
+
65
+ ```ts
66
+ disclosure.open(details);
67
+ // => void
68
+ //
69
+ // details: HTMLDetailsElement
70
+ ```
71
+
72
+ ### `close`
73
+
74
+ ```ts
75
+ disclosure.close(details);
76
+ // => void
77
+ //
78
+ // details: HTMLDetailsElement
79
+ ```
80
+
81
+ ### `destroy`
82
+
83
+ Destroys the instance and cleans up all event listeners.
84
+
85
+ ```ts
86
+ disclosure.destroy(force);
87
+ // => Promise<void>
88
+ //
89
+ // force (optional): If true, skips waiting for animations to finish.
90
+ ```
91
+
92
+ ## Demo
93
+
94
+ - https://y14e.github.io/disclosure-ts/
package/dist/index.cjs ADDED
@@ -0,0 +1,311 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ var Disclosure = class _Disclosure {
5
+ static defaults = {};
6
+ #rootElement;
7
+ #defaults = {
8
+ animation: {
9
+ duration: 300,
10
+ easing: "ease"
11
+ }
12
+ };
13
+ #settings;
14
+ #detailsElements;
15
+ #summaryElements;
16
+ #contentElements;
17
+ #bindings = /* @__PURE__ */ new WeakMap();
18
+ #eventController = null;
19
+ #animationController = null;
20
+ #observers = [];
21
+ #isDestroyed = false;
22
+ constructor(root, options = {}) {
23
+ if (!(root instanceof HTMLElement)) {
24
+ throw new TypeError("Invalid root element");
25
+ }
26
+ if (root.hasAttribute("data-disclosure-initialized")) {
27
+ console.warn("Already initialized");
28
+ return;
29
+ }
30
+ this.#rootElement = root;
31
+ this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
32
+ this.#settings = this.#mergeOptions(this.#defaults, options);
33
+ matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
34
+ const NOT_NESTED = ":not(:scope summary + * *)";
35
+ this.#detailsElements = [
36
+ ...this.#rootElement.querySelectorAll(
37
+ `details${NOT_NESTED}`
38
+ )
39
+ ];
40
+ if (!this.#detailsElements.length) {
41
+ console.warn("Missing <details> elements");
42
+ return;
43
+ }
44
+ this.#summaryElements = [
45
+ ...this.#rootElement.querySelectorAll(
46
+ `summary${NOT_NESTED}`
47
+ )
48
+ ];
49
+ if (!this.#summaryElements.length) {
50
+ console.warn("Missing <summary> elements");
51
+ return;
52
+ }
53
+ this.#contentElements = [
54
+ ...this.#rootElement.querySelectorAll(
55
+ `summary${NOT_NESTED} + *`
56
+ )
57
+ ];
58
+ if (!this.#contentElements.length) {
59
+ console.warn("Missing content elements");
60
+ return;
61
+ }
62
+ this.#detailsElements.forEach((details, i) => {
63
+ const summary = this.#summaryElements[i];
64
+ const content = this.#contentElements[i];
65
+ if (!summary || !content) {
66
+ return;
67
+ }
68
+ const binding = createBinding(details, summary, content);
69
+ this.#bindings.set(details, binding);
70
+ this.#bindings.set(summary, binding);
71
+ this.#bindings.set(content, binding);
72
+ });
73
+ this.#initialize();
74
+ }
75
+ open(details) {
76
+ if (this.#isDestroyed) {
77
+ return;
78
+ }
79
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
80
+ console.warn("Invalid <details> element");
81
+ return;
82
+ }
83
+ this.#toggle(details, true);
84
+ }
85
+ close(details) {
86
+ if (this.#isDestroyed) {
87
+ return;
88
+ }
89
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
90
+ console.warn("Invalid <details> element");
91
+ return;
92
+ }
93
+ this.#toggle(details, false);
94
+ }
95
+ async destroy(force = false) {
96
+ if (this.#isDestroyed) {
97
+ return;
98
+ }
99
+ this.#isDestroyed = true;
100
+ this.#eventController?.abort();
101
+ this.#eventController = null;
102
+ this.#observers.forEach((observer) => {
103
+ observer.disconnect();
104
+ });
105
+ this.#observers.length = 0;
106
+ !force && await this.#waitAnimationsFinish();
107
+ this.#contentElements.forEach((content) => {
108
+ force && this.#bindings.get(content)?.animation?.finish();
109
+ this.#onAnimationFinish(content);
110
+ });
111
+ this.#animationController?.abort();
112
+ this.#animationController = null;
113
+ this.#detailsElements.forEach((details) => {
114
+ details.removeAttribute("data-disclosure-name");
115
+ details.removeAttribute("data-disclosure-open");
116
+ });
117
+ this.#detailsElements.length = 0;
118
+ this.#summaryElements.length = 0;
119
+ this.#contentElements.length = 0;
120
+ this.#rootElement.removeAttribute("data-disclosure-initialized");
121
+ }
122
+ #initialize() {
123
+ this.#eventController = new AbortController();
124
+ const { signal } = this.#eventController;
125
+ this.#detailsElements.forEach((details, i) => {
126
+ details.name && details.setAttribute("data-disclosure-name", details.name);
127
+ function sync() {
128
+ details.toggleAttribute("data-disclosure-open", details.open);
129
+ }
130
+ const observer = new MutationObserver(sync);
131
+ observer.observe(details, { attributeFilter: ["open"] });
132
+ this.#observers.push(observer);
133
+ sync();
134
+ const summary = this.#summaryElements[i];
135
+ if (!summary) {
136
+ return;
137
+ }
138
+ if (!isFocusable(summary)) {
139
+ summary.setAttribute("aria-disabled", "true");
140
+ summary.setAttribute("tabindex", "-1");
141
+ summary.style.setProperty("pointer-events", "none");
142
+ }
143
+ summary.addEventListener("click", this.#onSummaryClick, { signal });
144
+ summary.addEventListener("keydown", this.#onSummaryKeyDown, { signal });
145
+ });
146
+ this.#rootElement.setAttribute("data-disclosure-initialized", "");
147
+ }
148
+ #onSummaryClick = (event) => {
149
+ event.preventDefault();
150
+ event.stopPropagation();
151
+ const summary = event.currentTarget;
152
+ if (!(summary instanceof HTMLElement)) {
153
+ return;
154
+ }
155
+ const binding = this.#bindings.get(summary);
156
+ if (!binding) {
157
+ return;
158
+ }
159
+ const { details } = binding;
160
+ this.#toggle(details, !details.hasAttribute("data-disclosure-open"));
161
+ };
162
+ #onSummaryKeyDown = (event) => {
163
+ const { key } = event;
164
+ if (!["End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
165
+ return;
166
+ }
167
+ event.preventDefault();
168
+ event.stopPropagation();
169
+ const focusables = this.#summaryElements.filter(isFocusable);
170
+ const active = getActiveElement();
171
+ if (!(active instanceof HTMLElement)) {
172
+ return;
173
+ }
174
+ const currentIndex = focusables.indexOf(active);
175
+ let newIndex = currentIndex;
176
+ switch (key) {
177
+ case "End":
178
+ newIndex = -1;
179
+ break;
180
+ case "Home":
181
+ newIndex = 0;
182
+ break;
183
+ case "ArrowUp":
184
+ newIndex = currentIndex - 1;
185
+ break;
186
+ case "ArrowDown":
187
+ newIndex = (currentIndex + 1) % focusables.length;
188
+ break;
189
+ }
190
+ focusables.at(newIndex)?.focus();
191
+ };
192
+ #toggle(details, isOpen) {
193
+ if (details.hasAttribute("data-disclosure-open") === isOpen) {
194
+ return;
195
+ }
196
+ const name = details.getAttribute("data-disclosure-name");
197
+ if (name && isOpen) {
198
+ details.removeAttribute("name");
199
+ const opened = this.#detailsElements.find(
200
+ (d) => d.hasAttribute("data-disclosure-open") && d.getAttribute("data-disclosure-name") === name
201
+ );
202
+ opened && this.close(opened);
203
+ }
204
+ const binding = this.#bindings.get(details);
205
+ if (!binding) {
206
+ return;
207
+ }
208
+ const { content, timer } = binding;
209
+ const startSize = details.open ? content.offsetHeight : 0;
210
+ binding.animation?.cancel();
211
+ if (isOpen) {
212
+ details.open = true;
213
+ }
214
+ const endSize = isOpen ? content.scrollHeight : 0;
215
+ binding.animation?.cancel();
216
+ timer && cancelAnimationFrame(timer);
217
+ binding.timer = requestAnimationFrame(() => {
218
+ binding.timer = void 0;
219
+ details.toggleAttribute("data-disclosure-open", isOpen);
220
+ });
221
+ content.style.setProperty("overflow", "clip");
222
+ const { duration, easing } = this.#settings.animation;
223
+ const animation = content.animate(
224
+ { blockSize: [`${startSize}px`, `${endSize}px`] },
225
+ { duration, easing }
226
+ );
227
+ binding.animation = animation;
228
+ function cleanup() {
229
+ if (binding?.animation === animation) {
230
+ binding.animation = null;
231
+ }
232
+ }
233
+ this.#animationController = new AbortController();
234
+ const { signal } = this.#animationController;
235
+ animation.addEventListener("cancel", cleanup, { once: true, signal });
236
+ animation.addEventListener(
237
+ "finish",
238
+ () => {
239
+ this.#onAnimationFinish(content);
240
+ cleanup();
241
+ },
242
+ { once: true, signal }
243
+ );
244
+ }
245
+ #mergeOptions(target, source) {
246
+ return {
247
+ animation: { ...target.animation, ...source.animation ?? {} }
248
+ };
249
+ }
250
+ #onAnimationFinish(content) {
251
+ const binding = this.#bindings.get(content);
252
+ if (!binding) {
253
+ return;
254
+ }
255
+ const details = binding.details;
256
+ if (!details) {
257
+ return;
258
+ }
259
+ const name = details.getAttribute("data-disclosure-name");
260
+ name && details.setAttribute("name", name);
261
+ if (!details.hasAttribute("data-disclosure-open")) {
262
+ details.open = false;
263
+ }
264
+ const { style } = content;
265
+ style.removeProperty("block-size");
266
+ style.removeProperty("overflow");
267
+ }
268
+ async #waitAnimationsFinish() {
269
+ const promises = [];
270
+ this.#contentElements.forEach((content) => {
271
+ const animation = this.#bindings.get(content)?.animation;
272
+ animation && promises.push(waitAnimationFinish(animation));
273
+ });
274
+ await Promise.allSettled(promises);
275
+ }
276
+ };
277
+ function createBinding(details, summary, content) {
278
+ return { details, summary, content, timer: void 0, animation: null };
279
+ }
280
+ function getActiveElement() {
281
+ let current = document.activeElement;
282
+ while (current?.shadowRoot?.activeElement) {
283
+ current = current.shadowRoot.activeElement;
284
+ }
285
+ return current;
286
+ }
287
+ function isFocusable(element) {
288
+ return element.tabIndex >= 0;
289
+ }
290
+ function waitAnimationFinish(animation) {
291
+ const { playState } = animation;
292
+ if (playState === "idle" || playState === "finished") {
293
+ return Promise.resolve();
294
+ }
295
+ return new Promise(
296
+ (resolve) => animation.addEventListener("finish", () => resolve(), { once: true })
297
+ );
298
+ }
299
+ /**
300
+ * disclosure.ts
301
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
302
+ * Using the <details> and <summary> element.
303
+ *
304
+ * @version 1.2.4
305
+ * @author Yusuke Kamiyamane
306
+ * @license MIT
307
+ * @copyright Copyright (c) Yusuke Kamiyamane
308
+ * @see {@link https://github.com/y14e/disclosure-ts}
309
+ */
310
+
311
+ module.exports = Disclosure;
@@ -0,0 +1,27 @@
1
+ /**
2
+ * disclosure.ts
3
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
+ * Using the <details> and <summary> element.
5
+ *
6
+ * @version 1.2.4
7
+ * @author Yusuke Kamiyamane
8
+ * @license MIT
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
+ * @see {@link https://github.com/y14e/disclosure-ts}
11
+ */
12
+ interface DisclosureOptions {
13
+ readonly animation?: {
14
+ readonly duration?: number;
15
+ readonly easing?: string;
16
+ };
17
+ }
18
+ declare class Disclosure {
19
+ #private;
20
+ static defaults: DisclosureOptions;
21
+ constructor(root: HTMLElement, options?: DisclosureOptions);
22
+ open(details: HTMLDetailsElement): void;
23
+ close(details: HTMLDetailsElement): void;
24
+ destroy(force?: boolean): Promise<void>;
25
+ }
26
+
27
+ export { type DisclosureOptions, Disclosure as default };
@@ -0,0 +1,27 @@
1
+ /**
2
+ * disclosure.ts
3
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
+ * Using the <details> and <summary> element.
5
+ *
6
+ * @version 1.2.4
7
+ * @author Yusuke Kamiyamane
8
+ * @license MIT
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
+ * @see {@link https://github.com/y14e/disclosure-ts}
11
+ */
12
+ interface DisclosureOptions {
13
+ readonly animation?: {
14
+ readonly duration?: number;
15
+ readonly easing?: string;
16
+ };
17
+ }
18
+ declare class Disclosure {
19
+ #private;
20
+ static defaults: DisclosureOptions;
21
+ constructor(root: HTMLElement, options?: DisclosureOptions);
22
+ open(details: HTMLDetailsElement): void;
23
+ close(details: HTMLDetailsElement): void;
24
+ destroy(force?: boolean): Promise<void>;
25
+ }
26
+
27
+ export { type DisclosureOptions, Disclosure as default };
package/dist/index.js ADDED
@@ -0,0 +1,309 @@
1
+ // src/index.ts
2
+ var Disclosure = class _Disclosure {
3
+ static defaults = {};
4
+ #rootElement;
5
+ #defaults = {
6
+ animation: {
7
+ duration: 300,
8
+ easing: "ease"
9
+ }
10
+ };
11
+ #settings;
12
+ #detailsElements;
13
+ #summaryElements;
14
+ #contentElements;
15
+ #bindings = /* @__PURE__ */ new WeakMap();
16
+ #eventController = null;
17
+ #animationController = null;
18
+ #observers = [];
19
+ #isDestroyed = false;
20
+ constructor(root, options = {}) {
21
+ if (!(root instanceof HTMLElement)) {
22
+ throw new TypeError("Invalid root element");
23
+ }
24
+ if (root.hasAttribute("data-disclosure-initialized")) {
25
+ console.warn("Already initialized");
26
+ return;
27
+ }
28
+ this.#rootElement = root;
29
+ this.#defaults = this.#mergeOptions(this.#defaults, _Disclosure.defaults);
30
+ this.#settings = this.#mergeOptions(this.#defaults, options);
31
+ matchMedia("(prefers-reduced-motion: reduce)").matches && Object.assign(this.#settings.animation, { duration: 0 });
32
+ const NOT_NESTED = ":not(:scope summary + * *)";
33
+ this.#detailsElements = [
34
+ ...this.#rootElement.querySelectorAll(
35
+ `details${NOT_NESTED}`
36
+ )
37
+ ];
38
+ if (!this.#detailsElements.length) {
39
+ console.warn("Missing <details> elements");
40
+ return;
41
+ }
42
+ this.#summaryElements = [
43
+ ...this.#rootElement.querySelectorAll(
44
+ `summary${NOT_NESTED}`
45
+ )
46
+ ];
47
+ if (!this.#summaryElements.length) {
48
+ console.warn("Missing <summary> elements");
49
+ return;
50
+ }
51
+ this.#contentElements = [
52
+ ...this.#rootElement.querySelectorAll(
53
+ `summary${NOT_NESTED} + *`
54
+ )
55
+ ];
56
+ if (!this.#contentElements.length) {
57
+ console.warn("Missing content elements");
58
+ return;
59
+ }
60
+ this.#detailsElements.forEach((details, i) => {
61
+ const summary = this.#summaryElements[i];
62
+ const content = this.#contentElements[i];
63
+ if (!summary || !content) {
64
+ return;
65
+ }
66
+ const binding = createBinding(details, summary, content);
67
+ this.#bindings.set(details, binding);
68
+ this.#bindings.set(summary, binding);
69
+ this.#bindings.set(content, binding);
70
+ });
71
+ this.#initialize();
72
+ }
73
+ open(details) {
74
+ if (this.#isDestroyed) {
75
+ return;
76
+ }
77
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
78
+ console.warn("Invalid <details> element");
79
+ return;
80
+ }
81
+ this.#toggle(details, true);
82
+ }
83
+ close(details) {
84
+ if (this.#isDestroyed) {
85
+ return;
86
+ }
87
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
88
+ console.warn("Invalid <details> element");
89
+ return;
90
+ }
91
+ this.#toggle(details, false);
92
+ }
93
+ async destroy(force = false) {
94
+ if (this.#isDestroyed) {
95
+ return;
96
+ }
97
+ this.#isDestroyed = true;
98
+ this.#eventController?.abort();
99
+ this.#eventController = null;
100
+ this.#observers.forEach((observer) => {
101
+ observer.disconnect();
102
+ });
103
+ this.#observers.length = 0;
104
+ !force && await this.#waitAnimationsFinish();
105
+ this.#contentElements.forEach((content) => {
106
+ force && this.#bindings.get(content)?.animation?.finish();
107
+ this.#onAnimationFinish(content);
108
+ });
109
+ this.#animationController?.abort();
110
+ this.#animationController = null;
111
+ this.#detailsElements.forEach((details) => {
112
+ details.removeAttribute("data-disclosure-name");
113
+ details.removeAttribute("data-disclosure-open");
114
+ });
115
+ this.#detailsElements.length = 0;
116
+ this.#summaryElements.length = 0;
117
+ this.#contentElements.length = 0;
118
+ this.#rootElement.removeAttribute("data-disclosure-initialized");
119
+ }
120
+ #initialize() {
121
+ this.#eventController = new AbortController();
122
+ const { signal } = this.#eventController;
123
+ this.#detailsElements.forEach((details, i) => {
124
+ details.name && details.setAttribute("data-disclosure-name", details.name);
125
+ function sync() {
126
+ details.toggleAttribute("data-disclosure-open", details.open);
127
+ }
128
+ const observer = new MutationObserver(sync);
129
+ observer.observe(details, { attributeFilter: ["open"] });
130
+ this.#observers.push(observer);
131
+ sync();
132
+ const summary = this.#summaryElements[i];
133
+ if (!summary) {
134
+ return;
135
+ }
136
+ if (!isFocusable(summary)) {
137
+ summary.setAttribute("aria-disabled", "true");
138
+ summary.setAttribute("tabindex", "-1");
139
+ summary.style.setProperty("pointer-events", "none");
140
+ }
141
+ summary.addEventListener("click", this.#onSummaryClick, { signal });
142
+ summary.addEventListener("keydown", this.#onSummaryKeyDown, { signal });
143
+ });
144
+ this.#rootElement.setAttribute("data-disclosure-initialized", "");
145
+ }
146
+ #onSummaryClick = (event) => {
147
+ event.preventDefault();
148
+ event.stopPropagation();
149
+ const summary = event.currentTarget;
150
+ if (!(summary instanceof HTMLElement)) {
151
+ return;
152
+ }
153
+ const binding = this.#bindings.get(summary);
154
+ if (!binding) {
155
+ return;
156
+ }
157
+ const { details } = binding;
158
+ this.#toggle(details, !details.hasAttribute("data-disclosure-open"));
159
+ };
160
+ #onSummaryKeyDown = (event) => {
161
+ const { key } = event;
162
+ if (!["End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
163
+ return;
164
+ }
165
+ event.preventDefault();
166
+ event.stopPropagation();
167
+ const focusables = this.#summaryElements.filter(isFocusable);
168
+ const active = getActiveElement();
169
+ if (!(active instanceof HTMLElement)) {
170
+ return;
171
+ }
172
+ const currentIndex = focusables.indexOf(active);
173
+ let newIndex = currentIndex;
174
+ switch (key) {
175
+ case "End":
176
+ newIndex = -1;
177
+ break;
178
+ case "Home":
179
+ newIndex = 0;
180
+ break;
181
+ case "ArrowUp":
182
+ newIndex = currentIndex - 1;
183
+ break;
184
+ case "ArrowDown":
185
+ newIndex = (currentIndex + 1) % focusables.length;
186
+ break;
187
+ }
188
+ focusables.at(newIndex)?.focus();
189
+ };
190
+ #toggle(details, isOpen) {
191
+ if (details.hasAttribute("data-disclosure-open") === isOpen) {
192
+ return;
193
+ }
194
+ const name = details.getAttribute("data-disclosure-name");
195
+ if (name && isOpen) {
196
+ details.removeAttribute("name");
197
+ const opened = this.#detailsElements.find(
198
+ (d) => d.hasAttribute("data-disclosure-open") && d.getAttribute("data-disclosure-name") === name
199
+ );
200
+ opened && this.close(opened);
201
+ }
202
+ const binding = this.#bindings.get(details);
203
+ if (!binding) {
204
+ return;
205
+ }
206
+ const { content, timer } = binding;
207
+ const startSize = details.open ? content.offsetHeight : 0;
208
+ binding.animation?.cancel();
209
+ if (isOpen) {
210
+ details.open = true;
211
+ }
212
+ const endSize = isOpen ? content.scrollHeight : 0;
213
+ binding.animation?.cancel();
214
+ timer && cancelAnimationFrame(timer);
215
+ binding.timer = requestAnimationFrame(() => {
216
+ binding.timer = void 0;
217
+ details.toggleAttribute("data-disclosure-open", isOpen);
218
+ });
219
+ content.style.setProperty("overflow", "clip");
220
+ const { duration, easing } = this.#settings.animation;
221
+ const animation = content.animate(
222
+ { blockSize: [`${startSize}px`, `${endSize}px`] },
223
+ { duration, easing }
224
+ );
225
+ binding.animation = animation;
226
+ function cleanup() {
227
+ if (binding?.animation === animation) {
228
+ binding.animation = null;
229
+ }
230
+ }
231
+ this.#animationController = new AbortController();
232
+ const { signal } = this.#animationController;
233
+ animation.addEventListener("cancel", cleanup, { once: true, signal });
234
+ animation.addEventListener(
235
+ "finish",
236
+ () => {
237
+ this.#onAnimationFinish(content);
238
+ cleanup();
239
+ },
240
+ { once: true, signal }
241
+ );
242
+ }
243
+ #mergeOptions(target, source) {
244
+ return {
245
+ animation: { ...target.animation, ...source.animation ?? {} }
246
+ };
247
+ }
248
+ #onAnimationFinish(content) {
249
+ const binding = this.#bindings.get(content);
250
+ if (!binding) {
251
+ return;
252
+ }
253
+ const details = binding.details;
254
+ if (!details) {
255
+ return;
256
+ }
257
+ const name = details.getAttribute("data-disclosure-name");
258
+ name && details.setAttribute("name", name);
259
+ if (!details.hasAttribute("data-disclosure-open")) {
260
+ details.open = false;
261
+ }
262
+ const { style } = content;
263
+ style.removeProperty("block-size");
264
+ style.removeProperty("overflow");
265
+ }
266
+ async #waitAnimationsFinish() {
267
+ const promises = [];
268
+ this.#contentElements.forEach((content) => {
269
+ const animation = this.#bindings.get(content)?.animation;
270
+ animation && promises.push(waitAnimationFinish(animation));
271
+ });
272
+ await Promise.allSettled(promises);
273
+ }
274
+ };
275
+ function createBinding(details, summary, content) {
276
+ return { details, summary, content, timer: void 0, animation: null };
277
+ }
278
+ function getActiveElement() {
279
+ let current = document.activeElement;
280
+ while (current?.shadowRoot?.activeElement) {
281
+ current = current.shadowRoot.activeElement;
282
+ }
283
+ return current;
284
+ }
285
+ function isFocusable(element) {
286
+ return element.tabIndex >= 0;
287
+ }
288
+ function waitAnimationFinish(animation) {
289
+ const { playState } = animation;
290
+ if (playState === "idle" || playState === "finished") {
291
+ return Promise.resolve();
292
+ }
293
+ return new Promise(
294
+ (resolve) => animation.addEventListener("finish", () => resolve(), { once: true })
295
+ );
296
+ }
297
+ /**
298
+ * disclosure.ts
299
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
300
+ * Using the <details> and <summary> element.
301
+ *
302
+ * @version 1.2.4
303
+ * @author Yusuke Kamiyamane
304
+ * @license MIT
305
+ * @copyright Copyright (c) Yusuke Kamiyamane
306
+ * @see {@link https://github.com/y14e/disclosure-ts}
307
+ */
308
+
309
+ export { Disclosure as default };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@y14e/disclosure",
3
+ "version": "1.2.4",
4
+ "description": "WAI-ARIA compliant disclosure pattern implementation in TypeScript",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "types": "./dist/index.d.ts",
12
+ "import": "./dist/index.js",
13
+ "require": "./dist/index.cjs"
14
+ }
15
+ },
16
+ "files": [
17
+ "dist",
18
+ "LICENSE",
19
+ "README.md"
20
+ ],
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "prepublishOnly": "npm run build",
24
+ "lint": "tsc --noEmit"
25
+ },
26
+ "keywords": [
27
+ "a11y",
28
+ "accessibility",
29
+ "disclosure",
30
+ "collapsible",
31
+ "component",
32
+ "typescript",
33
+ "utility"
34
+ ],
35
+ "author": "Yusuke Kamiyamane",
36
+ "license": "MIT",
37
+ "repository": {
38
+ "type": "git",
39
+ "url": "git+https://github.com/y14e/disclosure.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/y14e/disclosure/issues"
43
+ },
44
+ "homepage": "https://github.com/y14e/disclosure#readme",
45
+ "devDependencies": {
46
+ "bun-types": "latest",
47
+ "tsup": "^8.0.0",
48
+ "typescript": "^5.6.0"
49
+ },
50
+ "engines": {
51
+ "node": ">=18"
52
+ }
53
+ }