Menu opens whenever I click any link anywhere
There was an issue #91, which unfortunately was promptly closed by the author without providing much explanation. Now I'm hitting the same issue and I have no idea what's wrong.
const burgerMenu = <Menu
isOpen={isSidebarOpen}
customBurgerIcon={false}
customCrossIcon={false}
>
I open the menu by invoking this:
openMenu = () => {
this.setState({
isSidebarOpen: true,
});
}
and rely on clicking on overlay for closing it.
The problem is every time I click on a link anywhere it triggers opening the menu. Has anyone seen this behaviour?
Sounds like this question in the FAQs is what you're looking for: https://github.com/negomi/react-burger-menu/wiki/FAQ#i-imported-the-menu-but-the-burger-icon-isnt-showing-and-the-menu-opens-when-i-click-anywhere
Hi @negomi, thanks for the response.
Just wanted to add a little bit of context.
I use this component in two different apps. In one of them it works perfectly fine (in fact, much better than the alternatives), but in another (kind of legacy) it shows the behaviour that I described in the first post. Both usages are extremely similar and in both cases I add pretty much the same styles.
There are three reasons why the FAQ solution is not applicable:
- the burger icon is disabled
- the styles are applied
- menu is not showing when I click anywhere, only the links
I suspect that this behaviour is caused by some clash of styles or DOM quirks (i.e clicking at uncontrolled links ends up handled by overlay), but what I don't get is how this kind of thing is possible in the first place. My understanding is that once I specified isOpen and disabled the customBurgerIcon via props, the component must never open for any reason other than me changing the state to open. But it does, probably because the unhandled click gets to the overlay, and the overlay just calls toggleMenu(), which is used for both opening and closing.
Ah, interesting. When you pasted the example above without the styles prop, it sounded like the usual reason this happens, but I guess not, so apologies for that :)
This might be difficult to debug usefully without a working example, but some things to check are:
- Is the overlay being moved fully out of the viewport when the menu is closed?
- What handlers are applied to the other links? Could they be interacting with the menu state?
- Do the link clicks bubble up to anything that could be setting your
isSidebarOpenstate to true?
I'm having this issue as well.
I saw something similar while handling my own isOpen state. Clicking out of overlay, or pressing escape would change the internal state of burger-menu, but obviously not update my own isOpen state. So if i closed the menu one of those ways, my state was still set to open, and a re-render from prop changes would cause the state check to open the menu.
This would also cause what seemed like having to double click my open/close icon, the first click updating my open state to false, then the setting it to open.
Adding the onStateChange handler to my app kept them both in synch.
I'm going to close this for now as no response from the OP, and @t-sean's response suggests this could be due to not keeping state in sync.
Happy to reopen if anyone's still experiencing this for some reason not already covered though.
I'm getting back to this issue.
@t-sean
It is correct that the menu is out of sync, but what can I do to prevent it from happening? The onStateChange simply confirms the problem but doesn't fix it.
@negomi The overlay is out of viewport when the menu is closed, so this is probably OK. I experimented with different links, and it seems that wrong behaviour starts when I do the following:
- trigger menu correctly at least once (and then hide it)
- click on the
Linkwith non-emptytoattribute fromreact-router@3
If the Link's handler calls event.preventDefault(), the menu doesn't open (but it will also prevent the Link from functioning properly). If it only has event.stopPropagation(), the menu is still triggered and opens. Have you seen anything similar?
My onStateChange calls a function to update my own internal state.
onStateChange={(state) => updateMenuState(state)}
@t-sean
Indeed! Thanks a lot for your suggestion; this solves my issue.
Basically, I needed onStateChange to update my internal state after clicking on the overlay. If I don't do that then the state remains open. In this case, clicking the Link triggers componentWillReceiveProps on the Menu, and since isOpen is wrong at this point, the Menu shows up.