feat: support non-singleton table of contents
This commit is contained in:
parent
5480269d38
commit
1cd8e7f0d5
4 changed files with 14 additions and 16 deletions
|
@ -29,8 +29,7 @@ const TableOfContents: QuartzComponent = ({
|
||||||
<div class={classNames(displayClass, "toc")}>
|
<div class={classNames(displayClass, "toc")}>
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
id="toc"
|
class={fileData.collapseToc ? "collapsed toc-header" : "toc-header"}
|
||||||
class={fileData.collapseToc ? "collapsed" : ""}
|
|
||||||
aria-controls="toc-content"
|
aria-controls="toc-content"
|
||||||
aria-expanded={!fileData.collapseToc}
|
aria-expanded={!fileData.collapseToc}
|
||||||
>
|
>
|
||||||
|
@ -50,7 +49,7 @@ const TableOfContents: QuartzComponent = ({
|
||||||
<polyline points="6 9 12 15 18 9"></polyline>
|
<polyline points="6 9 12 15 18 9"></polyline>
|
||||||
</svg>
|
</svg>
|
||||||
</button>
|
</button>
|
||||||
<div id="toc-content" class={fileData.collapseToc ? "collapsed" : ""}>
|
<div class={fileData.collapseToc ? "collapsed toc-content" : "toc-content"}>
|
||||||
<OverflowList id="toc-ul">
|
<OverflowList id="toc-ul">
|
||||||
{fileData.toc.map((tocEntry) => (
|
{fileData.toc.map((tocEntry) => (
|
||||||
<li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}>
|
<li key={tocEntry.slug} class={`depth-${tocEntry.depth}`}>
|
||||||
|
@ -72,7 +71,7 @@ const LegacyTableOfContents: QuartzComponent = ({ fileData, cfg }: QuartzCompone
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
<details id="toc" open={!fileData.collapseToc}>
|
<details class="toc" open={!fileData.collapseToc}>
|
||||||
<summary>
|
<summary>
|
||||||
<h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
|
<h3>{i18n(cfg.locale).components.tableOfContents.title}</h3>
|
||||||
</summary>
|
</summary>
|
||||||
|
|
|
@ -25,16 +25,15 @@ function toggleToc(this: HTMLElement) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupToc() {
|
function setupToc() {
|
||||||
const toc = document.getElementById("toc")
|
for (const toc of document.querySelectorAll(".toc")) {
|
||||||
if (toc) {
|
const button = toc.querySelector(".toc-header")
|
||||||
const content = toc.nextElementSibling as HTMLElement | undefined
|
const content = toc.querySelector(".toc-content")
|
||||||
if (!content) return
|
if (!button || !content) return
|
||||||
toc.addEventListener("click", toggleToc)
|
button.addEventListener("click", toggleToc)
|
||||||
window.addCleanup(() => toc.removeEventListener("click", toggleToc))
|
window.addCleanup(() => button.removeEventListener("click", toggleToc))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("resize", setupToc)
|
|
||||||
document.addEventListener("nav", () => {
|
document.addEventListener("nav", () => {
|
||||||
setupToc()
|
setupToc()
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
details#toc {
|
details.toc {
|
||||||
& summary {
|
& summary {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
|
|
|
@ -6,18 +6,18 @@
|
||||||
|
|
||||||
overflow-y: hidden;
|
overflow-y: hidden;
|
||||||
flex: 0 1 auto;
|
flex: 0 1 auto;
|
||||||
&:has(button#toc.collapsed) {
|
&:has(button.toc-header.collapsed) {
|
||||||
flex: 0 1 1.2rem;
|
flex: 0 1 1.2rem;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@media all and not ($mobile) {
|
@media all and not ($mobile) {
|
||||||
.toc {
|
.toc-header {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
button#toc {
|
button.toc-header {
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
border: none;
|
border: none;
|
||||||
text-align: left;
|
text-align: left;
|
||||||
|
@ -44,7 +44,7 @@ button#toc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#toc-content {
|
.toc-content {
|
||||||
list-style: none;
|
list-style: none;
|
||||||
position: relative;
|
position: relative;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue