-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(section-navigation): add section navigation component
- Loading branch information
Showing
5 changed files
with
84 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.section-navigation { | ||
border-bottom: 1px solid var(--contrast-8); | ||
border-top: 1px solid var(--contrast-8); | ||
display: flex; | ||
flex-direction: row; | ||
margin: 20px 0; | ||
} | ||
.section-navigation h2 { | ||
flex: 0 0 auto; | ||
font-size: 1rem; | ||
font-weight: 400; | ||
margin: 20px 0; | ||
} | ||
.section-navigation ul { | ||
display: flex; | ||
flex-direction: row; | ||
flex-wrap: wrap; | ||
margin: 0; | ||
padding: 10px 0; | ||
} | ||
.section-navigation li { | ||
list-style-type: none; | ||
flex: 0 0 auto; | ||
margin: 10px 0 10px 20px; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { useEffect, useState } from "preact/hooks"; | ||
|
||
import Icon from "./icon"; | ||
|
||
export default function SectionNavigation({ prompt = "Jump To:" }) { | ||
const [sections, setSections] = useState([]); | ||
const updateSections = () => { | ||
const newSections = []; | ||
for (let heading of document.querySelectorAll("[data-section-title]")) | ||
newSections.push({ | ||
icon: heading.dataset.sectionIcon, | ||
id: heading.id, | ||
title: heading.dataset.sectionTitle, | ||
}); | ||
|
||
setSections(newSections); | ||
}; | ||
useEffect(() => { | ||
updateSections(); | ||
document.addEventListener("accessci-update-sections", updateSections); | ||
}, []); | ||
return ( | ||
<nav class="section-navigation"> | ||
{prompt ? <h2>{prompt}</h2> : null} | ||
<ul> | ||
{sections.map(({ icon, id, title }) => ( | ||
<li> | ||
<a href={`#${id}`}> | ||
{icon ? ( | ||
<> | ||
<Icon name={icon} />{" "} | ||
</> | ||
) : null} | ||
{title} | ||
</a> | ||
</li> | ||
))} | ||
</ul> | ||
</nav> | ||
); | ||
} |
This file contains 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