Skip to content

Commit ea7c85a

Browse files
authored
refactor!: move menu-bar buttons to light DOM (#4687)
1 parent 016299b commit ea7c85a

11 files changed

Lines changed: 106 additions & 94 deletions

‎packages/menu-bar/src/vaadin-menu-bar-button.js‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { css, registerStyles } from '@vaadin/vaadin-themable-mixin/vaadin-themab
99
registerStyles(
1010
'vaadin-menu-bar-button',
1111
css`
12+
:host {
13+
flex-shrink: 0;
14+
}
15+
16+
:host([slot='overflow']) {
17+
margin-inline-end: 0;
18+
}
19+
1220
[part='label'] ::slotted(vaadin-context-menu-item) {
1321
position: relative;
1422
z-index: 1;

‎packages/menu-bar/src/vaadin-menu-bar-buttons-mixin.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import type { Constructor } from '@open-wc/dedupe-mixin';
7+
import type { ControllerMixinClass } from '@vaadin/component-base/src/controller-mixin.js';
78
import type { ResizeMixinClass } from '@vaadin/component-base/src/resize-mixin.js';
89

910
export declare function ButtonsMixin<T extends Constructor<HTMLElement>>(
1011
base: T,
11-
): Constructor<ButtonsMixinClass> & Constructor<ResizeMixinClass> & T;
12+
): Constructor<ButtonsMixinClass> & Constructor<ControllerMixinClass> & Constructor<ResizeMixinClass> & T;
1213

1314
export declare class ButtonsMixinClass {
1415
protected readonly _buttons: HTMLElement[];

‎packages/menu-bar/src/vaadin-menu-bar-buttons-mixin.js‎

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
* Copyright (c) 2019 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6+
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
67
import { ResizeMixin } from '@vaadin/component-base/src/resize-mixin.js';
8+
import { SlotController } from '@vaadin/component-base/src/slot-controller.js';
79

810
/**
911
* @polymerMixin
1012
* @mixes ResizeMixin
13+
* @mixes ControllerMixin
1114
*/
1215
export const ButtonsMixin = (superClass) =>
13-
class extends ResizeMixin(superClass) {
16+
class extends ResizeMixin(ControllerMixin(superClass)) {
1417
static get properties() {
1518
return {
1619
/**
@@ -21,11 +24,20 @@ export const ButtonsMixin = (superClass) =>
2124
type: Boolean,
2225
value: false,
2326
},
27+
28+
/** @protected */
29+
_overflow: {
30+
type: Object,
31+
},
2432
};
2533
}
2634

2735
static get observers() {
28-
return ['_menuItemsChanged(items, items.splices)'];
36+
return [
37+
'__hasOverflowChanged(_hasOverflow, _overflow)',
38+
'__i18nChanged(i18n, _overflow)',
39+
'_menuItemsChanged(items, _overflow, items.splices)',
40+
];
2941
}
3042

3143
/**
@@ -43,21 +55,32 @@ export const ButtonsMixin = (superClass) =>
4355
super.ready();
4456

4557
this.setAttribute('role', 'menubar');
46-
}
4758

48-
/** @protected */
49-
connectedCallback() {
50-
super.connectedCallback();
59+
this._overflowController = new SlotController(
60+
this,
61+
'overflow',
62+
() => document.createElement('vaadin-menu-bar-button'),
63+
(_, btn) => {
64+
btn.setAttribute('hidden', '');
5165

52-
this._initButtonAttrs(this._overflow);
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);
5376
}
5477

5578
/**
5679
* @return {!Array<!HTMLElement>}
5780
* @protected
5881
*/
5982
get _buttons() {
60-
return Array.from(this.shadowRoot.querySelectorAll('[part$="button"]'));
83+
return Array.from(this.querySelectorAll('vaadin-menu-bar-button'));
6184
}
6285

6386
/**
@@ -68,22 +91,36 @@ export const ButtonsMixin = (superClass) =>
6891
return this.shadowRoot.querySelector('[part="container"]');
6992
}
7093

71-
/**
72-
* @return {!HTMLElement}
73-
* @protected
74-
*/
75-
get _overflow() {
76-
return this.shadowRoot.querySelector('[part="overflow-button"]');
94+
/** @private */
95+
__hasOverflowChanged(hasOverflow, overflow) {
96+
if (overflow) {
97+
overflow.toggleAttribute('hidden', !hasOverflow);
98+
}
7799
}
78100

79101
/** @private */
80-
_menuItemsChanged(items) {
102+
_menuItemsChanged(items, overflow) {
103+
if (!overflow) {
104+
return;
105+
}
106+
81107
if (items !== this._oldItems) {
82108
this._oldItems = items;
83109
this.__renderButtons(items);
84110
}
85111
}
86112

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+
87124
/** @private */
88125
__getOverflowCount(overflow) {
89126
// We can't use optional chaining due to webpack 4
@@ -170,17 +207,16 @@ export const ButtonsMixin = (superClass) =>
170207

171208
/** @protected */
172209
_removeButtons() {
173-
const container = this._container;
174-
175-
while (container.children.length > 1) {
176-
container.removeChild(container.firstElementChild);
177-
}
210+
this._buttons.forEach((button) => {
211+
if (button !== this._overflow) {
212+
this.removeChild(button);
213+
}
214+
});
178215
}
179216

180217
/** @protected */
181218
_initButton(item) {
182219
const button = document.createElement('vaadin-menu-bar-button');
183-
button.setAttribute('part', 'menu-bar-button');
184220

185221
const itemCopy = { ...item };
186222
button.item = itemCopy;
@@ -231,11 +267,6 @@ export const ButtonsMixin = (superClass) =>
231267
}
232268
}
233269

234-
/** @protected */
235-
_appendButton(button) {
236-
this._container.insertBefore(button, this._overflow);
237-
}
238-
239270
/** @private */
240271
__getComponent(item) {
241272
const itemComponent = item.component;
@@ -268,7 +299,7 @@ export const ButtonsMixin = (superClass) =>
268299

269300
items.forEach((item) => {
270301
const button = this._initButton(item);
271-
this._appendButton(button);
302+
this.insertBefore(button, this._overflow);
272303
this._setButtonDisabled(button, item.disabled);
273304
this._initButtonAttrs(button);
274305
this._setButtonTheme(button, this._theme);

‎packages/menu-bar/src/vaadin-menu-bar-interactions-mixin.js‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
66
import { FocusMixin } from '@vaadin/component-base/src/focus-mixin.js';
7-
import { isKeyboardActive } from '@vaadin/component-base/src/focus-utils.js';
7+
import { isElementFocused, isKeyboardActive } from '@vaadin/component-base/src/focus-utils.js';
88
import { KeyboardDirectionMixin } from '@vaadin/component-base/src/keyboard-direction-mixin.js';
99

1010
/**
@@ -55,15 +55,15 @@ export const InteractionsMixin = (superClass) =>
5555

5656
/**
5757
* Override getter from `KeyboardDirectionMixin`
58-
* to look for activeElement in shadow root, or
59-
* use the expanded button as a fallback.
58+
* to use expanded button for arrow navigation
59+
* when the sub-menu is opened and has focus.
6060
*
6161
* @return {Element | null}
6262
* @protected
6363
* @override
6464
*/
6565
get focused() {
66-
return this.shadowRoot.activeElement || this._expandedButton;
66+
return (this._getItems() || []).find(isElementFocused) || this._expandedButton;
6767
}
6868

6969
/**
@@ -89,11 +89,6 @@ export const InteractionsMixin = (superClass) =>
8989
return this._buttons;
9090
}
9191

92-
/** @private */
93-
get __isRTL() {
94-
return this.getAttribute('dir') === 'rtl';
95-
}
96-
9792
/** @protected */
9893
disconnectedCallback() {
9994
super.disconnectedCallback();
@@ -129,7 +124,7 @@ export const InteractionsMixin = (superClass) =>
129124

130125
/** @protected */
131126
_hideTooltip(immediate) {
132-
const tooltip = this._tooltipController.node;
127+
const tooltip = this._tooltipController && this._tooltipController.node;
133128
if (tooltip) {
134129
tooltip._stateController.close(immediate);
135130
}
@@ -192,7 +187,7 @@ export const InteractionsMixin = (superClass) =>
192187
*/
193188
_setFocused(focused) {
194189
if (focused) {
195-
const target = this.shadowRoot.querySelector('[part$="button"][tabindex="0"]');
190+
const target = this.querySelector('[tabindex="0"]');
196191
if (target) {
197192
this._buttons.forEach((btn) => {
198193
this._setTabindex(btn, btn === target);

‎packages/menu-bar/src/vaadin-menu-bar.d.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Copyright (c) 2019 - 2022 Vaadin Ltd.
44
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
55
*/
6-
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
76
import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
87
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
98
import { ThemableMixin } from '@vaadin/vaadin-themable-mixin/vaadin-themable-mixin.js';
@@ -87,8 +86,6 @@ export interface MenuBarEventMap extends HTMLElementEventMap, MenuBarCustomEvent
8786
* Part name | Description
8887
* ------------------|----------------
8988
* `container` | The container wrapping menu bar buttons.
90-
* `menu-bar-button` | The menu bar button.
91-
* `overflow-button` | The "overflow" button appearing when menu bar width is not enough to fit all the buttons.
9289
*
9390
* The following state attributes are available for styling:
9491
*
@@ -111,9 +108,7 @@ export interface MenuBarEventMap extends HTMLElementEventMap, MenuBarCustomEvent
111108
*
112109
* @fires {CustomEvent} item-selected - Fired when a submenu item or menu bar button without children is clicked.
113110
*/
114-
declare class MenuBar extends ButtonsMixin(
115-
DisabledMixin(InteractionsMixin(ElementMixin(ThemableMixin(ControllerMixin(HTMLElement))))),
116-
) {
111+
declare class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(ElementMixin(ThemableMixin(HTMLElement))))) {
117112
/**
118113
* Defines a hierarchical structure, where root level items represent menu bar buttons,
119114
* and `children` property configures a submenu with items to be opened below

‎packages/menu-bar/src/vaadin-menu-bar.js‎

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import './vaadin-menu-bar-submenu.js';
77
import './vaadin-menu-bar-button.js';
88
import { html, PolymerElement } from '@polymer/polymer/polymer-element.js';
9-
import { ControllerMixin } from '@vaadin/component-base/src/controller-mixin.js';
109
import { DisabledMixin } from '@vaadin/component-base/src/disabled-mixin.js';
1110
import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
1211
import { TooltipController } from '@vaadin/component-base/src/tooltip-controller.js';
@@ -38,8 +37,6 @@ import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
3837
* Part name | Description
3938
* ------------------|----------------
4039
* `container` | The container wrapping menu bar buttons.
41-
* `menu-bar-button` | The menu bar button.
42-
* `overflow-button` | The "overflow" button appearing when menu bar width is not enough to fit all the buttons.
4340
*
4441
* The following state attributes are available for styling:
4542
*
@@ -63,16 +60,13 @@ import { InteractionsMixin } from './vaadin-menu-bar-interactions-mixin.js';
6360
* @fires {CustomEvent<boolean>} item-selected - Fired when a submenu item or menu bar button without children is clicked.
6461
*
6562
* @extends HTMLElement
66-
* @mixes ControllerMixin
6763
* @mixes ButtonsMixin
6864
* @mixes InteractionsMixin
6965
* @mixes DisabledMixin
7066
* @mixes ElementMixin
7167
* @mixes ThemableMixin
7268
*/
73-
class MenuBar extends ButtonsMixin(
74-
DisabledMixin(InteractionsMixin(ElementMixin(ThemableMixin(ControllerMixin(PolymerElement))))),
75-
) {
69+
class MenuBar extends ButtonsMixin(DisabledMixin(InteractionsMixin(ElementMixin(ThemableMixin(PolymerElement))))) {
7670
static get template() {
7771
return html`
7872
<style>
@@ -91,27 +85,11 @@ class MenuBar extends ButtonsMixin(
9185
flex-wrap: nowrap;
9286
overflow: hidden;
9387
}
94-
95-
[part$='button'] {
96-
flex-shrink: 0;
97-
}
98-
99-
[part='overflow-button'] {
100-
margin-right: 0;
101-
}
102-
103-
.dots::before {
104-
display: block;
105-
content: '\\00B7\\00B7\\00B7';
106-
font-size: inherit;
107-
line-height: inherit;
108-
}
10988
</style>
11089
11190
<div part="container">
112-
<vaadin-menu-bar-button part="overflow-button" hidden$="[[!_hasOverflow]]" aria-label$="[[i18n.moreOptions]]">
113-
<div class="dots"></div>
114-
</vaadin-menu-bar-button>
91+
<slot></slot>
92+
<slot name="overflow"></slot>
11593
</div>
11694
<vaadin-menu-bar-submenu is-root=""></vaadin-menu-bar-submenu>
11795
@@ -219,7 +197,7 @@ class MenuBar extends ButtonsMixin(
219197
}
220198

221199
static get observers() {
222-
return ['_themeChanged(_theme)'];
200+
return ['_themeChanged(_theme, _overflow)'];
223201
}
224202

225203
/** @protected */
@@ -255,8 +233,8 @@ class MenuBar extends ButtonsMixin(
255233
* @param {string | null} theme
256234
* @protected
257235
*/
258-
_themeChanged(theme) {
259-
if (this.shadowRoot) {
236+
_themeChanged(theme, overflow) {
237+
if (overflow) {
260238
this._buttons.forEach((btn) => this._setButtonTheme(btn, theme));
261239
this.__detectOverflow();
262240
}

0 commit comments

Comments
 (0)