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' ;
67import { 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 */
1215export 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 ) ;
0 commit comments