Skip to content

Commit 9ad28ee

Browse files
committed
Show more headings in the table of contents
- Add h3 and h4 in the table of contents - When the table of contents is taller than the viewport's height, it will now be scrollable - Scroll the active table of content link in view if needed - Mark `<a>` as active instead of `<li>` so the border is only as tall as the active heading - Remove the bold styling for active to prevent content jump when a link takes one extra line when bolded
1 parent abb1d56 commit 9ad28ee

File tree

3 files changed

+26
-21
lines changed

3 files changed

+26
-21
lines changed

.eleventy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module.exports = function(config) {
88
config.addPassthroughCopy('src/img');
99
config.addPassthroughCopy('src/manifest.json');
1010

11-
config.addPlugin(pluginTOC, {tags: ['h2']});
11+
config.addPlugin(pluginTOC, {tags: ['h2', 'h3', 'h4']});
1212
config.addPlugin(syntaxHighlight);
1313
config.addPlugin(imageOptimizations);
1414
config.addPlugin(eleventyNavigationPlugin);

src/_includes/scripts/in-page-nav.js

+16-15
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
const observer = new IntersectionObserver(entries => {
2-
entries.forEach(entry => {
3-
const id = entry.target.getAttribute('id');
4-
5-
if (entry.intersectionRatio > 0) {
6-
if (document.querySelector(`.pageNav li.is-active`)) {
7-
document.querySelector(`.pageNav li.is-active`).classList.remove('is-active');
8-
}
9-
10-
document.querySelector(`.pageNav a[href="#${id}"]`).parentElement.classList.add('is-active');
2+
const intersectingEntries = entries.filter(e => e.isIntersecting);
3+
for (const entry of intersectingEntries) {
4+
const previouslyActive = document.querySelector('.pageNav a.is-active');
5+
if (previouslyActive) {
6+
previouslyActive.classList.remove('is-active');
117
}
12-
})
8+
9+
const id = entry.target.getAttribute('id')
10+
const newActive = document.querySelector(`.pageNav a[href="#${id}"]`);
11+
newActive.classList.add('is-active');
12+
newActive.scrollIntoView({ block: 'nearest' });
13+
}
1314
}, { rootMargin: `0% 0% -90% 0%` }
1415
);
1516

16-
//track h2's with an id
17+
//track headings with an id
1718
if (document.querySelector('.pageNav')) {
18-
document.querySelectorAll('h2[id]').forEach((section) => {
19-
observer.observe(section);
20-
});
21-
}
19+
for (const heading of document.querySelectorAll(':is(h2,h3,h4)[id]')) {
20+
observer.observe(heading)
21+
}
22+
}

src/_includes/styles.css

+9-5
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,11 @@ nav{
337337
padding-left: 0;
338338
border-left: 1px solid var(--light-grey);
339339
font-size: .9em;
340-
340+
margin: 0;
341341
}
342342
.pageNav a{
343343
padding-left: 1em;
344+
border-left: 3px solid transparent;
344345
display: inline-block;
345346
}
346347
nav ul{
@@ -358,7 +359,7 @@ nav a:hover{
358359
nav li{
359360
margin-bottom: 1em;
360361
}
361-
nav .is-active > a{
362+
.primaryNav .is-active > a{
362363
font-weight: bold;
363364
color: #000;
364365
}
@@ -368,14 +369,17 @@ nav li ul{
368369
}
369370

370371
.toc .is-active{
371-
border-left: 3px solid var(--wpt-navy);
372+
border-left-color: var(--wpt-navy);
372373
color: var(--wpt-navy);
373374
}
374375
@media (min-width: 68em) {
375376
.toc{
377+
--top-offset: 1em;
376378
position: sticky;
377-
top: 1em;
378-
padding-top: .5em;
379+
top: var(--top-offset);
380+
margin-top: 1.5em;
381+
max-height: calc(100vh - 2 * var(--top-offset));
382+
overflow-y: auto;
379383
}
380384
}
381385
.toc li{

0 commit comments

Comments
 (0)