@c2n/accordion 0.0.6

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 Nguyen Thai Vinh
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,56 @@
1
+ # Accordion
2
+
3
+ `c2-accordion` combines directly slotted `c2-details` panels into a connected, animated disclosure surface. It owns their shared borders, dividers and outside corners, and coordinates expansion through Lit context. Opening one closes its siblings; add `multiple` to allow independent expansion. All panels may be closed.
4
+
5
+ ```sh
6
+ npm install @c2n/accordion
7
+ ```
8
+
9
+ ```js
10
+ import '@c2n/accordion'
11
+ ```
12
+
13
+ The import also registers `c2-details`.
14
+
15
+ ```html
16
+ <c2-accordion>
17
+ <c2-details label="Shipping" expanded>Delivered in 3–5 business days.</c2-details>
18
+ <c2-details label="Returns">Return within 30 days.</c2-details>
19
+ <c2-details label="Unavailable" disabled>Coming soon.</c2-details>
20
+ </c2-accordion>
21
+
22
+ <c2-accordion multiple>
23
+ <c2-details label="Sizing" expanded>Choose your usual size.</c2-details>
24
+ <c2-details label="Care" expanded>Wash cold.</c2-details>
25
+ </c2-accordion>
26
+ ```
27
+
28
+ ## API
29
+
30
+ - `multiple`: reflected Boolean property/attribute, default `false`.
31
+ - Default slot: direct `c2-details` children. Wrapped descendants are not coordinated; nested accordions remain independent.
32
+ - `--c2-accordion--gap`: panel spacing, default `0px`. Zero joins the panels with single dividers and only outside corners rounded. A positive gap restores individual rounded cards.
33
+ - `--c2-accordion--border-width`: outer borders and dividers, default `1px`.
34
+ - `--c2-accordion--border-color`: outer borders and dividers, default `#d5d5d5`.
35
+ - `--c2-accordion--border-radius`: outside corners, default `8px`.
36
+ - `--c2-accordion--transition-duration`: expansion and chevron timing, default `300ms`; use `0ms` for instant toggling. Reduced-motion preferences disable transitions.
37
+
38
+ The accordion controls panel frames and animation timing. Use inherited `--c2-details-*` properties for header/content colors, padding and typography. Panel height is animated by `c2-details` itself (slide plus fade), so it behaves the same in every browser.
39
+
40
+ Control individual panels with `expanded` or `toggle(force?)`. Listen for `toggle` directly on each panel; it does not bubble. There is no aggregate accordion event. Disabled panels block user toggling but may still be closed when a sibling opens.
41
+
42
+ When multiple panels start expanded, or `multiple` is removed, the first expanded panel in DOM order stays open. Child insertion and reconnection use the same rule. Subsequent openings take precedence. Grouping belongs to the container and does not require panel `name` values.
43
+
44
+ Panels retain native Tab, Enter/Space, focus, and disclosure semantics, their title/header/icon slots, and reduced-motion support.
45
+
46
+ ```html
47
+ <c2-accordion>
48
+ <c2-details label="Products" expanded>
49
+ <c2-accordion>
50
+ <c2-details label="Clothing">Shirts and jackets.</c2-details>
51
+ <c2-details label="Accessories">Bags and hats.</c2-details>
52
+ </c2-accordion>
53
+ </c2-details>
54
+ <c2-details label="Support">Contact our team.</c2-details>
55
+ </c2-accordion>
56
+ ```
@@ -0,0 +1,62 @@
1
+ import { LitElement, html, unsafeCSS } from "lit";
2
+ import { customElement, property } from "lit/decorators.js";
3
+ import "@c2n/details";
4
+ import { provide } from "@lit/context";
5
+ import { accordionContext } from "@c2n/details/details-context.js";
6
+ //#region src/accordion.scss?inline
7
+ var accordion_default = "/* ex : var((width: 24px), width, c2-checkbox) returns var(--c2-checkbox-width, 24px) */\n:host {\n display: block;\n min-width: 0;\n}\n\n.c2-accordion {\n display: flex;\n flex-direction: column;\n box-sizing: border-box;\n min-width: 0;\n gap: var(--c2-accordion--gap,0px);\n}\n\n::slotted(c2-details) {\n --c2-details--border-top: min(var(--c2-accordion--border-width,1px), calc(var(--c2-accordion--gap,0px) * 10000)) solid var(--c2-accordion--border-color,#d5d5d5);\n --c2-details--border-right: var(--c2-accordion--border-width,1px) solid var(--c2-accordion--border-color,#d5d5d5);\n --c2-details--border-bottom: var(--c2-accordion--border-width,1px) solid var(--c2-accordion--border-color,#d5d5d5);\n --c2-details--border-left: var(--c2-accordion--border-width,1px) solid var(--c2-accordion--border-color,#d5d5d5);\n --c2-details--border-top-left-radius: min(var(--c2-accordion--border-radius,8px), calc(var(--c2-accordion--gap,0px) * 10000));\n --c2-details--border-top-right-radius: min(var(--c2-accordion--border-radius,8px), calc(var(--c2-accordion--gap,0px) * 10000));\n --c2-details--border-bottom-left-radius: min(var(--c2-accordion--border-radius,8px), calc(var(--c2-accordion--gap,0px) * 10000));\n --c2-details--border-bottom-right-radius: min(var(--c2-accordion--border-radius,8px), calc(var(--c2-accordion--gap,0px) * 10000));\n --c2-details--transition-duration: var(--c2-accordion--transition-duration,300ms);\n}\n\n::slotted(c2-details:first-of-type) {\n --c2-details--border-top: var(--c2-accordion--border-width,1px) solid var(--c2-accordion--border-color,#d5d5d5);\n --c2-details--border-top-left-radius: var(--c2-accordion--border-radius,8px);\n --c2-details--border-top-right-radius: var(--c2-accordion--border-radius,8px);\n}\n\n::slotted(c2-details:last-of-type) {\n --c2-details--border-bottom-left-radius: var(--c2-accordion--border-radius,8px);\n --c2-details--border-bottom-right-radius: var(--c2-accordion--border-radius,8px);\n}";
8
+ //#endregion
9
+ //#region \0@oxc-project+runtime@0.148.0/helpers/esm/decorate.js
10
+ function __decorate(decorators, target, key, desc) {
11
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
12
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
13
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
14
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
15
+ }
16
+ //#endregion
17
+ //#region src/accordion.ts
18
+ var Accordion = class Accordion extends LitElement {
19
+ constructor(..._args) {
20
+ super(..._args);
21
+ this.multiple = false;
22
+ this.accordion = { expandedChanged: (panel) => {
23
+ if (this.isConnected && this.panels.includes(panel) && panel.expanded) this.closeOthers(panel);
24
+ } };
25
+ }
26
+ static {
27
+ this.styles = unsafeCSS(accordion_default);
28
+ }
29
+ get panels() {
30
+ return (this.renderRoot.querySelector("slot")?.assignedElements() ?? []).filter((element) => element.localName === "c2-details");
31
+ }
32
+ connectedCallback() {
33
+ super.connectedCallback();
34
+ if (this.hasUpdated) this.syncPanels();
35
+ }
36
+ updated(changed) {
37
+ if (changed.has("multiple")) this.syncPanels();
38
+ }
39
+ syncPanels() {
40
+ if (!this.isConnected) return;
41
+ this.closeOthers(this.panels.find((panel) => panel.expanded));
42
+ }
43
+ closeOthers(expandedPanel) {
44
+ if (this.multiple) return;
45
+ for (const panel of this.panels) if (panel !== expandedPanel && panel.expanded) panel.expanded = false;
46
+ }
47
+ render() {
48
+ return html`
49
+ <div class="c2-accordion">
50
+ <slot @slotchange=${this.syncPanels}></slot>
51
+ </div>
52
+ `;
53
+ }
54
+ };
55
+ __decorate([property({
56
+ type: Boolean,
57
+ reflect: true
58
+ })], Accordion.prototype, "multiple", void 0);
59
+ __decorate([provide({ context: accordionContext })], Accordion.prototype, "accordion", void 0);
60
+ Accordion = __decorate([customElement("c2-accordion")], Accordion);
61
+ //#endregion
62
+ export { Accordion };
package/package.json ADDED
@@ -0,0 +1,64 @@
1
+ {
2
+ "name": "@c2n/accordion",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
+ "main": "dist/accordion.js",
6
+ "exports": {
7
+ ".": {
8
+ "default": "./dist/accordion.js",
9
+ "types": "./types/src/accordion.d.ts"
10
+ },
11
+ "./custom-elements.json": "./custom-elements.json"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "types"
16
+ ],
17
+ "keywords": [
18
+ "accordion",
19
+ "web component",
20
+ "lit"
21
+ ],
22
+ "license": "MIT",
23
+ "author": "code2nguyen@gmail.com",
24
+ "publishConfig": {
25
+ "registry": "https://registry.npmjs.org",
26
+ "access": "public"
27
+ },
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "https://github.com/code2nguyen/web-components.git"
31
+ },
32
+ "scripts": {
33
+ "dev": "vite",
34
+ "build": "wireit",
35
+ "build:only": "vite build",
36
+ "type-check": "wireit"
37
+ },
38
+ "wireit": {
39
+ "type-check": {
40
+ "dependencies": [
41
+ "../../core:build",
42
+ "../details:build"
43
+ ],
44
+ "command": "tsc -p tsconfig.lib.json --composite false"
45
+ },
46
+ "build": {
47
+ "dependencies": [
48
+ "type-check"
49
+ ],
50
+ "command": "vite build"
51
+ }
52
+ },
53
+ "dependencies": {
54
+ "@c2n/core": "0.0.6",
55
+ "@c2n/details": "0.0.6",
56
+ "@lit/context": "1.1.6",
57
+ "lit": "3.3.3"
58
+ },
59
+ "devDependencies": {
60
+ "@c2n/config": "*"
61
+ },
62
+ "customElements": "custom-elements.json",
63
+ "gitHead": "2921054bc75299da66dad11c1e374246ff88a51e"
64
+ }
@@ -0,0 +1,35 @@
1
+ import { LitElement, type PropertyValues } from 'lit';
2
+ import '@c2n/details';
3
+ import { type AccordionContext } from '@c2n/details/details-context.js';
4
+ /**
5
+ * A connected disclosure surface with shared borders, outside corners and animated panels.
6
+ * Opening a panel closes its siblings unless `multiple` is set.
7
+ *
8
+ * @tag c2-accordion
9
+ *
10
+ * @slot - Direct `c2-details` children to coordinate.
11
+ * @cssproperty {pixel} [--c2-accordion--gap=0px] - Space between panels. Zero joins their borders and corners; a positive gap separates them into cards.
12
+ * @cssproperty {pixel} [--c2-accordion--border-width=1px] - Width of the outer frame and shared panel dividers.
13
+ * @cssproperty {color} [--c2-accordion--border-color=#d5d5d5] - Color of the outer frame and shared panel dividers.
14
+ * @cssproperty {border-radius} [--c2-accordion--border-radius=8px] - Outside corner radius. With a positive gap, applies to every panel.
15
+ * @cssproperty {time} [--c2-accordion--transition-duration=300ms] - Panel and chevron animation duration. Set to 0ms for instant toggling; respects reduced motion.
16
+ * @slotcomponent c2-details
17
+ */
18
+ export declare class Accordion extends LitElement {
19
+ static styles: import("lit").CSSResult;
20
+ /** Allows several panels to remain expanded. All panels may be closed in either mode. */
21
+ multiple: boolean;
22
+ protected accordion: AccordionContext;
23
+ private get panels();
24
+ connectedCallback(): void;
25
+ updated(changed: PropertyValues<this>): void;
26
+ private syncPanels;
27
+ private closeOthers;
28
+ render(): import("lit-html").TemplateResult<1>;
29
+ }
30
+ declare global {
31
+ interface HTMLElementTagNameMap {
32
+ 'c2-accordion': Accordion;
33
+ }
34
+ }
35
+ //# sourceMappingURL=accordion.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"accordion.d.ts","sourceRoot":"","sources":["../../src/accordion.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAmB,KAAK,cAAc,EAAE,MAAM,KAAK,CAAA;AAEtE,OAAO,cAAc,CAAA;AAGrB,OAAO,EAAoB,KAAK,gBAAgB,EAAE,MAAM,iCAAiC,CAAA;AAGzF;;;;;;;;;;;;;GAaG;AACH,qBACa,SAAU,SAAQ,UAAU;IACvC,OAAgB,MAAM,0BAAoB;IAE1C,yFAAyF;IAC7C,QAAQ,UAAQ;IAG5D,SAAS,CAAC,SAAS,EAAE,gBAAgB,CAIpC;IAED,OAAO,KAAK,MAAM,GAEjB;IAEQ,iBAAiB;IAKjB,OAAO,CAAC,OAAO,EAAE,cAAc,CAAC,IAAI,CAAC;IAI9C,OAAO,CAAC,UAAU;IAKlB,OAAO,CAAC,WAAW;IAOV,MAAM;CAOhB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,cAAc,EAAE,SAAS,CAAA;KAC1B;CACF"}