diff --git a/examples/example-dockpanel/src/index.ts b/examples/example-dockpanel/src/index.ts index 6c884c2e8..d236c7d4c 100644 --- a/examples/example-dockpanel/src/index.ts +++ b/examples/example-dockpanel/src/index.ts @@ -280,10 +280,15 @@ function main(): void { menu3.title.label = 'View'; menu3.title.mnemonic = 0; + let emptyMenu = new Menu({ commands }); + emptyMenu.title.label = 'Empty Menu'; + emptyMenu.title.mnemonic = 0; + let bar = new MenuBar(); bar.addMenu(menu1); bar.addMenu(menu2); bar.addMenu(menu3); + bar.addMenu(emptyMenu); bar.id = 'menuBar'; let palette = new CommandPalette({ commands }); diff --git a/packages/widgets/src/menubar.ts b/packages/widgets/src/menubar.ts index 25cd7eea8..0a88bfb10 100644 --- a/packages/widgets/src/menubar.ts +++ b/packages/widgets/src/menubar.ts @@ -405,6 +405,9 @@ export class MenuBar extends Widget { for (let i = 0, n = menus.length; i < n; ++i) { let title = menus[i].title; let active = i === activeIndex; + if (active && menus[i].items.length == 0) { + active = false; + } content[i] = renderer.renderItem({ title, active, @@ -607,7 +610,9 @@ export class MenuBar extends Widget { let { left, bottom } = (itemNode as HTMLElement).getBoundingClientRect(); // Open the new menu at the computed location. - newMenu.open(left, bottom, this._forceItemsPosition); + if (newMenu.items.length > 0) { + newMenu.open(left, bottom, this._forceItemsPosition); + } } /** diff --git a/packages/widgets/tests/src/menubar.spec.ts b/packages/widgets/tests/src/menubar.spec.ts index be4d31107..bd81506f3 100644 --- a/packages/widgets/tests/src/menubar.spec.ts +++ b/packages/widgets/tests/src/menubar.spec.ts @@ -288,6 +288,16 @@ describe('@lumino/widgets', () => { expect(menu.isAttached).to.equal(false); bar.dispose(); }); + + it('should be a no-op if the active menu is empty', () => { + let bar = new MenuBar(); + let menu = new Menu({ commands }); + bar.addMenu(menu); + bar.activeMenu = menu; + bar.openActiveMenu(); + expect(menu.isAttached).to.equal(false); + bar.dispose(); + }); }); describe('#addMenu()', () => {