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
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/astro.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions demo/src/content/docs/table-terms.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
14 changes: 13 additions & 1 deletion packages/starlight/src/lib/glossary-tooltips.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -26,25 +28,31 @@ 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;
const term = current;
// 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;
};
const open = (term) => {
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);
Expand Down Expand Up @@ -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);
};
}
27 changes: 22 additions & 5 deletions packages/starlight/src/styles/glossary.css
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}

.sl-markdown-content .term-tip {
display: none;
position: absolute;
top: 100%;
left: 0;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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. */
Expand Down
69 changes: 69 additions & 0 deletions scripts/check-glossary-browser.mjs
Original file line number Diff line number Diff line change
@@ -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));
}
Loading