Skip to content
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.0', '8.1', '8.2']
['8.0', '8.1', '8.2', '8.3']
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.1.1 under development

- Chg #86: Raise required `yiisoft/view` version to `^10.0` (@vjik)
- Enh #94: Enhance icon rendering in `Menu` widget to support custom tag (@terabytesoftw)

## 2.1.0 February 19, 2023

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"rector/rector": "^2.0.3",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.22",
"vimeo/psalm": "^4.30|^5.22|^6.4.1",
"yiisoft/psr-dummy-provider": "^1.0",
"yiisoft/test-support": "^3.0"
},
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
ensureOverrideAttribute="false"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
strictBinaryOperands="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
3 changes: 2 additions & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function render(): string
throw new RuntimeException('You must assign the "id" using the "id()" setter.');
}

$block = ob_get_clean();
$content = ob_get_clean();
$block = $content === false ? '' : $content;

if ($this->renderInPlace) {
return $block;
Expand Down
20 changes: 17 additions & 3 deletions src/Helper/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\I;
use Yiisoft\Html\Tag\Span;

final class Normalizer
Expand Down Expand Up @@ -106,13 +105,20 @@ public static function renderLabel(
): string {
$html = '';

$tagName = self::iconTagName($iconAttributes);

unset($iconAttributes['tag']);

if ($iconClass !== '') {
Html::addCssClass($iconAttributes, $iconClass);
}

if ($icon !== '' || $iconAttributes !== [] || $iconClass !== '') {
$i = I::tag()->attributes($iconAttributes)->content($icon);
$html = Span::tag()->attributes($iconContainerAttributes)->content($i)->encode(false)->render();
$html = Html::tag($tagName)->attributes($iconAttributes)->content($icon)->render();

if ($tagName === 'i') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wrapping usage that depends from tag name is very unexpected.

This package contains common framework-agnostic widgets, so result should be defined clearly. Use container or not, should be defined clearly and not depend from tag name.

@terabytesoftw terabytesoftw May 10, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

The change is about the handling of icons, which are usually wrapped in a container, while SVGs, for example, are not wrapped, just like an image. But i understand the proposal, i will improve it.

$html = Span::tag()->attributes($iconContainerAttributes)->content($html)->encode(false)->render();
}
}

if ($label !== '') {
Expand Down Expand Up @@ -170,6 +176,14 @@ private static function iconContainerAttributes(array $item, array $iconContaine
? $item['iconContainerAttributes'] : $iconContainerAttributes;
}

/**
* @psalm-return non-empty-string
*/
private static function iconTagName(array $item): string
{
return array_key_exists('tag', $item) && is_string($item['tag']) && $item['tag'] !== '' ? $item['tag'] : 'i';
}

/**
* Checks whether a menu item is active.
*
Expand Down
1 change: 1 addition & 0 deletions src/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public function iconContainerAttributes(array $valuesMap): self
* - linkAttributes: array, the HTML attributes of the item's link. For default `linkAttributes` is `[]`.
* - icon: string, the item's icon. For default is ``.
* - iconAttributes: array, the HTML attributes of the item's icon. For default `iconAttributes` is `[]`.
* - tag: string, the item's icon tag. Default is `i`.
* - iconClass: string, the item's icon CSS class. For default is ``.
* - visible: bool, optional, whether this menu item is visible. Defaults to true.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/Menu/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,52 @@ public function testItemsIcon(): void
);
}

/**
* @throws CircularReferenceException
* @throws InvalidConfigException
* @throws NotFoundException
* @throws NotInstantiableException
*/
public function testItemsIconWithTagName(): void
{
Assert::equalsWithoutLE(
<<<HTML
<ul>
<li><a class="me-2" href="/active"><span class="me-2"><i>🏠</i></span>Home</a></li>
<li><a class="me-2" href="#"><span class="me-2"><i>📧</i></span>Contact</a></li>
<li><a class="me-2" href="#"><img src="icon-url" alt="Dahsbaord">Login</a></li>
</ul>
HTML,
Menu::widget()
->iconContainerAttributes(['class' => 'me-2'])
->linkAttributes(['class' => 'me-2'])
->items(
[
[
'label' => 'Home',
'link' => '/active',
'icon' => '🏠',
],
[
'label' => 'Contact',
'link' => '#',
'icon' => '📧',
],
[
'label' => 'Login',
'link' => '#',
'iconAttributes' => [
'tag' => 'img',
'src' => 'icon-url',
'alt' => 'Dahsbaord',
],
],
],
)
->render(),
);
}

/**
* @throws CircularReferenceException
* @throws InvalidConfigException
Expand Down