Merge branch 'codex/mobile-portal-navigation'
This commit is contained in:
commit
f7a311dece
5 files changed
+230
-4
No files matched your search
@@ -195,6 +195,28 @@ a {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mobile-portal-menu-panel {
|
||||||
|
animation: mobile-portal-menu-enter 150ms ease-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes mobile-portal-menu-enter {
|
||||||
|
from {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-0.5rem);
|
||||||
|
}
|
||||||
|
|
||||||
|
to {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (prefers-reduced-motion: reduce) {
|
||||||
|
.mobile-portal-menu-panel {
|
||||||
|
animation: none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.public-account-page {
|
.public-account-page {
|
||||||
display: grid;
|
display: grid;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
|
|||||||
@@ -2,8 +2,12 @@
|
|||||||
|
|
||||||
import { readFileSync } from "node:fs";
|
import { readFileSync } from "node:fs";
|
||||||
|
|
||||||
import { render, screen } from "@testing-library/react";
|
import { fireEvent, render, screen } from "@testing-library/react";
|
||||||
import { expect, test } from "vitest";
|
import { expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
vi.mock("next/navigation", () => ({
|
||||||
|
usePathname: () => "/redaktion/alben",
|
||||||
|
}));
|
||||||
|
|
||||||
import { PortalShell } from "@/components/layout/portal-shell";
|
import { PortalShell } from "@/components/layout/portal-shell";
|
||||||
import { BrandLogo } from "./logo";
|
import { BrandLogo } from "./logo";
|
||||||
@@ -37,6 +41,11 @@ test("keeps masthead controls keyboard-focusable with a paper-coloured focus out
|
|||||||
// The brand link's accessible name is the section label (aria-label).
|
// The brand link's accessible name is the section label (aria-label).
|
||||||
expect(screen.getByRole("link", { name: /redaktionsarchiv/i }).tagName).toBe("A");
|
expect(screen.getByRole("link", { name: /redaktionsarchiv/i }).tagName).toBe("A");
|
||||||
expect(screen.getByRole("link", { name: "Alben" }).tagName).toBe("A");
|
expect(screen.getByRole("link", { name: "Alben" }).tagName).toBe("A");
|
||||||
|
const menuTrigger = screen.getByRole("button", { name: /menü öffnen/i });
|
||||||
|
expect(menuTrigger.parentElement?.className).toContain("sm:hidden");
|
||||||
|
fireEvent.click(menuTrigger);
|
||||||
|
expect(screen.getAllByRole("link", { name: "Alben" })).toHaveLength(2);
|
||||||
|
expect(screen.getByRole("navigation", { name: /mobile navigation/i })).toBeTruthy();
|
||||||
const styles = readFileSync("src/app/globals.css", "utf8");
|
const styles = readFileSync("src/app/globals.css", "utf8");
|
||||||
expect(styles).toContain(".portal-masthead :focus-visible");
|
expect(styles).toContain(".portal-masthead :focus-visible");
|
||||||
expect(styles).toContain("outline-color: var(--paper)");
|
expect(styles).toContain("outline-color: var(--paper)");
|
||||||
|
|||||||
@@ -0,0 +1,89 @@
|
|||||||
|
// @vitest-environment jsdom
|
||||||
|
|
||||||
|
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||||
|
import { afterEach, beforeEach, expect, test, vi } from "vitest";
|
||||||
|
|
||||||
|
const { usePathname } = vi.hoisted(() => ({
|
||||||
|
usePathname: vi.fn(),
|
||||||
|
}));
|
||||||
|
|
||||||
|
vi.mock("next/navigation", () => ({ usePathname }));
|
||||||
|
|
||||||
|
import { MobilePortalMenu } from "./mobile-portal-menu";
|
||||||
|
|
||||||
|
const nav = [
|
||||||
|
{ href: "/admin", label: "Dashboard" },
|
||||||
|
{ href: "/admin/redaktionen", label: "Redaktionen" },
|
||||||
|
{ href: "/admin/abrechnungen", label: "Abrechnungen" },
|
||||||
|
{ href: "/admin/audit", label: "Audit" },
|
||||||
|
];
|
||||||
|
|
||||||
|
beforeEach(() => usePathname.mockReturnValue("/admin/redaktionen/abc"));
|
||||||
|
afterEach(cleanup);
|
||||||
|
|
||||||
|
test("opens every mobile portal action and marks the most specific active route", () => {
|
||||||
|
render(<MobilePortalMenu nav={nav} sectionLabel="Administration" />);
|
||||||
|
|
||||||
|
const trigger = screen.getByRole("button", { name: /menü öffnen/i });
|
||||||
|
expect(trigger.getAttribute("aria-expanded")).toBe("false");
|
||||||
|
expect(screen.queryByRole("navigation", { name: /mobile navigation/i })).toBeNull();
|
||||||
|
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
|
||||||
|
expect(trigger.getAttribute("aria-expanded")).toBe("true");
|
||||||
|
expect(screen.getByRole("link", { name: "Dashboard" })).toBeTruthy();
|
||||||
|
expect(screen.getByRole("link", { name: "Redaktionen" }).getAttribute("aria-current")).toBe("page");
|
||||||
|
expect(screen.getByRole("link", { name: "Abrechnungen" })).toBeTruthy();
|
||||||
|
expect(screen.getByRole("link", { name: "Audit" })).toBeTruthy();
|
||||||
|
expect(screen.getByRole("button", { name: /design umschalten/i })).toBeTruthy();
|
||||||
|
expect(screen.getByRole("button", { name: /abmelden/i })).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test("keeps nested theme and logout buttons at least 44px tall", () => {
|
||||||
|
render(<MobilePortalMenu nav={nav} sectionLabel="Administration" />);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: /menü öffnen/i }));
|
||||||
|
|
||||||
|
const themeToggle = screen.getByRole("button", { name: /design umschalten/i });
|
||||||
|
const logoutButton = screen.getByRole("button", { name: /abmelden/i });
|
||||||
|
|
||||||
|
expect(themeToggle.parentElement?.getAttribute("class")).toContain("[&>button]:min-h-11");
|
||||||
|
expect(themeToggle.parentElement?.getAttribute("class")).toContain("[&>button]:min-w-11");
|
||||||
|
expect(logoutButton.parentElement?.getAttribute("class")).toContain("[&>button]:min-h-11");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("caps the open panel to the dynamic viewport and keeps every action scrollable", () => {
|
||||||
|
render(<MobilePortalMenu nav={nav} sectionLabel="Administration" />);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByRole("button", { name: /menü öffnen/i }));
|
||||||
|
|
||||||
|
const panel = screen.getByRole("navigation", { name: /mobile navigation/i }).parentElement;
|
||||||
|
expect(panel?.getAttribute("class")).toContain("max-h-[calc(100dvh-5.5rem)]");
|
||||||
|
expect(panel?.getAttribute("class")).toContain("overflow-y-auto");
|
||||||
|
expect(panel?.getAttribute("class")).toContain("overscroll-contain");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("closes after navigation, Escape and an outside pointer interaction", () => {
|
||||||
|
render(
|
||||||
|
<div>
|
||||||
|
<MobilePortalMenu nav={nav} sectionLabel="Administration" />
|
||||||
|
<button type="button">Außerhalb</button>
|
||||||
|
</div>,
|
||||||
|
);
|
||||||
|
|
||||||
|
const trigger = screen.getByRole("button", { name: /menü öffnen/i });
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
const auditLink = screen.getByRole("link", { name: "Audit" });
|
||||||
|
auditLink.addEventListener("click", (event) => event.preventDefault(), { once: true });
|
||||||
|
fireEvent.click(auditLink);
|
||||||
|
expect(trigger.getAttribute("aria-expanded")).toBe("false");
|
||||||
|
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
fireEvent.keyDown(document, { key: "Escape" });
|
||||||
|
expect(trigger.getAttribute("aria-expanded")).toBe("false");
|
||||||
|
expect(document.activeElement).toBe(trigger);
|
||||||
|
|
||||||
|
fireEvent.click(trigger);
|
||||||
|
fireEvent.pointerDown(screen.getByRole("button", { name: "Außerhalb" }));
|
||||||
|
expect(trigger.getAttribute("aria-expanded")).toBe("false");
|
||||||
|
});
|
||||||
@@ -0,0 +1,104 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import { useEffect, useMemo, useRef, useState } from "react";
|
||||||
|
import Link from "next/link";
|
||||||
|
import { usePathname } from "next/navigation";
|
||||||
|
import { Menu, X } from "lucide-react";
|
||||||
|
|
||||||
|
import type { NavItem } from "@/components/layout/portal-shell";
|
||||||
|
import { LogoutButton } from "@/components/layout/logout-button";
|
||||||
|
import { ThemeToggle } from "@/components/theme-toggle";
|
||||||
|
|
||||||
|
type MobilePortalMenuProps = {
|
||||||
|
nav: NavItem[];
|
||||||
|
sectionLabel: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function MobilePortalMenu({ nav, sectionLabel }: Readonly<MobilePortalMenuProps>) {
|
||||||
|
const pathname = usePathname();
|
||||||
|
const [open, setOpen] = useState(false);
|
||||||
|
const containerRef = useRef<HTMLDivElement>(null);
|
||||||
|
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||||
|
const menuId = "mobile-portal-menu";
|
||||||
|
|
||||||
|
const activeHref = useMemo(
|
||||||
|
() => nav
|
||||||
|
.filter(({ href }) => pathname === href || pathname.startsWith(`${href}/`))
|
||||||
|
.sort((left, right) => right.href.length - left.href.length)[0]?.href,
|
||||||
|
[nav, pathname],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!open) return;
|
||||||
|
|
||||||
|
function closeOnOutsidePointer(event: PointerEvent) {
|
||||||
|
if (!containerRef.current?.contains(event.target as Node)) setOpen(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function closeOnEscape(event: KeyboardEvent) {
|
||||||
|
if (event.key !== "Escape") return;
|
||||||
|
setOpen(false);
|
||||||
|
triggerRef.current?.focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("pointerdown", closeOnOutsidePointer);
|
||||||
|
document.addEventListener("keydown", closeOnEscape);
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener("pointerdown", closeOnOutsidePointer);
|
||||||
|
document.removeEventListener("keydown", closeOnEscape);
|
||||||
|
};
|
||||||
|
}, [open]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div ref={containerRef} className="relative sm:hidden">
|
||||||
|
<button
|
||||||
|
ref={triggerRef}
|
||||||
|
type="button"
|
||||||
|
aria-controls={menuId}
|
||||||
|
aria-expanded={open}
|
||||||
|
aria-label={open ? "Menü schließen" : "Menü öffnen"}
|
||||||
|
className="inline-flex min-h-11 items-center gap-2 rounded-md border border-border bg-background px-3 text-sm font-semibold"
|
||||||
|
onClick={() => setOpen((current) => !current)}
|
||||||
|
>
|
||||||
|
{open ? <X aria-hidden="true" className="size-5" /> : <Menu aria-hidden="true" className="size-5" />}
|
||||||
|
Menü
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{open ? (
|
||||||
|
<div
|
||||||
|
id={menuId}
|
||||||
|
className="mobile-portal-menu-panel absolute right-0 top-[calc(100%+0.75rem)] z-50 max-h-[calc(100dvh-5.5rem)] w-[min(22rem,calc(100vw-2rem))] overflow-y-auto overscroll-contain rounded-xl border border-border bg-background p-2 shadow-xl"
|
||||||
|
>
|
||||||
|
<p className="px-3 py-2 text-xs font-bold uppercase tracking-widest text-[var(--muted)]">
|
||||||
|
{sectionLabel}
|
||||||
|
</p>
|
||||||
|
<nav aria-label="Mobile Navigation" className="grid gap-1">
|
||||||
|
{nav.map((item) => {
|
||||||
|
const active = item.href === activeHref;
|
||||||
|
return (
|
||||||
|
<Link
|
||||||
|
key={item.href}
|
||||||
|
href={item.href}
|
||||||
|
aria-current={active ? "page" : undefined}
|
||||||
|
className={active
|
||||||
|
? "flex min-h-11 items-center rounded-lg bg-foreground px-3 font-semibold text-background"
|
||||||
|
: "flex min-h-11 items-center rounded-lg px-3 font-medium hover:bg-[var(--soft)]"}
|
||||||
|
onClick={() => setOpen(false)}
|
||||||
|
>
|
||||||
|
{item.label}
|
||||||
|
</Link>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</nav>
|
||||||
|
<div className="mt-2 flex min-h-11 items-center justify-between border-t border-border px-3 pt-2 [&>button]:min-h-11 [&>button]:min-w-11">
|
||||||
|
<span className="text-sm text-[var(--muted)]">Darstellung</span>
|
||||||
|
<ThemeToggle />
|
||||||
|
</div>
|
||||||
|
<div className="mt-2 border-t border-border px-3 pt-2 [&>button]:min-h-11">
|
||||||
|
<LogoutButton />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import Link from "next/link";
|
|||||||
|
|
||||||
import { BrandLogo } from "@/components/brand/logo";
|
import { BrandLogo } from "@/components/brand/logo";
|
||||||
import { LogoutButton } from "@/components/layout/logout-button";
|
import { LogoutButton } from "@/components/layout/logout-button";
|
||||||
|
import { MobilePortalMenu } from "@/components/layout/mobile-portal-menu";
|
||||||
import { ThemeToggle } from "@/components/theme-toggle";
|
import { ThemeToggle } from "@/components/theme-toggle";
|
||||||
|
|
||||||
export type NavItem = { href: string; label: string };
|
export type NavItem = { href: string; label: string };
|
||||||
@@ -30,7 +31,7 @@ export function PortalShell({
|
|||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-background flex flex-col">
|
<div className="min-h-screen bg-background flex flex-col">
|
||||||
<header className="sticky top-0 z-50 w-full border-b border-border/40 bg-background/80 backdrop-blur-md transition-colors duration-300">
|
<header className="sticky top-0 z-50 w-full border-b border-border/40 bg-background/80 backdrop-blur-md transition-colors duration-300">
|
||||||
<div className="flex h-16 items-center justify-between px-6 lg:px-12">
|
<div className="flex h-16 items-center justify-between px-4 sm:px-6 lg:px-12">
|
||||||
<Link aria-label={sectionLabel} className="flex items-center gap-2 transition-opacity hover:opacity-80" href={brandHref}>
|
<Link aria-label={sectionLabel} className="flex items-center gap-2 transition-opacity hover:opacity-80" href={brandHref}>
|
||||||
<div className="flex items-center"><BrandLogo /></div>
|
<div className="flex items-center"><BrandLogo /></div>
|
||||||
</Link>
|
</Link>
|
||||||
@@ -47,10 +48,11 @@ export function PortalShell({
|
|||||||
</Link>
|
</Link>
|
||||||
))}
|
))}
|
||||||
</nav>
|
</nav>
|
||||||
<div className="flex items-center gap-4">
|
<div className="hidden items-center gap-4 sm:flex">
|
||||||
<ThemeToggle />
|
<ThemeToggle />
|
||||||
<LogoutButton />
|
<LogoutButton />
|
||||||
</div>
|
</div>
|
||||||
|
<MobilePortalMenu nav={nav} sectionLabel={sectionLabel} />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</header>
|
</header>
|
||||||
|
|||||||
Reference in new issue
Block a user