@@ -2,7 +2,7 @@ import React, { useState, useEffect } from 'react'
2
2
import { graphql } from 'gatsby'
3
3
import styled from 'styled-components'
4
4
import Layout from '../../components/layout'
5
- import Footer from '../../components/footer '
5
+ import Footer from '../../components/Footer '
6
6
import ToC from '../../components/TableOfContents'
7
7
import NavMenu from '../../components/NavMenu'
8
8
import TopicSection from '../../components/TopicSection'
@@ -33,17 +33,58 @@ export default function Index({ data }) {
33
33
// these "nav" references are for the secondary nav that appears once you've scrolled past table of contents
34
34
const [ navIsActive , setNavIsActive ] = useState ( false )
35
35
const [ navMenuIsActive , setNavMenuIsActive ] = useState ( false )
36
- // console.log('Index data: ', data )
36
+ const [ activeSection , setActiveSection ] = useState ( null )
37
37
38
38
useEffect ( ( ) => {
39
39
window . addEventListener ( 'scroll' , handleScroll )
40
+ window . addEventListener ( 'scroll' , handleObserver )
40
41
return ( ) => {
41
42
window . removeEventListener ( 'scroll' , handleScroll )
43
+ // TODO: this should be here, right?
44
+ window . addEventListener ( 'scroll' , handleObserver )
42
45
}
43
46
} , [ ] )
44
47
48
+ function handleObserver ( ) {
49
+ const options = { rootMargin : '-30px 0px -50% 0px' } ;
50
+ const observer = new IntersectionObserver ( checkIntersection , options )
51
+
52
+ const topicSections = document . querySelectorAll ( 'section' )
53
+ topicSections . forEach ( section => {
54
+ observer . observe ( section )
55
+ } )
56
+
57
+ function checkIntersection ( entries ) {
58
+ entries . forEach ( entry => {
59
+ if ( entry . isIntersecting ) {
60
+ intersectionHandler ( entry )
61
+ }
62
+ } )
63
+ }
64
+
65
+ function intersectionHandler ( entry ) {
66
+ // console.log('intersectionHandler entry: ', entry)
67
+ const id = entry . target . id
68
+ setActiveSection ( id )
69
+ const currentlyActive = document . querySelector ( '#navmenu_list .current-section' )
70
+ // console.log('currentlyActive: ', currentlyActive)
71
+ const shouldBeActive = document . querySelector ( `#navmenu_list a[href='#${ id } ']` )
72
+ // console.log('shouldBeActive: ', shouldBeActive)
73
+ if ( currentlyActive ) {
74
+ currentlyActive . classList . remove ( 'current-section' )
75
+ // console.log('currentlyActive if: ', currentlyActive)
76
+ }
77
+ if ( shouldBeActive ) {
78
+ shouldBeActive . classList . add ( 'current-section' )
79
+ // console.log('shouldBeActive if: ', shouldBeActive)
80
+ }
81
+ }
82
+ }
83
+
45
84
function handleScroll ( ) {
46
85
console . log ( 'handleScroll' )
86
+ // TODO: is this all neccessary for cross-browser issues?
87
+ // setDocScrollTop(Math.max(document.documentElement.scrollTop, document.body.scrollTop))
47
88
// TODO: replace offset number with calculation that determines once ToC section has been passed
48
89
if ( window . pageYOffset > 900 ) {
49
90
setNavIsActive ( true )
@@ -56,9 +97,9 @@ export default function Index({ data }) {
56
97
function handleNavMenuToggle ( ) {
57
98
setNavMenuIsActive ( navMenuIsActive ? false : true )
58
99
}
59
-
100
+
60
101
return (
61
- < >
102
+ < div >
62
103
< Layout id = "top-of-page" >
63
104
< ToC data = { data . allSanitySection . edges } />
64
105
@@ -84,13 +125,15 @@ export default function Index({ data }) {
84
125
< NavMenu
85
126
data = { data . allSanitySection . edges }
86
127
handleNavMenuToggle = { handleNavMenuToggle }
128
+ activeSection = { activeSection }
129
+ id = "navmenu_list"
87
130
/>
88
131
) }
89
132
</ SecondaryNavBar >
90
133
) }
91
134
92
135
< Footer />
93
- </ >
136
+ </ div >
94
137
)
95
138
}
96
139
0 commit comments