Skip to content

Commit

Permalink
fix(menuitem): prevent disabled and danger styles where it doesn't ma…
Browse files Browse the repository at this point in the history
…ke sense (#13223)

* fix(menuitem): prevent onClick when item is disabled

* fix(menuitem): prevent hover on danger disabled items

* fix(menuitem): items with submenus can't be disabled

* fix(menuitem): items with submenus can't be danger

---------

Co-authored-by: Andrea N. Cardona <cardona.n.andrea@gmail.com>
  • Loading branch information
janhassel and andreancardona authored Feb 27, 2023
1 parent 51b3de6 commit 0f73b41
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
25 changes: 15 additions & 10 deletions packages/react/src/components/Menu/MenuItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ const MenuItem = React.forwardRef(function MenuItem(
const [submenuOpen, setSubmenuOpen] = useState(false);
const hoverIntentTimeout = useRef(null);

const isDisabled = disabled && !hasChildren;
const isDanger = kind === 'danger' && !hasChildren;

function registerItem() {
context.dispatch({
type: 'registerItem',
Expand All @@ -71,13 +74,15 @@ const MenuItem = React.forwardRef(function MenuItem(
}

function handleClick(e) {
if (hasChildren) {
openSubmenu();
} else {
context.state.requestCloseRoot(e);

if (onClick) {
onClick(e);
if (!isDisabled) {
if (hasChildren) {
openSubmenu();
} else {
context.state.requestCloseRoot(e);

if (onClick) {
onClick(e);
}
}
}
}
Expand Down Expand Up @@ -109,8 +114,8 @@ const MenuItem = React.forwardRef(function MenuItem(
}

const classNames = cx(className, `${prefix}--menu-item`, {
[`${prefix}--menu-item--disabled`]: disabled,
[`${prefix}--menu-item--${kind}`]: kind !== 'default',
[`${prefix}--menu-item--disabled`]: isDisabled,
[`${prefix}--menu-item--danger`]: isDanger,
});

// on first render, register this menuitem in the context's state
Expand All @@ -127,7 +132,7 @@ const MenuItem = React.forwardRef(function MenuItem(
ref={ref}
className={classNames}
tabIndex="-1"
aria-disabled={disabled}
aria-disabled={isDisabled || null}
aria-haspopup={hasChildren || null}
aria-expanded={hasChildren ? submenuOpen : null}
onClick={handleClick}
Expand Down
4 changes: 3 additions & 1 deletion packages/styles/scss/components/menu/_menu.scss
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@
cursor: not-allowed;
}

.#{$prefix}--menu-item--disabled:hover {
.#{$prefix}--menu-item--disabled:hover,
.#{$prefix}--menu-item--disabled.#{$prefix}--menu-item--danger:hover {
background-color: $layer;
color: $text-disabled;
}

.#{$prefix}--menu-item--danger:focus,
Expand Down

0 comments on commit 0f73b41

Please sign in to comment.