In-app history only tracked the top-level page, so Back from a module sub-view (e.g. a Messages thread) or with a modal open jumped straight to the previous module. Add two history primitives: useHistorySubview (a module's sub-view rides in history.state, so Back returns to its root first) and useBackToClose (a mounted overlay occupies one history entry; Back closes the topmost, nesting-safe so a button-close doesn't trip the modals underneath). Apply to the Messages page views and to PlayerModal + the shared Modal. setPage now pushes a clean entry so each page starts at its root.
74 lines
2.4 KiB
TypeScript
74 lines
2.4 KiB
TypeScript
import { useEffect, useRef, type ReactNode } from "react";
|
|
import { createPortal } from "react-dom";
|
|
import { X } from "lucide-react";
|
|
import { useBackToClose } from "../lib/history";
|
|
|
|
// Stack of open modals so ESC only closes the topmost one — e.g. an error dialog over the
|
|
// tag editor: pressing ESC dismisses just the error and returns to the editor underneath.
|
|
let modalStack: number[] = [];
|
|
let nextModalId = 1;
|
|
|
|
// Small centered modal shell (portaled to <body>): backdrop + ESC + scroll-lock close.
|
|
export default function Modal({
|
|
title,
|
|
onClose,
|
|
children,
|
|
maxWidth = "max-w-lg",
|
|
}: {
|
|
title: ReactNode;
|
|
onClose: () => void;
|
|
children: ReactNode;
|
|
maxWidth?: string;
|
|
}) {
|
|
// Browser/mouse Back closes the topmost modal instead of navigating away.
|
|
useBackToClose(onClose);
|
|
|
|
// Keep a stable handler across renders so the stack id is assigned once per mount.
|
|
const onCloseRef = useRef(onClose);
|
|
onCloseRef.current = onClose;
|
|
useEffect(() => {
|
|
const id = nextModalId++;
|
|
modalStack.push(id);
|
|
const onKey = (e: KeyboardEvent) => {
|
|
if (e.key === "Escape" && modalStack[modalStack.length - 1] === id) {
|
|
e.stopPropagation();
|
|
onCloseRef.current();
|
|
}
|
|
};
|
|
window.addEventListener("keydown", onKey);
|
|
const prev = document.body.style.overflow;
|
|
document.body.style.overflow = "hidden";
|
|
return () => {
|
|
window.removeEventListener("keydown", onKey);
|
|
modalStack = modalStack.filter((x) => x !== id);
|
|
document.body.style.overflow = prev;
|
|
};
|
|
}, []);
|
|
|
|
return createPortal(
|
|
<div
|
|
className="fixed inset-0 z-50 flex items-center justify-center p-4 sm:p-6 bg-black/70 backdrop-blur-sm"
|
|
onClick={onClose}
|
|
role="dialog"
|
|
aria-modal="true"
|
|
>
|
|
<div
|
|
className={`glass relative w-full ${maxWidth} max-h-full overflow-y-auto rounded-2xl shadow-2xl`}
|
|
onClick={(e) => e.stopPropagation()}
|
|
>
|
|
<div className="flex items-start justify-between gap-3 p-5 pb-3">
|
|
<h2 className="text-lg font-semibold leading-snug">{title}</h2>
|
|
<button
|
|
onClick={onClose}
|
|
title="Close"
|
|
className="shrink-0 p-1.5 rounded-lg text-muted hover:text-fg hover:bg-surface transition"
|
|
>
|
|
<X className="w-4 h-4" />
|
|
</button>
|
|
</div>
|
|
<div className="px-5 pb-5">{children}</div>
|
|
</div>
|
|
</div>,
|
|
document.body
|
|
);
|
|
}
|