forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

mobile-nav-sheet.tsx247 lines, 10.4 KiBHistoryRawDownload
1import { For, Show, createEffect, createSignal, onCleanup } from "solid-js";
2import { Link, useRouterState } from "@tanstack/solid-router";
3import { routeNavItem, visibleNavGroups, type NavItem } from "@/components/layout/nav-items";
4import { SidebarAccount } from "@/components/layout/sidebar-account";
5import { IconX } from "@/components/ui/icons";
6import { cn } from "@/lib/cn";
7import { useAuth } from "@/stores/auth-context";
8import { useModules } from "@/stores/modules-context";
9import { useShellFeed } from "@/stores/shell-feed-context";
10import { useT } from "@/stores/preferences-context";
11
12/**
13 * The phone-sized navigation menu: a sheet that rises from the bottom of the
14 * screen, where a thumb already is, instead of the side drawer this replaces.
15 *
16 * It stays mounted and is moved off-screen when closed, so opening and closing
17 * both animate; a `<Show>` around it would make it appear and disappear in one
18 * frame.
19 *
20 * It closes by tapping the X, tapping outside, or dragging it down past
21 * DISMISS_PX — the gesture people expect from a sheet.
22 */
23
24/** How far the sheet has to be pulled down before letting go dismisses it. */
25const DISMISS_PX = 96;
26export function MobileNavSheet(props: {
27 open: boolean;
28 onClose: () => void;
29 onLogout: () => void | Promise<void>;
30 onOpenCelebi?: () => void;
31 onOpenProfile?: () => void;
32}) {
33 const auth = useAuth();
34 const t = useT();
35 const feed = useShellFeed();
36 const modules = useModules();
37 const pathname = useRouterState({ select: (state) => state.location.pathname });
38 const search = useRouterState({ select: (state) => state.location.search as Record<string, unknown> });
39
40 // The primary destinations are already one tap away in the tab bar, so the
41 // sheet lists everything else.
42 const groups = () => visibleNavGroups(auth.user()?.role, modules.enabled());
43 const current = () => routeNavItem(pathname(), auth.user()?.role, search());
44 const badgeFor = (item: NavItem) => (item.id === "messages" ? feed.unreadMessages().total : 0);
45 const runAction = (item: NavItem) => {
46 props.onClose();
47 if (item.action === "celebi") props.onOpenCelebi?.();
48 if (item.action === "profile") props.onOpenProfile?.();
49 };
50
51 // Drag-to-dismiss. Only the grab area drives it, so a flick inside the list
52 // still scrolls the list. While dragging, the sheet follows the finger with
53 // its transition off; on release it either closes or springs back.
54 const [dragY, setDragY] = createSignal(0);
55 const [dragging, setDragging] = createSignal(false);
56 let startY = 0;
57 // Keyboard users arrive here through the Menü tab control. Park focus on
58 // the close button while the sheet is up (Escape closes it too) and hand
59 // focus back to the Menü trigger on dismiss, so focus never strands on
60 // the body or inside an off-screen panel.
61 let closeBtn: HTMLButtonElement | undefined;
62 let wasOpen = false;
63 createEffect(() => {
64 if (props.open) {
65 wasOpen = true;
66 closeBtn?.focus();
67 const onKey = (event: KeyboardEvent) => {
68 if (event.key === "Escape") props.onClose();
69 };
70 window.addEventListener("keydown", onKey);
71 onCleanup(() => window.removeEventListener("keydown", onKey));
72 } else if (wasOpen) {
73 wasOpen = false;
74 document.getElementById("mobile-menu-trigger")?.focus();
75 }
76 });
77 const startDrag = (event: PointerEvent) => {
78 if (event.pointerType === "mouse" && event.button !== 0) return;
79 // Buttons inside a drag surface (the sheet's X) must keep their own
80 // gesture — setPointerCapture on this parent otherwise eats the click on
81 // touch devices, so the sheet looks stuck open.
82 const target = event.target;
83 if (target instanceof Element && target.closest("button, a, input, textarea, select, [role='button']")) {
84 return;
85 }
86 startY = event.clientY;
87 setDragging(true);
88 (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId);
89 };
90
91 const moveDrag = (event: PointerEvent) => {
92 if (!dragging()) return;
93 // Downward only: dragging up must not lift the sheet past its own top.
94 setDragY(Math.max(0, event.clientY - startY));
95 };
96
97 const endDrag = () => {
98 if (!dragging()) return;
99 const travelled = dragY();
100 setDragging(false);
101 setDragY(0);
102 if (travelled > DISMISS_PX) props.onClose();
103 };
104
105 return (
106 <div
107 // Closed: invisible + pointer-events-none. visibility:hidden alone still
108 // leaves pointer-events:auto, and on some mobile WebViews that full-screen
109 // z-60 layer swallows taps meant for dialogs/side panels underneath (z-50
110 // historically, now z-70). Open sheet stays above the tab bar (z-40).
111 class={cn(
112 "fixed inset-0 z-[60] lg:hidden",
113 props.open ? "pointer-events-auto visible" : "pointer-events-none invisible",
114 )}
115 aria-hidden={props.open ? undefined : "true"}
116 >
117 <button
118 type="button"
119 aria-label={t("nav.close")}
120 onClick={props.onClose}
121 class={cn(
122 "absolute inset-0 h-full w-full cursor-default bg-black/45 transition-opacity duration-300",
123 props.open ? "opacity-100" : "opacity-0",
124 )}
125 />
126
127 <div
128 // Only a dialog while it is actually up: left on permanently, this
129 // off-screen panel would answer every [role=dialog] query and sit in
130 // the accessibility tree next to whatever modal is really open.
131 role={props.open ? "dialog" : undefined}
132 aria-modal={props.open ? "true" : undefined}
133 aria-label={t("nav.menu")}
134 class={cn(
135 "absolute inset-x-0 bottom-0 flex max-h-[75vh] flex-col rounded-t-3xl border-t border-border bg-background",
136 "pb-[max(env(safe-area-inset-bottom),var(--android-nav-inset,0px))]",
137 "transition-transform duration-300 ease-out",
138 props.open ? "translate-y-0" : "translate-y-full",
139 )}
140 style={dragging() ? { transform: `translateY(${dragY()}px)`, transition: "none" } : undefined}
141 >
142 {/* Drag only on the handle — keeping the title/close row outside this
143 surface so the X receives taps on mobile (pointer capture on a
144 parent eats button clicks). touch-none keeps the browser from
145 claiming the vertical gesture for scrolling. */}
146 <div
147 class="shrink-0 touch-none"
148 onPointerDown={startDrag}
149 onPointerMove={moveDrag}
150 onPointerUp={endDrag}
151 onPointerCancel={endDrag}
152 >
153 <div class="flex justify-center pt-2.5 pb-1">
154 <span class="h-1 w-9 rounded-full bg-border" />
155 </div>
156 </div>
157
158 <div class="flex shrink-0 items-center justify-between px-4 pb-2 pt-1">
159 <h2 class="text-base font-semibold">{t("nav.menu")}</h2>
160 <button
161 type="button"
162 ref={(el) => {
163 closeBtn = el;
164 }}
165 onClick={props.onClose}
166 aria-label={t("nav.close")}
167 class="topbar-control flex h-8 w-8 items-center justify-center rounded-md outline-hidden focus-visible:ring-2 focus-visible:ring-ring"
168 >
169 <IconX class="h-4 w-4" />
170 </button>
171 </div>
172
173 <div class="no-scrollbar min-h-0 flex-1 overflow-y-auto overscroll-contain px-2 pb-1">
174 <For each={groups()}>
175 {(group) => (
176 <div class="mt-1.5 border-t border-border/60 pt-1.5 first:mt-0 first:border-t-0 first:pt-0">
177 <p class="px-3 pb-0.5 pt-1 text-[11px] font-semibold uppercase tracking-wider text-muted-foreground">
178 {t(group.labelKey)}
179 </p>
180 <div class="flex flex-col">
181 <For each={group.items}>
182 {(item) => {
183 const active = () => current()?.id === item.id;
184 const rowClass = () => cn(
185 "flex w-full items-center gap-2.5 rounded-md px-3 py-1.5 text-left transition-colors active:bg-muted",
186 active() ? "text-primary-text" : "text-foreground",
187 );
188 const content = (
189 <>
190 <span
191 class={cn(
192 "inline-flex size-7 shrink-0 items-center justify-center rounded-md",
193 active() ? "bg-primary/10 text-primary-text" : "bg-muted text-muted-foreground",
194 )}
195 >
196 <item.Icon class="h-4 w-4" />
197 </span>
198 <span class="min-w-0 flex-1 truncate text-[13px] font-medium">
199 {t(item.labelKey)}
200 </span>
201 <Show when={item.soon}>
202 <span class="shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground">
203 {t("nav.soon")}
204 </span>
205 </Show>
206 <Show when={badgeFor(item) > 0}>
207 <span class="flex h-5 min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 text-[11px] font-bold text-white">
208 {badgeFor(item) > 99 ? "99+" : badgeFor(item)}
209 </span>
210 </Show>
211 </>
212 );
213 return (
214 <Show
215 when={!item.action}
216 fallback={
217 <button type="button" onClick={() => runAction(item)} class={rowClass()}>
218 {content}
219 </button>
220 }
221 >
222 <Link to={item.to} search={item.search as never} onClick={props.onClose} aria-current={active() ? "page" : undefined} class={rowClass()}>
223 {content}
224 </Link>
225 </Show>
226 );
227 }}
228 </For>
229 </div>
230 </div>
231 )}
232 </For>
233 </div>
234
235 <SidebarAccount
236 menuPlacement="top-start"
237 onLogout={props.onLogout}
238 onNavigateAway={props.onClose}
239 onOpenSettings={() => {
240 props.onClose();
241 props.onOpenProfile?.();
242 }}
243 />
244 </div>
245 </div>
246 );
247}