Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 24 additions & 13 deletions scripts/screenshots/checks.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 20 additions & 0 deletions server/frontend/src/__tests__/help.test.tsx
Original file line number Diff line number Diff line change
@@ -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(<Help>One short sentence.</Help>)
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)
})
})
55 changes: 50 additions & 5 deletions server/frontend/src/components/ui.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<HTMLButtonElement>(null)
const tip = useRef<HTMLSpanElement>(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 (
<span className="help">
<button type="button" className="help-btn" aria-label={label} aria-describedby={id}>
<span className="help" onMouseEnter={() => setOpen(true)} onMouseLeave={() => setOpen(false)}>
<button
ref={button}
type="button"
className="help-btn"
aria-label={label}
aria-describedby={id}
aria-expanded={open}
onFocus={() => setOpen(true)}
onBlur={() => setOpen(false)}
onClick={() => setOpen((o) => !o)}
>
<QuestionIcon size={14} weight="bold" aria-hidden />
</button>
<span role="tooltip" id={id} className="help-tip">
<span
ref={tip}
role="tooltip"
id={id}
className="help-tip"
data-open={open || undefined}
style={place ? { left: place.left, top: place.top } : undefined}
>
{children}
</span>
</span>
Expand Down
21 changes: 9 additions & 12 deletions server/frontend/src/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
Loading