// compact.browser.mjs: open the compact, stacked bar at each width, with // a finger where a phone or a tablet has one, and measure it. Driven by // compact_browser_test.go (go test -tags browser ./chrome), which serves // the page and makes every assertion; this file only drives the browsers // and reports what it measured. With job.shots set it saves screenshots // there: each case at rest, and with the ••• menu (or File) open. // // ESTILO_PLAYWRIGHT= \ // node compact.browser.mjs import { createRequire } from 'node:module' import { mkdirSync } from 'node:fs' const require = createRequire(import.meta.url) const pw = require(process.env.ESTILO_PLAYWRIGHT || 'playwright') const job = JSON.parse(process.argv[2]) // kind, width, height, touch const sizes = [ ['phone', 360, 780, true], ['phone', 390, 844, true], ['phone', 430, 932, true], ['landscape', 844, 390, true], ['tablet', 768, 1024, true], ['tablet', 1024, 768, true], ['desktop', 1280, 800, false], ] const cases = [] for (const [kind, w, h, touch] of sizes) { for (const lang of ['en', 'ar', 'ru']) { for (const scheme of ['light', 'dark']) { if (lang !== 'en' && scheme === 'dark' && !(lang === 'ar' && w === 390)) continue if (lang !== 'en' && kind === 'landscape') continue cases.push({ name: `${kind} ${w} ${lang} ${scheme}`, kind, w, h, touch, lang, scheme }) } } } function measure() { const q = (s) => document.querySelector(s) const shown = (el) => !!el && el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden' const box = (el) => { const r = el.getBoundingClientRect(); return [r.x, r.y, r.width, r.height] } const bar = q('[estilo-bar]') const probe = document.createElement('div') probe.style.blockSize = 'var(--estilo-bar-block-size)' document.body.append(probe) const v = probe.getBoundingClientRect().height probe.remove() const input = q('[estilo-bar-title-input]') const out = { header: bar.getBoundingClientRect().height, var: v, coarse: matchMedia('(pointer: coarse)').matches, overflow: document.documentElement.scrollWidth > window.innerWidth, shown: { menubar: shown(q('[estilo-bar-menubar]')), actions: shown(q('[estilo-bar-actions]')), switcher: shown(q('[estilo-switcher]')), account: shown(q('[estilo-account]')), more: shown(q('[estilo-bar-more]')), icon: shown(q('[estilo-bar-brand] img')), back: shown(q('[estilo-bar-back]')), name: shown(q('[estilo-bar-name]')), title: shown(input), lead: shown(q('[estilo-bar-lead]')), trail: shown(q('[estilo-bar-trail]')), }, boxes: { back: box(q('[estilo-bar-brand]')), title: box(q('[estilo-bar-title]')), end: box(q('[estilo-bar-end]')), menubar: box(q('[estilo-bar-menubar]')) }, ellipsis: getComputedStyle(input).textOverflow, } Array.prototype.filter.call(document.querySelectorAll('[estilo-bar-tool]'), shown).forEach((el, i) => { out.boxes['tool-' + i] = box(el) }) return out } function panel(sel) { const d = document.querySelector(sel) const p = d.querySelector('[estilo-menu]') const r = p.getBoundingClientRect() const shown = (el) => el.getClientRects().length > 0 return { panel: [r.x, r.y, r.width, r.height], rows: Array.prototype.filter.call(p.querySelectorAll('[estilo-menu-item]'), shown).map((el) => el.getBoundingClientRect().height), keys: Array.prototype.filter.call(p.querySelectorAll('[estilo-menu-key]'), shown).length, } } function large() { const q = (s) => document.querySelector(s) const box = (el) => { const r = el.getBoundingClientRect(); return [r.x, r.y, r.width, r.height] } const t = q('[estilo-bar-compact-title]') const cs = getComputedStyle(t) return { title: cs.visibility === 'visible' && parseFloat(cs.opacity) > 0.99, name: q('[estilo-bar-name]').getBoundingClientRect().width, label: q('[estilo-bar-brand]').innerText, // drops undisplayed text, keeps a visually hidden name border: getComputedStyle(q('[estilo-bar]')).borderBlockEndColor, box: box(t), back: box(q('[estilo-bar-brand]')), end: box(q('[estilo-bar-end]')), } } const results = [] for (const engine of job.engines) { const result = { engine, version: '', error: '', cases: [] } results.push(result) let browser try { browser = await pw[engine].launch() result.version = browser.version() if (job.shots) mkdirSync(job.shots, { recursive: true }) for (const c of cases) { // One case that cannot be driven is reported and the rest go on, // so a broken layout shows as the assertions it breaks. try { const opts = { viewport: { width: c.w, height: c.h }, deviceScaleFactor: 2, colorScheme: c.scheme } if (c.touch) Object.assign(opts, { hasTouch: true, isMobile: engine !== 'firefox' }) const ctx = await browser.newContext(opts) const page = await ctx.newPage() page.setDefaultTimeout(5000) await page.goto(`${job.origin}/?lang=${c.lang}&scheme=${c.scheme}`, { waitUntil: 'load' }) await page.evaluate(() => document.fonts.ready) await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) const m = await page.evaluate(measure) const file = (s) => `${job.shots}/${engine}-${c.name.replaceAll(' ', '-')}${s}.png` if (job.shots) await page.screenshot({ path: file('') }) const sel = (c.kind === 'phone' || c.kind === 'landscape') ? '[data-demo="more"]' : '[estilo-menubar-menu]:has([data-demo="file"])' await page.locator(sel + ' > summary').click() await page.evaluate(() => new Promise((r) => requestAnimationFrame(r))) const p = await page.evaluate(panel, sel) if (job.shots) await page.screenshot({ path: file('-open') }) const entry = { name: c.name, width: c.w, touch: c.touch, ...m, ...p } if (c.kind === 'phone' || c.kind === 'landscape') { await page.goto(`${job.origin}/?lang=${c.lang}&scheme=${c.scheme}&inline=1`, { waitUntil: 'load' }) await page.evaluate(() => document.fonts.ready) entry.rest = await page.evaluate(large) if (job.shots) await page.screenshot({ path: file('-large') }) await page.evaluate(() => document.querySelector('[estilo-bar]').setAttribute('estilo-bar-folded', '')) await page.waitForTimeout(300) entry.folded = await page.evaluate(large) if (job.shots) await page.screenshot({ path: file('-folded') }) } if (c.kind === 'phone' && c.lang === 'en' && c.scheme === 'light') { await page.goto(`${job.origin}/?lang=en&scheme=light&public=1`, { waitUntil: 'load' }) entry.public = await page.evaluate(() => { const probe = document.createElement('span') probe.style.color = 'var(--rst-accent)' document.body.append(probe) const accent = getComputedStyle(probe).color probe.remove() return { brand: getComputedStyle(document.querySelector('[estilo-bar-brand]')).color, accent } }) if (job.shots) await page.screenshot({ path: file('-public') }) } else { entry.public = { brand: 'n/a', accent: '' } } result.cases.push(entry) await ctx.close() } catch (e) { result.cases.push({ name: c.name, width: c.w, touch: c.touch, error: String(e && e.message || e).split('\n')[0] }) } } // A compact bar loaded narrow and widened into the 704-800px band is // the 56px header there; and in forced colours the band's controls // keep the border the forced-colors rule gives them, as from 800px. result.extra = {} try { const ctx = await browser.newContext({ viewport: { width: 650, height: 900 }, deviceScaleFactor: 2, hasTouch: true, isMobile: engine !== 'firefox' }) const page = await ctx.newPage() await page.goto(`${job.origin}/?lang=en&scheme=light`, { waitUntil: 'load' }) await page.evaluate(() => document.fonts.ready) const at650 = await page.evaluate(measure) await page.setViewportSize({ width: 768, height: 1024 }) await page.waitForTimeout(300) const at768 = await page.evaluate(measure) result.extra.resized = { from: at650.header, header: at768.header, var: at768.var } await ctx.close() } catch (e) { result.extra.resizedError = String(e && e.message || e).split('\n')[0] } result.extra.forced = {} for (const w of [768, 1280]) { try { const ctx = await browser.newContext({ viewport: { width: w, height: 800 }, forcedColors: 'active' }) const page = await ctx.newPage() await page.goto(`${job.origin}/?lang=en&scheme=light`, { waitUntil: 'load' }) result.extra.forced[w] = await page.evaluate(() => { const el = document.querySelector('[estilo-bar-control~="primary"] > a') return { border: parseFloat(getComputedStyle(el).borderTopWidth), forced: matchMedia('(forced-colors: active)').matches } }) await ctx.close() } catch (e) { result.extra.forced[w] = { error: String(e && e.message || e).split('\n')[0] } } } // Mapa and Fichas, as stand-ins: stacked bars with nav, not compact. result.standIns = [] for (const kind of ['mapa', 'fichas']) { for (const [w, h, touch] of [[390, 844, true], [768, 1024, true], [844, 390, true], [1280, 800, false]]) { const name = `${kind} ${w}x${h}${touch ? ' touch' : ''}` try { const opts = { viewport: { width: w, height: h }, deviceScaleFactor: 2, colorScheme: 'light' } if (touch) Object.assign(opts, { hasTouch: true, isMobile: engine !== 'firefox' }) const ctx = await browser.newContext(opts) const page = await ctx.newPage() page.setDefaultTimeout(5000) await page.goto(`${job.origin}/?kind=${kind}&lang=en&scheme=light`, { waitUntil: 'load' }) await page.evaluate(() => document.fonts.ready) await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r)))) const shot = (s) => `${job.shots}/${engine}-${kind}-${w}x${h}${s}.png` if (job.shots) await page.screenshot({ path: shot('') }) const m = await page.evaluate(() => { const shown = (el) => el.getClientRects().length > 0 && getComputedStyle(el).visibility !== 'hidden' const label = (el) => (el.getAttribute('aria-label') || el.textContent).trim() const bar = document.querySelector('[estilo-bar]') const probe = document.createElement('div') probe.style.blockSize = 'var(--estilo-bar-block-size)' document.body.append(probe) const v = probe.getBoundingClientRect().height probe.remove() const nav = [...bar.querySelectorAll('[estilo-bar-nav] a')] const acts = [...bar.querySelectorAll('[estilo-bar-actions] :is(a, button)')].filter((el) => !el.matches('[estilo-bar-wide], [estilo-bar-control~="wide"] *')) return { header: bar.getBoundingClientRect().height, var: v, overflow: document.documentElement.scrollWidth > window.innerWidth, wanted: [...nav, ...acts].map(label), reached: [...nav, ...acts].filter(shown).map(label), more: !!bar.querySelector('[estilo-bar-more]') && shown(bar.querySelector('[estilo-bar-more]')), } }) if (m.more) { await page.locator('[estilo-bar-more] > summary').click() await page.evaluate(() => new Promise((r) => requestAnimationFrame(r))) const rows = await page.evaluate(() => [...document.querySelectorAll('[estilo-bar-more] [estilo-menu-item]')].filter((el) => el.getClientRects().length > 0).map((el) => el.textContent.trim())) m.reached.push(...rows) if (job.shots) await page.screenshot({ path: shot('-open') }) } result.standIns.push({ name, width: w, ...m }) await ctx.close() } catch (e) { result.standIns.push({ name, width: w, error: String(e && e.message || e).split('\n')[0] }) } } } } catch (e) { result.error = String(e && e.stack || e) } finally { if (browser) await browser.close() } } process.stdout.write(JSON.stringify(results))