Favourite pages that follow the user, and a narrow icon-only sidebar
A star beside each page in the sidebar pins it under "Favourites" at the top of the menu; the list is saved with the user (users.favorites, migration 0020), so it follows them to any browser, is offered first in the search box, and can be changed from there too. A button at the foot of the sidebar, or the [ key, narrows it to a strip of icons, where a group's pages open in a small menu beside its icon and the alert and maintenance counts stay visible; the choice is kept per browser and applies only on wide screens. Menu text on a coloured (environment) sidebar is full strength for contrast, and menu badges sit in line on a phone. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
57fd4f8c52
commit
65263a64e3
17 files changed
+2736
-86
No files matched your search
@@ -0,0 +1,180 @@
|
||||
import { useEffect, useRef, useState, type ReactNode } from "react";
|
||||
import { NavLink } from "react-router-dom";
|
||||
import { IconStar, IconStarFilled } from "@tabler/icons-react";
|
||||
import { isCurrent, isGroup, type NavEntry, type NavGroup, type NavLeaf } from "./nav";
|
||||
import { toggleFavorite, useFavorites } from "../utils/favorites";
|
||||
|
||||
export interface NavBadge {
|
||||
text: string;
|
||||
className: string;
|
||||
title: string;
|
||||
}
|
||||
|
||||
interface Flyout {
|
||||
group: NavGroup;
|
||||
top: number;
|
||||
left: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The menu in the sidebar: this person's pinned pages first, then the pages and groups. Wide, a group opens in place; narrow (the
|
||||
* icon-only strip) it opens to the side as a small menu instead, because there is no room beside an icon for a list.
|
||||
*/
|
||||
export default function SidebarNav({
|
||||
nav,
|
||||
pathname,
|
||||
badges,
|
||||
openGroups,
|
||||
onToggleGroup,
|
||||
rail,
|
||||
onNavigate,
|
||||
}: {
|
||||
nav: NavEntry[];
|
||||
pathname: string;
|
||||
badges: Record<string, NavBadge>;
|
||||
openGroups: Set<string>;
|
||||
onToggleGroup: (id: string) => void;
|
||||
/** The sidebar is the narrow, icon-only strip. */
|
||||
rail: boolean;
|
||||
/** Called when a link is followed (to close the menu on a phone). */
|
||||
onNavigate: () => void;
|
||||
}) {
|
||||
const favorites = useFavorites();
|
||||
const [flyout, setFlyout] = useState<Flyout | null>(null);
|
||||
const flyoutRef = useRef<HTMLDivElement>(null);
|
||||
const opener = useRef<HTMLElement | null>(null);
|
||||
|
||||
// Every page that can be pinned, by path.
|
||||
const leaves = new Map<string, NavLeaf>();
|
||||
for (const entry of nav) {
|
||||
if (isGroup(entry)) entry.items.forEach((i) => leaves.set(i.to, i));
|
||||
else leaves.set(entry.to, entry);
|
||||
}
|
||||
const pinned = favorites.map((p) => leaves.get(p)).filter((l): l is NavLeaf => !!l);
|
||||
|
||||
// The side menu goes away when the page changes, when the strip is widened, on Escape, and on a click anywhere else.
|
||||
useEffect(() => setFlyout(null), [pathname, rail]);
|
||||
useEffect(() => {
|
||||
if (!flyout) return;
|
||||
const onDown = (e: MouseEvent) => {
|
||||
const t = e.target as Node;
|
||||
if (flyoutRef.current?.contains(t) || opener.current?.contains(t)) return;
|
||||
setFlyout(null);
|
||||
};
|
||||
const onKey = (e: KeyboardEvent) => {
|
||||
if (e.key === "Escape") {
|
||||
setFlyout(null);
|
||||
opener.current?.focus();
|
||||
}
|
||||
};
|
||||
document.addEventListener("mousedown", onDown);
|
||||
document.addEventListener("keydown", onKey);
|
||||
return () => {
|
||||
document.removeEventListener("mousedown", onDown);
|
||||
document.removeEventListener("keydown", onKey);
|
||||
};
|
||||
}, [flyout]);
|
||||
|
||||
const badgeOf = (to: string): ReactNode => {
|
||||
const b = badges[to];
|
||||
return b ? (
|
||||
<span className={`badge ms-auto ${b.className}`} title={b.title}>
|
||||
<span aria-hidden="true">{b.text}</span>
|
||||
<span className="visually-hidden">{b.title}</span>
|
||||
</span>
|
||||
) : null;
|
||||
};
|
||||
|
||||
const star = (leaf: NavLeaf) => {
|
||||
const on = favorites.includes(leaf.to);
|
||||
const label = on ? `Remove ${leaf.label} from favourites` : `Add ${leaf.label} to favourites`;
|
||||
return (
|
||||
<button type="button" className="fav-star" aria-pressed={on} aria-label={label} title={label} onClick={() => void toggleFavorite(leaf.to)}>
|
||||
{on ? <IconStarFilled size={16} /> : <IconStar size={16} />}
|
||||
</button>
|
||||
);
|
||||
};
|
||||
|
||||
const leafRow = (leaf: NavLeaf, key: string) => (
|
||||
<li className="nav-item nav-row" key={key}>
|
||||
<NavLink to={leaf.to} end={leaf.to === "/"} className="nav-link" title={rail ? leaf.label : undefined} onClick={onNavigate}>
|
||||
<span className="nav-link-icon d-md-none d-lg-inline-block">{leaf.icon}</span>
|
||||
<span className="nav-link-title">{leaf.label}</span>
|
||||
{badgeOf(leaf.to)}
|
||||
</NavLink>
|
||||
{star(leaf)}
|
||||
</li>
|
||||
);
|
||||
|
||||
const subRow = (leaf: NavLeaf) => (
|
||||
<div className="nav-row" key={leaf.to}>
|
||||
<NavLink to={leaf.to} className={({ isActive }) => `dropdown-item d-flex align-items-center ${isActive ? "active" : ""}`} onClick={onNavigate}>
|
||||
{leaf.label}
|
||||
{badgeOf(leaf.to)}
|
||||
</NavLink>
|
||||
{star(leaf)}
|
||||
</div>
|
||||
);
|
||||
|
||||
return (
|
||||
<nav aria-label="Main menu">
|
||||
<ul className="navbar-nav pt-lg-3">
|
||||
{pinned.length > 0 && (
|
||||
<li className="nav-item nav-favourites">
|
||||
<div className="nav-heading" id="nav-favourites-heading">
|
||||
Favourites
|
||||
</div>
|
||||
<ul className="navbar-nav" aria-labelledby="nav-favourites-heading">
|
||||
{pinned.map((leaf) => leafRow(leaf, `fav-${leaf.to}`))}
|
||||
</ul>
|
||||
</li>
|
||||
)}
|
||||
|
||||
{nav.map((entry) => {
|
||||
if (!isGroup(entry)) return leafRow(entry, entry.to);
|
||||
const open = openGroups.has(entry.id);
|
||||
const holdsCurrent = entry.items.some((item) => isCurrent(pathname, item.to));
|
||||
const inside = entry.items.map((item) => badges[item.to]).filter(Boolean);
|
||||
const showSummary = (!open || rail) && inside.length > 0;
|
||||
const worst = inside.find((b) => b.className.includes("bg-red")) ?? inside.find((b) => b.className.includes("bg-yellow")) ?? inside[0];
|
||||
const flyOpen = rail && flyout?.group.id === entry.id;
|
||||
return (
|
||||
<li className="nav-item dropdown" key={entry.id}>
|
||||
<button
|
||||
type="button"
|
||||
className={`nav-link dropdown-toggle w-100 border-0 bg-transparent text-start ${(rail ? flyOpen : open) ? "show" : ""} ${holdsCurrent ? "active" : ""}`}
|
||||
aria-expanded={rail ? flyOpen : open}
|
||||
title={rail ? entry.label : undefined}
|
||||
onClick={(e) => {
|
||||
if (!rail) return onToggleGroup(entry.id);
|
||||
if (flyOpen) return setFlyout(null);
|
||||
const r = e.currentTarget.getBoundingClientRect();
|
||||
opener.current = e.currentTarget;
|
||||
setFlyout({ group: entry, top: r.top, left: r.right + 6 });
|
||||
}}
|
||||
>
|
||||
<span className="nav-link-icon d-md-none d-lg-inline-block">{entry.icon}</span>
|
||||
<span className="nav-link-title">{entry.label}</span>
|
||||
{/* A closed group still says that something inside it needs a look. */}
|
||||
{showSummary && (
|
||||
<span className={`badge ms-auto me-4 ${worst.className}`} title={inside.map((b) => b.title).join(", ")}>
|
||||
<span aria-hidden="true">{inside.reduce((n, b) => n + Number(b.text), 0)}</span>
|
||||
<span className="visually-hidden">{inside.map((b) => b.title).join(", ")}</span>
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
<div className={`dropdown-menu ${open ? "show" : ""}`}>{entry.items.map(subRow)}</div>
|
||||
</li>
|
||||
);
|
||||
})}
|
||||
</ul>
|
||||
|
||||
{rail && flyout && (
|
||||
<div ref={flyoutRef} className="rail-flyout" role="group" aria-label={flyout.group.label} style={{ top: flyout.top, left: flyout.left, maxHeight: `calc(100vh - ${flyout.top}px - 0.75rem)` }}>
|
||||
<div className="rail-flyout-title">{flyout.group.label}</div>
|
||||
{flyout.group.items.map(subRow)}
|
||||
</div>
|
||||
)}
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user