1
- import { useEffect , useState } from "react"
2
1
import Link from "next/link"
2
+ import colors from "../../styles/colors"
3
3
import styles from "./SideMenu.module.css"
4
4
import typographyStyles from "../../styles/typography.module.css"
5
- import colors from "../../styles/colors "
5
+ import { useRouter } from "next/router "
6
6
import { Pages } from "../../types/types"
7
7
8
8
function 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
48
11
49
12
return (
50
13
< aside className = { styles . menu } >
@@ -63,57 +26,41 @@ function Menu({ pages = [] }: { pages: Pages }) {
63
26
64
27
< ul className = "scrollArea" >
65
28
{ 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
70
30
71
31
return (
72
32
< li
73
33
key = { page . pathname }
74
- className = { `${ styles . menuItem } ${
75
- isActive || hasActiveChild ? styles . activeParent : ""
76
- } `}
34
+ className = { styles . menuItem }
77
35
style = { {
78
36
display : page ?. pages ? "block" : "flex" ,
79
37
} }
80
38
>
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
86
41
className = { isActive ? styles . isActive : "" }
87
- onClick = { ( e ) => scrollToSection ( page . pathname , e ) }
42
+ href = { page . pathname }
88
43
>
89
44
{ page . name }
90
- </ a >
45
+ </ Link >
91
46
92
47
{ page ?. pages && (
93
48
< 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
97
51
98
52
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 }
114
61
>
115
- { subPage . name }
116
- </ a >
62
+ { page . name }
63
+ </ Link >
117
64
</ li >
118
65
)
119
66
} ) }
0 commit comments