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.

Best practice for hiding menu programmatically?

See original GitHub issue

First, 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
negomicommented, Sep 7, 2017

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:

class ParentComponent extends React.Component {
  constructor (props) {
    super(props)
    this.state = {
      menuOpen: false
    }
  }

  // This keeps your state in sync with the opening/closing of the menu
  // via the expected means, e.g. clicking the X, pressing the ESC key etc.
  handleStateChange (state) {
    this.setState({menuOpen: state.isOpen})  
  }

  // This callback function can be used to make
  // the menu close on any event you want
  closeMenu () {
    this.setState({menuOpen: false})
  }

  render () {
    return (
      <Menu isOpen={this.state.menuOpen} onStateChange={this.handleStateChange}>
        // Your component, doesn't matter where it is as long as you can pass
        // it a callback to be able to trigger the menu state change
        <TableOfContents onClick={() => this.closeMenu()} />
      </Menu>
    )
  }
}

Hope that helps 😃

1reaction
tyrongcommented, Jan 12, 2018

@negomi it was because I’m using next.js and on Ssr was causing the re render! Thanks for the reply though!

Read more comments on GitHub >

github_iconTop 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 >

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