Best practice for hiding menu programmatically?
See original GitHub issueFirst, great component! Question: I’m implementing a non-conventional way of hiding the menu whereby if the user taps on an element within the burger menu overlay, the menu hides. I’m able to successfully make this happen with the following lines of code:
if(document.body.classList.contains('open-toc')){
document.body.classList.remove('open-toc');
let bmCrossButton = document.getElementsByClassName('bm-cross-button');
let bmCrossButtonChildren = bmCrossButton[0].childNodes;
let crossButton = bmCrossButtonChildren[ bmCrossButtonChildren.length - 1 ];
crossButton.click();
}
else if( state && state.isOpen ) {
document.body.classList.toggle('open-toc');
}
I’m newer to React conventions, so I’m not sure using Javascript like this is a best practice, or if there might be a better way of implementing. Thanks in advance for the help, - B
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
java - How to hide by default menu from custom perspective ...
I am building a plugin for my own custom perspective. In which i want to hide some default menu like navigate, run for...
Read more >Hiding menu items dynamically - Drupal Answers
I set the links hidden attribute to 1 to hide it. I then saved the menu link. Then I cleared the menu cache....
Read more >How to hide menu items without relying on permissions ...
I need to hide some main menu items which should only be shown under some specific circumstances. I cannot use hook_menu_alter() because it ......
Read more >Programmatically show and hide menu bar items - aka switch ...
Programmatically show and hide menu bar items - aka switch between to ... It's in general good practice to disable (not hide) menu...
Read more >Drupal 8 - Programmatically hide or show menu items
When a user is not allowed to see a page, menu links leading to this page are also hidden from menus. However, there...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Hey @briankuyath, thanks, glad you like the component!
The more React-y way to make components do things is to pass down data/instructions via their props (the
isOpen
prop is what you need in this case).So to close the menu programmatically, you would need to store the menu state somewhere outside the component (the simplest place would be in a parent component’s state, so I’ll use that to demonstrate). Now you have control of that state (i.e. the menu is a ‘controlled component’), you can set it to whatever you want via other actions (e.g. a tap on your table of contents). Then all you have to do is pass that state down (via the
isOpen
prop), and the menu will open/close according to the state you pass.The code would look something like this:
Hope that helps 😃
@negomi it was because I’m using next.js and on Ssr was causing the re render! Thanks for the reply though!