From dfb50ca6bab339e3a283d5bcd18e1ca52e06dd8b Mon Sep 17 00:00:00 2001 From: Stevan Freeborn <65925598+StevanFreeborn@users.noreply.github.com> Date: Thu, 27 Apr 2023 16:03:27 -0500 Subject: [PATCH] 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. --- src/app/components/Main.tsx | 2 +- src/app/components/NavBar.tsx | 2 +- src/app/components/Page.tsx | 24 +++++++++++++-------- src/app/components/SectionDropdown.tsx | 12 +++++++---- src/app/components/SideBar.tsx | 13 +++++------- src/app/context/section.tsx | 29 ++++++++++++++++++++++++++ src/app/{ => context}/theme.tsx | 0 src/app/layout.tsx | 7 +++++-- src/app/utils/stringUtils.ts | 3 +++ 9 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 src/app/context/section.tsx rename src/app/{ => context}/theme.tsx (100%) create mode 100644 src/app/utils/stringUtils.ts 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 => (