diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3caf0e8..4f96443 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -40,5 +40,8 @@ jobs: - name: Build the demo run: bun run build + - name: Check glossary cards in a browser + run: CHROMIUM_PATH=/usr/bin/google-chrome bun run check:glossary-browser + - name: Check the packed package run: bun run check:package diff --git a/README.md b/README.md index 750f347..4125b6d 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,10 @@ python3 -m http.server 4340 --bind 127.0.0.1 --directory demo/dist Open `http://localhost:4340/starlight/` on the same machine. When sharing a remote preview, use the host's shared URL with the same path. +To check glossary cards in tables, build the demo and run +`CHROMIUM_PATH=/path/to/chromium bun run check:glossary-browser` with a local +Chromium executable. + ## What the demo shows - One `Demo0ver` site explaining the template through working examples. diff --git a/bun.lock b/bun.lock index d0cd4e8..d604ce6 100644 --- a/bun.lock +++ b/bun.lock @@ -4,6 +4,9 @@ "workspaces": { "": { "name": "@tech0ver/starlight-workspace", + "devDependencies": { + "playwright-core": "^1.64.1", + }, }, "demo": { "name": "@tech0ver/starlight-demo", @@ -646,6 +649,8 @@ "picomatch": ["picomatch@4.0.7", "", {}, "sha512-qcJu88Q2IWqJsDD529JKMdwGm/dvInW4HvQnRwiH9JtihJvzGOscDtHE3x1pBKeUOTysQ8kVmLnJ2kJu7yhcGA=="], + "playwright-core": ["playwright-core@1.64.1", "", { "bin": { "playwright-core": "cli.js" } }, "sha512-BC4s8y6SEVA6Q1rSxZTto8hI1KUI3xXlHGZgvGqP4IqPdI/a6FX9H29IDNz7vKo9l21b6LfYosScPL6M3xxyDQ=="], + "postcss": ["postcss@8.5.31", "", { "dependencies": { "nanoid": "^3.3.20", "picocolors": "^1.1.1", "source-map-js": "^1.2.2" } }, "sha512-YUbWYCgErwnMAVkjUnYa+KlMBzGpHwiIt/DE7f0G33AuX2LH9UcTk6KgWxd3zRU+pN3u/C+Xl3EnMLM4Hea7Sg=="], "postcss-nested": ["postcss-nested@6.2.0", "", { "dependencies": { "postcss-selector-parser": "^6.1.1" }, "peerDependencies": { "postcss": "^8.2.14" } }, "sha512-HQbt28KulC5AJzG+cZtj9kvKB93CFCdLvog1WFLf1D+xmMvPGlBstkpTEZfK5+AN9hfJocyBFCNiqyS48bpgzQ=="], diff --git a/demo/astro.config.mjs b/demo/astro.config.mjs index 61ab420..49cd19c 100644 --- a/demo/astro.config.mjs +++ b/demo/astro.config.mjs @@ -62,7 +62,7 @@ export default defineTech0verConfig({ description: 'A compact demonstration of reusable documentation UI.', sidebar: [ { label: 'Learn the template', items: ['getting-started', 'configuration'] }, - { label: 'Feature gallery', items: ['reading', 'board-diagrams'] }, + { label: 'Feature gallery', items: ['reading', 'board-diagrams', 'table-terms'] }, 'glossary', ], pagination: true, diff --git a/demo/src/content/docs/table-terms.md b/demo/src/content/docs/table-terms.md new file mode 100644 index 0000000..d43fffb --- /dev/null +++ b/demo/src/content/docs/table-terms.md @@ -0,0 +1,12 @@ +--- +title: Glossary terms in tables +description: Try glossary cards in a compact table. +--- + +| Feature | Term | +| --- | --- | +| Site | Consumer | +| Guidance | Callout | +| URL | Base path | + +Hover, focus, or tap a term to read its definition. diff --git a/package.json b/package.json index d7e2fba..d70f74d 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,11 @@ "build": "bun run --cwd demo build", "test": "bun test", "check:package": "bun scripts/check-package.mjs", + "check:glossary-browser": "node scripts/check-glossary-browser.mjs", "check:release": "node scripts/check-release.mjs", "check:release-please": "node scripts/check-release-please.mjs" + }, + "devDependencies": { + "playwright-core": "^1.64.1" } } diff --git a/packages/starlight/src/lib/glossary-tooltips.mjs b/packages/starlight/src/lib/glossary-tooltips.mjs index fe6068a..b90e62a 100644 --- a/packages/starlight/src/lib/glossary-tooltips.mjs +++ b/packages/starlight/src/lib/glossary-tooltips.mjs @@ -17,6 +17,8 @@ export function initializeGlossaryTooltips() { current.removeAttribute('data-above'); if (!matchMedia('(min-width: 72rem)').matches) return; const trigger = label(current).getBoundingClientRect(); + tip.style.setProperty('--term-tip-left', `${trigger.left}px`); + tip.style.setProperty('--term-tip-top', `${trigger.bottom}px`); const above = trigger.top - Math.max(8, document.querySelector('header')?.getBoundingClientRect().bottom ?? 8) - 8; const below = innerHeight - trigger.bottom - 8; const placeAbove = tip.getBoundingClientRect().height > below && above > below; @@ -26,6 +28,8 @@ export function initializeGlossaryTooltips() { const width = document.documentElement.clientWidth; const offset = Math.max(8 - bounds.left, Math.min(0, width - 8 - bounds.right)); tip.style.setProperty('--term-tip-offset', `${offset}px`); + tip.style.setProperty('--term-tip-left', `${trigger.left + offset}px`); + tip.style.setProperty('--term-tip-top', `${placeAbove ? trigger.top - tip.getBoundingClientRect().height : trigger.bottom}px`); }; const close = (restoreFocus = false) => { if (!current) return; @@ -33,6 +37,7 @@ export function initializeGlossaryTooltips() { // Restore focus before closing so focusin cannot reopen the dismissed card. if (restoreFocus && term.querySelector('.term-tip').contains(document.activeElement)) label(term).focus({ preventScroll: true }); term.removeAttribute('data-open'); + term.querySelector('.term-tip').hidePopover(); label(term).setAttribute('aria-expanded', 'false'); current = undefined; }; @@ -40,11 +45,14 @@ export function initializeGlossaryTooltips() { if (current !== term) close(); current = term; term.setAttribute('data-open', ''); + const tip = term.querySelector('.term-tip'); + if (!tip.matches(':popover-open')) tip.showPopover(); label(term).setAttribute('aria-expanded', 'true'); position(); }; for (const term of terms) { term.setAttribute('data-enhanced', ''); + term.querySelector('.term-tip').setAttribute('popover', 'manual'); term.querySelector('.term-tip-close').hidden = false; term.addEventListener('pointerenter', (event) => { if (event.pointerType === 'mouse') open(term); @@ -78,10 +86,14 @@ export function initializeGlossaryTooltips() { window.addEventListener('scroll', () => { cancelAnimationFrame(frame); frame = requestAnimationFrame(position); - }, { signal, passive: true }); + }, { signal, passive: true, capture: true }); cleanup = () => { close(); controller.abort(); + for (const term of terms) { + term.removeAttribute('data-enhanced'); + term.querySelector('.term-tip').removeAttribute('popover'); + } cancelAnimationFrame(frame); }; } diff --git a/packages/starlight/src/styles/glossary.css b/packages/starlight/src/styles/glossary.css index 3f7768f..0608d70 100644 --- a/packages/starlight/src/styles/glossary.css +++ b/packages/starlight/src/styles/glossary.css @@ -20,6 +20,7 @@ } .sl-markdown-content .term-tip { + display: none; position: absolute; top: 100%; left: 0; @@ -28,9 +29,6 @@ max-width: min(24rem, 85vw); transform: translateX(var(--term-tip-offset, 0px)); padding-top: 0.4rem; /* keeps the hover area continuous between the term and the card */ - visibility: hidden; - opacity: 0; - transition: opacity 0.12s ease, visibility 0.12s; font-size: var(--sl-text-sm); font-weight: normal; line-height: 1.45; @@ -93,8 +91,16 @@ .sl-markdown-content .term:not([data-enhanced]):hover .term-tip, .sl-markdown-content .term:not([data-enhanced]):focus-within .term-tip, .sl-markdown-content .term[data-open] .term-tip { - visibility: visible; - opacity: 1; + display: block; +} + +.sl-markdown-content .term-tip:popover-open { + position: fixed; + margin: 0; + border: 0; + background: transparent; + color: inherit; + inset: auto; } @media (width >= 72rem) { @@ -109,6 +115,12 @@ padding-top: 0; padding-bottom: 0.4rem; } + .sl-markdown-content .term .term-tip:popover-open { + left: var(--term-tip-left); + top: var(--term-tip-top); + bottom: auto; + transform: none; + } } @media (width < 72rem) { @@ -123,6 +135,11 @@ padding-top: 0; transform: none; } + .sl-markdown-content .term-tip:popover-open { + left: 1rem; + right: 1rem; + bottom: 1rem; + } } /* Each term is a heading so Starlight can track it in the table of contents. */ diff --git a/scripts/check-glossary-browser.mjs b/scripts/check-glossary-browser.mjs new file mode 100644 index 0000000..456ee58 --- /dev/null +++ b/scripts/check-glossary-browser.mjs @@ -0,0 +1,69 @@ +import assert from 'node:assert/strict'; +import { createServer } from 'node:http'; +import { readFile } from 'node:fs/promises'; +import { resolve, sep } from 'node:path'; +import { chromium } from 'playwright-core'; + +const root = resolve('demo/dist'); +const server = createServer(async (request, response) => { + const pathname = new URL(request.url, 'http://localhost').pathname; + const file = resolve(root, `.${pathname}${pathname.endsWith('/') ? 'index.html' : ''}`); + if (!file.startsWith(root + sep)) return response.writeHead(403).end(); + try { + const content = await readFile(file); + const type = file.endsWith('.html') ? 'text/html' : file.endsWith('.css') ? 'text/css' : file.endsWith('.js') ? 'text/javascript' : 'application/octet-stream'; + response.writeHead(200, { 'content-type': type }).end(content); + } catch { + response.writeHead(404).end(); + } +}); +await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve)); +let browser; +try { + browser = await chromium.launch({ executablePath: process.env.CHROMIUM_PATH }); + for (const width of [390, 1280]) for (const theme of ['light', 'dark']) { + const page = await browser.newPage({ viewport: { width, height: 800 }, hasTouch: width === 390 }); + const errors = []; + page.on('pageerror', (error) => errors.push(error.message)); + page.on('console', (message) => { if (message.type() === 'error') errors.push(message.text()); }); + await page.addInitScript((value) => localStorage.setItem('starlight-theme', value), theme); + await page.goto(`http://127.0.0.1:${server.address().port}/starlight/table-terms/`); + assert.equal(await page.locator('html').getAttribute('data-theme'), theme); + const tables = await page.locator('table').evaluateAll((items) => items.map((table) => ({ width: table.clientWidth, scrollWidth: table.scrollWidth, height: table.clientHeight, scrollHeight: table.scrollHeight }))); + assert.ok(tables.length); + for (const table of tables) { + assert.equal(table.scrollWidth, table.width, 'table should not scroll horizontally'); + assert.equal(table.scrollHeight, table.height, 'table should not scroll vertically'); + } + const term = page.locator('table .term').last(); + const label = term.locator('.term-label'); + if (width === 390) await label.tap(); else await label.hover(); + const tip = term.locator('.term-tip'); + assert.equal(await tip.evaluate((el) => el.matches(':popover-open')), true); + const rect = await tip.boundingBox(); + assert.ok(rect && rect.x >= 0 && rect.x + rect.width <= width && rect.y >= 0 && rect.y + rect.height <= 800, 'card fits viewport'); + assert.equal(await tip.locator('a').getAttribute('href'), '/starlight/configuration'); + if (width === 1280) { + await label.focus(); + await page.evaluate(() => { document.querySelector('table').style.marginTop = '900px'; document.body.style.paddingBottom = '900px'; }); + await label.evaluate((el) => window.scrollBy(0, el.getBoundingClientRect().bottom - (innerHeight - 30))); + await page.waitForFunction(() => document.querySelector('table .term:last-child[data-above]') || document.querySelector('table .term[data-above]')); + const aboveRect = await tip.boundingBox(); + const labelRect = await label.boundingBox(); + assert.ok(aboveRect.y >= 0 && aboveRect.y + aboveRect.height <= labelRect.y + 1, 'card is placed above the term'); + } + await term.locator('.term-tip-close').click(); + assert.equal(await label.getAttribute('aria-expanded'), 'false'); + await label.focus(); + await page.keyboard.press('Enter'); + assert.equal(await label.getAttribute('aria-expanded'), 'true'); + await page.keyboard.press('Escape'); + assert.equal(await label.getAttribute('aria-expanded'), 'false'); + assert.deepEqual(errors, []); + console.log(`${width} ${theme}:`, tables); + await page.close(); + } +} finally { + await browser?.close(); + await new Promise((resolve) => server.close(resolve)); +}