Current page not detected properly by menu
stancl opened this issue · comments
@stancl just to confirm, what are the complete URL paths of your 'Configuration' and 'Middleware Configuration' pages?
@stancl thanks for your patience, this was tricky 😂
TL;DR - in the isActive
helper on line 34 of your config.php
, make the following change:
- return ends_with(trimPath($page->getPath()), trimPath($path));
+ return ends_with(trimPath($page->getPath()), trimPath($page->version() . '/' . $path));
What's happening here is that isActive
assumes it's checking the entire path to the current page. In the documentation starter template, it is—we specify the complete path for every page in navigation.php
:
'Getting Started' => [
'url' => 'docs/getting-started',
'children' => [
'Customizing Your Site' => 'docs/customizing-your-site',
'Navigation' => 'docs/navigation',
// ...
With this setup, isActive
is going to check if docs/middleware-configuration
ends with docs/configuration
, which it doesn't, so everything works.
This involves a fair bit of repetition in path names, because we have to write out docs/
over and over. You've quite thoughtfully hoisted this up into top level v1
and v2
keys in your navigation, and you add the version back into each page's URL in the link
helper, which means for every page you only have to specify the last part of the slug:
'v1' => [
'Getting Started' => [
'url' => 'getting-started',
'children' => [
'Installation' => 'installation',
'Storage Drivers' => 'storage-drivers',
// ...
The default isActive
helper, however, naively still thinks it's getting the complete path, so on your site it checks if middleware-configuration
ends with configuration
, which it does, and that's why you get two active menu items.
One solution to this of course would be adding the version back to every path in your navigation.php
. But, since you've also got a handy version
helper set up, it's quicker to just add the version back to the path dynamically right in the isActive
helper.
Ah, thanks a lot for the detailed answer @bakerkretzmar :)