Sidebar
Also called side navigation, sidenav, or navigation rail
A vertical panel along the side of an app that holds the main navigation, often collapsible to a slim strip of icons.
import { useState, type ReactNode } from 'react';
import { MotionConfig, motion } from 'motion/react';
import { Tooltip } from '@base-ui/react/tooltip';
type NavItem = { href: string; label: string; icon: ReactNode };
type Props = { items: NavItem[]; currentPath: string };
export function Sidebar({ items, currentPath }: Props) {
const [collapsed, setCollapsed] = useState(
() => localStorage.getItem('sidebar') === 'collapsed'
);
function toggle() {
setCollapsed(!collapsed);
localStorage.setItem('sidebar', collapsed ? 'expanded' : 'collapsed');
}
// `layout` animates the width change with transforms, so the page
// isn't laid out again on every frame.
return (
<MotionConfig transition={{ duration: 0.25, ease: [0.23, 1, 0.32, 1] }}>
<motion.nav
layout
aria-label='Main'
className={`flex h-dvh flex-col gap-1 border-r bg-muted p-3 ${collapsed ? 'w-16' : 'w-60'}`}
>
<motion.button
layout='position'
type='button'
onClick={toggle}
aria-label={collapsed ? 'Expand sidebar' : 'Collapse sidebar'}
className='mb-2 size-10 self-end rounded-lg text-muted-foreground hover:bg-background'
>
{collapsed ? '»' : '«'}
</motion.button>
{items.map((item) => (
<Tooltip.Root key={item.href} disabled={!collapsed}>
<Tooltip.Trigger
render={<motion.a layout='position' href={item.href} />}
aria-current={item.href === currentPath ? 'page' : undefined}
className='flex h-10 items-center gap-3 rounded-lg px-3 text-sm text-muted-foreground hover:bg-background aria-[current=page]:bg-background aria-[current=page]:font-medium aria-[current=page]:text-foreground'
>
<span className='size-4 shrink-0'>{item.icon}</span>
<span className={collapsed ? 'sr-only' : 'truncate'}>
{item.label}
</span>
</Tooltip.Trigger>
<Tooltip.Portal>
<Tooltip.Positioner side='right' sideOffset={12}>
<Tooltip.Popup className='rounded-md bg-foreground px-2 py-1 text-xs text-background'>
{item.label}
</Tooltip.Popup>
</Tooltip.Positioner>
</Tooltip.Portal>
</Tooltip.Root>
))}
</motion.nav>
</MotionConfig>
);
}Add a collapsible left sidebar: 240px wide with an icon and label for Home, Inbox, Projects, Calendar, and Settings, highlighting the current page. A toggle button at the top collapses it to a 64px icon-only rail with tooltips on hover, animated over 250ms, and the choice is saved to localStorage. Under 768px, hide it and open it as a drawer from a menu button.
Paste into Layerwise or any AI coding tool.
What is Sidebar?
Most dashboards, admin panels, and productivity apps put navigation in a sidebar on the left: a list of sections with an icon and a label each, the current one highlighted, and the account or settings menu at the bottom. A sidebar fits more items than a top bar and shows the structure of the whole app at a glance.
A collapsible sidebar shrinks to an icon-only rail, about 56 to 64px wide, to give the content more room. Show each label as a tooltip while it is collapsed, remember the choice between visits, and keep the toggle button in the same spot in both states so people can find it again.
On phones there is no room for a permanent panel, so the sidebar usually becomes a drawer that opens from a menu button, or a bottom tab bar. Animating the width directly makes the browser recalculate the page layout on every frame; a layout animation such as Motion’s layout prop gets the same effect with transforms.
Often confused with
- Drawer
- A drawer slides over the page when opened and closes again. A sidebar is a lasting part of the layout that sits beside the content.
Related terms
All termsDrawer
A panel that slides in from the edge of the screen, usually the bottom on phones or the side on desktop, to show extra content or options.
Layout animation
An animation that smoothly moves and resizes elements when the layout changes, such as list items gliding to new spots after a sort or filter.
Split screen
A layout that divides the screen into two side-by-side panels, each with its own content, sometimes with a divider you drag to resize.
Responsive design
Designing one layout that adapts to any screen width, rearranging columns, sizes, and navigation at set widths called breakpoints.
Build it with Layerwise
Describe your app, watch it run in a live preview, and publish it with a database, storage, and sign-in built in.





