Skip to content

Commit ec4dbfd

Browse files
fix(TOC): hide active overlay when no sections active (saicaca#586)
* fix(TOC): hide active overlay when none active * fix indentation
1 parent 4ad55e0 commit ec4dbfd

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ pnpm-debug.log*
2525
package-lock.json
2626
bun.lockb
2727
yarn.lock
28+
29+
# ide
30+
.idea
31+
*.iml

src/components/widget/TOC.astro

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const maxLevel = siteConfig.toc.depth;
5555
}]}>{removeTailingHash(heading.text)}</div>
5656
</a>
5757
)}
58-
<div id="active-indicator" class:list={[{'hidden': headings.length == 0}, "-z-10 absolute bg-[var(--toc-btn-hover)] left-0 right-0 rounded-xl transition-all " +
58+
<div id="active-indicator" style="opacity: 0" class:list={[{'hidden': headings.length == 0}, "-z-10 absolute bg-[var(--toc-btn-hover)] left-0 right-0 rounded-xl transition-all " +
5959
"group-hover:bg-transparent border-2 border-[var(--toc-btn-hover)] group-hover:border-[var(--toc-btn-active)] border-dashed"]}></div>
6060
</table-of-contents>}
6161

@@ -97,7 +97,7 @@ class TableOfContents extends HTMLElement {
9797

9898
toggleActiveHeading = () => {
9999
let i = this.active.length - 1;
100-
let min = this.active.length - 1, max = 0;
100+
let min = this.active.length - 1, max = -1;
101101
while (i >= 0 && !this.active[i]) {
102102
this.tocEntries[i].classList.remove(this.visibleClass);
103103
i--;
@@ -112,11 +112,15 @@ class TableOfContents extends HTMLElement {
112112
this.tocEntries[i].classList.remove(this.visibleClass);
113113
i--;
114114
}
115-
let parentOffset = this.tocEl?.getBoundingClientRect().top || 0;
116-
let scrollOffset = this.tocEl?.scrollTop || 0;
117-
let top = this.tocEntries[min].getBoundingClientRect().top - parentOffset + scrollOffset;
118-
let bottom = this.tocEntries[max].getBoundingClientRect().bottom - parentOffset + scrollOffset;
119-
this.activeIndicator?.setAttribute("style", `top: ${top}px; height: ${bottom - top}px`);
115+
if (min > max) {
116+
this.activeIndicator?.setAttribute("style", `opacity: 0`);
117+
} else {
118+
let parentOffset = this.tocEl?.getBoundingClientRect().top || 0;
119+
let scrollOffset = this.tocEl?.scrollTop || 0;
120+
let top = this.tocEntries[min].getBoundingClientRect().top - parentOffset + scrollOffset;
121+
let bottom = this.tocEntries[max].getBoundingClientRect().bottom - parentOffset + scrollOffset;
122+
this.activeIndicator?.setAttribute("style", `top: ${top}px; height: ${bottom - top}px`);
123+
}
120124
};
121125

122126
scrollToActiveHeading = () => {

0 commit comments

Comments
 (0)