tfrere HF Staff commited on
Commit
e409b8e
·
1 Parent(s): 3a6d7d7

fix(router): use query param instead of hash for anchor scrolling

Browse files

HashRouter already uses # for routing, so we can't use hash anchors.
Use ?scrollTo=assembly-video query param instead.
Example: /#/getting-started?scrollTo=assembly-video

Files changed (1) hide show
  1. src/App.jsx +9 -6
src/App.jsx CHANGED
@@ -16,20 +16,23 @@ function ScrollToTop() {
16
  const location = useLocation();
17
 
18
  useEffect(() => {
19
- // Si il y a un hash (ancre), scroll vers cet élément
20
- if (location.hash) {
21
- // Petit délai pour laisser le DOM se charger
 
 
 
22
  setTimeout(() => {
23
- const element = document.getElementById(location.hash.slice(1));
24
  if (element) {
25
  element.scrollIntoView({ behavior: 'smooth', block: 'center' });
26
  }
27
  }, 100);
28
  } else {
29
- // Sinon, scroll en haut de la page
30
  window.scrollTo({ top: 0, behavior: 'smooth' });
31
  }
32
- }, [location.pathname, location.hash]);
33
 
34
  return null;
35
  }
 
16
  const location = useLocation();
17
 
18
  useEffect(() => {
19
+ // Check for scrollTo query parameter (used for anchor-like behavior with HashRouter)
20
+ const params = new URLSearchParams(location.search);
21
+ const scrollTo = params.get('scrollTo');
22
+
23
+ if (scrollTo) {
24
+ // Small delay to let the DOM render
25
  setTimeout(() => {
26
+ const element = document.getElementById(scrollTo);
27
  if (element) {
28
  element.scrollIntoView({ behavior: 'smooth', block: 'center' });
29
  }
30
  }, 100);
31
  } else {
32
+ // Otherwise scroll to top
33
  window.scrollTo({ top: 0, behavior: 'smooth' });
34
  }
35
+ }, [location.pathname, location.search]);
36
 
37
  return null;
38
  }