Skip to content

Commit e2241b6

Browse files
committed
fix(sidebar): allow dropdown sections to toggle open and closed on click
1 parent 50d6991 commit e2241b6

File tree

2 files changed

+32
-13
lines changed

2 files changed

+32
-13
lines changed

src/components/Layout/Sidebar/SidebarLink.tsx

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface SidebarLinkProps {
2222
isExpanded?: boolean;
2323
hideArrow?: boolean;
2424
isPending: boolean;
25+
onClick?: React.MouseEventHandler<HTMLDivElement>;
2526
}
2627

2728
export function SidebarLink({
@@ -51,13 +52,17 @@ export function SidebarLink({
5152
target = '_blank';
5253
}
5354
return (
54-
<Link
55-
href={href}
55+
<Link
56+
href={href}
57+
passHref
58+
legacyBehavior // required to manually render an <a> tag or custom wrapper
59+
>
60+
<div
5661
ref={ref}
62+
onClick={onClick}
5763
title={title}
58-
target={target}
59-
passHref
60-
aria-current={selected ? 'page' : undefined}
64+
role="link"
65+
tabIndex={0}
6166
className={cn(
6267
'p-2 pe-2 w-full rounded-none lg:rounded-e-2xl text-start hover:bg-gray-5 dark:hover:bg-gray-80 relative flex items-center justify-between',
6368
{
@@ -72,14 +77,15 @@ export function SidebarLink({
7277
'dark:bg-gray-70 bg-gray-3 dark:hover:bg-gray-70 hover:bg-gray-3':
7378
isPending,
7479
}
75-
)}>
76-
{/* This here needs to be refactored ofc */}
80+
)}
81+
>
7782
<div>
7883
{title}{' '}
7984
{version === 'major' && (
8085
<span
8186
title="- This feature is available in React 19 beta and the React canary channel"
82-
className={`text-xs px-1 ms-1 rounded bg-gray-10 dark:bg-gray-40 dark:bg-opacity-20 text-gray-40 dark:text-gray-40`}>
87+
className="text-xs px-1 ms-1 rounded bg-gray-10 dark:bg-gray-40 dark:bg-opacity-20 text-gray-40 dark:text-gray-40"
88+
>
8389
React 19
8490
</span>
8591
)}
@@ -102,10 +108,13 @@ export function SidebarLink({
102108
className={cn('pe-1', {
103109
'text-link dark:text-link-dark': isExpanded,
104110
'text-tertiary dark:text-tertiary-dark': !isExpanded,
105-
})}>
111+
})}
112+
>
106113
<IconNavArrow displayDirection={isExpanded ? 'down' : 'end'} />
107114
</span>
108115
)}
109-
</Link>
110-
);
116+
</div>
117+
</Link>
118+
);
119+
111120
}

src/components/Layout/Sidebar/SidebarRouteTree.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
* Copyright (c) Facebook, Inc. and its affiliates.
33
*/
44

5-
import {useRef, useLayoutEffect, Fragment} from 'react';
5+
import { useRef, useLayoutEffect, Fragment, useState} from 'react';
6+
67

78
import cn from 'classnames';
89
import {useRouter} from 'next/router';
@@ -112,7 +113,15 @@ export function SidebarRouteTree({
112113
const isBreadcrumb =
113114
breadcrumbs.length > 1 &&
114115
breadcrumbs[breadcrumbs.length - 1].path === path;
115-
const isExpanded = isForceExpanded || isBreadcrumb || selected;
116+
117+
const [isExpanded, setIsExpanded] = useState(
118+
isForceExpanded || isBreadcrumb || selected
119+
);
120+
121+
const toggleExpand = (e: React.MouseEvent) => {
122+
e.preventDefault();
123+
setIsExpanded(prev => !prev);
124+
};
116125
listItem = (
117126
<li key={`${title}-${path}-${level}-heading`}>
118127
<SidebarLink
@@ -125,6 +134,7 @@ export function SidebarRouteTree({
125134
version={version}
126135
isExpanded={isExpanded}
127136
hideArrow={isForceExpanded}
137+
onClick={toggleExpand}
128138
/>
129139
<CollapseWrapper duration={250} isExpanded={isExpanded}>
130140
<SidebarRouteTree

0 commit comments

Comments
 (0)