1- import { useEffect , useState } from "react"
21import Link from "next/link"
2+ import colors from "../../styles/colors"
33import styles from "./SideMenu.module.css"
44import typographyStyles from "../../styles/typography.module.css"
5- import colors from "../../styles/colors "
5+ import { useRouter } from "next/router "
66import { Pages } from "../../types/types"
77
88function Menu ( { pages = [ ] } : { pages : Pages } ) {
9- const [ activeSection , setActiveSection ] = useState < string > ( "" )
10-
11- useEffect ( ( ) => {
12- const handleScroll = ( ) => {
13- const sections = pages . flatMap ( ( page ) =>
14- [ page , ...( page . pages || [ ] ) ] . map ( ( p ) => ( {
15- id : p . pathname . replace ( "#" , "" ) ,
16- top :
17- document . getElementById ( p . pathname . replace ( "#" , "" ) ) ?. offsetTop ||
18- 0 ,
19- } ) )
20- )
21-
22- const currentSection = sections . reduce ( ( acc , section ) => {
23- const { id, top } = section
24- if ( window . scrollY >= top - 100 ) {
25- console . log ( id )
26- return id
27- }
28- return acc
29- } , "" )
30-
31- setActiveSection ( currentSection )
32- }
33-
34- window . addEventListener ( "scroll" , handleScroll )
35-
36- handleScroll ( )
37-
38- return ( ) => window . removeEventListener ( "scroll" , handleScroll )
39- } , [ pages ] )
40-
41- const scrollToSection = ( sectionId : string , event : React . MouseEvent ) => {
42- event . preventDefault ( )
43- const element = document . getElementById ( sectionId . replace ( "#" , "" ) )
44- if ( element ) {
45- element . scrollIntoView ( { behavior : "smooth" } )
46- }
47- }
9+ const router = useRouter ( )
10+ const { asPath : pathname } = router
4811
4912 return (
5013 < aside className = { styles . menu } >
@@ -63,57 +26,41 @@ function Menu({ pages = [] }: { pages: Pages }) {
6326
6427 < ul className = "scrollArea" >
6528 { pages . map ( ( page ) => {
66- const isActive = activeSection === page . pathname . replace ( "#" , "" )
67- const hasActiveChild = page . pages ?. some (
68- ( subPage ) => activeSection === subPage . pathname . replace ( "#" , "" )
69- )
29+ const isActive = pathname === page . pathname
7030
7131 return (
7232 < li
7333 key = { page . pathname }
74- className = { `${ styles . menuItem } ${
75- isActive || hasActiveChild ? styles . activeParent : ""
76- } `}
34+ className = { styles . menuItem }
7735 style = { {
7836 display : page ?. pages ? "block" : "flex" ,
7937 } }
8038 >
81- < code aria-hidden className = { styles . code } >
82- { `</>` }
83- </ code >
84- < a
85- href = { page . pathname }
39+ < code aria-hidden className = { styles . code } > { `</>` } </ code >
40+ < Link
8641 className = { isActive ? styles . isActive : "" }
87- onClick = { ( e ) => scrollToSection ( page . pathname , e ) }
42+ href = { page . pathname }
8843 >
8944 { page . name }
90- </ a >
45+ </ Link >
9146
9247 { page ?. pages && (
9348 < ul >
94- { page . pages . map ( ( subPage ) => {
95- const isSubActive =
96- activeSection === subPage . pathname . replace ( "#" , "" )
49+ { page . pages . map ( ( page ) => {
50+ const isActive = pathname === page . pathname
9751
9852 return (
99- < li
100- key = { subPage . pathname }
101- className = { `${ styles . menuItem } ${
102- isSubActive ? styles . activeItem : ""
103- } `}
104- >
105- < code aria-hidden className = { styles . code } >
106- { `</>` }
107- </ code >
108- < a
109- href = { subPage . pathname }
110- className = { isSubActive ? styles . isActive : "" }
111- onClick = { ( e ) =>
112- scrollToSection ( subPage . pathname , e )
113- }
53+ < li key = { page . pathname } className = { styles . menuItem } >
54+ < code
55+ aria-hidden
56+ className = { styles . code }
57+ > { `</>` } </ code > { " " }
58+ < Link
59+ className = { isActive ? styles . isActive : "" }
60+ href = { page . pathname }
11461 >
115- { subPage . name }
116- </ a >
62+ { page . name }
63+ </ Link >
11764 </ li >
11865 )
11966 } ) }
0 commit comments