|
1 | | -import React from 'react' |
| 1 | +import React, { useState, useEffect } from 'react' |
2 | 2 | import { graphql } from 'gatsby' |
| 3 | +import styled from 'styled-components' |
3 | 4 | import Layout from '../../components/layout' |
4 | 5 | import Footer from '../../components/footer' |
5 | 6 | import ToC from '../../components/TableOfContents' |
| 7 | +import NavMenu from '../../components/NavMenu' |
6 | 8 | import TopicSection from '../../components/TopicSection' |
| 9 | +import { styles } from 'prism-react-renderer/themes/oceanicNext' |
| 10 | + |
| 11 | +// TODO: add styles |
| 12 | +const SecondaryNavBar = styled.nav` |
| 13 | +
|
| 14 | +` |
| 15 | +const NavMenuBtn = styled.button` |
| 16 | + position: fixed; |
| 17 | + top: 0; |
| 18 | + right: 0; |
| 19 | + z-index: 1; |
| 20 | + opacity: .7; |
| 21 | + height: 50px; |
| 22 | + /* width: 100%; */ |
| 23 | + background-color: rgb(255, 159, 128); |
| 24 | + text-align: center; |
| 25 | + border: none; |
| 26 | + &:hover { |
| 27 | + cursor: pointer; |
| 28 | + } |
| 29 | +` |
7 | 30 |
|
8 | 31 | export default function Index({ data }) { |
9 | | - console.log('Index data: ', data) |
10 | | - return ( |
11 | | - <> |
12 | | - <Layout> |
13 | | - <ToC data={data.allSanitySection.edges} /> |
| 32 | + // these "nav" references are for the secondary nav that appears once you've scrolled past table of contents |
| 33 | + const [navActive, setNavActive] = useState(false) |
| 34 | + const [navMenuActive, toggleNavMenu] = useState(false) |
| 35 | + // console.log('Index data: ', data) |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + window.addEventListener('scroll', handleScroll) |
| 39 | + return () => { |
| 40 | + window.removeEventListener('scroll', handleScroll) |
| 41 | + } |
| 42 | + }, []) |
| 43 | + |
| 44 | + function handleScroll() { |
| 45 | + console.log('handleScroll') |
| 46 | + // TODO: replace offset number with calculation that determines once ToC section has been passed |
| 47 | + if (window.pageYOffset > 900) { |
| 48 | + setNavActive(true) |
| 49 | + } else { |
| 50 | + setNavActive(false) |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function handleNavMenuToggle() { |
| 55 | + toggleNavMenu(navMenuActive ? false : true) |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <> |
| 60 | + <Layout> |
| 61 | + <ToC data={data.allSanitySection.edges} /> |
| 62 | + |
| 63 | + {data.allSanitySection.edges.map(section => { |
| 64 | + return ( |
| 65 | + <TopicSection |
| 66 | + section={section.node} |
| 67 | + key={section._id} |
| 68 | + /> |
| 69 | + ) |
| 70 | + })} |
| 71 | + </Layout> |
| 72 | + |
| 73 | + {navActive && ( |
| 74 | + <SecondaryNavBar> |
| 75 | + <NavMenuBtn |
| 76 | + id="navmenu_button" |
| 77 | + onClick={handleNavMenuToggle} |
| 78 | + > |
| 79 | + Toggle Menu |
| 80 | + </NavMenuBtn> |
| 81 | + </SecondaryNavBar> |
| 82 | + )} |
14 | 83 |
|
15 | | - {data.allSanitySection.edges.map(section => { |
16 | | - return ( |
17 | | - <TopicSection |
18 | | - section={section.node} |
19 | | - key={section._id} |
20 | | - /> |
21 | | - ) |
22 | | - })} |
23 | | - </Layout> |
| 84 | + {navMenuActive && ( |
| 85 | + <NavMenu data={data.allSanitySection.edges} /> |
| 86 | + )} |
24 | 87 |
|
25 | | - <Footer /> |
26 | | - </> |
27 | | - ) |
| 88 | + <Footer /> |
| 89 | + </> |
| 90 | + ) |
28 | 91 | } |
29 | 92 |
|
30 | 93 | export const query = graphql` |
|
0 commit comments