Skip to content

Commit 22c5c29

Browse files
authored
ENG-6187: automatic scrolling during navigation (#1423)
* automatic scrolling during navigation * remove smooth scrolling
1 parent 7482568 commit 22c5c29

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

pcweb/components/docpage/sidebar/sidebar.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,33 @@
1515
from .sidebar_items.reference import api_reference
1616
from .state import SideBarBase, SideBarItem, SidebarState
1717

18+
Scrollable_SideBar = """
19+
function scrollToActiveSidebarLink() {
20+
const currentPath = window.location.pathname.replace(/\\/+$|\\/$/g, "") + "/";
21+
22+
const activeLink = document.querySelector(`a[href="${currentPath}"]`) ||
23+
document.querySelector(`a[href="${currentPath.slice(0, -1)}"]`);
24+
25+
if (activeLink) {
26+
activeLink.scrollIntoView({
27+
block: "center",
28+
});
29+
}
30+
}
31+
32+
setTimeout(scrollToActiveSidebarLink, 100);
33+
34+
window.addEventListener("popstate", () => {
35+
setTimeout(scrollToActiveSidebarLink, 100);
36+
});
37+
38+
document.addEventListener('click', (e) => {
39+
const link = e.target.closest('a[href^="/docs"]');
40+
if (link) {
41+
setTimeout(scrollToActiveSidebarLink, 200);
42+
}
43+
});
44+
"""
1845

1946
def sidebar_link(*children, **props):
2047
"""Create a sidebar link that closes the sidebar when clicked."""
@@ -628,6 +655,7 @@ def sidebar(url=None, width: str = "100%") -> rx.Component:
628655
#
629656
width=width,
630657
),
658+
on_mount=rx.call_script(Scrollable_SideBar),
631659
class_name="flex justify-end w-full h-full",
632660
)
633661

0 commit comments

Comments
 (0)