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.

[Bug]: Modal closes automatically with Next.JS

See original GitHub issue

What package within Headless UI are you using?

@headlessui/react

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:closed
  • Created 2 years ago
  • Reactions:15
  • Comments:10

github_iconTop GitHub Comments

6reactions
andrewli-cacommented, May 7, 2021

Somewhat related issue where I’m trying to render a Dialog on one page(ie. page1) and another Dialog on another page(ie. page2).

Opening the Dialog works fine on page1. However, when navigating from page 1 to page2, the Dialog fails to open on this page. It seems the cause of this problem is the headlessui-portal-root element is missing from the DOM which is why the Dialog 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:

headlessui-portal

2reactions
ldnshcommented, Aug 7, 2021

SSR playing trickery here. This hook solved it for me:

// useHasMounted.ts

export default function useIsMounted() {
  const [isMounted, setIsMounted] = React.useState(false);
  React.useEffect(() => {
    setIsMounted(true);
  }, []);
  return isMounted;
}

Use it like so

// page.tsx (NextJS)

import useIsMounted from '@hooks/useIsMounted'

 const isMounted = useIsMounted()

  if (!isMounted) {
    return null
  }

Read more comments on GitHub >

github_iconTop 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 >

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