From 580ff282143235ef6091438290d15418a66af47c Mon Sep 17 00:00:00 2001 From: bchanot Date: Sun, 8 Nov 2026 19:25:49 +0100 Subject: [PATCH] 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). --- apps/app/e2e/shell/editor-shell.spec.ts | 83 +++++++++++++ apps/app/src/components/shell/AppLayout.tsx | 73 ++--------- .../app/src/components/shell/MobileDrawer.tsx | 20 ++-- apps/app/src/components/shell/ShellChrome.tsx | 113 ++++++++++++++++++ apps/app/tests/unit/shell.AppLayout.test.tsx | 91 +++++++++++++- apps/app/tests/unit/shell.navigation.test.tsx | 11 +- 6 files changed, 317 insertions(+), 74 deletions(-) create mode 100644 apps/app/src/components/shell/ShellChrome.tsx diff --git a/apps/app/e2e/shell/editor-shell.spec.ts b/apps/app/e2e/shell/editor-shell.spec.ts index adfbda2f..e747bab9 100644 --- a/apps/app/e2e/shell/editor-shell.spec.ts +++ b/apps/app/e2e/shell/editor-shell.spec.ts @@ -11,6 +11,7 @@ import { openEditor, openReferences, openSection, + pickAnnotation, place, pointOf, seedDraft, @@ -244,3 +245,85 @@ test.describe('editor shell, width crossing (B2b)', () => { await expect(field).toHaveValue('Brouillon') }) }) + +// V1-2.6 U1 F (AC9): in the editor the sidebar shows from 1 280 px only; below, the top +// bar's menu button opens the drawer, which stays open while the sidebar stays hidden. +test.describe('editor shell, sidebar from 1 280 px (F)', () => { + test.use({ locale: 'fr-FR', actionTimeout: 15_000 }) + test.beforeEach(async ({ context }, info) => { + test.skip(isPhone(info), 'the widths are set by the tests themselves') // floor-guard: allow C16 editing needs >= 1024 px; phone read-only covered in editor-access + await routeToSandbox(context) + }) + test.afterEach(async ({ context }) => { + await dropLateReplays(context) + }) + + const nav = (page: Page) => + page.getByRole('navigation', { name: dict['shell.nav_label'] }) + const menu = (page: Page) => + page.getByRole('button', { name: dict['shell.open_menu'] }) + const drawer = (page: Page) => + page.getByRole('dialog', { name: dict['shell.nav_label'] }) + + async function measure(page: Page, width: number) { + await page.setViewportSize({ width, height: 800 }) + await selectTab(page, dict, 'annotations') + await pickAnnotation(page, dict, 'Point libre') + const canvas = await page.getByTestId('annotation-canvas').boundingBox() + const scroll = await page.evaluate(() => ({ + scrollWidth: document.documentElement.scrollWidth, + clientWidth: document.documentElement.clientWidth, + })) + console.log(`F-measure ${width}px canvas=${canvas?.width} ${JSON.stringify(scroll)}`) + return scroll + } + + test('the sidebar is hidden below 1 280 px and shown from 1 280 px', async ({ + page, + }) => { + await open(page) + for (const width of [1024, 1180, 1279]) { + const scroll = await measure(page, width) + expect(scroll.scrollWidth, `${width} px`).toBeLessThanOrEqual(scroll.clientWidth) + await expect(nav(page), `${width} px`).toHaveCount(0) + await expect(menu(page), `${width} px`).toBeVisible() + } + const scroll = await measure(page, 1280) + expect(scroll.scrollWidth).toBeLessThanOrEqual(scroll.clientWidth) + await expect(nav(page)).toBeVisible() + await expect(menu(page)).toHaveCount(0) + }) + + test('the menu is one tap away and the drawer stays open at 1 180 px', async ({ + page, + }) => { + await open(page) + await page.setViewportSize({ width: 1180, height: 800 }) + await menu(page).click() + await expect(drawer(page)).toBeVisible() + await page.setViewportSize({ width: 1279, height: 800 }) + await page.waitForTimeout(300) + await expect(drawer(page)).toBeVisible() + await page.setViewportSize({ width: 1280, height: 800 }) + await expect(drawer(page)).toBeHidden() + await expect(nav(page)).toBeVisible() + }) + + test('a drawer link asks before leaving while the draft has unsaved changes', async ({ + page, + }) => { + await open(page) + await page.setViewportSize({ width: 1180, height: 800 }) + await selectTab(page, dict, 'general') + await page.locator('#general-name-fr').fill('Travail non enregistré') + await menu(page).click() + await drawer(page) + .getByRole('link', { name: new RegExp(dict['shell.nav.account']) }) + .click() + const leave = page.getByRole('dialog', { name: dict['leave.title'] }) + await expect(leave).toBeVisible() + await leave.getByRole('button', { name: dict['leave.stay'] }).click() + await expect(leave).toBeHidden() + await expect(page).toHaveURL(/\/draft/) + }) +}) diff --git a/apps/app/src/components/shell/AppLayout.tsx b/apps/app/src/components/shell/AppLayout.tsx index c49330a3..ca92b61d 100644 --- a/apps/app/src/components/shell/AppLayout.tsx +++ b/apps/app/src/components/shell/AppLayout.tsx @@ -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 (
- +
-
- - - - -

- {t(pageTitleKey(location.pathname))} -

-
- - - - {displayName} - -
-
+ setDrawerOpen(true)} />
@@ -126,7 +69,11 @@ export default function AppLayout() {
- setDrawerOpen(false)} /> + setDrawerOpen(false)} + closesFrom={shell.closesFrom} + /> setLeaveTo(null)} diff --git a/apps/app/src/components/shell/MobileDrawer.tsx b/apps/app/src/components/shell/MobileDrawer.tsx index 94cafbe8..38f8a18b 100644 --- a/apps/app/src/components/shell/MobileDrawer.tsx +++ b/apps/app/src/components/shell/MobileDrawer.tsx @@ -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 {open && } +export default function MobileDrawer({ open, onClose, closesFrom = '64rem' }: MobileDrawerProps) { + return ( + + {open && } + + ) } -function DrawerSurface({ onClose }: Pick) { +function DrawerSurface({ onClose, closesFrom }: Required>) { 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 ( diff --git a/apps/app/src/components/shell/ShellChrome.tsx b/apps/app/src/components/shell/ShellChrome.tsx new file mode 100644 index 00000000..2f94d8ff --- /dev/null +++ b/apps/app/src/components/shell/ShellChrome.tsx @@ -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 ( + + ) +} + +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 ( +
+ + + + +

+ {t(pageTitleKey(pathname))} +

+
+ + + + {displayName} + +
+
+ ) +} diff --git a/apps/app/tests/unit/shell.AppLayout.test.tsx b/apps/app/tests/unit/shell.AppLayout.test.tsx index e5148044..a421ac8f 100644 --- a/apps/app/tests/unit/shell.AppLayout.test.tsx +++ b/apps/app/tests/unit/shell.AppLayout.test.tsx @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' -import { cleanup, render, screen, within } from '@testing-library/react' +import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react' import userEvent from '@testing-library/user-event' import { MemoryRouter, Route, Routes } from 'react-router-dom' import { createFakeSupabase, fakeProfile, fakeUser, type FakeSupabase } from '../helpers/fakeSupabase' @@ -13,6 +13,7 @@ vi.mock('@/lib/supabase', () => ({ import AppLayout from '@/components/shell/AppLayout' import { monogram } from '@/components/shell/UserBadge' import { useAuthStore } from '@/state/authStore' +import { useLeaveGuardStore } from '@/state/leaveGuardStore' import { useLocaleStore } from '@/state/localeStore' import { fr } from '@/i18n/fr' @@ -31,7 +32,28 @@ beforeEach(() => { avatarUrl: null, }) }) -afterEach(cleanup) +afterEach(() => { + cleanup() + vi.unstubAllGlobals() + useLeaveGuardStore.getState().arm(false) +}) + +/** A matchMedia whose queries match from `widthRem`; `fire` flips it to a wider screen. */ +function stubScreen(widthRem: number) { + const listeners = new Map void>>() + const min = (query: string) => Number(/min-width: (\d+)rem/.exec(query)![1]) + vi.stubGlobal('matchMedia', (query: string) => ({ + matches: widthRem >= min(query), + addEventListener: (_: string, fn: (e: { matches: boolean }) => void) => { + listeners.set(query, (listeners.get(query) ?? new Set()).add(fn)) + }, + removeEventListener: (_: string, fn: (e: { matches: boolean }) => void) => { + listeners.get(query)?.delete(fn) + }, + })) + return (query: string) => + act(() => listeners.get(query)?.forEach((fn) => fn({ matches: true }))) +} function mount(path = '/app') { return render( @@ -105,6 +127,7 @@ describe('AppLayout', () => { it('opens the mobile drawer from the menu button and closes it on navigation', async () => { const user = userEvent.setup() + stubScreen(40) mount() await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] })) const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] }) @@ -124,6 +147,70 @@ describe('AppLayout', () => { }) }) +describe('AppLayout, editor sidebar from 1 280 px', () => { + const sidebar = () => document.querySelector('aside') as HTMLElement + const topBarMenu = () => screen.getByRole('button', { name: fr['shell.open_menu'], hidden: true }) + + it('uses the xl class set on the editor path, the lg set elsewhere', () => { + mount('/app/admin/sheets/s-1/draft') + expect(sidebar()).toHaveClass('xl:flex') + expect(sidebar()).not.toHaveClass('lg:flex') + expect(topBarMenu()).toHaveClass('xl:hidden') + expect(screen.getByRole('heading', { level: 1, hidden: true })).toHaveClass('xl:block') + cleanup() + mount('/app/admin/sheets/s-1') + expect(sidebar()).toHaveClass('lg:flex') + expect(sidebar()).not.toHaveClass('xl:flex') + expect(topBarMenu()).toHaveClass('lg:hidden') + }) + + it('keeps the drawer open at 1 180 px in the editor, closes it from 1 280 px', async () => { + const user = userEvent.setup() + const fire = stubScreen(73.75) + mount('/app/admin/sheets/s-1/draft') + await user.click(topBarMenu()) + expect(await screen.findByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument() + fire('(min-width: 64rem)') + expect(screen.getByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument() + fire('(min-width: 80rem)') + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + }) + + it('closes the drawer on the 64rem query on other pages', async () => { + const user = userEvent.setup() + const fire = stubScreen(50) + mount('/app') + await user.click(topBarMenu()) + await screen.findByRole('dialog', { name: fr['shell.nav_label'] }) + fire('(min-width: 64rem)') + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + }) + + it('closes at setup when its query already matches', async () => { + const user = userEvent.setup() + stubScreen(73.75) + mount('/app') + await user.click(topBarMenu()) + await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull()) + cleanup() + mount('/app/admin/sheets/s-1/draft') + await user.click(topBarMenu()) + expect(await screen.findByRole('dialog', { name: fr['shell.nav_label'] })).toBeInTheDocument() + }) + + it('asks before leaving from a drawer link while changes are unsaved', async () => { + const user = userEvent.setup() + stubScreen(73.75) + mount('/app/admin/sheets/s-1/draft') + useLeaveGuardStore.getState().arm(true) + await user.click(topBarMenu()) + const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] }) + await user.click(within(drawer).getByRole('link', { name: new RegExp(fr['shell.nav.account']) })) + expect(screen.queryByText('account page')).toBeNull() + expect(await screen.findByText(fr['leave.title'])).toBeInTheDocument() + }) +}) + describe('monogram', () => { it('takes initials, else two letters, else ?', () => { expect(monogram('Alice Local', undefined)).toBe('AL') diff --git a/apps/app/tests/unit/shell.navigation.test.tsx b/apps/app/tests/unit/shell.navigation.test.tsx index c951ba01..75c741db 100644 --- a/apps/app/tests/unit/shell.navigation.test.tsx +++ b/apps/app/tests/unit/shell.navigation.test.tsx @@ -80,7 +80,10 @@ beforeEach(() => { avatarUrl: null, }) }) -afterEach(cleanup) +afterEach(() => { + cleanup() + vi.unstubAllGlobals() +}) describe('SidebarNav entries', () => { it('hides both V1-2 entries from an account without a right', () => { @@ -126,6 +129,12 @@ describe('SidebarNav entries', () => { it('carries the same entries into the mobile drawer', async () => { const user = userEvent.setup() + // A narrow screen: the drawer closes by itself once the sidebar breakpoint matches. + vi.stubGlobal('matchMedia', () => ({ + matches: false, + addEventListener: () => {}, + removeEventListener: () => {}, + })) mount([grant('sheet_editor')]) await user.click(screen.getByRole('button', { name: fr['shell.open_menu'] })) const drawer = await screen.findByRole('dialog', { name: fr['shell.nav_label'] })