Skip to content

Commit bf130cb

Browse files
fix(menu-item): bugs with disabled state (#21340)
* fix(menu-item): cursor not allowed, only shows on parts of the menu-item * fix(menu-item): do not dispatch close event if entry clicked is disabled * fix(menu-button): prevent closing menu on click regardless of state * fix(menu-tem): prevent mouse focus onto disabled items --------- Co-authored-by: Kritvi <158570656+Kritvi-bhatia17@users.noreply.github.com>
1 parent d125d30 commit bf130cb

4 files changed

Lines changed: 24 additions & 3 deletions

File tree

packages/react/src/components/Menu/MenuItem.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,12 @@ export const MenuItem = forwardRef<HTMLLIElement, MenuItemProps>(
190190
const keyboardClickEvent = (e: KeyboardEvent) =>
191191
match(e, keys.Enter) || match(e, keys.Space);
192192

193+
function handleMouseDown(e: MouseEvent<HTMLLIElement>) {
194+
if (isDisabled) {
195+
e.preventDefault();
196+
}
197+
}
198+
193199
function handleKeyDown(e: KeyboardEvent<HTMLLIElement>) {
194200
if (hasChildren && match(e, keys.ArrowRight)) {
195201
openSubmenu();
@@ -268,6 +274,7 @@ export const MenuItem = forwardRef<HTMLLIElement, MenuItemProps>(
268274
aria-disabled={isDisabled ?? undefined}
269275
aria-haspopup={hasChildren ?? undefined}
270276
aria-expanded={hasChildren ? submenuOpen : undefined}
277+
onMouseDown={handleMouseDown}
271278
onClick={handleClick}
272279
onKeyDown={handleKeyDown}
273280
onKeyUp={handleKeyUp}

packages/web-components/src/components/menu-button/menu-button.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,14 @@ class CDSMenuButton extends HostListenerMixin(LitElement) {
9595
return;
9696
}
9797

98+
// Handle clicks on trigger only, other listeners handle clicks on menu items
9899
const path = event.composedPath();
99100
if (path.includes(trigger)) {
100101
if (this._open) {
101102
this._closeMenu({ restoreFocus: true });
102103
} else {
103104
this._open = true;
104105
}
105-
} else if (this._open) {
106-
this._closeMenu();
107106
}
108107
};
109108

packages/web-components/src/components/menu/menu-item.scss

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,15 @@ $css--plex: true !default;
7171
display: var(--child-icon-property, none);
7272
}
7373

74-
:host([aria-disabled='true']) * {
74+
:host([aria-disabled='true']) {
7575
color: $text-disabled;
7676
cursor: not-allowed;
7777
}
7878

79+
:host([aria-disabled='true']) :where(*) {
80+
color: inherit;
81+
}
82+
7983
:host([aria-disabled='true']:hover),
8084
:host([aria-disabled='true'].#{$prefix}--menu-item--danger:hover) {
8185
background-color: $layer;

packages/web-components/src/components/menu/menu-item.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ class CDSmenuItem extends HostListenerMixin(HostListenerMixin(LitElement)) {
188188
this._handleClick(event);
189189
}
190190

191+
@HostListener('mousedown')
192+
handleMouseDown(event: MouseEvent) {
193+
if (this.disabled) {
194+
event.preventDefault();
195+
}
196+
}
197+
191198
@HostListener('mouseenter')
192199
handleMouseEnter() {
193200
if (this.hasSubmenu) {
@@ -263,6 +270,10 @@ class CDSmenuItem extends HostListenerMixin(HostListenerMixin(LitElement)) {
263270
}
264271

265272
_handleClick = (e: MouseEvent | KeyboardEvent): void => {
273+
if (this.disabled) {
274+
return;
275+
}
276+
266277
if (this.hasSubmenu) {
267278
this._openSubmenu();
268279
return;

0 commit comments

Comments
 (0)