1
- import React , { useEffect , useState } from 'react' ;
1
+ import React , { useEffect , useRef , useState } from 'react' ;
2
2
import { useParams } from 'react-router-dom' ;
3
3
import ReactMarkdown from 'react-markdown' ;
4
4
import remarkGfm from 'remark-gfm' ;
@@ -13,6 +13,20 @@ const DocDetail = () => {
13
13
const [ loading , setLoading ] = useState ( true ) ;
14
14
const [ error , setError ] = useState ( null ) ;
15
15
const [ headings , setHeadings ] = useState ( [ ] ) ;
16
+ const tableRef = useRef ( null ) ;
17
+
18
+ useEffect ( ( ) => {
19
+ const handleScroll = ( ) => {
20
+ if ( tableRef . current ) {
21
+ const rect = tableRef . current . getBoundingClientRect ( ) ;
22
+ const isTableVisible = rect . top <= 0 && rect . bottom >= 100 ;
23
+ setIsSticky ( isTableVisible ) ;
24
+ }
25
+ } ;
26
+
27
+ window . addEventListener ( 'scroll' , handleScroll ) ;
28
+ return ( ) => window . removeEventListener ( 'scroll' , handleScroll ) ;
29
+ } , [ ] ) ;
16
30
17
31
useEffect ( ( ) => {
18
32
const fetchContent = async ( ) => {
@@ -52,8 +66,9 @@ const DocDetail = () => {
52
66
} , [ content ] ) ;
53
67
54
68
useEffect ( ( ) => {
55
- if ( window . location . hash ) {
56
- const id = window . location . hash . substring ( 1 ) ;
69
+ const hash = window . location . hash ;
70
+ if ( hash ) {
71
+ const id = hash . substring ( 1 ) ;
57
72
const element = document . getElementById ( id ) ;
58
73
if ( element ) {
59
74
element . scrollIntoView ( { behavior : 'smooth' } ) ;
@@ -66,17 +81,20 @@ const DocDetail = () => {
66
81
67
82
return (
68
83
< Layout >
69
- < section className = "container mx-auto p-4" >
84
+ < section className = "container mx-auto p-4 min-h-screen " >
70
85
< h3 className = "text-2xl md:text-3xl capitalize text-center my-10 mb-20 font-semibold" >
71
86
{ slug . replace ( / - / g, ' ' ) }
72
87
</ h3 >
73
- < div className = "flex sticky top-20 " >
74
- < aside className = "w-1/4 p-4" >
88
+ < div className = "flex" >
89
+ < aside ref = { tableRef } className = "sticky top-20 w-1/4 p-4 h-0 " >
75
90
< h2 className = "text-xl font-bold mb-2" > Table of Contents</ h2 >
76
91
< ul >
77
92
{ headings . map ( ( heading , index ) => (
78
93
< li key = { index } className = { `ml-${ heading . level } ` } >
79
- < a href = { `#${ heading . title . replace ( / \s + / g, '-' ) . toLowerCase ( ) } ` } >
94
+ < a href = { `#${ heading . title . replace ( / \s + / g, '-' ) . toLowerCase ( ) } ` }
95
+
96
+ onClick = { ( ) => console . log ( heading . title . replace ( / \s + / g, '-' ) . toLowerCase ( ) ) }
97
+ >
80
98
{ heading . title }
81
99
</ a >
82
100
</ li >
@@ -105,13 +123,13 @@ const DocDetail = () => {
105
123
) ;
106
124
} ,
107
125
h1 ( { node, children } ) {
108
- return < h1 className = 'text-xl font-normal mt-5 ' id = { children [ 0 ] . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > { children } </ h1 > ;
126
+ return < h1 className = 'text-xl font-normal mt-10 mb-3 ' id = { children . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > { children } </ h1 > ;
109
127
} ,
110
128
h2 ( { node, children } ) {
111
- return < h2 className = 'text-xl font-normal mt-5 ' id = { children [ 0 ] . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > 🌿 { children } </ h2 > ;
129
+ return < h2 className = 'text-xl font-normal mt-10 mb-3 ' id = { children . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > 🌿 { children } </ h2 > ;
112
130
} ,
113
131
h3 ( { node, children } ) {
114
- return < h3 className = 'text-xl font-normal mt-5 ' id = { children [ 0 ] . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > 🌿 { children } </ h3 > ;
132
+ return < h3 className = 'text-xl font-normal mt-10 mb-3 ' id = { children . toLowerCase ( ) . replace ( / \s + / g, '-' ) } > 🌿 { children } </ h3 > ;
115
133
} ,
116
134
// Handle other heading levels if needed
117
135
} }
0 commit comments