Skip to content

Commit 230d9e5

Browse files
committed
docs: updated NavLink docs
1 parent 770ee11 commit 230d9e5

File tree

1 file changed

+94
-108
lines changed

1 file changed

+94
-108
lines changed

docs/components/nav-link.md

Lines changed: 94 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,134 +4,120 @@ title: NavLink
44

55
# `<NavLink>`
66

7-
<details>
8-
<summary>Type declaration</summary>
7+
A `<NavLink>` is a special kind of `<Link>` that knows whether or not it is "active" or "pending". This is useful when building a navigation menu, such as a breadcrumb or a set of tabs where you'd like to show which of them is currently selected. It also provides useful context for assistive technology like screen readers.
98

109
```tsx
11-
declare function NavLink(
12-
props: NavLinkProps
13-
): React.ReactElement;
14-
15-
interface NavLinkProps
16-
extends Omit<
17-
LinkProps,
18-
"className" | "style" | "children"
19-
> {
20-
caseSensitive?: boolean;
21-
children?:
22-
| React.ReactNode
23-
| ((props: { isActive: boolean }) => React.ReactNode);
24-
className?:
25-
| string
26-
| ((props: {
27-
isActive: boolean;
28-
}) => string | undefined);
29-
end?: boolean;
30-
style?:
31-
| React.CSSProperties
32-
| ((props: {
33-
isActive: boolean;
34-
}) => React.CSSProperties);
35-
}
36-
```
10+
import { NavLink } from "react-router-dom";
3711

38-
</details>
12+
<NavLink
13+
to="/messages"
14+
className={({ isActive, isPending }) =>
15+
isPending ? "pending" : isActive ? "active" : ""
16+
}
17+
>
18+
Messages
19+
</NavLink>;
20+
```
3921

40-
A `<NavLink>` is a special kind of [`<Link>`][link] that knows whether or not it is "active". This is useful when building a navigation menu such as a breadcrumb or a set of tabs where you'd like to show which of them is currently selected. It also provides useful context for assistive technology like screen readers.
22+
## Default `active` class
4123

42-
By default, an `active` class is added to a `<NavLink>` component when it is active. This provides the same simple styling mechanism for most users who are upgrading from v5. One difference as of `v6.0.0-beta.3` is that `activeClassName` and `activeStyle` have been removed from `NavLinkProps`. Instead, you can pass a function to either `style` or `className` that will allow you to customize the inline styling or the class string based on the component's active state. You can also pass a function as children to customize the content of the `<NavLink>` component based on their active state, specially useful to change styles on internal elements.
24+
By default, an `active` class is added to a `<NavLink>` component when it is active so you can use CSS to style it.
4325

4426
```tsx
45-
import * as React from "react";
46-
import { NavLink } from "react-router-dom";
27+
<nav id="sidebar">
28+
<NavLink to="/messages" />
29+
</nav>
30+
```
4731

48-
function NavList() {
49-
// This styling will be applied to a <NavLink> when the
50-
// route that it links to is currently selected.
51-
let activeStyle = {
52-
textDecoration: "underline",
53-
};
54-
55-
let activeClassName = "underline";
56-
57-
return (
58-
<nav>
59-
<ul>
60-
<li>
61-
<NavLink
62-
to="messages"
63-
style={({ isActive }) =>
64-
isActive ? activeStyle : undefined
65-
}
66-
>
67-
Messages
68-
</NavLink>
69-
</li>
70-
<li>
71-
<NavLink
72-
to="tasks"
73-
className={({ isActive }) =>
74-
isActive ? activeClassName : undefined
75-
}
76-
>
77-
Tasks
78-
</NavLink>
79-
</li>
80-
<li>
81-
<NavLink to="tasks">
82-
{({ isActive }) => (
83-
<span
84-
className={
85-
isActive ? activeClassName : undefined
86-
}
87-
>
88-
Tasks
89-
</span>
90-
)}
91-
</NavLink>
92-
</li>
93-
</ul>
94-
</nav>
95-
);
32+
```css
33+
#sidebar a.active {
34+
color: red;
9635
}
9736
```
9837

99-
If you prefer the v5 API, you can create your own `<NavLink />` as a wrapper component:
38+
## `className`
39+
40+
The `className` prop works like a normal className, but you can also pass it a function to customize the classNames applied based on the active and pending state of the link.
10041

10142
```tsx
102-
import * as React from "react";
103-
import { NavLink as BaseNavLink } from "react-router-dom";
104-
105-
const NavLink = React.forwardRef(
106-
({ activeClassName, activeStyle, ...props }, ref) => {
107-
return (
108-
<BaseNavLink
109-
ref={ref}
110-
{...props}
111-
className={({ isActive }) =>
112-
[
113-
props.className,
114-
isActive ? activeClassName : null,
115-
]
116-
.filter(Boolean)
117-
.join(" ")
118-
}
119-
style={({ isActive }) => ({
120-
...props.style,
121-
...(isActive ? activeStyle : null),
122-
})}
123-
/>
124-
);
43+
<NavLink
44+
to="/messages"
45+
className={({ isActive, isPending }) =>
46+
isPending ? "pending" : isActive ? "active" : ""
12547
}
126-
);
48+
>
49+
Messages
50+
</NavLink>
51+
```
52+
53+
## `style`
54+
55+
The `style` prop works like a normal style prop, but you can also pass it a function to customize the styles applied based on the active and pending state of the link.
56+
57+
```tsx
58+
<NavLink
59+
to="/messages"
60+
style={({ isActive, isPending }) => {
61+
return {
62+
fontWeight: isActive ? "bold" : "",
63+
color: isPending ? "red" : "black",
64+
};
65+
}}
66+
>
67+
Messages
68+
</NavLink>
69+
```
70+
71+
## `children`
72+
73+
You can pass a render prop as children to customize the content of the `<NavLink>` based on the active and pending state, which is useful to change styles on internal elements.
74+
75+
```tsx
76+
<NavLink to="/tasks">
77+
{({ isActive, isPending }) => (
78+
<span className={isActive ? "active" : ""}>Tasks</span>
79+
)}
80+
</NavLink>
12781
```
12882

129-
If the `end` prop is used, it will ensure this component isn't matched as "active" when its descendant paths are matched. For example, to render a link that is only active at the website root and not any other URLs, you can use:
83+
## `end`
84+
85+
The `end` prop changes the matching logic for the `active` and `pending` states to only match to the "end" of the NavLinks's `to` path. If the URL is longer than `to`, it will no longer be considered active.
86+
87+
Without the end prop, this link is always active because every URL matches `/`.
88+
89+
```tsx
90+
<NavLink to="/">Home</NavLink>
91+
```
92+
93+
To match the URL "to the end" of `to`, use `end`:
13094

13195
```tsx
13296
<NavLink to="/" end>
13397
Home
13498
</NavLink>
13599
```
136100

137-
[link]: ./link
101+
Now this link will only be active at `"/"`. This works for paths with more segments as well:
102+
103+
| Link | URL | isActive |
104+
| ----------------------------- | ------------ | -------- |
105+
| `<NavLink to="/tasks" />` | `/tasks` | true |
106+
| `<NavLink to="/tasks" />` | `/tasks/123` | true |
107+
| `<NavLink to="/tasks" end />` | `/tasks` | true |
108+
| `<NavLink to="/tasks" end />` | `/tasks/123` | false |
109+
110+
## `caseSensitive`
111+
112+
Adding the `caseSensitive` prop changes the matching logic to make it case sensitive.
113+
114+
| Link | URL | isActive |
115+
| -------------------------------------------- | ------------- | -------- |
116+
| `<NavLink to="/SpOnGe-bOB" />` | `/sponge-bob` | true |
117+
| `<NavLink to="/SpOnGe-bOB" caseSensitive />` | `/sponge-bob` | false |
118+
119+
## `aria-current`
120+
121+
When a `NavLink` is active it will automatically apply `<a aria-current="page">` to the underlying anchor tag. See [aria-current][aria-current] on MDN.
122+
123+
[aria-current]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current

0 commit comments

Comments
 (0)