website-astro/src/components/TOC.astro

95 lines
1.9 KiB
Plaintext
Raw Normal View History

2022-12-26 23:15:27 +01:00
---
import { t } from "astro-i18n";
const { toc } = Astro.props;
---
<aside class="table-of-content">
<details open="true">
2022-12-28 10:36:15 +01:00
<summary>{t("index.toc")}</summary>
<nav role="navigation" aria-label={t("index.toc")}>
<ol class="table-of-content__list" role="list">
2022-12-26 23:15:27 +01:00
{
// 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>
2022-12-28 10:36:15 +01:00
) : // if h3, set as inner ol > li
2022-12-26 23:15:27 +01:00
heading.depth === 3 ? (
2022-12-28 10:36:15 +01:00
<ol role="list">
2022-12-26 23:15:27 +01:00
<li>
<a href={`#${heading.slug}`} class="toc-3">
{heading.text}
</a>
</li>
2022-12-28 10:36:15 +01:00
</ol>
2022-12-26 23:15:27 +01:00
) : null
)
}
2022-12-28 10:36:15 +01:00
</ol>
2022-12-26 23:15:27 +01:00
</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);
}
2022-12-28 10:36:15 +01:00
summary {
padding: var(--space-2xs-xs);
font-size: var(--size-0);
font-weight: 500;
background-color: var(--color-soft-blue);
}
2022-12-26 23:15:27 +01:00
.table-of-content__list {
2022-12-28 10:36:15 +01:00
padding: 0 var(--space-2xs);
padding-block-end: var(--space-2xs);
border: 2px solid var(--color-soft-blue);
2022-12-26 23:15:27 +01:00
}
.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;
2022-12-28 10:36:15 +01:00
transform: translate(-6px, 0);
2022-12-26 23:15:27 +01:00
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 {
2022-12-28 10:36:15 +01:00
margin-inline-start: 1rem;
margin-block-start: 0.4rem;
2022-12-26 23:15:27 +01:00
}
</style>