on navbar add animation when drawer is open, sepearte the drawer in custom component. also use shadcn sidebar to make it.
index.tsx:
'use client'
import { useState } from 'react';
import { Menu, X } from 'lucide-react';
import Image from 'next/image';
import Link from 'next/link';
import LanguageSelector from './LanguageSelector';
A Sidebar component is composed of the following parts:
code
SidebarProvider - Handles collapsible state.
Sidebar - The sidebar container.
SidebarHeader and SidebarFooter - Sticky at the top and bottom of the sidebar.
SidebarContent - Scrollable content.
SidebarGroup - Section within the SidebarContent.
SidebarTrigger - Trigger for the Sidebar.
Sidebar Structure
Usage
app/layout.tsx
import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
import { AppSidebar } from "@/components/app-sidebar"
export default function Layout({ children }: { children: React.ReactNode }) {
return (
{children}
)
}
components/app-sidebar.tsx
import {
Sidebar,
SidebarContent,
SidebarFooter,
SidebarGroup,
SidebarHeader,
} from "@/components/ui/sidebar"
export function AppSidebar() {
return (
)
}
Let's start with the most basic sidebar. A collapsible sidebar with a menu.
Add a SidebarProvider and SidebarTrigger at the root of your application.
app/layout.tsx
import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
import { AppSidebar } from "@/components/app-sidebar"
export default function Layout({ children }: { children: React.ReactNode }) {
return (
{children}
)
}
Create a new sidebar component at components/app-sidebar.tsx.
components/app-sidebar.tsx
import { Sidebar, SidebarContent } from "@/components/ui/sidebar"
export function AppSidebar() {
return (
)
}
Now, let's add a SidebarMenu to the sidebar.
We'll use the SidebarMenu component in a SidebarGroup.
components/app-sidebar.tsx
import { Calendar, Home, Inbox, Search, Settings } from "lucide-react"
The components in sidebar.tsx are built to be composable i.e you build your sidebar by putting the provided components together. They also compose well with other shadcn/ui components such as DropdownMenu, Collapsible or Dialog etc.
If you need to change the code in sidebar.tsx, you are encouraged to do so. The code is yours. Use sidebar.tsx as a starting point and build your own.
In the next sections, we'll go over each component and how to use them.
The SidebarProvider component is used to provide the sidebar context to the Sidebar component. You should always wrap your application in a SidebarProvider component.
Props
Name Type Description
defaultOpen boolean Default open state of the sidebar.
open boolean Open state of the sidebar (controlled).
onOpenChange (open: boolean) => void Sets open state of the sidebar (controlled).
Width
If you have a single sidebar in your application, you can use the SIDEBAR_WIDTH and SIDEBAR_WIDTH_MOBILE variables in sidebar.tsx to set the width of the sidebar.
This will handle the width of the sidebar but also the layout spacing.
Keyboard Shortcut
The SIDEBAR_KEYBOARD_SHORTCUT variable is used to set the keyboard shortcut used to open and close the sidebar.
To trigger the sidebar, you use the cmd+b keyboard shortcut on Mac and ctrl+b on Windows.
You can change the keyboard shortcut by updating the SIDEBAR_KEYBOARD_SHORTCUT variable.
components/ui/sidebar.tsx
const SIDEBAR_KEYBOARD_SHORTCUT = "b"
Persisted State
The SidebarProvider supports persisting the sidebar state across page reloads and server-side rendering. It uses cookies to store the current state of the sidebar. When the sidebar state changes, a default cookie named sidebar:state is set with the current open/closed state. This cookie is then read on subsequent page loads to restore the sidebar state.
To persist sidebar state in Next.js, set up your SidebarProvider in app/layout.tsx like this:
app/layout.tsx
import { cookies } from "next/headers"
import { SidebarProvider, SidebarTrigger } from "@/components/ui/sidebar"
import { AppSidebar } from "@/components/app-sidebar"
You can change the name of the cookie by updating the SIDEBAR_COOKIE_NAME variable in sidebar.tsx.
components/ui/sidebar.tsx
const SIDEBAR_COOKIE_NAME = "sidebar:state"
The main Sidebar component used to render a collapsible sidebar.
import { Sidebar } from "@/components/ui/sidebar"
export function AppSidebar() {
return
}
Props
Property Type Description
side left or right The side of the sidebar.
variant sidebar, floating, or inset The variant of the sidebar.
collapsible offcanvas, icon, or none Collapsible state of the sidebar.
side
Use the side prop to change the side of the sidebar.
Available options are left and right.
import { Sidebar } from "@/components/ui/sidebar"
export function AppSidebar() {
return
}
variant
Use the variant prop to change the variant of the sidebar.
Available options are sidebar, floating and inset.
import { Sidebar } from "@/components/ui/sidebar"
export function AppSidebar() {
return
}
collapsible
Use the collapsible prop to make the sidebar collapsible.
Available options are offcanvas, icon and none.
import { Sidebar } from "@/components/ui/sidebar"
export function AppSidebar() {
return
}
Prop Description
offcanvas A collapsible sidebar that slides in from the left or right.
icon A sidebar that collapses to icons.
none A non-collapsible sidebar.
The useSidebar hook is used to control the sidebar.
import { useSidebar } from "@/components/ui/sidebar"
Property Type Description
state expanded or collapsed The current state of the sidebar.
open boolean Whether the sidebar is open.
setOpen (open: boolean) => void Sets the open state of the sidebar.
openMobile boolean Whether the sidebar is open on mobile.
setOpenMobile (open: boolean) => void Sets the open state of the sidebar on mobile.
isMobile boolean Whether the sidebar is on mobile.
toggleSidebar () => void Toggles the sidebar. Desktop and mobile.
Use the SidebarHeader component to add a sticky header to the sidebar.
The following example adds a to the SidebarHeader.
A sidebar header with a dropdown menu.
components/app-sidebar.tsx
Use the SidebarFooter component to add a sticky footer to the sidebar.
The following example adds a to the SidebarFooter.
A sidebar footer with a dropdown menu.
components/app-sidebar.tsx
export function AppSidebar() {
return (
Username
Account
Billing
Sign out
)
}
SidebarContent
The SidebarContent component is used to wrap the content of the sidebar. This is where you add your SidebarGroup components. It is scrollable.
import { Sidebar, SidebarContent } from "@/components/ui/sidebar"
export function AppSidebar() {
return (
)
}
Use the SidebarGroup component to create a section within the sidebar.
A SidebarGroup has a SidebarGroupLabel, a SidebarGroupContent and an optional SidebarGroupAction.
A sidebar group.
import { Sidebar, SidebarContent, SidebarGroup } from "@/components/ui/sidebar"
The SidebarMenu component is used for building a menu within a SidebarGroup.
A SidebarMenu component is composed of SidebarMenuItem, SidebarMenuButton, and components.
Sidebar Menu
Here's an example of a SidebarMenu component rendering a list of projects.
A sidebar menu with a list of projects.
The SidebarMenuButton component is used to render a menu button within a SidebarMenuItem.
Link or Anchor
By default, the SidebarMenuButton renders a button but you can use the asChild prop to render a different component such as a Link or an a tag.
Icon and Label
You can render an icon and a truncated label inside the button. Remember to wrap the label in a .
isActive
Use the isActive prop to mark a menu item as active.
The SidebarMenuAction component is used to render a menu action within a SidebarMenuItem.
This button works independently of the SidebarMenuButton i.e you can have the as a clickable link and the as a button.
Here's an example of a SidebarMenuAction component rendering a DropdownMenu.
A sidebar menu action with a dropdown menu.
The SidebarMenuSub component is used to render a submenu within a SidebarMenu.
Use and to render a submenu item.
A sidebar menu with a submenu.
To make a SidebarMenu component collapsible, wrap it and the SidebarMenuSub components in a Collapsible.
A collapsible sidebar menu.
The SidebarMenuBadge component is used to render a badge within a SidebarMenuItem.
A sidebar menu with a badge.
The SidebarMenuSkeleton component is used to render a skeleton for a SidebarMenu. You can use this to show a loading state when using React Server Components, SWR or react-query.
Use the open and onOpenChange props to control the sidebar.
A controlled sidebar.
export function AppSidebar() {
const [open, setOpen] = React.useState(false)
return (
)
}
To enhance your Navbar with an animated drawer using shadcn/ui's Sidebar, follow these steps:
Set Up SidebarProvider in app/layout.tsx
Create a Custom AppSidebar Component
Update Navbar to Use SidebarTrigger and Remove Local State
Add Animations to the Sidebar and Navbar
Finalize and Test
Let's go through each step in detail.
1. Set Up SidebarProvider in app/layout.tsx
First, ensure that the SidebarProvider from shadcn/ui is set up at the root of your application. This allows the sidebar's state to be managed globally.
SidebarProvider: Wraps the entire application to provide sidebar context.
AppSidebar: Your custom sidebar component (to be created in the next step).
Navbar: The navigation bar at the top.
main: The main content area where your pages will render.
2. Create a Custom AppSidebar Component
Next, create a separate component for the sidebar. This component will handle the drawer's content and animations using shadcn/ui's Sidebar.
Create components/AppSidebar.tsx
tsx
// components/AppSidebar.tsx
'use client'
import { Sidebar, SidebarContent, SidebarFooter, SidebarGroup, SidebarHeader, SidebarSeparator } from "@/components/ui/sidebar"
import { Calendar, Home, Inbox, Search, Settings } from "lucide-react"
import Link from "next/link"
import LanguageSelector from "./LanguageSelector"
const navItems = [
{ label: 'Home', href: '/', icon: Home },
{ label: 'Services', href: '/services', icon: Search },
{ label: 'Offers', href: '/offers', icon: Inbox },
{ label: 'About us', href: '/about', icon: Calendar },
{ label: 'Jobs', href: '/jobs', icon: Settings },
{ label: 'Contact us', href: '/contact', icon: Search },
]
export function AppSidebar() {
return (
<Sidebar
side="left" // or "right" based on your preference
variant="sidebar" // options: "sidebar", "floating", "inset"
collapsible="offcanvas" // options: "offcanvas", "icon", "none"
>
<SidebarHeader className="p-4">
{/* You can add a header here, like a logo or title */}
<Link href="/">
<img src="/logo-placeholder.png" alt="Logo" className="w-32 h-auto object-contain" />
</Link>
</SidebarHeader>
<SidebarSeparator />
<SidebarContent className="px-2 py-4">
<SidebarGroup>
<SidebarGroupLabel className="text-lg font-semibold">Navigation</SidebarGroupLabel>
<SidebarGroupContent className="mt-2 space-y-1">
{navItems.map((item) => (
<SidebarMenuItem key={item.label} label={item.label} href={item.href} icon={item.icon} />
))}
</SidebarGroupContent>
</SidebarGroup>
</SidebarContent>
<SidebarSeparator />
<SidebarFooter className="p-4">
{/* You can add footer content here, like language selector or user info */}
<LanguageSelector />
</SidebarFooter>
</Sidebar>
)
}
interface SidebarMenuItemProps {
label: string
href: string
icon: React.ComponentType<any>
}
const SidebarMenuItem: React.FC<SidebarMenuItemProps> = ({ label, href, icon: Icon }) => (
<Link href={href}>
<div className="flex items-center p-2 rounded-md hover:bg-gray-100 transition-colors cursor-pointer">
<Icon className="w-5 h-5 mr-3 text-gray-600" />
<span className="text-base font-medium text-gray-700">{label}</span>
</div>
</Link>
)
Explanation:
Sidebar: Configured with side, variant, and collapsible props to define its behavior and appearance.
SidebarHeader: Contains the logo. You can customize this as needed.
SidebarContent: Holds the navigation items.
SidebarGroup: Groups related navigation items.
SidebarMenuItem: A custom component to render each navigation link with an icon.
SidebarFooter: Contains the LanguageSelector. You can add more footer content here.
3. Update Navbar to Use SidebarTrigger and Remove Local State
Replace the existing mobile menu logic in your Navbar with shadcn/ui's SidebarTrigger. This will allow you to control the sidebar without managing local state.
Update components/Navbar.tsx
tsx
Explanation:
Remove Local State: The isOpen state is no longer needed as the SidebarProvider manages it.
SidebarTrigger: Used to toggle the sidebar. It uses a render prop to provide the isOpen state, which you can use to change the icon from Menu to X.
Animations: Added transition-transform duration-300 to the button for smooth icon transitions.
4. Add Animations to the Sidebar and Navbar
To enhance the user experience, add smooth animations to the sidebar opening/closing and any additional animations to the navbar if desired.
Customize Animations in components/ui/sidebar.tsx
Assuming you have a sidebar.tsx in your components/ui/ directory as per shadcn/ui's structure, you can customize animations here.
(Assuming no changes are needed based on your initial code.)
Conclusion
By following these steps, you've successfully:
Separated the Drawer: Created a distinct AppSidebar component for the drawer.
Integrated shadcn/ui's Sidebar: Leveraged shadcn/ui's Sidebar for advanced features and animations.
Added Animations: Utilized framer-motion for smooth sliding animations of the drawer.
Enhanced Navbar Interactivity: Replaced the local state with a global state managed by SidebarProvider and added transition effects to the navbar's mobile menu button.
Feel free to further customize the sidebar and navbar to fit your design requirements. For more advanced features, refer to the shadcn/ui documentation.