@commmerce/ui 1.0.0 → 1.1.1

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.
@@ -0,0 +1,71 @@
1
+ import { LitElement } from 'lit';
2
+ /**
3
+ * The admin's frame: puts the Header bar, the Side navigation, the page, the Bottom navigation and the Mobile
4
+ * menu in their places, so every Commmerce product gets the same grid. For Commmerce products only, not for apps:
5
+ * apps run inside the admin, which already has this frame.
6
+ *
7
+ * When the frame is 1024px wide or more, the side navigation sits on the start side under the header; narrower,
8
+ * it is hidden and the bottom navigation is fixed to the bottom of the window. The frame follows its own width
9
+ * (normally the window's). The header sticks to the top (below the device's notch or status bar), the side
10
+ * navigation sticks under it and scrolls on its own when it is taller than the window, and the page scrolls with
11
+ * the window. The page gets bottom padding as tall as the bottom navigation, so nothing hides behind it.
12
+ *
13
+ * The mobile menu is a full-screen layer above the page and below the bottom navigation, shown while
14
+ * `menu-open` is set. Set `menu-item` to the value of the bottom navigation item that opens it (usually `more`)
15
+ * and the frame opens and closes it for you; otherwise listen for `cm-navigate` and set `menu-open` yourself.
16
+ *
17
+ * The frame adds one landmark, `main`, around the page. The header bar (`banner`) and the navigations (`nav`)
18
+ * bring their own, so don't wrap them, and don't put another `main` in the page.
19
+ * @element cm-app-frame
20
+ * @slot header - A `cm-header-bar`.
21
+ * @slot navigation - A `cm-side-navigation`. Shown when the frame is 1024px wide or more. Collapse it with its `collapsed` attribute; the frame follows its width.
22
+ * @slot - The page, usually a `cm-page`. Wrapped in the frame's `main` landmark.
23
+ * @slot bottom-navigation - A `cm-bottom-navigation`. Shown, fixed to the bottom of the window, when the frame is narrower than 1024px. No need to set its `fixed` attribute.
24
+ * @slot mobile-menu - A `cm-mobile-menu`. Shown over the page while `menu-open` is set and the frame is narrower than 1024px.
25
+ * @fires cm-menu-toggle - When the frame opens or closes the mobile menu itself: from the `menu-item` in the bottom navigation, Escape, or a choice in the menu. `detail: { open }`. Not fired when you set `menu-open` yourself.
26
+ * @cssprop --cm-app-frame-height - Height the side navigation fits in (it scrolls when taller). Default `100dvh`, the window. Set it when the frame is inside a box of its own.
27
+ */
28
+ export declare class CmAppFrame extends LitElement {
29
+ #private;
30
+ static properties: {
31
+ menuOpen: {
32
+ type: BooleanConstructor;
33
+ attribute: string;
34
+ reflect: boolean;
35
+ };
36
+ menuItem: {
37
+ attribute: string;
38
+ };
39
+ _wide: {
40
+ state: boolean;
41
+ };
42
+ };
43
+ /** Shows the mobile menu (on narrow frames). Set it yourself, or let `menu-item` do it. */
44
+ menuOpen: boolean;
45
+ /**
46
+ * Value of the bottom navigation item that opens and closes the mobile menu, e.g. `more` for
47
+ * `<cm-nav-item label="More" value="more">`. When set, choosing it toggles `menu-open`, Escape closes the
48
+ * menu, and choosing a row in the menu closes it. Empty: you wire the menu yourself.
49
+ */
50
+ menuItem: string;
51
+ _wide: boolean;
52
+ static styles: import("lit").CSSResult[];
53
+ connectedCallback(): void;
54
+ disconnectedCallback(): void;
55
+ /** True while the frame is 1024px wide or more and shows the side navigation instead of the bottom navigation. */
56
+ get wide(): boolean;
57
+ firstUpdated(): void;
58
+ updated(changed: Map<string, unknown>): void;
59
+ /** Opens the mobile menu and moves focus into it. */
60
+ openMenu(): void;
61
+ /** Closes the mobile menu and returns focus to where it was before the menu opened. */
62
+ closeMenu(): void;
63
+ /** Opens the mobile menu if it is closed, closes it if it is open. */
64
+ toggleMenu(): void;
65
+ render(): import("lit-html").TemplateResult<1>;
66
+ }
67
+ declare global {
68
+ interface HTMLElementTagNameMap {
69
+ 'cm-app-frame': CmAppFrame;
70
+ }
71
+ }
@@ -0,0 +1,202 @@
1
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
+ };
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _CmAppFrame_instances, _CmAppFrame_ro, _CmAppFrame_returnFocus, _CmAppFrame_heights, _CmAppFrame_set, _CmAppFrame_slotOf, _CmAppFrame_onNavigate, _CmAppFrame_onKey;
13
+ import { LitElement, html, css } from 'lit';
14
+ import { base, define } from '../base.js';
15
+ /** Frames at least this wide show the side navigation; narrower ones show the bottom navigation. */
16
+ const WIDE = 1024;
17
+ /** The element that has focus, looking inside shadow roots. */
18
+ const deepActive = () => {
19
+ let a = document.activeElement;
20
+ while (a?.shadowRoot?.activeElement)
21
+ a = a.shadowRoot.activeElement;
22
+ return a;
23
+ };
24
+ /**
25
+ * The admin's frame: puts the Header bar, the Side navigation, the page, the Bottom navigation and the Mobile
26
+ * menu in their places, so every Commmerce product gets the same grid. For Commmerce products only, not for apps:
27
+ * apps run inside the admin, which already has this frame.
28
+ *
29
+ * When the frame is 1024px wide or more, the side navigation sits on the start side under the header; narrower,
30
+ * it is hidden and the bottom navigation is fixed to the bottom of the window. The frame follows its own width
31
+ * (normally the window's). The header sticks to the top (below the device's notch or status bar), the side
32
+ * navigation sticks under it and scrolls on its own when it is taller than the window, and the page scrolls with
33
+ * the window. The page gets bottom padding as tall as the bottom navigation, so nothing hides behind it.
34
+ *
35
+ * The mobile menu is a full-screen layer above the page and below the bottom navigation, shown while
36
+ * `menu-open` is set. Set `menu-item` to the value of the bottom navigation item that opens it (usually `more`)
37
+ * and the frame opens and closes it for you; otherwise listen for `cm-navigate` and set `menu-open` yourself.
38
+ *
39
+ * The frame adds one landmark, `main`, around the page. The header bar (`banner`) and the navigations (`nav`)
40
+ * bring their own, so don't wrap them, and don't put another `main` in the page.
41
+ * @element cm-app-frame
42
+ * @slot header - A `cm-header-bar`.
43
+ * @slot navigation - A `cm-side-navigation`. Shown when the frame is 1024px wide or more. Collapse it with its `collapsed` attribute; the frame follows its width.
44
+ * @slot - The page, usually a `cm-page`. Wrapped in the frame's `main` landmark.
45
+ * @slot bottom-navigation - A `cm-bottom-navigation`. Shown, fixed to the bottom of the window, when the frame is narrower than 1024px. No need to set its `fixed` attribute.
46
+ * @slot mobile-menu - A `cm-mobile-menu`. Shown over the page while `menu-open` is set and the frame is narrower than 1024px.
47
+ * @fires cm-menu-toggle - When the frame opens or closes the mobile menu itself: from the `menu-item` in the bottom navigation, Escape, or a choice in the menu. `detail: { open }`. Not fired when you set `menu-open` yourself.
48
+ * @cssprop --cm-app-frame-height - Height the side navigation fits in (it scrolls when taller). Default `100dvh`, the window. Set it when the frame is inside a box of its own.
49
+ */
50
+ export class CmAppFrame extends LitElement {
51
+ constructor() {
52
+ super(...arguments);
53
+ _CmAppFrame_instances.add(this);
54
+ /** Shows the mobile menu (on narrow frames). Set it yourself, or let `menu-item` do it. */
55
+ this.menuOpen = false;
56
+ /**
57
+ * Value of the bottom navigation item that opens and closes the mobile menu, e.g. `more` for
58
+ * `<cm-nav-item label="More" value="more">`. When set, choosing it toggles `menu-open`, Escape closes the
59
+ * menu, and choosing a row in the menu closes it. Empty: you wire the menu yourself.
60
+ */
61
+ this.menuItem = '';
62
+ this._wide = false;
63
+ _CmAppFrame_ro.set(this, void 0);
64
+ _CmAppFrame_returnFocus.set(this, null);
65
+ _CmAppFrame_onNavigate.set(this, (e) => {
66
+ if (!this.menuItem)
67
+ return;
68
+ const slot = __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_slotOf).call(this, e);
69
+ const value = e.detail?.value;
70
+ if (slot === 'bottom-navigation' && value === this.menuItem)
71
+ __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, !this.menuOpen);
72
+ else if (slot === 'mobile-menu' || slot === 'bottom-navigation')
73
+ __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, false, false);
74
+ });
75
+ _CmAppFrame_onKey.set(this, (e) => {
76
+ // The mobile menu uses Escape itself to leave a sub-screen.
77
+ if (e.key !== 'Escape' || e.defaultPrevented || !this.menuItem || !this.menuOpen || this._wide)
78
+ return;
79
+ e.preventDefault();
80
+ __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, false);
81
+ });
82
+ }
83
+ connectedCallback() {
84
+ super.connectedCallback();
85
+ this.addEventListener('cm-navigate', __classPrivateFieldGet(this, _CmAppFrame_onNavigate, "f"));
86
+ this.addEventListener('keydown', __classPrivateFieldGet(this, _CmAppFrame_onKey, "f"));
87
+ // Pick the layout before the first render, so a wide frame never flashes the narrow one.
88
+ const w = this.getBoundingClientRect().width;
89
+ if (w > 0)
90
+ this._wide = w >= WIDE;
91
+ }
92
+ disconnectedCallback() {
93
+ super.disconnectedCallback();
94
+ this.removeEventListener('cm-navigate', __classPrivateFieldGet(this, _CmAppFrame_onNavigate, "f"));
95
+ this.removeEventListener('keydown', __classPrivateFieldGet(this, _CmAppFrame_onKey, "f"));
96
+ __classPrivateFieldGet(this, _CmAppFrame_ro, "f")?.disconnect();
97
+ __classPrivateFieldSet(this, _CmAppFrame_ro, undefined, "f");
98
+ }
99
+ /** True while the frame is 1024px wide or more and shows the side navigation instead of the bottom navigation. */
100
+ get wide() { return this._wide; }
101
+ firstUpdated() {
102
+ // Watches the frame's width (through a zero-height probe, so the frame's own height changes don't count) and the
103
+ // heights of the header and the bottom bar. Heights are applied at once, before paint: they only change padding
104
+ // and offsets, nothing watched. Crossing 1024px shows and hides watched parts, so that is applied in the next
105
+ // frame instead (inside the callback it would be a resize loop error).
106
+ let raf = 0;
107
+ __classPrivateFieldSet(this, _CmAppFrame_ro, new ResizeObserver(() => {
108
+ __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_heights).call(this);
109
+ const w = this.renderRoot.querySelector('.probe').getBoundingClientRect().width;
110
+ if (w > 0 && w >= WIDE !== this._wide) {
111
+ cancelAnimationFrame(raf);
112
+ raf = requestAnimationFrame(() => { this._wide = w >= WIDE; });
113
+ }
114
+ }), "f");
115
+ this.renderRoot.querySelectorAll('.probe, .header, .bottom').forEach((el) => __classPrivateFieldGet(this, _CmAppFrame_ro, "f").observe(el));
116
+ }
117
+ updated(changed) {
118
+ if (changed.has('_wide'))
119
+ __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_heights).call(this);
120
+ }
121
+ /** Opens the mobile menu and moves focus into it. */
122
+ openMenu() { __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, true); }
123
+ /** Closes the mobile menu and returns focus to where it was before the menu opened. */
124
+ closeMenu() { __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, false); }
125
+ /** Opens the mobile menu if it is closed, closes it if it is open. */
126
+ toggleMenu() { __classPrivateFieldGet(this, _CmAppFrame_instances, "m", _CmAppFrame_set).call(this, !this.menuOpen); }
127
+ render() {
128
+ const wide = this._wide;
129
+ // While the menu covers the page, the page and header can't be reached behind it; the bottom bar can.
130
+ const covered = this.menuOpen && !wide && !!this.querySelector(':scope > [slot="mobile-menu"]');
131
+ return html `<div class="frame ${wide ? 'wide' : ''}" part="frame">
132
+ <div class="probe"></div>
133
+ <div class="header" part="header" ?inert=${covered}><slot name="header"></slot></div>
134
+ <div class="body">
135
+ <div class="nav" part="navigation" ?hidden=${!wide}><slot name="navigation"></slot></div>
136
+ <main part="main" ?inert=${covered}><slot></slot></main>
137
+ </div>
138
+ <div class="menu" part="mobile-menu" ?hidden=${!covered}><slot name="mobile-menu" @slotchange=${() => this.requestUpdate()}></slot></div>
139
+ <div class="bottom" part="bottom-navigation" ?hidden=${wide}><slot name="bottom-navigation"></slot></div>
140
+ </div>`;
141
+ }
142
+ }
143
+ _CmAppFrame_ro = new WeakMap(), _CmAppFrame_returnFocus = new WeakMap(), _CmAppFrame_onNavigate = new WeakMap(), _CmAppFrame_onKey = new WeakMap(), _CmAppFrame_instances = new WeakSet(), _CmAppFrame_heights = function _CmAppFrame_heights() {
144
+ const frame = this.renderRoot.querySelector('.frame');
145
+ if (!frame)
146
+ return;
147
+ const h = (sel) => Math.round(this.renderRoot.querySelector(sel)?.getBoundingClientRect().height ?? 0);
148
+ frame.style.setProperty('--_header', `${h('.header')}px`);
149
+ frame.style.setProperty('--_bottom', `${this._wide ? 0 : h('.bottom')}px`);
150
+ }, _CmAppFrame_set = function _CmAppFrame_set(open, restore = true) {
151
+ if (open === this.menuOpen)
152
+ return;
153
+ if (open)
154
+ __classPrivateFieldSet(this, _CmAppFrame_returnFocus, deepActive(), "f");
155
+ this.menuOpen = open;
156
+ this.dispatchEvent(new CustomEvent('cm-menu-toggle', { bubbles: true, composed: true, detail: { open } }));
157
+ this.updateComplete.then(() => {
158
+ if (open) {
159
+ const menu = this.querySelector(':scope > [slot="mobile-menu"]');
160
+ const first = (menu?.shadowRoot ?? menu)?.querySelector('a[href], button');
161
+ first?.focus();
162
+ }
163
+ else if (restore) {
164
+ __classPrivateFieldGet(this, _CmAppFrame_returnFocus, "f")?.focus();
165
+ __classPrivateFieldSet(this, _CmAppFrame_returnFocus, null, "f");
166
+ }
167
+ });
168
+ }, _CmAppFrame_slotOf = function _CmAppFrame_slotOf(e) {
169
+ const own = e.composedPath().find((n) => n.parentElement === this);
170
+ return own?.getAttribute('slot') ?? '';
171
+ };
172
+ CmAppFrame.properties = {
173
+ menuOpen: { type: Boolean, attribute: 'menu-open', reflect: true },
174
+ menuItem: { attribute: 'menu-item' },
175
+ _wide: { state: true },
176
+ };
177
+ CmAppFrame.styles = [
178
+ base,
179
+ css `
180
+ :host { display: block; min-block-size: var(--cm-app-frame-height, 100dvh); background: var(--cm-bg-page); --_header: 0px; --_bottom: 0px; }
181
+ .header { position: sticky; inset-block-start: 0; z-index: 30; padding-block-start: env(safe-area-inset-top, 0px); background: var(--cm-bg-surface); }
182
+ .body { display: grid; grid-template-columns: minmax(0, 1fr); align-items: start; }
183
+ .wide .body { grid-template-columns: auto minmax(0, 1fr); }
184
+ .nav {
185
+ position: sticky; inset-block-start: var(--_header); box-sizing: border-box;
186
+ max-block-size: calc(var(--cm-app-frame-height, 100dvh) - var(--_header));
187
+ overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin;
188
+ padding: var(--cm-space-20); padding-inline-end: 0;
189
+ }
190
+ main { display: block; min-inline-size: 0; padding-block-end: var(--_bottom); }
191
+ main:focus { outline: none; }
192
+ .bottom { position: fixed; inset-inline: 0; inset-block-end: 0; z-index: 40; }
193
+ ::slotted([slot='bottom-navigation']) { position: static; }
194
+ .menu {
195
+ position: fixed; inset: 0; z-index: 35; overflow-y: auto; overscroll-behavior: contain;
196
+ padding-block: env(safe-area-inset-top, 0px) var(--_bottom); background: var(--cm-bg-page);
197
+ }
198
+ .probe { block-size: 0; }
199
+ [hidden] { display: none !important; }
200
+ `,
201
+ ];
202
+ define('cm-app-frame', CmAppFrame);
@@ -1,12 +1,29 @@
1
- import { LitElement } from 'lit';
1
+ import { CmLocalizedElement } from '../localize.js';
2
+ import './icon.js';
3
+ declare const strings: {
4
+ /** Accessible name of an icon-only button with a `count`: `{label}` is the button's `label`. */
5
+ labelWithCount: string;
6
+ /** Read after the visible text of a button with a `count` that is not icon-only. */
7
+ count: string;
8
+ /** Shown instead of the number when `count` is over 99. */
9
+ countOverflow: string;
10
+ };
11
+ declare module '../localize.js' {
12
+ interface CmStrings {
13
+ button: typeof strings;
14
+ }
15
+ }
2
16
  /**
3
- * Black primary or outlined secondary action.
17
+ * Black primary or outlined secondary action. With `icon-only` it is a square button that shows just an icon
18
+ * (bell, refresh, search, row actions) and is named by `label`. `count` adds a small counter ("3", "99+").
4
19
  * @element cm-button
5
- * @slot - Label.
6
- * @slot icon - Optional leading icon.
20
+ * @slot - Label. Visually hidden when `icon-only` (it still names the button if `label` is empty).
21
+ * @slot icon - Optional leading icon. Overrides the `icon` attribute.
22
+ * @csspart control - The native `<button>` or `<a>`.
23
+ * @csspart count - The counter shown when `count` is above 0.
7
24
  * @fires click - Native click; suppressed while disabled or loading.
8
25
  */
9
- export declare class CmButton extends LitElement {
26
+ export declare class CmButton extends CmLocalizedElement {
10
27
  #private;
11
28
  static formAssociated: boolean;
12
29
  static properties: {
@@ -27,6 +44,16 @@ export declare class CmButton extends LitElement {
27
44
  };
28
45
  href: {};
29
46
  target: {};
47
+ iconOnly: {
48
+ type: BooleanConstructor;
49
+ reflect: boolean;
50
+ attribute: string;
51
+ };
52
+ icon: {};
53
+ label: {};
54
+ count: {
55
+ type: NumberConstructor;
56
+ };
30
57
  };
31
58
  /** `primary` = black fill (one per group); `secondary` = light fill, black outline. */
32
59
  variant: 'primary' | 'secondary';
@@ -42,8 +69,17 @@ export declare class CmButton extends LitElement {
42
69
  href?: string;
43
70
  /** Link target when `href` is set. `_blank` adds `rel="noopener noreferrer"`. */
44
71
  target?: string;
72
+ /** Square button that shows only its icon (from `icon` or the `icon` slot). Give it a `label`. */
73
+ iconOnly: boolean;
74
+ /** Name of a @commmerce/icons icon shown before the label (16px; 20px when `size="large"`). */
75
+ icon: string;
76
+ /** Accessible name of an `icon-only` button ("Notifications", "Refresh"). Required with `icon-only`; ignored otherwise, so the visible text stays the name. */
77
+ label: string;
78
+ /** Small counter on the button, e.g. unread notifications. Hidden at 0; over 99 shows "99+". Screen readers hear the exact number ("Notifications, 3 unread"). */
79
+ count: number;
45
80
  static styles: import("lit").CSSResult[];
46
81
  constructor();
82
+ updated(): void;
47
83
  render(): import("lit-html").TemplateResult<1>;
48
84
  }
49
85
  declare global {
@@ -51,3 +87,4 @@ declare global {
51
87
  'cm-button': CmButton;
52
88
  }
53
89
  }
90
+ export {};
@@ -3,17 +3,38 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
3
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
- var _CmButton_internals, _CmButton_onClick;
7
- import { LitElement, html, css, nothing } from 'lit';
6
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
+ if (kind === "m") throw new TypeError("Private method is not writable");
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
+ };
12
+ var _CmButton_internals, _CmButton_onClick, _CmButton_warned;
13
+ import { html, css, nothing } from 'lit';
8
14
  import { base, define } from '../base.js';
15
+ import { CmLocalizedElement, defineStrings } from '../localize.js';
16
+ import './icon.js';
17
+ /** Highest count shown in full; above it the button shows "99+". */
18
+ const COUNT_MAX = 99;
19
+ const strings = defineStrings('button', {
20
+ /** Accessible name of an icon-only button with a `count`: `{label}` is the button's `label`. */
21
+ labelWithCount: '{label}, {count} unread',
22
+ /** Read after the visible text of a button with a `count` that is not icon-only. */
23
+ count: '{count} unread',
24
+ /** Shown instead of the number when `count` is over 99. */
25
+ countOverflow: '{max}+',
26
+ });
9
27
  /**
10
- * Black primary or outlined secondary action.
28
+ * Black primary or outlined secondary action. With `icon-only` it is a square button that shows just an icon
29
+ * (bell, refresh, search, row actions) and is named by `label`. `count` adds a small counter ("3", "99+").
11
30
  * @element cm-button
12
- * @slot - Label.
13
- * @slot icon - Optional leading icon.
31
+ * @slot - Label. Visually hidden when `icon-only` (it still names the button if `label` is empty).
32
+ * @slot icon - Optional leading icon. Overrides the `icon` attribute.
33
+ * @csspart control - The native `<button>` or `<a>`.
34
+ * @csspart count - The counter shown when `count` is above 0.
14
35
  * @fires click - Native click; suppressed while disabled or loading.
15
36
  */
16
- export class CmButton extends LitElement {
37
+ export class CmButton extends CmLocalizedElement {
17
38
  constructor() {
18
39
  super();
19
40
  /** `primary` = black fill (one per group); `secondary` = light fill, black outline. */
@@ -26,6 +47,14 @@ export class CmButton extends LitElement {
26
47
  this.disabled = false;
27
48
  /** Shows a spinner and blocks clicks while an action runs. */
28
49
  this.loading = false;
50
+ /** Square button that shows only its icon (from `icon` or the `icon` slot). Give it a `label`. */
51
+ this.iconOnly = false;
52
+ /** Name of a @commmerce/icons icon shown before the label (16px; 20px when `size="large"`). */
53
+ this.icon = '';
54
+ /** Accessible name of an `icon-only` button ("Notifications", "Refresh"). Required with `icon-only`; ignored otherwise, so the visible text stays the name. */
55
+ this.label = '';
56
+ /** Small counter on the button, e.g. unread notifications. Hidden at 0; over 99 shows "99+". Screen readers hear the exact number ("Notifications, 3 unread"). */
57
+ this.count = 0;
29
58
  _CmButton_internals.set(this, this.attachInternals());
30
59
  _CmButton_onClick.set(this, (e) => {
31
60
  if (this.disabled || this.loading) {
@@ -41,20 +70,38 @@ export class CmButton extends LitElement {
41
70
  else if (this.type === 'reset')
42
71
  form.reset();
43
72
  });
73
+ _CmButton_warned.set(this, false);
44
74
  this.addEventListener('click', __classPrivateFieldGet(this, _CmButton_onClick, "f"), { capture: true });
45
75
  }
76
+ updated() {
77
+ if (this.iconOnly && !this.label && !this.textContent?.trim() && !this.querySelector('[label]') && !__classPrivateFieldGet(this, _CmButton_warned, "f")) {
78
+ __classPrivateFieldSet(this, _CmButton_warned, true, "f");
79
+ console.warn('cm-button: an icon-only button needs a `label` (its accessible name).', this);
80
+ }
81
+ }
46
82
  render() {
47
- const inner = html `${this.loading ? html `<span class="spinner" aria-hidden="true"></span>` : html `<slot name="icon"></slot>`}<slot></slot>`;
83
+ const count = Number.isFinite(this.count) && this.count > 0 ? Math.floor(this.count) : 0;
84
+ const iconSize = this.size === 'large' ? 20 : 16;
85
+ const icon = this.loading
86
+ ? html `<span class="spinner" aria-hidden="true"></span>`
87
+ : html `<slot name="icon">${this.icon ? html `<cm-icon name=${this.icon} size=${iconSize}></cm-icon>` : nothing}</slot>`;
88
+ const text = this.iconOnly ? html `<span class="vh"><slot></slot></span>` : html `<slot></slot>`;
89
+ const counter = count
90
+ ? html `<span class="count" part="count" aria-hidden="true">${count > COUNT_MAX ? this.t('countOverflow', { max: COUNT_MAX }) : this.formatNumber(count)}</span>${this.iconOnly ? nothing : html `<span class="vh"> ${this.t('count', { count })}</span>`}`
91
+ : nothing;
92
+ const name = this.iconOnly ? (this.label || this.textContent?.trim() || '') : '';
93
+ const ariaLabel = !this.iconOnly || !name ? nothing : count ? this.t('labelWithCount', { label: name, count }) : this.label || nothing;
94
+ const inner = html `${icon}${text}${counter}`;
48
95
  if (this.href && !this.disabled) {
49
96
  return html `<a class="btn" part="control" href=${this.href} target=${this.target ?? nothing}
50
- rel=${this.target === '_blank' ? 'noopener noreferrer' : nothing}>${inner}</a>`;
97
+ rel=${this.target === '_blank' ? 'noopener noreferrer' : nothing} aria-label=${ariaLabel}>${inner}</a>`;
51
98
  }
52
99
  return html `<button class="btn" part="control" type="button"
53
100
  ?disabled=${this.disabled} aria-disabled=${this.loading ? 'true' : nothing}
54
- aria-busy=${this.loading ? 'true' : nothing}>${inner}</button>`;
101
+ aria-busy=${this.loading ? 'true' : nothing} aria-label=${ariaLabel}>${inner}</button>`;
55
102
  }
56
103
  }
57
- _CmButton_internals = new WeakMap(), _CmButton_onClick = new WeakMap();
104
+ _CmButton_internals = new WeakMap(), _CmButton_onClick = new WeakMap(), _CmButton_warned = new WeakMap();
58
105
  CmButton.formAssociated = true;
59
106
  CmButton.properties = {
60
107
  variant: { reflect: true },
@@ -64,6 +111,10 @@ CmButton.properties = {
64
111
  loading: { type: Boolean, reflect: true },
65
112
  href: {},
66
113
  target: {},
114
+ iconOnly: { type: Boolean, reflect: true, attribute: 'icon-only' },
115
+ icon: {},
116
+ label: {},
117
+ count: { type: Number },
67
118
  };
68
119
  CmButton.styles = [
69
120
  base,
@@ -96,6 +147,25 @@ CmButton.styles = [
96
147
  @keyframes spin { to { transform: rotate(360deg); } }
97
148
  @media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } }
98
149
  ::slotted([slot='icon']) { display: inline-flex; }
150
+ /* Icon-only: a square of the size's height; at least 44 x 44 on touch screens. */
151
+ :host([icon-only]) .btn { position: relative; padding: 0; }
152
+ :host([icon-only][size='large']) .btn { width: 46px; }
153
+ :host([icon-only]) .btn, :host([icon-only][size='medium']) .btn { width: 32px; }
154
+ :host([icon-only][size='slim']) .btn { width: 24px; }
155
+ @media (pointer: coarse) {
156
+ :host([icon-only]) .btn { min-width: 44px; min-height: 44px; }
157
+ }
158
+ .vh { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
159
+ .count {
160
+ display: inline-flex; align-items: center; justify-content: center; box-sizing: border-box;
161
+ min-width: 18px; height: 18px; padding: 0 5px; border-radius: var(--cm-radius-full);
162
+ font-size: 11px; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums;
163
+ color: var(--cm-text-on-dark); background: var(--cm-nav-badge);
164
+ }
165
+ :host([icon-only]) .count {
166
+ position: absolute; inset-block-start: -7px; inset-inline-end: -7px;
167
+ box-shadow: 0 0 0 2px var(--cm-bg-surface); pointer-events: none;
168
+ }
99
169
  `,
100
170
  ];
101
171
  define('cm-button', CmButton);
@@ -9,11 +9,12 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (
9
9
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
11
  };
12
- var _CmCombobox_instances, _CmCombobox_internals, _CmCombobox_id, _CmCombobox_default, _CmCombobox_labels, _CmCombobox_observer, _CmCombobox_readChildren, _CmCombobox_labelOf, _CmCombobox_createText_get, _CmCombobox_rows, _CmCombobox_emit, _CmCombobox_show, _CmCombobox_hide, _CmCombobox_enabled, _CmCombobox_move, _CmCombobox_choose, _CmCombobox_create, _CmCombobox_focusInput, _CmCombobox_onInput, _CmCombobox_onKeyDown, _CmCombobox_onBlur, _CmCombobox_onOutside, _CmCombobox_highlight;
12
+ var _CmCombobox_instances, _CmCombobox_internals, _CmCombobox_id, _CmCombobox_default, _CmCombobox_labels, _CmCombobox_observer, _CmCombobox_stopOutside, _CmCombobox_readChildren, _CmCombobox_labelOf, _CmCombobox_createText_get, _CmCombobox_rows, _CmCombobox_emit, _CmCombobox_show, _CmCombobox_hide, _CmCombobox_enabled, _CmCombobox_move, _CmCombobox_choose, _CmCombobox_create, _CmCombobox_focusInput, _CmCombobox_onInput, _CmCombobox_onKeyDown, _CmCombobox_onBlur, _CmCombobox_highlight;
13
13
  import { LitElement, html, css, nothing } from 'lit';
14
14
  import { live } from 'lit/directives/live.js';
15
15
  import { base, define, nextId } from '../base.js';
16
16
  import { CmLocalizedElement, defineStrings } from '../localize.js';
17
+ import { popoverStyles, closeOnOutsidePointer } from '../popover.js';
17
18
  import './icon.js';
18
19
  import './tag.js';
19
20
  import './spinner.js';
@@ -93,6 +94,7 @@ export class CmCombobox extends CmLocalizedElement {
93
94
  /** Labels for chosen values, so tags keep their text when async options change. */
94
95
  _CmCombobox_labels.set(this, new Map());
95
96
  _CmCombobox_observer.set(this, new MutationObserver(() => __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_readChildren).call(this)));
97
+ _CmCombobox_stopOutside.set(this, void 0);
96
98
  _CmCombobox_onInput.set(this, (e) => {
97
99
  e.stopPropagation(); // the native per-keystroke input event; `input` is fired when the value changes
98
100
  this._query = e.target.value;
@@ -175,21 +177,17 @@ export class CmCombobox extends CmLocalizedElement {
175
177
  this._query = this.multiple ? '' : this.value ? __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_labelOf).call(this, this.value) : '';
176
178
  });
177
179
  });
178
- _CmCombobox_onOutside.set(this, (e) => {
179
- if (this.open && !e.composedPath().includes(this))
180
- __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_hide).call(this);
181
- });
182
180
  }
183
181
  connectedCallback() {
184
182
  super.connectedCallback();
185
183
  __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_readChildren).call(this);
186
184
  __classPrivateFieldGet(this, _CmCombobox_observer, "f").observe(this, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['value', 'label', 'disabled', 'selected'] });
187
- document.addEventListener('pointerdown', __classPrivateFieldGet(this, _CmCombobox_onOutside, "f"), true);
185
+ __classPrivateFieldSet(this, _CmCombobox_stopOutside, closeOnOutsidePointer(this, () => this.open, () => __classPrivateFieldGet(this, _CmCombobox_instances, "m", _CmCombobox_hide).call(this)), "f");
188
186
  }
189
187
  disconnectedCallback() {
190
188
  super.disconnectedCallback();
191
189
  __classPrivateFieldGet(this, _CmCombobox_observer, "f").disconnect();
192
- document.removeEventListener('pointerdown', __classPrivateFieldGet(this, _CmCombobox_onOutside, "f"), true);
190
+ __classPrivateFieldGet(this, _CmCombobox_stopOutside, "f")?.call(this);
193
191
  }
194
192
  get form() { return __classPrivateFieldGet(this, _CmCombobox_internals, "f").form; }
195
193
  get validity() { return __classPrivateFieldGet(this, _CmCombobox_internals, "f").validity; }
@@ -335,7 +333,7 @@ export class CmCombobox extends CmLocalizedElement {
335
333
  </div>`;
336
334
  }
337
335
  }
338
- _CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombobox_default = new WeakMap(), _CmCombobox_labels = new WeakMap(), _CmCombobox_observer = new WeakMap(), _CmCombobox_onInput = new WeakMap(), _CmCombobox_onKeyDown = new WeakMap(), _CmCombobox_onBlur = new WeakMap(), _CmCombobox_onOutside = new WeakMap(), _CmCombobox_instances = new WeakSet(), _CmCombobox_readChildren = function _CmCombobox_readChildren() {
336
+ _CmCombobox_internals = new WeakMap(), _CmCombobox_id = new WeakMap(), _CmCombobox_default = new WeakMap(), _CmCombobox_labels = new WeakMap(), _CmCombobox_observer = new WeakMap(), _CmCombobox_stopOutside = new WeakMap(), _CmCombobox_onInput = new WeakMap(), _CmCombobox_onKeyDown = new WeakMap(), _CmCombobox_onBlur = new WeakMap(), _CmCombobox_instances = new WeakSet(), _CmCombobox_readChildren = function _CmCombobox_readChildren() {
339
337
  const opts = [...this.querySelectorAll('option')];
340
338
  this._slotted = opts.map((o) => ({ value: o.value, label: o.label || o.textContent.trim(), disabled: o.disabled }));
341
339
  if (!this.hasUpdated) {
@@ -463,6 +461,7 @@ CmCombobox.properties = {
463
461
  };
464
462
  CmCombobox.styles = [
465
463
  base,
464
+ popoverStyles,
466
465
  css `
467
466
  :host { display: block; position: relative; }
468
467
  .field { display: flex; flex-direction: column; gap: var(--cm-space-10); }
@@ -485,12 +484,6 @@ CmCombobox.styles = [
485
484
  :host([error]:not([error=''])) .box { border-color: var(--cm-border-critical); }
486
485
  :host([disabled]) .box { background: var(--cm-bg-input-filled); border-color: var(--cm-border-default); cursor: not-allowed; }
487
486
  :host([disabled]) input { color: var(--cm-text-secondary); cursor: not-allowed; }
488
- .pop {
489
- position: absolute; z-index: 20; inset-block-start: calc(100% + var(--cm-space-10)); inset-inline: 0;
490
- background: var(--cm-bg-surface); border: var(--cm-border-1) solid var(--cm-border-default);
491
- border-radius: var(--cm-radius-md); box-shadow: var(--cm-shadow-100); overflow: hidden;
492
- }
493
- .pop[hidden] { display: none; }
494
487
  [role='listbox'] { max-height: 242px; overflow-y: auto; margin: 0; padding: var(--cm-space-10) 0; list-style: none; }
495
488
  [role='listbox'][hidden] { display: none; }
496
489
  [role='option'] {
@@ -506,11 +499,6 @@ CmCombobox.styles = [
506
499
  [role='option'][aria-disabled='true'] { color: var(--cm-text-secondary); cursor: not-allowed; }
507
500
  [role='option'] mark { background: none; color: inherit; font-weight: 600; }
508
501
  .create { font-style: normal; }
509
- .message { display: flex; align-items: center; gap: var(--cm-space-10); margin: 0; padding: var(--cm-space-15) var(--cm-space-20); font-size: 14px; color: var(--cm-text-tertiary); }
510
- .vh {
511
- position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden;
512
- clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
513
- }
514
502
  .help, .error { margin: 0; font-size: 12px; color: var(--cm-text-tertiary); }
515
503
  .error { color: var(--cm-text-critical); }
516
504
  slot { display: none; }