-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
||
return ( | ||
<Link | ||
|
@@ -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( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), | ||
})} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
className: '', // Custom CSS class (for styling any item) | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not
activeBasePath instanceof RegExp
or something like that?There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.