@joralvmel/cosmos-elements 0.1.0 → 0.3.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/README.md CHANGED
@@ -23,9 +23,19 @@ import '@joralvmel/cosmos-elements/button';
23
23
  | Element | Import | Description |
24
24
  | --------------- | ----------------------------------- | ---------------------------------- |
25
25
  | `<cosm-button>` | `@joralvmel/cosmos-elements/button` | Action or form submit/reset button |
26
+ | `<cosm-text-field>` | `@joralvmel/cosmos-elements/text-field` | Labeled single-line entry with browser form validation |
26
27
 
27
28
  The Custom Elements Manifest is published as `@joralvmel/cosmos-elements/custom-elements.json`.
28
29
 
30
+ Text-field start/end icons support independent `--cosm-text-field-start-icon-color` and
31
+ `--cosm-text-field-end-icon-color` hooks, with optional `-focus-color` equivalents replacing
32
+ `-color`. `icon-focus-scope="control"` (default) includes the input and action buttons;
33
+ `"input"` limits those focus colors to the editable input. Unset hooks preserve existing colors;
34
+ affixes and action icons are unaffected.
35
+
36
+ Text fields default to `20ch` wide, capped at the container width. Set CSS `width` to fit your
37
+ layout. Clear/reveal actions and decorations take space inside the field without changing its width.
38
+
29
39
  ## License
30
40
 
31
41
  MIT
@@ -0,0 +1,65 @@
1
+ import { type ContractProps, iconContract } from '@joralvmel/cosmos-core';
2
+ import { type IconDefinition } from '@joralvmel/cosmos-icons';
3
+ import { CosmElement } from '../../internal/cosm-element.js';
4
+ type Props = ContractProps<typeof iconContract, 'web', {
5
+ IconDefinition: IconDefinition;
6
+ }>;
7
+ export type CosmIconSize = Exclude<Props['size'], undefined>;
8
+ /**
9
+ * Renders a structured SVG icon as a decorative graphic or a named informative image.
10
+ *
11
+ * @tag cosm-icon
12
+ * @csspart icon - The rendered SVG image.
13
+ * @cssprop --cosm-icon-size - Override the rendered icon width and height.
14
+ */
15
+ export declare class CosmIcon extends CosmElement {
16
+ #private;
17
+ static properties: {
18
+ icon: {
19
+ attribute: boolean;
20
+ };
21
+ size: {
22
+ reflect: boolean;
23
+ converter: {
24
+ fromAttribute: (value: string | null) => string | number | null;
25
+ toAttribute: (value: CosmIconSize) => string;
26
+ };
27
+ };
28
+ stroke: {
29
+ type: StringConstructor;
30
+ };
31
+ fill: {
32
+ type: StringConstructor;
33
+ };
34
+ strokeWidth: {
35
+ type: NumberConstructor;
36
+ attribute: string;
37
+ };
38
+ label: {
39
+ type: StringConstructor;
40
+ };
41
+ decorative: {
42
+ type: BooleanConstructor;
43
+ reflect: boolean;
44
+ };
45
+ };
46
+ static styles: import("lit").CSSResult[];
47
+ icon: Props['icon'];
48
+ size: CosmIconSize;
49
+ stroke: string;
50
+ fill: string;
51
+ strokeWidth: number;
52
+ label: string | undefined;
53
+ decorative: boolean;
54
+ constructor();
55
+ protected updated(): void;
56
+ protected willUpdate(): void;
57
+ protected render(): import("lit-html").TemplateResult<1>;
58
+ }
59
+ declare global {
60
+ interface HTMLElementTagNameMap {
61
+ 'cosm-icon': CosmIcon;
62
+ }
63
+ }
64
+ export {};
65
+ //# sourceMappingURL=cosm-icon.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cosm-icon.d.ts","sourceRoot":"","sources":["../../../src/components/icon/cosm-icon.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAE,YAAY,EAAE,MAAM,wBAAwB,CAAC;AAC1E,OAAO,EAAE,KAAK,cAAc,EAAoB,MAAM,yBAAyB,CAAC;AAEhF,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAI7D,KAAK,KAAK,GAAG,aAAa,CAAC,OAAO,YAAY,EAAE,KAAK,EAAE;IAAE,cAAc,EAAE,cAAc,CAAA;CAAE,CAAC,CAAC;AAC3F,MAAM,MAAM,YAAY,GAAG,OAAO,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,SAAS,CAAC,CAAC;AA6B7D;;;;;;GAMG;AACH,qBAAa,QAAS,SAAQ,WAAW;;IACvC,OAAgB,UAAU;;;;;;;uCAKG,MAAM,GAAG,IAAI;qCAIf,YAAY;;;;;;;;;;;;;;;;;;;;MAQrC;IAEF,OAAgB,MAAM,4BAAuB;IAErC,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACpB,IAAI,EAAE,YAAY,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,EAAE,MAAM,CAAC;IACpB,KAAK,EAAE,MAAM,GAAG,SAAS,CAAC;IAC1B,UAAU,EAAE,OAAO,CAAC;;cAYT,OAAO;cAcP,UAAU;cAsCV,MAAM;CAmB1B;AAID,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,WAAW,EAAE,QAAQ,CAAC;KACvB;CACF"}
@@ -0,0 +1,137 @@
1
+ import { iconContract } from '@joralvmel/cosmos-core';
2
+ import { isIconDefinition } from '@joralvmel/cosmos-icons';
3
+ import { css, html, nothing } from 'lit';
4
+ import { CosmElement } from '../../internal/cosm-element.js';
5
+ import { define } from '../../internal/define.js';
6
+ import { boxSizing } from '../../internal/styles.js';
7
+ const svgNamespace = 'http://www.w3.org/2000/svg';
8
+ const styles = css `
9
+ :host {
10
+ display: inline-block;
11
+ width: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-md, 20px)));
12
+ height: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-md, 20px)));
13
+ vertical-align: middle;
14
+ }
15
+ :host([size='sm']) {
16
+ width: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-sm, 16px)));
17
+ height: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-sm, 16px)));
18
+ }
19
+ :host([size='lg']) {
20
+ width: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-lg, 24px)));
21
+ height: var(--cosm-icon-size, var(--_cosm-icon-size, var(--cosm-icon-size-lg, 24px)));
22
+ }
23
+ svg {
24
+ display: block;
25
+ width: 100%;
26
+ height: 100%;
27
+ overflow: visible;
28
+ stroke-linecap: round;
29
+ stroke-linejoin: round;
30
+ }
31
+ `;
32
+ /**
33
+ * Renders a structured SVG icon as a decorative graphic or a named informative image.
34
+ *
35
+ * @tag cosm-icon
36
+ * @csspart icon - The rendered SVG image.
37
+ * @cssprop --cosm-icon-size - Override the rendered icon width and height.
38
+ */
39
+ export class CosmIcon extends CosmElement {
40
+ static properties = {
41
+ icon: { attribute: false },
42
+ size: {
43
+ reflect: true,
44
+ converter: {
45
+ fromAttribute: (value) => value !== null && value.trim() !== '' && Number.isFinite(Number(value))
46
+ ? Number(value)
47
+ : value,
48
+ toAttribute: (value) => String(value),
49
+ },
50
+ },
51
+ stroke: { type: String },
52
+ fill: { type: String },
53
+ strokeWidth: { type: Number, attribute: 'stroke-width' },
54
+ label: { type: String },
55
+ decorative: { type: Boolean, reflect: true },
56
+ };
57
+ static styles = [boxSizing, styles];
58
+ constructor() {
59
+ super();
60
+ this.size = iconContract.props.size.default;
61
+ this.stroke = iconContract.props.stroke.default;
62
+ this.fill = iconContract.props.fill.default;
63
+ this.strokeWidth = iconContract.props.strokeWidth.default;
64
+ this.label = undefined;
65
+ this.decorative = iconContract.props.decorative.default;
66
+ }
67
+ updated() {
68
+ const icon = this.#requireIcon();
69
+ if (!icon)
70
+ return;
71
+ const svg = this.renderRoot.querySelector('svg');
72
+ if (!svg)
73
+ return;
74
+ svg.replaceChildren();
75
+ for (const [tag, attributes] of icon.nodes) {
76
+ const node = document.createElementNS(svgNamespace, tag);
77
+ for (const [name, value] of Object.entries(attributes))
78
+ node.setAttribute(name, String(value));
79
+ svg.append(node);
80
+ }
81
+ }
82
+ willUpdate() {
83
+ if (!(typeof this.size === 'number' && Number.isFinite(this.size) && this.size > 0) &&
84
+ this.size !== 'sm' &&
85
+ this.size !== 'md' &&
86
+ this.size !== 'lg')
87
+ throw new TypeError('Icon size must be sm, md, lg or a positive finite number.');
88
+ if (!Number.isFinite(this.strokeWidth) || this.strokeWidth < 0)
89
+ throw new TypeError('Icon strokeWidth must be a nonnegative finite number.');
90
+ for (const paint of [this.stroke, this.fill]) {
91
+ if (typeof paint !== 'string' ||
92
+ !paint.trim() ||
93
+ /url\s*\(|javascript:|[;{}\\]/i.test(paint) ||
94
+ [...paint].some(character => character.charCodeAt(0) < 32) ||
95
+ (typeof CSS !== 'undefined' &&
96
+ typeof CSS.supports === 'function' &&
97
+ !CSS.supports('fill', paint)))
98
+ throw new TypeError('Icon paint must be a supported SVG paint without a URL.');
99
+ }
100
+ if (typeof this.size === 'number')
101
+ this.style.setProperty('--_cosm-icon-size', `${this.size}px`);
102
+ else
103
+ this.style.removeProperty('--_cosm-icon-size');
104
+ }
105
+ #requireIcon() {
106
+ if (this.icon === undefined)
107
+ return undefined;
108
+ if (!isIconDefinition(this.icon)) {
109
+ throw new TypeError('cosm-icon requires a valid IconDefinition in its "icon" property.');
110
+ }
111
+ if (!this.decorative && !this.label?.trim()) {
112
+ throw new TypeError('cosm-icon requires a non-empty "label" when decorative is false.');
113
+ }
114
+ return this.icon;
115
+ }
116
+ render() {
117
+ const icon = this.#requireIcon();
118
+ if (!icon) {
119
+ return html `<span part="error" role="alert">
120
+ cosm-icon requires a valid IconDefinition in its "icon" property.
121
+ </span>`;
122
+ }
123
+ return html `<svg
124
+ part="icon"
125
+ aria-hidden=${this.decorative ? 'true' : nothing}
126
+ role=${this.decorative ? nothing : 'img'}
127
+ aria-label=${this.decorative ? nothing : this.label}
128
+ focusable="false"
129
+ viewBox=${icon.viewBox}
130
+ stroke=${this.stroke}
131
+ fill=${this.fill}
132
+ stroke-width=${this.strokeWidth}
133
+ ></svg>`;
134
+ }
135
+ }
136
+ define('cosm-icon', CosmIcon);
137
+ //# sourceMappingURL=cosm-icon.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cosm-icon.js","sourceRoot":"","sources":["../../../src/components/icon/cosm-icon.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,YAAY,EAAE,MAAM,wBAAwB,CAAC;A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YAAY;QACV,IAAI,IAAI,CAAC,IAAI,KAAK,SAAS;YAAE,OAAO,SAAS,CAAC;QAC9C,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;YACjC,MAAM,IAAI,SAAS,CAAC,mEAAmE,CAAC,CAAC;QAC3F,CAAC;QACD,IAAI,CAAC,IAAI,CAAC,UAAU,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC;YAC5C,MAAM,IAAI,SAAS,CAAC,kEAAkE,CAAC,CAAC;QAC1F,CAAC;QACD,OAAO,IAAI,CAAC,IAAI,CAAC;IACnB,CAAC;IAEkB,MAAM;QACvB,MAAM,IAAI,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QACjC,IAAI,CAAC,IAAI,EAAE,CAAC;YACV,OAAO,IAAI,CAAA;;cAEH,CAAC;QACX,CAAC;QACD,OAAO,IAAI,CAAA;;oBAEK,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO;aACzC,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,KAAK;mBAC3B,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK;;gBAEzC,IAAI,CAAC,OAAO;eACb,IAAI,CAAC,MAAM;aACb,IAAI,CAAC,IAAI;qBACD,IAAI,CAAC,WAAW;YACzB,CAAC;IACX,CAAC;;AAGH,MAAM,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC"}
@@ -0,0 +1,79 @@
1
+ import { type ContractProps, type IconButtonPressedChangeDetail, iconButtonContract } from '@joralvmel/cosmos-core';
2
+ import { CosmElement } from '../../internal/cosm-element.js';
3
+ type Props = Required<ContractProps<typeof iconButtonContract, 'web'>>;
4
+ /**
5
+ * Labeled icon-only action or toggle.
6
+ * @tag cosm-icon-button
7
+ * @slot - Decorative icon.
8
+ * @csspart base - Native button.
9
+ * @fires {CustomEvent<IconButtonPressDetail>} cosm-press - Activation.
10
+ * @fires {CustomEvent<IconButtonPressedChangeDetail>} cosm-pressed-change - Proposed toggle state.
11
+ * @cssprop --cosm-icon-button-background - Background.
12
+ * @cssprop --cosm-icon-button-color - Icon color.
13
+ * @cssprop --cosm-icon-button-border - Border color.
14
+ * @cssprop --cosm-icon-button-radius - Corner radius.
15
+ * @cssprop --cosm-icon-button-focus - Focus indicator color.
16
+ */
17
+ export declare class CosmIconButton extends CosmElement {
18
+ #private;
19
+ static formAssociated: boolean;
20
+ static shadowRootOptions: ShadowRootInit;
21
+ static properties: {
22
+ label: {};
23
+ mode: {
24
+ reflect: boolean;
25
+ };
26
+ variant: {
27
+ reflect: boolean;
28
+ };
29
+ tone: {
30
+ reflect: boolean;
31
+ };
32
+ size: {
33
+ reflect: boolean;
34
+ };
35
+ disabled: {
36
+ type: BooleanConstructor;
37
+ reflect: boolean;
38
+ };
39
+ type: {};
40
+ pressed: {
41
+ type: BooleanConstructor;
42
+ noAccessor: boolean;
43
+ };
44
+ defaultPressed: {
45
+ type: BooleanConstructor;
46
+ attribute: string;
47
+ };
48
+ };
49
+ static styles: import("lit").CSSResult[];
50
+ label: string;
51
+ mode: Props['mode'];
52
+ variant: Props['variant'];
53
+ tone: Props['tone'];
54
+ size: Props['size'];
55
+ disabled: boolean;
56
+ type: Props['type'];
57
+ defaultPressed: boolean;
58
+ /** Adapters set ownership when a pressed binding is present. */
59
+ pressedControlled: boolean;
60
+ constructor();
61
+ get pressed(): boolean;
62
+ set pressed(value: boolean | undefined);
63
+ get form(): HTMLFormElement | null;
64
+ attributeChangedCallback(name: string, old: string | null, value: string | null): void;
65
+ formDisabledCallback(disabled: boolean): void;
66
+ protected willUpdate(changed: Map<PropertyKey, unknown>): void;
67
+ click(): void;
68
+ protected render(): import("lit-html").TemplateResult<1>;
69
+ }
70
+ declare global {
71
+ interface HTMLElementTagNameMap {
72
+ 'cosm-icon-button': CosmIconButton;
73
+ }
74
+ interface HTMLElementEventMap {
75
+ 'cosm-pressed-change': CustomEvent<IconButtonPressedChangeDetail>;
76
+ }
77
+ }
78
+ export {};
79
+ //# sourceMappingURL=cosm-icon-button.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cosm-icon-button.d.ts","sourceRoot":"","sources":["../../../src/components/icon-button/cosm-icon-button.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,aAAa,EAElB,KAAK,6BAA6B,EAClC,kBAAkB,EAEnB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAI7D,KAAK,KAAK,GAAG,QAAQ,CAAC,aAAa,CAAC,OAAO,kBAAkB,EAAE,KAAK,CAAC,CAAC,CAAC;AAwEvE;;;;;;;;;;;;GAYG;AACH,qBAAa,cAAe,SAAQ,WAAW;;IAC7C,MAAM,CAAC,cAAc,UAAQ;IAC7B,OAAgB,iBAAiB,EAAE,cAAc,CAG/C;IACF,OAAgB,UAAU;;;;;;;;;;;;;;;;;;;;;;;;;;;MAUxB;IACF,OAAgB,MAAM,4BAAuB;IAErC,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACpB,OAAO,EAAE,KAAK,CAAC,SAAS,CAAC,CAAC;IAC1B,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACpB,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACpB,QAAQ,EAAE,OAAO,CAAC;IAClB,IAAI,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;IACpB,cAAc,EAAE,OAAO,CAAC;IAEhC,gEAAgE;IAChE,iBAAiB,UAAS;;IAsB1B,IAAI,OAAO,IAAI,OAAO,CAErB;IACD,IAAI,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,SAAS,EAGrC;IAMD,IAAI,IAAI,IAAI,eAAe,GAAG,IAAI,CAEjC;IACQ,wBAAwB,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI;IAI/F,oBAAoB,CAAC,QAAQ,EAAE,OAAO,GAAG,IAAI;cAI1B,UAAU,CAAC,OAAO,EAAE,GAAG,CAAC,WAAW,EAAE,OAAO,CAAC,GAAG,IAAI;IAK9D,KAAK,IAAI,IAAI;cA+BH,MAAM;CAU1B;AAID,OAAO,CAAC,MAAM,CAAC;IACb,UAAU,qBAAqB;QAC7B,kBAAkB,EAAE,cAAc,CAAC;KACpC;IACD,UAAU,mBAAmB;QAC3B,qBAAqB,EAAE,WAAW,CAAC,6BAA6B,CAAC,CAAC;KACnE;CACF"}
@@ -0,0 +1,206 @@
1
+ import { iconButtonContract, } from '@joralvmel/cosmos-core';
2
+ import { css, html, LitElement, nothing } from 'lit';
3
+ import { CosmElement } from '../../internal/cosm-element.js';
4
+ import { define } from '../../internal/define.js';
5
+ import { boxSizing } from '../../internal/styles.js';
6
+ const styles = css `
7
+ :host {
8
+ --_solid: var(--cosm-primary, oklch(0.55 0.2 268));
9
+ --_on-solid: var(--cosm-primary-fg, oklch(1 0 0));
10
+ --_subtle: var(--cosm-primary-subtle, oklch(0.945 0.048 268));
11
+ --_text: var(--cosm-primary-text, oklch(0.44 0.18 268));
12
+ --_size: var(--cosm-control-md, 2.5rem);
13
+ display: inline-flex;
14
+ vertical-align: middle;
15
+ }
16
+ :host([tone='neutral']) {
17
+ --_solid: var(--cosm-fg, oklch(0.24 0.01 268));
18
+ --_on-solid: var(--cosm-bg, oklch(0.985 0.007 268));
19
+ --_subtle: var(--cosm-neutral-3, oklch(0.945 0.01 268));
20
+ --_text: var(--cosm-fg, oklch(0.24 0.01 268));
21
+ }
22
+ :host([tone='danger']) {
23
+ --_solid: var(--cosm-danger, oklch(0.55 0.16 19));
24
+ --_on-solid: var(--cosm-danger-fg, oklch(1 0 0));
25
+ --_subtle: var(--cosm-danger-subtle, oklch(0.945 0.038 19));
26
+ --_text: var(--cosm-danger-text, oklch(0.44 0.144 19));
27
+ }
28
+ :host([size='sm']) { --_size: var(--cosm-control-sm, 2rem); }
29
+ :host([size='lg']) { --_size: var(--cosm-control-lg, 3rem); }
30
+ .base {
31
+ --_bg: var(--_solid);
32
+ --_fg: var(--_on-solid);
33
+ --_border: transparent;
34
+ display: inline-flex;
35
+ align-items: center;
36
+ justify-content: center;
37
+ width: var(--_size);
38
+ height: var(--_size);
39
+ padding: 0;
40
+ border: 1px solid var(--cosm-icon-button-border, var(--_border));
41
+ border-radius: var(--cosm-icon-button-radius, var(--cosm-radius-md, 0.5rem));
42
+ background: var(--cosm-icon-button-background, var(--_bg));
43
+ color: var(--cosm-icon-button-color, var(--_fg));
44
+ font: inherit;
45
+ cursor: pointer;
46
+ transition: background-color var(--cosm-duration-fast, 120ms) var(--cosm-easing-standard, ease);
47
+ }
48
+ .icon {
49
+ display: flex;
50
+ align-items: center;
51
+ justify-content: center;
52
+ }
53
+ :host([variant='soft']) .base { --_bg: var(--_subtle); --_fg: var(--_text); }
54
+ :host([variant='outline']) .base {
55
+ --_bg: transparent;
56
+ --_fg: var(--_text);
57
+ --_border: var(--cosm-border-strong, oklch(0.64 0.01 268));
58
+ }
59
+ :host([variant='ghost']) .base { --_bg: transparent; --_fg: var(--_text); }
60
+ .base:hover { --_bg: color-mix(in oklab, var(--_solid), var(--_on-solid) 14%); }
61
+ :host([variant='soft']) .base:hover,
62
+ :host([variant='outline']) .base:hover,
63
+ :host([variant='ghost']) .base:hover { --_bg: var(--_subtle); }
64
+ .base:active { filter: brightness(0.9); }
65
+ .base[aria-pressed='true'] {
66
+ box-shadow: inset 0 0 0 2px currentColor;
67
+ }
68
+ .base:focus-visible {
69
+ outline: 2px solid var(--cosm-icon-button-focus, var(--cosm-focus-ring, oklch(0.55 0.2 268)));
70
+ outline-offset: 2px;
71
+ }
72
+ .base:disabled { cursor: not-allowed; opacity: 0.5; }
73
+ @media (prefers-reduced-motion: reduce) { .base { transition: none; } }
74
+ `;
75
+ /**
76
+ * Labeled icon-only action or toggle.
77
+ * @tag cosm-icon-button
78
+ * @slot - Decorative icon.
79
+ * @csspart base - Native button.
80
+ * @fires {CustomEvent<IconButtonPressDetail>} cosm-press - Activation.
81
+ * @fires {CustomEvent<IconButtonPressedChangeDetail>} cosm-pressed-change - Proposed toggle state.
82
+ * @cssprop --cosm-icon-button-background - Background.
83
+ * @cssprop --cosm-icon-button-color - Icon color.
84
+ * @cssprop --cosm-icon-button-border - Border color.
85
+ * @cssprop --cosm-icon-button-radius - Corner radius.
86
+ * @cssprop --cosm-icon-button-focus - Focus indicator color.
87
+ */
88
+ export class CosmIconButton extends CosmElement {
89
+ static formAssociated = true;
90
+ static shadowRootOptions = {
91
+ ...LitElement.shadowRootOptions,
92
+ delegatesFocus: true,
93
+ };
94
+ static properties = {
95
+ label: {},
96
+ mode: { reflect: true },
97
+ variant: { reflect: true },
98
+ tone: { reflect: true },
99
+ size: { reflect: true },
100
+ disabled: { type: Boolean, reflect: true },
101
+ type: {},
102
+ pressed: { type: Boolean, noAccessor: true },
103
+ defaultPressed: { type: Boolean, attribute: 'default-pressed' },
104
+ };
105
+ static styles = [boxSizing, styles];
106
+ /** Adapters set ownership when a pressed binding is present. */
107
+ pressedControlled = false;
108
+ #pressed = false;
109
+ #edited = false;
110
+ #formDisabled = false;
111
+ #nextSource;
112
+ #internals;
113
+ constructor() {
114
+ super();
115
+ const props = iconButtonContract.props;
116
+ this.label = '';
117
+ this.mode = props.mode.default;
118
+ this.variant = props.variant.default;
119
+ this.tone = props.tone.default;
120
+ this.size = props.size.default;
121
+ this.disabled = props.disabled.default;
122
+ this.type = props.type.default;
123
+ this.defaultPressed = props.defaultPressed.default;
124
+ this.#internals =
125
+ typeof this.attachInternals === 'function' ? this.attachInternals() : undefined;
126
+ }
127
+ get pressed() {
128
+ return this.#pressed;
129
+ }
130
+ set pressed(value) {
131
+ this.pressedControlled = value !== undefined;
132
+ if (value !== undefined)
133
+ this.#setPressed(value);
134
+ }
135
+ #setPressed(value) {
136
+ const old = this.#pressed;
137
+ this.#pressed = value;
138
+ this.requestUpdate('pressed', old);
139
+ }
140
+ get form() {
141
+ return this.#internals?.form ?? this.closest('form');
142
+ }
143
+ attributeChangedCallback(name, old, value) {
144
+ super.attributeChangedCallback(name, old, value);
145
+ if (name === 'pressed' && value === null)
146
+ this.pressedControlled = false;
147
+ }
148
+ formDisabledCallback(disabled) {
149
+ this.#formDisabled = disabled;
150
+ this.requestUpdate();
151
+ }
152
+ willUpdate(changed) {
153
+ if (changed.has('defaultPressed') && !this.pressedControlled && !this.#edited) {
154
+ this.#setPressed(this.defaultPressed);
155
+ }
156
+ }
157
+ click() {
158
+ const button = this.renderRoot.querySelector('button');
159
+ if (!button || button.disabled)
160
+ return;
161
+ this.#nextSource = 'programmatic';
162
+ button.click();
163
+ }
164
+ #onPointerDown = () => {
165
+ this.#nextSource = 'pointer';
166
+ };
167
+ #onKeyDown = (event) => {
168
+ if (event.key === 'Enter' || event.key === ' ')
169
+ this.#nextSource = 'keyboard';
170
+ };
171
+ #onClick = (event) => {
172
+ const source = this.#nextSource ?? (event.detail === 0 ? 'keyboard' : 'pointer');
173
+ this.#nextSource = undefined;
174
+ if (this.disabled || this.#formDisabled) {
175
+ event.preventDefault();
176
+ event.stopPropagation();
177
+ return;
178
+ }
179
+ if (this.mode === 'toggle') {
180
+ const pressed = !this.#pressed;
181
+ this.#edited = true;
182
+ if (!this.pressedControlled)
183
+ this.#setPressed(pressed);
184
+ this.emit('cosm-pressed-change', { pressed });
185
+ }
186
+ const form = this.form;
187
+ if (form && this.type === 'submit')
188
+ form.requestSubmit();
189
+ if (form && this.type === 'reset')
190
+ form.reset();
191
+ this.emit('cosm-press', { source });
192
+ };
193
+ render() {
194
+ if (!this.label?.trim())
195
+ throw new Error('cosm-icon-button requires a non-empty label.');
196
+ return html `<button
197
+ part="base" class="base" type="button"
198
+ ?disabled=${this.disabled || this.#formDisabled}
199
+ aria-label=${this.label}
200
+ aria-pressed=${this.mode === 'toggle' ? String(this.#pressed) : nothing}
201
+ @pointerdown=${this.#onPointerDown} @keydown=${this.#onKeyDown} @click=${this.#onClick}
202
+ ><span class="icon" aria-hidden="true"><slot></slot></span></button>`;
203
+ }
204
+ }
205
+ define('cosm-icon-button', CosmIconButton);
206
+ //# sourceMappingURL=cosm-icon-button.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cosm-icon-button.js","sourceRoot":"","sources":["../../../src/components/icon-button/cosm-icon-button.ts"],"names":[],"mappings":"AAAA,OAAO,EAIL,kBAAkB,GAEnB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,gCAAgC,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,0BAA0B,CAAC;AAIrD,MAAM,MAAM,GAAG,GAAG,CAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAoEjB,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,OAAO,cAAe,SAAQ,WAAW;IAC7C,MAAM,CAAC,cAAc,GAAG,IAAI,CAAC;IAC7B,MAAM,CAAU,iBAAiB,GAAmB;QAClD,GAAG,UAAU,CAAC,iBAAiB;QAC/B,cAAc,EAAE,IAAI;KACrB,CAAC;IACF,MAAM,CAAU,UAAU,GAAG;QAC3B,KAAK,EAAE,EAAE;QACT,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;QACvB,OAAO,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;QAC1B,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,EAAE;QACvB,IAAI,EAAE,EAAE,OAAO,EAAE,IAAI,E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