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

feat(v2): allow activeBaseTest in NavLink #2690

Merged
merged 10 commits into from
May 17, 2020
7 changes: 5 additions & 2 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import styles from './styles.module.css';

function NavLink({
activeBasePath,
activeBaseRegex,
to,
href,
label,
Expand All @@ -43,10 +44,12 @@ function NavLink({
isNavLink: true,
activeClassName,
to: toUrl,
...(activeBasePath
...(activeBasePath || activeBaseRegex
? {
isActive: (_match, location) =>
location.pathname.startsWith(activeBaseUrl),
activeBaseRegex
? new RegExp(activeBaseRegex).test(location.pathname)
: location.pathname.startsWith(activeBaseUrl),
}
: null),
})}
Expand Down
4 changes: 4 additions & 0 deletions website/docs/theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ module.exports = {
position: 'left', // or 'right'
// To apply the active class styling on all
// routes starting with this path.
// This usually isn't necessary
activeBasePath: 'docs',
activeBaseRegex: 'docs/(next|v8)', // as an alternative to activeBasePath if required
className: '', // Custom CSS class (for styling any item)
},
// ... other links
Expand All @@ -148,6 +150,8 @@ module.exports = {
};
```

React Router should automatically apply active link styling to links, but you can use `activeBasePath` in edge cases. For cases in which a link should be active on several different paths (such as when you have multiple doc folders under the same sidebar), you can use `activeBaseRegex`. `activeBaseRegex` is a more flexible alternative to `activeBasePath` and takes precedence over it -- Docusaurus parses it into a regular expression that is tested against the current URL.

Outbound links automatically get `target="_blank" rel="noopener noreferrer"` attributes.

### Navbar Dropdown
Expand Down