From 5e94c7b824ebca81c9a4f9c591992be8a7386e77 Mon Sep 17 00:00:00 2001 From: Rishabh Date: Wed, 27 May 2026 09:52:14 +0530 Subject: [PATCH] fix: scroll sidebar to active item on navigation Replaced scroll position preservation with scroll-to-active-item behavior. When navigating via breadcrumbs or other non-sidebar links, the sidebar now scrolls the active item into view if it's outside the visible area. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../chronicle/src/themes/default/Layout.tsx | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/packages/chronicle/src/themes/default/Layout.tsx b/packages/chronicle/src/themes/default/Layout.tsx index e10654e..ca84daf 100644 --- a/packages/chronicle/src/themes/default/Layout.tsx +++ b/packages/chronicle/src/themes/default/Layout.tsx @@ -62,8 +62,6 @@ function renderConfigIcon( return {alt}; } -let savedScrollTop = 0; - export function Layout({ children, config, @@ -97,22 +95,20 @@ export function Layout({ ); useEffect(() => { - const el = scrollRef.current; - if (!el) return; - const onScroll = () => { - savedScrollTop = el.scrollTop; - }; - el.addEventListener('scroll', onScroll); - return () => el.removeEventListener('scroll', onScroll); - }, []); - - useEffect(() => { - const el = scrollRef.current; - if (el) - requestAnimationFrame(() => { - el.scrollTop = savedScrollTop; - }); + const timer = setTimeout(() => { + const container = document.querySelector(`.${styles.sidebarMain}`); + if (!container) return; + const allActive = container.querySelectorAll('[data-active="true"]'); + const activeItem = allActive[allActive.length - 1]; + if (!activeItem) return; + const containerRect = container.getBoundingClientRect(); + const itemRect = activeItem.getBoundingClientRect(); + if (itemRect.top < containerRect.top || itemRect.bottom > containerRect.bottom) { + container.scrollTop += itemRect.top - containerRect.top - containerRect.height / 2 + itemRect.height / 2; + } + }, 100); setMobileSidebarOpen(false); + return () => clearTimeout(timer); }, [pathname]); return ( @@ -436,6 +432,7 @@ function ApiSidebarNode({ item, pathname }: { item: Node; pathname: string }) { align='center' gap={3} className={`${styles.apiItem} ${isActive ? styles.apiItemActive : ''}`} + data-active={isActive} render={} > {item.name}