Skip to content
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

🐛 Bug - Menu with all items disabled does not disappear on clicking away #2924

Open
andreascful opened this issue Oct 29, 2024 · 3 comments · May be fixed by #2957
Open

🐛 Bug - Menu with all items disabled does not disappear on clicking away #2924

andreascful opened this issue Oct 29, 2024 · 3 comments · May be fixed by #2957
Assignees
Labels
bug Something isn't working as expected in backlog

Comments

@andreascful
Copy link

andreascful commented Oct 29, 2024

Forma 36 bug report

Summary

When having a menu that contains only disabled items, clicking anywhere on the screen does not close it, like it usually would. Only clicking on the Menu.Trigger will close it again.

image

Playground reproduction

Environment

MacOS
Browsers: Latest Chrome and Firefox

Steps to reproduce

  • Click on the menu trigger
  • Click somewhere else and notice that the menu does not close

Expected results

The menu should close when clicking away. Set one of the items to enabled and you'll see the expected behaviour

Actual results

The menu stays open when clicking away.

@andreascful andreascful added the bug Something isn't working as expected label Oct 29, 2024
@DavidSanwald
Copy link

DavidSanwald commented Nov 13, 2024

I've been looking into this interesting behavior and I think I might have found a potential cause. It seems to be related to PR #2741, where items marked as disabled are implemented using the native HTML disabled attribute, which prevents focus/blur events. This explains why having at least one enabled item allows the expected behavior to work.

One potential approach could be to use aria-disabled instead of the native disabled attribute. Would love to hear thoughts on this direction - if it seems reasonable, I'd be happy to create a PR.

@ManasiRoy
Copy link

import React, { useState, useRef, useEffect } from 'react';
import { Menu, IconButton } from '@contentful/f36-components';
import { MenuIcon } from '@contentful/f36-icons';

export default function BasicMenu() {
const [isOpen, setIsOpen] = useState(false);
const menuRef = useRef(null);

const handleMenuToggle = () => {
setIsOpen((prev) => !prev);
};

const handleClickOutside = (event) => {
if (menuRef.current && !menuRef.current.contains(event.target)) {
setIsOpen(false);
}
};

useEffect(() => {
document.addEventListener('mousedown', handleClickOutside);
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, []);

return (



<Menu.Trigger>
<IconButton
variant="secondary"
icon={}
aria-label="toggle menu"
onClick={handleMenuToggle}
/>
</Menu.Trigger>
<Menu.List>
<Menu.Item disabled>Create an entry</Menu.Item>
<Menu.Item disabled>Remove an entry</Menu.Item>
<Menu.Item disabled>Embed existing entry</Menu.Item>
</Menu.List>


);
}

I hope this will work. Happy coding :)

@DavidSanwald
Copy link

DavidSanwald commented Dec 3, 2024

@ManasiRoy, thanks for your suggestion. Did you accidentally paste the wrong code?
Your currently shared code does not compile. The icon prop for the IconButton is missing, you do not attach/pass the ref you create to something and need to wrap your returned JSX with a Menu component.

It seems like you intended to use the Menu as a controlled component, which is a good workaround but it wouldn't fix the behavior for the uncontrolled component when used with only disabled MenuItems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected in backlog
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants