nodejs / nodejs/nodejs.org

Sidebar scroll position resets on navigation, active item can be off-screen

Ouverte Adaptée aux débutants
#8,828 4 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Langage dominant
TypeScript
Étoiles
6.9k
Forks
6.5k
Merge moyen
2 j 8 h
PR mergées (30 j)
29

Description

I couldn't push my own branch to make a PR so sharing this as issue instead. The fix belongs in components/Sidebar/index.jsx.

Problem
Clicking an item in the left sidebar causes a full page reload because navigation uses window.location.href in components/Sidebar/index.jsx. After the reload, the sidebar’s internal scroll position resets to the top.

As a result, when a user clicks a page located lower in the sidebar list, such as an item under TypeScript or Diagnostics, the selected item may still be active but no longer visible. The user then has to scroll through the sidebar again to find their current location.

Reproduction steps

  • Open https://nodejs.org/learn
  • Scroll the left sidebar to a lower item, for example Diagnostics → Memory
  • Click the item
  • After the page reloads, the sidebar scroll position has returned to the top, and the active item is off-screen

Expected behavior

The active sidebar item should remain visible after navigation.

Proposed fix

When the sidebar mounts, locate the active link inside the sidebar <aside>.
If that link is outside the visible portion of the sidebar’s own scroll container, adjust aside.scrollTop so the active item is centered in view. This only affects the sidebar’s internal scroll position and does not change the main window scroll.

Example approach

useEffect(() => {
  const aside = asideRef.current;
  const active = aside?.querySelector(`a[href="${CSS.escape(pathname)}"]`);
  if (!aside || !active) return;

  const offsetTop =
    active.getBoundingClientRect().top -
    aside.getBoundingClientRect().top +
    aside.scrollTop;

  const viewTop = aside.scrollTop;
  const viewBottom = viewTop + aside.clientHeight;

  if (offsetTop >= viewTop && offsetTop + active.offsetHeight <= viewBottom) {
    return;
  }

  aside.scrollTop = Math.max(
    0,
    offsetTop - aside.clientHeight / 2 + active.offsetHeight / 2
  );
}, [pathname]);

Notes

This approach uses the existing forwardRef exposed by @node-core/ui-components/Containers/Sidebar, so no upstream or vendor changes are required.

Alternative considered

Persisting the sidebar’s scrollTop in sessionStorage across reloads was considered, but rejected. That approach adds more complexity, such as saving on unload, restoring at the right time during hydration, and handling stale scroll state when the sidebar structure changes.

Ensuring the active item is visible is simpler and aligns with expected user behavior in most cases.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez dans components/Sidebar/index.jsx et examinez le comportement existant de forwardRef et de la navigation. Reproduisez le rechargement avec un élément situé plus bas dans la barre latérale, puis vérifiez que le lien actif est visible après la navigation, tandis que seule la position de défilement interne de la barre latérale change, et non celle de la fenêtre principale.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
javascript, react
Domaine
frontend, web-dev
Type d'issue
Bug
Difficulté
2/5
Temps estimé
1-3 heures
Activité
Active
Clarté
Clairement spécifiée
Accessibilité débutants
86/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.