Skip to content

Commit 046239f

Browse files
authored
fix: revert navlink back to prior approach (#9497)
* fix: revert navlink back to prior approach * add changeset
1 parent e50b937 commit 046239f

File tree

3 files changed

+84
-16
lines changed

3 files changed

+84
-16
lines changed

.changeset/unlucky-hats-play.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"react-router-dom": patch
3+
---
4+
5+
Fix NavLink behavior for root urls

packages/react-router-dom/__tests__/nav-link-active-test.tsx

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,57 @@ describe("NavLink", () => {
288288

289289
expect(anchors.map((a) => a.props.className)).toEqual(["active", ""]);
290290
});
291+
292+
it("does not automatically apply to root non-layout segments", () => {
293+
let renderer: TestRenderer.ReactTestRenderer;
294+
TestRenderer.act(() => {
295+
renderer = TestRenderer.create(
296+
<MemoryRouter initialEntries={["/home"]}>
297+
<Routes>
298+
<Route index element={<h1>Root</h1>} />
299+
<Route
300+
path="home"
301+
element={<NavLink to="/">Root</NavLink>}
302+
></Route>
303+
</Routes>
304+
</MemoryRouter>
305+
);
306+
});
307+
308+
let anchor = renderer.root.findByType("a");
309+
310+
expect(anchor.props.className).not.toMatch("active");
311+
});
312+
313+
it("does not automatically apply to root layout segments", () => {
314+
let renderer: TestRenderer.ReactTestRenderer;
315+
TestRenderer.act(() => {
316+
renderer = TestRenderer.create(
317+
<MemoryRouter initialEntries={["/home"]}>
318+
<Routes>
319+
<Route
320+
path="/"
321+
element={
322+
<>
323+
<h1>Root</h1>
324+
<Outlet />
325+
</>
326+
}
327+
>
328+
<Route
329+
path="home"
330+
element={<NavLink to="/">Root</NavLink>}
331+
></Route>
332+
</Route>
333+
</Routes>
334+
</MemoryRouter>
335+
);
336+
});
337+
338+
let anchor = renderer.root.findByType("a");
339+
340+
expect(anchor.props.className).not.toMatch("active");
341+
});
291342
});
292343

293344
describe("when it matches just the beginning but not to the end", () => {

packages/react-router-dom/index.tsx

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,24 +442,36 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
442442
ref
443443
) {
444444
let path = useResolvedPath(to, { relative: rest.relative });
445-
let match = useMatch({ path: path.pathname, end, caseSensitive });
446-
445+
let location = useLocation();
447446
let routerState = React.useContext(DataRouterStateContext);
448-
let nextLocation = routerState?.navigation.location;
449-
let nextPath = useResolvedPath(nextLocation || "");
450-
let nextMatch = React.useMemo(
451-
() =>
452-
nextLocation
453-
? matchPath(
454-
{ path: path.pathname, end, caseSensitive },
455-
nextPath.pathname
456-
)
457-
: null,
458-
[nextLocation, path.pathname, caseSensitive, end, nextPath.pathname]
459-
);
460447

461-
let isPending = nextMatch != null;
462-
let isActive = match != null;
448+
let toPathname = path.pathname;
449+
let locationPathname = location.pathname;
450+
let nextLocationPathname =
451+
routerState && routerState.navigation && routerState.navigation.location
452+
? routerState.navigation.location.pathname
453+
: null;
454+
455+
if (!caseSensitive) {
456+
locationPathname = locationPathname.toLowerCase();
457+
nextLocationPathname = nextLocationPathname
458+
? nextLocationPathname.toLowerCase()
459+
: null;
460+
toPathname = toPathname.toLowerCase();
461+
}
462+
463+
let isActive =
464+
locationPathname === toPathname ||
465+
(!end &&
466+
locationPathname.startsWith(toPathname) &&
467+
locationPathname.charAt(toPathname.length) === "/");
468+
469+
let isPending =
470+
nextLocationPathname != null &&
471+
(nextLocationPathname === toPathname ||
472+
(!end &&
473+
nextLocationPathname.startsWith(toPathname) &&
474+
nextLocationPathname.charAt(toPathname.length) === "/"));
463475

464476
let ariaCurrent = isActive ? ariaCurrentProp : undefined;
465477

0 commit comments

Comments
 (0)