NavLink
Navigation link
Polymorphic
Source
LLM docs
Docs
PackageIcon
Usage
Active
Set the active prop to add active styles to NavLink.
Note that if you're using a React Router NavLink inside renderRoot, the active styles will be based on the
aria-current attribute that's set by React Router,
so you won't need to explicitly set the active prop.
You can customize active styles with the color and variant props:
autoContrast
NavLink supports the autoContrast prop and theme.autoContrast. If autoContrast is set either on NavLink or on the theme, the content color will be adjusted to have sufficient contrast with the value specified in the color prop.
Note that the autoContrast feature works only if you use the color prop to change the background color. autoContrast works only with the filled variant.
Nested NavLinks
To create nested links, put NavLink as children of another NavLink:
Polymorphic component
NavLink is a polymorphic component – its default root element is a, but it can be changed to any other element or component with the component prop:
You can also use components in the component prop, for example, Next.js Link:
Polymorphic components with TypeScript
Note that polymorphic component prop types are different from regular components – they do not extend HTML element props of the default element. For example,
NavLinkPropsdoes not extendReact.ComponentProps'<'div'>'althoughais the default element.If you want to create a wrapper for a polymorphic component that is not polymorphic (does not support the
componentprop), then your component props interface should extend HTML element props, for example:If you want your component to remain polymorphic after wrapping, use the
polymorphicfunction described in this guide.