|
15 | 15 | from .sidebar_items.reference import api_reference |
16 | 16 | from .state import SideBarBase, SideBarItem, SidebarState |
17 | 17 |
|
| 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 | +""" |
18 | 45 |
|
19 | 46 | def sidebar_link(*children, **props): |
20 | 47 | """Create a sidebar link that closes the sidebar when clicked.""" |
@@ -628,6 +655,7 @@ def sidebar(url=None, width: str = "100%") -> rx.Component: |
628 | 655 | # |
629 | 656 | width=width, |
630 | 657 | ), |
| 658 | + on_mount=rx.call_script(Scrollable_SideBar), |
631 | 659 | class_name="flex justify-end w-full h-full", |
632 | 660 | ) |
633 | 661 |
|
|
0 commit comments