@commmerce/ui 1.0.0-rc.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,15 +9,20 @@ 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 _CmSideNavigation_instances, _CmSideNavigation_seen, _CmSideNavigation_observer, _CmSideNavigation_id, _CmSideNavigation_read, _CmSideNavigation_key, _CmSideNavigation_choose, _CmSideNavigation_focusables, _CmSideNavigation_onKeyDown, _CmSideNavigation_flatten, _CmSideNavigation_link;
12
+ var _CmSideNavigation_instances, _CmSideNavigation_seen, _CmSideNavigation_observer, _CmSideNavigation_id, _CmSideNavigation_read, _CmSideNavigation_key, _CmSideNavigation_choose, _CmSideNavigation_focusables, _CmSideNavigation_onKeyDown, _CmSideNavigation_flatten, _CmSideNavigation_setCollapsed, _CmSideNavigation_openFromRail, _CmSideNavigation_icon, _CmSideNavigation_text, _CmSideNavigation_tip, _CmSideNavigation_link, _CmSideNavigation_toggleButton;
13
13
  import { html, css, nothing } from 'lit';
14
14
  import { base, define, nextId, logicalKey } from '../base.js';
15
15
  import { CmLocalizedElement, defineStrings } from '../localize.js';
16
16
  import { readNavItems, watchNavItems, hasCurrent, fireNavigate, navValue } from './nav-item.js';
17
17
  import './icon.js';
18
+ import './tooltip.js';
18
19
  const strings = defineStrings('sideNavigation', {
19
20
  /** Accessible name of the navigation landmark. */
20
21
  label: 'Main',
22
+ /** The built-in collapse button, while the full menu is shown. */
23
+ collapse: 'Collapse menu',
24
+ /** The built-in collapse button, while only icons are shown. */
25
+ expand: 'Expand menu',
21
26
  });
22
27
  /**
23
28
  * The admin's main menu: a black rounded panel down the left of the page, with an icon and a label per
@@ -25,12 +30,20 @@ const strings = defineStrings('sideNavigation', {
25
30
  * apps are shown inside the admin, which already has this menu.
26
31
  *
27
32
  * Items come from `cm-nav-item` children or the `items` property, so every Commmerce product can use it.
33
+ *
34
+ * `collapsed` turns it into a narrow rail of icons, to give wide pages (tables, reports) more room. Each item's
35
+ * label is still its accessible name and shows as a tooltip on hover and focus; badges become a dot (or a small
36
+ * count when the badge is a number). Add `collapsible` for a built-in button that switches between the two: it
37
+ * fires `cm-collapse` so the app can remember the choice.
28
38
  * @element cm-side-navigation
29
39
  * @slot - `cm-nav-item` elements (nest them for sub-items).
30
40
  * @slot header - Optional content above the items, such as a store switcher on narrow layouts.
31
41
  * @slot footer - Optional content below the items.
32
42
  * @fires cm-navigate - When an item is chosen. `detail: { item, value, href }`. Cancelable: call `preventDefault()` to route in your app instead of following the link.
33
43
  * @fires cm-expand - When a section opens or closes. `detail: { item, expanded }`.
44
+ * @fires cm-collapse - When the merchant collapses the menu to icons or expands it again: from the built-in button (`collapsible`), or by choosing a section while collapsed (that expands the menu and opens the section). `detail: { collapsed }`. Cancelable: call `preventDefault()` to keep the current state. Not fired when you set `collapsed` yourself.
45
+ * @cssprop --cm-side-navigation-width - Width of the full menu. Default `250px`.
46
+ * @cssprop --cm-side-navigation-collapsed-width - Width of the icon rail (`collapsed`). Default `72px`.
34
47
  */
35
48
  export class CmSideNavigation extends CmLocalizedElement {
36
49
  constructor() {
@@ -38,6 +51,10 @@ export class CmSideNavigation extends CmLocalizedElement {
38
51
  _CmSideNavigation_instances.add(this);
39
52
  /** Items as data. Used instead of `cm-nav-item` children when set. */
40
53
  this.items = [];
54
+ /** Shows only the icons, in a narrow rail. Labels become tooltips and stay the items' accessible names; sub-items are hidden. */
55
+ this.collapsed = false;
56
+ /** Adds a button at the bottom that collapses the menu to icons and expands it again, firing `cm-collapse`. */
57
+ this.collapsible = false;
41
58
  this._slotted = [];
42
59
  this._open = new Set();
43
60
  _CmSideNavigation_seen.set(this, new Set());
@@ -81,6 +98,7 @@ export class CmSideNavigation extends CmLocalizedElement {
81
98
  this.dispatchEvent(new CustomEvent('cm-expand', { bubbles: true, composed: true, detail: { item: it, expanded: next } }));
82
99
  }
83
100
  render() {
101
+ const rail = this.collapsed;
84
102
  return html `
85
103
  <nav part="nav" aria-label=${this.label || this.t('label')} @keydown=${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_onKeyDown)}>
86
104
  <slot name="header"></slot>
@@ -88,20 +106,21 @@ export class CmSideNavigation extends CmLocalizedElement {
88
106
  ${this.navItems.map((it, i) => {
89
107
  if (!it.items?.length)
90
108
  return html `<li>${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_link).call(this, it, '', { index: i })}</li>`;
91
- const open = this._open.has(__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_key).call(this, it, i));
109
+ const open = !rail && this._open.has(__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_key).call(this, it, i));
92
110
  const sub = `${__classPrivateFieldGet(this, _CmSideNavigation_id, "f")}-${i}`;
93
111
  return html `<li>
94
- <button type="button" class="item parent ${hasCurrent(it) ? 'has-current' : ''}" part="item" data-index=${i}
95
- aria-expanded=${open ? 'true' : 'false'} aria-controls=${sub} @click=${() => this.toggle(i)}>
96
- ${it.icon ? html `<cm-icon name=${it.icon} size="20"></cm-icon>` : nothing}<span class="label">${it.label}</span>${it.badge ? html `<span class="badge">${it.badge}</span>` : nothing}
97
- <cm-icon class="chev" name="chevron-down" size="18"></cm-icon>
98
- </button>
112
+ ${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_tip).call(this, it, html `<button type="button" class="item parent ${hasCurrent(it) ? 'has-current' : ''}" part="item" data-index=${i}
113
+ aria-expanded=${open ? 'true' : 'false'} aria-controls=${sub} @click=${() => (rail ? __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_openFromRail).call(this, i) : this.toggle(i))}>
114
+ ${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_icon).call(this, it)}${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_text).call(this, it)}
115
+ ${rail ? nothing : html `<cm-icon class="chev" name="chevron-down" size="18"></cm-icon>`}
116
+ </button>`)}
99
117
  <ul class="sub" id=${sub} ?hidden=${!open} aria-label=${it.label}>
100
118
  ${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_flatten).call(this, it.items).map(([s, d]) => html `<li>${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_link).call(this, { ...s, icon: undefined }, d ? 'deep' : '', { parent: i })}</li>`)}
101
119
  </ul>
102
120
  </li>`;
103
121
  })}
104
122
  </ul>
123
+ ${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_toggleButton).call(this)}
105
124
  <slot name="footer"></slot>
106
125
  <slot hidden></slot>
107
126
  </nav>
@@ -130,6 +149,8 @@ _CmSideNavigation_seen = new WeakMap(), _CmSideNavigation_observer = new WeakMap
130
149
  next = all[0];
131
150
  else if (e.key === 'End')
132
151
  next = all[all.length - 1];
152
+ else if (this.collapsed && (key === 'ArrowRight' || key === 'ArrowLeft'))
153
+ return;
133
154
  else if (key === 'ArrowRight' && t.classList.contains('parent')) {
134
155
  this.toggle(idx, true);
135
156
  e.preventDefault();
@@ -151,17 +172,51 @@ _CmSideNavigation_seen = new WeakMap(), _CmSideNavigation_observer = new WeakMap
151
172
  next?.focus();
152
173
  }, _CmSideNavigation_flatten = function _CmSideNavigation_flatten(items, depth = 0) {
153
174
  return items.flatMap((it) => [[it, depth], ...__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_flatten).call(this, it.items ?? [], depth + 1)]);
175
+ }, _CmSideNavigation_setCollapsed = function _CmSideNavigation_setCollapsed(next) {
176
+ if (next === this.collapsed)
177
+ return true;
178
+ const ok = this.dispatchEvent(new CustomEvent('cm-collapse', { bubbles: true, composed: true, cancelable: true, detail: { collapsed: next } }));
179
+ if (ok)
180
+ this.collapsed = next;
181
+ return ok;
182
+ }, _CmSideNavigation_openFromRail = function _CmSideNavigation_openFromRail(i) {
183
+ const it = this.navItems[i];
184
+ if (!__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_setCollapsed).call(this, false))
185
+ return;
186
+ this.toggle(i, true);
187
+ this.updateComplete.then(() => this.renderRoot.querySelector(`.parent[data-index="${i}"]`)?.focus());
188
+ }, _CmSideNavigation_icon = function _CmSideNavigation_icon(it) {
189
+ if (it.icon)
190
+ return html `<cm-icon name=${it.icon} size="20"></cm-icon>`;
191
+ return this.collapsed ? html `<span class="initial" aria-hidden="true">${[...it.label.trim()][0] ?? ''}</span>` : nothing;
192
+ }, _CmSideNavigation_text = function _CmSideNavigation_text(it) {
193
+ if (!this.collapsed)
194
+ return html `<span class="label">${it.label}</span>${it.badge ? html `<span class="badge">${it.badge}</span>` : nothing}`;
195
+ const count = it.badge && /^\d{1,2}\+?$/.test(it.badge.trim());
196
+ return html `<span class="sr">${it.label}${it.badge ? ` (${it.badge})` : ''}</span>${it.badge ? html `<span class="dot ${count ? 'count' : ''}" aria-hidden="true">${count ? it.badge : nothing}</span>` : nothing}`;
197
+ }, _CmSideNavigation_tip = function _CmSideNavigation_tip(it, inner) {
198
+ return this.collapsed ? html `<cm-tooltip content=${it.label} placement="end" visual-only>${inner}</cm-tooltip>` : inner;
154
199
  }, _CmSideNavigation_link = function _CmSideNavigation_link(it, cls, extra = {}) {
155
- const inner = html `${it.icon ? html `<cm-icon name=${it.icon} size="20"></cm-icon>` : nothing}<span class="label">${it.label}</span>${it.badge ? html `<span class="badge">${it.badge}</span>` : nothing}`;
156
- return it.href
200
+ const inner = html `${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_icon).call(this, it)}${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_text).call(this, it)}`;
201
+ return __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_tip).call(this, it, it.href
157
202
  ? html `<a class="item ${cls}" part="item" href=${it.href} aria-current=${it.current ? 'page' : nothing}
158
203
  data-index=${extra.index ?? nothing} data-parent=${extra.parent ?? nothing} @click=${(e) => __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_choose).call(this, e, it)}>${inner}</a>`
159
204
  : html `<button type="button" class="item ${cls}" part="item" aria-current=${it.current ? 'page' : nothing}
160
- data-index=${extra.index ?? nothing} data-parent=${extra.parent ?? nothing} @click=${(e) => __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_choose).call(this, e, it)}>${inner}</button>`;
205
+ data-index=${extra.index ?? nothing} data-parent=${extra.parent ?? nothing} @click=${(e) => __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_choose).call(this, e, it)}>${inner}</button>`);
206
+ }, _CmSideNavigation_toggleButton = function _CmSideNavigation_toggleButton() {
207
+ if (!this.collapsible)
208
+ return nothing;
209
+ const label = this.t(this.collapsed ? 'expand' : 'collapse');
210
+ const inner = html `<cm-icon name=${this.collapsed ? 'chevron-right' : 'chevron-left'} size="20"></cm-icon>${this.collapsed
211
+ ? html `<span class="sr">${label}</span>` : html `<span class="label">${label}</span>`}`;
212
+ return html `<div class="toggle-row">${__classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_tip).call(this, { label }, html `<button type="button" class="item toggle" part="collapse-button"
213
+ @click=${() => __classPrivateFieldGet(this, _CmSideNavigation_instances, "m", _CmSideNavigation_setCollapsed).call(this, !this.collapsed)}>${inner}</button>`)}</div>`;
161
214
  };
162
215
  CmSideNavigation.properties = {
163
216
  items: { type: Array },
164
217
  label: { string: 'label' },
218
+ collapsed: { type: Boolean, reflect: true },
219
+ collapsible: { type: Boolean, reflect: true },
165
220
  _slotted: { state: true },
166
221
  _open: { state: true },
167
222
  };
@@ -193,6 +248,20 @@ CmSideNavigation.styles = [
193
248
  .sub .item { min-height: 36px; padding-inline-start: 40px; font-size: 14px; }
194
249
  .sub .item.deep { padding-inline-start: 56px; }
195
250
  @media (prefers-reduced-motion: reduce) { .chev { transition: none; } }
251
+ .sr { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
252
+ .toggle { margin-block-start: 8px; color: var(--cm-text-on-dark-secondary); }
253
+ .toggle .label { font-size: 14px; }
254
+ /* Collapsed: an icon rail. */
255
+ :host([collapsed]) { width: var(--cm-side-navigation-collapsed-width, 72px); }
256
+ :host([collapsed]) cm-tooltip { display: block; color: inherit; }
257
+ :host([collapsed]) .item { position: relative; justify-content: center; min-height: 44px; padding: 0; }
258
+ :host([collapsed]) .item[aria-current='page'], :host([collapsed]) .parent.has-current { background: var(--cm-nav-active); }
259
+ .initial { display: grid; place-items: center; width: 20px; height: 20px; font-size: 14px; font-weight: 600; text-transform: uppercase; }
260
+ .dot {
261
+ position: absolute; inset-block-start: 6px; inset-inline-start: calc(50% + 4px); min-width: 8px; height: 8px; border-radius: var(--cm-radius-full);
262
+ background: var(--cm-nav-badge); box-shadow: 0 0 0 2px var(--cm-bg-dashboard);
263
+ }
264
+ .dot.count { inset-block-start: 3px; height: 16px; padding: 0 4px; font-size: 10px; font-weight: 600; line-height: 16px; text-align: center; color: var(--cm-text-on-dark); }
196
265
  `,
197
266
  ];
198
267
  define('cm-side-navigation', CmSideNavigation);
@@ -26,6 +26,10 @@ export declare class CmTooltip extends LitElement {
26
26
  type: BooleanConstructor;
27
27
  reflect: boolean;
28
28
  };
29
+ visualOnly: {
30
+ type: BooleanConstructor;
31
+ attribute: string;
32
+ };
29
33
  _side: {
30
34
  state: boolean;
31
35
  };
@@ -38,6 +42,12 @@ export declare class CmTooltip extends LitElement {
38
42
  delay: number;
39
43
  /** True while the tooltip is shown. Read it; use `show()` / `hide()` to change it. */
40
44
  open: boolean;
45
+ /**
46
+ * Shows the bubble without adding its text to the control's accessible description. Use it only when the text
47
+ * repeats the control's accessible name, such as the label of an icon-only navigation item, so screen readers
48
+ * don't hear it twice.
49
+ */
50
+ visualOnly: boolean;
41
51
  _side: 'top' | 'bottom' | 'start' | 'end';
42
52
  static styles: import("lit").CSSResult[];
43
53
  connectedCallback(): void;
@@ -38,6 +38,12 @@ export class CmTooltip extends LitElement {
38
38
  this.delay = 300;
39
39
  /** True while the tooltip is shown. Read it; use `show()` / `hide()` to change it. */
40
40
  this.open = false;
41
+ /**
42
+ * Shows the bubble without adding its text to the control's accessible description. Use it only when the text
43
+ * repeats the control's accessible name, such as the label of an icon-only navigation item, so screen readers
44
+ * don't hear it twice.
45
+ */
46
+ this.visualOnly = false;
41
47
  this._side = 'top';
42
48
  _CmTooltip_desc.set(this, void 0);
43
49
  _CmTooltip_target.set(this, void 0);
@@ -76,7 +82,7 @@ export class CmTooltip extends LitElement {
76
82
  const slot = this.renderRoot.querySelector('slot');
77
83
  const trigger = slot?.assignedElements({ flatten: true })[0];
78
84
  __classPrivateFieldGet(this, _CmTooltip_instances, "m", _CmTooltip_unlink).call(this);
79
- if (!trigger || !this.content)
85
+ if (!trigger || !this.content || this.visualOnly)
80
86
  return;
81
87
  // A component trigger renders its control later: link once it has.
82
88
  const pending = trigger.updateComplete;
@@ -189,7 +195,7 @@ export class CmTooltip extends LitElement {
189
195
  this.dispatchEvent(new CustomEvent('cm-hide', { bubbles: true, composed: true }));
190
196
  }
191
197
  updated(changed) {
192
- if (changed.has('content')) {
198
+ if (changed.has('content') || changed.has('visualOnly')) {
193
199
  __classPrivateFieldGet(this, _CmTooltip_link, "f").call(this);
194
200
  if (!this.content)
195
201
  this.hide();
@@ -244,6 +250,7 @@ CmTooltip.properties = {
244
250
  placement: { reflect: true },
245
251
  delay: { type: Number },
246
252
  open: { type: Boolean, reflect: true },
253
+ visualOnly: { type: Boolean, attribute: 'visual-only' },
247
254
  _side: { state: true },
248
255
  };
249
256
  CmTooltip.styles = [