diff --git a/src/app/components/Main.tsx b/src/app/components/Main.tsx index 6f97a05..d2e00aa 100644 --- a/src/app/components/Main.tsx +++ b/src/app/components/Main.tsx @@ -1,7 +1,7 @@ 'use client'; import { Fragment } from 'react'; -import { themes, useThemeContext } from '../theme'; +import { themes, useThemeContext } from '../context/theme'; import { DocSection } from '../types/types'; import styles from './Main.module.css'; import Section from './Section'; diff --git a/src/app/components/NavBar.tsx b/src/app/components/NavBar.tsx index b728ed0..a22c71a 100644 --- a/src/app/components/NavBar.tsx +++ b/src/app/components/NavBar.tsx @@ -2,7 +2,7 @@ import Link from 'next/link'; import { useState } from 'react'; -import { themes, useThemeContext } from '../theme'; +import { themes, useThemeContext } from '../context/theme'; import { DocsStructure } from '../types/types'; import styles from './NavBar.module.css'; import SectionDropdown from './SectionDropdown'; diff --git a/src/app/components/Page.tsx b/src/app/components/Page.tsx index 1ce34c2..98408e3 100644 --- a/src/app/components/Page.tsx +++ b/src/app/components/Page.tsx @@ -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(null); - const [currentSection, setCurrentSection] = useState(''); + + const { setSection } = useSectionContext(); useEffect(() => { const titles = document.querySelectorAll( - '.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 (
-
+
diff --git a/src/app/components/SectionDropdown.tsx b/src/app/components/SectionDropdown.tsx index e31e155..83c430d 100644 --- a/src/app/components/SectionDropdown.tsx +++ b/src/app/components/SectionDropdown.tsx @@ -1,5 +1,7 @@ import { Fragment } from 'react'; +import { useSectionContext } from '../context/section'; import { Doc, DocsStructure } from '../types/types'; +import { toKebabCase } from '../utils/stringUtils'; import styles from './SectionDropdown.module.css'; function SectionDropdownOption({ doc }: { doc: Doc }) { @@ -9,6 +11,7 @@ function SectionDropdownOption({ doc }: { doc: Doc }) { @@ -17,6 +20,7 @@ function SectionDropdownOption({ doc }: { doc: Doc }) { {doc.children.map(child => (