Files
libretro/website/src/components/PageTitle.astro
T

114 lines
3.7 KiB
Plaintext

---
// Breadcrumbs, the level label and the title. The trail says which tab a
// deep page belongs to, and the label says who the page is written for. The
// label is left out where the header tab already says it: on a tab's own
// page, and where the first crumb names that tab. A page with a `logo` (a
// path under the site root) shows it beside its title, on a white square so
// a dark mark stays visible in the dark theme. A setup page with no logo file
// gets its initials in the same square, as its tile does (sitemarkup.py
// platform_tiles).
//
// Each crumb's label sits in a span: the item keeps its "/" (retrobios.css,
// `li { white-space: nowrap }`) and only the label wraps, so a long label
// never leaves the separator alone at the start of a line.
//
// The crumbs and the title stay out of the search index: every excerpt
// began with the title and the crumbs matched "bios" on every page. The
// title still names the result, as the page's title field.
import setups from '../../public/data/picker/index.json';
const PAGE_TITLE_ID = '_top';
const MARK = 40;
const LABELS = {
start: 'Start here',
setup: 'Setup guide',
explore: 'Details',
reference: 'Reference',
contribute: 'For contributors',
} as const;
// The tab each level is read in, as the header names it.
const TABS = {
start: 'Start here',
setup: 'Setups',
explore: 'Explore',
reference: 'Reference',
contribute: 'Contribute',
} as const;
// Up to two capitals of a name: MyMinUI is MM (sitemarkup.initials).
const initials = (name: string): string => {
const capitals = [...name].filter((c) => c !== c.toLowerCase());
return capitals.slice(0, 2).join('') || name.slice(0, 2).toUpperCase();
};
const { id, data } = Astro.locals.starlightRoute.entry;
const { title, level, logo } = data;
const crumbs = Astro.locals.rbBreadcrumbs ?? [];
const badge = level && crumbs.length > 0 && crumbs[0].label !== TABS[level] && crumbs[0].label !== LABELS[level];
const base = import.meta.env.BASE_URL.replace(/\/?$/, '/');
const logoSrc = logo && (/^(https?:)?\//.test(logo) ? logo : base + logo);
const setup = !logoSrc && setups.find((entry) => id === `platforms/${entry.platform}`);
---
{
crumbs.length > 0 && (
<nav class="rb-breadcrumbs" aria-label="Breadcrumb" data-pagefind-ignore>
<ol>
{crumbs.map((crumb) => (
<li>{crumb.href ? <a href={crumb.href}><span>{crumb.label}</span></a> : <span>{crumb.label}</span>}</li>
))}
</ol>
</nav>
)
}
{badge && <span class={`rb-level rb-level-${level}`}>{LABELS[level]}</span>}
{
logoSrc || setup ? (
<div class="rb-title-logo">
{logoSrc ? (
<img class="rb-title-mark" src={logoSrc} alt="" width={MARK} height={MARK} />
) : (
<svg class="rb-title-mark" viewBox="0 0 40 40" width={MARK} height={MARK} aria-hidden="true">
<text x="20" y="25" text-anchor="middle" font-size="14" font-weight="700" fill="#2b2e5c">
{initials(setup.name)}
</text>
</svg>
)}
<h1 id={PAGE_TITLE_ID} data-pagefind-ignore data-pagefind-meta="title">{title}</h1>
</div>
) : (
<h1 id={PAGE_TITLE_ID} data-pagefind-ignore data-pagefind-meta="title">{title}</h1>
)
}
<style>
h1 {
margin-top: 0.25rem;
font-size: var(--sl-text-h1);
line-height: var(--sl-line-height-headings);
font-weight: 700;
color: var(--sl-color-white);
}
.rb-title-logo {
display: flex;
gap: 0.75rem;
align-items: center;
margin-top: 0.25rem;
}
.rb-title-logo h1 { margin-top: 0; }
/* The tiles' square: white in both themes, a hairline, the mark inside. */
.rb-title-mark {
flex: none;
box-sizing: border-box;
width: 40px;
height: 40px;
padding: 3px;
border: 1px solid var(--rb-border);
border-radius: 6px;
background: #fff;
object-fit: contain;
}
</style>