@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/cdn.js +822 -679
- package/dist/components/button-group.d.ts +90 -0
- package/dist/components/button-group.js +290 -0
- package/dist/components/command-palette.d.ts +197 -0
- package/dist/components/command-palette.js +325 -0
- package/dist/components/data-table.d.ts +6 -0
- package/dist/components/data-table.js +24 -4
- package/dist/components/global-search.js +10 -37
- package/dist/components/info-panel.d.ts +19 -1
- package/dist/components/info-panel.js +26 -3
- package/dist/components/layout.d.ts +48 -2
- package/dist/components/layout.js +134 -5
- package/dist/components/page-header.d.ts +22 -2
- package/dist/components/page-header.js +25 -5
- package/dist/components/status-tabs.js +7 -2
- package/dist/components/toast.d.ts +38 -1
- package/dist/components/toast.js +163 -15
- package/dist/custom-elements.json +4314 -2522
- package/dist/index.d.ts +6 -2
- package/dist/index.js +4 -1
- package/dist/overlay.d.ts +5 -0
- package/dist/overlay.js +17 -5
- package/dist/popover.d.ts +32 -0
- package/dist/popover.js +55 -1
- package/dist/shortcut.d.ts +21 -0
- package/dist/shortcut.js +107 -0
- package/package.json +3 -3
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { type PropertyValues } from 'lit';
|
|
2
|
+
import { CmLocalizedElement } from '../localize.js';
|
|
3
|
+
import type { MenuItem } from './menu-item.js';
|
|
4
|
+
import './action-menu.js';
|
|
5
|
+
declare const strings: {
|
|
6
|
+
/** Text of the trigger that holds the buttons that don't fit (its accessible name with `more-icon`), and the menu's name. */
|
|
7
|
+
more: string;
|
|
8
|
+
};
|
|
9
|
+
declare module '../localize.js' {
|
|
10
|
+
interface CmStrings {
|
|
11
|
+
buttonGroup: typeof strings;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A row of buttons 8px apart: a task's outcome choices ("Spoke to them", "No answer, reschedule", "Not interested"),
|
|
16
|
+
* card actions or a toolbar. By default the row wraps onto more lines when it runs out of room. With
|
|
17
|
+
* `overflow="menu"` it stays on one line: the buttons that don't fit move into a trailing "More" menu (measured
|
|
18
|
+
* from the group's own width, so it works in cards and side panels as well as on phones), the least important first.
|
|
19
|
+
*
|
|
20
|
+
* Choosing a menu item clicks the original button, so your click handlers, `href` and `type="submit"` keep working.
|
|
21
|
+
* Buttons in the menu are `hidden` (out of the layout, the tab order and the accessibility tree) and come back when
|
|
22
|
+
* there is room again.
|
|
23
|
+
* @element cm-button-group
|
|
24
|
+
* @slot - `cm-button`s, in reading order. Put `data-priority="high"` on a button to keep it visible longest and
|
|
25
|
+
* `data-priority="low"` to move it into the menu first; a `variant="primary"` button stays visible if it fits at all.
|
|
26
|
+
* @csspart row - The flex row (`role="group"`).
|
|
27
|
+
* @csspart more - The `cm-action-menu` that holds the buttons that don't fit.
|
|
28
|
+
* @fires cm-overflow-change - When the set of buttons in the More menu changes. `detail: { hidden: HTMLElement[] }`.
|
|
29
|
+
*/
|
|
30
|
+
export declare class CmButtonGroup extends CmLocalizedElement {
|
|
31
|
+
#private;
|
|
32
|
+
static properties: {
|
|
33
|
+
overflow: {
|
|
34
|
+
reflect: boolean;
|
|
35
|
+
};
|
|
36
|
+
moreLabel: {
|
|
37
|
+
attribute: string;
|
|
38
|
+
string: string;
|
|
39
|
+
};
|
|
40
|
+
moreIcon: {
|
|
41
|
+
attribute: string;
|
|
42
|
+
};
|
|
43
|
+
size: {
|
|
44
|
+
reflect: boolean;
|
|
45
|
+
};
|
|
46
|
+
align: {
|
|
47
|
+
reflect: boolean;
|
|
48
|
+
};
|
|
49
|
+
label: {};
|
|
50
|
+
_items: {
|
|
51
|
+
state: boolean;
|
|
52
|
+
};
|
|
53
|
+
_size: {
|
|
54
|
+
state: boolean;
|
|
55
|
+
};
|
|
56
|
+
};
|
|
57
|
+
/** `wrap` (default): buttons wrap onto more lines. `menu`: one line; the buttons that don't fit move into a More menu at the end. */
|
|
58
|
+
overflow: 'wrap' | 'menu';
|
|
59
|
+
/** Text of the More trigger, and its accessible name with `more-icon`. Default: the built-in string `more` ("More"). */
|
|
60
|
+
moreLabel: string;
|
|
61
|
+
/**
|
|
62
|
+
* Makes the More trigger a square icon button named by `more-label`. The value is the icon: empty (just the attribute)
|
|
63
|
+
* is `more` (⋮). Use it when even "More" is too wide, e.g. in table rows.
|
|
64
|
+
*/
|
|
65
|
+
moreIcon?: string | null;
|
|
66
|
+
/** Size of the More trigger: `large` 40, `medium` 36, `slim` 32 (40 on touch screens). Default: the first button's `size`. Give every button the same size. */
|
|
67
|
+
size?: 'large' | 'medium' | 'slim';
|
|
68
|
+
/** `start` (default) lines the buttons up at the start edge; `end` at the end edge (dialog and card footers). */
|
|
69
|
+
align: 'start' | 'end';
|
|
70
|
+
/** Accessible name of the group, e.g. "Call outcome". */
|
|
71
|
+
label: string;
|
|
72
|
+
_items: MenuItem[];
|
|
73
|
+
_size: 'large' | 'medium' | 'slim';
|
|
74
|
+
static styles: import("lit").CSSResult[];
|
|
75
|
+
/** The buttons currently moved into the More menu, in reading order. */
|
|
76
|
+
get overflowing(): HTMLElement[];
|
|
77
|
+
connectedCallback(): void;
|
|
78
|
+
disconnectedCallback(): void;
|
|
79
|
+
protected updated(changed: PropertyValues): void;
|
|
80
|
+
protected firstUpdated(): void;
|
|
81
|
+
/** Measures again and moves buttons into or out of the More menu. Runs by itself on resize and on changes to the buttons. */
|
|
82
|
+
relayout(): void;
|
|
83
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
84
|
+
}
|
|
85
|
+
declare global {
|
|
86
|
+
interface HTMLElementTagNameMap {
|
|
87
|
+
'cm-button-group': CmButtonGroup;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
export {};
|
|
@@ -0,0 +1,290 @@
|
|
|
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 _CmButtonGroup_instances, _CmButtonGroup_hidden, _CmButtonGroup_mo, _CmButtonGroup_ro, _CmButtonGroup_frame, _CmButtonGroup_watched, _CmButtonGroup_refocus, _CmButtonGroup_schedule, _CmButtonGroup_mutated, _CmButtonGroup_buttons, _CmButtonGroup_restore, _CmButtonGroup_more, _CmButtonGroup_layout, _CmButtonGroup_item, _CmButtonGroup_choose, _CmButtonGroup_rescueFocus, _CmButtonGroup_focusMore, _CmButtonGroup_focusLastButton;
|
|
13
|
+
import { html, css, nothing } from 'lit';
|
|
14
|
+
import { base, define } from '../base.js';
|
|
15
|
+
import { CmLocalizedElement, defineStrings } from '../localize.js';
|
|
16
|
+
import './action-menu.js';
|
|
17
|
+
const strings = defineStrings('buttonGroup', {
|
|
18
|
+
/** Text of the trigger that holds the buttons that don't fit (its accessible name with `more-icon`), and the menu's name. */
|
|
19
|
+
more: 'More',
|
|
20
|
+
});
|
|
21
|
+
/** Set on a button the group moved into its More menu (together with `hidden`). */
|
|
22
|
+
const MOVED = 'data-cm-overflow';
|
|
23
|
+
/** Attributes of a button that change what its menu item shows or how it fits. */
|
|
24
|
+
const WATCHED = ['disabled', 'loading', 'tone', 'icon', 'label', 'variant', 'size', 'icon-only', 'count', 'href', 'hidden', 'data-priority'];
|
|
25
|
+
/**
|
|
26
|
+
* A row of buttons 8px apart: a task's outcome choices ("Spoke to them", "No answer, reschedule", "Not interested"),
|
|
27
|
+
* card actions or a toolbar. By default the row wraps onto more lines when it runs out of room. With
|
|
28
|
+
* `overflow="menu"` it stays on one line: the buttons that don't fit move into a trailing "More" menu (measured
|
|
29
|
+
* from the group's own width, so it works in cards and side panels as well as on phones), the least important first.
|
|
30
|
+
*
|
|
31
|
+
* Choosing a menu item clicks the original button, so your click handlers, `href` and `type="submit"` keep working.
|
|
32
|
+
* Buttons in the menu are `hidden` (out of the layout, the tab order and the accessibility tree) and come back when
|
|
33
|
+
* there is room again.
|
|
34
|
+
* @element cm-button-group
|
|
35
|
+
* @slot - `cm-button`s, in reading order. Put `data-priority="high"` on a button to keep it visible longest and
|
|
36
|
+
* `data-priority="low"` to move it into the menu first; a `variant="primary"` button stays visible if it fits at all.
|
|
37
|
+
* @csspart row - The flex row (`role="group"`).
|
|
38
|
+
* @csspart more - The `cm-action-menu` that holds the buttons that don't fit.
|
|
39
|
+
* @fires cm-overflow-change - When the set of buttons in the More menu changes. `detail: { hidden: HTMLElement[] }`.
|
|
40
|
+
*/
|
|
41
|
+
export class CmButtonGroup extends CmLocalizedElement {
|
|
42
|
+
constructor() {
|
|
43
|
+
super(...arguments);
|
|
44
|
+
_CmButtonGroup_instances.add(this);
|
|
45
|
+
/** `wrap` (default): buttons wrap onto more lines. `menu`: one line; the buttons that don't fit move into a More menu at the end. */
|
|
46
|
+
this.overflow = 'wrap';
|
|
47
|
+
/** `start` (default) lines the buttons up at the start edge; `end` at the end edge (dialog and card footers). */
|
|
48
|
+
this.align = 'start';
|
|
49
|
+
/** Accessible name of the group, e.g. "Call outcome". */
|
|
50
|
+
this.label = '';
|
|
51
|
+
this._items = [];
|
|
52
|
+
this._size = 'medium';
|
|
53
|
+
_CmButtonGroup_hidden.set(this, []);
|
|
54
|
+
_CmButtonGroup_mo.set(this, void 0);
|
|
55
|
+
_CmButtonGroup_ro.set(this, void 0);
|
|
56
|
+
_CmButtonGroup_frame.set(this, 0);
|
|
57
|
+
_CmButtonGroup_watched.set(this, new Set());
|
|
58
|
+
_CmButtonGroup_refocus.set(this, false);
|
|
59
|
+
_CmButtonGroup_choose.set(this, (e) => {
|
|
60
|
+
// The group's menu is an implementation detail: the original button's click is the event apps listen to.
|
|
61
|
+
e.stopPropagation();
|
|
62
|
+
const b = __classPrivateFieldGet(this, _CmButtonGroup_hidden, "f")[Number(e.detail.value)];
|
|
63
|
+
if (!b)
|
|
64
|
+
return;
|
|
65
|
+
__classPrivateFieldSet(this, _CmButtonGroup_refocus, true, "f");
|
|
66
|
+
const control = b.shadowRoot?.querySelector('[part="control"]');
|
|
67
|
+
(control ?? b).click();
|
|
68
|
+
requestAnimationFrame(() => requestAnimationFrame(() => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_rescueFocus).call(this)));
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/** The buttons currently moved into the More menu, in reading order. */
|
|
72
|
+
get overflowing() { return [...__classPrivateFieldGet(this, _CmButtonGroup_hidden, "f")]; }
|
|
73
|
+
connectedCallback() {
|
|
74
|
+
super.connectedCallback();
|
|
75
|
+
__classPrivateFieldSet(this, _CmButtonGroup_mo, new MutationObserver((records) => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_mutated).call(this, records)), "f");
|
|
76
|
+
__classPrivateFieldGet(this, _CmButtonGroup_mo, "f").observe(this, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: WATCHED });
|
|
77
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
78
|
+
// The group, the More trigger and each button: their widths change with the page, fonts and late rendering.
|
|
79
|
+
// Layout runs a frame later, never inside this callback, so it can't cause a resize loop.
|
|
80
|
+
__classPrivateFieldSet(this, _CmButtonGroup_ro, new ResizeObserver(() => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_schedule).call(this)), "f");
|
|
81
|
+
__classPrivateFieldGet(this, _CmButtonGroup_ro, "f").observe(this);
|
|
82
|
+
__classPrivateFieldSet(this, _CmButtonGroup_watched, new Set([this]), "f");
|
|
83
|
+
}
|
|
84
|
+
document.fonts?.ready.then(() => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_schedule).call(this));
|
|
85
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_schedule).call(this);
|
|
86
|
+
}
|
|
87
|
+
disconnectedCallback() {
|
|
88
|
+
super.disconnectedCallback();
|
|
89
|
+
__classPrivateFieldGet(this, _CmButtonGroup_mo, "f")?.disconnect();
|
|
90
|
+
__classPrivateFieldGet(this, _CmButtonGroup_ro, "f")?.disconnect();
|
|
91
|
+
__classPrivateFieldGet(this, _CmButtonGroup_watched, "f").clear();
|
|
92
|
+
cancelAnimationFrame(__classPrivateFieldGet(this, _CmButtonGroup_frame, "f"));
|
|
93
|
+
__classPrivateFieldSet(this, _CmButtonGroup_frame, 0, "f");
|
|
94
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_restore).call(this, __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_buttons).call(this));
|
|
95
|
+
}
|
|
96
|
+
updated(changed) {
|
|
97
|
+
if (changed.has('overflow') || changed.has('moreLabel') || changed.has('moreIcon') || changed.has('size'))
|
|
98
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_schedule).call(this);
|
|
99
|
+
}
|
|
100
|
+
// Lay out straight after the first render (a microtask, so before the first paint).
|
|
101
|
+
firstUpdated() { this.updateComplete.then(() => this.relayout()); }
|
|
102
|
+
/** Measures again and moves buttons into or out of the More menu. Runs by itself on resize and on changes to the buttons. */
|
|
103
|
+
relayout() {
|
|
104
|
+
cancelAnimationFrame(__classPrivateFieldGet(this, _CmButtonGroup_frame, "f"));
|
|
105
|
+
__classPrivateFieldSet(this, _CmButtonGroup_frame, 0, "f");
|
|
106
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_layout).call(this);
|
|
107
|
+
}
|
|
108
|
+
render() {
|
|
109
|
+
const icon = this.moreIcon != null;
|
|
110
|
+
return html `<div class="row" part="row" role="group" aria-label=${this.label || nothing}>
|
|
111
|
+
<slot></slot>
|
|
112
|
+
<cm-action-menu class="more ${this._size}${icon ? ' icon' : ''}" part="more" hidden align="end"
|
|
113
|
+
.items=${this._items} label=${this.moreLabel} ?icon-only=${icon} icon=${this.moreIcon || 'more'}
|
|
114
|
+
@cm-select=${__classPrivateFieldGet(this, _CmButtonGroup_choose, "f")}></cm-action-menu>
|
|
115
|
+
</div>`;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
_CmButtonGroup_hidden = new WeakMap(), _CmButtonGroup_mo = new WeakMap(), _CmButtonGroup_ro = new WeakMap(), _CmButtonGroup_frame = new WeakMap(), _CmButtonGroup_watched = new WeakMap(), _CmButtonGroup_refocus = new WeakMap(), _CmButtonGroup_choose = new WeakMap(), _CmButtonGroup_instances = new WeakSet(), _CmButtonGroup_schedule = function _CmButtonGroup_schedule() {
|
|
119
|
+
if (__classPrivateFieldGet(this, _CmButtonGroup_frame, "f") || !this.isConnected)
|
|
120
|
+
return;
|
|
121
|
+
__classPrivateFieldSet(this, _CmButtonGroup_frame, requestAnimationFrame(() => {
|
|
122
|
+
__classPrivateFieldSet(this, _CmButtonGroup_frame, 0, "f");
|
|
123
|
+
// Wait for buttons that are still rendering, so their widths are real.
|
|
124
|
+
const pending = __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_buttons).call(this).map((b) => b.updateComplete).filter(Boolean);
|
|
125
|
+
if (pending.length)
|
|
126
|
+
Promise.all(pending).then(() => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_layout).call(this));
|
|
127
|
+
else
|
|
128
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_layout).call(this);
|
|
129
|
+
}), "f");
|
|
130
|
+
}, _CmButtonGroup_mutated = function _CmButtonGroup_mutated(records) {
|
|
131
|
+
for (const r of records) {
|
|
132
|
+
// A button taken out of the group while it was in the menu gets its place back.
|
|
133
|
+
for (const n of Array.from(r.removedNodes)) {
|
|
134
|
+
if (n instanceof HTMLElement && n.hasAttribute(MOVED) && n.parentElement !== this) {
|
|
135
|
+
n.removeAttribute(MOVED);
|
|
136
|
+
n.hidden = false;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_schedule).call(this);
|
|
141
|
+
}, _CmButtonGroup_buttons = function _CmButtonGroup_buttons() {
|
|
142
|
+
return Array.from(this.children).filter((c) => c.localName === 'cm-button' && (!c.hidden || c.hasAttribute(MOVED)));
|
|
143
|
+
}, _CmButtonGroup_restore = function _CmButtonGroup_restore(list) {
|
|
144
|
+
for (const b of list)
|
|
145
|
+
if (b.hasAttribute(MOVED)) {
|
|
146
|
+
b.removeAttribute(MOVED);
|
|
147
|
+
b.hidden = false;
|
|
148
|
+
}
|
|
149
|
+
}, _CmButtonGroup_more = function _CmButtonGroup_more() { return this.renderRoot.querySelector('.more'); }, _CmButtonGroup_layout = function _CmButtonGroup_layout() {
|
|
150
|
+
const row = this.renderRoot.querySelector('.row');
|
|
151
|
+
const more = __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_more).call(this);
|
|
152
|
+
if (!row || !more || !this.isConnected)
|
|
153
|
+
return;
|
|
154
|
+
const buttons = __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_buttons).call(this);
|
|
155
|
+
// Where focus is now, so it isn't lost when its element moves into the menu or More goes away.
|
|
156
|
+
const hadFocus = this.shadowRoot.activeElement === more ? more : buttons.find((b) => b.matches(':focus-within')) ?? null;
|
|
157
|
+
const first = buttons[0]?.getAttribute('size');
|
|
158
|
+
this._size = this.size || (first === 'slim' || first === 'large' ? first : 'medium');
|
|
159
|
+
const hide = new Set();
|
|
160
|
+
if (__classPrivateFieldGet(this, _CmButtonGroup_ro, "f")) {
|
|
161
|
+
const now = new Set([this, more, ...buttons]);
|
|
162
|
+
for (const el of __classPrivateFieldGet(this, _CmButtonGroup_watched, "f"))
|
|
163
|
+
if (!now.has(el))
|
|
164
|
+
__classPrivateFieldGet(this, _CmButtonGroup_ro, "f").unobserve(el);
|
|
165
|
+
for (const el of now)
|
|
166
|
+
if (!__classPrivateFieldGet(this, _CmButtonGroup_watched, "f").has(el))
|
|
167
|
+
__classPrivateFieldGet(this, _CmButtonGroup_ro, "f").observe(el);
|
|
168
|
+
__classPrivateFieldSet(this, _CmButtonGroup_watched, now, "f");
|
|
169
|
+
}
|
|
170
|
+
if (this.overflow === 'menu') {
|
|
171
|
+
// Measure every item as if all fit: put the moved buttons back for a moment (no paint happens in between).
|
|
172
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_restore).call(this, buttons);
|
|
173
|
+
more.hidden = false;
|
|
174
|
+
const avail = row.clientWidth;
|
|
175
|
+
const gap = parseFloat(getComputedStyle(row).columnGap) || 0;
|
|
176
|
+
const items = Array.from(this.children).filter((c) => !c.hidden);
|
|
177
|
+
const width = new Map(items.map((c) => [c, c.getBoundingClientRect().width]));
|
|
178
|
+
const total = items.reduce((s, c) => s + width.get(c), 0) + gap * Math.max(items.length - 1, 0);
|
|
179
|
+
if (total > avail + 0.5) {
|
|
180
|
+
const rank = (b) => {
|
|
181
|
+
const p = b.dataset.priority;
|
|
182
|
+
return p === 'low' ? 0 : p === 'high' ? 2 : b.getAttribute('variant') === 'primary' ? 3 : 1;
|
|
183
|
+
};
|
|
184
|
+
// Least important first; among equals, the last in reading order first.
|
|
185
|
+
const order = buttons.map((b, i) => ({ b, i, r: rank(b) })).sort((a, z) => a.r - z.r || z.i - a.i);
|
|
186
|
+
let used = total + gap + more.getBoundingClientRect().width;
|
|
187
|
+
for (const { b } of order) {
|
|
188
|
+
if (used <= avail + 0.5)
|
|
189
|
+
break;
|
|
190
|
+
hide.add(b);
|
|
191
|
+
used -= width.get(b) + gap;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
for (const b of buttons) {
|
|
196
|
+
if (hide.has(b)) {
|
|
197
|
+
b.setAttribute(MOVED, '');
|
|
198
|
+
b.hidden = true;
|
|
199
|
+
}
|
|
200
|
+
else if (b.hasAttribute(MOVED)) {
|
|
201
|
+
b.removeAttribute(MOVED);
|
|
202
|
+
b.hidden = false;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
more.hidden = hide.size === 0;
|
|
206
|
+
if (hadFocus === more && more.hidden)
|
|
207
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_focusLastButton).call(this);
|
|
208
|
+
else if (hadFocus && hadFocus !== more && hide.has(hadFocus))
|
|
209
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_focusMore).call(this);
|
|
210
|
+
if (hide.size === 0 && more.open)
|
|
211
|
+
more.hide();
|
|
212
|
+
// Our own attribute changes are not changes to the buttons.
|
|
213
|
+
__classPrivateFieldGet(this, _CmButtonGroup_mo, "f")?.takeRecords();
|
|
214
|
+
const hidden = buttons.filter((b) => hide.has(b));
|
|
215
|
+
this._items = hidden.map((b, i) => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_item).call(this, b, i));
|
|
216
|
+
const changed = hidden.length !== __classPrivateFieldGet(this, _CmButtonGroup_hidden, "f").length || hidden.some((b, i) => b !== __classPrivateFieldGet(this, _CmButtonGroup_hidden, "f")[i]);
|
|
217
|
+
__classPrivateFieldSet(this, _CmButtonGroup_hidden, hidden, "f");
|
|
218
|
+
if (changed)
|
|
219
|
+
this.dispatchEvent(new CustomEvent('cm-overflow-change', { detail: { hidden: [...hidden] }, bubbles: true, composed: true }));
|
|
220
|
+
if (__classPrivateFieldGet(this, _CmButtonGroup_refocus, "f"))
|
|
221
|
+
this.updateComplete.then(() => __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_rescueFocus).call(this));
|
|
222
|
+
}, _CmButtonGroup_item = function _CmButtonGroup_item(b, i) {
|
|
223
|
+
const text = Array.from(b.childNodes)
|
|
224
|
+
.filter((n) => !(n instanceof Element && n.hasAttribute('slot')))
|
|
225
|
+
.map((n) => n.textContent ?? '').join('').replace(/\s+/g, ' ').trim();
|
|
226
|
+
const label = b.hasAttribute('icon-only') ? b.getAttribute('label') || text : text || b.getAttribute('label') || '';
|
|
227
|
+
const icon = b.getAttribute('icon') || b.querySelector(':scope > [slot="icon"]')?.getAttribute('name') || undefined;
|
|
228
|
+
return {
|
|
229
|
+
label, value: String(i), icon,
|
|
230
|
+
disabled: b.hasAttribute('disabled') || b.hasAttribute('loading') || undefined,
|
|
231
|
+
critical: b.getAttribute('tone') === 'critical' || undefined,
|
|
232
|
+
};
|
|
233
|
+
}, _CmButtonGroup_rescueFocus = function _CmButtonGroup_rescueFocus() {
|
|
234
|
+
if (!__classPrivateFieldGet(this, _CmButtonGroup_refocus, "f"))
|
|
235
|
+
return;
|
|
236
|
+
__classPrivateFieldSet(this, _CmButtonGroup_refocus, false, "f");
|
|
237
|
+
const active = document.activeElement;
|
|
238
|
+
const more = __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_more).call(this);
|
|
239
|
+
const lost = !active || active === document.body || active === document.documentElement
|
|
240
|
+
|| (active === this && (!more || more.hidden || this.shadowRoot.activeElement !== more));
|
|
241
|
+
if (!lost)
|
|
242
|
+
return;
|
|
243
|
+
if (more && !more.hidden)
|
|
244
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_focusMore).call(this);
|
|
245
|
+
else
|
|
246
|
+
__classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_focusLastButton).call(this);
|
|
247
|
+
}, _CmButtonGroup_focusMore = function _CmButtonGroup_focusMore() { __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_more).call(this)?.shadowRoot?.querySelector('[part="trigger"]')?.focus(); }, _CmButtonGroup_focusLastButton = function _CmButtonGroup_focusLastButton() {
|
|
248
|
+
const visible = __classPrivateFieldGet(this, _CmButtonGroup_instances, "m", _CmButtonGroup_buttons).call(this).filter((b) => !b.hidden && !b.hasAttribute('disabled'));
|
|
249
|
+
const last = visible[visible.length - 1];
|
|
250
|
+
(last?.shadowRoot?.querySelector('[part="control"]') ?? last)?.focus();
|
|
251
|
+
};
|
|
252
|
+
CmButtonGroup.properties = {
|
|
253
|
+
overflow: { reflect: true },
|
|
254
|
+
moreLabel: { attribute: 'more-label', string: 'more' },
|
|
255
|
+
moreIcon: { attribute: 'more-icon' },
|
|
256
|
+
size: { reflect: true },
|
|
257
|
+
align: { reflect: true },
|
|
258
|
+
label: {},
|
|
259
|
+
_items: { state: true },
|
|
260
|
+
_size: { state: true },
|
|
261
|
+
};
|
|
262
|
+
CmButtonGroup.styles = [
|
|
263
|
+
base,
|
|
264
|
+
css `
|
|
265
|
+
:host { display: block; min-inline-size: 0; }
|
|
266
|
+
.row { display: flex; flex-wrap: wrap; align-items: center; gap: var(--cm-space-8); min-inline-size: 0; }
|
|
267
|
+
:host([align='end']) .row { justify-content: flex-end; }
|
|
268
|
+
:host([overflow='menu']) .row { flex-wrap: nowrap; }
|
|
269
|
+
::slotted(*) { flex: none; }
|
|
270
|
+
.more { flex: none; }
|
|
271
|
+
.more[hidden] { display: none; }
|
|
272
|
+
/* The More trigger matches the buttons (owner standard, 1.6): medium 36, slim 32, large 40; 40 on touch screens with
|
|
273
|
+
a 44px hit area (set by cm-action-menu). Text 14px (13px slim), like cm-button. */
|
|
274
|
+
.more::part(trigger) { min-height: var(--cm-control-height-md, 36px); padding: 0 var(--cm-space-16); font-size: var(--cm-font-size-14); }
|
|
275
|
+
.more.slim::part(trigger) { min-height: var(--cm-control-height-sm, 32px); padding: 0 var(--cm-space-12); font-size: var(--cm-font-size-13); }
|
|
276
|
+
.more.large::part(trigger) { min-height: var(--cm-control-height-lg, 40px); padding: 0 var(--cm-space-20); }
|
|
277
|
+
/* Icon-only: a square outlined like a secondary icon button. */
|
|
278
|
+
.more.icon::part(trigger) {
|
|
279
|
+
width: var(--cm-control-height-md, 36px); padding: 0; background: var(--cm-bg-surface); border-color: var(--cm-border-input);
|
|
280
|
+
}
|
|
281
|
+
.more.icon.slim::part(trigger) { width: var(--cm-control-height-sm, 32px); }
|
|
282
|
+
.more.icon.large::part(trigger) { width: var(--cm-control-height-lg, 40px); }
|
|
283
|
+
.more.icon::part(trigger):hover, .more.icon[open]::part(trigger) { background: var(--cm-bg-hover); }
|
|
284
|
+
@media (pointer: coarse) {
|
|
285
|
+
.more::part(trigger), .more.slim::part(trigger) { min-height: var(--cm-control-height-lg, 40px); }
|
|
286
|
+
.more.icon::part(trigger), .more.icon.slim::part(trigger) { width: var(--cm-control-height-lg, 40px); }
|
|
287
|
+
}
|
|
288
|
+
`,
|
|
289
|
+
];
|
|
290
|
+
define('cm-button-group', CmButtonGroup);
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { type PropertyValues } from 'lit';
|
|
2
|
+
import { CmOverlay } from '../overlay.js';
|
|
3
|
+
import './icon.js';
|
|
4
|
+
import './spinner.js';
|
|
5
|
+
/** One record or action in `cm-command-palette`. */
|
|
6
|
+
export interface CommandPaletteItem {
|
|
7
|
+
/** What it is or does: "Ravi Stores", "New lead", "Go to Tasks". */
|
|
8
|
+
title: string;
|
|
9
|
+
/** A second line in smaller grey text: "Lead · Mumbai". */
|
|
10
|
+
description?: string;
|
|
11
|
+
/** A page to open. Items with an `href` are links; items without one are actions your app runs (see `run` and `cm-select`). */
|
|
12
|
+
href?: string;
|
|
13
|
+
/** Icon name from `@commmerce/icons`, shown before the title. */
|
|
14
|
+
icon?: string;
|
|
15
|
+
/** Identifies the item in `cm-select`. */
|
|
16
|
+
value?: string;
|
|
17
|
+
/**
|
|
18
|
+
* A key hint shown on the right as keycaps: `mod+shift+l` (⌘ ⇧ L on a Mac, Ctrl Shift L elsewhere), or `g t` for
|
|
19
|
+
* keys pressed one after the other. Only shown: your app listens for the shortcut itself.
|
|
20
|
+
*/
|
|
21
|
+
shortcut?: string;
|
|
22
|
+
/** More words that find this action when typed: `['create', 'add']` for "New lead". Only used for `actions`. */
|
|
23
|
+
keywords?: string[];
|
|
24
|
+
/** Called when the item is chosen and `cm-select` wasn't cancelled, after the palette has closed. Set it in JavaScript. */
|
|
25
|
+
run?: (item: CommandPaletteItem) => void;
|
|
26
|
+
}
|
|
27
|
+
/** A group of items in `cm-command-palette`: "Records", "Actions", "Navigate". */
|
|
28
|
+
export interface CommandPaletteGroup {
|
|
29
|
+
/** Group heading: "Actions". */
|
|
30
|
+
label: string;
|
|
31
|
+
/** For search results: how many records match in total, shown next to the heading. Not shown when left out. */
|
|
32
|
+
total?: number;
|
|
33
|
+
/** The items, best first. */
|
|
34
|
+
items: CommandPaletteItem[];
|
|
35
|
+
/** For search results: link to every match (your list page filtered by the query). Shows a "See all" row. */
|
|
36
|
+
href?: string;
|
|
37
|
+
/** Text of that row, instead of the component's `see-all-label`. */
|
|
38
|
+
seeAllLabel?: string;
|
|
39
|
+
}
|
|
40
|
+
declare const strings: {
|
|
41
|
+
/** Accessible name of the dialog and its text box. */
|
|
42
|
+
label: string;
|
|
43
|
+
placeholder: string;
|
|
44
|
+
/** Heading of the recent items, shown while the box is empty. */
|
|
45
|
+
recent: string;
|
|
46
|
+
/** Shown and announced when nothing matches. */
|
|
47
|
+
noResults: string;
|
|
48
|
+
/** Announced as the list changes. */
|
|
49
|
+
results: {
|
|
50
|
+
one: string;
|
|
51
|
+
other: string;
|
|
52
|
+
};
|
|
53
|
+
/** The row under a group of search results that links to every match. */
|
|
54
|
+
seeAll: string;
|
|
55
|
+
};
|
|
56
|
+
declare module '../localize.js' {
|
|
57
|
+
interface CmStrings {
|
|
58
|
+
commandPalette: typeof strings;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
/** Whether an action matches a query: every word of the query appears in the title or a keyword (any case, any accents). */
|
|
62
|
+
export declare function commandMatches(item: CommandPaletteItem, query: string): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* A search box in a dialog for people who work from the keyboard: jump to any record or run an action without
|
|
65
|
+
* leaving the keys. Opens with `show()`, the `open` attribute or an opt-in page-wide shortcut (`shortcut="mod+k"`:
|
|
66
|
+
* Cmd+K on a Mac, Ctrl+K elsewhere).
|
|
67
|
+
*
|
|
68
|
+
* Two kinds of content, in groups:
|
|
69
|
+
* - `actions`: things your app can do or places it can go ("New lead", "Go to Tasks"), set once. The palette
|
|
70
|
+
* filters them itself as the merchant types (every typed word must appear in the title or a keyword).
|
|
71
|
+
* - `results`: records your app finds, as in `cm-global-search`: listen for `cm-search`, fetch, then set
|
|
72
|
+
* `results` (and `loading` while it runs).
|
|
73
|
+
*
|
|
74
|
+
* While the box is empty it shows `recent` items, then every action.
|
|
75
|
+
* Choosing an item fires a cancelable `cm-select` and closes the palette; links are followed, and an item's
|
|
76
|
+
* `run` function is called, unless the event is cancelled.
|
|
77
|
+
*
|
|
78
|
+
* A native modal `<dialog>` like `cm-modal`: the page behind is inert, Escape and the backdrop close it, and focus
|
|
79
|
+
* goes back to where it was. Inside, the text box is a combobox over a grouped listbox, as in `cm-global-search`.
|
|
80
|
+
* @element cm-command-palette
|
|
81
|
+
* @fires cm-search - As the merchant types (and when the box is cleared), with `detail: { query }`. Fetch records and set `results`.
|
|
82
|
+
* @fires cm-select - When an item or a "See all" row is chosen, with `detail: { item, group, href }` (`item` is undefined for "See all"). The palette has already closed. Cancelable: `preventDefault()` stops the link and the item's `run`, to route in your app.
|
|
83
|
+
* @fires cm-open - After the palette opens.
|
|
84
|
+
* @fires cm-close - Before it closes from Escape, the backdrop, the close button or the shortcut. `detail: { reason }`. Cancelable.
|
|
85
|
+
* @fires cm-closed - After it has closed and focus has gone back.
|
|
86
|
+
* @csspart dialog - The native dialog.
|
|
87
|
+
* @csspart panel - The white box.
|
|
88
|
+
* @csspart box - The row with the search icon and the text input.
|
|
89
|
+
* @csspart input - The text input.
|
|
90
|
+
* @csspart close - The close button (narrow screens).
|
|
91
|
+
* @csspart results - The scrolling area under the box.
|
|
92
|
+
* @csspart listbox - The grouped list.
|
|
93
|
+
*/
|
|
94
|
+
export declare class CmCommandPalette extends CmOverlay {
|
|
95
|
+
#private;
|
|
96
|
+
static properties: {
|
|
97
|
+
label: {
|
|
98
|
+
string: string;
|
|
99
|
+
};
|
|
100
|
+
placeholder: {
|
|
101
|
+
string: string;
|
|
102
|
+
};
|
|
103
|
+
value: {};
|
|
104
|
+
results: {
|
|
105
|
+
type: ArrayConstructor;
|
|
106
|
+
};
|
|
107
|
+
actions: {
|
|
108
|
+
type: ArrayConstructor;
|
|
109
|
+
};
|
|
110
|
+
recent: {
|
|
111
|
+
type: ArrayConstructor;
|
|
112
|
+
};
|
|
113
|
+
loading: {
|
|
114
|
+
type: BooleanConstructor;
|
|
115
|
+
reflect: boolean;
|
|
116
|
+
};
|
|
117
|
+
shortcut: {};
|
|
118
|
+
emptyText: {
|
|
119
|
+
attribute: string;
|
|
120
|
+
string: string;
|
|
121
|
+
};
|
|
122
|
+
loadingText: {
|
|
123
|
+
attribute: string;
|
|
124
|
+
string: string;
|
|
125
|
+
};
|
|
126
|
+
recentLabel: {
|
|
127
|
+
attribute: string;
|
|
128
|
+
string: string;
|
|
129
|
+
};
|
|
130
|
+
seeAllLabel: {
|
|
131
|
+
attribute: string;
|
|
132
|
+
string: string;
|
|
133
|
+
};
|
|
134
|
+
_active: {
|
|
135
|
+
state: boolean;
|
|
136
|
+
};
|
|
137
|
+
open: {
|
|
138
|
+
type: BooleanConstructor;
|
|
139
|
+
reflect: boolean;
|
|
140
|
+
};
|
|
141
|
+
heading: {
|
|
142
|
+
string: string;
|
|
143
|
+
};
|
|
144
|
+
closeLabel: {
|
|
145
|
+
attribute: string;
|
|
146
|
+
string: string;
|
|
147
|
+
};
|
|
148
|
+
};
|
|
149
|
+
/** Accessible name of the dialog and its text box, read by screen readers but not shown. Default: the built-in string `label` ("Command palette"). */
|
|
150
|
+
label: string;
|
|
151
|
+
/** Text in the empty box: "Search leads, contacts or run a command". Default: the built-in string `placeholder` ("Search or run a command"). */
|
|
152
|
+
placeholder: string;
|
|
153
|
+
/** The query. Updated as the merchant types; emptied when the palette closes. */
|
|
154
|
+
value: string;
|
|
155
|
+
/** Records found for the current query, grouped by kind, as in `cm-global-search`. Shown only while there is a query. Groups with no items are skipped. */
|
|
156
|
+
results: CommandPaletteGroup[];
|
|
157
|
+
/** Actions and places, grouped ("Actions", "Navigate"). Always available: filtered by the palette as the merchant types. */
|
|
158
|
+
actions: CommandPaletteGroup[];
|
|
159
|
+
/** Items the merchant chose recently, shown under "Recent" while the box is empty. Your app keeps the list (e.g. add the chosen item in `cm-select`). */
|
|
160
|
+
recent: CommandPaletteItem[];
|
|
161
|
+
/** Shows "Loading" with a spinner in place of `results` while you fetch them. Matching actions still show. */
|
|
162
|
+
loading: boolean;
|
|
163
|
+
/**
|
|
164
|
+
* A shortcut that opens (and closes) the palette from anywhere on the page: `mod+k` is Cmd+K on a Mac and
|
|
165
|
+
* Ctrl+K elsewhere. Off by default: turn it on for one palette per page. A shortcut without Ctrl, Cmd or Alt
|
|
166
|
+
* (like `/`) is ignored while the merchant is typing in a field. The page only gets the key while it has focus
|
|
167
|
+
* (not from the address bar; in an embedded app, only while focus is in the app's frame), and shortcuts the
|
|
168
|
+
* browser reserves (Ctrl+T, Ctrl+W, Ctrl+N) can't be used: keep a visible button that calls `show()` too.
|
|
169
|
+
*/
|
|
170
|
+
shortcut: string;
|
|
171
|
+
/** Shown when nothing matches. Default: the built-in string `noResults` ("No results"). */
|
|
172
|
+
emptyText: string;
|
|
173
|
+
/** Shown and announced while `loading`. Default: the built-in string `loading` ("Loading", shared with other components). */
|
|
174
|
+
loadingText: string;
|
|
175
|
+
/** Heading of the `recent` items. Default: the built-in string `recent` ("Recent"). */
|
|
176
|
+
recentLabel: string;
|
|
177
|
+
/** Text of each results group's "See all" row. Default: the built-in string `seeAll` ("See all"). */
|
|
178
|
+
seeAllLabel: string;
|
|
179
|
+
_active: number;
|
|
180
|
+
constructor();
|
|
181
|
+
static styles: import("lit").CSSResult[];
|
|
182
|
+
/** Opens the palette if it is closed and closes it if it is open. */
|
|
183
|
+
toggle(): void;
|
|
184
|
+
connectedCallback(): void;
|
|
185
|
+
disconnectedCallback(): void;
|
|
186
|
+
willUpdate(changed: PropertyValues): void;
|
|
187
|
+
protected updated(changed: PropertyValues): void;
|
|
188
|
+
/** Focus goes to the text box, with any text in it selected. */
|
|
189
|
+
protected focusFirst(): void;
|
|
190
|
+
render(): import("lit-html").TemplateResult<1>;
|
|
191
|
+
}
|
|
192
|
+
declare global {
|
|
193
|
+
interface HTMLElementTagNameMap {
|
|
194
|
+
'cm-command-palette': CmCommandPalette;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
export {};
|