Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: revert navlink back to prior approach #9497

Merged
merged 2 commits into from Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/unlucky-hats-play.md
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Fix NavLink behavior for root urls
51 changes: 51 additions & 0 deletions packages/react-router-dom/__tests__/nav-link-active-test.tsx
Expand Up @@ -288,6 +288,57 @@ describe("NavLink", () => {

expect(anchors.map((a) => a.props.className)).toEqual(["active", ""]);
});

it("does not automatically apply to root non-layout segments", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route index element={<h1>Root</h1>} />
<Route
path="home"
element={<NavLink to="/">Root</NavLink>}
></Route>
</Routes>
</MemoryRouter>
);
});

let anchor = renderer.root.findByType("a");

expect(anchor.props.className).not.toMatch("active");
});

it("does not automatically apply to root layout segments", () => {
let renderer: TestRenderer.ReactTestRenderer;
TestRenderer.act(() => {
renderer = TestRenderer.create(
<MemoryRouter initialEntries={["/home"]}>
<Routes>
<Route
path="/"
element={
<>
<h1>Root</h1>
<Outlet />
</>
}
>
<Route
path="home"
element={<NavLink to="/">Root</NavLink>}
></Route>
</Route>
</Routes>
</MemoryRouter>
);
});

let anchor = renderer.root.findByType("a");

expect(anchor.props.className).not.toMatch("active");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mjackson Wanted to get your thoughts on the behavior of <NavLink to="/">. In 6.3 and before it seems like it was impossible to get that to apply as a partial match (end=false). Assuming that's intentional (since / is a weird case and matches everything), then this change reverts back to the previous logic from 6.3 and updates it for isPending.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks right to me. A <NavLink to="/"> should not be active at /home.

});
});

describe("when it matches just the beginning but not to the end", () => {
Expand Down
44 changes: 28 additions & 16 deletions packages/react-router-dom/index.tsx
Expand Up @@ -442,24 +442,36 @@ export const NavLink = React.forwardRef<HTMLAnchorElement, NavLinkProps>(
ref
) {
let path = useResolvedPath(to, { relative: rest.relative });
let match = useMatch({ path: path.pathname, end, caseSensitive });

let location = useLocation();
let routerState = React.useContext(DataRouterStateContext);
let nextLocation = routerState?.navigation.location;
let nextPath = useResolvedPath(nextLocation || "");
let nextMatch = React.useMemo(
() =>
nextLocation
? matchPath(
{ path: path.pathname, end, caseSensitive },
nextPath.pathname
)
: null,
[nextLocation, path.pathname, caseSensitive, end, nextPath.pathname]
);

let isPending = nextMatch != null;
let isActive = match != null;
let toPathname = path.pathname;
let locationPathname = location.pathname;
let nextLocationPathname =
routerState && routerState.navigation && routerState.navigation.location
? routerState.navigation.location.pathname
: null;

if (!caseSensitive) {
locationPathname = locationPathname.toLowerCase();
nextLocationPathname = nextLocationPathname
? nextLocationPathname.toLowerCase()
: null;
toPathname = toPathname.toLowerCase();
}

let isActive =
locationPathname === toPathname ||
(!end &&
locationPathname.startsWith(toPathname) &&
locationPathname.charAt(toPathname.length) === "/");

let isPending =
nextLocationPathname != null &&
(nextLocationPathname === toPathname ||
(!end &&
nextLocationPathname.startsWith(toPathname) &&
nextLocationPathname.charAt(toPathname.length) === "/"));

let ariaCurrent = isActive ? ariaCurrentProp : undefined;

Expand Down