-
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.
- Loading branch information
1 parent
d4e0c51
commit 566c775
Showing
2 changed files
with
96 additions
and
75 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { useState, useEffect, SetStateAction } from 'react'; | ||
import { SideNav as UswdsSideNav } from '@trussworks/react-uswds'; | ||
import classNames from 'classnames'; | ||
|
||
interface Section { | ||
title: string; | ||
id: string; | ||
} | ||
|
||
interface SideNavProps { | ||
sections: Section[]; | ||
} | ||
|
||
export const SideNav = ({ sections }: SideNavProps) => { | ||
const [selectedHash, setSelectedHash] = useState(`#${sections[0].id}`); | ||
|
||
useEffect(() => { | ||
let currentSection = ''; | ||
|
||
const observer = new IntersectionObserver( | ||
(entries) => { | ||
entries.forEach((entry) => { | ||
if (entry.isIntersecting) { | ||
const newSection = `#${entry.target.id}`; | ||
if (currentSection !== newSection) { | ||
currentSection = newSection; | ||
setSelectedHash(newSection); | ||
} | ||
} | ||
}); | ||
}, | ||
{ | ||
rootMargin: '-20% 0px -70% 0px', | ||
threshold: 0, | ||
}, | ||
); | ||
|
||
sections.forEach(({ id }) => { | ||
const element = document.getElementById(id); | ||
if (element) observer.observe(element); | ||
}); | ||
|
||
return () => { | ||
sections.forEach(({ id }) => { | ||
const element = document.getElementById(id); | ||
if (element) observer.unobserve(element); | ||
}); | ||
}; | ||
}, [sections]); | ||
|
||
return ( | ||
<UswdsSideNav | ||
items={sections.map(({ title, id }) => ( | ||
<NavItem | ||
title={title} | ||
id={id} | ||
key={id} | ||
selectedHash={selectedHash} | ||
setSelectedHash={setSelectedHash} | ||
/> | ||
))} | ||
/> | ||
); | ||
}; | ||
|
||
interface NavItemProps { | ||
title: string; | ||
id: string; | ||
selectedHash: string; | ||
setSelectedHash: (value: SetStateAction<string>) => void; | ||
} | ||
function NavItem({ title, id, selectedHash, setSelectedHash }: NavItemProps) { | ||
const itemHash = `#${id}`; | ||
return ( | ||
<a | ||
href={itemHash} | ||
key={id} | ||
className={classNames({ | ||
'usa-current': itemHash === selectedHash, | ||
})} | ||
onClick={() => setSelectedHash(itemHash)} | ||
> | ||
{title} | ||
</a> | ||
); | ||
} |
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