ui: [bug]: Sidebar seems incompatable with a header navigation
Describe the bug
I am building a dashboard and desire to have a header nav bar as well as the sidebar.
Problem:
Ideal:
All examples show the popular use case of a sidebar under a header.
Please let me know if my understanding on this is correct. As i write it may be the case: 1. this pattern may be redundant as the side bar may need to be used for primary navigation. 2. I may need to incorporate the site logo in the head of the sidebar
Affected component/components
Sidebar
How to reproduce
- Set up a new next.js application
- clear out the default page
- add full width header with a logo on the far left and a login button on the far right
- implement the header as presented in the documentation
The sidebar overlaps the header covering the site logo vs being a in the div below on the left side with the content on the right side all under the header nav.
Codesandbox/StackBlitz link
https://stackblitz.com/edit/stackblitz-starters-vqqoj5
Logs
No response
System Info
Stackblitz on Firefox and Chrome
Before submitting
- I’ve made research efforts and searched the documentation
- I’ve searched for existing issues
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 30
- Comments: 38 (9 by maintainers)
Commits related to this issue
- feat(sidebar blocks): adds sidebar-16 block with components and styles (#5889) Introduces a new sidebar-16 block with an inset sidebar and site header navigation. Includes components for main navigat... — committed to shadcn-ui/ui by Jacksonmills 2 years ago
- feat(sidebar blocks): adds sidebar-16 block with components and styles (#5889) Introduces a new sidebar-16 block with an inset sidebar and site header navigation. Includes components for main navigat... — committed to base-buzz/ui by Jacksonmills 2 years ago
- feat(sidebar blocks): adds sidebar-16 block with components and styles (#5889) Introduces a new sidebar-16 block with an inset sidebar and site header navigation. Includes components for main navigat... — committed to democrata-labs/loki-ui by Jacksonmills 2 years ago
This is expected with the fixed sidebar (not a bug). Let me add an example for doing navbar and sidebar.
@shadcn, have you had a chance to write this up? I’m running into the same issue here.
My solution is just putting
w-autoandstickylike this:Is there an official recommended solution for this?
Will have something for you guys to review in the next couple days!
Not released yet no
Take a look at my PR, it introduces the new
sidebar-16block. This block features an inset-style sidebar with site header navigation, designed to match a similar style to: https://shadcn-10-30-24.vercel.app/. CLI support will allow adding it like any other sidebar block. ✨Hey @youngclaude and @shadcn,
I attempted the header and sidebar integration based on the discussion: https://shadcn-10-30-24.vercel.app/
I made several modifications to the
Sidebarcomponent to resolve the overlapping issue with the header, and the solution seems to work effectively for my setup. Here are the key adjustments I implemented:Added a top offset to the
Sidebar:This ensures the sidebar is positioned below the header by offsetting it based on the header’s height, preventing any overlap.
Defined the header height as a CSS variable within the
bodytag:This allows us to reference the header height dynamically across different components.
Modified
SidebarInsetto factor in the header height:The
SidebarInsetadjustment ensures that the sidebar height takes into account the space occupied by the header, maintaining proper positioning without overlap.Modified
ScrollAreato factor in the double header height:The
ScrollAreausesheader-height * 2because of the inner header within theSidebarInset, ensuring that the scrollable content respects both the main header and the additional inset header.Set a high z-index for the
NavigationMenuwithin the root layout header:This ensures that the navigation menu is displayed above the sidebar when hovering over the top header, even if the sidebar is open.
⚠️ These modifications might vary slightly depending on the sidebar variant being used. For this example, I used the ‘inset’ variant, so behavior may differ with other variants.
You can check out the repository for further details: shadcn-10-30-24.
If any of these adjustments seem useful, feel free to incorporate them into the project or let me know if you need further details. I’d be happy to collaborate or refine any part of this solution.
I’m working on sidebar-16 that will come with a sticky header nav
@Jacksonmills Thanks for you’re time on this. Fixed header + (fixed sidebar - header height) ≅ gives us the intended dashboard effect. I’ll float another idea: there may be an opportunity to handle navigation entirely by taking in a nav bar as a prop/ child for the a parent ‘Group’/ ‘Provider’ component.
Precedence for nesting UI elements already exists in ShadCN in elements like the RadioGroup, ResizablePanelGroup and TooltipProvider.
These elements show support for the pattern of not just deriving a stand-alone component but using a parent provider to ensure desired behavior of child components.
An example solution, using @Jacksonmills 's example, could be:
A solution like this would be great for dashboards, documentation pages and admin panel use-cases. I can add a
featurerequest of @shadcn has interest.Same issue for me as well. We have a full width header with a toggle (ignore the button alignment)
. Seems like the panel that comes out on mobile doesn’t respect the top header either.
yup! should just be
npx shadcn@latest add sidebar-16Hey 👋 same for me 😢 , I would have preferred the sidebar to fit its container and not overlap everything, need to completely override its styling to make it work…
first time trying the sidebar from shadcn i in my layout created a appsidebar which is in a div and that sidebar is acting very different from the div settings i set for sidebar. its working independent from the div i wrapped it around. its overlapping the header and footer . i am sad this component is half baked need more work its not ready to use properly for now like other components .
So I edited the Sidebar component itself. Offsets and other approaches don’t quite fit my needs. Here’s the part:
Since I’m using the inset variant, I tweaked this portion into:
Now I have it respect its parent container’s boundaries.
Just wanted to add to the conversation as I ended up in this discussion having had the same issues as everyone else. I tried a bunch of things but couldn’t get it to work with it not overlaying the footer. The offset for the navbar is easy enough, but it would be nice to have the component be able to work within the confines of both a global nav and footer.
For this project I just ended up building the sidebar myself as it was a pretty simple one and I am using it just for the accounts page. But, I would like to use the shadcn sidebar for other projects, but will run into the same issue. Hopefully there can be an option for this at some point as it seems like a very common use case to me and most of my projects I work on have a footer that the client wants.
I saw one comment suggesting taking in the nav and footer as props, which at first seemed like a great solution, but upon thinking and unless this is to somehow adjust the sidebar to fit within the bounds of those, I am not sure it will work. I assume taking in the props would be to render the Nav, Sidebar, Footer, but this would end up doubling printing them assuming the global layout has nav and footer already. I could see it working for uses where you wanted it on all routes, but often I only want a sidebar on certain routes.
I could easily be completely wrong also.
Wouldn’t it be easier to just have an option where it acts like an aside within the body of the layout like I do when building one? Just for the container itself, and then still have all the amazing options available to work within that container.
@Jacksonmills Thanks for the effort of creating an example! I had a look at your PR and it seems your solution requires a fixed header height, which is then used to manipulate the top and padding-bottom css properties of the Sidebar. While this does work for the specific use-case of having a fixed height header, it does not work for wrapping the Sidebar Provider and its content in a container that is dynamically sized in height, if I am not mistaken?
Would really love a solution to this 🙏
yep, another user was able to fix in the issue related. Check out the fix here https://github.com/shadcn-ui/ui/pull/6782
sidebar-16 to the rescue!
How can I use sidebar-16 in my project? Do I need to run a command similar to “npx shadcn@latest add sidebar”?
Probably for simplicity sake, I assume you can change the sidebar style just fine though?
PR seems to be merged, but the example on the live website no longer has the inset sidebar, does anyone know why?
I ended up here because I also tried to use the sidebar together with a header. I’m sure there are many others facing the same issue. It’s unfortunate that the PR hasn’t been merged yet, and I have to create my own sidebar.
@navjot101 It’s due to the
fixedclass. That means it’s positioned relative to the window, not any wrapper div https://tailwindcss.com/docs/position#fixed-positioning-elements. I ran into a similar problem. It’s why it overlaps.It would be nice if there was an option to toggle
fixedbuilt in to the provided component. For now you have to edit it. For example notice how @charlietlamb has changedfixedtoabsolute. That way it’s instead positioned relative to its parent (well, that which hasrelative).This works great for me - had to add h-full to the rail to keep it working
I’ve also stumbled on this issue - while the offset hack works, it breaks overscroll (at least in Chrome on macOS) and looks disjointed when this happens. Having the option to use a variant with non-fixed positioning would be the great.
https://github.com/Jacksonmills/shadcn-10-30-24Thank you, I tried this, navbar is okay, but the footer and sidebar overlap. How to put the sidebar inside the block?I tried it with DataTable (
https://ui.shadcn.com/docs/components/data-table), it breaks the table’s responsiveness.