Skip to content

Commit 4323d09

Browse files
authored
Add support for BS ScrollSpy to show active TOC entry (#2287)
1 parent aa0f617 commit 4323d09

File tree

5 files changed

+43
-7
lines changed

5 files changed

+43
-7
lines changed

assets/scss/_sidebar-toc.scss

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
}
2626

2727
.td-toc {
28+
$toc-padding-base: 1rem;
29+
$toc-padding-increment: 0.5rem; // for TOC entry nesting
30+
31+
margin-top: 1rem;
32+
2833
a {
2934
display: block;
3035
font-weight: $font-weight-light;
@@ -36,23 +41,49 @@
3641
display: block;
3742
}
3843

39-
li li {
40-
margin-left: 0.5rem;
44+
> .toc-title {
45+
font-weight: $font-weight-bold;
46+
color: var(--bs-secondary-color);
47+
border-bottom: 1px solid var(--bs-tertiary-color);
48+
padding-bottom: 0.5rem;
49+
margin-bottom: 0.5rem;
4150
}
4251

4352
#TableOfContents {
44-
// Hugo's ToC is a mouthful, this can be used to style the top level h2 entries.
45-
> ul > li > ul > li > a {
46-
}
53+
margin-left: -0.75rem;
4754

4855
a {
56+
margin-left: 0;
57+
padding-left: $toc-padding-base;
58+
text-decoration: none;
59+
border-left: .125rem solid transparent;
60+
transition: color 0.15s ease-in-out, border-color 0.15s ease-in-out;
4961
color: var(--bs-secondary-color);
5062

63+
&.active {
64+
color: var(--bs-primary);
65+
border-left-color: var(--bs-primary);
66+
font-weight: $font-weight-medium;
67+
}
68+
5169
&:focus,
5270
&:hover {
5371
color: initial;
5472
}
5573
}
74+
75+
ul ul a {
76+
padding-left: $toc-padding-base + $toc-padding-increment;
77+
}
78+
ul ul ul a {
79+
padding-left: $toc-padding-base + ($toc-padding-increment * 2);
80+
}
81+
ul ul ul ul a {
82+
padding-left: $toc-padding-base + ($toc-padding-increment * 3);
83+
}
84+
ul ul ul ul ul a {
85+
padding-left: $toc-padding-base + ($toc-padding-increment * 4);
86+
}
5687
}
5788

5889
ul {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data-bs-spy="scroll" {{/**/ -}}
2+
data-bs-target="#TableOfContents" {{/**/ -}}
3+
data-bs-smooth-scroll="true" {{/**/ -}}
4+
data-bs-root-margin="0px 0px -40%" {{/**/ -}}

layouts/_partials/toc.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{{ with .TableOfContents -}}
33
{{ if ne . `<nav id="TableOfContents"></nav>` -}}
44
<div class="td-toc">
5+
<div class="toc-title">On this page</div>
56
{{ . }}
67
</div>
78
{{ end -}}

layouts/blog/baseof.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{{ partial "navbar.html" . }}
1212
</header>
1313
<div class="container-fluid td-outer">
14-
<div class="td-main">
14+
<div class="td-main" {{ partialCached "td/scroll-spy-attr.txt" . | safeHTMLAttr }}>
1515
<div class="row flex-xl-nowrap">
1616
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
1717
{{ partial "sidebar.html" . }}

layouts/docs/baseof.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
{{ partial "navbar.html" . }}
1212
</header>
1313
<div class="container-fluid td-outer">
14-
<div class="td-main">
14+
<div class="td-main" {{ partialCached "td/scroll-spy-attr.txt" . | safeHTMLAttr }}>
1515
<div class="row flex-xl-nowrap">
1616
<aside class="col-12 col-md-3 col-xl-2 td-sidebar d-print-none">
1717
{{ partial "sidebar.html" . }}

0 commit comments

Comments
 (0)