Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to keep menu open when clicking outside? #1422

Open
tdsoundation opened this issue Oct 16, 2024 · 4 comments
Open

Possible to keep menu open when clicking outside? #1422

tdsoundation opened this issue Oct 16, 2024 · 4 comments
Labels
question Ask for help and support

Comments

@tdsoundation
Copy link

React/React-dom version: 18
React-Menu version: 4.2.2

Hi, is it possible to keep the menu open when clicking outside of the menu? I'm using a ControlledMenu btw, but I'm not sure how to stop onClose from being called.

Thanks

@tdsoundation tdsoundation added the question Ask for help and support label Oct 16, 2024
@szhsin
Copy link
Owner

szhsin commented Oct 17, 2024

The onClose event cannot be prevented from being called, but you can decide whether to close the menu.

For example, when clicking outside the menu, the reason in the event will be 'blur'. You can keep the menu open by not calling the state setter. https://codesandbox.io/p/sandbox/react-menu-1422-nml5q8

@tdsoundation
Copy link
Author

Yup, that gets the job done :) thank you

@tdsoundation
Copy link
Author

ok, one issue though, the toggle doesn't close the dropdown when clicked even though it's being passed the handlers from useClick:

const [open, setOpen] = useState(false)
const anchorProps = useClick(open, () => setOpen(true))

return (
  <>
       <button {...anchorProps}>Toggle</button>

       <ControlledMenu...
  </>
)

is there a way to make it close the menu when clicked?

@szhsin
Copy link
Owner

szhsin commented Oct 17, 2024

The useClick hook was designed to always close the menu during the onClose event. In this case, it's perfectly acceptable to toggle the isOpen state when the toggle button is clicked: setOpen((o) => !o)
Have updated the example as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Ask for help and support
Projects
None yet
Development

No branches or pull requests

2 participants