Skip to content

Commit 1b29380

Browse files
authored
fix: reset item attributes when teleporting from the sub-menu (#5610)
1 parent 9274b04 commit 1b29380

2 files changed

Lines changed: 47 additions & 21 deletions

File tree

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,13 +223,21 @@ export const MenuBarMixin = (superClass) =>
223223
// Teleport item component back from "overflow" sub-menu
224224
const item = button.item && button.item.component;
225225
if (item instanceof HTMLElement && item.getAttribute('role') === 'menuitem') {
226-
button.appendChild(item);
227-
item.removeAttribute('role');
226+
this.__restoreItem(button, item);
228227
}
229228
});
230229
this.__updateOverflow([]);
231230
}
232231

232+
/** @private */
233+
__restoreItem(button, item) {
234+
button.appendChild(item);
235+
item.removeAttribute('role');
236+
item.removeAttribute('aria-expanded');
237+
item.removeAttribute('aria-haspopup');
238+
item.removeAttribute('tabindex');
239+
}
240+
233241
/** @private */
234242
__updateOverflow(items) {
235243
this._overflow.item = { children: items };

‎packages/menu-bar/test/menu-bar.test.js‎

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ describe('item components', () => {
668668
{ component: makeComponent('3') },
669669
{ text: 'Item 4 text', component: makeComponent('4') },
670670
{ text: 'Item 5', component: document.createElement('vaadin-menu-bar-item') },
671+
{ component: makeComponent('6'), children: [{ text: 'SubItem6.1' }, { text: 'SubItem6.2' }] },
671672
];
672673
await nextRender(menu);
673674
buttons = menu._buttons;
@@ -703,25 +704,6 @@ describe('item components', () => {
703704
expect(buttons[4].item.component.textContent).to.equal('Item 5');
704705
});
705706

706-
it('should teleport the same component to overflow sub-menu and back', async () => {
707-
menu.style.width = '250px';
708-
await onceResized(menu);
709-
await nextFrame();
710-
const subMenu = menu._subMenu;
711-
overflow.click();
712-
await nextRender(subMenu);
713-
const listBox = subMenu.$.overlay.querySelector('vaadin-menu-bar-list-box');
714-
expect(listBox.items[0]).to.equal(buttons[2].item.component);
715-
expect(listBox.items[0].firstChild).to.equal(menu.items[2].component);
716-
expect(listBox.items[0].firstChild.localName).to.equal('div');
717-
subMenu.close();
718-
menu.style.width = 'auto';
719-
await onceResized(menu);
720-
const item = buttons[2].firstChild;
721-
expect(item).to.equal(buttons[2].item.component);
722-
expect(item.getAttribute('role')).to.not.equal('menuitem');
723-
});
724-
725707
it('should close the overflow sub-menu on resize', async () => {
726708
menu.style.width = '150px';
727709
await onceResized(menu);
@@ -739,6 +721,42 @@ describe('item components', () => {
739721
expect(style.position).to.equal('relative');
740722
expect(Number(style.zIndex)).to.equal(1);
741723
});
724+
725+
describe('overflow', () => {
726+
let subMenu;
727+
728+
beforeEach(async () => {
729+
menu.style.width = '250px';
730+
await onceResized(menu);
731+
subMenu = menu._subMenu;
732+
});
733+
734+
it('should teleport the same component to overflow sub-menu and back', async () => {
735+
overflow.click();
736+
await nextRender(subMenu);
737+
const listBox = subMenu.$.overlay.querySelector('vaadin-menu-bar-list-box');
738+
expect(listBox.items[0]).to.equal(buttons[2].item.component);
739+
expect(listBox.items[0].firstChild).to.equal(menu.items[2].component);
740+
expect(listBox.items[0].firstChild.localName).to.equal('div');
741+
subMenu.close();
742+
menu.style.width = 'auto';
743+
await onceResized(menu);
744+
const item = buttons[2].firstChild;
745+
expect(item).to.equal(buttons[2].item.component);
746+
expect(item.getAttribute('role')).to.not.equal('menuitem');
747+
});
748+
749+
it('should restore menu bar item attribute state when moved from sub-menu back to menu bar', async () => {
750+
const item = buttons[5].firstChild;
751+
const itemAttributes = item.getAttributeNames();
752+
overflow.click();
753+
await nextRender(subMenu);
754+
subMenu.close();
755+
menu.style.width = 'auto';
756+
await onceResized(menu);
757+
expect(item.getAttributeNames()).to.have.members(itemAttributes);
758+
});
759+
});
742760
});
743761

744762
describe('menu-bar in flex', () => {

0 commit comments

Comments
 (0)