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

[Question]: On Disclosure.Panel, clicking a navigation item reloads whole page rather than updating DOM content #453

Closed
FullStackJohnMack opened this issue Apr 26, 2021 · 8 comments

Comments

@FullStackJohnMack
Copy link

What package within Headless UI are you using?

@headlessui/react

What version of that package are you using?

v1.0.0

What browser are you using?

Chrome

Reproduction repository

https://www.youtube.com/watch?v=n5MFF_ViaCg

Describe your issue

When using Disclosure.Panel below, a click on a navigation item reloads the whole page. When I swapped out the tag for a react-router-dom Link component tag, the DOM is correctly reloaded with content of the destination link (and no page reload). However, the Disclosure.Panel stays open.

I see similar issues like this with Vue but wasn't able to correctly apply any fixes.

<Disclosure.Panel as="nav" className="sm:hidden" aria-label="Global" > <div className="pt-2 pb-3 px-2 space-y-1"> {navigation.map((item) => ( <a key={item.name} href={item.href} className={classNames( item.current ? "bg-gray-100 text-gray-900" : "text-gray-900 hover:bg-gray-50 hover:text-gray-900", "block rounded-md py-2 px-3 text-base font-medium" )} aria-current={item.current ? "page" : undefined} > {item.name} </a> ))} </div>

and the Link usage is:

<Disclosure.Panel as="nav" // className={disclosurePanelVisibility} className="sm:hidden" aria-label="Global" > <div className="pt-2 pb-3 px-2 space-y-1"> {navigation.map((item) => ( <Link key={item.name} to={item.href} // onClick={() => setDisclosurePanelVisibility("hidden")} className={classNames( item.current ? "bg-gray-100 text-gray-900" : "text-gray-900 hover:bg-gray-50 hover:text-gray-900", "block rounded-md py-2 px-3 text-base font-medium" )} aria-current={item.current ? "page" : undefined} > {item.name} </Link> ))} </div>

@FullStackJohnMack
Copy link
Author

I apologize for the format of the code... I'm not sure how to clean it up.

@FullStackJohnMack FullStackJohnMack changed the title [Bug]: On Disclosure.Panel, clicking a navigation item reloads whole page rather than updating DOM content [Feature]: On Disclosure.Panel, clicking a navigation item reloads whole page rather than updating DOM content Apr 26, 2021
@FullStackJohnMack FullStackJohnMack changed the title [Feature]: On Disclosure.Panel, clicking a navigation item reloads whole page rather than updating DOM content [Question]: On Disclosure.Panel, clicking a navigation item reloads whole page rather than updating DOM content Apr 26, 2021
@FullStackJohnMack
Copy link
Author

Solved: It appears that I can wrap the link text in a Disclosure.Button tag and it closes the menu.

@FullStackJohnMack
Copy link
Author

What a trainwreck of a post I made... I just want to make sure I'm handling this correctly.

@RobinMalfait
Copy link
Member

Hey! Thank you for your bug report!
Much appreciated! 🙏

I'm not 100% what you are asking here. Currently the only way to open/close a Disclosure is by clicking the actual Disclosure.Button component.

That said, your main question seems to be related to a page refresh. The Disclosure component doesn't handle anything regarding your routing. If your page refreshes then you probably have an issue where you use a Link from the router library you are using.

@depsimon
Copy link

Had the same issue and the same solution, for future reference here's what to change (in this case it's a Next.js project)

Issue : (triggers page reloading when clicking)

<Link href="/clients" passHref={true}>
  <Disclosure.Button
    as="a"
    className="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 block pl-3 pr-4 py-2 border-l-4 text-base font-medium"
  >
    Clients
  </Disclosure.Button>
</Link>

Solution :

<Link href="/clients">
    <a
      className="border-transparent text-gray-500 hover:border-gray-300 hover:text-gray-700 block pl-3 pr-4 py-2 border-l-4 text-base font-medium"
    >
      <Disclosure.Button>
        Clients
      </Disclosure.Button>
    </a>
  )}
</ActiveLink>

@MJGTwo
Copy link

MJGTwo commented Jan 27, 2022

thanks for posting this, the example at https://tailwindui.com/components/application-ui/navigation/navbars has the version of the code that makes it reload everything, so maybe someone can fix this on the website. @RobinMalfait who should be ask to do that?

@RobinMalfait
Copy link
Member

@depsimon I would recommend to create a custom Link component instead, because now you will be rendering a button inside an a tag which isn't ideal.

@MJGTwo I'll make sure to add this page from the Menu component to other components as well. Checkout: https://headlessui.dev/react/menu#integrating-with-next-js

@depsimon
Copy link

Thanks for clarifying this @RobinMalfait !

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

No branches or pull requests

4 participants