-
-
Notifications
You must be signed in to change notification settings - Fork 19
Fix #286: Add Nav::activateParents() that makes parent dropdown active when one of its child items is active
#302
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -62,6 +62,8 @@ final class Nav extends Widget | |
|
|
||
| private bool $activateItems = true; | ||
|
|
||
| private bool $activateParents = false; | ||
|
|
||
| private array $attributes = []; | ||
|
|
||
| private array $cssClasses = []; | ||
|
|
@@ -103,6 +105,29 @@ public function activateItems(bool $enabled): self | |
| return $new; | ||
| } | ||
|
|
||
| /** | ||
| * Whether to activate the parent dropdown item when one of its child items is active. | ||
| * | ||
| * When set to `true`, if a dropdown item's URL matches {@see currentPath}, the dropdown toggle (parent) will also | ||
| * receive the `active` CSS class. | ||
| * | ||
| * @param bool $enabled Whether to activate parent items. Defaults to `false`. | ||
| * | ||
| * @return self A new instance with the specified activate parents value. | ||
| * | ||
| * Example usage: | ||
| * ```php | ||
| * $nav->activateParents(true); | ||
| * ``` | ||
| */ | ||
| public function activateParents(bool $enabled): self | ||
| { | ||
| $new = clone $this; | ||
| $new->activateParents = $enabled; | ||
|
|
||
| return $new; | ||
| } | ||
|
|
||
| /** | ||
| * Adds a set of attributes. | ||
| * | ||
|
|
@@ -591,6 +616,24 @@ private function isTabsOrPills(): bool | |
| || in_array(NavStyle::PILLS, $this->styleClasses, true); | ||
| } | ||
|
|
||
| /** | ||
| * Checks whether any item in the dropdown is active. | ||
| * | ||
| * @param Dropdown $dropdown The dropdown to check. | ||
| * | ||
| * @return bool Whether any item in the dropdown is active. | ||
| */ | ||
| private function hasActiveDropdownItem(Dropdown $dropdown): bool | ||
| { | ||
| foreach ($dropdown->getItems() as $item) { | ||
| if ($item->isActive()) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Renders the items for the nav component. | ||
| * | ||
|
|
@@ -623,6 +666,11 @@ private function renderItems(): array | |
| private function renderItemsDropdown(Dropdown $items): Li | ||
| { | ||
| $dropDownItems = $this->isDropdownActive($items); | ||
| $togglerClasses = ['nav-link', 'dropdown-toggle']; | ||
|
|
||
| if ($this->activateParents && $this->hasActiveDropdownItem($dropDownItems)) { | ||
| $togglerClasses[] = self::NAV_LINK_ACTIVE_CLASS; | ||
| } | ||
|
Comment on lines
668
to
+673
|
||
|
|
||
| return Li::tag() | ||
| ->addClass(...$this->dropdownCssClasses, ...$dropDownItems->getCssClasses()) | ||
|
|
@@ -631,7 +679,7 @@ private function renderItemsDropdown(Dropdown $items): Li | |
| $dropDownItems | ||
| ->container(false) | ||
| ->togglerAsLink() | ||
| ->togglerClass('nav-link', 'dropdown-toggle') | ||
| ->togglerClass(...$togglerClasses) | ||
| ->render(), | ||
| "\n", | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The PHPDoc states “Defaults to
false” for$enabled, but the method signature doesn’t provide a default value. To avoid confusing API consumers, either remove the “Defaults…” wording (and rely on the property default) or change the signature to include an explicit default (if that’s intended).