Sidebar scroll position resets on navigation, active item can be off-screen
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 2/5
- Thời gian dự kiến
- 1-3 giờ
- Mức phù hợp với người mới
- 86/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Công nghệ
- javascript, react
Hướng nghiên cứu
Bắt đầu tại components/Sidebar/index.jsx và kiểm tra hành vi hiện có của forwardRef và điều hướng. Tái hiện việc tải lại với một mục ở phía dưới của thanh bên, sau đó xác minh rằng liên kết đang hoạt động hiển thị sau khi điều hướng, trong khi chỉ vị trí cuộn bên trong của thanh bên thay đổi, không phải vị trí cuộn của cửa sổ chính.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- TypeScript
- Star
- 6.9k
- Fork
- 6.5k
- Merge trung bình
- 2 ngày 9 giờ
- Pull request đã merge (30 ngày)
- 29
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của nodejs/nodejs.org
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
nodejs/nodejs.org#9162 · 1 bình luận ·
-
web-agenda
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
nodejs/nodejs.org#9098 · 3 bình luận · 1 reaction ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 55/100
nodejs/nodejs.org#9166 ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 68/100
nodejs/nodejs.org#9140 · 10 bình luận · 3 reaction ·
-
Option to close side navigations Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 58/100
nodejs/nodejs.org#9099 · 3 bình luận · 1 reaction ·
Tất cả issue của nodejs/nodejs.org
Issue tương tự
-
Type/Bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
OpenNSW/nsw-srilanka#497 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
0xMiden/bridge-portal#132 ·
-
react-doctor severity:warning tech-debt
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
digidem/comapeo-cloud-app#403 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100