diff --git a/src/components/layout/app-shell.tsx b/src/components/layout/app-shell.tsx index 5d20d27..a82e724 100644 --- a/src/components/layout/app-shell.tsx +++ b/src/components/layout/app-shell.tsx @@ -4,6 +4,7 @@ import { LogoMark } from "@/components/brand/logo-mark"; import { AccountProfileDialog } from "@/components/users/account-profile-dialog"; import { CelebiPanel } from "@/components/layout/celebi-panel"; import { CommandPalette } from "@/components/layout/command-palette"; +import { MobileQuickActions } from "@/components/layout/mobile-quick-actions"; import { MobileTabBar } from "@/components/layout/mobile-tab-bar"; import { NavBar } from "@/components/layout/nav-bar"; import { NetworkStatusBanner } from "@/components/layout/network-status-banner"; @@ -22,6 +23,7 @@ import { commandPaletteOpen, openCommandPalette, setCommandPaletteOpen } from "@ import { ShellFeedProvider } from "@/stores/shell-feed-context"; import { usePreferences, useT } from "@/stores/preferences-context"; import { cn } from "@/lib/cn"; +import { createMediaQuery } from "@/lib/create-media-query"; import { ModuleGate } from "@/components/layout/module-gate"; const SIDEBAR_EXPANDED = "w-[260px]"; @@ -38,6 +40,10 @@ export function AppShell(props: ParentProps) { const collapsed = () => prefs.sidebarCollapsed(); const location = useLocation(); const fullScreen = () => location().pathname.startsWith("/exam-room/"); + // Below lg the header gives way to the floating action button. Chosen in + // JS rather than with CSS so only one of the two notification surfaces is + // mounted, and their dismissed state never has two in-memory copies. + const phone = createMediaQuery("(max-width: 63.98rem)"); const routeLabel = createMemo(() => { const key = routeLabelKey(location().pathname, auth.user()?.role); return key ? t(key) : ""; @@ -130,7 +136,7 @@ export function AppShell(props: ParentProps) {
- +
@@ -175,11 +181,16 @@ export function AppShell(props: ParentProps) {
- {/* MobileTabBar stays inside the provider — it is a shell surface, so a - useShellFeed() badge there must not throw. */} + {/* MobileTabBar and the quick actions stay inside the provider — they are + shell surfaces, so a useShellFeed() badge there must not throw. */} setMobileOpen(true)} onSearch={openCommandPalette} /> + + diff --git a/src/components/layout/floating-action-button.test.tsx b/src/components/layout/floating-action-button.test.tsx new file mode 100644 index 0000000..9905450 --- /dev/null +++ b/src/components/layout/floating-action-button.test.tsx @@ -0,0 +1,78 @@ +import { fireEvent, render, screen } from "@solidjs/testing-library"; +import { FloatingActionButton } from "@/components/layout/floating-action-button"; +import { IconBell } from "@/components/ui/icons"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +// A 400×700 layer: the hide target's top-left is (172, 632), the resting +// bottom-right button's is (332, 632). +const W = 400; +const H = 700; + +function renderFab(onSelect = vi.fn()) { + render(() => ( + + + )); + return { fab: screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false }), onSelect }; +} + +function drag(el: HTMLElement, from: [number, number], to: [number, number]) { + fireEvent.pointerDown(el, { pointerId: 1, clientX: from[0], clientY: from[1], button: 0, pointerType: "touch" }); + fireEvent.pointerMove(el, { pointerId: 1, clientX: (from[0] + to[0]) / 2, clientY: (from[1] + to[1]) / 2 }); + fireEvent.pointerMove(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); + fireEvent.pointerUp(el, { pointerId: 1, clientX: to[0], clientY: to[1] }); + fireEvent.click(el); +} + +describe("FloatingActionButton", () => { + beforeEach(() => { + localStorage.clear(); + vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue({ + width: W, height: H, top: 0, left: 0, right: W, bottom: H, x: 0, y: 0, toJSON: () => ({}), + } as DOMRect); + HTMLElement.prototype.setPointerCapture = vi.fn(); + }); + afterEach(() => vi.restoreAllMocks()); + + it("fans its actions out on a tap and runs the one picked", () => { + const { fab, onSelect } = renderFab(); + fireEvent.pointerDown(fab, { pointerId: 1, clientX: 10, clientY: 10, button: 0 }); + fireEvent.pointerUp(fab, { pointerId: 1, clientX: 12, clientY: 11 }); + fireEvent.click(fab); + expect(fab.getAttribute("aria-expanded")).toBe("true"); + + fireEvent.click(screen.getByRole("button", { name: "Bildirimler" })); + expect(onSelect).toHaveBeenCalledOnce(); + expect(fab.getAttribute("aria-expanded")).toBe("false"); + }); + + it("remembers where it was dropped, and a drag is not a tap", () => { + const { fab } = renderFab(); + drag(fab, [360, 660], [60, 300]); + expect(fab.getAttribute("aria-expanded")).toBe("false"); + const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); + expect(saved.parked).toBe(false); + expect(saved.x).toBeLessThan(0.1); + expect(saved.y).toBeGreaterThan(0.3); + expect(saved.y).toBeLessThan(0.5); + }); + + it("parks on the edge when dropped on the hide target and comes back with one tap", () => { + const { fab } = renderFab(); + // 332 → 172 horizontally lands the button on the hide target. + drag(fab, [360, 660], [200, 660]); + expect(screen.queryByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeNull(); + const saved = JSON.parse(localStorage.getItem("hezarfen.floatingButton")!); + // Parked, but still remembering the spot it was dragged from. + expect(saved).toEqual({ x: 1, y: 1, parked: true }); + + fireEvent.click(screen.getByRole("button", { name: /göster|show quick actions/i })); + expect(screen.getByRole("button", { name: /hızlı işlemler|quick actions/i, expanded: false })).toBeTruthy(); + expect(JSON.parse(localStorage.getItem("hezarfen.floatingButton")!).parked).toBe(false); + }); +}); diff --git a/src/components/layout/floating-action-button.tsx b/src/components/layout/floating-action-button.tsx new file mode 100644 index 0000000..f5d087b --- /dev/null +++ b/src/components/layout/floating-action-button.tsx @@ -0,0 +1,354 @@ +import { Index, Show, createEffect, createSignal, onCleanup, onMount, type Component } from "solid-js"; +import { IconChevronLeft, IconChevronRight, IconPlus, IconX } from "@/components/ui/icons"; +import { cn } from "@/lib/cn"; +import { clamp01, radialOffsets, readFloatingButton, writeFloatingButton } from "@/lib/floating-button"; +import { useT } from "@/stores/preferences-context"; + +export interface FloatingAction { + id: string; + label: string; + Icon: Component<{ class?: string }>; + badge?: number; + onSelect: () => void; +} + +/** Button diameter. */ +const SIZE = 56; +/** Gap the resting button keeps from the layer's edges. */ +const MARGIN = 12; +/** Radial item diameter. */ +const ITEM = 44; +/** Distance from the button centre to each radial item centre. */ +const RADIUS = 84; +/** Movement under this is a tap, not a drag — a thumb is never perfectly still. */ +const TAP_SLOP = 8; +/** How close the button centre has to come to the hide target to snap into it. */ +const HIDE_REACH = 64; + +const badgeText = (count: number) => (count > 9 ? "9+" : String(count)); +const clamp = (value: number, min: number, max: number) => Math.min(max, Math.max(min, value)); + +/** + * The phone shell's floating action button, standing in for the desktop + * header. Tapping it fans its actions out around it; dragging moves it + * anywhere between the status bar and the tab bar; dropping it on the hide + * target that rises while dragging parks it as a small tab on the nearest + * side edge, and one tap on that tab brings it back where it was. + * + * Position and parked state are remembered per device (lib/floating-button). + * Drag follows the pointer-event pattern of MobileNavSheet: pointer capture on + * the button itself, touch-none so the browser does not claim the gesture for + * scrolling. + */ +export function FloatingActionButton(props: { + actions: FloatingAction[]; + /** Unread total shown on the button and, as a dot, on the parked tab. */ + badge: number; + /** An overlay owns the screen; step aside without losing state. */ + hidden: boolean; +}) { + const t = useT(); + const saved = readFloatingButton(); + const [pos, setPos] = createSignal({ x: saved.x, y: saved.y }); + const [parked, setParked] = createSignal(saved.parked); + const [open, setOpen] = createSignal(false); + const [bounds, setBounds] = createSignal({ w: 0, h: 0 }); + // Live top-left while a drag is under way; null at rest. + const [drag, setDrag] = createSignal<{ left: number; top: number } | null>(null); + const [overHide, setOverHide] = createSignal(false); + + let layer: HTMLDivElement | undefined; + let fab: HTMLButtonElement | undefined; + const measure = () => { + if (!layer) return; + const rect = layer.getBoundingClientRect(); + setBounds({ w: rect.width, h: rect.height }); + }; + onMount(() => { + measure(); + window.addEventListener("resize", measure); + onCleanup(() => window.removeEventListener("resize", measure)); + }); + + const persist = () => writeFloatingButton({ ...pos(), parked: parked() }); + + const travelX = () => Math.max(0, bounds().w - SIZE - 2 * MARGIN); + const travelY = () => Math.max(0, bounds().h - SIZE - 2 * MARGIN); + const restLeft = () => MARGIN + pos().x * travelX(); + const restTop = () => MARGIN + pos().y * travelY(); + const hideLeft = () => bounds().w / 2 - SIZE / 2; + const hideTop = () => bounds().h - SIZE - MARGIN; + const left = () => (overHide() ? hideLeft() : (drag()?.left ?? restLeft())); + const top = () => (overHide() ? hideTop() : (drag()?.top ?? restTop())); + + createEffect(() => { + if (props.hidden) setOpen(false); + }); + createEffect(() => { + if (!open()) return; + const onKey = (event: KeyboardEvent) => { + if (event.key !== "Escape") return; + setOpen(false); + fab?.focus(); + }; + window.addEventListener("keydown", onKey); + onCleanup(() => window.removeEventListener("keydown", onKey)); + }); + + let start: { x: number; y: number; left: number; top: number } | null = null; + let moved = false; + // A drag that ends over the button is followed by a click on mouse (not + // always on touch), which must not toggle the menu. Reset on every press so + // a swallowed click cannot leave the next real tap ignored. + let suppressClick = false; + + const startDrag = (event: PointerEvent) => { + if (event.pointerType === "mouse" && event.button !== 0) return; + suppressClick = false; + moved = false; + start = { x: event.clientX, y: event.clientY, left: left(), top: top() }; + (event.currentTarget as HTMLElement).setPointerCapture(event.pointerId); + }; + + const moveDrag = (event: PointerEvent) => { + if (!start) return; + const dx = event.clientX - start.x; + const dy = event.clientY - start.y; + if (!moved && Math.hypot(dx, dy) < TAP_SLOP) return; + if (!moved) { + moved = true; + setOpen(false); + } + const next = { + left: clamp(start.left + dx, 0, bounds().w - SIZE), + top: clamp(start.top + dy, 0, bounds().h - SIZE), + }; + setDrag(next); + const hit = Math.hypot(next.left - hideLeft(), next.top - hideTop()) < HIDE_REACH; + if (hit && !overHide()) navigator.vibrate?.(10); + setOverHide(hit); + }; + + const endDrag = (commit: boolean) => { + if (!start) return; + start = null; + if (!moved) return; + suppressClick = true; + const last = drag(); + if (commit && overHide()) { + // Parked at the spot it was dragged from, so a tap on the tab puts it + // back there rather than on the hide target. + setParked(true); + persist(); + } else if (commit && last) { + setPos({ + x: travelX() ? clamp01((last.left - MARGIN) / travelX()) : pos().x, + y: travelY() ? clamp01((last.top - MARGIN) / travelY()) : pos().y, + }); + persist(); + } + setDrag(null); + setOverHide(false); + }; + + const toggle = () => { + if (suppressClick) { + suppressClick = false; + return; + } + setOpen(!open()); + }; + + const restore = () => { + setParked(false); + persist(); + }; + + const select = (action: FloatingAction) => { + setOpen(false); + action.onSelect(); + }; + + const centreX = () => left() + SIZE / 2; + const centreY = () => top() + SIZE / 2; + const offsets = () => + radialOffsets( + props.actions.length, + bounds().w ? centreX() / bounds().w : 1, + bounds().h ? centreY() / bounds().h : 1, + RADIUS, + ); + // Top-left of item i, kept inside the layer as a last resort for fans the + // heading alone does not keep on screen. + const itemPos = (i: number) => { + const offset = offsets()[i] ?? { dx: 0, dy: 0 }; + return { + left: clamp(centreX() + offset.dx - ITEM / 2, 4, bounds().w - ITEM - 4), + top: clamp(centreY() + offset.dy - ITEM / 2, 4, bounds().h - ITEM - 4), + }; + }; + const parkedRight = () => pos().x >= 0.5; + const dragging = () => drag() !== null; + + return ( + <> + + + ); + }} + + + + + + + + {/* Parked: a slim tab on the side the button rested nearest. The + visible pill is narrow; the button around it is a full thumb + target. */} + + + + + ); +} diff --git a/src/components/layout/mobile-quick-actions.tsx b/src/components/layout/mobile-quick-actions.tsx new file mode 100644 index 0000000..12b26cd --- /dev/null +++ b/src/components/layout/mobile-quick-actions.tsx @@ -0,0 +1,81 @@ +import { createEffect, createMemo, createSignal } from "solid-js"; +import { useNavigate } from "@tanstack/solid-router"; +import { FloatingActionButton, type FloatingAction } from "@/components/layout/floating-action-button"; +import { createNotificationFeed } from "@/components/layout/notification-feed"; +import { NotificationList } from "@/components/layout/notification-list"; +import { SidePanel } from "@/components/ui/side-panel"; +import { IconBell, IconMessage, IconSparkles } from "@/components/ui/icons"; +import { openCelebiPanel } from "@/stores/celebi-panel"; +import { useModules } from "@/stores/modules-context"; +import { useShellFeed } from "@/stores/shell-feed-context"; +import { useT } from "@/stores/preferences-context"; + +/** + * What the desktop header offers — messages, notifications, Çelebi — on a + * phone, behind the floating action button. Notifications open in a panel + * rather than the header's popover: a popover anchored to a button the user + * can drag anywhere would open off screen. + */ +export function MobileQuickActions(props: { hidden: boolean }) { + const t = useT(); + const navigate = useNavigate(); + const modules = useModules(); + const feed = useShellFeed(); + const notifications = createNotificationFeed(); + const [notificationsOpen, setNotificationsOpen] = createSignal(false); + + createEffect(() => { + if (notificationsOpen()) notifications.refreshAll(); + }); + + const actions = createMemo(() => { + const list: FloatingAction[] = []; + if (modules.isEnabled("messages")) { + list.push({ + id: "messages", + label: t("nav.messages"), + Icon: IconMessage, + badge: feed.unreadMessages().total, + onSelect: () => void navigate({ to: "/messages" }), + }); + } + list.push({ + id: "notifications", + label: t("notifications.title"), + Icon: IconBell, + badge: notifications.unreadCount(), + onSelect: () => setNotificationsOpen(true), + }); + if (modules.isEnabled("chatbot")) { + list.push({ id: "celebi", label: t("ai.askCelebi"), Icon: IconSparkles, onSelect: openCelebiPanel }); + } + return list; + }); + + return ( + <> + {/* Unread messages are notifications too, so this one count covers both. */} +