Metadata
-
Date
-
Tagged
-
Older post
-
Newer post
Adding a Table of Contents that updates on scroll
Adding a Table of Contents to an article can be useful to see at a glance which topics the article covers.
Scott Spence recently wrote an in depth guide on how to add a table of contents to a Gatsby blog that uses mdx.
I took the ideas from that post, expanded on them and implemented a table of contents for this blog. If all went well, you will see a section called “TABLE OF CONTENTS” floating next to the text of this article, updating the styles to indicate which heading you’re currently on.
Starting point
The following guide starts off from a functioning Gatsby blog that uses mdx (by using gatsby-plugin-mdx
).
Getting the data
As a wise man once said: “Get the data, do the thing!”.
The goal is to create an object that lists every heading on the page, along with the corresponding CSS-id for that heading.
Add CSS-ids to all headings
By default, headings (<h1>
s to <h6>
s) do not have a css id
tied to them.
We want to add one to each heading. It doesn’t matter how we accomplish this, only that each heading has an id
.
That id
will later be used in an anchor-tag to link to that heading on the page.
I did this by adding the remark-slug
plugin.
<h2>Puppies are cute</h2>
will turn into <h2 id="puppies-are-cute">Puppies are cute</h2>
, opening the door to link to that point in the page with <a href="#puppies-are-cute">Puppies are cute</a>
.
Querying for the tableOfContents
object.
gatsby-plugin-mdx
allows you to query for a field called tableOfContents
.
The tableOfContents
object lists the text of all headings in the document (eg. Puppies are awesome
), along with the link to their corresponding CSS-id (eg. #puppies-are-awesome
).
Lower level headings are nested under their higher level parents (an h3 will be nested under an h2).
Example
To visualize this better, an example!
For an .mdx
file with the following headings:
The resulting tableOfContents
object would look like:
Using the data
Once you query the tableOfContents
for a blogpost, pass it down to its own component.
Since the tableOfContents
object can be empty if there are no headings, conditionally render the <TableOfContents />
component.
One level of headings
The items
prop is an array filled with objects.
Scroll up for a reminder of how those objects look.
For now, let’s iterate over that top-level array and render the first level of headings.
Nested heading levels
How do we get the nested levels of headings to also show up?
Drumroll 🥁🥁🥁 … RECURSION!
Recursion is one of those scary words for a concept that seems very complicated at first and suddenly clicks. This video by Computerphile explains it beautifully
Each item
we previously rendered can have an items
array within it.
This can continue up to 6 times (h1 to h6).
To account for that, we’ll repeat the logic we wrote to display a single level of headings.
First, a little refactor
In the renderItems
function, check if the current item in the loop has an items
property on it.
If it does, repeat the same logic for those items
.
Styling the active heading
To style the link to the active heading differently from all the other headings in the table of contents, we first have to find out which one is currently active.
You can mark a heading as “active” when it’s visible to the user, when it’s in the viewport. The Intersection Observer API is an ideal tool for this.
An IntersectionObserver
works by first setting up the logic for it, and then telling it to start watching an element (eg. an anchor tag).
After telling an IntersectionObserver
to keep track of an element, it will fire a callback function every time it is triggered (eg. the anchor tag enters the viewport).
Get all the heading-ids
To tell the InterSectionObserver
which element to observe, it expects a reference to a DOM-element.
Getting references to all heading elements in the table of contents can be done by calling document.getElementById()
with each heading’s CSS-id.
Luckily, all the information needed for this is already there, inside the tableOfContents
object.
The following helper function will take in an items
array from the tableOfContents
object and return a flat array that contains all the CSS-ids.
For our example above, the resulting array from getIds(props.items)
in the <TableOfContents items={tableOfContents.items}/>
component would be:
Get the active heading’s id
We can use the data we just gathered in order to get the data we’re really after: which heading is active right now?.
We’ll keep track of which heading is active inside a React custom hook called useActiveId
.
This hook returns a piece of state that holds the id of the heading that’s currently active.
Using the gathered data
Back in the function for the <TableOfContents />
component, the active id can be used to change the styling of the active heading.
Pass the id of the active heading to the renderItems
function.
Inside the renderItems
function, pass the activeId
down to the recursive call of the function.
Check if the id for the current element is the same as the one passed in as activeId
and adjust the styling accordingly.