Reputation: 117
I am making a responsive sidebar with headlessui Transition.
"use client";
export default function Layout({ children }: { children: React.ReactNode }) {
const [sidebarOpen, setSidebarOpen] = useState(false);
return (
<>
<div>
<Transition.Root show={sidebarOpen} as={Fragment}>
...
</Transition.Root>
{/* Static sidebar for desktop */}
<div className="hidden lg:fixed">
<SideBar />
</div>
<div className="lg:pl-72">
<div>
<button
type="button"
className="lg:hidden"
onClick={() => setSidebarOpen(true)}
>
<Bars3Icon />
</button>
</div>
<NavBar />
<main className="py-10">
<div className="px-4 sm:px-6 lg:px-8">{children}</div>
</main>
</div>
<div>
</>
);
}
-----------------------------------------
| | button when not lg, navbar |
| sidebar |-----------------------------|
| | |
| | content |
| | |
-----------------------------------------
The sidebarOpen
in Transition and setSidebarOpen
in the button is making the whole layout client component. Any way I can extract this and make layout server component or just let it be.
Upvotes: 0
Views: 218
Reputation: 3726
The "problem" with the layout being a client component is that you have crossed the barrier and basically everything would be a child of a client.
If you really want the application to use Server Components, you need to split the layout into children where some are client. My "usual" layout looks like this
<>
<header><TopNav/></header>
<main>{children}</main>
<footer>{// whatever you want in your footer}</footer>
</>
and is a server component. Now you can have the TopNav
as a client component without affecting all the {children}
.
In this case, TopNav
is not only the bar at the top but top level navigation and can include sidebar, mobile menu, etc...
Upvotes: 2