Skip to content

Commit fdbc4e8

Browse files
committed
chore: fix sidebar autoexpand
1 parent b726c86 commit fdbc4e8

File tree

1 file changed

+12
-6
lines changed
  • src/components/pages/docs/sidebar/item

1 file changed

+12
-6
lines changed

src/components/pages/docs/sidebar/item/item.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import { useState } from 'react';
3+
import { useState, useEffect } from 'react';
44

55
import clsx from 'clsx';
66

@@ -37,11 +37,17 @@ const Item = ({
3737
// Normalize the pathname to remove the trailing slash
3838
const pathname = (usePathname() ?? '').replace(/\/$/, '');
3939
const hasActiveChild = isActiveItem(children, pathname);
40-
const [isOpen, setIsOpen] = useState(() => {
41-
return (
42-
url === pathname || hasActiveChild || (title && depth === 1 && expandedList?.includes(title))
43-
);
44-
});
40+
const shouldBeOpen =
41+
url === pathname || hasActiveChild || (title && depth === 1 && expandedList?.includes(title));
42+
43+
const [isOpen, setIsOpen] = useState(shouldBeOpen);
44+
45+
// Update open state when pathname changes (e.g., when coming from search)
46+
useEffect(() => {
47+
if (shouldBeOpen && !isOpen) {
48+
setIsOpen(true);
49+
}
50+
}, [pathname, shouldBeOpen, isOpen]);
4551

4652
const toggle = () => {
4753
if (closeMenu && url) {

0 commit comments

Comments
 (0)