File tree Expand file tree Collapse file tree 2 files changed +40
-11
lines changed
Expand file tree Collapse file tree 2 files changed +40
-11
lines changed Original file line number Diff line number Diff line change 1- // src/main.tsx
2- import { createRoot } from 'react-dom/client'
3- import { BrowserRouter } from 'react-router-dom'
4- import App from './App.tsx'
5- import './index.css'
1+ import React from 'react' ;
2+ import { createRoot } from 'react-dom/client' ;
3+ import { BrowserRouter , useLocation } from 'react-router-dom' ;
4+ import App from './App' ;
5+ import './index.css' ;
6+
7+ /**
8+ * Scroll to the element pointed by the URL hash after each navigation.
9+ * If no hash is present (or not found), scroll to top.
10+ */
11+ function ScrollToHash ( ) {
12+ const { pathname, hash } = useLocation ( ) ;
13+
14+ React . useEffect ( ( ) => {
15+ // try to scroll to an in-page anchor first
16+ if ( hash ) {
17+ // decode in case ids contain encoded chars
18+ const selector = decodeURIComponent ( hash ) ;
19+ const el = document . querySelector ( selector ) ;
20+ if ( el ) {
21+ el . scrollIntoView ( { behavior : 'smooth' , block : 'start' } ) ;
22+ return ;
23+ }
24+ }
25+ // otherwise go to the top on route change
26+ window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
27+ } , [ pathname , hash ] ) ;
28+
29+ return null ;
30+ }
631
732createRoot ( document . getElementById ( 'root' ) ! ) . render (
8- < BrowserRouter basename = "/Endoshare" >
9- < App />
10- </ BrowserRouter >
11- )
33+ < React . StrictMode >
34+ < BrowserRouter basename = "/Endoshare" >
35+ < ScrollToHash />
36+ < App />
37+ </ BrowserRouter >
38+ </ React . StrictMode >
39+ ) ;
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ import Footer from "@/components/Footer";
33import { Button } from "@/components/ui/button" ;
44import { Badge } from "@/components/ui/badge" ;
55import { Link } from "react-router-dom" ;
6+ import { HashLink } from "react-router-hash-link" ;
67import { Home , Download } from "lucide-react" ;
78
89const Documentation = ( ) => {
@@ -27,10 +28,10 @@ const Documentation = () => {
2728 </ Link >
2829 </ Button >
2930 < Button asChild >
30- < a href = "#installation" className = "flex items-center gap-2" >
31+ < HashLink smooth to = "#installation" className = "flex items-center gap-2" >
3132 < Download className = "h-4 w-4" />
3233 Jump to Installation
33- </ a >
34+ </ HashLink >
3435 </ Button >
3536 </ div >
3637 </ section >
You can’t perform that action at this time.
0 commit comments