Skip to content

Commit 18bc637

Browse files
Lorenzo ArboitLorenzo Arboit
authored andcommitted
fixed url docs
1 parent 25749fc commit 18bc637

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/main.tsx

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
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

732
createRoot(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+
);

src/pages/Documentation.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Footer from "@/components/Footer";
33
import { Button } from "@/components/ui/button";
44
import { Badge } from "@/components/ui/badge";
55
import { Link } from "react-router-dom";
6+
import { HashLink } from "react-router-hash-link";
67
import { Home, Download } from "lucide-react";
78

89
const 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>

0 commit comments

Comments
 (0)