Files
onspring-api-docs/src/app/components/Page.tsx
T

67 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-04-20 23:36:52 -05:00
'use client';
2023-04-27 16:03:27 -05:00
import { useEffect } from 'react';
import { useSectionContext } from '../context/section';
import { themes, useThemeContext } from '../context/theme';
2023-04-20 23:36:52 -05:00
import { DocSection, DocsStructure } from '../types/types';
import Main from './Main';
import NavBar from './NavBar';
import styles from './Page.module.css';
import SideBar from './SideBar';
export default function Page({
versionDocStructure,
2023-04-20 23:36:52 -05:00
versionSections,
}: {
versionDocStructure: DocsStructure;
2023-04-20 23:36:52 -05:00
versionSections: DocSection[];
}) {
const { theme } = useThemeContext();
const themeStyles = themes[theme];
2023-04-27 16:03:27 -05:00
const { setSection } = useSectionContext();
2023-04-27 07:54:36 -05:00
useEffect(() => {
const titles =
document.querySelectorAll<HTMLHeadingElement>(
2023-04-27 16:03:27 -05:00
'article:nth-child(1) > h1'
2023-04-27 07:54:36 -05:00
);
const observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
if (entry.isIntersecting) {
2023-04-27 16:03:27 -05:00
setSection(entry.target.id);
history.pushState(
null,
'',
`#${entry.target.id}`
);
2023-04-27 07:54:36 -05:00
}
});
},
{
root: null,
2023-04-27 16:03:27 -05:00
rootMargin: '-10% 0% -70% 0%',
2023-04-27 07:54:36 -05:00
threshold: 1,
}
);
titles.forEach(title => observer.observe(title));
return () => {
titles.forEach(title => observer.unobserve(title));
};
2023-04-27 16:03:27 -05:00
}, [setSection]);
2023-04-20 23:36:52 -05:00
return (
<div className={styles.wrapper} style={themeStyles}>
<SideBar versionDocStructure={versionDocStructure} />
2023-04-27 16:03:27 -05:00
<div className={styles.container}>
<NavBar versionDocStructure={versionDocStructure} />
2023-04-20 23:36:52 -05:00
<Main versionSections={versionSections} />
</div>
</div>
);
}