Skip to content

Commit 630fd7c

Browse files
authored
fix: navlink investigation (#16073)
Normally the react router Navlink should normalize the url path and match with or without a / on the end of the path. This is a fix to use useMatch() to explicitly see if the current path is a match to an href to determine whether to apply active styling to the navlink
1 parent 20c36a6 commit 630fd7c

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

site/src/components/Sidebar/Sidebar.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { CSSObject, Interpolation, Theme } from "@emotion/react";
33
import { Stack } from "components/Stack/Stack";
44
import { type ClassName, useClassName } from "hooks/useClassName";
55
import type { ElementType, FC, ReactNode } from "react";
6-
import { Link, NavLink } from "react-router-dom";
6+
import { Link, NavLink, useMatch } from "react-router-dom";
77
import { cn } from "utils/cn";
88

99
interface SidebarProps {
@@ -61,18 +61,19 @@ export const SettingsSidebarNavItem: FC<SettingsSidebarNavItemProps> = ({
6161
href,
6262
end,
6363
}) => {
64+
// useMatch is necessary to verify if the current path matches the href on the initial render of the route
65+
const matchResult = useMatch(href);
66+
6467
return (
6568
<NavLink
6669
end={end}
6770
to={href}
68-
className={({ isActive }) =>
69-
cn(
70-
"relative text-sm text-content-secondary no-underline font-medium py-2 px-3 hover:bg-surface-secondary rounded-md transition ease-in-out duration-150",
71-
{
72-
"font-semibold text-content-primary": isActive,
73-
},
74-
)
75-
}
71+
className={cn(
72+
"relative text-sm text-content-secondary no-underline font-medium py-2 px-3 hover:bg-surface-secondary rounded-md transition ease-in-out duration-150",
73+
{
74+
"font-semibold text-content-primary": matchResult !== null,
75+
},
76+
)}
7677
>
7778
{children}
7879
</NavLink>

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy