[Bug]: Modal closes automatically with Next.JS
See original GitHub issueWhat package within Headless UI are you using?
What version of that package are you using?
1.1.1
What browser are you using?
Chrome 90
Reproduction repository
https://codesandbox.io/s/headless-ui-next-issue-l17wk?file=/pages/page2.js
Describe your issue
Basic setup: there are two pages, each page has a modal. So modals should be independent from pages. But if you open and close modal on page1
then switch to page2
and click on Open dialog
button, onClose
is called for whatever reason right after modal opens. You can see this in console.
Firstly, i thought that initialFocus
that is pointing to cancel button is pressed on open. But that’s not the case here. closeModal
is called from onClose
prop.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:15
- Comments:10
Top Results From Across the Web
How to close modal window after clicking a link - Stack Overflow
When a user clicks a link in the navigation using the NextJS Link component, the popup doesn't automatically close—they have to close it ......
Read more >Advanced Features: Error Handling - Next.js
It is a modal that covers the webpage. It is only visible when the development server runs ... Fixing the error will automatically...
Read more >Modal component with Next.js - Dev Recipes
In this tutorial, I will show you how to create a basic Modal component from scratch in a Next.js application.
Read more >Modal · Bootstrap v5.2
Modals are built with HTML, CSS, and JavaScript. · Clicking on the modal “backdrop” will automatically close the modal. · Bootstrap only supports...
Read more >Modal - Ant Design
Modal.destroyAll() will destroy all confirmation modal dialogs. Usually, you can use it in router change event to destroy confirm modal dialog automatically.
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
Somewhat related issue where I’m trying to render a
Dialog
on one page(ie. page1) and anotherDialog
on another page(ie. page2).Opening the
Dialog
works fine on page1. However, when navigating from page 1 to page2, theDialog
fails to open on this page. It seems the cause of this problem is theheadlessui-portal-root
element is missing from the DOM which is why theDialog
cannot open.When navigating back from page2 to page1, the
headlessui-portal-root
element shows up again.I’m not sure why this is happening, would love to have an explanation.
Here’s a GIF of the issue using @TheAsda 's sandbox:
SSR playing trickery here. This hook solved it for me:
Use it like so