Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 | 2x 55x 185x 359x 1111x 482x 204x 24x 217x 35x 28x 47x 185x 452x 364x 364x 88x 88x 88x 47x 370x 370x 370x 11x 359x 55x 55x 55x 97x 55x 25x 55x 22x 22x 22x 55x 22x 22x 22x 22x 88x 22x 22x 22x 6x 55x 3x 3x 3x 2x 3x 55x 3x 3x 3x 2x 3x 55x 55x 54x 30x 24x 24x 24x 2x 24x 24x 55x 22x 22x 22x 1x 7x 55x 532x 217x 217x 55x 162x 162x 312x 217x 217x 2x 2x 69x 55x 217x | 'use client';
import Link from 'next/link';
import { usePathname } from 'next/navigation';
import { useEffect, useState } from 'react';
import { CaretDownIcon, CaretRightIcon } from './icons';
import { STYLE_PREVIEW_ENABLED } from '@/lib/dev-tools';
import type { NavCategory, NavGroup, NavPage, NavTab } from '@/lib/navigation';
interface SidebarClientProps {
categories: NavCategory[];
}
type Level = 'l1' | 'l2' | 'l3' | 'l4';
// `trailingSlash: true` makes usePathname() return `/components/badge/`; nav URLs have no trailing slash.
function stripTrailingSlash(pathname: string | null): string | null {
return pathname && pathname.length > 1 ? pathname.replace(/\/+$/, '') : pathname;
}
function isInCategory(pathname: string | null, cat: NavCategory): boolean {
return !!pathname?.startsWith(`/${cat.slug}`);
}
function isExternal(url: string): boolean {
return /^https?:\/\//.test(url);
}
function isGroup(item: NavPage | NavGroup): item is NavGroup {
return 'kind' in item && item.kind === 'group';
}
function hasTabs(page: NavPage): page is NavPage & { tabs: NavTab[] } {
return Array.isArray(page.tabs) && page.tabs.length > 0;
}
function isInPage(pathname: string | null, page: NavPage): boolean {
return pathname === page.url || !!page.tabs?.some((t) => t.url === pathname);
}
// The first tab aliases the page URL (same as the page's tab strip), so `/components/badge`
// and `/components/badge--usage` both highlight the first tab.
function tabHref(page: NavPage, index: number): string {
return index === 0 ? page.url : page.tabs![index].url;
}
// The category's own landing page (e.g. `/components/components`); its header links there.
function landingPage(cat: NavCategory): NavPage | undefined {
return cat.children.find((p) => p.slug === cat.slug);
}
// Expand-state keys: pages by URL, groups by id (label groups have no URL).
const pageKey = (page: NavPage) => `page:${page.url}`;
const groupKey = (group: NavGroup) => `group:${group.id}`;
// Groups and tabbed pages that contain the current pathname, so they start open.
function currentExpandKeys(categories: NavCategory[], pathname: string | null): string[] {
const keys: string[] = [];
for (const item of categories.flatMap((c) => c.items)) {
if (!isGroup(item)) {
if (hasTabs(item) && isInPage(pathname, item)) keys.push(pageKey(item));
continue;
}
const current = item.children.find((p) => isInPage(pathname, p));
if (current || (item.page && pathname === item.page.url)) keys.push(groupKey(item));
if (current && hasTabs(current)) keys.push(pageKey(current));
}
return keys;
}
function NavItem({
level,
href,
label,
active,
onNavigate,
expanded,
onToggle,
}: {
level: Level;
href?: string;
label: string;
active?: boolean;
onNavigate?: () => void;
expanded?: boolean;
onToggle?: () => void;
}) {
const Caret = expanded ? CaretDownIcon : CaretRightIcon;
const className = `site-sidebar__item site-sidebar__item--${level}${active ? ' is-active' : ''}`;
// No page to link to (label group): the whole row toggles.
if (!href) {
return (
<div className={className}>
<button type="button" className="site-sidebar__link" onClick={onToggle} aria-expanded={expanded}>
<span className="site-sidebar__label">{label}</span>
<span className="site-sidebar__chevron">
<Caret className="site-sidebar__caret" aria-hidden="true" />
</span>
</button>
</div>
);
}
return (
<div className={className}>
{isExternal(href) ? (
<a href={href} target="_blank" rel="noopener noreferrer" className="site-sidebar__link">
<span className="site-sidebar__label">{label}</span>
</a>
) : (
<Link
href={href}
className="site-sidebar__link"
aria-current={active ? 'page' : undefined}
onClick={onNavigate}
>
<span className="site-sidebar__label">{label}</span>
</Link>
)}
{onToggle && (
<button
type="button"
className="site-sidebar__chevron"
onClick={onToggle}
aria-expanded={expanded}
aria-label={`${expanded ? 'Collapse' : 'Expand'} ${label}`}
>
<Caret className="site-sidebar__caret" aria-hidden="true" />
</button>
)}
</div>
);
}
export function SidebarClient({ categories }: SidebarClientProps) {
const pathname = stripTrailingSlash(usePathname());
const [mobileOpen, setMobileOpen] = useState(false);
// Only the current category starts open, so the static HTML matches the hydrated state.
const [expandedCats, setExpandedCats] = useState<Set<number>>(
() => new Set<number>(categories.filter((c) => isInCategory(pathname, c)).map((c) => c.id)),
);
const [expanded, setExpanded] = useState<Set<string>>(
() => new Set(currentExpandKeys(categories, pathname)),
);
useEffect(() => {
const handler = () => setMobileOpen((v) => !v);
document.addEventListener('toggle-sidebar', handler);
return () => document.removeEventListener('toggle-sidebar', handler);
}, []);
useEffect(() => {
setMobileOpen(false);
setExpandedCats((prev) => {
const next = new Set(prev);
for (const c of categories) {
if (isInCategory(pathname, c)) next.add(c.id);
}
return next;
});
const current = currentExpandKeys(categories, pathname);
if (current.length > 0) {
setExpanded((prev) => new Set([...prev, ...current]));
}
}, [pathname, categories]);
const toggleCat = (id: number) => {
setExpandedCats((prev) => {
const next = new Set(prev);
if (next.has(id)) next.delete(id);
else next.add(id);
return next;
});
};
const toggle = (key: string) => {
setExpanded((prev) => {
const next = new Set(prev);
if (next.has(key)) next.delete(key);
else next.add(key);
return next;
});
};
const closeMobile = () => setMobileOpen(false);
// `level` is the page row's level; its tabs sit one level deeper.
const renderPage = (page: NavPage, level: Level = 'l2') => {
if (!hasTabs(page)) {
return (
<NavItem
key={page.id}
level={level}
href={page.url}
label={page.name}
active={pathname === page.url}
/>
);
}
const key = pageKey(page);
const isExpanded = expanded.has(key);
return (
<div key={page.id} className="site-sidebar__group">
<NavItem
level={level}
href={page.url}
label={page.name}
// The current tab is highlighted instead; the row only shows when the tabs are collapsed.
active={!isExpanded && isInPage(pathname, page)}
// Clicking the label also opens the tab list (never closes it).
onNavigate={() => !isExpanded && toggle(key)}
expanded={isExpanded}
onToggle={() => toggle(key)}
/>
{isExpanded && (
<div className="site-sidebar__list">
{page.tabs.map((tab, i) => {
const href = tabHref(page, i);
return (
<NavItem
key={tab.url}
level={level === 'l2' ? 'l3' : 'l4'}
href={href}
label={tab.name}
active={pathname === href || pathname === tab.url}
/>
);
})}
</div>
)}
</div>
);
};
const renderGroup = (group: NavGroup) => {
const key = groupKey(group);
const isExpanded = expanded.has(key);
return (
<div key={`group-${group.id}`} className="site-sidebar__group">
<NavItem
level="l2"
href={group.page?.url}
label={group.name}
active={!!group.page && pathname === group.page.url}
onNavigate={() => !isExpanded && toggle(key)}
expanded={isExpanded}
onToggle={() => toggle(key)}
/>
{isExpanded && (
<div className="site-sidebar__list">{group.children.map((page) => renderPage(page, 'l3'))}</div>
)}
</div>
);
};
const renderCategory = (cat: NavCategory) => {
const items = cat.items.filter((item) => isGroup(item) || !item.hidden);
const only = items[0];
if (items.length === 1 && !isGroup(only) && !hasTabs(only)) {
return (
<NavItem key={cat.id} level="l1" href={only.url} label={only.name} active={pathname === only.url} />
);
}
const landing = landingPage(cat);
const categoryUrl = landing?.url ?? `/${cat.slug}`;
const children = landing ? items.filter((item) => item !== landing) : items;
const isExpanded = expandedCats.has(cat.id);
return (
<div key={cat.id} className="site-sidebar__group">
<NavItem
level="l1"
href={categoryUrl}
label={cat.name}
active={pathname === categoryUrl}
// Clicking the label also opens the category (never closes it).
onNavigate={() => !isExpanded && toggleCat(cat.id)}
expanded={isExpanded}
onToggle={() => toggleCat(cat.id)}
/>
{isExpanded && (
<div className="site-sidebar__list">
{children.map((item) => (isGroup(item) ? renderGroup(item) : renderPage(item)))}
</div>
)}
</div>
);
};
return (
<>
{mobileOpen && (
<button
type="button"
className="site-sidebar__backdrop"
onClick={closeMobile}
aria-label="Close navigation"
/>
)}
<aside className={mobileOpen ? 'site-sidebar is-open' : 'site-sidebar'} aria-label="Primary navigation">
<nav className="site-sidebar__nav site-sidebar__list">
{categories.filter((c) => !c.hidden).map(renderCategory)}
{/* Dev-only link, left out of the public production build (see lib/dev-tools). */}
{STYLE_PREVIEW_ENABLED && (
<div className="site-sidebar__dev">
<NavItem
level="l1"
href="/style-preview"
label="🧪 Style Preview"
active={pathname === '/style-preview'}
onNavigate={closeMobile}
/>
</div>
)}
</nav>
</aside>
</>
);
}
|