Skip to content

refactor: do not disable buttons when moving to overflow #9027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 9 additions & 30 deletions packages/menu-bar/src/vaadin-menu-bar-mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export const MenuBarMixin = (superClass) =>
'_themeChanged(_theme, _overflow, _container)',
'__hasOverflowChanged(_hasOverflow, _overflow)',
'__i18nChanged(__effectiveI18n, _overflow)',
'_menuItemsChanged(items, _overflow, _container)',
'__updateButtons(items, disabled, _overflow, _container)',
'_reverseCollapseChanged(reverseCollapse, _overflow, _container)',
'_tabNavigationChanged(tabNavigation, _overflow, _container)',
];
Expand Down Expand Up @@ -381,23 +381,6 @@ export const MenuBarMixin = (superClass) =>
this.__detectOverflow();
}

/**
* Override method inherited from `DisabledMixin`
* to update the `disabled` property for the buttons
* whenever the property changes on the menu bar.
*
* @param {boolean} newValue the new disabled value
* @param {boolean} oldValue the previous disabled value
* @override
* @protected
*/
_disabledChanged(newValue, oldValue) {
super._disabledChanged(newValue, oldValue);
if (oldValue !== newValue) {
this.__updateButtonsDisabled(newValue);
}
}

/**
* A callback for the `_theme` property observer.
* It propagates the host theme to the buttons and the sub menu.
Expand Down Expand Up @@ -456,7 +439,7 @@ export const MenuBarMixin = (superClass) =>
}

/** @private */
_menuItemsChanged(items, overflow, container) {
__updateButtons(items, disabled, overflow, container) {
if (!overflow || !container) {
return;
}
Expand All @@ -467,6 +450,12 @@ export const MenuBarMixin = (superClass) =>
this.__detectOverflow();
}

if (disabled !== this._oldDisabled) {
this._oldDisabled = disabled;
this.__renderButtons(items);
overflow.toggleAttribute('disabled', disabled);
}

const subMenu = this._subMenu;
if (subMenu && subMenu.opened) {
const button = subMenu._overlayElement.positionTarget;
Expand Down Expand Up @@ -499,7 +488,6 @@ export const MenuBarMixin = (superClass) =>
/** @private */
__restoreButtons(buttons) {
buttons.forEach((button) => {
button.disabled = (button.item && button.item.disabled) || this.disabled;
button.style.visibility = '';
button.style.position = '';
button.style.width = '';
Expand All @@ -522,14 +510,6 @@ export const MenuBarMixin = (superClass) =>
item.removeAttribute('tabindex');
}

/** @private */
__updateButtonsDisabled(disabled) {
this._buttons.forEach((btn) => {
// Disable the button if the entire menu-bar is disabled or the item alone is disabled
btn.disabled = disabled || (btn.item && btn.item.disabled);
});
}

/** @private */
__updateOverflow(items) {
this._overflow.item = { children: items };
Expand Down Expand Up @@ -563,7 +543,6 @@ export const MenuBarMixin = (superClass) =>

// Save width for buttons with component
btn.style.width = getComputedStyle(btn).width;
btn.disabled = true;
btn.style.visibility = 'hidden';
btn.style.position = 'absolute';
}
Expand Down Expand Up @@ -662,7 +641,7 @@ export const MenuBarMixin = (superClass) =>
return html`
<vaadin-menu-bar-button
.item="${itemCopy}"
.disabled="${item.disabled}"
.disabled="${this.disabled || item.disabled}"
role="${this.tabNavigation ? 'button' : 'menuitem'}"
aria-haspopup="${ifDefined(hasChildren ? 'true' : nothing)}"
aria-expanded="${ifDefined(hasChildren ? 'false' : nothing)}"
Expand Down
68 changes: 2 additions & 66 deletions packages/menu-bar/test/overflow.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,15 @@ describe('overflow', () => {
`);
menu = wrapper.querySelector('vaadin-menu-bar');
await nextRender(menu);
menu.items = [
{ text: 'Item 1' },
{ text: 'Item 2' },
{ text: 'Item 3' },
{ text: 'Item 4' },
{ text: 'Item 5', disabled: true },
];
menu.items = [{ text: 'Item 1' }, { text: 'Item 2' }, { text: 'Item 3' }, { text: 'Item 4' }, { text: 'Item 5' }];
await nextUpdate(menu);
buttons = menu._buttons;
overflow = buttons[buttons.length - 1];
});

it('should show overflow button and hide the buttons which do not fit', () => {
assertHidden(buttons[2]);
expect(buttons[2].disabled).to.be.true;
assertHidden(buttons[3]);
expect(buttons[3].disabled).to.be.true;
expect(overflow.hasAttribute('hidden')).to.be.false;
});

Expand All @@ -69,9 +61,7 @@ describe('overflow', () => {
menu.style.width = '350px';
await nextResize(menu);
assertVisible(buttons[2]);
expect(buttons[2].disabled).to.not.be.true;
assertVisible(buttons[3]);
expect(buttons[3].disabled).to.not.be.true;
expect(overflow.item.children.length).to.equal(1);
expect(overflow.item.children[0]).to.deep.equal(menu.items[4]);
});
Expand All @@ -81,9 +71,7 @@ describe('overflow', () => {
menu.style.width = '350px';
await nextResize(menu);
assertVisible(buttons[2]);
expect(buttons[2].disabled).to.not.be.true;
assertVisible(buttons[3]);
expect(buttons[3].disabled).to.not.be.true;
expect(overflow.item.children.length).to.equal(1);
expect(overflow.item.children[0]).to.deep.equal(menu.items[4]);
});
Expand All @@ -92,7 +80,6 @@ describe('overflow', () => {
menu.style.width = '150px';
await nextResize(menu);
assertHidden(buttons[1]);
expect(buttons[1].disabled).to.be.true;
expect(overflow.item.children.length).to.equal(4);
expect(overflow.item.children[0]).to.deep.equal(menu.items[1]);
expect(overflow.item.children[1]).to.deep.equal(menu.items[2]);
Expand All @@ -105,7 +92,6 @@ describe('overflow', () => {
menu.style.width = '150px';
await nextResize(menu);
assertHidden(buttons[1]);
expect(buttons[1].disabled).to.be.true;
expect(overflow.item.children.length).to.equal(4);
expect(overflow.item.children[0]).to.deep.equal(menu.items[1]);
expect(overflow.item.children[1]).to.deep.equal(menu.items[2]);
Expand All @@ -117,11 +103,8 @@ describe('overflow', () => {
menu.style.width = 'auto';
await nextResize(menu);
assertVisible(buttons[2]);
expect(buttons[2].disabled).to.not.be.true;
assertVisible(buttons[3]);
expect(buttons[3].disabled).to.not.be.true;
assertVisible(buttons[4]);
expect(buttons[4].disabled).to.be.true;
expect(overflow.hasAttribute('hidden')).to.be.true;
expect(overflow.item.children.length).to.equal(0);
});
Expand Down Expand Up @@ -176,22 +159,6 @@ describe('overflow', () => {
await nextResize(menu);

expect(buttons[0].getAttribute('tabindex')).to.equal('0');
expect(buttons[1].getAttribute('tabindex')).to.equal('-1');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is not so relevant for the test, the actual thing is to ensure tabindex is set to 0 on the first visible button (which had tabindex="-1" before resizing above).

});

it('should set tabindex -1 on the overflow menu in tab navigation', async () => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a copy-paste of the test above. With tabNavigation the logic for setting tabindex="0" on the buttons that remain visible is not needed since all enabled buttons have tabindex="0" by default. Removed the test.

menu.tabNavigation = true;
buttons[0].focus();
arrowRight(buttons[0]);

expect(buttons[0].getAttribute('tabindex')).to.equal('0');
expect(buttons[1].getAttribute('tabindex')).to.equal('0');

menu.style.width = '150px';
await nextResize(menu);

expect(buttons[0].getAttribute('tabindex')).to.equal('0');
expect(buttons[1].getAttribute('tabindex')).to.equal('-1');
});

it('should set the aria-label of the overflow button according to the i18n of the menu bar', async () => {
Expand All @@ -216,15 +183,10 @@ describe('overflow', () => {

it('should show overflow button and hide the buttons which do not fit', () => {
assertHidden(buttons[0]);
expect(buttons[0].disabled).to.be.true;
assertHidden(buttons[1]);
expect(buttons[1].disabled).to.be.true;
assertHidden(buttons[2]);
expect(buttons[2].disabled).to.be.true;
assertVisible(buttons[3]);
expect(buttons[3].disabled).to.be.false;
assertVisible(buttons[4]);
expect(buttons[4].disabled).to.be.true;

expect(overflow.hasAttribute('hidden')).to.be.false;
});
Expand All @@ -242,15 +204,10 @@ describe('overflow', () => {
menu.reverseCollapse = false;
await nextUpdate(menu);
assertVisible(buttons[0]);
expect(buttons[0].disabled).to.be.false;
assertVisible(buttons[1]);
expect(buttons[1].disabled).to.be.false;
assertHidden(buttons[2]);
expect(buttons[2].disabled).to.be.true;
assertHidden(buttons[3]);
expect(buttons[3].disabled).to.be.true;
assertHidden(buttons[4]);
expect(buttons[4].disabled).to.be.true;
});
});
});
Expand Down Expand Up @@ -337,13 +294,7 @@ describe('overflow', () => {

container.style.width = '250px';

menu.items = [
{ text: 'Item 1' },
{ text: 'Item 2' },
{ text: 'Item 3' },
{ text: 'Item 4' },
{ text: 'Item 5', disabled: true },
];
menu.items = [{ text: 'Item 1' }, { text: 'Item 2' }, { text: 'Item 3' }, { text: 'Item 4' }, { text: 'Item 5' }];
await nextRender(menu);
buttons = menu._buttons;
overflow = buttons[buttons.length - 1];
Expand All @@ -357,31 +308,16 @@ describe('overflow', () => {
container.style.width = '150px';
await nextResize(menu);
assertHidden(buttons[2]);
expect(buttons[2].disabled).to.be.true;
assertHidden(buttons[3]);
expect(buttons[3].disabled).to.be.true;

container.style.width = '400px';
await nextResize(menu);
assertVisible(buttons[2]);
expect(buttons[2].disabled).to.not.be.true;
assertVisible(buttons[3]);
expect(buttons[3].disabled).to.not.be.true;
assertVisible(buttons[4]);
expect(buttons[4].disabled).to.be.true;
expect(overflow.hasAttribute('hidden')).to.be.true;
expect(overflow.item.children.length).to.equal(0);
});

it('should keep buttons disabled when resizing', async () => {
menu.disabled = true;
await nextUpdate(menu);
container.style.width = '150px';
await nextResize(menu);
buttons.forEach((btn) => {
expect(btn.disabled).to.be.true;
});
});
});

describe('parent resize', () => {
Expand Down