@c2n/card 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,27 @@
1
+ # @c2n/card
2
+
3
+ Themeable surface that groups related content and actions, built with Lit.
4
+
5
+ ```bash
6
+ npm install @c2n/card
7
+ ```
8
+
9
+ ```html
10
+ <script type="module">
11
+ import '@c2n/card'
12
+ </script>
13
+
14
+ <c2-card>
15
+ <img slot="media" src="cover.jpg" alt="" />
16
+ <h3 slot="header">Title</h3>
17
+ Body text goes in the default slot.
18
+ <c2-button slot="footer">Action</c2-button>
19
+ </c2-card>
20
+ ```
21
+
22
+ - **Sections**: `media` (full-bleed, clipped to the card radius), `header`, the default slot (body) and `footer` (a flex row). A section is rendered only when its slot has content, so a plain `<c2-card>text</c2-card>` is just a padded box.
23
+ - **Link card**: set `href` (plus `target` / `rel`) and the whole card becomes an anchor.
24
+ - **Interactive card**: set `interactive` for button semantics: focusable, `role="button"`, Enter and Space dispatch `click`.
25
+ - **Disabled**: `disabled` dims the card and blocks interaction; a link card loses its anchor.
26
+
27
+ Everything visual is a CSS custom property: `--c2-card--*` for the surface (padding, radius, borders, background, color, shadow), `--c2-card__header--*`, `--c2-card__footer--*` and `--c2-card__media--*` for the sections, and `--c2-card__hover--*`, `--c2-card__focus--*`, `--c2-card__disabled--*` for the states. `--c2-card__hover--transform: translateY(-2px)` with a hover shadow gives the classic lift. The full list is in `custom-elements.json` and on the docs site.
package/dist/card.js ADDED
@@ -0,0 +1,107 @@
1
+ import { LitElement, html, nothing, unsafeCSS } from "lit";
2
+ import { customElement, property, state } from "lit/decorators.js";
3
+ import { classMap } from "lit/directives/class-map.js";
4
+ import { ifDefined } from "lit/directives/if-defined.js";
5
+ //#region src/card.scss?inline
6
+ var card_default = "/* ex : var((width: 24px), width, c2-checkbox) returns var(--c2-checkbox-width, 24px) */\n:host {\n display: flex;\n}\n\n.c2-card {\n flex: 1;\n display: flex;\n flex-direction: column;\n box-sizing: border-box;\n min-width: 0;\n border-top-left-radius: var(--c2-card--border-top-left-radius,8px);\n border-top-right-radius: var(--c2-card--border-top-right-radius,8px);\n border-bottom-left-radius: var(--c2-card--border-bottom-left-radius,8px);\n border-bottom-right-radius: var(--c2-card--border-bottom-right-radius,8px);\n border-top: var(--c2-card--border-top,1px solid rgb(213, 213, 213));\n border-bottom: var(--c2-card--border-bottom,1px solid rgb(213, 213, 213));\n border-right: var(--c2-card--border-right,1px solid rgb(213, 213, 213));\n border-left: var(--c2-card--border-left,1px solid rgb(213, 213, 213));\n background: var(--c2-card--background,rgb(255, 255, 255));\n color: var(--c2-card--color);\n box-shadow: var(--c2-card--box-shadow,none);\n transform: var(--c2-card--transform);\n transition: background 250ms cubic-bezier(0.2, 0, 0, 1), border-color 250ms cubic-bezier(0.2, 0, 0, 1), box-shadow 250ms cubic-bezier(0.2, 0, 0, 1), transform 250ms cubic-bezier(0.2, 0, 0, 1);\n}\n.c2-card:hover {\n border-top: var(--c2-card__hover--border-top,var(--c2-card--border-top,1px solid rgb(213, 213, 213)));\n border-bottom: var(--c2-card__hover--border-bottom,var(--c2-card--border-bottom,1px solid rgb(213, 213, 213)));\n border-right: var(--c2-card__hover--border-right,var(--c2-card--border-right,1px solid rgb(213, 213, 213)));\n border-left: var(--c2-card__hover--border-left,var(--c2-card--border-left,1px solid rgb(213, 213, 213)));\n background: var(--c2-card__hover--background,var(--c2-card--background,rgb(255, 255, 255)));\n box-shadow: var(--c2-card__hover--box-shadow,var(--c2-card--box-shadow,none));\n transform: var(--c2-card__hover--transform,var(--c2-card--transform));\n}\n.c2-card.is-interactive {\n cursor: pointer;\n user-select: none;\n}\n.c2-card:focus-visible {\n outline: var(--c2-card__focus--outline,2px solid rgba(2, 101, 220, 0.4));\n outline-offset: var(--c2-card__focus--outline-offset,2px);\n}\n\na.c2-card {\n text-decoration: none;\n}\n\n.c2-card-media {\n overflow: hidden;\n border-top-left-radius: inherit;\n border-top-right-radius: inherit;\n}\n.c2-card-media:last-child {\n border-bottom-left-radius: inherit;\n border-bottom-right-radius: inherit;\n}\n.c2-card-media ::slotted(*) {\n display: block;\n width: 100%;\n height: var(--c2-card__media--height);\n aspect-ratio: var(--c2-card__media--aspect-ratio);\n object-fit: var(--c2-card__media--object-fit,cover);\n}\n\n.c2-card-header {\n padding-top: var(--c2-card__header--padding-top,var(--c2-card--padding-top,12px));\n padding-right: var(--c2-card__header--padding-right,var(--c2-card--padding-right,16px));\n padding-bottom: var(--c2-card__header--padding-bottom,0px);\n padding-left: var(--c2-card__header--padding-left,var(--c2-card--padding-left,16px));\n border-bottom: var(--c2-card__header--border-bottom);\n}\n\n.c2-card-content {\n flex: 1;\n padding-top: var(--c2-card--padding-top,12px);\n padding-right: var(--c2-card--padding-right,16px);\n padding-bottom: var(--c2-card--padding-bottom,12px);\n padding-left: var(--c2-card--padding-left,16px);\n}\n\n.c2-card-footer {\n display: flex;\n align-items: center;\n gap: var(--c2-card__footer--gap,8px);\n justify-content: var(--c2-card__footer--justify-content,flex-start);\n padding-top: var(--c2-card__footer--padding-top,0px);\n padding-right: var(--c2-card__footer--padding-right,var(--c2-card--padding-right,16px));\n padding-bottom: var(--c2-card__footer--padding-bottom,var(--c2-card--padding-bottom,12px));\n padding-left: var(--c2-card__footer--padding-left,var(--c2-card--padding-left,16px));\n border-top: var(--c2-card__footer--border-top);\n}\n\n[hidden] {\n display: none !important;\n}\n\n:host([disabled]) .c2-card {\n pointer-events: none;\n box-shadow: var(--c2-card__disabled--box-shadow,var(--c2-card--box-shadow,none));\n opacity: var(--c2-card__disabled--opacity,0.38);\n}\n\n@media (prefers-reduced-motion: reduce) {\n .c2-card {\n transition: none;\n }\n}";
7
+ //#endregion
8
+ //#region \0@oxc-project+runtime@0.148.0/helpers/esm/decorate.js
9
+ function __decorate(decorators, target, key, desc) {
10
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
11
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
12
+ 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;
13
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
14
+ }
15
+ //#endregion
16
+ //#region src/card.ts
17
+ var Card = class Card extends LitElement {
18
+ constructor(..._args) {
19
+ super(..._args);
20
+ this.disabled = false;
21
+ this.interactive = false;
22
+ this.href = void 0;
23
+ this.target = void 0;
24
+ this.rel = void 0;
25
+ this.hasMedia = false;
26
+ this.hasHeader = false;
27
+ this.hasContent = false;
28
+ this.hasFooter = false;
29
+ }
30
+ static {
31
+ this.styles = unsafeCSS(card_default);
32
+ }
33
+ handleSlotChange(event) {
34
+ this.updateSection(event.target);
35
+ }
36
+ /** `slotchange` does not fire for slots that were server-rendered (declarative shadow DOM), so read every slot once. */
37
+ firstUpdated() {
38
+ for (const slot of this.renderRoot.querySelectorAll("slot")) this.updateSection(slot);
39
+ }
40
+ updateSection(slot) {
41
+ const filled = slot.assignedNodes({ flatten: true }).some((node) => node.nodeType === Node.ELEMENT_NODE || (node.textContent ?? "").trim() !== "");
42
+ switch (slot.name) {
43
+ case "media":
44
+ this.hasMedia = filled;
45
+ break;
46
+ case "header":
47
+ this.hasHeader = filled;
48
+ break;
49
+ case "footer":
50
+ this.hasFooter = filled;
51
+ break;
52
+ default: this.hasContent = filled;
53
+ }
54
+ }
55
+ handleKeydown(event) {
56
+ if (event.key === "Enter" || event.key === " ") {
57
+ event.preventDefault();
58
+ this.click();
59
+ }
60
+ }
61
+ renderSections() {
62
+ return html`
63
+ <div class="c2-card-media" ?hidden=${!this.hasMedia}><slot name="media" @slotchange=${this.handleSlotChange}></slot></div>
64
+ <div class="c2-card-header" ?hidden=${!this.hasHeader}><slot name="header" @slotchange=${this.handleSlotChange}></slot></div>
65
+ <div class="c2-card-content" ?hidden=${!this.hasContent}><slot @slotchange=${this.handleSlotChange}></slot></div>
66
+ <div class="c2-card-footer" ?hidden=${!this.hasFooter}><slot name="footer" @slotchange=${this.handleSlotChange}></slot></div>
67
+ `;
68
+ }
69
+ render() {
70
+ const isLink = !!this.href && !this.disabled;
71
+ const interactive = this.interactive || !!this.href;
72
+ const classes = classMap({
73
+ "c2-card": true,
74
+ "is-interactive": interactive
75
+ });
76
+ if (isLink) return html`<a class=${classes} href=${this.href} target=${ifDefined(this.target)} rel=${ifDefined(this.rel)}>${this.renderSections()}</a>`;
77
+ return html`
78
+ <div
79
+ class=${classes}
80
+ role=${interactive ? "button" : nothing}
81
+ tabindex=${interactive && !this.disabled ? "0" : nothing}
82
+ aria-disabled=${interactive && this.disabled ? "true" : nothing}
83
+ @keydown=${interactive && !this.disabled ? this.handleKeydown : nothing}
84
+ >
85
+ ${this.renderSections()}
86
+ </div>
87
+ `;
88
+ }
89
+ };
90
+ __decorate([property({
91
+ type: Boolean,
92
+ reflect: true
93
+ })], Card.prototype, "disabled", void 0);
94
+ __decorate([property({
95
+ type: Boolean,
96
+ reflect: true
97
+ })], Card.prototype, "interactive", void 0);
98
+ __decorate([property()], Card.prototype, "href", void 0);
99
+ __decorate([property()], Card.prototype, "target", void 0);
100
+ __decorate([property()], Card.prototype, "rel", void 0);
101
+ __decorate([state()], Card.prototype, "hasMedia", void 0);
102
+ __decorate([state()], Card.prototype, "hasHeader", void 0);
103
+ __decorate([state()], Card.prototype, "hasContent", void 0);
104
+ __decorate([state()], Card.prototype, "hasFooter", void 0);
105
+ Card = __decorate([customElement("c2-card")], Card);
106
+ //#endregion
107
+ export { Card };
package/package.json ADDED
@@ -0,0 +1,59 @@
1
+ {
2
+ "name": "@c2n/card",
3
+ "version": "0.0.6",
4
+ "type": "module",
5
+ "main": "dist/card.js",
6
+ "exports": {
7
+ ".": {
8
+ "default": "./dist/card.js",
9
+ "types": "./types/src/card.d.ts"
10
+ },
11
+ "./custom-elements.json": "./custom-elements.json"
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "types"
16
+ ],
17
+ "keywords": [
18
+ "card",
19
+ "web component",
20
+ "lit"
21
+ ],
22
+ "author": "code2nguyen@gmail.com",
23
+ "publishConfig": {
24
+ "registry": "https://registry.npmjs.org",
25
+ "access": "public"
26
+ },
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "https://github.com/code2nguyen/web-components.git"
30
+ },
31
+ "scripts": {
32
+ "dev": "vite",
33
+ "build": "wireit",
34
+ "type-check": "wireit"
35
+ },
36
+ "wireit": {
37
+ "type-check": {
38
+ "dependencies": [
39
+ "../../core:build"
40
+ ],
41
+ "command": "tsc -p tsconfig.lib.json --composite false"
42
+ },
43
+ "build": {
44
+ "dependencies": [
45
+ "type-check"
46
+ ],
47
+ "command": "vite build"
48
+ }
49
+ },
50
+ "dependencies": {
51
+ "@c2n/core": "0.0.6",
52
+ "lit": "3.3.3"
53
+ },
54
+ "devDependencies": {
55
+ "@c2n/config": "*"
56
+ },
57
+ "customElements": "custom-elements.json",
58
+ "gitHead": "2921054bc75299da66dad11c1e374246ff88a51e"
59
+ }
@@ -0,0 +1,94 @@
1
+ import { LitElement } from 'lit';
2
+ /**
3
+ * Surface that groups related content and actions. The body goes in the default slot; `media`, `header` and `footer`
4
+ * are optional sections that render only when filled: media bleeds to the edges at the top, header and footer get
5
+ * their own padding and divider tokens. With `href` the whole card is a link; with `interactive` it behaves like a
6
+ * button (focusable, Enter and Space click it). Hover, focus and disabled looks are themeable.
7
+ *
8
+ * @tag c2-card
9
+ *
10
+ * @slot - Card body.
11
+ * @slot media - Full-bleed image, video or any block shown above the body; it is clipped to the card radius.
12
+ * @slot header - Title area above the body.
13
+ * @slot footer - Actions row below the body (a flex row, see the `footer--*` tokens).
14
+ *
15
+ * @cssproperty {padding} [--c2-card--padding-top=12px]
16
+ * @cssproperty {padding} [--c2-card--padding-right=16px]
17
+ * @cssproperty {padding} [--c2-card--padding-bottom=12px]
18
+ * @cssproperty {padding} [--c2-card--padding-left=16px]
19
+ *
20
+ * @cssproperty {border-radius} [--c2-card--border-top-left-radius=8px]
21
+ * @cssproperty {border-radius} [--c2-card--border-top-right-radius=8px]
22
+ * @cssproperty {border-radius} [--c2-card--border-bottom-left-radius=8px]
23
+ * @cssproperty {border-radius} [--c2-card--border-bottom-right-radius=8px]
24
+ *
25
+ * @cssproperty {border} [--c2-card--border-top=1px solid rgb(213, 213, 213)]
26
+ * @cssproperty {border} [--c2-card--border-bottom=1px solid rgb(213, 213, 213)]
27
+ * @cssproperty {border} [--c2-card--border-right=1px solid rgb(213, 213, 213)]
28
+ * @cssproperty {border} [--c2-card--border-left=1px solid rgb(213, 213, 213)]
29
+ *
30
+ * @cssproperty {background} [--c2-card--background=rgb(255, 255, 255)]
31
+ * @cssproperty {color} --c2-card--color - Text color of the card; inherits by default.
32
+ * @cssproperty {box-shadow} [--c2-card--box-shadow=none]
33
+ * @cssproperty {transform} --c2-card--transform
34
+ *
35
+ * @cssproperty {padding} --c2-card__header--padding-top - Falls back to `--c2-card--padding-top`.
36
+ * @cssproperty {padding} --c2-card__header--padding-right - Falls back to `--c2-card--padding-right`.
37
+ * @cssproperty {padding} [--c2-card__header--padding-bottom=0px]
38
+ * @cssproperty {padding} --c2-card__header--padding-left - Falls back to `--c2-card--padding-left`.
39
+ * @cssproperty {border} --c2-card__header--border-bottom - Divider under the header; raise `header--padding-bottom` with it.
40
+ *
41
+ * @cssproperty {padding} [--c2-card__footer--padding-top=0px]
42
+ * @cssproperty {padding} --c2-card__footer--padding-right - Falls back to `--c2-card--padding-right`.
43
+ * @cssproperty {padding} --c2-card__footer--padding-bottom - Falls back to `--c2-card--padding-bottom`.
44
+ * @cssproperty {padding} --c2-card__footer--padding-left - Falls back to `--c2-card--padding-left`.
45
+ * @cssproperty {border} --c2-card__footer--border-top - Divider above the footer; raise `footer--padding-top` with it.
46
+ * @cssproperty {pixel} [--c2-card__footer--gap=8px]
47
+ * @cssproperty {justify-content} [--c2-card__footer--justify-content=flex-start]
48
+ *
49
+ * @cssproperty {pixel} --c2-card__media--height
50
+ * @cssproperty {aspect-ratio} --c2-card__media--aspect-ratio
51
+ * @cssproperty {object-fit} [--c2-card__media--object-fit=cover]
52
+ *
53
+ * @cssproperty {border} --c2-card__hover--border-top
54
+ * @cssproperty {border} --c2-card__hover--border-bottom
55
+ * @cssproperty {border} --c2-card__hover--border-right
56
+ * @cssproperty {border} --c2-card__hover--border-left
57
+ * @cssproperty {background} --c2-card__hover--background
58
+ * @cssproperty {box-shadow} --c2-card__hover--box-shadow
59
+ * @cssproperty {transform} --c2-card__hover--transform - e.g. `translateY(-2px)` for a lift.
60
+ *
61
+ * @cssproperty {outline} [--c2-card__focus--outline=2px solid rgba(2, 101, 220, 0.4)]
62
+ * @cssproperty {pixel} [--c2-card__focus--outline-offset=2px]
63
+ *
64
+ * @cssproperty {box-shadow} --c2-card__disabled--box-shadow
65
+ * @cssproperty {opacity} [--c2-card__disabled--opacity=0.38]
66
+ */
67
+ export declare class Card extends LitElement {
68
+ static styles: import("lit").CSSResult;
69
+ /** Dims the card and ignores pointer and keyboard interaction. A link card renders without its anchor. */
70
+ disabled: boolean;
71
+ /** Button-like card: focusable, `role="button"`, Enter and Space dispatch `click`, pointer cursor. Implied by `href`. */
72
+ interactive: boolean;
73
+ /** Makes the whole card a link. */
74
+ href: string | undefined;
75
+ target: string | undefined;
76
+ rel: string | undefined;
77
+ private hasMedia;
78
+ private hasHeader;
79
+ private hasContent;
80
+ private hasFooter;
81
+ private handleSlotChange;
82
+ /** `slotchange` does not fire for slots that were server-rendered (declarative shadow DOM), so read every slot once. */
83
+ firstUpdated(): void;
84
+ private updateSection;
85
+ private handleKeydown;
86
+ private renderSections;
87
+ render(): import("lit-html").TemplateResult<1>;
88
+ }
89
+ declare global {
90
+ interface HTMLElementTagNameMap {
91
+ 'c2-card': Card;
92
+ }
93
+ }
94
+ //# sourceMappingURL=card.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"card.d.ts","sourceRoot":"","sources":["../../src/card.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAA4B,MAAM,KAAK,CAAA;AAM1D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgEG;AACH,qBACa,IAAK,SAAQ,UAAU;IAClC,OAAgB,MAAM,0BAAoB;IAE1C,0GAA0G;IAC9D,QAAQ,UAAQ;IAE5D,yHAAyH;IAC7E,WAAW,UAAQ;IAE/D,mCAAmC;IACvB,IAAI,EAAE,MAAM,GAAG,SAAS,CAAY;IAEpC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAY;IAEtC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAY;IAEtC,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,UAAU,CAAQ;IAC1B,OAAO,CAAC,SAAS,CAAQ;IAElC,OAAO,CAAC,gBAAgB;IAIxB,wHAAwH;IAC/G,YAAY;IAIrB,OAAO,CAAC,aAAa;IAiBrB,OAAO,CAAC,aAAa;IAOrB,OAAO,CAAC,cAAc;IASb,MAAM;CAoBhB;AAED,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,SAAS,EAAE,IAAI,CAAA;KAChB;CACF"}