Next.js Discord

Discord Forum

Drawer with Next Navigation

Unanswered
German Roughhaired Pointer posted this in #help-forum
Open in Discord
German Roughhaired PointerOP
Drawer with Next Navigation

I using router= useRouter() from next/navigation
and I wanna open a drawer on some click event but I also want the url to be /someslug and open the drawer, how can I do it?

I'm managing local state for drawer to open or close , and it works fine.

when I do router.push such page doesn't exit for now but I want to have /someslug when opening the drawer on click event

29 Replies

@German Roughhaired Pointer Drawer with Next Navigation I using router= useRouter() from next/navigation and I wanna open a drawer on some click event but I also want the url to be /someslug and open the drawer, how can I do it? I'm managing local state for drawer to open or close , and it works fine. when I do router.push such page doesn't exit for now but I want to have `/someslug` when opening the drawer on click event
like this
// drawer.tsx
'use client'

export function Drawer() {
  const pathname = usePathname()

  return pathname === 'someslug' ? <div>Drawer</div> : null
}

// page.tsx
import Link from 'next/link'

export default function Page() {

  return (
    <>
      <Link href='/someslug'>open</Link>
    </>
  )
}
and you could render the drawer component to layout or page
German Roughhaired PointerOP
// someslug/page.tsx
const SomeSlug = () => {
  return <Drawer />;
};

export default SomeSlug;

// page.tsx
      <Link href="/someslug">open</Link>

// drawer.tsx
  return pathname === "someslug" ? (
    <DrawerPortal>
      {isDrawerOpen && (
        <>
          <div
            className="fixed inset-0 bg-black opacity-50"
            onClick={closeDrawer}
          ></div>
          <div className="fixed inset-y-0 right-0 max-w-xs w-full bg-white overflow-y-auto transform translate-x-0 transition-transform ease-in-out duration-300 drawer-portal open">
            <DrawerContent onClose={closeDrawer} />
          </div>
        </>
      )}
    </DrawerPortal>
  ) : null;
hmm you want to render the slug page in a drawer?
@Ray hmm you want to render the slug page in a drawer?
German Roughhaired PointerOP
yeah, right now it goes completely to the new page, I wanna render drawer on the same page but the url should change and when drawer close back to normal url
yes intercepting route is what you want
German Roughhaired PointerOP
https://nextjs.org/docs/app/api-reference/file-conventions/default

what do we do with default.js ?
I see it is being used for intercepting routes
it should be return null in this case
German Roughhaired PointerOP
tried this says
a client-side exception has occured
German Roughhaired PointerOP
can you show the code
German Roughhaired PointerOP
// @modal/(.)t1/page.tsx
import Modal from "@/components/ui/modal";

export default function PhotoModal() {
  return <Modal>Test 2</Modal>;
}


// t1/page.tsx


const TEST1 = () => {
  return <div>TEST1</div>;
};

export default TEST1;


// app/page.tsx
<Link href="/t1">open test</Link>


And this modal is from the github mentioned on the docs
folder structure
app > @modal > (.)t1 > page.tsx
app > t1 > page.tsx
localhost/t1 does work
what is not working?
German Roughhaired PointerOP
same page drawer/modal opening is not working
Application error: a client-side exception has occurred (see the browser console for more information).
does it work if you rename @drawer to _@drawer and remove the drawer props in layout
@Ray does it work if you rename `@drawer` to `_@drawer` and remove the drawer props in layout
German Roughhaired PointerOP
nope
btw i had forgotten to add modal as props on layout
but did not work
restart the dev server
@Ray restart the dev server
German Roughhaired PointerOP
thanks!
does work
nice
so I had forgotten to add modal/drawer to layout
I'm using react portal for drawer, where would I add and how would I add it for drawer? for Intercepting routes
you dont need it for intercept route
just render the div with css like the modal component