website-astro/src/components/TOC.astro

99 lines
2 KiB
Text
Raw Normal View History

2022-12-26 22:15:27 +00:00
---
2024-12-29 11:33:22 +00:00
const { toc } = Astro.props
2022-12-26 22:15:27 +00:00
---
2024-12-29 11:33:22 +00:00
<aside class='table-of-content'>
<details open='true'>
<summary id='toc-title'>Table des matières</summary>
<nav role='navigation' aria-labelledby='toc-title'>
<ol class='table-of-content__list' role='list'>
2022-12-26 22:15:27 +00:00
{
// loop over the toc
2023-06-12 13:47:35 +00:00
toc.map(
(
heading
// if h2, set as a li
) =>
heading.depth === 2 ? (
2022-12-26 22:15:27 +00:00
<li>
2024-12-29 11:33:22 +00:00
<a href={`#${heading.slug}`} class='toc-2'>
2022-12-26 22:15:27 +00:00
{heading.text}
</a>
2023-06-12 13:47:35 +00:00
</li> // if h3, set as inner ol > li
) : heading.depth === 3 ? (
2024-12-29 11:33:22 +00:00
<ol role='list'>
2023-06-12 13:47:35 +00:00
<li>
2024-12-29 11:33:22 +00:00
<a href={`#${heading.slug}`} class='toc-3'>
2023-06-12 13:47:35 +00:00
{heading.text}
</a>
</li>
</ol>
) : null
2022-12-26 22:15:27 +00:00
)
}
2022-12-28 09:36:15 +00:00
</ol>
2022-12-26 22:15:27 +00: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 09:36:15 +00:00
summary {
padding: var(--space-2xs-xs);
font-size: var(--size-0);
font-weight: 500;
background-color: var(--color-soft-blue);
}
2022-12-26 22:15:27 +00:00
.table-of-content__list {
2022-12-28 09:36:15 +00:00
padding: 0 var(--space-2xs);
padding-block-end: var(--space-2xs);
border: 2px solid var(--color-soft-blue);
2022-12-26 22:15:27 +00: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 {
2024-12-29 11:33:22 +00:00
content: '';
2022-12-26 22:15:27 +00:00
position: absolute;
2023-06-12 13:47:35 +00:00
top: 50%;
2022-12-26 22:15:27 +00:00
left: 0;
2023-06-12 13:47:35 +00:00
inline-size: 2px;
block-size: 2px;
border-radius: 4px;
2022-12-28 09:36:15 +00:00
transform: translate(-6px, 0);
2023-06-12 13:47:35 +00:00
background-color: var(--color-grey);
2022-12-26 22:15:27 +00:00
}
.table-of-content__list a:visited::before {
2023-06-12 13:47:35 +00:00
background-color: white;
2022-12-26 22:15:27 +00:00
}
.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 09:36:15 +00:00
margin-inline-start: 1rem;
margin-block-start: 0.4rem;
2022-12-26 22:15:27 +00:00
}
</style>