@vaadin/menu-bar 24.0.0-alpha1 → 24.0.0-alpha11
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/package.json +9 -9
- package/src/vaadin-menu-bar-button.js +1 -1
- package/src/{vaadin-menu-bar-interactions-mixin.d.ts → vaadin-menu-bar-mixin.d.ts} +16 -10
- package/src/{vaadin-menu-bar-interactions-mixin.js → vaadin-menu-bar-mixin.js} +325 -31
- package/src/vaadin-menu-bar-submenu.js +1 -1
- package/src/vaadin-menu-bar.d.ts +11 -4
- package/src/vaadin-menu-bar.js +18 -10
- package/theme/lumo/vaadin-menu-bar-item-styles.js +2 -4
- package/theme/material/vaadin-menu-bar-item-styles.js +1 -2
- package/web-types.json +31 -9
- package/web-types.lit.json +13 -6
- package/src/vaadin-menu-bar-buttons-mixin.d.ts +0 -22
- package/src/vaadin-menu-bar-buttons-mixin.js +0 -318
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vaadin/menu-bar",
|
|
3
|
-
"version": "24.0.0-
|
|
3
|
+
"version": "24.0.0-alpha11",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
@@ -37,16 +37,16 @@
|
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@open-wc/dedupe-mixin": "^1.3.0",
|
|
39
39
|
"@polymer/polymer": "^3.0.0",
|
|
40
|
-
"@vaadin/button": "24.0.0-
|
|
41
|
-
"@vaadin/component-base": "24.0.0-
|
|
42
|
-
"@vaadin/context-menu": "24.0.0-
|
|
43
|
-
"@vaadin/vaadin-lumo-styles": "24.0.0-
|
|
44
|
-
"@vaadin/vaadin-material-styles": "24.0.0-
|
|
45
|
-
"@vaadin/vaadin-themable-mixin": "24.0.0-
|
|
40
|
+
"@vaadin/button": "24.0.0-alpha11",
|
|
41
|
+
"@vaadin/component-base": "24.0.0-alpha11",
|
|
42
|
+
"@vaadin/context-menu": "24.0.0-alpha11",
|
|
43
|
+
"@vaadin/vaadin-lumo-styles": "24.0.0-alpha11",
|
|
44
|
+
"@vaadin/vaadin-material-styles": "24.0.0-alpha11",
|
|
45
|
+
"@vaadin/vaadin-themable-mixin": "24.0.0-alpha11"
|
|
46
46
|
},
|
|
47
47
|
"devDependencies": {
|
|
48
48
|
"@esm-bundle/chai": "^4.3.4",
|
|
49
|
-
"@vaadin/icon": "24.0.0-
|
|
49
|
+
"@vaadin/icon": "24.0.0-alpha11",
|
|
50
50
|
"@vaadin/testing-helpers": "^0.3.2",
|
|
51
51
|
"sinon": "^13.0.2"
|
|
52
52
|
},
|
|
@@ -54,5 +54,5 @@
|
|
|
54
54
|
"web-types.json",
|
|
55
55
|
"web-types.lit.json"
|
|
56
56
|
],
|
|
57
|
-
"gitHead": "
|
|
57
|
+
"gitHead": "641b3d96ceeb3e503a093682ebe686afdd8c3a68"
|
|
58
58
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
6
|
import { Button } from '@vaadin/button/src/vaadin-button.js';
|
|
@@ -1,31 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
6
|
import type { Constructor } from '@open-wc/dedupe-mixin';
|
|
7
|
+
import type { ControllerMixinClass } from '@vaadin/component-base/src/controller-mixin.js';
|
|
7
8
|
import type { FocusMixinClass } from '@vaadin/component-base/src/focus-mixin.js';
|
|
8
9
|
import type { KeyboardDirectionMixinClass } from '@vaadin/component-base/src/keyboard-direction-mixin.js';
|
|
9
10
|
import type { KeyboardMixinClass } from '@vaadin/component-base/src/keyboard-mixin.js';
|
|
11
|
+
import type { ResizeMixinClass } from '@vaadin/component-base/src/resize-mixin.js';
|
|
10
12
|
|
|
11
|
-
export declare function
|
|
13
|
+
export declare function MenuBarMixin<T extends Constructor<HTMLElement>>(
|
|
12
14
|
base: T,
|
|
13
|
-
): Constructor<
|
|
14
|
-
Constructor<
|
|
15
|
+
): Constructor<ControllerMixinClass> &
|
|
16
|
+
Constructor<FocusMixinClass> &
|
|
15
17
|
Constructor<KeyboardDirectionMixinClass> &
|
|
16
18
|
Constructor<KeyboardMixinClass> &
|
|
19
|
+
Constructor<MenuBarMixinClass> &
|
|
20
|
+
Constructor<ResizeMixinClass> &
|
|
17
21
|
T;
|
|
18
22
|
|
|
19
|
-
export declare class
|
|
23
|
+
export declare class MenuBarMixinClass {
|
|
20
24
|
/**
|
|
21
25
|
* If true, the submenu will open on hover (mouseover) instead of click.
|
|
22
26
|
* @attr {boolean} open-on-hover
|
|
23
27
|
*/
|
|
24
28
|
openOnHover: boolean | null | undefined;
|
|
25
29
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
protected readonly _buttons: HTMLElement[];
|
|
31
|
+
|
|
32
|
+
protected readonly _container: HTMLElement;
|
|
33
|
+
|
|
34
|
+
protected readonly _overflow: HTMLElement;
|
|
35
|
+
|
|
36
|
+
protected _hasOverflow: boolean;
|
|
31
37
|
}
|
|
@@ -1,19 +1,24 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
|
+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
|
|
6
7
|
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
|
|
7
8
|
import { isElementFocused, isKeyboardActive } from '@vaadin/component-base/src/focus-utils.js';
|
|
8
9
|
import { KeyboardDirectionMixin } from '@vaadin/component-base/src/keyboard-direction-mixin.js';
|
|
10
|
+
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
|
|
11
|
+
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* @polymerMixin
|
|
15
|
+
* @mixes ControllerMixin
|
|
12
16
|
* @mixes FocusMixin
|
|
13
|
-
* @mixes
|
|
17
|
+
* @mixes KeyboardDirectionMixin
|
|
18
|
+
* @mixes ResizeMixin
|
|
14
19
|
*/
|
|
15
|
-
export const
|
|
16
|
-
class
|
|
20
|
+
export const MenuBarMixin = (superClass) =>
|
|
21
|
+
class MenuBarMixinClass extends KeyboardDirectionMixin(ResizeMixin(FocusMixin(ControllerMixin(superClass)))) {
|
|
17
22
|
static get properties() {
|
|
18
23
|
return {
|
|
19
24
|
/**
|
|
@@ -23,34 +28,40 @@ export const InteractionsMixin = (superClass) =>
|
|
|
23
28
|
openOnHover: {
|
|
24
29
|
type: Boolean,
|
|
25
30
|
},
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
31
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
+
/**
|
|
33
|
+
* @type {boolean}
|
|
34
|
+
* @protected
|
|
35
|
+
*/
|
|
36
|
+
_hasOverflow: {
|
|
37
|
+
type: Boolean,
|
|
38
|
+
value: false,
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
/** @protected */
|
|
42
|
+
_overflow: {
|
|
43
|
+
type: Object,
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
/** @protected */
|
|
47
|
+
_container: {
|
|
48
|
+
type: Object,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
32
51
|
}
|
|
33
52
|
|
|
34
53
|
static get observers() {
|
|
35
|
-
return [
|
|
54
|
+
return [
|
|
55
|
+
'_itemsChanged(items, items.splices)',
|
|
56
|
+
'__hasOverflowChanged(_hasOverflow, _overflow)',
|
|
57
|
+
'__i18nChanged(i18n, _overflow)',
|
|
58
|
+
'_menuItemsChanged(items, _overflow, _container, items.splices)',
|
|
59
|
+
];
|
|
36
60
|
}
|
|
37
61
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.addEventListener('mousedown', () => this._hideTooltip());
|
|
43
|
-
this.addEventListener('mouseleave', () => this._hideTooltip());
|
|
44
|
-
|
|
45
|
-
this._subMenu.addEventListener('item-selected', this.__onItemSelected.bind(this));
|
|
46
|
-
this._subMenu.addEventListener('close-all-menus', this.__onEscapeClose.bind(this));
|
|
47
|
-
|
|
48
|
-
const overlay = this._subMenu.$.overlay;
|
|
49
|
-
overlay.addEventListener('keydown', this.__boundOnContextMenuKeydown);
|
|
50
|
-
|
|
51
|
-
const container = this._container;
|
|
52
|
-
container.addEventListener('click', this.__onButtonClick.bind(this));
|
|
53
|
-
container.addEventListener('mouseover', (e) => this._onMouseOver(e));
|
|
62
|
+
constructor() {
|
|
63
|
+
super();
|
|
64
|
+
this.__boundOnContextMenuKeydown = this.__onContextMenuKeydown.bind(this);
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
/**
|
|
@@ -77,6 +88,66 @@ export const InteractionsMixin = (superClass) =>
|
|
|
77
88
|
return false;
|
|
78
89
|
}
|
|
79
90
|
|
|
91
|
+
/**
|
|
92
|
+
* Override getter from `ResizeMixin` to observe parent.
|
|
93
|
+
*
|
|
94
|
+
* @protected
|
|
95
|
+
* @override
|
|
96
|
+
*/
|
|
97
|
+
get _observeParent() {
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* @return {!Array<!HTMLElement>}
|
|
103
|
+
* @protected
|
|
104
|
+
*/
|
|
105
|
+
get _buttons() {
|
|
106
|
+
return Array.from(this.querySelectorAll('vaadin-menu-bar-button'));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** @private */
|
|
110
|
+
get _subMenu() {
|
|
111
|
+
return this.shadowRoot.querySelector('vaadin-menu-bar-submenu');
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @protected */
|
|
115
|
+
ready() {
|
|
116
|
+
super.ready();
|
|
117
|
+
|
|
118
|
+
this.setAttribute('role', 'menubar');
|
|
119
|
+
|
|
120
|
+
this._overflowController = new SlotController(this, 'overflow', 'vaadin-menu-bar-button', {
|
|
121
|
+
initializer: (btn) => {
|
|
122
|
+
btn.setAttribute('hidden', '');
|
|
123
|
+
|
|
124
|
+
const dots = document.createElement('div');
|
|
125
|
+
dots.setAttribute('aria-hidden', 'true');
|
|
126
|
+
dots.textContent = '···';
|
|
127
|
+
btn.appendChild(dots);
|
|
128
|
+
|
|
129
|
+
this._overflow = btn;
|
|
130
|
+
this._initButtonAttrs(btn);
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
this.addController(this._overflowController);
|
|
134
|
+
|
|
135
|
+
this.addEventListener('mousedown', () => this._hideTooltip());
|
|
136
|
+
this.addEventListener('mouseleave', () => this._hideTooltip());
|
|
137
|
+
|
|
138
|
+
this._subMenu.addEventListener('item-selected', this.__onItemSelected.bind(this));
|
|
139
|
+
this._subMenu.addEventListener('close-all-menus', this.__onEscapeClose.bind(this));
|
|
140
|
+
|
|
141
|
+
const overlay = this._subMenu.$.overlay;
|
|
142
|
+
overlay.addEventListener('keydown', this.__boundOnContextMenuKeydown);
|
|
143
|
+
|
|
144
|
+
const container = this.shadowRoot.querySelector('[part="container"]');
|
|
145
|
+
container.addEventListener('click', this.__onButtonClick.bind(this));
|
|
146
|
+
container.addEventListener('mouseover', (e) => this._onMouseOver(e));
|
|
147
|
+
|
|
148
|
+
this._container = container;
|
|
149
|
+
}
|
|
150
|
+
|
|
80
151
|
/**
|
|
81
152
|
* Override method inherited from `KeyboardDirectionMixin`
|
|
82
153
|
* to use the list of menu-bar buttons as items.
|
|
@@ -95,6 +166,234 @@ export const InteractionsMixin = (superClass) =>
|
|
|
95
166
|
this._hideTooltip(true);
|
|
96
167
|
}
|
|
97
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Implement callback from `ResizeMixin` to update buttons
|
|
171
|
+
* and detect whether to show or hide the overflow button.
|
|
172
|
+
*
|
|
173
|
+
* @protected
|
|
174
|
+
* @override
|
|
175
|
+
*/
|
|
176
|
+
_onResize() {
|
|
177
|
+
this.__detectOverflow();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** @private */
|
|
181
|
+
__hasOverflowChanged(hasOverflow, overflow) {
|
|
182
|
+
if (overflow) {
|
|
183
|
+
overflow.toggleAttribute('hidden', !hasOverflow);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** @private */
|
|
188
|
+
_menuItemsChanged(items, overflow, container) {
|
|
189
|
+
if (!overflow || !container) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (items !== this._oldItems) {
|
|
194
|
+
this._oldItems = items;
|
|
195
|
+
this.__renderButtons(items);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/** @private */
|
|
200
|
+
__i18nChanged(i18n, overflow) {
|
|
201
|
+
if (overflow && i18n && i18n.moreOptions !== undefined) {
|
|
202
|
+
if (i18n.moreOptions) {
|
|
203
|
+
overflow.setAttribute('aria-label', i18n.moreOptions);
|
|
204
|
+
} else {
|
|
205
|
+
overflow.removeAttribute('aria-label');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** @private */
|
|
211
|
+
__getOverflowCount(overflow) {
|
|
212
|
+
// We can't use optional chaining due to webpack 4
|
|
213
|
+
return (overflow.item && overflow.item.children && overflow.item.children.length) || 0;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** @private */
|
|
217
|
+
__restoreButtons(buttons) {
|
|
218
|
+
for (let i = 0; i < buttons.length; i++) {
|
|
219
|
+
const btn = buttons[i];
|
|
220
|
+
btn.disabled = (btn.item && btn.item.disabled) || this.disabled;
|
|
221
|
+
btn.style.visibility = '';
|
|
222
|
+
btn.style.position = '';
|
|
223
|
+
|
|
224
|
+
// Teleport item component back from "overflow" sub-menu
|
|
225
|
+
const item = btn.item && btn.item.component;
|
|
226
|
+
if (item instanceof HTMLElement && item.getAttribute('role') === 'menuitem') {
|
|
227
|
+
btn.appendChild(item);
|
|
228
|
+
item.removeAttribute('role');
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
this.__updateOverflow([]);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** @private */
|
|
235
|
+
__updateOverflow(items) {
|
|
236
|
+
this._overflow.item = { children: items };
|
|
237
|
+
this._hasOverflow = items.length > 0;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** @private */
|
|
241
|
+
__setOverflowItems(buttons, overflow) {
|
|
242
|
+
const container = this._container;
|
|
243
|
+
|
|
244
|
+
if (container.offsetWidth < container.scrollWidth) {
|
|
245
|
+
this._hasOverflow = true;
|
|
246
|
+
|
|
247
|
+
const isRTL = this.__isRTL;
|
|
248
|
+
|
|
249
|
+
let i;
|
|
250
|
+
for (i = buttons.length; i > 0; i--) {
|
|
251
|
+
const btn = buttons[i - 1];
|
|
252
|
+
const btnStyle = getComputedStyle(btn);
|
|
253
|
+
|
|
254
|
+
// If this button isn't overflowing, then the rest aren't either
|
|
255
|
+
if (
|
|
256
|
+
(!isRTL && btn.offsetLeft + btn.offsetWidth < container.offsetWidth - overflow.offsetWidth) ||
|
|
257
|
+
(isRTL && btn.offsetLeft >= overflow.offsetWidth)
|
|
258
|
+
) {
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
btn.disabled = true;
|
|
263
|
+
btn.style.visibility = 'hidden';
|
|
264
|
+
btn.style.position = 'absolute';
|
|
265
|
+
// Save width for buttons with component
|
|
266
|
+
btn.style.width = btnStyle.width;
|
|
267
|
+
}
|
|
268
|
+
const items = buttons.filter((_, idx) => idx >= i).map((b) => b.item);
|
|
269
|
+
this.__updateOverflow(items);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/** @private */
|
|
274
|
+
__detectOverflow() {
|
|
275
|
+
const overflow = this._overflow;
|
|
276
|
+
const buttons = this._buttons.filter((btn) => btn !== overflow);
|
|
277
|
+
const oldOverflowCount = this.__getOverflowCount(overflow);
|
|
278
|
+
|
|
279
|
+
// Reset all buttons in the menu bar and the overflow button
|
|
280
|
+
this.__restoreButtons(buttons);
|
|
281
|
+
|
|
282
|
+
// Hide any overflowing buttons and put them in the 'overflow' button
|
|
283
|
+
this.__setOverflowItems(buttons, overflow);
|
|
284
|
+
|
|
285
|
+
const newOverflowCount = this.__getOverflowCount(overflow);
|
|
286
|
+
if (oldOverflowCount !== newOverflowCount && this._subMenu.opened) {
|
|
287
|
+
this._subMenu.close();
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const isSingleButton = newOverflowCount === buttons.length || (newOverflowCount === 0 && buttons.length === 1);
|
|
291
|
+
this.toggleAttribute('has-single-button', isSingleButton);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/** @protected */
|
|
295
|
+
_removeButtons() {
|
|
296
|
+
this._buttons.forEach((button) => {
|
|
297
|
+
if (button !== this._overflow) {
|
|
298
|
+
this.removeChild(button);
|
|
299
|
+
}
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** @protected */
|
|
304
|
+
_initButton(item) {
|
|
305
|
+
const button = document.createElement('vaadin-menu-bar-button');
|
|
306
|
+
|
|
307
|
+
const itemCopy = { ...item };
|
|
308
|
+
button.item = itemCopy;
|
|
309
|
+
|
|
310
|
+
if (item.component) {
|
|
311
|
+
const component = this.__getComponent(itemCopy);
|
|
312
|
+
itemCopy.component = component;
|
|
313
|
+
// Save item for overflow menu
|
|
314
|
+
component.item = itemCopy;
|
|
315
|
+
button.appendChild(component);
|
|
316
|
+
} else if (item.text) {
|
|
317
|
+
button.textContent = item.text;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
return button;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** @protected */
|
|
324
|
+
_initButtonAttrs(button) {
|
|
325
|
+
button.setAttribute('role', 'menuitem');
|
|
326
|
+
|
|
327
|
+
if (button === this._overflow || (button.item && button.item.children)) {
|
|
328
|
+
button.setAttribute('aria-haspopup', 'true');
|
|
329
|
+
button.setAttribute('aria-expanded', 'false');
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** @protected */
|
|
334
|
+
_setButtonDisabled(button, disabled) {
|
|
335
|
+
button.disabled = disabled;
|
|
336
|
+
button.setAttribute('tabindex', disabled ? '-1' : '0');
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** @protected */
|
|
340
|
+
_setButtonTheme(btn, hostTheme) {
|
|
341
|
+
let theme = hostTheme;
|
|
342
|
+
|
|
343
|
+
// Item theme takes precedence over host theme even if it's empty, as long as it's not undefined or null
|
|
344
|
+
const itemTheme = btn.item && btn.item.theme;
|
|
345
|
+
if (itemTheme != null) {
|
|
346
|
+
theme = Array.isArray(itemTheme) ? itemTheme.join(' ') : itemTheme;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
if (theme) {
|
|
350
|
+
btn.setAttribute('theme', theme);
|
|
351
|
+
} else {
|
|
352
|
+
btn.removeAttribute('theme');
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** @private */
|
|
357
|
+
__getComponent(item) {
|
|
358
|
+
const itemComponent = item.component;
|
|
359
|
+
let component;
|
|
360
|
+
|
|
361
|
+
const isElement = itemComponent instanceof HTMLElement;
|
|
362
|
+
// Use existing item component, if any
|
|
363
|
+
if (isElement && itemComponent.localName === 'vaadin-context-menu-item') {
|
|
364
|
+
component = itemComponent;
|
|
365
|
+
} else {
|
|
366
|
+
component = document.createElement('vaadin-context-menu-item');
|
|
367
|
+
component.appendChild(isElement ? itemComponent : document.createElement(itemComponent));
|
|
368
|
+
}
|
|
369
|
+
if (item.text) {
|
|
370
|
+
const node = component.firstChild || component;
|
|
371
|
+
node.textContent = item.text;
|
|
372
|
+
}
|
|
373
|
+
component.setAttribute('theme', 'menu-bar-item');
|
|
374
|
+
return component;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** @private */
|
|
378
|
+
__renderButtons(items = []) {
|
|
379
|
+
this._removeButtons();
|
|
380
|
+
|
|
381
|
+
/* Empty array, do nothing */
|
|
382
|
+
if (items.length === 0) {
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
items.forEach((item) => {
|
|
387
|
+
const button = this._initButton(item);
|
|
388
|
+
this.insertBefore(button, this._overflow);
|
|
389
|
+
this._setButtonDisabled(button, item.disabled);
|
|
390
|
+
this._initButtonAttrs(button);
|
|
391
|
+
this._setButtonTheme(button, this._theme);
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
this.__detectOverflow();
|
|
395
|
+
}
|
|
396
|
+
|
|
98
397
|
/**
|
|
99
398
|
* @param {HTMLElement} button
|
|
100
399
|
* @protected
|
|
@@ -273,11 +572,6 @@ export const InteractionsMixin = (superClass) =>
|
|
|
273
572
|
}
|
|
274
573
|
}
|
|
275
574
|
|
|
276
|
-
/** @private */
|
|
277
|
-
get _subMenu() {
|
|
278
|
-
return this.shadowRoot.querySelector('vaadin-menu-bar-submenu');
|
|
279
|
-
}
|
|
280
|
-
|
|
281
575
|
/** @private */
|
|
282
576
|
_itemsChanged() {
|
|
283
577
|
const subMenu = this._subMenu;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
6
|
import { ContextMenu } from '@vaadin/context-menu/src/vaadin-context-menu.js';
|
package/src/vaadin-menu-bar.d.ts
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
6
|
import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
|
|
7
7
|
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
|
|
8
8
|
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
|
|
9
|
-
import {
|
|
10
|
-
import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
|
|
9
|
+
import { MenuBarMixin } from './vaadin-menu-bar-mixin.js';
|
|
11
10
|
|
|
12
11
|
export interface MenuBarItem {
|
|
13
12
|
/**
|
|
@@ -108,7 +107,7 @@ export interface MenuBarEventMap extends HTMLElementEventMap, MenuBarCustomEvent
|
|
|
108
107
|
*
|
|
109
108
|
* @fires {CustomEvent} item-selected - Fired when a submenu item or menu bar button without children is clicked.
|
|
110
109
|
*/
|
|
111
|
-
declare class MenuBar extends
|
|
110
|
+
declare class MenuBar extends MenuBarMixin(DisabledMixin(ElementMixin(ThemableMixin(HTMLElement)))) {
|
|
112
111
|
/**
|
|
113
112
|
* Defines a hierarchical structure, where root level items represent menu bar buttons,
|
|
114
113
|
* and `children` property configures a submenu with items to be opened below
|
|
@@ -161,6 +160,14 @@ declare class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(Eleme
|
|
|
161
160
|
*/
|
|
162
161
|
i18n: MenuBarI18n;
|
|
163
162
|
|
|
163
|
+
/**
|
|
164
|
+
* A space-delimited list of CSS class names
|
|
165
|
+
* to set on each sub-menu overlay element.
|
|
166
|
+
*
|
|
167
|
+
* @attr {string} overlay-class
|
|
168
|
+
*/
|
|
169
|
+
overlayClass: string;
|
|
170
|
+
|
|
164
171
|
addEventListener<K extends keyof MenuBarEventMap>(
|
|
165
172
|
type: K,
|
|
166
173
|
listener: (this: MenuBar, ev: MenuBarEventMap[K]) => void,
|
package/src/vaadin-menu-bar.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @license
|
|
3
|
-
* Copyright (c) 2019 -
|
|
3
|
+
* Copyright (c) 2019 - 2023 Vaadin Ltd.
|
|
4
4
|
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
5
|
*/
|
|
6
6
|
import './vaadin-menu-bar-submenu.js';
|
|
@@ -10,8 +10,7 @@ import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
|
|
|
10
10
|
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
|
|
11
11
|
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
|
|
12
12
|
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
|
|
13
|
-
import {
|
|
14
|
-
import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
|
|
13
|
+
import { MenuBarMixin } from './vaadin-menu-bar-mixin.js';
|
|
15
14
|
|
|
16
15
|
/**
|
|
17
16
|
* `<vaadin-menu-bar>` is a Web Component providing a set of horizontally stacked buttons offering
|
|
@@ -60,13 +59,12 @@ import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
|
|
|
60
59
|
* @fires {CustomEvent<boolean>} item-selected - Fired when a submenu item or menu bar button without children is clicked.
|
|
61
60
|
*
|
|
62
61
|
* @extends HTMLElement
|
|
63
|
-
* @mixes ButtonsMixin
|
|
64
|
-
* @mixes InteractionsMixin
|
|
65
62
|
* @mixes DisabledMixin
|
|
66
63
|
* @mixes ElementMixin
|
|
64
|
+
* @mixes MenuBarMixin
|
|
67
65
|
* @mixes ThemableMixin
|
|
68
66
|
*/
|
|
69
|
-
class MenuBar extends
|
|
67
|
+
class MenuBar extends MenuBarMixin(DisabledMixin(ElementMixin(ThemableMixin(PolymerElement)))) {
|
|
70
68
|
static get template() {
|
|
71
69
|
return html`
|
|
72
70
|
<style>
|
|
@@ -91,7 +89,7 @@ class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(ElementMixin(
|
|
|
91
89
|
<slot></slot>
|
|
92
90
|
<slot name="overflow"></slot>
|
|
93
91
|
</div>
|
|
94
|
-
<vaadin-menu-bar-submenu is-root=""></vaadin-menu-bar-submenu>
|
|
92
|
+
<vaadin-menu-bar-submenu is-root overlay-class="[[overlayClass]]"></vaadin-menu-bar-submenu>
|
|
95
93
|
|
|
96
94
|
<slot name="tooltip"></slot>
|
|
97
95
|
`;
|
|
@@ -193,11 +191,21 @@ class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(ElementMixin(
|
|
|
193
191
|
};
|
|
194
192
|
},
|
|
195
193
|
},
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* A space-delimited list of CSS class names
|
|
197
|
+
* to set on each sub-menu overlay element.
|
|
198
|
+
*
|
|
199
|
+
* @attr {string} overlay-class
|
|
200
|
+
*/
|
|
201
|
+
overlayClass: {
|
|
202
|
+
type: String,
|
|
203
|
+
},
|
|
196
204
|
};
|
|
197
205
|
}
|
|
198
206
|
|
|
199
207
|
static get observers() {
|
|
200
|
-
return ['_themeChanged(_theme, _overflow)'];
|
|
208
|
+
return ['_themeChanged(_theme, _overflow, _container)'];
|
|
201
209
|
}
|
|
202
210
|
|
|
203
211
|
/** @protected */
|
|
@@ -233,8 +241,8 @@ class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(ElementMixin(
|
|
|
233
241
|
* @param {string | null} theme
|
|
234
242
|
* @protected
|
|
235
243
|
*/
|
|
236
|
-
_themeChanged(theme, overflow) {
|
|
237
|
-
if (overflow) {
|
|
244
|
+
_themeChanged(theme, overflow, container) {
|
|
245
|
+
if (overflow && container) {
|
|
238
246
|
this._buttons.forEach((btn) => this._setButtonTheme(btn, theme));
|
|
239
247
|
this.__detectOverflow();
|
|
240
248
|
}
|
|
@@ -12,15 +12,13 @@ registerStyles(
|
|
|
12
12
|
justify-content: inherit;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon)
|
|
16
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(iron-icon) {
|
|
15
|
+
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon) {
|
|
17
16
|
display: inline-block;
|
|
18
17
|
width: var(--lumo-icon-size-m);
|
|
19
18
|
height: var(--lumo-icon-size-m);
|
|
20
19
|
}
|
|
21
20
|
|
|
22
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon[icon^='vaadin:'])
|
|
23
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(iron-icon[icon^='vaadin:']) {
|
|
21
|
+
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon[icon^='vaadin:']) {
|
|
24
22
|
padding: var(--lumo-space-xs);
|
|
25
23
|
box-sizing: border-box !important;
|
|
26
24
|
}
|
|
@@ -12,8 +12,7 @@ registerStyles(
|
|
|
12
12
|
font-size: var(--material-button-font-size);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon[icon^='vaadin:'])
|
|
16
|
-
:host([theme='menu-bar-item']) [part='content'] ::slotted(iron-icon[icon^='vaadin:']) {
|
|
15
|
+
:host([theme='menu-bar-item']) [part='content'] ::slotted(vaadin-icon[icon^='vaadin:']) {
|
|
17
16
|
display: inline-block;
|
|
18
17
|
width: 18px;
|
|
19
18
|
height: 18px;
|
package/web-types.json
CHANGED
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/web-types",
|
|
3
3
|
"name": "@vaadin/menu-bar",
|
|
4
|
-
"version": "24.0.0-
|
|
4
|
+
"version": "24.0.0-alpha11",
|
|
5
5
|
"description-markup": "markdown",
|
|
6
6
|
"contributions": {
|
|
7
7
|
"html": {
|
|
8
8
|
"elements": [
|
|
9
9
|
{
|
|
10
10
|
"name": "vaadin-menu-bar",
|
|
11
|
-
"description": "`<vaadin-menu-bar>` is a Web Component providing a set of horizontally stacked buttons offering\nthe user quick access to a consistent set of commands. Each button can toggle a submenu with\nsupport for additional levels of nested menus.\n\nTo create the menu bar, first add the component to the page:\n\n```\n<vaadin-menu-bar></vaadin-menu-bar>\n```\n\nAnd then use [`items`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-
|
|
11
|
+
"description": "`<vaadin-menu-bar>` is a Web Component providing a set of horizontally stacked buttons offering\nthe user quick access to a consistent set of commands. Each button can toggle a submenu with\nsupport for additional levels of nested menus.\n\nTo create the menu bar, first add the component to the page:\n\n```\n<vaadin-menu-bar></vaadin-menu-bar>\n```\n\nAnd then use [`items`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-menu-bar#property-items) property to initialize the structure:\n\n```\ndocument.querySelector('vaadin-menu-bar').items = [{text: 'File'}, {text: 'Edit'}];\n```\n\n### Styling\n\nThe following shadow DOM parts are exposed for styling:\n\nPart name | Description\n------------------|----------------\n`container` | The container wrapping menu bar buttons.\n\nThe following state attributes are available for styling:\n\nAttribute | Description\n--------------------|----------------------------------\n`disabled` | Set when the menu bar is disabled\n`has-single-button` | Set when there is only one button visible\n\nSee [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.\n\n### Internal components\n\nIn addition to `<vaadin-menu-bar>` itself, the following internal\ncomponents are themable:\n\n- `<vaadin-menu-bar-button>` - has the same API as [`<vaadin-button>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-button).\n- `<vaadin-context-menu-item>` - has the same API as [`<vaadin-item>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-item).\n- `<vaadin-context-menu-list-box>` - has the same API as [`<vaadin-list-box>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-list-box).\n- `<vaadin-context-menu-overlay>` - has the same API as [`<vaadin-overlay>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-overlay).",
|
|
12
12
|
"attributes": [
|
|
13
|
+
{
|
|
14
|
+
"name": "disabled",
|
|
15
|
+
"description": "If true, the user cannot interact with this element.",
|
|
16
|
+
"value": {
|
|
17
|
+
"type": [
|
|
18
|
+
"boolean",
|
|
19
|
+
"null",
|
|
20
|
+
"undefined"
|
|
21
|
+
]
|
|
22
|
+
}
|
|
23
|
+
},
|
|
13
24
|
{
|
|
14
25
|
"name": "open-on-hover",
|
|
15
26
|
"description": "If true, the submenu will open on hover (mouseover) instead of click.",
|
|
@@ -22,11 +33,11 @@
|
|
|
22
33
|
}
|
|
23
34
|
},
|
|
24
35
|
{
|
|
25
|
-
"name": "
|
|
26
|
-
"description": "
|
|
36
|
+
"name": "overlay-class",
|
|
37
|
+
"description": "A space-delimited list of CSS class names\nto set on each sub-menu overlay element.",
|
|
27
38
|
"value": {
|
|
28
39
|
"type": [
|
|
29
|
-
"
|
|
40
|
+
"string",
|
|
30
41
|
"null",
|
|
31
42
|
"undefined"
|
|
32
43
|
]
|
|
@@ -47,8 +58,8 @@
|
|
|
47
58
|
"js": {
|
|
48
59
|
"properties": [
|
|
49
60
|
{
|
|
50
|
-
"name": "
|
|
51
|
-
"description": "If true, the
|
|
61
|
+
"name": "disabled",
|
|
62
|
+
"description": "If true, the user cannot interact with this element.",
|
|
52
63
|
"value": {
|
|
53
64
|
"type": [
|
|
54
65
|
"boolean",
|
|
@@ -58,8 +69,8 @@
|
|
|
58
69
|
}
|
|
59
70
|
},
|
|
60
71
|
{
|
|
61
|
-
"name": "
|
|
62
|
-
"description": "If true, the
|
|
72
|
+
"name": "openOnHover",
|
|
73
|
+
"description": "If true, the submenu will open on hover (mouseover) instead of click.",
|
|
63
74
|
"value": {
|
|
64
75
|
"type": [
|
|
65
76
|
"boolean",
|
|
@@ -85,6 +96,17 @@
|
|
|
85
96
|
"MenuBarI18n"
|
|
86
97
|
]
|
|
87
98
|
}
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"name": "overlayClass",
|
|
102
|
+
"description": "A space-delimited list of CSS class names\nto set on each sub-menu overlay element.",
|
|
103
|
+
"value": {
|
|
104
|
+
"type": [
|
|
105
|
+
"string",
|
|
106
|
+
"null",
|
|
107
|
+
"undefined"
|
|
108
|
+
]
|
|
109
|
+
}
|
|
88
110
|
}
|
|
89
111
|
],
|
|
90
112
|
"events": [
|
package/web-types.lit.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://json.schemastore.org/web-types",
|
|
3
3
|
"name": "@vaadin/menu-bar",
|
|
4
|
-
"version": "24.0.0-
|
|
4
|
+
"version": "24.0.0-alpha11",
|
|
5
5
|
"description-markup": "markdown",
|
|
6
6
|
"framework": "lit",
|
|
7
7
|
"framework-config": {
|
|
@@ -16,19 +16,19 @@
|
|
|
16
16
|
"elements": [
|
|
17
17
|
{
|
|
18
18
|
"name": "vaadin-menu-bar",
|
|
19
|
-
"description": "`<vaadin-menu-bar>` is a Web Component providing a set of horizontally stacked buttons offering\nthe user quick access to a consistent set of commands. Each button can toggle a submenu with\nsupport for additional levels of nested menus.\n\nTo create the menu bar, first add the component to the page:\n\n```\n<vaadin-menu-bar></vaadin-menu-bar>\n```\n\nAnd then use [`items`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-
|
|
19
|
+
"description": "`<vaadin-menu-bar>` is a Web Component providing a set of horizontally stacked buttons offering\nthe user quick access to a consistent set of commands. Each button can toggle a submenu with\nsupport for additional levels of nested menus.\n\nTo create the menu bar, first add the component to the page:\n\n```\n<vaadin-menu-bar></vaadin-menu-bar>\n```\n\nAnd then use [`items`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-menu-bar#property-items) property to initialize the structure:\n\n```\ndocument.querySelector('vaadin-menu-bar').items = [{text: 'File'}, {text: 'Edit'}];\n```\n\n### Styling\n\nThe following shadow DOM parts are exposed for styling:\n\nPart name | Description\n------------------|----------------\n`container` | The container wrapping menu bar buttons.\n\nThe following state attributes are available for styling:\n\nAttribute | Description\n--------------------|----------------------------------\n`disabled` | Set when the menu bar is disabled\n`has-single-button` | Set when there is only one button visible\n\nSee [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.\n\n### Internal components\n\nIn addition to `<vaadin-menu-bar>` itself, the following internal\ncomponents are themable:\n\n- `<vaadin-menu-bar-button>` - has the same API as [`<vaadin-button>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-button).\n- `<vaadin-context-menu-item>` - has the same API as [`<vaadin-item>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-item).\n- `<vaadin-context-menu-list-box>` - has the same API as [`<vaadin-list-box>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-list-box).\n- `<vaadin-context-menu-overlay>` - has the same API as [`<vaadin-overlay>`](https://cdn.vaadin.com/vaadin-web-components/24.0.0-alpha11/#/elements/vaadin-overlay).",
|
|
20
20
|
"extension": true,
|
|
21
21
|
"attributes": [
|
|
22
22
|
{
|
|
23
|
-
"name": "?
|
|
24
|
-
"description": "If true, the
|
|
23
|
+
"name": "?disabled",
|
|
24
|
+
"description": "If true, the user cannot interact with this element.",
|
|
25
25
|
"value": {
|
|
26
26
|
"kind": "expression"
|
|
27
27
|
}
|
|
28
28
|
},
|
|
29
29
|
{
|
|
30
|
-
"name": "?
|
|
31
|
-
"description": "If true, the
|
|
30
|
+
"name": "?openOnHover",
|
|
31
|
+
"description": "If true, the submenu will open on hover (mouseover) instead of click.",
|
|
32
32
|
"value": {
|
|
33
33
|
"kind": "expression"
|
|
34
34
|
}
|
|
@@ -47,6 +47,13 @@
|
|
|
47
47
|
"kind": "expression"
|
|
48
48
|
}
|
|
49
49
|
},
|
|
50
|
+
{
|
|
51
|
+
"name": ".overlayClass",
|
|
52
|
+
"description": "A space-delimited list of CSS class names\nto set on each sub-menu overlay element.",
|
|
53
|
+
"value": {
|
|
54
|
+
"kind": "expression"
|
|
55
|
+
}
|
|
56
|
+
},
|
|
50
57
|
{
|
|
51
58
|
"name": "@item-selected",
|
|
52
59
|
"description": "Fired when either a submenu item or menu bar button without nested children is clicked.",
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright (c) 2019 - 2022 Vaadin Ltd.
|
|
4
|
-
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
-
*/
|
|
6
|
-
import type { Constructor } from '@open-wc/dedupe-mixin';
|
|
7
|
-
import type { ControllerMixinClass } from '@vaadin/component-base/src/controller-mixin.js';
|
|
8
|
-
import type { ResizeMixinClass } from '@vaadin/component-base/src/resize-mixin.js';
|
|
9
|
-
|
|
10
|
-
export declare function ButtonsMixin<T extends Constructor<HTMLElement>>(
|
|
11
|
-
base: T,
|
|
12
|
-
): Constructor<ButtonsMixinClass> & Constructor<ControllerMixinClass> & Constructor<ResizeMixinClass> & T;
|
|
13
|
-
|
|
14
|
-
export declare class ButtonsMixinClass {
|
|
15
|
-
protected readonly _buttons: HTMLElement[];
|
|
16
|
-
|
|
17
|
-
protected readonly _container: HTMLElement;
|
|
18
|
-
|
|
19
|
-
protected readonly _overflow: HTMLElement;
|
|
20
|
-
|
|
21
|
-
protected _hasOverflow: boolean;
|
|
22
|
-
}
|
|
@@ -1,318 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license
|
|
3
|
-
* Copyright (c) 2019 - 2022 Vaadin Ltd.
|
|
4
|
-
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
|
|
5
|
-
*/
|
|
6
|
-
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
|
|
7
|
-
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
|
|
8
|
-
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
|
|
9
|
-
|
|
10
|
-
/**
|
|
11
|
-
* @polymerMixin
|
|
12
|
-
* @mixes ResizeMixin
|
|
13
|
-
* @mixes ControllerMixin
|
|
14
|
-
*/
|
|
15
|
-
export const ButtonsMixin = (superClass) =>
|
|
16
|
-
class extends ResizeMixin(ControllerMixin(superClass)) {
|
|
17
|
-
static get properties() {
|
|
18
|
-
return {
|
|
19
|
-
/**
|
|
20
|
-
* @type {boolean}
|
|
21
|
-
* @protected
|
|
22
|
-
*/
|
|
23
|
-
_hasOverflow: {
|
|
24
|
-
type: Boolean,
|
|
25
|
-
value: false,
|
|
26
|
-
},
|
|
27
|
-
|
|
28
|
-
/** @protected */
|
|
29
|
-
_overflow: {
|
|
30
|
-
type: Object,
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
static get observers() {
|
|
36
|
-
return [
|
|
37
|
-
'__hasOverflowChanged(_hasOverflow, _overflow)',
|
|
38
|
-
'__i18nChanged(i18n, _overflow)',
|
|
39
|
-
'_menuItemsChanged(items, _overflow, items.splices)',
|
|
40
|
-
];
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Override getter from `ResizeMixin` to observe parent.
|
|
45
|
-
*
|
|
46
|
-
* @protected
|
|
47
|
-
* @override
|
|
48
|
-
*/
|
|
49
|
-
get _observeParent() {
|
|
50
|
-
return true;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/** @protected */
|
|
54
|
-
ready() {
|
|
55
|
-
super.ready();
|
|
56
|
-
|
|
57
|
-
this.setAttribute('role', 'menubar');
|
|
58
|
-
|
|
59
|
-
this._overflowController = new SlotController(
|
|
60
|
-
this,
|
|
61
|
-
'overflow',
|
|
62
|
-
() => document.createElement('vaadin-menu-bar-button'),
|
|
63
|
-
(_, btn) => {
|
|
64
|
-
btn.setAttribute('hidden', '');
|
|
65
|
-
|
|
66
|
-
const dots = document.createElement('div');
|
|
67
|
-
dots.setAttribute('aria-hidden', 'true');
|
|
68
|
-
dots.textContent = '···';
|
|
69
|
-
btn.appendChild(dots);
|
|
70
|
-
|
|
71
|
-
this._overflow = btn;
|
|
72
|
-
this._initButtonAttrs(btn);
|
|
73
|
-
},
|
|
74
|
-
);
|
|
75
|
-
this.addController(this._overflowController);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
/**
|
|
79
|
-
* @return {!Array<!HTMLElement>}
|
|
80
|
-
* @protected
|
|
81
|
-
*/
|
|
82
|
-
get _buttons() {
|
|
83
|
-
return Array.from(this.querySelectorAll('vaadin-menu-bar-button'));
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* @return {!HTMLElement}
|
|
88
|
-
* @protected
|
|
89
|
-
*/
|
|
90
|
-
get _container() {
|
|
91
|
-
return this.shadowRoot.querySelector('[part="container"]');
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/** @private */
|
|
95
|
-
__hasOverflowChanged(hasOverflow, overflow) {
|
|
96
|
-
if (overflow) {
|
|
97
|
-
overflow.toggleAttribute('hidden', !hasOverflow);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/** @private */
|
|
102
|
-
_menuItemsChanged(items, overflow) {
|
|
103
|
-
if (!overflow) {
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
if (items !== this._oldItems) {
|
|
108
|
-
this._oldItems = items;
|
|
109
|
-
this.__renderButtons(items);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** @private */
|
|
114
|
-
__i18nChanged(i18n, overflow) {
|
|
115
|
-
if (overflow && i18n && i18n.moreOptions !== undefined) {
|
|
116
|
-
if (i18n.moreOptions) {
|
|
117
|
-
overflow.setAttribute('aria-label', i18n.moreOptions);
|
|
118
|
-
} else {
|
|
119
|
-
overflow.removeAttribute('aria-label');
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** @private */
|
|
125
|
-
__getOverflowCount(overflow) {
|
|
126
|
-
// We can't use optional chaining due to webpack 4
|
|
127
|
-
return (overflow.item && overflow.item.children && overflow.item.children.length) || 0;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/** @private */
|
|
131
|
-
__restoreButtons(buttons) {
|
|
132
|
-
for (let i = 0; i < buttons.length; i++) {
|
|
133
|
-
const btn = buttons[i];
|
|
134
|
-
btn.disabled = (btn.item && btn.item.disabled) || this.disabled;
|
|
135
|
-
btn.style.visibility = '';
|
|
136
|
-
btn.style.position = '';
|
|
137
|
-
|
|
138
|
-
// Teleport item component back from "overflow" sub-menu
|
|
139
|
-
const item = btn.item && btn.item.component;
|
|
140
|
-
if (item instanceof HTMLElement && item.classList.contains('vaadin-menu-item')) {
|
|
141
|
-
btn.appendChild(item);
|
|
142
|
-
item.classList.remove('vaadin-menu-item');
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
this.__updateOverflow([]);
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
/** @private */
|
|
149
|
-
__updateOverflow(items) {
|
|
150
|
-
this._overflow.item = { children: items };
|
|
151
|
-
this._hasOverflow = items.length > 0;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/** @private */
|
|
155
|
-
__setOverflowItems(buttons, overflow) {
|
|
156
|
-
const container = this._container;
|
|
157
|
-
|
|
158
|
-
if (container.offsetWidth < container.scrollWidth) {
|
|
159
|
-
this._hasOverflow = true;
|
|
160
|
-
|
|
161
|
-
const isRTL = this.getAttribute('dir') === 'rtl';
|
|
162
|
-
|
|
163
|
-
let i;
|
|
164
|
-
for (i = buttons.length; i > 0; i--) {
|
|
165
|
-
const btn = buttons[i - 1];
|
|
166
|
-
const btnStyle = getComputedStyle(btn);
|
|
167
|
-
|
|
168
|
-
// If this button isn't overflowing, then the rest aren't either
|
|
169
|
-
if (
|
|
170
|
-
(!isRTL && btn.offsetLeft + btn.offsetWidth < container.offsetWidth - overflow.offsetWidth) ||
|
|
171
|
-
(isRTL && btn.offsetLeft >= overflow.offsetWidth)
|
|
172
|
-
) {
|
|
173
|
-
break;
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
btn.disabled = true;
|
|
177
|
-
btn.style.visibility = 'hidden';
|
|
178
|
-
btn.style.position = 'absolute';
|
|
179
|
-
// Save width for buttons with component
|
|
180
|
-
btn.style.width = btnStyle.width;
|
|
181
|
-
}
|
|
182
|
-
const items = buttons.filter((_, idx) => idx >= i).map((b) => b.item);
|
|
183
|
-
this.__updateOverflow(items);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/** @private */
|
|
188
|
-
__detectOverflow() {
|
|
189
|
-
const overflow = this._overflow;
|
|
190
|
-
const buttons = this._buttons.filter((btn) => btn !== overflow);
|
|
191
|
-
const oldOverflowCount = this.__getOverflowCount(overflow);
|
|
192
|
-
|
|
193
|
-
// Reset all buttons in the menu bar and the overflow button
|
|
194
|
-
this.__restoreButtons(buttons);
|
|
195
|
-
|
|
196
|
-
// Hide any overflowing buttons and put them in the 'overflow' button
|
|
197
|
-
this.__setOverflowItems(buttons, overflow);
|
|
198
|
-
|
|
199
|
-
const newOverflowCount = this.__getOverflowCount(overflow);
|
|
200
|
-
if (oldOverflowCount !== newOverflowCount && this._subMenu.opened) {
|
|
201
|
-
this._subMenu.close();
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const isSingleButton = newOverflowCount === buttons.length || (newOverflowCount === 0 && buttons.length === 1);
|
|
205
|
-
this.toggleAttribute('has-single-button', isSingleButton);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
/** @protected */
|
|
209
|
-
_removeButtons() {
|
|
210
|
-
this._buttons.forEach((button) => {
|
|
211
|
-
if (button !== this._overflow) {
|
|
212
|
-
this.removeChild(button);
|
|
213
|
-
}
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/** @protected */
|
|
218
|
-
_initButton(item) {
|
|
219
|
-
const button = document.createElement('vaadin-menu-bar-button');
|
|
220
|
-
|
|
221
|
-
const itemCopy = { ...item };
|
|
222
|
-
button.item = itemCopy;
|
|
223
|
-
|
|
224
|
-
if (item.component) {
|
|
225
|
-
const component = this.__getComponent(itemCopy);
|
|
226
|
-
itemCopy.component = component;
|
|
227
|
-
// Save item for overflow menu
|
|
228
|
-
component.item = itemCopy;
|
|
229
|
-
button.appendChild(component);
|
|
230
|
-
} else if (item.text) {
|
|
231
|
-
button.textContent = item.text;
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
return button;
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
/** @protected */
|
|
238
|
-
_initButtonAttrs(button) {
|
|
239
|
-
button.setAttribute('role', 'menuitem');
|
|
240
|
-
|
|
241
|
-
if (button === this._overflow || (button.item && button.item.children)) {
|
|
242
|
-
button.setAttribute('aria-haspopup', 'true');
|
|
243
|
-
button.setAttribute('aria-expanded', 'false');
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
/** @protected */
|
|
248
|
-
_setButtonDisabled(button, disabled) {
|
|
249
|
-
button.disabled = disabled;
|
|
250
|
-
button.setAttribute('tabindex', disabled ? '-1' : '0');
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/** @protected */
|
|
254
|
-
_setButtonTheme(btn, hostTheme) {
|
|
255
|
-
let theme = hostTheme;
|
|
256
|
-
|
|
257
|
-
// Item theme takes precedence over host theme even if it's empty, as long as it's not undefined or null
|
|
258
|
-
const itemTheme = btn.item && btn.item.theme;
|
|
259
|
-
if (itemTheme != null) {
|
|
260
|
-
theme = Array.isArray(itemTheme) ? itemTheme.join(' ') : itemTheme;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
if (theme) {
|
|
264
|
-
btn.setAttribute('theme', theme);
|
|
265
|
-
} else {
|
|
266
|
-
btn.removeAttribute('theme');
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
/** @private */
|
|
271
|
-
__getComponent(item) {
|
|
272
|
-
const itemComponent = item.component;
|
|
273
|
-
let component;
|
|
274
|
-
|
|
275
|
-
const isElement = itemComponent instanceof HTMLElement;
|
|
276
|
-
// Use existing item component, if any
|
|
277
|
-
if (isElement && itemComponent.localName === 'vaadin-context-menu-item') {
|
|
278
|
-
component = itemComponent;
|
|
279
|
-
} else {
|
|
280
|
-
component = document.createElement('vaadin-context-menu-item');
|
|
281
|
-
component.appendChild(isElement ? itemComponent : document.createElement(itemComponent));
|
|
282
|
-
}
|
|
283
|
-
if (item.text) {
|
|
284
|
-
const node = component.firstChild || component;
|
|
285
|
-
node.textContent = item.text;
|
|
286
|
-
}
|
|
287
|
-
component.setAttribute('theme', 'menu-bar-item');
|
|
288
|
-
return component;
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/** @private */
|
|
292
|
-
__renderButtons(items = []) {
|
|
293
|
-
this._removeButtons();
|
|
294
|
-
|
|
295
|
-
/* Empty array, do nothing */
|
|
296
|
-
if (items.length === 0) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
items.forEach((item) => {
|
|
301
|
-
const button = this._initButton(item);
|
|
302
|
-
this.insertBefore(button, this._overflow);
|
|
303
|
-
this._setButtonDisabled(button, item.disabled);
|
|
304
|
-
this._initButtonAttrs(button);
|
|
305
|
-
this._setButtonTheme(button, this._theme);
|
|
306
|
-
});
|
|
307
|
-
|
|
308
|
-
this.__detectOverflow();
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* @protected
|
|
313
|
-
* @override
|
|
314
|
-
*/
|
|
315
|
-
_onResize() {
|
|
316
|
-
this.__detectOverflow();
|
|
317
|
-
}
|
|
318
|
-
};
|