Dropdown doesn't hide on single page web apps
See original GitHub issueI’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:
- Created 8 years ago
- Comments:7 (3 by maintainers)
Top 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 >
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
I’ll look into an update which should allow something like this:
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: