@commmerce/ui 1.6.0 → 1.7.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { CmButton } from './components/button.js';
2
+ export { CmButtonGroup } from './components/button-group.js';
2
3
  export { CmTextField } from './components/text-field.js';
3
4
  export { CmCheckbox } from './components/checkbox.js';
4
5
  export { CmRadio, CmRadioGroup } from './components/radio.js';
@@ -9,6 +10,9 @@ export { CmSearch } from './components/search.js';
9
10
  export { CmCombobox } from './components/combobox.js';
10
11
  export { CmGlobalSearch } from './components/global-search.js';
11
12
  export type { GlobalSearchGroup, GlobalSearchItem } from './components/global-search.js';
13
+ export { CmCommandPalette, commandMatches } from './components/command-palette.js';
14
+ export type { CommandPaletteGroup, CommandPaletteItem } from './components/command-palette.js';
15
+ export { matchesShortcut, shortcutKeys, isApplePlatform } from './shortcut.js';
12
16
  export { CmDatePicker } from './components/date-picker.js';
13
17
  export { CmDateRangePicker, resolvePreset } from './components/date-range-picker.js';
14
18
  export type { DateRangePreset, DateRangePresetKey } from './components/date-range-picker.js';
@@ -70,8 +74,8 @@ export { CmBottomSheet } from './components/bottom-sheet.js';
70
74
  export { CmNotificationPanel, CmNotificationItem } from './components/notification-panel.js';
71
75
  export type { NotificationItem } from './components/notification-panel.js';
72
76
  export type { CloseReason } from './overlay.js';
73
- export { CmToast, CmToastRegion, showToast } from './components/toast.js';
74
- export type { ToastTone, ToastDismissReason, ShowToastOptions } from './components/toast.js';
77
+ export { CmToast, CmToastRegion, showToast, undoable } from './components/toast.js';
78
+ export type { ToastTone, ToastDismissReason, ToastCloseReason, ShowToastOptions, UndoableOptions } from './components/toast.js';
75
79
  export { CmSpinner } from './components/spinner.js';
76
80
  export { CmProgressBar } from './components/progress-bar.js';
77
81
  export { CmBadge } from './components/badge.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export { CmButton } from './components/button.js';
2
+ export { CmButtonGroup } from './components/button-group.js';
2
3
  export { CmTextField } from './components/text-field.js';
3
4
  export { CmCheckbox } from './components/checkbox.js';
4
5
  export { CmRadio, CmRadioGroup } from './components/radio.js';
@@ -7,6 +8,8 @@ export { CmSelect } from './components/select.js';
7
8
  export { CmSearch } from './components/search.js';
8
9
  export { CmCombobox } from './components/combobox.js';
9
10
  export { CmGlobalSearch } from './components/global-search.js';
11
+ export { CmCommandPalette, commandMatches } from './components/command-palette.js';
12
+ export { matchesShortcut, shortcutKeys, isApplePlatform } from './shortcut.js';
10
13
  export { CmDatePicker } from './components/date-picker.js';
11
14
  export { CmDateRangePicker, resolvePreset } from './components/date-range-picker.js';
12
15
  export { CmFileUpload } from './components/file-upload.js';
@@ -52,7 +55,7 @@ export { CmMenuItem } from './components/menu-item.js';
52
55
  export { CmActionMenu } from './components/action-menu.js';
53
56
  export { CmBottomSheet } from './components/bottom-sheet.js';
54
57
  export { CmNotificationPanel, CmNotificationItem } from './components/notification-panel.js';
55
- export { CmToast, CmToastRegion, showToast } from './components/toast.js';
58
+ export { CmToast, CmToastRegion, showToast, undoable } from './components/toast.js';
56
59
  export { CmSpinner } from './components/spinner.js';
57
60
  export { CmProgressBar } from './components/progress-bar.js';
58
61
  export { CmBadge } from './components/badge.js';
package/dist/overlay.d.ts CHANGED
@@ -49,6 +49,11 @@ export declare class CmOverlay extends CmLocalizedElement {
49
49
  * Returns true when the overlay closed.
50
50
  */
51
51
  requestClose(reason?: CloseReason): boolean;
52
+ /**
53
+ * Closes at once, in this task (no `cm-close`): the dialog closes, scrolling unlocks and focus is back on the
54
+ * opener before this returns, so whatever the caller does next (open another overlay) starts from there.
55
+ */
56
+ protected closeNow(): void;
52
57
  protected get dialog(): HTMLDialogElement | null;
53
58
  protected updated(changed: PropertyValues): void;
54
59
  /** Focus on opening: a light-DOM child with `autofocus`, otherwise the panel itself (so its name is read). */
package/dist/overlay.js CHANGED
@@ -1,14 +1,14 @@
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
+ };
1
6
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
7
  if (kind === "m") throw new TypeError("Private method is not writable");
3
8
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
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");
5
10
  return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
11
  };
7
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
- 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");
10
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
- };
12
12
  var _CmOverlay_instances, _CmOverlay_opener, _CmOverlay_locked, _CmOverlay_downOnBackdrop, _CmOverlay_teardown, _CmOverlay_keydown, _CmOverlay_cancel, _CmOverlay_closed, _CmOverlay_down, _CmOverlay_click;
13
13
  import { html, css, nothing } from 'lit';
14
14
  import { base, nextId } from './base.js';
@@ -92,6 +92,18 @@ export class CmOverlay extends CmLocalizedElement {
92
92
  this.open = false;
93
93
  return ok;
94
94
  }
95
+ /**
96
+ * Closes at once, in this task (no `cm-close`): the dialog closes, scrolling unlocks and focus is back on the
97
+ * opener before this returns, so whatever the caller does next (open another overlay) starts from there.
98
+ */
99
+ closeNow() {
100
+ this.open = false;
101
+ const d = this.dialog;
102
+ if (d?.open) {
103
+ d.close();
104
+ __classPrivateFieldGet(this, _CmOverlay_instances, "m", _CmOverlay_teardown).call(this);
105
+ }
106
+ }
95
107
  get dialog() { return this.renderRoot?.querySelector('dialog'); }
96
108
  updated(changed) {
97
109
  super.updated(changed);
package/dist/popover.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type TemplateResult } from 'lit';
1
2
  /**
2
3
  * The floating list under a text box, shared by `cm-combobox` and `cm-global-search`: `.pop` sits 10px under its
3
4
  * positioned parent (`.wrap`), full width, over the content below; `.message` is its "Loading" / "No results"
@@ -30,3 +31,34 @@ export declare function groupRuns<T extends {
30
31
  index: number;
31
32
  }>;
32
33
  }>;
34
+ /**
35
+ * Rows of a grouped results listbox, shared by `cm-global-search` and `cm-command-palette`: options with an
36
+ * optional icon, a title and a grey second line, the highlighted one with a grey fill and a bar on the start
37
+ * edge, "See all" rows (`.all`), and key hints (`kbd`).
38
+ */
39
+ export declare const resultStyles: import("lit").CSSResult;
40
+ /** What a result row shows: `cm-global-search` and `cm-command-palette` items. */
41
+ export interface ResultContent {
42
+ title: string;
43
+ description?: string;
44
+ icon?: string;
45
+ }
46
+ /** A group heading for a grouped listbox (see `groupStyles`), with an optional count read as `countLabel`. */
47
+ export declare function resultGroupHeading(id: string, label: string, count?: string, countLabel?: string): TemplateResult<1>;
48
+ /** Icon, title and second line of a result, then `after` (e.g. key hints). The second line is read after a comma. */
49
+ export declare function resultContent(it: ResultContent, after?: unknown): TemplateResult<1>;
50
+ /**
51
+ * One option of a results listbox: a link when it has an `href` (choosing it follows the link unless `cm-select`
52
+ * is cancelled), otherwise a plain option for an action your app runs.
53
+ */
54
+ export declare function resultOption(o: {
55
+ id: string;
56
+ active: boolean;
57
+ href?: string;
58
+ cls?: string;
59
+ keyshortcuts?: string;
60
+ onHover: () => void;
61
+ onChoose: (e: MouseEvent) => void;
62
+ }, content: unknown): TemplateResult;
63
+ /** The next highlighted row for a step (wrapping), or -1 when there are no rows. */
64
+ export declare function moveActive(active: number, step: number, count: number): number;
package/dist/popover.js CHANGED
@@ -1,4 +1,4 @@
1
- import { css } from 'lit';
1
+ import { css, html, nothing } from 'lit';
2
2
  /**
3
3
  * The floating list under a text box, shared by `cm-combobox` and `cm-global-search`: `.pop` sits 10px under its
4
4
  * positioned parent (`.wrap`), full width, over the content below; `.message` is its "Loading" / "No results"
@@ -83,3 +83,57 @@ export function groupRuns(rows) {
83
83
  });
84
84
  return runs;
85
85
  }
86
+ /**
87
+ * Rows of a grouped results listbox, shared by `cm-global-search` and `cm-command-palette`: options with an
88
+ * optional icon, a title and a grey second line, the highlighted one with a grey fill and a bar on the start
89
+ * edge, "See all" rows (`.all`), and key hints (`kbd`).
90
+ */
91
+ export const resultStyles = css `
92
+ [role='listbox'] { padding-block: var(--cm-space-4); }
93
+ [role='listbox'][hidden] { display: none; }
94
+ [role='option'] {
95
+ display: flex; align-items: center; gap: var(--cm-space-8); min-height: var(--cm-control-height-md); padding: var(--cm-space-8) var(--cm-space-12);
96
+ font-size: 14px; line-height: 20px; color: var(--cm-text-default); text-decoration: none; cursor: pointer;
97
+ }
98
+ [role='option'].active { background: var(--cm-bg-hover); box-shadow: inset 2px 0 0 var(--cm-border-strong); }
99
+ [role='option'].active:dir(rtl) { box-shadow: inset -2px 0 0 var(--cm-border-strong); }
100
+ [role='option'] cm-icon { flex: none; color: var(--cm-icon-selected); }
101
+ .text { flex: 1; min-width: 0; }
102
+ .title, .desc { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
103
+ .desc { font-size: 12px; line-height: 16px; color: var(--cm-text-tertiary); }
104
+ .all { font-size: var(--cm-font-size-13); text-decoration: underline; }
105
+ .gh { padding-inline: var(--cm-space-12); }
106
+ kbd {
107
+ flex: none; min-width: 20px; padding: 0 var(--cm-space-4); font: inherit; font-size: var(--cm-font-size-12); line-height: var(--cm-line-height-12); text-align: center;
108
+ color: var(--cm-text-tertiary); border: var(--cm-border-1) solid var(--cm-border-subtle); border-radius: var(--cm-radius-xs);
109
+ }
110
+ `;
111
+ /** A group heading for a grouped listbox (see `groupStyles`), with an optional count read as `countLabel`. */
112
+ export function resultGroupHeading(id, label, count, countLabel) {
113
+ return html `<div role="presentation" class="gh" id=${id}><span>${label}</span>${count !== undefined
114
+ ? html `<span class="count" aria-hidden="true">${count}</span><span class="vh">, ${countLabel}</span>` : nothing}</div>`;
115
+ }
116
+ /** Icon, title and second line of a result, then `after` (e.g. key hints). The second line is read after a comma. */
117
+ export function resultContent(it, after = nothing) {
118
+ return html `${it.icon ? html `<cm-icon name=${it.icon} size="16"></cm-icon>` : nothing}<span class="text"><span class="title">${it.title}</span>${it.description ? html `<span class="vh">, </span><span class="desc">${it.description}</span>` : nothing}</span>${after}`;
119
+ }
120
+ /**
121
+ * One option of a results listbox: a link when it has an `href` (choosing it follows the link unless `cm-select`
122
+ * is cancelled), otherwise a plain option for an action your app runs.
123
+ */
124
+ export function resultOption(o, content) {
125
+ const cls = `${o.cls ?? ''}${o.active ? ' active' : ''}`;
126
+ return o.href
127
+ ? html `<a role="option" id=${o.id} class=${cls} href=${o.href} tabindex="-1" aria-selected=${o.active ? 'true' : 'false'}
128
+ aria-keyshortcuts=${o.keyshortcuts || nothing} @pointerenter=${o.onHover} @click=${o.onChoose}>${content}</a>`
129
+ : html `<div role="option" id=${o.id} class=${cls} tabindex="-1" aria-selected=${o.active ? 'true' : 'false'}
130
+ aria-keyshortcuts=${o.keyshortcuts || nothing} @pointerenter=${o.onHover} @click=${o.onChoose}>${content}</div>`;
131
+ }
132
+ /** The next highlighted row for a step (wrapping), or -1 when there are no rows. */
133
+ export function moveActive(active, step, count) {
134
+ if (!count)
135
+ return -1;
136
+ if (active < 0)
137
+ return step > 0 ? 0 : count - 1;
138
+ return (active + step + count) % count;
139
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Keyboard shortcuts for components that listen on the whole page (`cm-command-palette`, `cm-global-search`).
3
+ * A shortcut is written `mod+k`, `ctrl+shift+p`, `alt+n` or `/`: modifiers joined to the key with `+`.
4
+ * `mod` is Cmd on macOS and iOS and Ctrl everywhere else. Several keys pressed one after the other (a hint like
5
+ * `g t`) are separated by spaces; they are only shown, never listened for.
6
+ */
7
+ /** True on Apple platforms, where `mod` means Cmd. */
8
+ export declare function isApplePlatform(): boolean;
9
+ /** Whether a shortcut holds Ctrl, Cmd or Alt (Shift alone doesn't count: Shift+letter types a letter). */
10
+ export declare function shortcutHasModifier(spec: string, apple?: boolean): boolean;
11
+ /** Whether a key press is the shortcut. Modifiers must match exactly; Shift is ignored for symbol keys (`?`, `/`). */
12
+ export declare function matchesShortcut(e: KeyboardEvent, spec: string, apple?: boolean): boolean;
13
+ /** Whether a key press happened while typing in a field (input, textarea, select or editable text), through shadow roots. */
14
+ export declare function isTypingTarget(e: Event): boolean;
15
+ /**
16
+ * The keys of a shortcut as they are printed on keycaps, one entry per `<kbd>`: `mod+shift+n` is ⌘ ⇧ N on a Mac
17
+ * and Ctrl Shift N elsewhere; `g t` is G T. Text that isn't a shortcut is returned as one entry.
18
+ */
19
+ export declare function shortcutKeys(spec: string, apple?: boolean): string[];
20
+ /** The shortcut in `aria-keyshortcuts` form (`Meta+K`, `Control+Shift+N`), or '' for a key sequence or text. */
21
+ export declare function ariaKeyShortcut(spec: string, apple?: boolean): string;
@@ -0,0 +1,107 @@
1
+ /**
2
+ * Keyboard shortcuts for components that listen on the whole page (`cm-command-palette`, `cm-global-search`).
3
+ * A shortcut is written `mod+k`, `ctrl+shift+p`, `alt+n` or `/`: modifiers joined to the key with `+`.
4
+ * `mod` is Cmd on macOS and iOS and Ctrl everywhere else. Several keys pressed one after the other (a hint like
5
+ * `g t`) are separated by spaces; they are only shown, never listened for.
6
+ */
7
+ /** True on Apple platforms, where `mod` means Cmd. */
8
+ export function isApplePlatform() {
9
+ const nav = typeof navigator === 'undefined' ? undefined : navigator;
10
+ return /mac|iphone|ipad|ipod/i.test(nav?.userAgentData?.platform || nav?.platform || '');
11
+ }
12
+ const ALIASES = { esc: 'escape', return: 'enter', space: ' ', up: 'arrowup', down: 'arrowdown', left: 'arrowleft', right: 'arrowright', plus: '+' };
13
+ function parse(spec, apple) {
14
+ const parts = spec.trim().toLowerCase().split('+').map((p) => p.trim());
15
+ // "mod++" is mod and the plus key.
16
+ if (parts.length > 1 && parts[parts.length - 1] === '' && parts[parts.length - 2] === '')
17
+ parts.splice(-2, 2, '+');
18
+ const key = parts.pop();
19
+ if (!key)
20
+ return null;
21
+ const p = { key: ALIASES[key] ?? key, ctrl: false, meta: false, alt: false, shift: false };
22
+ for (const m of parts) {
23
+ if (m === 'mod') {
24
+ if (apple)
25
+ p.meta = true;
26
+ else
27
+ p.ctrl = true;
28
+ }
29
+ else if (m === 'ctrl' || m === 'control')
30
+ p.ctrl = true;
31
+ else if (m === 'meta' || m === 'cmd' || m === 'command')
32
+ p.meta = true;
33
+ else if (m === 'alt' || m === 'option' || m === 'opt')
34
+ p.alt = true;
35
+ else if (m === 'shift')
36
+ p.shift = true;
37
+ else
38
+ return null;
39
+ }
40
+ return p;
41
+ }
42
+ /** Whether a shortcut holds Ctrl, Cmd or Alt (Shift alone doesn't count: Shift+letter types a letter). */
43
+ export function shortcutHasModifier(spec, apple = isApplePlatform()) {
44
+ const p = parse(spec, apple);
45
+ return !!p && (p.ctrl || p.meta || p.alt);
46
+ }
47
+ /** Whether a key press is the shortcut. Modifiers must match exactly; Shift is ignored for symbol keys (`?`, `/`). */
48
+ export function matchesShortcut(e, spec, apple = isApplePlatform()) {
49
+ const p = parse(spec, apple);
50
+ if (!p || !e.key)
51
+ return false;
52
+ if (e.ctrlKey !== p.ctrl || e.metaKey !== p.meta || e.altKey !== p.alt)
53
+ return false;
54
+ const letterOrDigit = /^[a-z0-9]$/.test(p.key);
55
+ if ((letterOrDigit || p.key.length > 1) && e.shiftKey !== p.shift)
56
+ return false;
57
+ if (e.key.toLowerCase() === p.key)
58
+ return true;
59
+ // Alt (Option on a Mac) changes the character typed: fall back to the physical key.
60
+ if (letterOrDigit)
61
+ return e.code === (/\d/.test(p.key) ? `Digit${p.key}` : `Key${p.key.toUpperCase()}`);
62
+ return false;
63
+ }
64
+ /** Whether a key press happened while typing in a field (input, textarea, select or editable text), through shadow roots. */
65
+ export function isTypingTarget(e) {
66
+ const t = e.composedPath()[0];
67
+ return !!t && (t.isContentEditable || /^(input|textarea|select)$/i.test(t.localName ?? ''));
68
+ }
69
+ const APPLE_NAMES = { meta: '⌘', ctrl: '⌃', alt: '⌥', shift: '⇧' };
70
+ const OTHER_NAMES = { meta: 'Win', ctrl: 'Ctrl', alt: 'Alt', shift: 'Shift' };
71
+ const KEY_NAMES = {
72
+ escape: 'Esc', enter: '↵', ' ': 'Space', arrowup: '↑', arrowdown: '↓', arrowleft: '←', arrowright: '→',
73
+ backspace: '⌫', delete: 'Del', tab: 'Tab', pageup: 'PgUp', pagedown: 'PgDn', home: 'Home', end: 'End',
74
+ };
75
+ const keyName = (k) => KEY_NAMES[k] ?? (k.length === 1 ? k.toUpperCase() : k[0].toUpperCase() + k.slice(1));
76
+ /**
77
+ * The keys of a shortcut as they are printed on keycaps, one entry per `<kbd>`: `mod+shift+n` is ⌘ ⇧ N on a Mac
78
+ * and Ctrl Shift N elsewhere; `g t` is G T. Text that isn't a shortcut is returned as one entry.
79
+ */
80
+ export function shortcutKeys(spec, apple = isApplePlatform()) {
81
+ const out = [];
82
+ for (const step of spec.trim().split(/\s+/).filter(Boolean)) {
83
+ const p = parse(step, apple);
84
+ if (!p)
85
+ return [spec.trim()];
86
+ const names = apple ? APPLE_NAMES : OTHER_NAMES;
87
+ // Apple's order is Ctrl Alt Shift Cmd; elsewhere Ctrl Win Alt Shift.
88
+ const order = apple ? ['ctrl', 'alt', 'shift', 'meta'] : ['ctrl', 'meta', 'alt', 'shift'];
89
+ for (const m of order)
90
+ if (p[m])
91
+ out.push(names[m]);
92
+ out.push(keyName(p.key));
93
+ }
94
+ return out;
95
+ }
96
+ /** The shortcut in `aria-keyshortcuts` form (`Meta+K`, `Control+Shift+N`), or '' for a key sequence or text. */
97
+ export function ariaKeyShortcut(spec, apple = isApplePlatform()) {
98
+ if (/\s/.test(spec.trim()))
99
+ return '';
100
+ const p = parse(spec, apple);
101
+ if (!p)
102
+ return '';
103
+ // aria-keyshortcuts uses KeyboardEvent.key names: K, Space, Escape, ArrowUp, PageDown.
104
+ const k = p.key === ' ' ? 'Space' : p.key.length === 1 ? p.key.toUpperCase()
105
+ : p.key.replace(/^(arrow|page)?(\w)/, (_, pre, c) => pre ? `${pre[0].toUpperCase()}${pre.slice(1)}${c.toUpperCase()}` : c.toUpperCase());
106
+ return [p.ctrl && 'Control', p.meta && 'Meta', p.alt && 'Alt', p.shift && 'Shift', k].filter(Boolean).join('+');
107
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@commmerce/ui",
3
- "version": "1.6.0",
3
+ "version": "1.7.1",
4
4
  "description": "Commmerce UI web components: build apps that feel native to the Commmerce admin. Works in plain HTML, React, Vue and server-rendered pages.",
5
5
  "keywords": [
6
6
  "web-components",
@@ -50,8 +50,8 @@
50
50
  "test": "vitest run"
51
51
  },
52
52
  "dependencies": {
53
- "@commmerce/icons": "1.6.0",
54
- "@commmerce/tokens": "1.6.0",
53
+ "@commmerce/icons": "1.7.1",
54
+ "@commmerce/tokens": "1.7.1",
55
55
  "lit": "3.2.1"
56
56
  },
57
57
  "devDependencies": {