feat(app): editor sidebar hidden below 1 280 px, menu one tap away

AC9: on the draft editor path AppLayout uses the xl class set (sidebar
from 1 280 px, menu button and brand below); other pages keep lg.
AppLayout split: Sidebar and TopBar in ShellChrome.tsx. MobileDrawer
takes closesFrom ('64rem' | '80rem') and also closes at setup when
its query already matches.

Rewritten assertions (R-24):
- shell.AppLayout "opens the mobile drawer" and shell.navigation
  "carries the same entries into the mobile drawer": stub a narrow
  matchMedia, the drawer now closes at setup at the happy-dom width.
New: unit tests (class sets, 64rem/80rem close, setup close, leave
guard on a drawer link) and editor-shell.spec F journeys (sidebar at
1024/1180/1279/1280, one-tap menu, drawer open at 1180, guard).
This commit is contained in:
bchanot committed 2026-11-08 19:25:49 +01:00
1 parent 89da8d3f55
commit 580ff28214
6 files changed
+317 -74

No files matched your search

+10 -63
View File
@@ -1,30 +1,16 @@
import { useState, type MouseEvent } from 'react'
import { Link, Outlet, matchPath, useLocation, useNavigate } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import { Outlet, matchPath, useLocation, useNavigate } from 'react-router-dom'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import Icon from '@/components/ui/Icon'
import MobileDrawer from './MobileDrawer'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge, { AvatarCircle } from './UserBadge'
import { DEFAULT_SHELL, EDITOR_SHELL, Sidebar, TopBar } from './ShellChrome'
import { usePurgeForeignCopies } from '@/app/usePurgeForeignCopies'
import { useAuthStore } from '@/state/authStore'
import { useLeaveGuardStore } from '@/state/leaveGuardStore'
import { useT } from '@/i18n/useT'
/** The draft editor takes the whole width (board 04's three columns). */
const DRAFT_EDITOR_PATH = '/app/admin/sheets/:sheetId/draft'
/** Title of the current /app page for the top bar, from the nav table. */
function pageTitleKey(pathname: string): string {
const match = NAV_ITEMS.find((item) =>
item.end ? item.to === pathname : pathname.startsWith(item.to) && item.to !== '/',
)
return match?.labelKey ?? 'shell.nav.dashboard'
}
/** An in-app link a plain click would follow: same origin, same tab, no modifier key. */
function followedLink(event: MouseEvent): HTMLAnchorElement | null {
if (event.defaultPrevented || event.button !== 0) return null
@@ -67,58 +53,15 @@ export default function AppLayout() {
setDrawerOpen(false)
navigate(to)
}
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const fullWidth = matchPath(DRAFT_EDITOR_PATH, location.pathname) !== null
const displayName = profile?.display_name ?? user?.email ?? ''
const shell = fullWidth ? EDITOR_SHELL : DEFAULT_SHELL
return (
<div className="flex min-h-dvh bg-[var(--color-bg)]" onClickCapture={askBeforeLeaving}>
<aside className="hidden lg:flex lg:w-64 lg:shrink-0 lg:flex-col border-r border-[var(--color-border)] bg-[var(--color-surface)]">
<div className="px-5 py-5">
<Link to="/app" aria-label={t('shell.nav.dashboard')} className="inline-block">
<Brand size="md" />
</Link>
</div>
<div className="flex-1 px-3">
<SidebarNav />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</aside>
<Sidebar shell={shell} />
<div className="flex min-w-0 flex-1 flex-col">
<header className="sticky top-0 z-30 flex h-14 items-center gap-3 border-b border-[var(--color-border)] bg-[var(--color-surface)]/95 px-3 backdrop-blur-sm sm:px-6 pt-[env(safe-area-inset-top)]">
<button
type="button"
onClick={() => setDrawerOpen(true)}
aria-label={t('shell.open_menu')}
aria-expanded={drawerOpen}
className="grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] lg:hidden"
>
<Icon name="menu" />
</button>
<Link to="/app" className="lg:hidden" aria-label={t('shell.nav.dashboard')}>
<Brand size="sm" />
</Link>
<h1 className="hidden lg:block text-sm font-medium text-[var(--color-ink-muted)]">
{t(pageTitleKey(location.pathname))}
</h1>
<div className="ml-auto flex items-center gap-2 sm:gap-3">
<LanguageSwitcher />
<Link
to="/app/account"
aria-label={t('shell.nav.account')}
data-testid="topbar-account"
className="flex min-h-11 items-center gap-2 rounded-full pr-1 pl-1 text-sm text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] sm:pr-3"
>
<AvatarCircle size="sm" />
<span className="hidden max-w-[12rem] truncate sm:inline">{displayName}</span>
</Link>
</div>
</header>
<TopBar shell={shell} menuOpen={drawerOpen} onOpenMenu={() => setDrawerOpen(true)} />
<main className="flex-1 px-4 py-6 sm:px-8 sm:py-8">
<div className={`mx-auto w-full ${fullWidth ? '' : 'max-w-4xl'}`}>
<Outlet />
@@ -126,7 +69,11 @@ export default function AppLayout() {
</main>
</div>
<MobileDrawer open={drawerOpen} onClose={() => setDrawerOpen(false)} />
<MobileDrawer
open={drawerOpen}
onClose={() => setDrawerOpen(false)}
closesFrom={shell.closesFrom}
/>
<Dialog
open={leaveTo !== null}
onClose={() => setLeaveTo(null)}
+12 -8
View File
@@ -12,27 +12,31 @@ import { useT } from '@/i18n/useT'
export interface MobileDrawerProps {
open: boolean
onClose: () => void
/** The sidebar's breakpoint (Tailwind `lg` or `xl`): from there the drawer has no reason to stay open. */
closesFrom?: '64rem' | '80rem'
}
/** The sidebar's breakpoint (Tailwind `lg`): from there the drawer has no reason to stay open. */
const LARGE_SCREEN = '(min-width: 64rem)'
/** Left-side navigation drawer for small screens (transform + opacity only). */
export default function MobileDrawer({ open, onClose }: MobileDrawerProps) {
return <AnimatePresence>{open && <DrawerSurface onClose={onClose} />}</AnimatePresence>
export default function MobileDrawer({ open, onClose, closesFrom = '64rem' }: MobileDrawerProps) {
return (
<AnimatePresence>
{open && <DrawerSurface onClose={onClose} closesFrom={closesFrom} />}
</AnimatePresence>
)
}
function DrawerSurface({ onClose }: Pick<MobileDrawerProps, 'onClose'>) {
function DrawerSurface({ onClose, closesFrom }: Required<Omit<MobileDrawerProps, 'open'>>) {
const t = useT()
const reduced = useReducedMotion()
const slide = reduced ? { x: 0 } : { x: '-100%' }
// A modal drawer left open while the window widens would keep the page inert.
useEffect(() => {
const query = window.matchMedia(LARGE_SCREEN)
const query = window.matchMedia(`(min-width: ${closesFrom})`)
if (query.matches) onClose()
const onChange = (event: MediaQueryListEvent) => event.matches && onClose()
query.addEventListener('change', onChange)
return () => query.removeEventListener('change', onChange)
}, [onClose])
}, [onClose, closesFrom])
return (
<ModalDialog onClose={onClose} label={t('shell.nav_label')} className="open:block">
@@ -0,0 +1,113 @@
import { Link, useLocation } from 'react-router-dom'
import Brand from '@/components/brand/Brand'
import LanguageSwitcher from '@/components/brand/LanguageSwitcher'
import Icon from '@/components/ui/Icon'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge, { AvatarCircle } from './UserBadge'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/**
* Where the sidebar shows and the top bar swaps its menu button for the page title.
* Tailwind needs literal class strings: two static sets, never a built string.
*/
export interface ShellBreakpoint {
sidebar: string
menuControl: string
title: string
/** The width from which the drawer has no reason to stay open. */
closesFrom: '64rem' | '80rem'
}
/** Every page but the editor: the sidebar from 1 024 px (Tailwind `lg`). */
export const DEFAULT_SHELL: ShellBreakpoint = {
sidebar: 'hidden lg:flex lg:w-64 lg:shrink-0 lg:flex-col',
menuControl: 'lg:hidden',
title: 'hidden lg:block',
closesFrom: '64rem',
}
/** The draft editor needs the width for its three columns: sidebar from 1 280 px (`xl`). */
export const EDITOR_SHELL: ShellBreakpoint = {
sidebar: 'hidden xl:flex xl:w-64 xl:shrink-0 xl:flex-col',
menuControl: 'xl:hidden',
title: 'hidden xl:block',
closesFrom: '80rem',
}
/** Title of the current /app page for the top bar, from the nav table. */
function pageTitleKey(pathname: string): string {
const match = NAV_ITEMS.find((item) =>
item.end ? item.to === pathname : pathname.startsWith(item.to) && item.to !== '/',
)
return match?.labelKey ?? 'shell.nav.dashboard'
}
export function Sidebar({ shell }: { shell: ShellBreakpoint }) {
const t = useT()
return (
<aside
className={`${shell.sidebar} border-r border-[var(--color-border)] bg-[var(--color-surface)]`}
>
<div className="px-5 py-5">
<Link to="/app" aria-label={t('shell.nav.dashboard')} className="inline-block">
<Brand size="md" />
</Link>
</div>
<div className="flex-1 px-3">
<SidebarNav />
</div>
<div className="space-y-3 border-t border-[var(--color-border)] px-4 py-4">
<UserBadge />
<SignOutButton className="w-full" />
</div>
</aside>
)
}
interface TopBarProps {
shell: ShellBreakpoint
menuOpen: boolean
onOpenMenu: () => void
}
export function TopBar({ shell, menuOpen, onOpenMenu }: TopBarProps) {
const t = useT()
const { pathname } = useLocation()
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const displayName = profile?.display_name ?? user?.email ?? ''
return (
<header className="sticky top-0 z-30 flex h-14 items-center gap-3 border-b border-[var(--color-border)] bg-[var(--color-surface)]/95 px-3 backdrop-blur-sm sm:px-6 pt-[env(safe-area-inset-top)]">
<button
type="button"
onClick={onOpenMenu}
aria-label={t('shell.open_menu')}
aria-expanded={menuOpen}
className={`grid h-11 w-11 place-items-center rounded-lg text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] ${shell.menuControl}`}
>
<Icon name="menu" />
</button>
<Link to="/app" className={shell.menuControl} aria-label={t('shell.nav.dashboard')}>
<Brand size="sm" />
</Link>
<h1 className={`${shell.title} text-sm font-medium text-[var(--color-ink-muted)]`}>
{t(pageTitleKey(pathname))}
</h1>
<div className="ml-auto flex items-center gap-2 sm:gap-3">
<LanguageSwitcher />
<Link
to="/app/account"
aria-label={t('shell.nav.account')}
data-testid="topbar-account"
className="flex min-h-11 items-center gap-2 rounded-full pr-1 pl-1 text-sm text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] sm:pr-3"
>
<AvatarCircle size="sm" />
<span className="hidden max-w-[12rem] truncate sm:inline">{displayName}</span>
</Link>
</div>
</header>
)
}