import { For, Show, createEffect, createSignal, onCleanup } from "solid-js"; import { Link, useRouterState } from "@tanstack/solid-router"; import { routeNavItem, visibleNavGroups, type NavItem } from "@/components/layout/nav-items"; import { accountDisplayName, createAccountMenu } from "@/components/layout/account-menu"; import { MobileAccountPanel } from "@/components/layout/mobile-account-panel"; import { IconChevronLeft, IconChevronRight, IconX } from "@/components/ui/icons"; import { UserAvatar } from "@/components/users/user-avatar"; import type { MessageKey } from "@/i18n/messages"; import { cn } from "@/lib/cn"; import { useAuth } from "@/stores/auth-context"; import { useModules } from "@/stores/modules-context"; import { useShellFeed } from "@/stores/shell-feed-context"; import { useT } from "@/stores/preferences-context"; /** * The phone-sized navigation menu: a sheet that rises from the bottom of the * screen, where a thumb already is, instead of the side drawer this replaces. * * It stays mounted and is moved off-screen when closed, so opening and closing * both animate; a `` around it would make it appear and disappear in one * frame. * * It closes by tapping the X, tapping outside, or dragging it down past * DISMISS_PX — the gesture people expect from a sheet. */ /** How far the sheet has to be pulled down before letting go dismisses it. */ const DISMISS_PX = 96; export function MobileNavSheet(props: { open: boolean; onClose: () => void; onLogout: () => void | Promise; onOpenCelebi?: () => void; onOpenProfile?: () => void; }) { const auth = useAuth(); const t = useT(); const feed = useShellFeed(); const modules = useModules(); const pathname = useRouterState({ select: (state) => state.location.pathname }); const search = useRouterState({ select: (state) => state.location.search as Record }); // The primary destinations are already one tap away in the tab bar, so the // sheet lists everything else. const groups = () => visibleNavGroups(auth.user()?.role, modules.enabled()); const current = () => routeNavItem(pathname(), auth.user()?.role, search()); const badgeFor = (item: NavItem) => (item.id === "messages" ? feed.unreadMessages().total : 0); // The account row drills into an account view inside this same sheet — // a back arrow returns — instead of a dropdown stacked on top of the sheet. const [view, setView] = createSignal<"nav" | "account">("nav"); createEffect(() => { if (!props.open) setView("nav"); }); const menu = createAccountMenu({ onLogout: props.onLogout, onLeave: props.onClose, onOpenSettings: () => { props.onClose(); props.onOpenProfile?.(); }, }); const runAction = (item: NavItem) => { props.onClose(); if (item.action === "celebi") props.onOpenCelebi?.(); if (item.action === "profile") props.onOpenProfile?.(); }; // Drag-to-dismiss. Only the grab area drives it, so a flick inside the list // still scrolls the list. While dragging, the sheet follows the finger with // its transition off; on release it either closes or springs back. const [dragY, setDragY] = createSignal(0); const [dragging, setDragging] = createSignal(false); let startY = 0; // Keyboard users arrive here through the Menü tab control. Park focus on // the close button while the sheet is up (Escape closes it too) and hand // focus back to the Menü trigger on dismiss, so focus never strands on // the body or inside an off-screen panel. let closeBtn: HTMLButtonElement | undefined; let wasOpen = false; createEffect(() => { if (props.open) { wasOpen = true; closeBtn?.focus(); const onKey = (event: KeyboardEvent) => { if (event.key === "Escape") props.onClose(); }; window.addEventListener("keydown", onKey); onCleanup(() => window.removeEventListener("keydown", onKey)); } else if (wasOpen) { wasOpen = false; document.getElementById("mobile-menu-trigger")?.focus(); } }); const startDrag = (event: PointerEvent) => { if (event.pointerType === "mouse" && event.button !== 0) return; // Buttons inside a drag surface (the sheet's X) must keep their own // gesture — setPointerCapture on this parent otherwise eats the click on // touch devices, so the sheet looks stuck open. const target = event.target; if (target instanceof Element && target.closest("button, a, input, textarea, select, [role='button']")) { return; } startY = event.clientY; setDragging(true); (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); }; const moveDrag = (event: PointerEvent) => { if (!dragging()) return; // Downward only: dragging up must not lift the sheet past its own top. setDragY(Math.max(0, event.clientY - startY)); }; const endDrag = () => { if (!dragging()) return; const travelled = dragY(); setDragging(false); setDragY(0); if (travelled > DISMISS_PX) props.onClose(); }; return (
{/* Both views share one grid cell and slide past each other; the cell takes the taller view's height, so the sheet does not jump when drilling in. `inert` keeps the off-screen view out of reach. */}
{(group) => (

{t(group.labelKey)}

{(item) => { const active = () => current()?.id === item.id; const rowClass = () => cn( "flex w-full items-center gap-2.5 rounded-md px-3 py-1.5 text-left transition-colors active:bg-muted", active() ? "text-primary-text" : "text-foreground", ); const content = ( <> {t(item.labelKey)} {t("nav.soon")} 0}> {badgeFor(item) > 99 ? "99+" : badgeFor(item)} ); return ( runAction(item)} class={rowClass()}> {content} } > {content} ); }}
)}
{(u) => (
)}
); }