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.

Dropdown doesn't hide on single page web apps

See original GitHub issue

I’m using this drop down on a single page web app with the Dropdown in a global header. In the Dropdown I have links for navigation change the URL. Its a single page web app so its just changing route and the page doesn’t reload. As such, after clicking a link and having the page change the Dropdown is still in its active state which is not desirable. This is because the component never umounts/mounts.

I’ve done some experimentation and put the modified _onWindowClick as follows:

_onWindowClick: function( event ){
    const dropdown_element = findDOMNode( this );
    if( event.target !== dropdown_element && !dropdown_element.contains( event.target ) && this.isActive() ){
      this.hide();
    }

    if (dropdown_element.contains( event.target ) && event.target.tagName === 'A' && !event.target.href.endsWith('#dropdown-trigger')) {
      this.hide();
    }
  }

The additional last if is says that:

  • If the event.target is contained with the Dropdown; and
  • its an A tag;
  • and that A tag does not have an href which ends with the trigger link itself;
  • then hide the menu.

This doesn’t seem like the most elegant solution but it works.

I’m happy to submit a pull request with this or with any other suggestions you might have.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Fauntleroycommented, Jun 20, 2016

I’ll look into an update which should allow something like this:

const MyDropdown = React.createClass({
  render () {
    return (<Dropdown ref="dropdown">
      <DropdownTrigger>Trigger</DropdownTrigger>
      <DropdownContent onClick={()=>{this.refs.dropdown.hide()}>
        Content
      </DropdownContent>
    </Dropdown>);
  }
});
1reaction
mjoyce91commented, Sep 21, 2017

I was getting a react/no-string-refs lint error, but took a look at this documentation about Refs and the DOM and was able to get something working:

export class AccountDropdown extends Component {
constructor(props) {
    super(props);
    this.hideDropdown = this.hideDropdown.bind(this);
  }
hideDropdown() {
    // Explicitly hide the dropdown using the built-in hide() function from react-simple-dropdown
    this.dropdown.hide();
  }
render() {
    return (
      <Dropdown className="account-dropdown" ref={(dropdown) => { this.dropdown = dropdown; }} >
        <DropdownTrigger href="/#">
        </DropdownTrigger>
        <DropdownContent>
          <div className="account-dropdown--identity account-dropdown--segment">
            <Link to="/profile" onClick={this.hideDropdown}>Profile</Link>
          </div>
        </DropdownContent>
      </Dropdown>
    );
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Avoid dropdown menu close on click inside - Stack Overflow
In the new Bootstrap 5 the solution is trivially simple. Quote from the documentation page: By default, the dropdown menu is closed when...
Read more >
How to Code a Sliding Dropdown Button using Javascript ...
Learning how to build out common web components that most web applications use is a great way to expand your javascript knowledge.
Read more >
How to create a multilevel dropdown menu in React
Multilevel menus are designed to reveal the deeply nested navigations when we click or hover over the submenu items, as shown in the...
Read more >
Solved: Show/Hide Button based on Dropdown selection - Por...
Solved: Hi I am trying to disable a Submit button based on a dropdown box in ... and Web Page, then you have...
Read more >
Dropdown | Components - BootstrapVue
Dropdowns are toggleable, contextual overlays for displaying lists of links and actions in a dropdown menu format. Show page table of contents.
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