nextui icon indicating copy to clipboard operation
nextui copied to clipboard

[Feature Request] Close Navbar.Collapse on Navbar.CollapseItem selection

Open kingjulien1 opened this issue 1 year ago • 2 comments

Is your feature request related to a problem? Please describe.

I have Next.js-Links inside the Navbar.CollapseItems inside the Navbar.Collapse element. If I navigated to one of those links, naturally, I want the Navbar.Collapse to close. As of now, it doesn't close automatically.

Describe the solution you'd like

On selection of a Navbar.CollapseItem I'd like the Navbar.Collapse to close again.

Describe alternatives you've considered

I tried to control the Navbar.Collapse with state, but that leads to unforeseen bugs - like if I wanted to set the state to false on the selection of a Navbar.CollapseItem, the navbar closes, however the icon doesn't change and the page is still not interactive due to some sort of overlay still being present.

If there is any other alternative, to accomplish this, I'm all ears.

Screenshots or Videos

No response

kingjulien1 avatar Sep 08 '22 21:09 kingjulien1

+1

iamalvisng avatar Sep 14 '22 23:09 iamalvisng

Would also love to see this implemented!

tjkyner avatar Sep 21 '22 13:09 tjkyner

Its not a pretty solution but it works for me at least until the feature gets an official implementation

https://codesandbox.io/s/navbarcollapse-demo-rb8tbk

Xariwey avatar Sep 23 '22 09:09 Xariwey

+1

brunostyle avatar Sep 26 '22 10:09 brunostyle

+1

rpsr15 avatar Sep 30 '22 10:09 rpsr15

+1

karim-alweheshy avatar Oct 02 '22 22:10 karim-alweheshy

Its not a pretty solution but it works for me at least until the feature gets an official implementation

codesandbox.io/s/navbarcollapse-demo-rb8tbk

Great solution! However, I noticed the fade in animation is lost after you click the link.

To reproduce:

  1. Click toggle (the side menu has animation)
  2. Click Menu Link 2
  3. Click toggle (the side menu fade in animation is lost)

5u4 avatar Oct 12 '22 23:10 5u4

my suggestion would be to disableAnimation in <Navbar.CollapseItem /> its the easy solution, or manually add the transitions but i dont see the point the Navbar component just came out and im sure in the next updates/patchs it will be great

Xariwey avatar Oct 14 '22 08:10 Xariwey

Definitely need this :)

bxjulien avatar Oct 18 '22 10:10 bxjulien

+1 Would love to see this implemented.

ninja-1337 avatar Nov 16 '22 14:11 ninja-1337

It's not a pretty solution as well, but here is what I did to close the Navbar.Collapse https://codesandbox.io/s/navbarcollapse-demo-forked-5nfh07.

snowplowtach avatar Nov 22 '22 12:11 snowplowtach

@snowplowtach its a prettier solution, nice one, heres with menu selection and auto select menu based on urlpath

https://codesandbox.io/s/navbarcollapse-demo-rb8tbk

Xariwey avatar Nov 22 '22 21:11 Xariwey

Ref! do good work!

LOGANLEEE avatar Dec 17 '22 19:12 LOGANLEEE

+1 I believe this should be the default behavior

Thanks @snowplowtach your solution looks good

renanlopescoder avatar Jan 15 '23 13:01 renanlopescoder

Esta es la solución que encontré y creo que puede ser la mejor:

...

import {
  Navbar, 
  NavbarBrand, 
  NavbarContent, 
  NavbarItem, 
  Link as Linke, 
  Button,
  NavbarMenuToggle,
  NavbarMenu,
  NavbarMenuItem
} from "@nextui-org/react";

...

 const handleMenuItemClick = (envurl: string) => {
    setIsMenuOpen(false);
    router.push(envurl)
  };

.....

<NavbarMenu>
        {menuItems.map((item, index) => (
          <NavbarMenuItem key={`${item}-${index}`}>
            <Linke
              className="w-full"
              color={
                index === 2 ? "warning" : index === menuItems.length - 1 ? "danger" : "foreground"
              }
              size="lg"
              onClick={() => handleMenuItemClick("/emp")}
            >
              {item}
            </Linke>
          </NavbarMenuItem>
        ))}
      </NavbarMenu>

....

Renombré Link as Linke para poder utilizar sin conflicto import Link from 'next/link' y no recargue la página, quité href de Linke y en handleMenuItemClick, claro, se pasa una variable on la url

HugDan avatar Aug 26 '23 08:08 HugDan

I solved this change a reference of link:

import { Link } from "@nextui-org/react";

And added the 'isMenuOpen' parameter to Navbar:

<Navbar
     isMenuOpen={isMenuOpen}
     onMenuOpenChange={setIsMenuOpen}
>

The menu looked like this:

<NavbarMenu className="dark pt-8 text-white">
        {navItems.map((item, index) => (
          <NavbarMenuItem key={index}>
            <Link
              className="w-full"
              href={item.href}
              onClick={() => setIsMenuOpen(false)}
            >
              {item.label}
            </Link>
          </NavbarMenuItem>
        ))}
</NavbarMenu>

RicardoIA avatar Jan 03 '24 20:01 RicardoIA