195 lines
6.8 KiB
TypeScript
195 lines
6.8 KiB
TypeScript
import { Link, useLocation } from "react-router";
|
|
import { cloneElement, isValidElement, useEffect, useRef, useState } from "react";
|
|
import Tabs from "../tabs/Tabs";
|
|
import EntityIcon from "~/components/layouts/icons/EntityIcon";
|
|
import { SidebarInside } from "~/ui";
|
|
import { isRouteActive } from "~/utils/navigation/routeMatching";
|
|
|
|
export type IconDto = {
|
|
name: string;
|
|
href: string;
|
|
prefetch?: "intent" | "render" | "none";
|
|
icon?: React.ReactNode;
|
|
iconSelected?: React.ReactNode;
|
|
bottom?: boolean;
|
|
exact?: boolean;
|
|
textIcon?: string;
|
|
textIconSelected?: string;
|
|
hidden?: boolean;
|
|
};
|
|
|
|
const HOVER_OPEN_DELAY_MS = 300;
|
|
const HOVER_CLOSE_DELAY_MS = 350;
|
|
const POINTER_ROUTE_SETTLE_MS = 750;
|
|
let suppressPointerHoverUntil = 0;
|
|
let lastPointerInteractionAt = 0;
|
|
|
|
function pointerHoverIsSuppressed() {
|
|
const now = Date.now();
|
|
if (now < lastPointerInteractionAt) {
|
|
lastPointerInteractionAt = 0;
|
|
suppressPointerHoverUntil = 0;
|
|
return false;
|
|
}
|
|
return now < suppressPointerHoverUntil;
|
|
}
|
|
|
|
export default function SidebarIconsLayout({
|
|
children,
|
|
items,
|
|
sticky = false,
|
|
}: {
|
|
children: React.ReactNode;
|
|
items: IconDto[];
|
|
// Beibehalten für API-Kompatibilität der 17 Call-Sites; in Davids Inner-Sidebar nicht mehr nötig.
|
|
label?: {
|
|
align: "bottom" | "right";
|
|
};
|
|
scrollRestoration?: boolean;
|
|
sticky?: boolean;
|
|
}) {
|
|
const location = useLocation();
|
|
const mainElement = useRef<HTMLDivElement>(null);
|
|
const [hovered, setHovered] = useState(false);
|
|
const [focusWithin, setFocusWithin] = useState(false);
|
|
const openDelayRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const closeDelayRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
|
const pointerInteractionRef = useRef(false);
|
|
const suppressHoverUntilLeaveRef = useRef(false);
|
|
const expanded = hovered || focusWithin;
|
|
|
|
function cancelScheduledOpen() {
|
|
if (openDelayRef.current === null) return;
|
|
clearTimeout(openDelayRef.current);
|
|
openDelayRef.current = null;
|
|
}
|
|
|
|
function cancelScheduledClose() {
|
|
if (closeDelayRef.current === null) return;
|
|
clearTimeout(closeDelayRef.current);
|
|
closeDelayRef.current = null;
|
|
}
|
|
|
|
function handleMouseEnter() {
|
|
cancelScheduledClose();
|
|
if (suppressHoverUntilLeaveRef.current || pointerHoverIsSuppressed()) return;
|
|
if (hovered || openDelayRef.current !== null) return;
|
|
openDelayRef.current = setTimeout(() => {
|
|
setHovered(true);
|
|
openDelayRef.current = null;
|
|
}, HOVER_OPEN_DELAY_MS);
|
|
}
|
|
|
|
function handleMouseLeave() {
|
|
suppressHoverUntilLeaveRef.current = false;
|
|
pointerInteractionRef.current = false;
|
|
cancelScheduledOpen();
|
|
cancelScheduledClose();
|
|
if (!hovered) return;
|
|
closeDelayRef.current = setTimeout(() => {
|
|
setHovered(false);
|
|
closeDelayRef.current = null;
|
|
}, HOVER_CLOSE_DELAY_MS);
|
|
}
|
|
|
|
useEffect(
|
|
() => () => {
|
|
if (openDelayRef.current !== null) clearTimeout(openDelayRef.current);
|
|
if (closeDelayRef.current !== null) clearTimeout(closeDelayRef.current);
|
|
},
|
|
[]
|
|
);
|
|
|
|
function findExactRoute(element: IconDto) {
|
|
return isRouteActive(location.pathname, element.href, { exact: element.exact });
|
|
}
|
|
const currentTab = items.find((element) => findExactRoute(element));
|
|
|
|
function renderItem(item: IconDto, idx: number) {
|
|
const current = currentTab?.name === item.name;
|
|
const textIcon = current ? (item.textIconSelected ?? item.textIcon) : item.textIcon;
|
|
const icon = current ? (item.iconSelected ?? item.icon) : item.icon;
|
|
const iconNode = textIcon !== undefined ? <EntityIcon className="size-5" icon={textIcon} /> : normalizeIcon(icon);
|
|
// The icon stays left-anchored in both states (NavItem keeps a constant 32px icon column),
|
|
// so the icon never shifts horizontally between the collapsed rail and the expanded label row.
|
|
return (
|
|
<Link key={idx} prefetch={item.prefetch} to={item.href} className="flex w-full justify-start" title={item.name}>
|
|
<SidebarInside.NavItem icon={iconNode} selected={current} variant={expanded ? "icon-and-text" : "icon-only"}>
|
|
{item.name}
|
|
</SidebarInside.NavItem>
|
|
</Link>
|
|
);
|
|
}
|
|
|
|
const topItems = items.filter((f) => !f.bottom && !f.hidden);
|
|
const bottomItems = items.filter((f) => f.bottom && !f.hidden);
|
|
|
|
return (
|
|
<div className="sm:flex sm:flex-row">
|
|
{/* Desktop: expand on pointer hover or keyboard focus; otherwise keep a compact icon rail. */}
|
|
<div
|
|
className={sticky ? "relative hidden sm:sticky sm:top-0 sm:flex sm:self-start" : "relative hidden sm:flex"}
|
|
onMouseEnter={handleMouseEnter}
|
|
onMouseLeave={handleMouseLeave}
|
|
onPointerDownCapture={() => {
|
|
const pointerInteractionAt = Date.now();
|
|
pointerInteractionRef.current = true;
|
|
suppressHoverUntilLeaveRef.current = true;
|
|
lastPointerInteractionAt = pointerInteractionAt;
|
|
suppressPointerHoverUntil = pointerInteractionAt + POINTER_ROUTE_SETTLE_MS;
|
|
cancelScheduledOpen();
|
|
setFocusWithin(false);
|
|
}}
|
|
onPointerUpCapture={() => {
|
|
pointerInteractionRef.current = false;
|
|
}}
|
|
onPointerCancelCapture={() => {
|
|
pointerInteractionRef.current = false;
|
|
}}
|
|
onFocusCapture={() => {
|
|
if (!pointerInteractionRef.current) setFocusWithin(true);
|
|
}}
|
|
onBlurCapture={(event) => {
|
|
if (!event.currentTarget.contains(event.relatedTarget as Node | null)) setFocusWithin(false);
|
|
}}
|
|
>
|
|
<SidebarInside visible={true} variant={expanded ? "open" : "closed"} className="min-h-[calc(100svh-3.5rem)]">
|
|
{topItems.map(renderItem)}
|
|
{bottomItems.length > 0 && (
|
|
<div className="mt-auto flex w-full flex-col">
|
|
<SidebarInside.NavDivider />
|
|
{bottomItems.map(renderItem)}
|
|
</div>
|
|
)}
|
|
</SidebarInside>
|
|
</div>
|
|
|
|
{/* Mobile: Tabs-Leiste (unverändert) */}
|
|
<div className="border-border-default bg-surface-neutral-primary w-full border-b py-2 shadow-2xs sm:hidden">
|
|
<div className="mx-auto flex max-w-5xl items-center justify-between gap-2 px-4 sm:px-6 lg:px-8 xl:max-w-7xl 2xl:max-w-(--breakpoint-2xl)">
|
|
<Tabs
|
|
tabs={items
|
|
.filter((f) => !f.hidden)
|
|
.map((i) => {
|
|
return { name: i.name, routePath: i.href, exact: i.exact };
|
|
})}
|
|
className="grow"
|
|
/>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Hauptbereich */}
|
|
<div ref={mainElement} className="w-full overflow-x-hidden" data-route-transition-content="true">
|
|
{children}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function normalizeIcon(icon: React.ReactNode) {
|
|
if (!isValidElement<{ className?: string }>(icon)) return icon;
|
|
return cloneElement(icon, {
|
|
className: `${icon.props.className ?? ""} inline-flex size-5 items-center justify-center [&_svg]:size-5`,
|
|
});
|
|
}
|