Is there a way to prevent rootlayout
from being wrapped around dashboardlayout
? Next.js v13
doc:
My file structure:
I could use route groups; however, doing so would disable the wrapping in my contact
, pricing
routes. Is there a way to prevent this from happening? I would like the home navbar across the contact and pricing page but don’t want the home page navbar in my dashboard.
I tried using route groups; however, it disabled wrapping in my pricing and contact routes.
navbar.tsx
"use client";
import { useEffect, useState } from "react";
import { Disclosure } from "@headlessui/react";
function joinClassName(...classes: string[]) {
return classes.filter(Boolean).join(" ");
}
export default function Navbar() {
const [navbar, setNavbar] = useState(false);
const changeBackground = () => {
if (window.scrollY >= 64) {
setNavbar(true);
} else {
setNavbar(false);
}
};
useEffect(() => {
changeBackground();
window.addEventListener("scroll", changeBackground);
});
return (
<Disclosure as="nav">
{({ open, close }) => (
<>
<div
className={joinClassName(
navbar || open ? "dark:bg-black bg-white" : "bg-transparent",
" fixed top-0 left-0 right-0 z-50 "
)}
></div>
</>
)}
</Disclosure>
);
}
2
Answers
After doing some digging, I managed to make it work with route groups.
File structure
/app/layout.tsx
/app/(dash)/dashboard/layout.tsx
/app/(landing)/layout.tsx
The solution from Youssouf works well. However, the dashboard route would still have the
rootlayout
CSS styles and other components, which would require me to manually add lines of code to components that I do not want shown in/dashboard
.Since your
Navbar
is a Client Component, you could avoid using Route Groups and yet be able to prevent it from being shown in/dashboard
, by usingusePathname
, like so: