From ee041168dad1436e73d529e31b68718db9c5b954 Mon Sep 17 00:00:00 2001 From: Nathan Broadbent Date: Wed, 2 Dec 2026 17:08:51 +1300 Subject: [PATCH] Pages wider than a phone: the (?) help tooltip was laid out while hidden and centred on its button, so near a phone's right edge it widened the page (account 427px, build changes 411px; 1487px at 1440) and was cut off when open. It's now display: none while closed, and placed in the screen (fixed, clamped, below the button when there's no room above) when open on hover, focus or a tap. checks.mjs missed it because html and body clip sideways, so scrollWidth never grew: it now fails on anything laid out past the right edge outside a box that scrolls on its own, and names it --- scripts/screenshots/checks.mjs | 37 +++++++++----- server/frontend/src/__tests__/help.test.tsx | 20 ++++++++ server/frontend/src/components/ui.tsx | 55 +++++++++++++++++++-- server/frontend/src/styles.css | 21 ++++---- 4 files changed, 103 insertions(+), 30 deletions(-) create mode 100644 server/frontend/src/__tests__/help.test.tsx diff --git a/scripts/screenshots/checks.mjs b/scripts/screenshots/checks.mjs index 1c41a8b3..308bda4a 100644 --- a/scripts/screenshots/checks.mjs +++ b/scripts/screenshots/checks.mjs @@ -8,8 +8,8 @@ // collapses them. // - The /screenshots/ lightbox shows the screen that was clicked while its image loads, never the one opened before // (lightbox.mjs). -// - No captured page scrolls sideways on a 390 px phone: a long line (a code block, a URL) has to scroll or wrap inside -// its own box, not widen the page. +// - No captured page is wider than a 390 px phone: nothing laid out past the right edge (the page clips sideways, so +// that would be cut off), except inside a box that scrolls on its own (a code block). Waits like capture.mjs does. import { chromium } from 'playwright' import { EMAIL, pages } from './pages.mjs' import { checkLightbox } from './lightbox.mjs' @@ -88,26 +88,37 @@ try { const path = typeof target === 'function' ? await target(api) : target const page = await ctx.newPage() await page.goto(path, { waitUntil: 'networkidle' }) - const { scrollWidth, innerWidth, culprit } = await page.evaluate(() => { + await page.evaluate(() => document.fonts.ready) + await page.waitForLoadState('networkidle') + // The page can't scroll sideways (html and body clip), so measure what's laid out past the right edge instead: + // anything there is cut off. A box that scrolls on its own (a code block) may be wider; what's inside it is fine. + const { width, innerWidth, culprits } = await page.evaluate(() => { const vw = window.innerWidth - const over = [...document.querySelectorAll('body *')].filter((e) => { - const right = (el) => el.getBoundingClientRect().right - return right(e) > vw + 1 && ![...e.children].some((c) => right(c) > vw + 1) - }) - const e = over[0] + const scrolls = (el) => { + for (let a = el.parentElement; a && a !== document.body; a = a.parentElement) { + const o = getComputedStyle(a).overflowX + if (o === 'auto' || o === 'scroll' || o === 'hidden' || o === 'clip') return true + } + return false + } + const right = (el) => el.getBoundingClientRect().right + const over = [...document.querySelectorAll('body *')].filter( + (e) => right(e) > vw + 1 && !scrolls(e) && ![...e.children].some((c) => right(c) > vw + 1 && !scrolls(c)), + ) + const name = (e) => `${e.tagName.toLowerCase()}${e.classList.length ? '.' + [...e.classList].join('.') : ''}` return { - scrollWidth: document.documentElement.scrollWidth, + width: Math.max(vw, ...over.map((e) => Math.ceil(right(e)))), innerWidth: vw, - culprit: e ? `${e.tagName.toLowerCase()}.${[...e.classList].join('.')}` : null, + culprits: [...new Set(over.slice(0, 4).map((e) => `${name(e)} (to ${Math.ceil(right(e))}px)`))], } }) - if (scrollWidth > innerWidth) wide.push(`${name} (${path}): ${scrollWidth}px wide in ${innerWidth}px, ${culprit}`) + if (width > innerWidth) wide.push(`${name} (${path}): ${width}px wide in ${innerWidth}px: ${culprits.join(', ')}`) checked++ await page.close() } for (const ctx of Object.values(contexts)) await ctx.close() - if (wide.length) throw new Error(`pages that scroll sideways at 390px:\n ${wide.join('\n ')}`) - console.log(`phone width: none of ${checked} pages scrolls sideways at 390px`) + if (wide.length) throw new Error(`pages wider than a 390px phone (cut off at the edge):\n ${wide.join('\n ')}`) + console.log(`phone width: none of ${checked} pages is wider than 390px`) } } finally { await browser.close() diff --git a/server/frontend/src/__tests__/help.test.tsx b/server/frontend/src/__tests__/help.test.tsx new file mode 100644 index 00000000..93516978 --- /dev/null +++ b/server/frontend/src/__tests__/help.test.tsx @@ -0,0 +1,20 @@ +import { describe, expect, it } from 'vitest' +import { fireEvent, render } from '@testing-library/react' +import { Help } from '../components/ui' + +describe('Help', () => { + it('takes no space while closed and opens on focus, a tap, and closes on Escape', () => { + const { container, getByRole } = render(One short sentence.) + const tip = container.querySelector('.help-tip')! + expect(tip.hasAttribute('data-open')).toBe(false) + const button = getByRole('button', { name: 'What is this?' }) + fireEvent.focus(button) + expect(tip.hasAttribute('data-open')).toBe(true) + fireEvent.keyDown(window, { key: 'Escape' }) + expect(tip.hasAttribute('data-open')).toBe(false) + fireEvent.click(button) + expect(tip.hasAttribute('data-open')).toBe(true) + // Screen readers get the sentence either way. + expect(button.getAttribute('aria-describedby')).toBe(tip.id) + }) +}) diff --git a/server/frontend/src/components/ui.tsx b/server/frontend/src/components/ui.tsx index a94024df..78485c4f 100644 --- a/server/frontend/src/components/ui.tsx +++ b/server/frontend/src/components/ui.tsx @@ -1,4 +1,4 @@ -import { useEffect, useId, useRef, useState, type ButtonHTMLAttributes, type ReactNode } from 'react' +import { useEffect, useId, useLayoutEffect, useRef, useState, type ButtonHTMLAttributes, type ReactNode } from 'react' import { CheckIcon, CopyIcon, QuestionIcon, WarningCircleIcon } from '@phosphor-icons/react' import type { BuildSummary, ReviewState, SnapshotReview, SnapshotState, Stats } from '../api/types' import { errorMessage } from '../api/client' @@ -222,16 +222,61 @@ export function Avatar({ name, url, size = 26 }: { name: string; url: string | n /** * A small (?) with one short sentence in a tooltip, for the rare thing that needs explaining. It's a button, so it - * takes keyboard focus, and the tooltip describes it for screen readers. + * takes keyboard focus, and the tooltip describes it for screen readers. The tooltip opens on hover, focus or a tap, + * and sits above the button (below when there's no room), always inside the screen: near the edge of a phone it + * shifts in rather than being cut off. Closed, it takes no space. */ export function Help({ children, label = 'What is this?' }: { children: ReactNode; label?: string }) { const id = useId() + const button = useRef(null) + const tip = useRef(null) + const [open, setOpen] = useState(false) + const [place, setPlace] = useState<{ left: number; top: number } | null>(null) + useLayoutEffect(() => { + if (!open || !button.current || !tip.current) return setPlace(null) + const b = button.current.getBoundingClientRect() + const t = tip.current.getBoundingClientRect() + const margin = 8 + const left = Math.min(Math.max(b.left + b.width / 2 - t.width / 2, margin), window.innerWidth - t.width - margin) + const above = b.top - t.height - 6 + setPlace({ left: Math.max(left, margin), top: above >= margin ? above : b.bottom + 6 }) + }, [open]) + useEffect(() => { + if (!open) return + const close = () => setOpen(false) + const key = (e: KeyboardEvent) => e.key === 'Escape' && close() + window.addEventListener('scroll', close, { passive: true, capture: true }) + window.addEventListener('resize', close) + window.addEventListener('keydown', key) + return () => { + window.removeEventListener('scroll', close, { capture: true }) + window.removeEventListener('resize', close) + window.removeEventListener('keydown', key) + } + }, [open]) return ( - - - + {children} diff --git a/server/frontend/src/styles.css b/server/frontend/src/styles.css index 1f338d7c..02ee7877 100644 --- a/server/frontend/src/styles.css +++ b/server/frontend/src/styles.css @@ -600,12 +600,14 @@ select.input { } .help-tip { - position: absolute; - left: 50%; - bottom: calc(100% + 6px); - z-index: 20; + /* Placed by Help from its button's position, inside the screen. */ + position: fixed; + left: 0; + top: 0; + z-index: 40; + display: none; width: max-content; - max-width: min(280px, 80vw); + max-width: min(280px, calc(100vw - 16px)); padding: var(--space-2) var(--space-3); border: 1px solid var(--color-rule); border-radius: var(--radius); @@ -617,16 +619,11 @@ select.input { line-height: 1.45; text-align: left; white-space: normal; - transform: translateX(-50%); - visibility: hidden; - opacity: 0; pointer-events: none; } -.help:hover .help-tip, -.help-btn:focus-visible + .help-tip { - visibility: visible; - opacity: 1; +.help-tip[data-open] { + display: block; } .field-error {