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.

Control closing the menu if clicking outside

See original GitHub issue
  • downshift version: 1.31.14
  • node version: 9
  • npm (or yarn) version: 1.6.0

I’m looking for a way to prevent the menu from closing when clicking outside the menu.

Right now when you click outside the menu , handleToggle is always called and isOpen set to false closing the menu. I’m looking to prevent this where I want to actually keep the menu open in some cases , is there anyway to achieve this ?

handleToggle = ({ isOpen, type }) => {
    this.setState({ open: isOpen })
}

<Downshift
   defaultIsOpen={open}
   isOpen={open}
   onStateChange={this.handleToggle}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, May 6, 2018

Hi! Look at the docs for how to use the stateReducer 👌

0reactions
mnpennercommented, Aug 25, 2019

I’m having a similar issue, but I don’t understand the solution here.

My component looks like this:

I want it so that when you click on the <input>, it won’t close, but if you click outside then it will. I’m also using a portal.

I’ve prevented the closing with a stateReducer:

function stateReducer(state: DownshiftState<Item>, changes: StateChangeOptions<Item>) {
    console.log('state:',state, 'changes:',changes);
    switch (changes.type) {
        case Downshift.stateChangeTypes.mouseUp:
            return {};
        default:
            return changes;
    }
}

But now it won’t close when I click outside either. What’s the solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - How to close menu when clicking outside of the div?
I want to be able to close a popup menu when the user clicks outside. How can I accomplish this in pure JS?...
Read more >
Closing on outside click | Kitty Giraudel
As part of the navigation on the Gorillas website, we needed a way to close the menu when clicking outside of it or...
Read more >
jQuery: click outside to close menu - CodePen
jQuery: click outside to close menu. Click the button to toggle the dropodown menu. Then click outside dropdown menu to close. Toggle menu....
Read more >
How to avoid dropdown menu to close menu items on clicking ...
In this article, We will use stropPropagation method to prevent the dropdown menu from closing the menu list.
Read more >
Dismissing a React Dropdown Menu by Clicking Outside its ...
In the toggleDropdown method, after opening the dropdown menu via setState({dropdownVisible: true}) when clicking the trigger button, the NativeClickListener ...
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