From 36a9db648478bef8aaff2d95a4abfffc8a1bab4a Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 13:42:45 -0400 Subject: [PATCH 1/4] Add support for custom toggler classes in `Dropdown` widget. --- src/Dropdown.php | 3 +-- tests/NavTest.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/Dropdown.php b/src/Dropdown.php index 1c3c7026..fafa3d3b 100644 --- a/src/Dropdown.php +++ b/src/Dropdown.php @@ -936,7 +936,6 @@ private function renderToggler(string|null $togglerId): string $togglerAttributes = $this->togglerAttributes; $togglerClasses = $this->togglerClasses; - $classes = $togglerAttributes['class'] ?? null; unset($togglerAttributes['class']); @@ -953,7 +952,7 @@ private function renderToggler(string|null $togglerId): string $classes, ], ), - default => Html::addCssClass($togglerAttributes, $togglerClasses), + default => Html::addCssClass($togglerAttributes, [...$togglerClasses, $classes]), }; if ($this->togglerLink) { diff --git a/tests/NavTest.php b/tests/NavTest.php index 6c301057..7feb5d79 100644 --- a/tests/NavTest.php +++ b/tests/NavTest.php @@ -1475,4 +1475,61 @@ public function testWorkingWithFlexUtilities(): void ->render(), ); } + + public function testDropdownAddTogglerClass(): void + { + Assert::equalsWithoutLE( + << + + + + + + HTML, + Nav::widget() + ->items( + NavLink::to('Active', '#', active: true), + Dropdown::widget() + ->addTogglerClass('test-class') + ->items( + DropdownItem::link('Action', '#'), + DropdownItem::link('Another action', '#'), + DropdownItem::link('Something else here', '#'), + DropdownItem::divider(), + DropdownItem::link('Separated link', '#'), + ) + ->togglerContent('Dropdown'), + NavLink::to('Link', url: '#'), + NavLink::to('Disabled', '#', disabled: true), + ) + ->styles(NavStyle::TABS) + ->render(), + ); + } } From 2d2919a6a5dda12f718f81e9be5338bb3c00512d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Fri, 1 Aug 2025 13:55:15 -0400 Subject: [PATCH 2/4] Add dropdown CSS class support to `Nav` widget and corresponding tests. --- src/Nav.php | 28 ++++++++++++++++++++++- tests/NavTest.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/src/Nav.php b/src/Nav.php index b899155d..7d6e0098 100644 --- a/src/Nav.php +++ b/src/Nav.php @@ -58,6 +58,7 @@ final class Nav extends Widget private array $attributes = []; private array $cssClasses = []; private string $currentPath = ''; + private array $dropdownCssClasses = [self::NAV_ITEM_DROPDOWN_CLASS]; private bool $fade = true; private bool|string $id = false; /** @var array */ @@ -158,6 +159,31 @@ public function addCssStyle(array|string $style, bool $overwrite = true): self return $new; } + /** + * Adds one or more CSS classes to the existing classes for dropdown. + * + * Multiple classes can be added by passing them as separate arguments. `null` values are filtered out + * automatically. + * + * @param BackedEnum|string|null ...$class One or more CSS class names to add. Pass `null` to skip adding a class. + * + * @return self A new instance with the specified CSS classes added to existing ones for dropdown. + * + * @link https://html.spec.whatwg.org/#classes + * + * Example usage: + * ```php + * $nav->addDropdownClass('custom-class', null, 'another-class', BackGroundColor::PRIMARY); + * ``` + */ + public function addDropdownClass(BackedEnum|string|null ...$class): self + { + $new = clone $this; + $new->dropdownCssClasses = [...$this->dropdownCssClasses, ...$class]; + + return $new; + } + /** * Sets attribute value. * @@ -581,7 +607,7 @@ private function renderItemsDropdown(Dropdown $items): Li $dropDownItems = $this->isDropdownActive($items); return Li::tag() - ->addClass(self::NAV_ITEM_DROPDOWN_CLASS) + ->addClass(...$this->dropdownCssClasses) ->addContent( "\n", $dropDownItems diff --git a/tests/NavTest.php b/tests/NavTest.php index 7feb5d79..840357d0 100644 --- a/tests/NavTest.php +++ b/tests/NavTest.php @@ -1532,4 +1532,61 @@ public function testDropdownAddTogglerClass(): void ->render(), ); } + + public function testDropdownAddClass(): void + { + Assert::equalsWithoutLE( + << + + + + + + HTML, + Nav::widget() + ->addDropdownClass('test-class') + ->items( + NavLink::to('Active', '#', active: true), + Dropdown::widget() + ->items( + DropdownItem::link('Action', '#'), + DropdownItem::link('Another action', '#'), + DropdownItem::link('Something else here', '#'), + DropdownItem::divider(), + DropdownItem::link('Separated link', '#'), + ) + ->togglerContent('Dropdown'), + NavLink::to('Link', url: '#'), + NavLink::to('Disabled', '#', disabled: true), + ) + ->styles(NavStyle::TABS) + ->render(), + ); + } } From b4375aa61d707c39cb7f0d8e16a6e0188d021610 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Sat, 2 Aug 2025 14:42:12 -0400 Subject: [PATCH 3/4] Add CSS class retrieval method to `Dropdown` and enhance `Nav` widget to include dropdown item classes. --- src/Dropdown.php | 10 ++++++++ src/Nav.php | 2 +- tests/NavTest.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 2 deletions(-) diff --git a/src/Dropdown.php b/src/Dropdown.php index fafa3d3b..a0b7a924 100644 --- a/src/Dropdown.php +++ b/src/Dropdown.php @@ -386,6 +386,16 @@ public function direction(DropdownDirection $direction): self return $new; } + /** + * Returns the CSS classes for the container. + * + * @return array The CSS classes for the container. + */ + public function getCssClasses(): array + { + return $this->cssClasses; + } + /** * Returns the list of links to appear in the dropdown. * diff --git a/src/Nav.php b/src/Nav.php index 7d6e0098..fe143c40 100644 --- a/src/Nav.php +++ b/src/Nav.php @@ -607,7 +607,7 @@ private function renderItemsDropdown(Dropdown $items): Li $dropDownItems = $this->isDropdownActive($items); return Li::tag() - ->addClass(...$this->dropdownCssClasses) + ->addClass(...$this->dropdownCssClasses, ...$dropDownItems->getCssClasses()) ->addContent( "\n", $dropDownItems diff --git a/tests/NavTest.php b/tests/NavTest.php index 840357d0..6a6c3c10 100644 --- a/tests/NavTest.php +++ b/tests/NavTest.php @@ -1533,7 +1533,7 @@ public function testDropdownAddTogglerClass(): void ); } - public function testDropdownAddClass(): void + public function testDropdownAddClassForAllItemsDropdown(): void { Assert::equalsWithoutLE( <<render(), ); } + + public function testDropdownAddClassForIndividualItemDropdown(): void + { + Assert::equalsWithoutLE( + << + + + + + + HTML, + Nav::widget() + ->items( + NavLink::to('Active', '#', active: true), + Dropdown::widget() + ->addClass('test-class') + ->items( + DropdownItem::link('Action', '#'), + DropdownItem::link('Another action', '#'), + DropdownItem::link('Something else here', '#'), + DropdownItem::divider(), + DropdownItem::link('Separated link', '#'), + ) + ->togglerContent('Dropdown'), + NavLink::to('Link', url: '#'), + NavLink::to('Disabled', '#', disabled: true), + ) + ->styles(NavStyle::TABS) + ->render(), + ); + } } From 4cf272382d543c2b00ebcf84bc62e1c58a7ffa0b Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Mon, 11 Aug 2025 09:17:41 -0400 Subject: [PATCH 4/4] Add line to `CHANGELOG.md`. --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 441c2faa..0bf8698b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,8 @@ ## 1.0.1 under development - Enh #287: Allow `Dropdown::togglerVariant()` to be `null`, avoiding it setting a variant class (@Mister-42) +- Bug #288: Add support for custom togglerClasses in `Dropdown` widget, and `addDropdownClass()` method (@terabytesoftw) ## 1.0.0 April 13, 2025 -- Initial release \ No newline at end of file +- Initial release