@y14e/disclosure-css 1.2.2

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,62 @@
1
+ # Disclosure (CSS)
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-css
9
+ ```
10
+
11
+ ```ts
12
+ // npm
13
+ import Disclosure from '@y14e/disclosure-css';
14
+
15
+ // CDNs
16
+ import Disclosure from 'https://esm.sh/@y14e/disclosure-css'
17
+ // or
18
+ import Disclosure from 'https://cdn.jsdelivr.net/npm/@y14e/disclosure-css/+esm';
19
+ // or
20
+ import Disclosure from 'https://unpkg.com/@y14e/disclosure-css/dist/index.js';
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ ```ts
26
+ new Disclosure(root);
27
+ // => Disclosure
28
+
29
+ ```
30
+
31
+ ## 📦 APIs
32
+
33
+ ### `open`
34
+
35
+ ```ts
36
+ disclosure.open(details);
37
+ // => void
38
+ //
39
+ // details: HTMLDetailsElement
40
+ ```
41
+
42
+ ### `close`
43
+
44
+ ```ts
45
+ disclosure.close(details);
46
+ // => void
47
+ //
48
+ // details: HTMLDetailsElement
49
+ ```
50
+
51
+ ### `destroy`
52
+
53
+ Destroys the instance and cleans up all event listeners.
54
+
55
+ ```ts
56
+ disclosure.destroy();
57
+ // => void
58
+ ```
59
+
60
+ ## Demo
61
+
62
+ - https://y14e.github.io/disclosure-css/
package/dist/index.cjs ADDED
@@ -0,0 +1,172 @@
1
+ 'use strict';
2
+
3
+ // src/index.ts
4
+ var Disclosure = class {
5
+ #rootElement;
6
+ #detailsElements;
7
+ #summaryElements;
8
+ #contentElements;
9
+ #bindings = /* @__PURE__ */ new WeakMap();
10
+ #controller = null;
11
+ #isDestroyed = false;
12
+ constructor(root) {
13
+ if (!(root instanceof HTMLElement)) {
14
+ throw new TypeError("Invalid root element");
15
+ }
16
+ if (root.hasAttribute("data-disclosure-initialized")) {
17
+ console.warn("Already initialized");
18
+ return;
19
+ }
20
+ this.#rootElement = root;
21
+ const NOT_NESTED = ":not(:scope summary + * *)";
22
+ this.#detailsElements = [
23
+ ...this.#rootElement.querySelectorAll(
24
+ `details${NOT_NESTED}`
25
+ )
26
+ ];
27
+ if (!this.#detailsElements.length) {
28
+ console.warn("Missing <details> elements");
29
+ return;
30
+ }
31
+ this.#summaryElements = [
32
+ ...this.#rootElement.querySelectorAll(
33
+ `summary${NOT_NESTED}`
34
+ )
35
+ ];
36
+ if (!this.#summaryElements.length) {
37
+ console.warn("Missing <summary> elements");
38
+ return;
39
+ }
40
+ this.#contentElements = [
41
+ ...this.#rootElement.querySelectorAll(
42
+ `summary${NOT_NESTED} + *`
43
+ )
44
+ ];
45
+ if (!this.#contentElements.length) {
46
+ console.warn("Missing content elements");
47
+ return;
48
+ }
49
+ this.#detailsElements.forEach((details, i) => {
50
+ const summary = this.#summaryElements[i];
51
+ const content = this.#contentElements[i];
52
+ if (!summary || !content) {
53
+ return;
54
+ }
55
+ const binding = createBinding(details, summary, content);
56
+ this.#bindings.set(details, binding);
57
+ this.#bindings.set(summary, binding);
58
+ this.#bindings.set(content, binding);
59
+ });
60
+ this.#initialize();
61
+ }
62
+ open(details) {
63
+ if (this.#isDestroyed) {
64
+ return;
65
+ }
66
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
67
+ console.warn("Invalid <details> element");
68
+ return;
69
+ }
70
+ this.#toggle(details, true);
71
+ }
72
+ close(details) {
73
+ if (this.#isDestroyed) {
74
+ return;
75
+ }
76
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
77
+ console.warn("Invalid <details> element");
78
+ return;
79
+ }
80
+ this.#toggle(details, false);
81
+ }
82
+ destroy() {
83
+ if (this.#isDestroyed) {
84
+ return;
85
+ }
86
+ this.#isDestroyed = true;
87
+ this.#controller?.abort();
88
+ this.#controller = null;
89
+ this.#detailsElements.length = 0;
90
+ this.#summaryElements.length = 0;
91
+ this.#contentElements.length = 0;
92
+ this.#rootElement.removeAttribute("data-disclosure-initialized");
93
+ }
94
+ #initialize() {
95
+ this.#controller = new AbortController();
96
+ const { signal } = this.#controller;
97
+ this.#detailsElements.forEach((details, i) => {
98
+ const summary = this.#summaryElements[i];
99
+ if (!summary) {
100
+ return;
101
+ }
102
+ if (!isFocusable(details)) {
103
+ summary.setAttribute("aria-disabled", "true");
104
+ summary.setAttribute("tabindex", "-1");
105
+ summary.style.setProperty("pointer-events", "none");
106
+ }
107
+ summary.addEventListener("keydown", this.#onSummaryKeyDown, { signal });
108
+ });
109
+ this.#rootElement.setAttribute("data-disclosure-initialized", "");
110
+ }
111
+ #onSummaryKeyDown = (event) => {
112
+ const { key } = event;
113
+ if (!["End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
114
+ return;
115
+ }
116
+ event.preventDefault();
117
+ event.stopPropagation();
118
+ const focusables = this.#summaryElements.filter(isFocusable);
119
+ const active = getActiveElement();
120
+ if (!(active instanceof HTMLElement)) {
121
+ return;
122
+ }
123
+ const currentIndex = focusables.indexOf(active);
124
+ let newIndex = currentIndex;
125
+ switch (key) {
126
+ case "End":
127
+ newIndex = -1;
128
+ break;
129
+ case "Home":
130
+ newIndex = 0;
131
+ break;
132
+ case "ArrowUp":
133
+ newIndex = currentIndex - 1;
134
+ break;
135
+ case "ArrowDown":
136
+ newIndex = (currentIndex + 1) % focusables.length;
137
+ break;
138
+ }
139
+ focusables.at(newIndex)?.focus();
140
+ };
141
+ #toggle(details, isOpen) {
142
+ if (details.open !== isOpen) {
143
+ details.open = isOpen;
144
+ }
145
+ }
146
+ };
147
+ function createBinding(details, summary, content) {
148
+ return { details, summary, content };
149
+ }
150
+ function getActiveElement() {
151
+ let current = document.activeElement;
152
+ while (current?.shadowRoot?.activeElement) {
153
+ current = current.shadowRoot.activeElement;
154
+ }
155
+ return current;
156
+ }
157
+ function isFocusable(element) {
158
+ return element.tabIndex >= 0;
159
+ }
160
+ /**
161
+ * Disclosure (CSS)
162
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
163
+ * Using the <details> and <summary> element.
164
+ *
165
+ * @version 1.2.2
166
+ * @author Yusuke Kamiyamane
167
+ * @license MIT
168
+ * @copyright Copyright (c) Yusuke Kamiyamane
169
+ * @see {@link https://github.com/y14e/disclosure-css}
170
+ */
171
+
172
+ module.exports = Disclosure;
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Disclosure (CSS)
3
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
+ * Using the <details> and <summary> element.
5
+ *
6
+ * @version 1.2.2
7
+ * @author Yusuke Kamiyamane
8
+ * @license MIT
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
+ * @see {@link https://github.com/y14e/disclosure-css}
11
+ */
12
+ declare class Disclosure {
13
+ #private;
14
+ constructor(root: HTMLElement);
15
+ open(details: HTMLDetailsElement): void;
16
+ close(details: HTMLDetailsElement): void;
17
+ destroy(): void;
18
+ }
19
+
20
+ export { Disclosure as default };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Disclosure (CSS)
3
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
4
+ * Using the <details> and <summary> element.
5
+ *
6
+ * @version 1.2.2
7
+ * @author Yusuke Kamiyamane
8
+ * @license MIT
9
+ * @copyright Copyright (c) Yusuke Kamiyamane
10
+ * @see {@link https://github.com/y14e/disclosure-css}
11
+ */
12
+ declare class Disclosure {
13
+ #private;
14
+ constructor(root: HTMLElement);
15
+ open(details: HTMLDetailsElement): void;
16
+ close(details: HTMLDetailsElement): void;
17
+ destroy(): void;
18
+ }
19
+
20
+ export { Disclosure as default };
package/dist/index.js ADDED
@@ -0,0 +1,170 @@
1
+ // src/index.ts
2
+ var Disclosure = class {
3
+ #rootElement;
4
+ #detailsElements;
5
+ #summaryElements;
6
+ #contentElements;
7
+ #bindings = /* @__PURE__ */ new WeakMap();
8
+ #controller = null;
9
+ #isDestroyed = false;
10
+ constructor(root) {
11
+ if (!(root instanceof HTMLElement)) {
12
+ throw new TypeError("Invalid root element");
13
+ }
14
+ if (root.hasAttribute("data-disclosure-initialized")) {
15
+ console.warn("Already initialized");
16
+ return;
17
+ }
18
+ this.#rootElement = root;
19
+ const NOT_NESTED = ":not(:scope summary + * *)";
20
+ this.#detailsElements = [
21
+ ...this.#rootElement.querySelectorAll(
22
+ `details${NOT_NESTED}`
23
+ )
24
+ ];
25
+ if (!this.#detailsElements.length) {
26
+ console.warn("Missing <details> elements");
27
+ return;
28
+ }
29
+ this.#summaryElements = [
30
+ ...this.#rootElement.querySelectorAll(
31
+ `summary${NOT_NESTED}`
32
+ )
33
+ ];
34
+ if (!this.#summaryElements.length) {
35
+ console.warn("Missing <summary> elements");
36
+ return;
37
+ }
38
+ this.#contentElements = [
39
+ ...this.#rootElement.querySelectorAll(
40
+ `summary${NOT_NESTED} + *`
41
+ )
42
+ ];
43
+ if (!this.#contentElements.length) {
44
+ console.warn("Missing content elements");
45
+ return;
46
+ }
47
+ this.#detailsElements.forEach((details, i) => {
48
+ const summary = this.#summaryElements[i];
49
+ const content = this.#contentElements[i];
50
+ if (!summary || !content) {
51
+ return;
52
+ }
53
+ const binding = createBinding(details, summary, content);
54
+ this.#bindings.set(details, binding);
55
+ this.#bindings.set(summary, binding);
56
+ this.#bindings.set(content, binding);
57
+ });
58
+ this.#initialize();
59
+ }
60
+ open(details) {
61
+ if (this.#isDestroyed) {
62
+ return;
63
+ }
64
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
65
+ console.warn("Invalid <details> element");
66
+ return;
67
+ }
68
+ this.#toggle(details, true);
69
+ }
70
+ close(details) {
71
+ if (this.#isDestroyed) {
72
+ return;
73
+ }
74
+ if (!(details instanceof HTMLDetailsElement) || !this.#bindings.has(details)) {
75
+ console.warn("Invalid <details> element");
76
+ return;
77
+ }
78
+ this.#toggle(details, false);
79
+ }
80
+ destroy() {
81
+ if (this.#isDestroyed) {
82
+ return;
83
+ }
84
+ this.#isDestroyed = true;
85
+ this.#controller?.abort();
86
+ this.#controller = null;
87
+ this.#detailsElements.length = 0;
88
+ this.#summaryElements.length = 0;
89
+ this.#contentElements.length = 0;
90
+ this.#rootElement.removeAttribute("data-disclosure-initialized");
91
+ }
92
+ #initialize() {
93
+ this.#controller = new AbortController();
94
+ const { signal } = this.#controller;
95
+ this.#detailsElements.forEach((details, i) => {
96
+ const summary = this.#summaryElements[i];
97
+ if (!summary) {
98
+ return;
99
+ }
100
+ if (!isFocusable(details)) {
101
+ summary.setAttribute("aria-disabled", "true");
102
+ summary.setAttribute("tabindex", "-1");
103
+ summary.style.setProperty("pointer-events", "none");
104
+ }
105
+ summary.addEventListener("keydown", this.#onSummaryKeyDown, { signal });
106
+ });
107
+ this.#rootElement.setAttribute("data-disclosure-initialized", "");
108
+ }
109
+ #onSummaryKeyDown = (event) => {
110
+ const { key } = event;
111
+ if (!["End", "Home", "ArrowUp", "ArrowDown"].includes(key)) {
112
+ return;
113
+ }
114
+ event.preventDefault();
115
+ event.stopPropagation();
116
+ const focusables = this.#summaryElements.filter(isFocusable);
117
+ const active = getActiveElement();
118
+ if (!(active instanceof HTMLElement)) {
119
+ return;
120
+ }
121
+ const currentIndex = focusables.indexOf(active);
122
+ let newIndex = currentIndex;
123
+ switch (key) {
124
+ case "End":
125
+ newIndex = -1;
126
+ break;
127
+ case "Home":
128
+ newIndex = 0;
129
+ break;
130
+ case "ArrowUp":
131
+ newIndex = currentIndex - 1;
132
+ break;
133
+ case "ArrowDown":
134
+ newIndex = (currentIndex + 1) % focusables.length;
135
+ break;
136
+ }
137
+ focusables.at(newIndex)?.focus();
138
+ };
139
+ #toggle(details, isOpen) {
140
+ if (details.open !== isOpen) {
141
+ details.open = isOpen;
142
+ }
143
+ }
144
+ };
145
+ function createBinding(details, summary, content) {
146
+ return { details, summary, content };
147
+ }
148
+ function getActiveElement() {
149
+ let current = document.activeElement;
150
+ while (current?.shadowRoot?.activeElement) {
151
+ current = current.shadowRoot.activeElement;
152
+ }
153
+ return current;
154
+ }
155
+ function isFocusable(element) {
156
+ return element.tabIndex >= 0;
157
+ }
158
+ /**
159
+ * Disclosure (CSS)
160
+ * WAI-ARIA compliant disclosure pattern implementation in TypeScript.
161
+ * Using the <details> and <summary> element.
162
+ *
163
+ * @version 1.2.2
164
+ * @author Yusuke Kamiyamane
165
+ * @license MIT
166
+ * @copyright Copyright (c) Yusuke Kamiyamane
167
+ * @see {@link https://github.com/y14e/disclosure-css}
168
+ */
169
+
170
+ export { Disclosure as default };
package/package.json ADDED
@@ -0,0 +1,53 @@
1
+ {
2
+ "name": "@y14e/disclosure-css",
3
+ "version": "1.2.2",
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-css.git"
40
+ },
41
+ "bugs": {
42
+ "url": "https://github.com/y14e/disclosure-css/issues"
43
+ },
44
+ "homepage": "https://github.com/y14e/disclosure-css#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
+ }