Feat: Sync Navbar Section Dropdown

Added section context that can be set when section heading
scrolls into browser viewport and it's value can be used
to set the current value of the navbar dropdown.

Added kebab case utility method for converting title to element
id.
This commit is contained in:
Stevan Freeborn
2023-04-27 16:03:27 -05:00
parent 7cb9cf24f7
commit dfb50ca6ba
9 changed files with 67 additions and 25 deletions
+15 -9
View File
@@ -1,7 +1,8 @@
'use client';
import { useEffect, useRef, useState } from 'react';
import { themes, useThemeContext } from '../theme';
import { useEffect } from 'react';
import { useSectionContext } from '../context/section';
import { themes, useThemeContext } from '../context/theme';
import { DocSection, DocsStructure } from '../types/types';
import Main from './Main';
import NavBar from './NavBar';
@@ -17,26 +18,31 @@ export default function Page({
}) {
const { theme } = useThemeContext();
const themeStyles = themes[theme];
const docRef = useRef<HTMLDivElement>(null);
const [currentSection, setCurrentSection] = useState('');
const { setSection } = useSectionContext();
useEffect(() => {
const titles =
document.querySelectorAll<HTMLHeadingElement>(
'.section-title'
'article:nth-child(1) > h1'
);
const observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
setCurrentSection(entry.target.id);
setSection(entry.target.id);
history.pushState(
null,
'',
`#${entry.target.id}`
);
}
});
},
{
root: null,
rootMargin: '-10% 0% -80% 0%',
rootMargin: '-10% 0% -70% 0%',
threshold: 1,
}
);
@@ -46,12 +52,12 @@ export default function Page({
return () => {
titles.forEach(title => observer.unobserve(title));
};
}, []);
}, [setSection]);
return (
<div className={styles.wrapper} style={themeStyles}>
<SideBar version={version} />
<div ref={docRef} className={styles.container}>
<div className={styles.container}>
<NavBar version={version} />
<Main versionSections={versionSections} />
</div>