From 793531210d0ef0635ac076d9aee1d31d3ee82655 Mon Sep 17 00:00:00 2001 From: Ryan Florence Date: Wed, 8 Mar 2023 08:04:15 -0700 Subject: [PATCH] docs: updated NavLink docs --- docs/components/nav-link.md | 154 ++++++++++++++++++++++++------------ 1 file changed, 104 insertions(+), 50 deletions(-) diff --git a/docs/components/nav-link.md b/docs/components/nav-link.md index 4b01edc80ab..24fa75e3d8a 100644 --- a/docs/components/nav-link.md +++ b/docs/components/nav-link.md @@ -5,66 +5,120 @@ toc: false # `` -A `` is a special kind of `` 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. - -By default, an `active` class is added to a `` component when it is active. You can pass a function as children to customize the content of the `` component based on their active state, specially useful to change styles on internal elements. +A `` is a special kind of `` 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. ```tsx import { NavLink } from "@remix-run/react"; -function NavList() { - // This styling will be applied to a when the - // route that it links to is currently selected. - const activeStyle = { - textDecoration: "underline", - }; - const activeClassName = "underline"; - return ( - - ); + + isPending ? "pending" : isActive ? "active" : "" + } +> + Messages +; +``` + +## Default `active` class + +By default, an `active` class is added to a `` component when it is active so you can use CSS to style it. + +```tsx + +``` + +```css +#sidebar a.active { + color: red; } ``` -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: +## `className` + +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. + +```tsx + + isPending ? "pending" : isActive ? "active" : "" + } +> + Messages + +``` + +## `style` + +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. + +```tsx + { + return { + fontWeight: isActive ? "bold" : "", + color: isPending ? "red" : "black", + }; + }} +> + Messages + +``` + +## `children` + +You can pass a render prop as children to customize the content of the `` based on the active and pending state, which is useful to change styles on internal elements. + +```tsx + + {({ isActive, isPending }) => ( + Tasks + )} + +``` + +## `end` + +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. + +Without the end prop, this link is always active because every URL matches `/`. + +```tsx +Home +``` + +To match the URL "to the end" of `to`, use `end`: ```tsx Home ``` + +Now this link will only be active at `"/"`. This works for paths with more segments as well: + +| Link | URL | isActive | +| ----------------------------- | ------------ | -------- | +| `` | `/tasks` | true | +| `` | `/tasks/123` | true | +| `` | `/tasks` | true | +| `` | `/tasks/123` | false | + +## `caseSensitive` + +Adding the `caseSensitive` prop changes the matching logic to make it case sensitive. + +| Link | URL | isActive | +| -------------------------------------------- | ------------- | -------- | +| `` | `/sponge-bob` | true | +| `` | `/sponge-bob` | false | + +## `aria-current` + +When a `NavLink` is active it will automatically apply `` to the underlying anchor tag. See [aria-current][aria-current] on MDN. + +[aria-current]: https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Attributes/aria-current