From 7696cdd4e15895fd1d363242c4853d79d3991c69 Mon Sep 17 00:00:00 2001 From: Brandy Smith <6577830+brandyscarney@users.noreply.github.com> Date: Tue, 22 Jul 2025 14:47:08 -0400 Subject: [PATCH] fix(sidebar): scroll to the active item after navigating --- src/theme/DocSidebar/index.tsx | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/theme/DocSidebar/index.tsx b/src/theme/DocSidebar/index.tsx index c99f9e85c83..5267a996e0e 100644 --- a/src/theme/DocSidebar/index.tsx +++ b/src/theme/DocSidebar/index.tsx @@ -3,15 +3,28 @@ * * Reason for modifying: * - Add a logo to the top of the sidebar + * - Scroll to the active item in the sidebar */ -import React from 'react'; +import React, { useEffect } from 'react'; +import { useLocation } from '@docusaurus/router'; import DocSidebar from '@theme-original/DocSidebar'; import type { Props } from '@theme/DocSidebar'; import Logo from '@theme-original/Logo'; export default function DocSidebarWrapper(props: Props): JSX.Element { + const location = useLocation(); + + useEffect(() => { + setTimeout(() => { + const activeItem = document.querySelector('.menu__link--active'); + if (activeItem && activeItem.scrollIntoView) { + activeItem.scrollIntoView({ block: 'center', behavior: 'auto' }); + } + }, 100); + }, [location.pathname]); + return ( <>