-
Notifications
You must be signed in to change notification settings - Fork 29
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
Showing
9 changed files
with
264 additions
and
12 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
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,13 @@ | ||
import React from 'react'; | ||
|
||
type IMiniNavBarHeader = { | ||
id: string; | ||
children: React.ReactNode; | ||
}; | ||
export default function MiniNavBarHeader({ id, children }: IMiniNavBarHeader) { | ||
return ( | ||
<h4 id={id} className={'mt-4'} mini-nav-bar-header=""> | ||
{children} | ||
</h4> | ||
); | ||
} |
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,218 @@ | ||
import React, { useEffect, useRef, useState, useCallback } from 'react'; | ||
import { Link, useLocation } from 'react-router-dom'; | ||
import { Row, Col } from 'react-bootstrap'; | ||
|
||
function getNavBarSectionElements() { | ||
return document.querySelectorAll('[mini-nav-bar-header]'); | ||
} | ||
|
||
function useScrollToHash({ stickyHeight }: { stickyHeight: number }) { | ||
const location = useLocation(); | ||
|
||
useEffect(() => { | ||
const hash = location.hash; | ||
if (hash) { | ||
let element: Element | null; | ||
try { | ||
element = document.querySelector(hash); | ||
} catch { | ||
element = null; | ||
} | ||
if (element) { | ||
const targetPosition = | ||
element.getBoundingClientRect().top + window.scrollY; | ||
window.scrollTo({ | ||
behavior: 'smooth', | ||
top: targetPosition - stickyHeight, | ||
}); | ||
} | ||
} | ||
}, [location]); | ||
} | ||
|
||
type IStickyMiniNavBar = { | ||
title: string; | ||
linkUnderlineColor: string; | ||
stickyBackgroundColor: string; | ||
}; | ||
|
||
export default function StickyMiniNavBar({ | ||
title, | ||
linkUnderlineColor, | ||
stickyBackgroundColor, | ||
}: IStickyMiniNavBar) { | ||
const [headerHeight, setHeaderHeight] = useState(0); | ||
const [isSticky, setIsSticky] = useState(false); | ||
const [sections, setSections] = useState< | ||
{ id: string; label: string | null }[] | ||
>([]); | ||
const [passedElements, setPassedElements] = useState<Record<string, boolean>>( | ||
{} | ||
); | ||
const stickyDivRef = useRef<HTMLDivElement | null>(null); | ||
useScrollToHash({ | ||
stickyHeight: | ||
headerHeight + | ||
(stickyDivRef.current?.getBoundingClientRect().height ?? 0), | ||
}); | ||
|
||
useEffect(() => { | ||
const newSections: typeof sections = []; | ||
const miniNavBarSections = getNavBarSectionElements(); | ||
miniNavBarSections.forEach(ele => { | ||
newSections.push({ | ||
id: ele.id, | ||
label: ele.textContent, | ||
}); | ||
}); | ||
setSections(newSections); | ||
|
||
const headerElement = document.querySelector('header'); | ||
|
||
const updateHeaderHeight = () => { | ||
if (headerElement) { | ||
setHeaderHeight(headerElement.getBoundingClientRect().height); | ||
} | ||
}; | ||
|
||
updateHeaderHeight(); | ||
|
||
const resizeObserver = new ResizeObserver(() => { | ||
updateHeaderHeight(); | ||
}); | ||
|
||
if (headerElement) { | ||
resizeObserver.observe(headerElement); | ||
} | ||
|
||
return () => { | ||
if (headerElement) { | ||
resizeObserver.unobserve(headerElement); | ||
} | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
const miniNavBarSections = getNavBarSectionElements(); | ||
const intersectionObserver = new IntersectionObserver(entries => { | ||
const newPassedElements: typeof passedElements = {}; | ||
entries.forEach(entry => { | ||
const targetId = entry.target.getAttribute('id') ?? ''; | ||
const hasId = sections.find(x => x.id === targetId); | ||
newPassedElements[targetId] = | ||
hasId !== undefined && | ||
(entry.isIntersecting || entry.boundingClientRect.y < 0); | ||
}); | ||
setPassedElements(x => { | ||
return { | ||
...x, | ||
...newPassedElements, | ||
}; | ||
}); | ||
}); | ||
miniNavBarSections.forEach(x => intersectionObserver.observe(x)); | ||
return () => { | ||
miniNavBarSections.forEach(x => intersectionObserver.unobserve(x)); | ||
}; | ||
}, [sections]); | ||
|
||
const handleScroll = () => { | ||
if (stickyDivRef.current) { | ||
const stickyOffset = stickyDivRef.current.getBoundingClientRect().top; | ||
setIsSticky(stickyOffset <= headerHeight); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
window.addEventListener('scroll', handleScroll); | ||
|
||
return () => { | ||
window.removeEventListener('scroll', handleScroll); | ||
}; | ||
}, [headerHeight]); | ||
|
||
let currentSectionId = sections[0]?.id; | ||
for (let i = sections.length - 1; i >= 0; i--) { | ||
const id = sections[i].id; | ||
if (passedElements[id]) { | ||
currentSectionId = id; | ||
break; | ||
} | ||
} | ||
|
||
return ( | ||
<Row | ||
className="justify-content-center" | ||
style={{ | ||
position: 'sticky', | ||
top: headerHeight, | ||
zIndex: 100, | ||
backgroundColor: isSticky ? stickyBackgroundColor : undefined, | ||
}} | ||
> | ||
<Col md={11}> | ||
<nav | ||
ref={stickyDivRef} | ||
className="d-flex flex-row" | ||
style={{ | ||
gap: '40px', | ||
}} | ||
> | ||
{isSticky && ( | ||
<Link | ||
className="h6 font-weight-bold" | ||
// # is removed from link so we have to use onclick to scroll to the top | ||
to="#" | ||
style={{ | ||
color: '#000000', | ||
fontFamily: 'Gotham Bold', | ||
padding: '7px 0px', | ||
}} | ||
onClick={e => { | ||
e.preventDefault(); | ||
window.scrollTo({ | ||
top: 0, | ||
behavior: 'smooth', | ||
}); | ||
}} | ||
> | ||
{title} | ||
</Link> | ||
)} | ||
<div | ||
className="d-flex flex-row" | ||
style={{ | ||
gap: '10px', | ||
}} | ||
> | ||
{sections.map(({ id, label }) => { | ||
const isInSection = currentSectionId === id; | ||
return ( | ||
<Link | ||
key={id} | ||
to={`#${id}`} | ||
className={`h6 ${ | ||
isInSection ? 'font-weight-bold' : 'font-weight-normal' | ||
}`} | ||
style={{ | ||
color: '#000000', | ||
borderColor: isInSection | ||
? linkUnderlineColor | ||
: 'transparent', | ||
borderBottomStyle: 'solid', | ||
borderBottomWidth: '4px', | ||
//fontFamily: isInSection ? 'Gotham Book' : 'Gotham Book', | ||
fontFamily: isInSection ? 'Gotham Bold' : 'Gotham Book', | ||
padding: '7px 0px', | ||
}} | ||
> | ||
{label} | ||
</Link> | ||
); | ||
})} | ||
</div> | ||
</nav> | ||
</Col> | ||
</Row> | ||
); | ||
} |
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