Skip to content

Commit 0e164eb

Browse files
fix(sidebar): scroll to the active item after navigating (#4211)
Co-authored-by: Brandy Smith <[email protected]>
1 parent 9c06008 commit 0e164eb

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

src/theme/DocSidebar/index.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,28 @@
33
*
44
* Reason for modifying:
55
* - Add a logo to the top of the sidebar
6+
* - Scroll to the active item in the sidebar
67
*/
78

8-
import React from 'react';
9+
import React, { useEffect } from 'react';
10+
import { useLocation } from '@docusaurus/router';
911
import DocSidebar from '@theme-original/DocSidebar';
1012
import type { Props } from '@theme/DocSidebar';
1113

1214
import Logo from '@theme-original/Logo';
1315

1416
export default function DocSidebarWrapper(props: Props): JSX.Element {
17+
const location = useLocation();
18+
19+
useEffect(() => {
20+
setTimeout(() => {
21+
const activeItem = document.querySelector('.menu__link--active');
22+
if (activeItem && activeItem.scrollIntoView) {
23+
activeItem.scrollIntoView({ block: 'center', behavior: 'auto' });
24+
}
25+
}, 100);
26+
}, [location.pathname]);
27+
1528
return (
1629
<>
1730
<Logo />

0 commit comments

Comments
 (0)