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 activeBasePath to be a RegExp #2683

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions packages/docusaurus-theme-classic/src/theme/Navbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ function NavLink({
...props
}) {
const toUrl = useBaseUrl(to);
const activeBaseUrl = useBaseUrl(activeBasePath);
const activeBaseUrl = useBaseUrl(activeBasePath.toString());
const activeBasePathIsRegex = typeof activeBasePath === 'object';
Copy link
Contributor

Choose a reason for hiding this comment

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

typeof activeBasePath === 'object';

Why not activeBasePath instanceof RegExp or something like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, there's a bug. For some reason, none of the methods I've used to check if activeBaseUrl is a Regular Expression work. It's almost like React Context breaks RegExps.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead, activeBasePath shows up (when it should be a RegExp) as an empty object.

Copy link
Contributor

Choose a reason for hiding this comment

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

But currently it looks rather unreliable, we need to find out what exactly is the matter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I almost think there's a bug, where React Context can't handle Regular Expressions correctly.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'll try to investigate this issue.


return (
<Link
Expand All @@ -45,8 +46,19 @@ function NavLink({
to: toUrl,
...(activeBasePath
? {
isActive: (_match, location) =>
location.pathname.startsWith(activeBaseUrl),
isActive: (_match, location) => {
if (activeBasePathIsRegex) {
return activeBasePath.test(location.pathname);
}

if (typeof activeBasePath === 'string') {
return location.pathname.startsWith(activeBaseUrl);
}

throw new Error(
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not use throw errors on invalid type checking, I think this should be removed for consistency.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, will do

'activeBasePath must be a string or Regular Expression',
);
},
}
: null),
})}
Expand Down
2 changes: 2 additions & 0 deletions website/docs/theme-classic.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ module.exports = {
position: 'left', // or 'right'
// To apply the active class styling on all
// routes starting with this path.
// This can also be a Regular Expression.
// If so, it will be tested against the current URL
activeBasePath: 'docs',
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to have a real-word example regex for this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍

className: '', // Custom CSS class (for styling any item)
},
Expand Down