@dax-ui/sidebar
v0.8.0
Published
dax-ui Sidebar: app navigation with sections, collapsing, nested items and a mobile drawer
Maintainers
Readme
@dax-ui/sidebar
App navigation sidebar: a dark brand panel with a logo, labeled sections of items with icons, badges and expandable groups, and a pinned user card. It collapses to icons with tooltips, and becomes a slide-in drawer on small screens.
Installation
pnpm add @dax-ui/sidebarMUI and Emotion are included; the app only needs React 18 or 19.
One-time setup per app (skip if the app already uses dax-ui): install
@dax-ui/theme and wrap the app in its UIProvider.
pnpm add @dax-ui/themeAny icon component works; the examples use lucide-react.
Usage
import { Sidebar, SidebarUser, type SidebarSection } from '@dax-ui/sidebar';
import { Boxes, LifeBuoy, ScrollText, User, Users } from 'lucide-react';
const sections: SidebarSection[] = [
{
id: 'workspace',
label: 'Workspace',
items: [
{ id: 'applications', label: 'My Applications', icon: <Boxes />, href: '/applications' },
{ id: 'support', label: 'Support', icon: <LifeBuoy />, href: '/support', badge: 3 },
{ id: 'account', label: 'My Account', icon: <User />, href: '/account' },
{ id: 'users', label: 'Users', icon: <Users />, href: '/users' },
{ id: 'audit', label: 'Audit Log', icon: <ScrollText />, href: '/audit' },
],
},
];
export function AppLayout({ children }: { children: React.ReactNode }) {
return (
<div style={{ display: 'flex' }}>
<Sidebar
sections={sections}
activeId="users"
logoIcon={<LogoMark />}
logoTitle="Syncora"
logoSubtitle="Sync people, sync process"
watermark={<LogoMark />}
footer={<SidebarUser name="testing" subtitle="Sync people, sync process." />}
/>
<main style={{ flex: 1 }}>{children}</main>
</div>
);
}The sidebar sticks to the top of the viewport at full height; put it first in a flex row next to the page content.
Logo
The brand area at the top is built from props; when collapsed, only the mark shows:
<Sidebar
logoIcon={<LogoMark />} // an SVG or icon component…
logoSrc="/brand/logo.svg" // …or an image URL (used when logoIcon isn't set)
logoTitle="Syncora" // brand name next to the mark
logoSubtitle="Sync people, sync process" // small uppercase tagline
logoHref="/" // makes the logo a link home (uses linkComponent)
sections={sections}
/>Without a mark, the collapsed sidebar shows the title's first letter in an accent tile.
For a completely custom header, pass logo (and logoCollapsed for the collapsed state);
they replace the built-in brand area.
With a router
Pass the router's link component; items with href render through it. Derive
activeId from the current route:
// Next.js App Router ('use client' file)
import Link from 'next/link';
import { usePathname } from 'next/navigation';
const pathname = usePathname();
<Sidebar sections={sections} linkComponent={Link} activeId={idForPath(pathname)} />;// React Router
import { Link, useLocation } from 'react-router-dom';
// React Router's Link takes `to`, so wrap it to accept `href`:
const RouterLink = forwardRef<HTMLAnchorElement, { href: string }>(({ href, ...props }, ref) => (
<Link ref={ref} to={href} {...props} />
));
<Sidebar sections={sections} linkComponent={RouterLink} activeId={idForPath(useLocation().pathname)} />;Without href, items are buttons; handle them in onItemClick.
Collapsing
The toggle on the sidebar's edge collapses it to icons. It works on its own
(defaultCollapsed sets the starting state), or control it, e.g. to remember the choice:
const [collapsed, setCollapsed] = useState(() => localStorage.getItem('sidebar') === 'collapsed');
<Sidebar
sections={sections}
collapsed={collapsed}
onCollapsedChange={(next) => {
setCollapsed(next);
localStorage.setItem('sidebar', next ? 'collapsed' : 'expanded');
}}
/>;When collapsed, labels become tooltips, section labels become dividers, badges become dots, and clicking a group expands the sidebar and opens it.
Mobile
With the default variant="responsive", screens narrower than 900px get a drawer
instead. Open it from your own menu button; it closes on backdrop click, Escape, or
when an item is chosen:
const [menuOpen, setMenuOpen] = useState(false);
<IconButton onClick={() => setMenuOpen(true)} aria-label="Open menu">…</IconButton>
<Sidebar sections={sections} mobileOpen={menuOpen} onMobileClose={() => setMenuOpen(false)} />;Groups, badges and disabled items
{
id: 'settings',
label: 'Settings',
icon: <Settings />,
items: [
{ id: 'general', label: 'General', href: '/settings' },
{ id: 'security', label: 'Security', href: '/settings/security' },
],
},
{ id: 'notifications', label: 'Notifications', icon: <Bell />, badge: 12 },
{ id: 'reports', label: 'Reports', icon: <BarChart3 />, disabled: true },A group opens automatically when activeId is one of its children.
Custom footer
footer takes any content. Use useSidebar() inside it to adapt to the collapsed state:
import { useSidebar } from '@dax-ui/sidebar';
function Footer() {
const { collapsed } = useSidebar();
return collapsed ? <HelpIcon /> : <a href="/help">Help center</a>;
}Floating
floating detaches the panel from the screen edge: a 12px gap around it, all corners
rounded and a soft shadow.
<Sidebar floating sections={sections} />Colors
By default the panel uses tokens.color.sidebar from
@dax-ui/tokens and stays dark in both light
and dark mode. colors overrides any of its colors; unset keys keep the defaults:
// Just a different accent
<Sidebar colors={{ accent: '#6366f1', onAccent: '#ffffff' }} sections={sections} />
// A light sidebar
<Sidebar
showGrid={false}
colors={{
background: '#ffffff',
accent: '#4f46e5',
onAccent: '#ffffff',
text: '#334155',
textActive: '#0f172a',
textMuted: '#475569',
tile: '#f1f5f9',
hover: '#f8fafc',
activeBackground: '#eef2ff',
activeBorder: '#c7d2fe',
divider: '#e2e8f0',
}}
sections={sections}
/>background takes any CSS color or gradient. Keep text and textMuted at a 4.5:1
contrast ratio against background and tile (the user card); Storybook's Accessibility
panel checks this. The colors are set as CSS variables on the panel
(--dax-sidebar-accent, …), so sx overrides can use them too. DEFAULT_SIDEBAR_COLORS
exports the defaults.
Background image
<Sidebar backgroundImage="/images/sidebar.jpg" backgroundOverlayOpacity={0.75} sections={sections} />colors.background is laid over the image as a tint so labels stay readable:
backgroundOverlayOpacity goes from 0 (image as is) to 1 (image hidden), default 0.8.
showGrid={false} removes the grid lines.
Accessibility
The sidebar is a <nav> landmark (named by aria-label, default "Main"), each section
is a list named by its heading, and the active item has aria-current="page". Groups
expose aria-expanded. When collapsed, labels stay readable by screen readers. Every
item is reachable by keyboard and shows a focus ring.
Sidebar props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| sections (required) | SidebarSection[] | | Navigation, grouped into sections. |
| activeId | string | | Id of the item for the current page. Gets the highlighted style and aria-current="page". |
| aria-label | string | Main | Accessible name of the navigation landmark. |
| backgroundImage | string | | Image URL shown behind the navigation. colors.background is laid over it at backgroundOverlayOpacity, so text stays readable. |
| backgroundOverlayOpacity | number | 0.8 | How strongly colors.background covers backgroundImage: 0 shows the image as is, 1 hides it. |
| collapsed | boolean | | Shows icons only, with labels as tooltips. Pair with onCollapsedChange to control it. |
| collapsedWidth | number | 88 | Width when collapsed, in px. |
| collapsible | boolean | true | Shows the collapse toggle on the sidebar's edge. |
| colors | Partial<SidebarColors> | | Overrides any of the sidebar's colors, e.g. { accent: '#6366f1' }, or a full set for a light sidebar. Unset keys keep the defaults, which come from the theme. |
| defaultCollapsed | boolean | false | Initial collapsed state when collapsed isn't controlled. |
| floating | boolean | false | Detaches the panel from the screen edge: a 12px gap around it, all corners rounded and a soft shadow. |
| footer | ReactNode | | Pinned to the bottom, e.g. <SidebarUser />. |
| linkComponent | ElementType | | Component used for items with href, e.g. Next.js Link or a React Router link. Defaults to <a>. |
| logo | ReactNode | | Replaces the whole brand area when expanded, for a fully custom header. Overrides the logo* props above. |
| logoCollapsed | ReactNode | | Replaces the brand area when collapsed. Defaults to the logo mark. |
| logoHref | string | | Makes the logo a link, e.g. to the home page. Uses linkComponent. |
| logoIcon | ReactNode | | Logo mark, e.g. an SVG or icon component. Shown beside the title, and alone when collapsed. |
| logoSrc | string | | Logo mark as an image URL; used when logoIcon isn't set. |
| logoSubtitle | ReactNode | | Small uppercase line under the title, e.g. a tagline. |
| logoTitle | ReactNode | | Brand name next to the logo mark, e.g. "Syncora". |
| mobileOpen | boolean | false | Whether the drawer is open when the sidebar is temporary. |
| onCollapsedChange | ((collapsed: boolean) => void) | | Called when the collapse toggle is clicked, or a group is opened while collapsed. |
| onItemClick | ((item: SidebarItem, event: MouseEvent<HTMLElement, MouseEvent>) => void) | | Called when any item is clicked, including group headers. |
| onMobileClose | (() => void) | | Called to close the drawer: backdrop click, Escape, or choosing an item. |
| showGrid | boolean | true | Draws faint grid lines over the background. |
| sx | SxProps<Theme> | | Style overrides for the sidebar panel. |
| variant | "responsive" \| "permanent" \| "temporary" | responsive | permanent: always visible. temporary: a drawer opened with mobileOpen. responsive: temporary below the md breakpoint (900px), permanent above. |
| watermark | ReactNode | | Large faint graphic in the bottom-right corner, e.g. the logo mark. Hidden when collapsed. |
| width | number | 272 | Width when expanded, in px. |
SidebarSection
| Field | Type | Description |
| --- | --- | --- |
| id (required) | string | Unique id for the section. |
| label | string | Small uppercase heading above the items. A divider when collapsed. |
| items (required) | SidebarItem[] | The section's navigation items. |
SidebarItem
| Field | Type | Description |
| --- | --- | --- |
| id (required) | string | Unique id, matched against the sidebar's activeId. |
| label (required) | string | Visible label; also the accessible name and the tooltip when collapsed. |
| icon | ReactNode | Icon shown in the item's tile. Sized to 20px. Without one, the label's first letter is shown. |
| href | string | Makes the item a link, rendered with linkComponent. |
| badge | ReactNode | Shown at the end of the item, e.g. an unread count. A dot when collapsed. |
| disabled | boolean | Shows the item but makes it unclickable. |
| items | SidebarItem[] | Child items; the item becomes an expandable group (one level deep). |
SidebarColors
All optional in the colors prop. Any CSS color; background also takes gradients.
| Key | Used for |
| --- | --- |
| background | Panel background (a tint over backgroundImage) |
| accent | Active item tile, badges, avatar, collapse toggle, focus ring |
| onAccent | Text and icons on accent |
| text | Item labels |
| textActive | Active and hovered labels, the user's name, the logo |
| textMuted | Section labels, chevrons, subtitles |
| tile | Icon tiles of inactive items, the user card |
| hover | Item background on hover |
| activeBackground | Background of the active item's card |
| activeBorder | Border of the active item's card |
| divider | Collapsed section dividers, the user card border |
| grid | Grid lines over the background |
SidebarUser props
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| name (required) | string | | Name shown in bold; its initials fill the avatar when there's no avatarSrc. |
| avatarSrc | string | | Avatar image URL. |
| onClick | ((event: MouseEvent<HTMLButtonElement, MouseEvent>) => void) | | Makes the card a button, e.g. to open an account menu. |
| subtitle | ReactNode | | Second line under the name, e.g. a role or tagline. |
Other exports
| Export | What it is |
| --- | --- |
| useSidebar() | { collapsed }, for custom content inside the sidebar |
| SIDEBAR_WIDTH, SIDEBAR_COLLAPSED_WIDTH | Default widths in px (272, 88), e.g. for layout offsets |
| DEFAULT_SIDEBAR_COLORS | The default colors, e.g. to build a palette from |
