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(null); const [hovered, setHovered] = useState(false); const [focusWithin, setFocusWithin] = useState(false); const openDelayRef = useRef | null>(null); const closeDelayRef = useRef | 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 ? : 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 ( {item.name} ); } const topItems = items.filter((f) => !f.bottom && !f.hidden); const bottomItems = items.filter((f) => f.bottom && !f.hidden); return (
{/* Desktop: expand on pointer hover or keyboard focus; otherwise keep a compact icon rail. */} {/* Mobile: Tabs-Leiste (unverändert) */}
!f.hidden) .map((i) => { return { name: i.name, routePath: i.href, exact: i.exact }; })} className="grow" />
{/* Hauptbereich */}
{children}
); } 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`, }); }