- Split approve/dismiss application workflows with distinct audit logs, rate-limited guards and real error logging - Validate article status/date/id input and stop resetting publishedAt on every update - Validate guild updates (state, forum enums, non-empty name) behind rate-limited guard - Fix scheduled-article publishing (ignore NULL dates, set updatedAt, type-safe predicates) - Harden admin search API (LIKE escaping, query cap, per-user rate limit, round-robin result cap) and fix search dialog abort/res.ok/loading races - Lock down HTML sanitizer to an allowlist profile and add XSS tests - Improve mobile nav accessibility (unique id, dialog role, focus management, scroll lock, outside close) - Log swallowed server errors instead of silent catch blocks - Remove dead eslint config, drop unused dompurify deps, restore knip CI step, add Playwright config with smoke spec
219 lines
5.9 KiB
TypeScript
219 lines
5.9 KiB
TypeScript
"use client";
|
|
|
|
import { AnimatePresence, motion } from "motion/react";
|
|
import Image from "next/image";
|
|
import {
|
|
type ReactNode,
|
|
useCallback,
|
|
useEffect,
|
|
useId,
|
|
useRef,
|
|
useState,
|
|
} from "react";
|
|
import { mobileMenuVariants } from "@/lib/motion";
|
|
|
|
interface MobileNavProps {
|
|
children: ReactNode;
|
|
menuLabel?: string;
|
|
closeLabel?: string;
|
|
brandLabel?: string;
|
|
}
|
|
|
|
export function MobileNav({
|
|
children,
|
|
menuLabel = "Open menu",
|
|
closeLabel = "Close menu",
|
|
brandLabel = "",
|
|
}: MobileNavProps) {
|
|
const [open, setOpen] = useState(false);
|
|
const detailsRef = useRef<HTMLDivElement>(null);
|
|
const menuRef = useRef<HTMLDivElement>(null);
|
|
const toggleRef = useRef<HTMLButtonElement>(null);
|
|
const prevFocusRef = useRef<HTMLElement | null>(null);
|
|
const menuId = useId();
|
|
|
|
const handleEscape = useCallback(
|
|
(e: KeyboardEvent) => {
|
|
if (e.key === "Escape" && open) {
|
|
setOpen(false);
|
|
(prevFocusRef.current ?? toggleRef.current)?.focus();
|
|
}
|
|
},
|
|
[open],
|
|
);
|
|
|
|
useEffect(() => {
|
|
document.addEventListener("keydown", handleEscape);
|
|
return () => document.removeEventListener("keydown", handleEscape);
|
|
}, [handleEscape]);
|
|
|
|
// Initial focus, scroll-lock, click-outside close, focus restore.
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
prevFocusRef.current =
|
|
document.activeElement instanceof HTMLElement
|
|
? document.activeElement
|
|
: null;
|
|
menuRef.current
|
|
?.querySelector<HTMLElement>(
|
|
'a[href], button:not([disabled]), [tabindex]:not([tabindex="-1"])',
|
|
)
|
|
?.focus();
|
|
const prevOverflow = document.body.style.overflow;
|
|
document.body.style.overflow = "hidden";
|
|
|
|
function handlePointerDown(e: PointerEvent) {
|
|
if (
|
|
menuRef.current &&
|
|
!menuRef.current.contains(e.target as Node) &&
|
|
!toggleRef.current?.contains(e.target as Node)
|
|
) {
|
|
setOpen(false);
|
|
}
|
|
}
|
|
document.addEventListener("pointerdown", handlePointerDown);
|
|
return () => {
|
|
document.body.style.overflow = prevOverflow;
|
|
document.removeEventListener("pointerdown", handlePointerDown);
|
|
(prevFocusRef.current ?? toggleRef.current)?.focus();
|
|
};
|
|
}, [open]);
|
|
|
|
useEffect(() => {
|
|
if (!open) return;
|
|
const menu = menuRef.current;
|
|
if (!menu) return;
|
|
|
|
const focusableSelector =
|
|
'a[href], button:not([disabled]), textarea, input, select, [tabindex]:not([tabindex="-1"])';
|
|
|
|
function handleTab(e: KeyboardEvent) {
|
|
if (e.key !== "Tab" || !menu) return;
|
|
const focusable = Array.from(
|
|
menu.querySelectorAll<HTMLElement>(focusableSelector),
|
|
);
|
|
if (focusable.length === 0) return;
|
|
const first = focusable[0];
|
|
const last = focusable[focusable.length - 1];
|
|
|
|
if (e.shiftKey) {
|
|
if (document.activeElement === first) {
|
|
e.preventDefault();
|
|
last.focus();
|
|
}
|
|
} else {
|
|
if (document.activeElement === last) {
|
|
e.preventDefault();
|
|
first.focus();
|
|
}
|
|
}
|
|
}
|
|
|
|
menu.addEventListener("keydown", handleTab);
|
|
return () => menu.removeEventListener("keydown", handleTab);
|
|
}, [open]);
|
|
|
|
return (
|
|
<div ref={detailsRef} className="group relative md:hidden">
|
|
<button
|
|
ref={toggleRef}
|
|
type="button"
|
|
onClick={() => setOpen((o) => !o)}
|
|
className="list-none cursor-pointer flex items-center justify-center w-11 h-11 rounded-xl shrink-0
|
|
transition-colors duration-150
|
|
hover:bg-[color-mix(in_srgb,var(--color-primary)_12%,transparent)]
|
|
active:scale-[0.96]
|
|
focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-primary-readable,var(--color-primary))] focus-visible:ring-offset-2 focus-visible:ring-offset-[var(--color-navbar)]"
|
|
aria-label={open ? closeLabel : menuLabel}
|
|
aria-expanded={open}
|
|
aria-haspopup="dialog"
|
|
aria-controls={menuId}
|
|
>
|
|
<motion.svg
|
|
className="w-6 h-6"
|
|
animate={{ rotate: open ? 90 : 0 }}
|
|
transition={{ duration: 0.2 }}
|
|
viewBox="0 0 24 24"
|
|
fill="none"
|
|
stroke="currentColor"
|
|
strokeWidth="2"
|
|
strokeLinecap="round"
|
|
strokeLinejoin="round"
|
|
>
|
|
{open ? (
|
|
<>
|
|
<line x1="18" y1="6" x2="6" y2="18" />
|
|
<line x1="6" y1="6" x2="18" y2="18" />
|
|
</>
|
|
) : (
|
|
<>
|
|
<line x1="4" y1="7" x2="20" y2="7" />
|
|
<line x1="4" y1="12" x2="20" y2="12" />
|
|
<line x1="4" y1="17" x2="20" y2="17" />
|
|
</>
|
|
)}
|
|
</motion.svg>
|
|
</button>
|
|
|
|
<AnimatePresence>
|
|
{open && (
|
|
<motion.div
|
|
id={menuId}
|
|
role="dialog"
|
|
aria-modal="true"
|
|
aria-label={brandLabel || "Navigation menu"}
|
|
className="absolute left-0 top-full mt-2 w-[min(88vw,360px)] z-50 origin-top-left"
|
|
style={{ backgroundColor: "transparent" }}
|
|
variants={mobileMenuVariants}
|
|
initial="hidden"
|
|
animate="visible"
|
|
exit="exit"
|
|
>
|
|
<div
|
|
ref={menuRef}
|
|
className="flex flex-col gap-y-1 p-2 rounded-2xl border shadow-xl max-h-[calc(100dvh-5rem)] overflow-y-auto overscroll-contain
|
|
bg-[var(--color-dropdown,var(--color-surface,#fff))]"
|
|
style={{
|
|
borderColor:
|
|
"color-mix(in srgb, var(--color-text-muted) 12%, transparent)",
|
|
boxShadow:
|
|
"0 12px 40px color-mix(in srgb, rgb(0 0 0) 22%, transparent)",
|
|
}}
|
|
>
|
|
<div
|
|
className="flex items-center gap-2 px-3 py-2 mb-1 rounded-xl"
|
|
style={{
|
|
backgroundColor:
|
|
"color-mix(in srgb, var(--color-primary) 12%, transparent)",
|
|
}}
|
|
>
|
|
<Image
|
|
src="/assets/images/icons/navigation/home.png"
|
|
alt=""
|
|
width={22}
|
|
height={22}
|
|
className="shrink-0"
|
|
/>
|
|
<span
|
|
className="text-sm font-extrabold uppercase tracking-wide"
|
|
style={{
|
|
color:
|
|
"var(--color-navbar-text-readable, var(--color-text))",
|
|
}}
|
|
>
|
|
{brandLabel}
|
|
</span>
|
|
<span className="ml-auto text-[11px] font-semibold uppercase tracking-wider opacity-75">
|
|
{closeLabel}
|
|
</span>
|
|
</div>
|
|
|
|
{children}
|
|
</div>
|
|
</motion.div>
|
|
)}
|
|
</AnimatePresence>
|
|
</div>
|
|
);
|
|
}
|