question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Menu on closing bug

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? Menu pops up for a seconds when closing. https://streamable.com/9ff7um

`const Test = () => { const { show } = useContextMenu({ id: “test” });

return (
    <>
        <div onContextMenu={(e) => {
            show(e, {
                id: "test"
            });
        }}>
            <Menu id={"test"}>
                <Item onClick={(e) => {console.log(2)}}>
                    We
                </Item>
            </Menu>
            WEWEWEWEW
        </div>
    </>
);

}`

Which versions of React, and which browser / OS are affected by this issue? Did this work in previous versions of React? React 18.2.0, Firefox, Chrome, Chromium on Windows 11

Also, I’m using Vite to the latest version.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
fkhadracommented, Nov 3, 2022

Hey @ sotarules, thanks for the detailed write-up. The library has not been updated for a while, lack of time, covid, new kids, etc… Anyway, I’m currently working on the next version scheduled for this month. I’m trying to see if I can fix the issue without relying on flushSync if not then I’ll go for it

1reaction
sotarulescommented, Oct 15, 2022

This problem is caused by React 18 update batching. I have created a patch that seems to fix the problem. May patch affects function handleAnimationEnd (in react-contexify.esm.js):

  function handleAnimationEnd() {
    ReactDOM.flushSync(() => {
      if (state.willLeave && state.visible) {
        setState({
          visible: false,
          willLeave: false
        });
      }
    })
  }

I’ve added flushSync to ensure that these animation end state changes are not batched. Here is my best understanding of why this fixes the problem. It all hinges on how the menu is hidden.

    hasExitAnimation(animation) ? setState(function (state) {
      return {
        willLeave: state.visible
      };
    }) : setState(function (state) {
      return {
        visible: state.visible ? false : state.visible
      };
    });

Assuming the context menu is animated, the system will set willLeave to true. But React 18 batch update logic prevents willLeave from being set to true at the time handleAnimationEnd fires. This since state.willLeave is false, handleAnimationEnd fails to setState visible to false, causing the context menu to remain visible.

I’ve attached my patch-package that I used to fix the problem on my system. react-contexify+5.0.0.zip

By the way just for the record, I’m upset that the React developers chose to force the concept of batched updates on us. I have had to fight a number of issues, each of them not obvious, both in my own code plus a variety of third-party packages because of this. This has been very costly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Menus keep closing. (bug) : r/wow - Reddit
(bug) Hello, i was wondering if someone could help me, anytime i open a menu like the map or the garrison mission table...
Read more >
Bug: Closing app when menu flyouts are open causes hang · Issue ...
Closing the app when the right click content menu is still open causes the application to hang for a couple of seconds before...
Read more >
Menu nav closing bug - Bugs - Forum | Webflow
By opening menu on my mobile (iPhone 6s) menu won't close after tapping outside of Nav Menu zone. However in preview mode and...
Read more >
Start Menu Closing by Itself, can't fix, PLEASE HELP!
Now the start menu works when I click it, but closes itself after 1 second so I can't really do much. I tried...
Read more >
Chrome drop down menu bug (Closing automatically)
Chrome drop down menu bug (Closing automatically) · Re-installing Chrome · Checked for malware with Chrome and all other trusted anti malware ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found