website-astro/src/components/TOC.astro

87 lines
1.6 KiB
Plaintext

---
import { t } from "astro-i18n";
const { toc } = Astro.props;
---
<aside class="table-of-content">
<details open="true">
<summary>{t("toc")}</summary>
<nav role="navigation" aria-label={t("toc")}>
<ul class="table-of-content__list" role="list">
{
// loop over the toc
toc.map((heading) =>
// if h2, set as a li
heading.depth === 2 ? (
<li>
<a href={`#${heading.slug}`} class="toc-2">
{heading.text}
</a>
</li>
) : // if h3, set as inner ul > li
heading.depth === 3 ? (
<ul role="list">
<li>
<a href={`#${heading.slug}`} class="toc-3">
{heading.text}
</a>
</li>
</ul>
) : null
)
}
</ul>
</nav>
</details>
</aside>
<style>
/* Table of content */
.table-of-content {
margin: 0;
padding: 0;
position: relative;
display: block;
font-size: var(--size--1);
}
.table-of-content details {
position: sticky;
top: var(--space-m);
}
.table-of-content__list {
padding-bottom: 2rem;
}
.table-of-content__list a {
position: relative;
display: inline-block;
font-weight: normal;
color: var(--color-grey);
text-decoration: none;
}
.table-of-content__list a::before {
content: "·";
position: absolute;
top: 0;
left: 0;
transform: translate(-1rem, 0);
color: var(--color-grey);
}
.table-of-content__list a:visited::before {
color: white;
}
.table-of-content__list a:focus,
.table-of-content__list a:hover {
color: var(--color-violet);
text-decoration: underline;
}
.toc-2 {
margin: 0.8rem 0 0.3rem;
}
.toc-3 {
margin-left: 1rem;
margin-top: 0.4rem;
}
</style>