@magic-spells/collapsible-content 0.1.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Magic Spells (Cory Schulz)
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.
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ class CollapsibleComponent extends HTMLElement {
4
+ #handleClick;
5
+
6
+ constructor() {
7
+ super();
8
+ this.#handleClick = () => {
9
+ const button = this.querySelector('button');
10
+ const content = this.querySelector('collapsible-content');
11
+ const expanded =
12
+ button.getAttribute('aria-expanded') !== 'true';
13
+ button.setAttribute('aria-expanded', expanded);
14
+ content.hidden = !expanded;
15
+ };
16
+ }
17
+
18
+ connectedCallback() {
19
+ const button = this.querySelector('button');
20
+ const content = this.querySelector('collapsible-content');
21
+
22
+ if (!button || !content) {
23
+ console.error(
24
+ 'CollapsibleComponent requires a <button> and a <collapsible-content>.'
25
+ );
26
+ return;
27
+ }
28
+
29
+ button.id ||= `collapsible-button-${Math.random().toString(36).slice(2, 9)}`;
30
+ content.id ||= `collapsible-content-${Math.random().toString(36).slice(2, 9)}`;
31
+
32
+ button.setAttribute('aria-controls', content.id);
33
+ content.setAttribute('role', 'region');
34
+ content.setAttribute('aria-labelledby', button.id);
35
+
36
+ const expanded = button.getAttribute('aria-expanded') === 'true';
37
+ content.hidden = !expanded;
38
+
39
+ button.addEventListener('click', this.#handleClick);
40
+ }
41
+
42
+ disconnectedCallback() {
43
+ const button = this.querySelector('button');
44
+ button?.removeEventListener('click', this.#handleClick);
45
+ }
46
+ }
47
+
48
+ class CollapsibleContent extends HTMLElement {
49
+ constructor() {
50
+ super();
51
+ this._onTransitionEnd = this._onTransitionEnd.bind(this);
52
+ // Add event listener to remove height after transition
53
+ this.addEventListener('transitionend', this._onTransitionEnd);
54
+ }
55
+
56
+ connectedCallback() {
57
+ if (this.hidden) {
58
+ this.style.height = '0';
59
+ } else {
60
+ this.style.height = `${this.scrollHeight}px`;
61
+ }
62
+ }
63
+
64
+ disconnectedCallback() {
65
+ this.removeEventListener('transitionend', this._onTransitionEnd);
66
+ }
67
+
68
+ set hidden(value) {
69
+ // animate to closed
70
+ if (value) {
71
+ // reset height to animate from
72
+ this.style.height = `${this.scrollHeight}px`;
73
+
74
+ // wait one frame and animate to 0
75
+ requestAnimationFrame(() => {
76
+ this.style.height = '0px';
77
+ });
78
+
79
+ this.setAttribute('aria-hidden', 'true');
80
+ this.setAttribute('inert', '');
81
+ this.setAttribute('hidden', '');
82
+ } else {
83
+ // animate to open
84
+ this.style.height = `${this.scrollHeight}px`;
85
+ this.removeAttribute('aria-hidden');
86
+ this.removeAttribute('inert');
87
+ this.removeAttribute('hidden');
88
+ }
89
+ }
90
+
91
+ get hidden() {
92
+ return this.hasAttribute('hidden');
93
+ }
94
+
95
+ _onTransitionEnd(event) {
96
+ // exit if isn't height property
97
+ if (event.propertyName != 'height') return;
98
+
99
+ // Remove the inline height to allow dynamic content changes
100
+ if (!this.hidden) {
101
+ this.style.height = 'auto';
102
+ }
103
+ }
104
+ }
105
+
106
+ customElements.define('collapsible-content', CollapsibleContent);
107
+ customElements.define('collapsible-component', CollapsibleComponent);
108
+
109
+ exports.CollapsibleComponent = CollapsibleComponent;
110
+ exports.CollapsibleContent = CollapsibleContent;
111
+ //# sourceMappingURL=collapsible-content.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collapsible-content.cjs.js","sources":["../src/collapsible-content.js"],"sourcesContent":["import './styles.css';\n\nclass CollapsibleComponent extends HTMLElement {\n\t#handleClick;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis.#handleClick = () => {\n\t\t\tconst button = this.querySelector('button');\n\t\t\tconst content = this.querySelector('collapsible-content');\n\t\t\tconst expanded =\n\t\t\t\tbutton.getAttribute('aria-expanded') !== 'true';\n\t\t\tbutton.setAttribute('aria-expanded', expanded);\n\t\t\tcontent.hidden = !expanded;\n\t\t};\n\t}\n\n\tconnectedCallback() {\n\t\tconst button = this.querySelector('button');\n\t\tconst content = this.querySelector('collapsible-content');\n\n\t\tif (!button || !content) {\n\t\t\tconsole.error(\n\t\t\t\t'CollapsibleComponent requires a <button> and a <collapsible-content>.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tbutton.id ||= `collapsible-button-${Math.random().toString(36).slice(2, 9)}`;\n\t\tcontent.id ||= `collapsible-content-${Math.random().toString(36).slice(2, 9)}`;\n\n\t\tbutton.setAttribute('aria-controls', content.id);\n\t\tcontent.setAttribute('role', 'region');\n\t\tcontent.setAttribute('aria-labelledby', button.id);\n\n\t\tconst expanded = button.getAttribute('aria-expanded') === 'true';\n\t\tcontent.hidden = !expanded;\n\n\t\tbutton.addEventListener('click', this.#handleClick);\n\t}\n\n\tdisconnectedCallback() {\n\t\tconst button = this.querySelector('button');\n\t\tbutton?.removeEventListener('click', this.#handleClick);\n\t}\n}\n\nclass CollapsibleContent extends HTMLElement {\n\tconstructor() {\n\t\tsuper();\n\t\tthis._onTransitionEnd = this._onTransitionEnd.bind(this);\n\t\t// Add event listener to remove height after transition\n\t\tthis.addEventListener('transitionend', this._onTransitionEnd);\n\t}\n\n\tconnectedCallback() {\n\t\tif (this.hidden) {\n\t\t\tthis.style.height = '0';\n\t\t} else {\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\t\t}\n\t}\n\n\tdisconnectedCallback() {\n\t\tthis.removeEventListener('transitionend', this._onTransitionEnd);\n\t}\n\n\tset hidden(value) {\n\t\t// animate to closed\n\t\tif (value) {\n\t\t\t// reset height to animate from\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\n\t\t\t// wait one frame and animate to 0\n\t\t\trequestAnimationFrame(() => {\n\t\t\t\tthis.style.height = '0px';\n\t\t\t});\n\n\t\t\tthis.setAttribute('aria-hidden', 'true');\n\t\t\tthis.setAttribute('inert', '');\n\t\t\tthis.setAttribute('hidden', '');\n\t\t} else {\n\t\t\t// animate to open\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\t\t\tthis.removeAttribute('aria-hidden');\n\t\t\tthis.removeAttribute('inert');\n\t\t\tthis.removeAttribute('hidden');\n\t\t}\n\t}\n\n\tget hidden() {\n\t\treturn this.hasAttribute('hidden');\n\t}\n\n\t_onTransitionEnd(event) {\n\t\t// exit if isn't height property\n\t\tif (event.propertyName != 'height') return;\n\n\t\t// Remove the inline height to allow dynamic content changes\n\t\tif (!this.hidden) {\n\t\t\tthis.style.height = 'auto';\n\t\t}\n\t}\n}\n\ncustomElements.define('collapsible-content', CollapsibleContent);\ncustomElements.define('collapsible-component', CollapsibleComponent);\n\nexport { CollapsibleContent, CollapsibleComponent };\n"],"names":[],"mappings":";;AAEA,MAAM,oBAAoB,SAAS,WAAW,CAAC;AAC/C,CAAC,YAAY,CAAC;AACd;AACA,CAAC,WAAW,GAAG;AACf,EAAE,KAAK,EAAE,CAAC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AAC;AACD;AACA,cAAc,CAAC,MAAM,CAAC,qBAAqB,EAAE,kBAAkB,CAAC,CAAC;AACjE,cAAc,CAAC,MAAM,CAAC,uBAAuB,EAAE,oBAAoB,CAAC;;;;;"}
@@ -0,0 +1,108 @@
1
+ class CollapsibleComponent extends HTMLElement {
2
+ #handleClick;
3
+
4
+ constructor() {
5
+ super();
6
+ this.#handleClick = () => {
7
+ const button = this.querySelector('button');
8
+ const content = this.querySelector('collapsible-content');
9
+ const expanded =
10
+ button.getAttribute('aria-expanded') !== 'true';
11
+ button.setAttribute('aria-expanded', expanded);
12
+ content.hidden = !expanded;
13
+ };
14
+ }
15
+
16
+ connectedCallback() {
17
+ const button = this.querySelector('button');
18
+ const content = this.querySelector('collapsible-content');
19
+
20
+ if (!button || !content) {
21
+ console.error(
22
+ 'CollapsibleComponent requires a <button> and a <collapsible-content>.'
23
+ );
24
+ return;
25
+ }
26
+
27
+ button.id ||= `collapsible-button-${Math.random().toString(36).slice(2, 9)}`;
28
+ content.id ||= `collapsible-content-${Math.random().toString(36).slice(2, 9)}`;
29
+
30
+ button.setAttribute('aria-controls', content.id);
31
+ content.setAttribute('role', 'region');
32
+ content.setAttribute('aria-labelledby', button.id);
33
+
34
+ const expanded = button.getAttribute('aria-expanded') === 'true';
35
+ content.hidden = !expanded;
36
+
37
+ button.addEventListener('click', this.#handleClick);
38
+ }
39
+
40
+ disconnectedCallback() {
41
+ const button = this.querySelector('button');
42
+ button?.removeEventListener('click', this.#handleClick);
43
+ }
44
+ }
45
+
46
+ class CollapsibleContent extends HTMLElement {
47
+ constructor() {
48
+ super();
49
+ this._onTransitionEnd = this._onTransitionEnd.bind(this);
50
+ // Add event listener to remove height after transition
51
+ this.addEventListener('transitionend', this._onTransitionEnd);
52
+ }
53
+
54
+ connectedCallback() {
55
+ if (this.hidden) {
56
+ this.style.height = '0';
57
+ } else {
58
+ this.style.height = `${this.scrollHeight}px`;
59
+ }
60
+ }
61
+
62
+ disconnectedCallback() {
63
+ this.removeEventListener('transitionend', this._onTransitionEnd);
64
+ }
65
+
66
+ set hidden(value) {
67
+ // animate to closed
68
+ if (value) {
69
+ // reset height to animate from
70
+ this.style.height = `${this.scrollHeight}px`;
71
+
72
+ // wait one frame and animate to 0
73
+ requestAnimationFrame(() => {
74
+ this.style.height = '0px';
75
+ });
76
+
77
+ this.setAttribute('aria-hidden', 'true');
78
+ this.setAttribute('inert', '');
79
+ this.setAttribute('hidden', '');
80
+ } else {
81
+ // animate to open
82
+ this.style.height = `${this.scrollHeight}px`;
83
+ this.removeAttribute('aria-hidden');
84
+ this.removeAttribute('inert');
85
+ this.removeAttribute('hidden');
86
+ }
87
+ }
88
+
89
+ get hidden() {
90
+ return this.hasAttribute('hidden');
91
+ }
92
+
93
+ _onTransitionEnd(event) {
94
+ // exit if isn't height property
95
+ if (event.propertyName != 'height') return;
96
+
97
+ // Remove the inline height to allow dynamic content changes
98
+ if (!this.hidden) {
99
+ this.style.height = 'auto';
100
+ }
101
+ }
102
+ }
103
+
104
+ customElements.define('collapsible-content', CollapsibleContent);
105
+ customElements.define('collapsible-component', CollapsibleComponent);
106
+
107
+ export { CollapsibleComponent, CollapsibleContent };
108
+ //# sourceMappingURL=collapsible-content.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"collapsible-content.esm.js","sources":["../src/collapsible-content.js"],"sourcesContent":["import './styles.css';\n\nclass CollapsibleComponent extends HTMLElement {\n\t#handleClick;\n\n\tconstructor() {\n\t\tsuper();\n\t\tthis.#handleClick = () => {\n\t\t\tconst button = this.querySelector('button');\n\t\t\tconst content = this.querySelector('collapsible-content');\n\t\t\tconst expanded =\n\t\t\t\tbutton.getAttribute('aria-expanded') !== 'true';\n\t\t\tbutton.setAttribute('aria-expanded', expanded);\n\t\t\tcontent.hidden = !expanded;\n\t\t};\n\t}\n\n\tconnectedCallback() {\n\t\tconst button = this.querySelector('button');\n\t\tconst content = this.querySelector('collapsible-content');\n\n\t\tif (!button || !content) {\n\t\t\tconsole.error(\n\t\t\t\t'CollapsibleComponent requires a <button> and a <collapsible-content>.'\n\t\t\t);\n\t\t\treturn;\n\t\t}\n\n\t\tbutton.id ||= `collapsible-button-${Math.random().toString(36).slice(2, 9)}`;\n\t\tcontent.id ||= `collapsible-content-${Math.random().toString(36).slice(2, 9)}`;\n\n\t\tbutton.setAttribute('aria-controls', content.id);\n\t\tcontent.setAttribute('role', 'region');\n\t\tcontent.setAttribute('aria-labelledby', button.id);\n\n\t\tconst expanded = button.getAttribute('aria-expanded') === 'true';\n\t\tcontent.hidden = !expanded;\n\n\t\tbutton.addEventListener('click', this.#handleClick);\n\t}\n\n\tdisconnectedCallback() {\n\t\tconst button = this.querySelector('button');\n\t\tbutton?.removeEventListener('click', this.#handleClick);\n\t}\n}\n\nclass CollapsibleContent extends HTMLElement {\n\tconstructor() {\n\t\tsuper();\n\t\tthis._onTransitionEnd = this._onTransitionEnd.bind(this);\n\t\t// Add event listener to remove height after transition\n\t\tthis.addEventListener('transitionend', this._onTransitionEnd);\n\t}\n\n\tconnectedCallback() {\n\t\tif (this.hidden) {\n\t\t\tthis.style.height = '0';\n\t\t} else {\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\t\t}\n\t}\n\n\tdisconnectedCallback() {\n\t\tthis.removeEventListener('transitionend', this._onTransitionEnd);\n\t}\n\n\tset hidden(value) {\n\t\t// animate to closed\n\t\tif (value) {\n\t\t\t// reset height to animate from\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\n\t\t\t// wait one frame and animate to 0\n\t\t\trequestAnimationFrame(() => {\n\t\t\t\tthis.style.height = '0px';\n\t\t\t});\n\n\t\t\tthis.setAttribute('aria-hidden', 'true');\n\t\t\tthis.setAttribute('inert', '');\n\t\t\tthis.setAttribute('hidden', '');\n\t\t} else {\n\t\t\t// animate to open\n\t\t\tthis.style.height = `${this.scrollHeight}px`;\n\t\t\tthis.removeAttribute('aria-hidden');\n\t\t\tthis.removeAttribute('inert');\n\t\t\tthis.removeAttribute('hidden');\n\t\t}\n\t}\n\n\tget hidden() {\n\t\treturn this.hasAttribute('hidden');\n\t}\n\n\t_onTransitionEnd(event) {\n\t\t// exit if isn't height property\n\t\tif (event.propertyName != 'height') return;\n\n\t\t// Remove the inline height to allow dynamic content changes\n\t\tif (!this.hidden) {\n\t\t\tthis.style.height = 'auto';\n\t\t}\n\t}\n}\n\ncustomElements.define('collapsible-content', CollapsibleContent);\ncustomElements.define('collapsible-component', CollapsibleComponent);\n\nexport { CollapsibleContent, CollapsibleComponent };\n"],"names":[],"mappings":"AAEA,MAAM,oBAAoB,SAAS,WAAW,CAAC;AAC/C,CAAC,YAAY,CAAC;AACd;AACA,CAAC,WAAW,GAAG;AACf,EAAE,KAAK,EAAE,CAAC;AACV,EAAE,IAAI,CAAC,YAAY,GAAG,MAAM;AAC5B,GAAG,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAC/C,GAAG,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,qBAAqB,CAAC,CAAC;AAC7D,GAAG,MAAM,QAAQ;AACjB,IAAI,MAAM,CAAC,YAAY,CAAC,eAAe,CAAC,KAAK,MAAM,CAAC;AACpD,GAAG,MAAM,CAAC,YAAY,CAAC,eAAe,EAAE,QAAQ,CAAC,CAAC;AAClD,GAAG,OAAO,CAAC,MAAM,GAAG,CAAC,QAAQ,CAAC;AAC9B,GAAG,CAAC;AACJ,EAAE;AACF;AACA,CAAC,iBAAiB,GAAG;AACrB,EAAE,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,CAAC;AAC9C,EAAE,MAAM,OAAO,GAAG,IAAI,CAAC,aAAa,CAAC,qBAAqB,CAAC,CAAC;AAC5D;AACA,EAAE,IAAI,CAAC,MAAM,IAAI,CAAC,OAAO,EAAE;AAC3B,GAAG,OAAO,CAAC,KAAK;AAChB,IAAI,uEAAuE;AAC3E,IAAI,CAAC;AACL,GAAG,OAAO;AACV,GAAG;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,CAAC,CAAC;AACjC,GAAG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,CAAC;AAClC,GAAG;AACH,EAAE;AACF;AACA,CAAC,IAAI,MAAM,GAAG;AACd,EAAE,OAAO,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;AACrC,EAAE;AACF;AACA,CAAC,gBAAgB,CAAC,KAAK,EAAE;AACzB;AACA,EAAE,IAAI,KAAK,CAAC,YAAY,IAAI,QAAQ,EAAE,OAAO;AAC7C;AACA;AACA,EAAE,IAAI,CAAC,IAAI,CAAC,MAAM,EAAE;AACpB,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,MAAM,CAAC;AAC9B,GAAG;AACH,EAAE;AACF,CAAC;AACD;AACA,cAAc,CAAC,MAAM,CAAC,qBAAqB,EAAE,kBAAkB,CAAC,CAAC;AACjE,cAAc,CAAC,MAAM,CAAC,uBAAuB,EAAE,oBAAoB,CAAC;;;;"}
@@ -0,0 +1 @@
1
+ collapsible-component{display:block}collapsible-component button{background:none;border:none;cursor:pointer;text-align:left;width:100%}collapsible-content{display:block;overflow:hidden;padding:0;transition:height .35s ease-out}
@@ -0,0 +1 @@
1
+ var CollapsibleContent=function(t){"use strict";class CollapsibleComponent extends HTMLElement{#t;constructor(){super(),this.#t=()=>{const t=this.querySelector("button"),e=this.querySelector("collapsible-content"),i="true"!==t.getAttribute("aria-expanded");t.setAttribute("aria-expanded",i),e.hidden=!i}}connectedCallback(){const t=this.querySelector("button"),e=this.querySelector("collapsible-content");if(!t||!e)return void console.error("CollapsibleComponent requires a <button> and a <collapsible-content>.");t.id||=`collapsible-button-${Math.random().toString(36).slice(2,9)}`,e.id||=`collapsible-content-${Math.random().toString(36).slice(2,9)}`,t.setAttribute("aria-controls",e.id),e.setAttribute("role","region"),e.setAttribute("aria-labelledby",t.id);const i="true"===t.getAttribute("aria-expanded");e.hidden=!i,t.addEventListener("click",this.#t)}disconnectedCallback(){const t=this.querySelector("button");t?.removeEventListener("click",this.#t)}}class CollapsibleContent extends HTMLElement{constructor(){super(),this._onTransitionEnd=this._onTransitionEnd.bind(this),this.addEventListener("transitionend",this._onTransitionEnd)}connectedCallback(){this.hidden?this.style.height="0":this.style.height=`${this.scrollHeight}px`}disconnectedCallback(){this.removeEventListener("transitionend",this._onTransitionEnd)}set hidden(t){t?(this.style.height=`${this.scrollHeight}px`,requestAnimationFrame((()=>{this.style.height="0px"})),this.setAttribute("aria-hidden","true"),this.setAttribute("inert",""),this.setAttribute("hidden","")):(this.style.height=`${this.scrollHeight}px`,this.removeAttribute("aria-hidden"),this.removeAttribute("inert"),this.removeAttribute("hidden"))}get hidden(){return this.hasAttribute("hidden")}_onTransitionEnd(t){"height"==t.propertyName&&(this.hidden||(this.style.height="auto"))}}return customElements.define("collapsible-content",CollapsibleContent),customElements.define("collapsible-component",CollapsibleComponent),t.CollapsibleComponent=CollapsibleComponent,t.CollapsibleContent=CollapsibleContent,t}({});
package/package.json ADDED
@@ -0,0 +1,71 @@
1
+ {
2
+ "name": "@magic-spells/collapsible-content",
3
+ "version": "0.1.0",
4
+ "description": "Collapsible content web component",
5
+ "author": "Cory Schulz",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "main": "dist/collapsible-content.cjs.js",
9
+ "module": "dist/collapsible-content.esm.js",
10
+ "unpkg": "dist/collapsible-content.min.js",
11
+ "style": "dist/collapsible-content.min.css",
12
+ "exports": {
13
+ ".": {
14
+ "import": "./dist/collapsible-content.esm.js",
15
+ "require": "./dist/collapsible-content.cjs.js",
16
+ "default": "./dist/collapsible-content.esm.js"
17
+ },
18
+ "./style.css": "./dist/collapsible-content.min.css"
19
+ },
20
+ "sideEffects": true,
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://github.com/magic-spells/collapsible-content"
24
+ },
25
+ "homepage": "https://github.com/magic-spells/collapsible-content#readme",
26
+ "bugs": {
27
+ "url": "https://github.com/magic-spells/collapsible-content/issues"
28
+ },
29
+ "keywords": [
30
+ "collapsible-content",
31
+ "web-components",
32
+ "accessibility",
33
+ "a11y",
34
+ "keyboard-navigation",
35
+ "custom-elements"
36
+ ],
37
+ "files": [
38
+ "dist/",
39
+ "src/"
40
+ ],
41
+ "scripts": {
42
+ "build": "rollup -c",
43
+ "lint": "eslint src/ rollup.config.mjs",
44
+ "format": "prettier --write .",
45
+ "prepublishOnly": "npm run build",
46
+ "serve": "rollup -c --watch",
47
+ "dev": "rollup -c --watch"
48
+ },
49
+ "publishConfig": {
50
+ "access": "public",
51
+ "registry": "https://registry.npmjs.org/"
52
+ },
53
+ "browserslist": [
54
+ "last 2 versions",
55
+ "not dead",
56
+ "not ie <= 11"
57
+ ],
58
+ "devDependencies": {
59
+ "@eslint/js": "^8.57.0",
60
+ "@rollup/plugin-node-resolve": "^15.2.3",
61
+ "@rollup/plugin-terser": "^0.4.4",
62
+ "eslint": "^8.0.0",
63
+ "globals": "^13.24.0",
64
+ "prettier": "^3.3.3",
65
+ "rollup": "^3.0.0",
66
+ "rollup-plugin-copy": "^3.5.0",
67
+ "rollup-plugin-postcss": "^4.0.2",
68
+ "rollup-plugin-serve": "^1.1.1",
69
+ "postcss-lightningcss": "^1.0.1"
70
+ }
71
+ }
@@ -0,0 +1,109 @@
1
+ import './styles.css';
2
+
3
+ class CollapsibleComponent extends HTMLElement {
4
+ #handleClick;
5
+
6
+ constructor() {
7
+ super();
8
+ this.#handleClick = () => {
9
+ const button = this.querySelector('button');
10
+ const content = this.querySelector('collapsible-content');
11
+ const expanded =
12
+ button.getAttribute('aria-expanded') !== 'true';
13
+ button.setAttribute('aria-expanded', expanded);
14
+ content.hidden = !expanded;
15
+ };
16
+ }
17
+
18
+ connectedCallback() {
19
+ const button = this.querySelector('button');
20
+ const content = this.querySelector('collapsible-content');
21
+
22
+ if (!button || !content) {
23
+ console.error(
24
+ 'CollapsibleComponent requires a <button> and a <collapsible-content>.'
25
+ );
26
+ return;
27
+ }
28
+
29
+ button.id ||= `collapsible-button-${Math.random().toString(36).slice(2, 9)}`;
30
+ content.id ||= `collapsible-content-${Math.random().toString(36).slice(2, 9)}`;
31
+
32
+ button.setAttribute('aria-controls', content.id);
33
+ content.setAttribute('role', 'region');
34
+ content.setAttribute('aria-labelledby', button.id);
35
+
36
+ const expanded = button.getAttribute('aria-expanded') === 'true';
37
+ content.hidden = !expanded;
38
+
39
+ button.addEventListener('click', this.#handleClick);
40
+ }
41
+
42
+ disconnectedCallback() {
43
+ const button = this.querySelector('button');
44
+ button?.removeEventListener('click', this.#handleClick);
45
+ }
46
+ }
47
+
48
+ class CollapsibleContent extends HTMLElement {
49
+ constructor() {
50
+ super();
51
+ this._onTransitionEnd = this._onTransitionEnd.bind(this);
52
+ // Add event listener to remove height after transition
53
+ this.addEventListener('transitionend', this._onTransitionEnd);
54
+ }
55
+
56
+ connectedCallback() {
57
+ if (this.hidden) {
58
+ this.style.height = '0';
59
+ } else {
60
+ this.style.height = `${this.scrollHeight}px`;
61
+ }
62
+ }
63
+
64
+ disconnectedCallback() {
65
+ this.removeEventListener('transitionend', this._onTransitionEnd);
66
+ }
67
+
68
+ set hidden(value) {
69
+ // animate to closed
70
+ if (value) {
71
+ // reset height to animate from
72
+ this.style.height = `${this.scrollHeight}px`;
73
+
74
+ // wait one frame and animate to 0
75
+ requestAnimationFrame(() => {
76
+ this.style.height = '0px';
77
+ });
78
+
79
+ this.setAttribute('aria-hidden', 'true');
80
+ this.setAttribute('inert', '');
81
+ this.setAttribute('hidden', '');
82
+ } else {
83
+ // animate to open
84
+ this.style.height = `${this.scrollHeight}px`;
85
+ this.removeAttribute('aria-hidden');
86
+ this.removeAttribute('inert');
87
+ this.removeAttribute('hidden');
88
+ }
89
+ }
90
+
91
+ get hidden() {
92
+ return this.hasAttribute('hidden');
93
+ }
94
+
95
+ _onTransitionEnd(event) {
96
+ // exit if isn't height property
97
+ if (event.propertyName != 'height') return;
98
+
99
+ // Remove the inline height to allow dynamic content changes
100
+ if (!this.hidden) {
101
+ this.style.height = 'auto';
102
+ }
103
+ }
104
+ }
105
+
106
+ customElements.define('collapsible-content', CollapsibleContent);
107
+ customElements.define('collapsible-component', CollapsibleComponent);
108
+
109
+ export { CollapsibleContent, CollapsibleComponent };
package/src/styles.css ADDED
@@ -0,0 +1,18 @@
1
+ collapsible-component {
2
+ display: block;
3
+ }
4
+
5
+ collapsible-component button {
6
+ width: 100%;
7
+ text-align: left;
8
+ background: none;
9
+ border: none;
10
+ cursor: pointer;
11
+ }
12
+
13
+ collapsible-content {
14
+ padding: 0px;
15
+ display: block;
16
+ overflow: hidden;
17
+ transition: height 0.35s ease-out;
18
+ }