0

I'm working on an admin panel in Next.js and I'm trying to achieve the following layout:

A fixed sidebar on the left side that remains visible as the user scrolls through the content. A fixed header at the top that also stays visible during scrolling. The sidebar should be conditionally renderable based on certain criteria (e.g., different pages may or may not have the sidebar). I'm having trouble with the following issues:

Fixed Positioning: I'm not sure how to correctly set the sidebar and header to be fixed without affecting the layout of the main content area. When I try to make the sidebar fixed, it overlaps with the header or causes layout issues with the children components.

Conditional Rendering: I want to render the sidebar only on specific pages (for example, on dashboard pages but not on login or other pages). What's the best way to handle this in Next.js?

I’m using Tailwind CSS for styling, but I'm open to suggestions for how to structure and style this in any way that works best.

My Current Issues:

How can I make sure that both the sidebar and header are fixed, but the main content scrolls independently without causing layout issues? What is the recommended approach for conditionally rendering the sidebar in a Next.js app? Any help or guidance would be greatly appreciated!

enter image description here

This is what I have done and

enter image description here

This is what I'm expecting where sidebar is scrollable and fixed and header is also fixed

1 Answer 1

0

To achieve the layout you're aiming for in a Next.js admin panel with a fixed sidebar and header using Tailwind CSS, follow these steps:

1. Fixed Positioning: Header and Sidebar

The key is to structure your layout using CSS Grid or Flexbox, ensuring that the header and sidebar are fixed while the main content remains scrollable.

Layout Structure:

<div className="flex h-screen">
  {showSidebar && (
    <aside className="fixed top-0 left-0 h-full w-64 bg-gray-800 text-white">
      {/* Sidebar content */}
    </aside>
  )}

  <div className={`flex-1 ${showSidebar ? 'ml-64' : ''}`}>
    <header className="fixed top-0 left-0 w-full h-16 bg-gray-900 text-white z-10">
      {/* Header content */}
    </header>

    <main className="pt-16 overflow-y-auto h-full">
      {/* Main content */}
    </main>
  </div>
</div>

Explanation:

  • Sidebar: Positioned using fixed, taking up the full height of the screen (h-full). The sidebar is 64px wide (w-64).
  • Header: Also positioned using fixed, occupying the full width of the screen (w-full) and with a height of 16px (h-16). It has a higher z-index (z-10) to ensure it stays above the sidebar and main content.
  • Main Content: It has ml-64 to account for the sidebar width when the sidebar is visible. The padding-top (pt-16) offsets the height of the fixed header. The overflow-y-auto allows the main content to scroll independently.

2. Conditional Rendering of the Sidebar

In Next.js, you can conditionally render the sidebar based on the current route. One approach is to use the useRouter hook from next/router and check the current path.

Example:

import { useRouter } from 'next/router';

const Layout = ({ children }) => {
  const router = useRouter();
  
  const showSidebar = router.pathname.startsWith('/dashboard');
  
  return (
    <div className="flex h-screen">
      {showSidebar && (
        <aside className="fixed top-0 left-0 h-full w-64 bg-gray-800 text-white">
          {/* Sidebar content */}
        </aside>
      )}

      <div className={`flex-1 ${showSidebar ? 'ml-64' : ''}`}>
        <header className="fixed top-0 left-0 w-full h-16 bg-gray-900 text-white z-10">
          {/* Header content */}
        </header>

        <main className="pt-16 overflow-y-auto h-full">
          {children}
        </main>
      </div>
    </div>
  );
};

export default Layout;

3. Handling Layouts in Next.js

You can wrap your pages in a layout component like the one above. In Next.js, you can specify layouts on a per-page basis:

In _app.js:

import Layout from '../components/Layout';

function MyApp({ Component, pageProps }) {
  const getLayout = Component.getLayout || ((page) => <Layout>{page}</Layout>);
  
  return getLayout(<Component {...pageProps} />);
}

export default MyApp;

In a Page Component:

const DashboardPage = () => {
  return (
    <div>
      {/* Dashboard content */}
    </div>
  );
};

DashboardPage.getLayout = function getLayout(page) {
  return <Layout>{page}</Layout>;
};

export default DashboardPage;

Final Thoughts:

  • Fixed Positioning: Ensure that you account for the space taken by the fixed header and sidebar when rendering the main content.
  • Conditional Rendering: Use useRouter or layout pattern to conditionally render the sidebar based on the current route.
  • Tailwind CSS: Tailwind provides utility classes that make it easy to manage layout and positioning without writing custom CSS.

This structure should give you the fixed sidebar and header with scrollable main content while allowing for the sidebar to be conditionally rendered based on the route.

Sign up to request clarification or add additional context in comments.

1 Comment

Please accept this answer if it was helpful to you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.