Last active
November 18, 2021 10:31
-
-
Save victory-sokolov/f527f6c92a5a509b18bada694da3d920 to your computer and use it in GitHub Desktop.
Gatsby Table of contents
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{typeof tableOfContents.items === "undefined" ? null : ( | |
<Toc> | |
<h3 className="uppercase">Table of contents</h3> | |
<ContentsList items={tableOfContents.items} /> | |
</Toc> | |
)} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import styled from "styled-components" | |
import React from 'react' | |
export const Toc = styled.nav` | |
display: flex; | |
flex-direction: column; | |
ul { | |
list-style-type: none; | |
li { | |
a { | |
color: var(--mode); | |
font-size: 1.8rem; | |
font-weight: 500; | |
&:hover { | |
color: var(--link-color); | |
} | |
} | |
} | |
} | |
li > ul li { | |
margin-left: 3rem; | |
a { | |
font-weight: 400; | |
font-size: 1.6rem; | |
} | |
} | |
li.active > a { | |
color: #333; | |
font-weight: 500; | |
} | |
` | |
export const ContentsList = ({ items }) => { | |
return ( | |
<ul> | |
{items.map(item => { | |
return <ContentsItem key={`${item.url}-item`} item={item} /> | |
})} | |
</ul> | |
) | |
} | |
export const ContentsItem = ({ item }) => ( | |
<li> | |
<a href={item.url}>{item.title}</a> | |
{item.items && item.items.length && ( | |
<ContentsList key={`${item.url}`} items={item.items} /> | |
)} | |
</li> | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment