|
|
|
@ -1,5 +1,12 @@ |
|
|
|
|
import NextLink from "next/link"; |
|
|
|
|
import React, { ReactNode, ComponentType } from "react"; |
|
|
|
|
import React, { |
|
|
|
|
ReactNode, |
|
|
|
|
ComponentType, |
|
|
|
|
useState, |
|
|
|
|
useRef, |
|
|
|
|
useEffect, |
|
|
|
|
useCallback, |
|
|
|
|
} from "react"; |
|
|
|
|
|
|
|
|
|
import styles from "./OrganizedContent.module.css"; |
|
|
|
|
|
|
|
|
@ -17,6 +24,7 @@ interface Props { |
|
|
|
|
sections: Section[]; |
|
|
|
|
id: string; |
|
|
|
|
children: ReactNode; |
|
|
|
|
pageTitle: string; |
|
|
|
|
link: Link; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -24,8 +32,10 @@ export function OrganizedContent({ |
|
|
|
|
sections, |
|
|
|
|
id, |
|
|
|
|
children, |
|
|
|
|
pageTitle, |
|
|
|
|
link: Link, |
|
|
|
|
}: Props) { |
|
|
|
|
const [mobileNavOpen, setMobileNavOpen] = useState(false); |
|
|
|
|
const currentIndex = sections.findIndex( |
|
|
|
|
({ id: sectionId }) => sectionId === id |
|
|
|
|
); |
|
|
|
@ -36,10 +46,34 @@ export function OrganizedContent({ |
|
|
|
|
|
|
|
|
|
const section = sections[currentIndex]; |
|
|
|
|
const isReadAll = section.id === READ_ALL_ID; |
|
|
|
|
const ref = useRef<HTMLDivElement>(null); |
|
|
|
|
const isVisible = useOnScreen(ref.current); |
|
|
|
|
const burgerVisible = useBurger(isVisible); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
mobileNavOpen |
|
|
|
|
? (document.body.style.overflow = "hidden") |
|
|
|
|
: (document.body.style.overflow = "visible"); |
|
|
|
|
}, [mobileNavOpen]); |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<div className={styles.wrapper}> |
|
|
|
|
<Nav sections={sections} currentIndex={currentIndex} link={Link} /> |
|
|
|
|
<div className={styles.wrapper} ref={ref}> |
|
|
|
|
<div |
|
|
|
|
className={ |
|
|
|
|
mobileNavOpen |
|
|
|
|
? `${styles.navMobileBackground} ${styles.show}` |
|
|
|
|
: styles.navMobileBackground |
|
|
|
|
} |
|
|
|
|
onClick={() => setMobileNavOpen(false)} |
|
|
|
|
/> |
|
|
|
|
<Nav |
|
|
|
|
sections={sections} |
|
|
|
|
currentIndex={currentIndex} |
|
|
|
|
link={Link} |
|
|
|
|
pageTitle={pageTitle} |
|
|
|
|
mobileNavOpen={mobileNavOpen} |
|
|
|
|
setMobileNavOpen={setMobileNavOpen} |
|
|
|
|
/> |
|
|
|
|
<div className={styles.content}> |
|
|
|
|
{isReadAll ? ( |
|
|
|
|
children |
|
|
|
@ -57,6 +91,14 @@ export function OrganizedContent({ |
|
|
|
|
</> |
|
|
|
|
)} |
|
|
|
|
</div> |
|
|
|
|
<button |
|
|
|
|
className={`${styles.burger} ${ |
|
|
|
|
burgerVisible ? styles.burgerVisible : "" |
|
|
|
|
}`}
|
|
|
|
|
onClick={() => setMobileNavOpen(!mobileNavOpen)} |
|
|
|
|
> |
|
|
|
|
<Burger /> |
|
|
|
|
</button> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
@ -65,11 +107,29 @@ interface NavProps { |
|
|
|
|
sections: Section[]; |
|
|
|
|
currentIndex: number; |
|
|
|
|
link: Link; |
|
|
|
|
pageTitle: string; |
|
|
|
|
mobileNavOpen: boolean; |
|
|
|
|
setMobileNavOpen: (mobileNavOpen: boolean) => void; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function Nav({ sections, currentIndex, link: Link }: NavProps) { |
|
|
|
|
function Nav({ |
|
|
|
|
sections, |
|
|
|
|
currentIndex, |
|
|
|
|
link: Link, |
|
|
|
|
pageTitle, |
|
|
|
|
mobileNavOpen, |
|
|
|
|
setMobileNavOpen, |
|
|
|
|
}: NavProps) { |
|
|
|
|
const navStyles = mobileNavOpen |
|
|
|
|
? [styles.nav, styles.mobileNavOpen] |
|
|
|
|
: [styles.nav]; |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<nav className={styles.nav}> |
|
|
|
|
<nav |
|
|
|
|
className={navStyles.join(" ")} |
|
|
|
|
onClick={(event) => event.stopPropagation()} |
|
|
|
|
> |
|
|
|
|
<h1 className={styles.mobileNavTitle}>{pageTitle}</h1> |
|
|
|
|
{sections.map((section, index) => { |
|
|
|
|
const classNames = [styles.navItem]; |
|
|
|
|
|
|
|
|
@ -82,14 +142,17 @@ function Nav({ sections, currentIndex, link: Link }: NavProps) { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return ( |
|
|
|
|
<Link |
|
|
|
|
className={classNames.join(" ")} |
|
|
|
|
id={section.id} |
|
|
|
|
<div |
|
|
|
|
onClick={() => { |
|
|
|
|
setMobileNavOpen(false); |
|
|
|
|
}} |
|
|
|
|
key={section.id} |
|
|
|
|
> |
|
|
|
|
<span className={styles.marker} /> |
|
|
|
|
<div>{section.title}</div> |
|
|
|
|
</Link> |
|
|
|
|
<Link className={classNames.join(" ")} id={section.id}> |
|
|
|
|
<span className={styles.marker} /> |
|
|
|
|
<div>{section.title}</div> |
|
|
|
|
</Link> |
|
|
|
|
</div> |
|
|
|
|
); |
|
|
|
|
})} |
|
|
|
|
</nav> |
|
|
|
@ -137,6 +200,20 @@ function Footer({ sections, currentIndex, link: Link }: FooterProps) { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function useDebounce(func: () => void, delay = 300) { |
|
|
|
|
const timerRef = useRef<number | undefined>(undefined); |
|
|
|
|
return useCallback(() => { |
|
|
|
|
if (timerRef.current != null) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
timerRef.current = window.setTimeout(() => { |
|
|
|
|
func(); |
|
|
|
|
timerRef.current = undefined; |
|
|
|
|
}, delay); |
|
|
|
|
}, [func, delay]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export interface SectionWithContent { |
|
|
|
|
section: Section; |
|
|
|
|
Content: ComponentType; |
|
|
|
@ -216,3 +293,91 @@ function Arrow({ direction }: { direction: "left" | "right" }) { |
|
|
|
|
</svg> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function useOnScreen(element: HTMLDivElement | null) { |
|
|
|
|
const [isIntersecting, setIntersecting] = useState(false); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
const observer = new IntersectionObserver(([entry]) => |
|
|
|
|
setIntersecting(entry.isIntersecting) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
if (element) { |
|
|
|
|
observer.observe(element); |
|
|
|
|
} |
|
|
|
|
// Remove the observer as soon as the component is unmounted
|
|
|
|
|
return () => { |
|
|
|
|
observer.disconnect(); |
|
|
|
|
}; |
|
|
|
|
}, [element]); |
|
|
|
|
|
|
|
|
|
return isIntersecting; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function useBurger(componentIsVisible: boolean): boolean { |
|
|
|
|
const [prevScrollPos, setPrevScrollPos] = useState(0); |
|
|
|
|
const [burgerVisible, setBurgerVisible] = useState(true); |
|
|
|
|
|
|
|
|
|
const handleScroll = useDebounce(() => { |
|
|
|
|
// find current scroll position
|
|
|
|
|
const currentScrollPos = window.pageYOffset; |
|
|
|
|
setBurgerVisible( |
|
|
|
|
componentIsVisible && |
|
|
|
|
((prevScrollPos > currentScrollPos && |
|
|
|
|
prevScrollPos - currentScrollPos > 70) || |
|
|
|
|
currentScrollPos < 10) |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
// set state to new scroll position
|
|
|
|
|
setPrevScrollPos(currentScrollPos); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
window.addEventListener("scroll", handleScroll); |
|
|
|
|
|
|
|
|
|
return () => window.removeEventListener("scroll", handleScroll); |
|
|
|
|
}, [handleScroll]); |
|
|
|
|
|
|
|
|
|
return burgerVisible; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Inlining this svg because we want to fill in colors using css variables
|
|
|
|
|
function Burger() { |
|
|
|
|
return ( |
|
|
|
|
<svg |
|
|
|
|
width="30" |
|
|
|
|
height="23" |
|
|
|
|
viewBox="0 0 30 23" |
|
|
|
|
fill="none" |
|
|
|
|
xmlns="http://www.w3.org/2000/svg" |
|
|
|
|
> |
|
|
|
|
<line |
|
|
|
|
x1="28" |
|
|
|
|
y1="2" |
|
|
|
|
x2="2" |
|
|
|
|
y2="2" |
|
|
|
|
strokeWidth="4" |
|
|
|
|
strokeLinecap="round" |
|
|
|
|
strokeLinejoin="round" |
|
|
|
|
/> |
|
|
|
|
<line |
|
|
|
|
x1="28" |
|
|
|
|
y1="11.375" |
|
|
|
|
x2="2" |
|
|
|
|
y2="11.375" |
|
|
|
|
strokeWidth="4" |
|
|
|
|
strokeLinecap="round" |
|
|
|
|
strokeLinejoin="round" |
|
|
|
|
/> |
|
|
|
|
<line |
|
|
|
|
x1="28" |
|
|
|
|
y1="20.75" |
|
|
|
|
x2="2" |
|
|
|
|
y2="20.75" |
|
|
|
|
strokeWidth="4" |
|
|
|
|
strokeLinecap="round" |
|
|
|
|
strokeLinejoin="round" |
|
|
|
|
/> |
|
|
|
|
</svg> |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|