0

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.

Evilolipop
  • 109
  • 5
  • 2
    Does this answer your question? [Does a client layout make everything client? If so, how to add client interactivities to it while keeping it a server component?](https://stackoverflow.com/questions/75801266/does-a-client-layout-make-everything-client-if-so-how-to-add-client-interactiv) – Youssouf Oumar Jul 21 '23 at 08:10

1 Answers1

1

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...

grekier
  • 2,322
  • 1
  • 16
  • 23