diff --git a/app/globals.css b/app/globals.css index ecabb03ab..0c707eca0 100644 --- a/app/globals.css +++ b/app/globals.css @@ -667,14 +667,6 @@ html[data-window-controls] [data-own-window-close] { display: none; } -/* - * A sheet docked at the window's end (the calendar's event and task panels) has its title - * and close at the top and Save at the foot: toasts stand beside it, not on it (pim-p2-19). - */ -body:has([data-end-sheet]) [data-toast-container] { - inset-inline-end: calc(var(--end-sheet-width, 400px) + 1.25rem); -} - /* Toasts that step out of the focus's way go to the top end corner: below the controls there. */ html[data-window-controls] [data-toast-container][data-dodging] { top: calc(var(--titlebar-row-height) + 0.75rem) !important; diff --git a/components/calendar/__tests__/toast-beside-sheet-ux-p2-pim.test.tsx b/components/calendar/__tests__/toast-beside-sheet-ux-p2-pim.test.tsx index 148a6a4e5..de252fba2 100644 --- a/components/calendar/__tests__/toast-beside-sheet-ux-p2-pim.test.tsx +++ b/components/calendar/__tests__/toast-beside-sheet-ux-p2-pim.test.tsx @@ -4,39 +4,25 @@ import { describe, expect, it } from 'vitest'; // pim-p2-19: toasts at the top right covered the event sheet's title and // close button; at the bottom right they covered its Save. They stand beside -// a sheet docked at the window's end now. jsdom has no layout, so the rule is -// checked through the cascade: the stylesheet's own rule, applied in jsdom. +// a sheet docked at the window's end, or above its buttons when the window +// has no room beside it. lib/toast-placement does that for whatever is marked +// (components/ui/__tests__/toast-placement.test.tsx); here: the marks. -const css = readFileSync(path.join(process.cwd(), 'app', 'globals.css'), 'utf8'); - -function endSheetRule(): string { - const match = /body:has\(\[data-end-sheet\]\) \[data-toast-container\] \{[^}]*\}/.exec(css); - expect(match, 'no end-sheet rule for toasts in globals.css').not.toBeNull(); - return match![0]; -} +const read = (...parts: string[]) => readFileSync(path.join(process.cwd(), ...parts), 'utf8'); describe('toasts beside an end sheet', () => { - it('move the stack past the sheet while one is open', () => { - const style = document.createElement('style'); - style.textContent = endSheetRule(); - document.head.appendChild(style); - const container = document.createElement('div'); - container.setAttribute('data-toast-container', ''); - document.body.appendChild(container); - expect(getComputedStyle(container).getPropertyValue('inset-inline-end')).not.toContain('400px'); - - const sheet = document.createElement('div'); - sheet.setAttribute('data-end-sheet', ''); - document.body.appendChild(sheet); - expect(getComputedStyle(container).getPropertyValue('inset-inline-end')).toBe('calc(var(--end-sheet-width, 400px) + 1.25rem)'); - style.remove(); - container.remove(); - sheet.remove(); + it('leaves the place to lib/toast-placement, with no fixed width in the stylesheet', () => { + expect(read('app', 'globals.css')).not.toMatch(/\[data-end-sheet\][^{]*\[data-toast-container\]/); }); it('marks the calendar event and task panels as end sheets', () => { - const app = readFileSync(path.join(process.cwd(), 'components', 'calendar', 'calendar-app.tsx'), 'utf8'); + const app = read('components', 'calendar', 'calendar-app.tsx'); expect(app).toMatch(/data-testid="event-panel" data-end-sheet=""/); expect(app).toMatch(/data-end-sheet="" className="w-\[400px\][^"]*"[\s\S]{0,40} { + expect(read('components', 'calendar', 'event-modal.tsx').match(/data-toast-avoid="bottom"/g)).toHaveLength(2); + expect(read('components', 'calendar', 'task-modal.tsx')).toContain('data-toast-avoid="bottom"'); + }); }); diff --git a/components/calendar/event-modal.tsx b/components/calendar/event-modal.tsx index a4b0ed5ed..f9502d0b4 100644 --- a/components/calendar/event-modal.tsx +++ b/components/calendar/event-modal.tsx @@ -1327,7 +1327,7 @@ export function EventModal({ )} {/* Action Bar */} -
+
{onDelete && canEditBody && ( onDelete(event.id, hasParticipants || undefined)} className="text-destructive hover:text-destructive"> @@ -1721,7 +1721,7 @@ export function EventModal({
-
+
{isEdit && onDelete && ( onDelete(event!.id, hasParticipants || undefined)} className="text-destructive hover:text-destructive"> diff --git a/components/calendar/task-modal.tsx b/components/calendar/task-modal.tsx index eab0f174f..7f698df23 100644 --- a/components/calendar/task-modal.tsx +++ b/components/calendar/task-modal.tsx @@ -393,7 +393,7 @@ export function TaskModal({
{/* Footer */} -
+
{isEdit && onDelete && task && ( :

next page

; + } + const { rerender } = render(<>); + act(() => { toast.success('Settings saved'); }); + act(() => { (document.getElementById('save') as HTMLElement).focus(); }); + await nextFrame(); + expect(stack()).toHaveAttribute('data-dodging'); + + rerender(<>); + await nextFrame(); + expect(stack()).not.toHaveAttribute('data-dodging'); + expect(stack().style.bottom).toBe('20px'); + }); + + it('stays at the bottom while the focus is in the editor of the reply dock under it', async () => { + // Above the dock's footer the stack covered the lower corner of the + // editor; stepping up for it put the toasts over the reader's toolbar. + render( + <> + + +
Thanks
+ , + ); + act(() => { (document.getElementById('editor') as HTMLElement).focus(); }); + act(() => { toast.success('Archived'); }); + await nextFrame(); + expect(stack()).not.toHaveAttribute('data-dodging'); + expect(stack().style.bottom).toBe(`${56 + 12}px`); + }); + + it('holds still while the mouse is on its way to it, and catches up once it leaves', async () => { + const { rerender } = render(<>); + act(() => { toast.success('Archived'); }); + expect(stack().style.bottom).toBe('20px'); + + // The pointer is next to the stack when the quick reply box appears. + fireEvent.pointerMove(document, { pointerType: 'mouse', clientX: W - 200, clientY: H - 60 }); + rerender(<>); + await nextFrame(); + expect(stack().style.bottom).toBe('20px'); + + fireEvent.pointerMove(document, { pointerType: 'mouse', clientX: 100, clientY: 100 }); + await nextFrame(); + expect(stack().style.bottom).toBe(`${90 + 12}px`); + }); + + it('keeps every toast while the pointer is on the stack, so none goes from under it', () => { + render(); + act(() => { toast.success('First'); }); + act(() => { toast.success('Second'); }); + fireEvent.pointerOver(screen.getByText('Second'), { pointerType: 'mouse' }); + act(() => { vi.advanceTimersByTime(20000); }); + expect(screen.getByText('First')).toBeInTheDocument(); + expect(screen.getByText('Second')).toBeInTheDocument(); + + fireEvent.pointerLeave(stack(), { pointerType: 'mouse' }); + act(() => { vi.advanceTimersByTime(6000); }); + expect(screen.queryByText('First')).not.toBeInTheDocument(); + }); +}); diff --git a/components/ui/toast.tsx b/components/ui/toast.tsx index 8de8acca0..b43572c76 100644 --- a/components/ui/toast.tsx +++ b/components/ui/toast.tsx @@ -8,6 +8,7 @@ import { cn } from "@/lib/utils"; import { TOUCH_HIT_AREA } from "@/components/ui/button"; import { useContextMenuOpen } from "@/components/ui/context-menu"; import { useMediaQuery, narrowerThan } from "@/hooks/use-media-query"; +import { computeToastPlacement, stackWouldCoverFocus, TOAST_AVOID_BOTTOM, TOAST_AVOID_END, TOAST_EDGE } from "@/lib/toast-placement"; export type ToastType = "success" | "error" | "info" | "warning"; @@ -62,6 +63,8 @@ interface ToastProps { onClose: (id: string) => void; /** Kept out of sight for now (a menu is open over it): the countdown waits. */ held?: boolean; + /** The pointer is on the stack: counts as hovering every toast in it. */ + stackHovered?: boolean; /** The toast goes away with the keyboard focus in it: put the focus back. */ onFocusLost?: () => void; } @@ -105,7 +108,7 @@ const progressBarStyles = { // A sideways swipe this far (px) on a touch screen dismisses the toast. const SWIPE_DISMISS_DISTANCE = 72; -export function ToastItem({ toast, onClose, held = false, onFocusLost }: ToastProps) { +export function ToastItem({ toast, onClose, held = false, stackHovered = false, onFocusLost }: ToastProps) { const tCommon = useTranslations("common"); const Icon = icons[toast.type]; const [exiting, setExiting] = useState(false); @@ -150,7 +153,7 @@ export function ToastItem({ toast, onClose, held = false, onFocusLost }: ToastPr }, [toast, dismiss]); const canPause = toast.pauseOnHover !== false; - const waiting = paused || held || (canPause && (hovered || focused)); + const waiting = paused || held || (canPause && (hovered || focused || stackHovered)); useEffect(() => { if (!toast.duration || toast.duration <= 0 || isProgress) return; @@ -361,38 +364,121 @@ export function ToastItem({ toast, onClose, held = false, onFocusLost }: ToastPr } /** - * True when the toast stack, in its usual place at the bottom, hides at - * least half of the element that has the focus: a button or a field. A - * large element it only overlaps at a corner (the composer's text, the - * quick reply box) stays usable, and moving the stack to the top for it put - * the toasts over the composer's close button and the reader's toolbar. The - * stack's own box is measured where it is now; while it sits at the top, - * its bottom place is worked out from the gap it left there - * (data-bottom-gap). + * Keeps the stack where lib/toast-placement puts it, while toasts are up: at + * the bottom end corner above what is docked at the bottom on screen now, + * beside a sheet at the window's end, and at the top end corner while it + * would cover the focused element (WCAG 2.4.11; not on a phone, where the + * top is the header with Back and the message actions, #1472). Measured + * again in the frame after anything on the page changes, scrolls or + * resizes, and when the focus moves or the focused element goes. It stays + * put while the pointer is on or near it, so an Undo doesn't move away from + * the pointer heading for it. */ -export function stackWouldCoverFocus(stack: HTMLElement | null): boolean { - if (!stack) return false; - const focused = document.activeElement as HTMLElement | null; - if (!focused || focused === document.body || stack.contains(focused)) return false; - const box = stack.getBoundingClientRect(); - if (box.height === 0) return false; - let top = box.top; - let bottom = box.bottom; - if (stack.dataset.dodging) { - const gap = Number(stack.dataset.bottomGap || 20); - bottom = window.innerHeight - gap; - top = bottom - box.height; - } else { - stack.dataset.bottomGap = String(Math.max(0, window.innerHeight - box.bottom)); - } - const f = focused.getBoundingClientRect(); - const overlapWidth = Math.min(f.right, box.right) - Math.max(f.left, box.left); - const overlapHeight = Math.min(f.bottom, bottom) - Math.max(f.top, top); - if (overlapWidth <= 0 || overlapHeight <= 0) return false; - const area = Math.max(f.width, 0) * Math.max(f.height, 0); - return area === 0 || overlapWidth * overlapHeight >= area / 2; +function useToastPlacement( + containerRef: React.RefObject, + active: boolean, + phone: boolean, +): void { + useLayoutEffect(() => { + const stack = containerRef.current; + if (!active || !stack) return; + let frame = 0; + let pointer: { x: number; y: number } | null = null; + const near = () => { + if (!pointer || phone) return false; + const r = stack.getBoundingClientRect(); + if (r.width === 0 || r.height === 0) return false; + const m = POINTER_HOLD_MARGIN; + return pointer.x >= r.left - m && pointer.x <= r.right + m && pointer.y >= r.top - m && pointer.y <= r.bottom + m; + }; + const apply = (force = false) => { + frame = 0; + if (!force && near()) return; + const rtl = getComputedStyle(stack).direction === "rtl"; + const place = computeToastPlacement(stack, { phone, rtl }); + const dodge = !phone && stackWouldCoverFocus(stack, place, rtl); + const set = (prop: "bottom" | "top" | "insetInlineEnd", value: string) => { + if (stack.style[prop] !== value) stack.style[prop] = value; + }; + set("bottom", dodge ? "auto" : `${place.bottom}px`); + set("top", dodge ? `${TOAST_EDGE}px` : ""); + set("insetInlineEnd", place.end === null ? "" : `${place.end}px`); + if (dodge) { + if (stack.dataset.dodging === undefined) stack.dataset.dodging = "true"; + } else if (stack.dataset.dodging !== undefined) { + delete stack.dataset.dodging; + } + }; + const schedule = () => { + if (!frame) frame = requestAnimationFrame(() => apply()); + }; + // Before the first paint: a new toast appears in its place. + apply(true); + + // Something docked resized without a change in the page (a field growing). + const watched = new Set(); + const sizes = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(schedule); + const watch = () => { + if (!sizes) return; + const now = new Set(document.querySelectorAll(`${TOAST_AVOID_BOTTOM}, ${TOAST_AVOID_END}`)); + for (const el of watched) if (!now.has(el)) { sizes.unobserve(el); watched.delete(el); } + for (const el of now) if (!watched.has(el)) { sizes.observe(el); watched.add(el); } + }; + watch(); + // The page changed: a box opened, closed or grew, a route rendered another + // page. Changes inside the stack itself (its own toasts) don't count. + const mutations = new MutationObserver((records) => { + if (!records.some((r) => !stack.contains(r.target))) return; + watch(); + schedule(); + }); + mutations.observe(document.body, { + childList: true, + subtree: true, + attributes: true, + attributeFilter: ["class", "style", "hidden", "open", "data-toast-avoid", "data-end-sheet"], + }); + + const onPointer = (e: PointerEvent) => { + if (e.pointerType !== "mouse" && e.pointerType !== "pen") return; + const wasNear = near(); + pointer = { x: e.clientX, y: e.clientY }; + // Leaving: catch up on what changed meanwhile. + if (wasNear && !near()) schedule(); + }; + const onPointerOut = (e: PointerEvent) => { + if (e.relatedTarget) return; + pointer = null; + schedule(); + }; + // Focus moved, or the focused element went (no focusin then). + document.addEventListener("focusin", schedule); + document.addEventListener("focusout", schedule); + document.addEventListener("scroll", schedule, { capture: true, passive: true }); + document.addEventListener("transitionend", schedule, true); + document.addEventListener("animationend", schedule, true); + document.addEventListener("pointermove", onPointer, { passive: true }); + document.addEventListener("pointerout", onPointerOut); + window.addEventListener("resize", schedule); + return () => { + cancelAnimationFrame(frame); + mutations.disconnect(); + sizes?.disconnect(); + document.removeEventListener("focusin", schedule); + document.removeEventListener("focusout", schedule); + document.removeEventListener("scroll", schedule, { capture: true }); + document.removeEventListener("transitionend", schedule, true); + document.removeEventListener("animationend", schedule, true); + document.removeEventListener("pointermove", onPointer); + document.removeEventListener("pointerout", onPointerOut); + window.removeEventListener("resize", schedule); + }; + }, [containerRef, active, phone]); } +/** How close (px) the mouse has to be for the stack to hold still. */ +const POINTER_HOLD_MARGIN = 48; + export function ToastContainer({ toasts, onClose }: { toasts: Toast[]; onClose: (id: string) => void }) { const tCenter = useTranslations("notification_center"); const [mounted, setMounted] = useState(false); @@ -405,38 +491,19 @@ export function ToastContainer({ toasts, onClose }: { toasts: Toast[]; onClose: const containerRef = useRef(null); // Where the focus was before the shortcut moved it into a toast. const returnFocusRef = useRef(null); - // The stack moves to the top corner while it would cover the element that - // has the keyboard focus (WCAG 2.4.11): a Save button at the foot of the - // event editor sat right under it. Not on a phone: there the top of the - // screen is the header with Back and the message actions, which an Undo - // toast covered after a delete (#1472), and the stack already rides above - // the bottom bars. - const [dodge, setDodge] = useState(false); + // The pointer is on the stack: every countdown waits, so no toast goes + // and none moves under the pointer. + const [stackHovered, setStackHovered] = useState(false); useEffect(() => { setMounted(true); }, []); const hasToasts = toasts.length > 0; - useLayoutEffect(() => { - if (!hasToasts || phone) { - setDodge(false); - return; - } - let frame = 0; - const check = () => { - cancelAnimationFrame(frame); - frame = requestAnimationFrame(() => setDodge(stackWouldCoverFocus(containerRef.current))); - }; - check(); - document.addEventListener("focusin", check); - window.addEventListener("resize", check); - return () => { - cancelAnimationFrame(frame); - document.removeEventListener("focusin", check); - window.removeEventListener("resize", check); - }; - }, [hasToasts, toasts.length, phone]); + useToastPlacement(containerRef, mounted && hasToasts, phone); + useEffect(() => { + if (!hasToasts) setStackHovered(false); + }, [hasToasts]); useEffect(() => { if (!hasToasts) return; const onKeyDown = (e: KeyboardEvent) => { @@ -471,22 +538,13 @@ export function ToastContainer({ toasts, onClose }: { toasts: Toast[]; onClose: return createPortal(
0 ? "region" : undefined} aria-label={toasts.length > 0 ? tCenter("title") : undefined} style={{ pointerEvents: "none" }} + onPointerOver={(e) => { if (e.pointerType === "mouse") setStackHovered(true); }} + onPointerLeave={() => setStackHovered(false)} > {toasts.map((toast) => (
- +
))}
, diff --git a/hooks/use-bottom-controls.ts b/hooks/use-bottom-controls.ts index 9dcf2f2de..e0b91fc89 100644 --- a/hooks/use-bottom-controls.ts +++ b/hooks/use-bottom-controls.ts @@ -7,7 +7,9 @@ import { useEffect, useState } from 'react'; * bar and a viewer's action bar, on any screen an editor's Save/Cancel * footer. While any is on screen, * `--bottom-controls-height` on says how far from the bottom of the - * window the topmost of them reaches, and toasts sit above it. + * window the topmost of them reaches, for what floats above them (the + * install and push prompts). Each is also marked data-toast-avoid="bottom", + * so the toasts keep clear of it (lib/toast-placement). */ const bars = new Set(); let frame = 0; @@ -44,6 +46,7 @@ export function useBottomControls(active = true): (el: HTMLElement | null) => vo useEffect(() => { if (!el || !active) return; bars.add(el); + el.setAttribute('data-toast-avoid', 'bottom'); update(); const observer = typeof ResizeObserver === 'undefined' ? null : new ResizeObserver(schedule); observer?.observe(el); @@ -52,6 +55,7 @@ export function useBottomControls(active = true): (el: HTMLElement | null) => vo observer?.disconnect(); window.removeEventListener('resize', schedule); bars.delete(el); + el.removeAttribute('data-toast-avoid'); update(); }; }, [el, active]); diff --git a/integration/tests/27-do-toast-place.spec.ts b/integration/tests/27-do-toast-place.spec.ts new file mode 100644 index 000000000..573c5dea4 --- /dev/null +++ b/integration/tests/27-do-toast-place.spec.ts @@ -0,0 +1,345 @@ +/** + * Where toasts stand, measured in a real browser after each step people take + * while one is up: archive and Undo, another folder, the next message, the + * quick reply box focused, the reply dock opened and closed, Calendar with its + * event panel and back, window widths from 640 to 1920, a phone. After each + * step every toast has to be inside the window, clear of the others, clear of + * what is docked at the bottom (quick reply box, composer footer, the phone's + * bars) and of the calendar's panel, and in the lower part of the window + * unless it stepped up to the top corner for the focused element. And it + * holds still while the mouse moves to it. + * + * Runs as dso-ux-mail@example.org on Stalwart 1.0 (seeded by + * ~/desktop-offline/dso_uxmail_provision.py on the VM), with IT_DSO_PASSWORD + * from the vault item "Stalwart test – dso-* throwaway accounts", through + * desktop-offline-rig/run-spec.sh. Every archive is undone. IT_TOAST_SHOTS: a + * folder for a screenshot of each step. + */ +import { test, expect, type Browser, type Page } from '@playwright/test'; +import { existsSync, mkdirSync } from 'node:fs'; +import path from 'node:path'; +import { requireEntEnv } from './helpers/ent'; +import { accountSwitcher, neutralizeDevOverlay } from './helpers/app'; +import { WEBMAIL_URL } from './helpers/config'; + +requireEntEnv(); +test.setTimeout(600_000); +test.use({ actionTimeout: 30_000 }); + +const USER = 'dso-ux-mail@example.org'; +const PASSWORD = process.env.IT_DSO_PASSWORD ?? ''; +if (!PASSWORD) throw new Error('Set IT_DSO_PASSWORD, and check the account on the VM first: see the top of this file'); +const SHOTS = process.env.IT_TOAST_SHOTS ?? ''; + +interface Box { top: number; bottom: number; left: number; right: number } +interface Measured { + vw: number; + vh: number; + dodging: boolean; + toasts: (Box & { text: string })[]; + docked: (Box & { what: string })[]; + sheets: (Box & { footer: Box | null })[]; +} + +async function signIn(browser: Browser, opts: { width: number; height: number; phone?: boolean }): Promise { + const context = await browser.newContext({ + baseURL: WEBMAIL_URL, + locale: 'en-US', + colorScheme: 'dark', + viewport: { width: opts.width, height: opts.height }, + ...(opts.phone ? { isMobile: true, hasTouch: true, deviceScaleFactor: 3 } : {}), + }); + const page = await context.newPage(); + await neutralizeDevOverlay(page); + await page.goto('/'); + await page.fill('#username', USER); + await page.fill('#password', PASSWORD); + await page.click('button[type="submit"]'); + await accountSwitcher(page).waitFor({ state: 'attached', timeout: 60000 }); + await rows(page).first().waitFor({ timeout: 60000 }); + return page; +} + +const rows = (page: Page) => page.locator('[data-testid=email-list-item]'); +/** Rows already read: opening them changes nothing on the server. */ +const readRows = (page: Page) => page.locator('[data-testid=email-list-item][data-unread="false"]'); +const folder = (page: Page, role: string) => page.locator(`[data-testid=folder-row][data-folder-role=${role}] button`).first(); +const undoButton = (page: Page) => page.locator('[data-toast-container] button', { hasText: /^Undo$/ }).first(); + +/** Toasts and what they must keep clear of, as they are on screen now. */ +async function measure(page: Page): Promise { + return page.evaluate(() => { + const box = (el: Element) => { + const r = el.getBoundingClientRect(); + return { top: r.top, bottom: r.bottom, left: r.left, right: r.right }; + }; + const shown = (el: Element) => { + const r = el.getBoundingClientRect(); + return r.width > 0 && r.height > 0 && getComputedStyle(el).visibility !== 'hidden' + && r.bottom > 0 && r.top < innerHeight && r.right > 0 && r.left < innerWidth; + }; + const stack = document.querySelector('[data-toast-container]'); + const toasts = [...document.querySelectorAll('[data-toast-container] .toast-item')] + .filter((t) => !t.classList.contains('toast-exit')) + .map((t) => ({ ...box(t), text: (t as HTMLElement).innerText.replace(/\s+/g, ' ').slice(0, 40) })); + // Docked at the bottom, named the way the app has always named them. + const dockedSel = '[data-quick-reply-box], [data-composer-footer], nav[data-mobile-navigation]'; + const docked = [...document.querySelectorAll(dockedSel)].filter(shown).map((el) => ({ + ...box(el), + what: el.hasAttribute('data-quick-reply-box') ? 'quick reply' : el.hasAttribute('data-composer-footer') ? 'composer footer' : 'tab bar', + })); + // The phone viewer's action bar: fixed at the bottom of the window. + for (const nav of document.querySelectorAll('nav')) { + const r = nav.getBoundingClientRect(); + if (shown(nav) && getComputedStyle(nav).position === 'fixed' && Math.abs(r.bottom - innerHeight) < 2 && r.height < 120) { + docked.push({ ...box(nav), what: 'bottom bar' }); + } + } + const sheets = [...document.querySelectorAll('[data-end-sheet]')].filter(shown).map((s) => { + const bars = [...s.querySelectorAll('.border-t')].filter(shown); + const last = bars[bars.length - 1]; + return { ...box(s), footer: last ? box(last) : null }; + }); + return { + vw: document.documentElement.clientWidth, + vh: innerHeight, + dodging: !!stack && stack.dataset.dodging !== undefined, + toasts, + docked, + sheets, + }; + }); +} + +const overlap = (a: Box, b: Box) => + Math.min(a.right, b.right) - Math.max(a.left, b.left) > 1 && Math.min(a.bottom, b.bottom) - Math.max(a.top, b.top) > 1; + +/** What's wrong with where the toasts are now; empty when nothing is. */ +function problems(m: Measured): string[] { + const out: string[] = []; + m.toasts.forEach((t, i) => { + const name = `"${t.text}"`; + if (t.left < -1 || t.top < -1 || t.right > m.vw + 1 || t.bottom > m.vh + 1) out.push(`${name} runs out of the window: ${JSON.stringify(t)}`); + m.toasts.slice(i + 1).forEach((u) => { if (overlap(t, u)) out.push(`${name} overlaps "${u.text}"`); }); + for (const d of m.docked) if (overlap(t, d)) out.push(`${name} covers the ${d.what} (toast ${Math.round(t.top)}-${Math.round(t.bottom)}, ${d.what} from ${Math.round(d.top)})`); + for (const s of m.sheets) { + const room = m.vw - (s.right - s.left) - 40 >= t.right - t.left; + if (room && overlap(t, s)) out.push(`${name} covers the panel beside which it has room`); + if (s.footer && overlap(t, s.footer)) out.push(`${name} covers the panel's buttons`); + } + if (!m.dodging && t.top < m.vh * 0.35) out.push(`${name} stands in the middle of the window (top ${Math.round(t.top)} of ${m.vh})`); + if (m.dodging && t.top > 160) out.push(`${name} is marked as moved to the top but is at ${Math.round(t.top)}`); + }); + return out; +} + +async function check(page: Page, step: string, found: string[], opts: { wantToast?: boolean } = {}): Promise { + // The enter animation slides in from the side; the place is what counts. + await page.waitForTimeout(450); + await page.waitForFunction(() => [...document.querySelectorAll('[data-toast-container] .toast-item')] + .every((t) => t.getAnimations().every((a) => a.playState !== 'running')), undefined, { timeout: 5000 }).catch(() => {}); + const m = await measure(page); + if (SHOTS) { + if (!existsSync(SHOTS)) mkdirSync(SHOTS, { recursive: true }); + await page.screenshot({ path: path.join(SHOTS, `toast-${step}.png`) }); + } + const now = problems(m); + if (opts.wantToast !== false && m.toasts.length === 0) now.push('no toast on screen to check'); + for (const p of now) found.push(`${step}: ${p}`); + console.log(`${step}: ${m.toasts.length} toast(s)${m.dodging ? ', at the top' : ''}${now.length ? ' -- ' + now.join('; ') : ', fine'}`); + return m; +} + +const archived: string[] = []; + +/** The subject of the selected row: the message that is open. */ +async function openSubject(page: Page): Promise { + const subject = await page.locator('[data-testid=email-list-item][aria-selected="true"]').first().getAttribute('data-subject'); + if (!subject) throw new Error('no message open'); + return subject; +} + +/** Archive the open message with its key (or `click`): an "Archived · Undo" toast. */ +async function archive(page: Page, click?: () => Promise): Promise { + await notLimited(page); + const subject = await openSubject(page); + const before = await page.locator('[data-toast-container] .toast-item', { hasText: 'Archived' }).count(); + if (click) await click(); else await page.keyboard.press('e'); + await expect.poll(() => page.locator('[data-toast-container] .toast-item', { hasText: 'Archived' }).count(), { timeout: 15000 }).toBeGreaterThan(before); + archived.push(subject); +} + +/** Undo the last archive with the keyboard, from the Inbox: the message is back in the list. */ +async function undoLast(page: Page): Promise { + await notLimited(page); + const subject = archived.pop(); + if (!subject) throw new Error('nothing to undo'); + await rows(page).first().focus(); + await page.keyboard.press('Control+z'); + await page.locator(`[data-testid=email-list-item][data-subject="${subject.replace(/"/g, '\\"')}"]`).first().waitFor({ state: 'attached', timeout: 60000 }); +} + +/** + * The test server limits requests per address, which other runs on this + * machine share: wait while the app says it's paused, so a step doesn't fail on it. + */ +async function notLimited(page: Page): Promise { + await expect(page.getByText('temporarily rate limited')).toHaveCount(0, { timeout: 120000 }); +} + +async function openRow(page: Page, n: number): Promise { + await notLimited(page); + await readRows(page).nth(n).click(); + await page.locator('[data-quick-reply-box]').first().waitFor({ timeout: 30000 }); +} + +test('toasts keep their place on the desktop: folders, next message, reply box and dock, calendar, widths, hover', async ({ browser }) => { + const page = await signIn(browser, { width: 1280, height: 720 }); + const found: string[] = []; + const subjects = await rows(page).evaluateAll((els) => els.slice(0, 8).map((e) => e.getAttribute('data-subject'))); + const unread = await page.locator('[data-testid=email-list-item][data-unread="true"]').count(); + + // Archive: the toast stands above the quick reply box of the next message. + await openRow(page, 2); + await archive(page); + await check(page, 'desktop-archived', found); + // Another folder while it's up, and back. + await folder(page, 'sent').click(); + await check(page, 'desktop-sent', found); + await folder(page, 'inbox').click(); + await rows(page).first().waitFor(); + await check(page, 'desktop-inbox-again', found); + await undoLast(page); + await check(page, 'desktop-moved-back', found); + + // The quick reply box grows when focused: toasts stay above it. + await openRow(page, 3); + await archive(page); + await undoLast(page); + await page.locator('[data-quick-reply-box] textarea').first().click(); + await check(page, 'desktop-quick-reply-focused', found); + await page.keyboard.press('Escape'); + + // The reply dock: above its footer while open, above the quick reply box + // once it closes, and after the toolbar's Archive takes the dock away. + await openRow(page, 3); + await archive(page); + await page.locator('button[title^="Reply ("]').first().click(); + const footer = page.locator('[data-composer-footer]').first(); + await footer.waitFor({ timeout: 20000 }); + await check(page, 'desktop-dock-open', found); + await footer.locator('xpath=ancestor::*[.//button[@aria-label="Close" or @title="Close"]][1]') + .locator('button[aria-label="Close"], button[title="Close"]').first().click(); + await footer.waitFor({ state: 'detached', timeout: 20000 }); + await page.locator('[data-quick-reply-box]').first().waitFor({ timeout: 20000 }); + await check(page, 'desktop-dock-closed', found); + await undoLast(page); + await openRow(page, 1); + await page.locator('button[title^="Reply ("]').first().click(); + await footer.waitFor({ timeout: 20000 }); + await archive(page, () => page.locator('button[title^="Archive ("]').first().click()); + await footer.waitFor({ state: 'detached', timeout: 20000 }); + await check(page, 'desktop-dock-archived', found); + await undoLast(page); + + // Calendar while a toast is up, its event panel, a narrow window, and back. + await openRow(page, 2); + await archive(page); + await page.locator('a[href="/calendar"]').first().click(); + await page.waitForURL(/\/calendar/); + await check(page, 'desktop-calendar', found); + await page.getByRole('button', { name: /^create event$/i }).first().click(); + await page.locator('[data-end-sheet]').first().waitFor({ timeout: 20000 }); + await check(page, 'desktop-event-panel', found); + await page.setViewportSize({ width: 760, height: 720 }); + await check(page, 'desktop-event-panel-760', found); + await page.setViewportSize({ width: 1280, height: 720 }); + await page.keyboard.press('Escape'); + await page.locator('nav a[href="/"]').first().click(); + await rows(page).first().waitFor({ timeout: 30000 }); + await undoLast(page); + await check(page, 'desktop-back-to-mail', found); + + // Window widths with a toast up and a message open. + await openRow(page, 2); + await archive(page); + for (const width of [640, 1024, 1920, 1280]) { + await page.setViewportSize({ width, height: 720 }); + await check(page, `desktop-width-${width}`, found); + } + await undoLast(page); + + // Moving the mouse to Undo: the toast holds still all the way. + await openRow(page, 3); + await archive(page); + const target = await undoButton(page).boundingBox(); + expect(target).not.toBeNull(); + const start = { x: 450, y: 300 }; + await page.mouse.move(start.x, start.y); + const places: string[] = []; + for (let i = 1; i <= 12; i++) { + const x = start.x + ((target!.x + target!.width / 2 - start.x) * i) / 12; + const y = start.y + ((target!.y + target!.height / 2 - start.y) * i) / 12; + await page.mouse.move(x, y); + const b = await undoButton(page).boundingBox(); + places.push(b ? `${Math.round(b.x)},${Math.round(b.y)}` : 'gone'); + } + expect(new Set(places).size, `the Undo moved while the mouse went to it: ${places.join(' ')}`).toBe(1); + await undoButton(page).click(); + const back = archived.pop()!; + await page.locator(`[data-testid=email-list-item][data-subject="${back}"]`).first().waitFor({ state: 'attached', timeout: 15000 }); + await page.mouse.move(10, 10); + + // A position worked out for an element that then goes: the focused element + // under the stack sends it to the top corner; once that element is gone the + // stack comes back down in the next frame. + await openRow(page, 3); + await archive(page); + await page.evaluate(() => { + const b = document.createElement('button'); + b.id = 'toast-test-target'; + b.textContent = 'Save'; + // Right under the stack, where a form's Save button would be. + const r = document.querySelector('[data-toast-container]')!.getBoundingClientRect(); + b.style.cssText = `position:fixed;left:${r.left + 20}px;top:${r.bottom - 40}px;width:80px;height:36px;z-index:1`; + document.body.appendChild(b); + b.focus(); + }); + await expect.poll(async () => (await measure(page)).dodging, { timeout: 3000 }).toBe(true); + await page.evaluate(() => document.getElementById('toast-test-target')?.remove()); + await expect.poll(async () => (await measure(page)).dodging, { timeout: 3000, message: 'the stack stayed at the top after the focused element went' }).toBe(false); + await check(page, 'desktop-focus-target-gone', found); + await undoLast(page); + + // Everything archived came back. + await folder(page, 'inbox').click(); + await page.waitForTimeout(1500); + // (New mail may have come in meanwhile.) + const after = await rows(page).evaluateAll((els) => els.map((e) => e.getAttribute('data-subject'))); + for (const subject of subjects) expect(after).toContain(subject); + expect(await page.locator('[data-testid=email-list-item][data-unread="true"]').count()).toBeGreaterThanOrEqual(unread); + + expect(found, found.join('\n')).toEqual([]); + await page.context().close(); +}); + +test('toasts keep their place on a phone: above the tab bar, the action bar and the quick reply box', async ({ browser }) => { + const page = await signIn(browser, { width: 390, height: 844, phone: true }); + const found: string[] = []; + const subject = await readRows(page).nth(3).getAttribute('data-subject'); + await readRows(page).nth(3).tap(); + await page.locator('[data-quick-reply-box]').first().waitFor({ timeout: 30000 }); + await page.getByRole('button', { name: /^archive$/i }).last().tap(); + await undoButton(page).waitFor({ timeout: 15000 }); + await check(page, 'phone-archived-list', found); + await readRows(page).nth(4).tap(); + await page.locator('[data-quick-reply-box]').first().waitFor({ timeout: 30000 }); + await check(page, 'phone-next-message', found); + await undoButton(page).tap(); + await page.locator('[data-toast-container] .toast-item', { hasText: 'Moved back' }).first().waitFor({ state: 'attached', timeout: 15000 }); + await check(page, 'phone-moved-back', found); + await page.getByRole('button', { name: /^back/i }).first().tap({ timeout: 5000 }).catch(() => {}); + await page.locator(`[data-testid=email-list-item][data-subject="${subject}"]`).first().waitFor({ state: 'attached', timeout: 15000 }); + expect(found, found.join('\n')).toEqual([]); + await page.context().close(); +}); diff --git a/lib/toast-placement.ts b/lib/toast-placement.ts new file mode 100644 index 000000000..24914a330 --- /dev/null +++ b/lib/toast-placement.ts @@ -0,0 +1,121 @@ +/** + * Where the toast stack goes, for the whole app. One rule: at the bottom end + * corner of the window (bottom left in RTL; across the bottom on a phone), + * above whatever is docked at the bottom on screen now, and beside a sheet + * docked at the window's end when there is room for it. + * + * The things to keep clear mark themselves: + * data-toast-avoid="bottom" docked at the bottom: the quick reply box, a + * composer's or form's footer, the phone's bars + * (hooks/use-bottom-controls marks those) + * data-end-sheet a panel docked at the window's end: the + * calendar's event and task panels + * + * Everything is measured where it is on screen at the moment, never kept from + * earlier: an offset worked out from a box that then moved, closed or went + * with a page change left toasts in the middle of the content. + */ + +export const TOAST_AVOID_BOTTOM = '[data-toast-avoid="bottom"]'; +export const TOAST_AVOID_END = '[data-end-sheet]'; + +/** Distance from the window's edges (px). */ +export const TOAST_EDGE = 20; +/** The same on a phone, where the stack spans the screen. */ +export const TOAST_PHONE_EDGE = 12; +/** Room left above something docked at the bottom (px). */ +export const TOAST_GAP = 12; +/** The stack's width when it can't be measured (w-[360px] in toast.tsx). */ +const STACK_WIDTH = 360; + +export interface ToastPlacement { + /** From the window's bottom edge to the stack's bottom (px). */ + bottom: number; + /** From the window's end edge (px); null on a phone, where the stack spans the screen. */ + end: number | null; +} + +function shownRect(el: Element): DOMRect | null { + const r = el.getBoundingClientRect(); + if (r.width <= 0 || r.height <= 0) return null; + if (getComputedStyle(el).visibility === 'hidden') return null; + return r; +} + +function viewport(): { width: number; height: number } { + const root = document.documentElement; + return { + width: root.clientWidth || window.innerWidth, + height: window.innerHeight || root.clientHeight, + }; +} + +/** The stack's place, from what is on screen now. */ +export function computeToastPlacement(stack: HTMLElement, opts: { phone: boolean; rtl: boolean }): ToastPlacement { + const { width: vw, height: vh } = viewport(); + const width = Math.min(stack.offsetWidth || STACK_WIDTH, vw); + + // Beside a sheet at the window's end, when the stack fits next to it. + // Without room it stays in the corner and clears the sheet's footer below. + let end: number | null = opts.phone ? null : TOAST_EDGE; + if (end !== null) { + for (const sheet of document.querySelectorAll(TOAST_AVOID_END)) { + if (stack.contains(sheet)) continue; + const r = shownRect(sheet); + if (!r) continue; + const atEnd = opts.rtl ? r.left <= 1 : r.right >= vw - 1; + if (!atEnd) continue; + const beside = (opts.rtl ? r.right : vw - r.left) + TOAST_EDGE; + if (beside + width + TOAST_EDGE <= vw) end = Math.max(end, beside); + } + } + + // The columns the stack covers. + const left = end === null ? TOAST_PHONE_EDGE : opts.rtl ? end : vw - end - width; + const right = end === null ? vw - TOAST_PHONE_EDGE : left + width; + + // Above what is docked at the bottom under those columns. Only what is on + // screen and sits in the lower half counts: a box scrolled out of view, or + // one standing in the middle, would put the toasts over the content. + let bottom = opts.phone ? TOAST_PHONE_EDGE : TOAST_EDGE; + for (const el of document.querySelectorAll(TOAST_AVOID_BOTTOM)) { + if (stack.contains(el)) continue; + const r = shownRect(el); + if (!r) continue; + if (r.right <= left || r.left >= right) continue; + if (r.top >= vh || r.bottom <= 0) continue; + if (r.top < vh / 2) continue; + bottom = Math.max(bottom, vh - r.top + TOAST_GAP); + } + return { bottom: Math.round(bottom), end }; +} + +/** + * True when the stack, at `place`, would hide at least half of the element + * that has the focus: a button or a one-line field. A large element it only + * overlaps at a corner stays usable, and a text area or editor never counts: + * moving the stack to the top for one put the toasts over the composer's close + * button and the reader's toolbar. + */ +export function stackWouldCoverFocus(stack: HTMLElement, place: ToastPlacement, rtl = false): boolean { + const focused = document.activeElement as HTMLElement | null; + if (!focused || focused === document.body || !focused.isConnected || stack.contains(focused)) return false; + // Text being written: the caret moves, and the stack over the lower corner + // of the reply dock's editor sent it to the top, over the toolbar. + if (focused.tagName === 'TEXTAREA' || focused.isContentEditable || focused.closest('[contenteditable]:not([contenteditable="false"])')) return false; + const height = stack.offsetHeight || stack.getBoundingClientRect().height; + if (height === 0) return false; + const { width: vw, height: vh } = viewport(); + const width = Math.min(stack.offsetWidth || STACK_WIDTH, vw); + const bottom = vh - place.bottom; + const top = bottom - height; + const end = place.end ?? TOAST_PHONE_EDGE; + const left = rtl ? end : vw - end - width; + const right = left + width; + const f = focused.getBoundingClientRect(); + const overlapWidth = Math.min(f.right, right) - Math.max(f.left, left); + const overlapHeight = Math.min(f.bottom, bottom) - Math.max(f.top, top); + if (overlapWidth <= 0 || overlapHeight <= 0) return false; + const area = Math.max(f.width, 0) * Math.max(f.height, 0); + return area === 0 || overlapWidth * overlapHeight >= area / 2; +}