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

More than one menu showup, how to disappear it automatic? #1025

Closed
kyle-go opened this issue Jul 29, 2023 · 5 comments · Fixed by #1040
Closed

More than one menu showup, how to disappear it automatic? #1025

kyle-go opened this issue Jul 29, 2023 · 5 comments · Fixed by #1040
Labels
bug Something isn't working

Comments

@kyle-go
Copy link

kyle-go commented Jul 29, 2023

React/React-dom version:
React-Menu version:
4.0.2

Describe the bug

If the browser lose focus, right click the clickable area, will show two menus.

To Reproduce

  1. right click the clickable area, will show one menu
  2. click other window (let the browser lose focus)
  3. right click second clickable area, will show two menus.

Expected behavior

Show only one menu.

Code snippets/CodeSandbox examples/Screenshots

https://codesandbox.io/s/usecontrolledmenu-forked-yxndtd

picuture:
Screenshot 2023-07-29 at 18 17 27

@kyle-go kyle-go added the unconfirmed Needs triage label Jul 29, 2023
@szhsin
Copy link
Owner

szhsin commented Aug 7, 2023

Hi @kyle-go , thanks for raising this issue.

The bug is due to checking document.activeElement in addition to relatedTarget in onBlur event to decide when a menu should be closed. The code was brought in to make the library compatible with IE 11. As of v4 we have dropped IE 11 support so we can remove the workaround.

Will be fixing it in the upcoming release.

@szhsin szhsin added bug Something isn't working and removed unconfirmed Needs triage labels Aug 7, 2023
@szhsin szhsin linked a pull request Aug 13, 2023 that will close this issue
@szhsin
Copy link
Owner

szhsin commented Aug 13, 2023

Fixed by v4.0.3.

In addition to the new version, we need to make sure open context menu only if the browser tab is in foreground:

onContextMenu: (e) => {
  e.preventDefault();
  if (!document.hasFocus()) return;

  setAnchorRef(null);
  setAnchorPoint({ x: e.clientX, y: e.clientY });
  toggleMenu(true);
}

CodeSandbox Link

@szhsin szhsin closed this as completed Aug 13, 2023
@kyle-go
Copy link
Author

kyle-go commented Aug 13, 2023

Thanks for your update new version.
I tested antd dropdown. example: https://codesandbox.io/s/you-jian-cai-dan-antd-5-8-3-forked-cngr25

It can disappear even if document is not in focus.

any way, It's better than before, Thanks so much! Greate job

@JonathanMEdwards
Copy link

Is there any way to keep the standard behavior that right-click focuses the document? The suggested code above ignores them. Thank

@szhsin
Copy link
Owner

szhsin commented Jan 29, 2024

hey @JonathanMEdwards, you could remove the checking for document focus if (!document.hasFocus()) return;, but this will cause the menu to lose focus and cannot be closed.

I think the setup in the CodeSandbox Link is a middle ground solution without introducing an undesired bug.

This is due to how focus management is implemented in this library. We're still able to navigate through menu items using keyboard even in a context menu.

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

Successfully merging a pull request may close this issue.

3 participants