Skip to content

Commit 14f22e0

Browse files
authored
Improve UXComponent performance
1 parent d9d163d commit 14f22e0

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

src/Icons/config/services.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\UX\Icons\Registry\CacheIconRegistry;
1717
use Symfony\UX\Icons\Registry\LocalSvgIconRegistry;
1818
use Symfony\UX\Icons\Twig\UXIconComponent;
19+
use Symfony\UX\Icons\Twig\UXIconComponentListener;
1920
use Symfony\UX\Icons\Twig\UXIconExtension;
2021

2122
return static function (ContainerConfigurator $container): void {
@@ -49,6 +50,15 @@
4950
])
5051
->tag('twig.runtime')
5152

53+
->set('.ux_icons.twig_component_listener', UXIconComponentListener::class)
54+
->args([
55+
service('.ux_icons.icon_renderer'),
56+
])
57+
->tag('kernel.event_listener', [
58+
'event' => 'Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent',
59+
'method' => 'onPreCreateForRender',
60+
])
61+
5262
->set('.ux_icons.twig_component.icon', UXIconComponent::class)
5363
;
5464
};

src/Icons/src/DependencyInjection/UXIconsExtension.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
7171
->setArgument(1, $mergedConfig['default_icon_attributes'])
7272
;
7373

74+
$container->getDefinition('.ux_icons.twig_component_listener')
75+
->setArgument(1, $mergedConfig['twig_component_name'])
76+
;
77+
7478
$container->getDefinition('.ux_icons.twig_component.icon')
7579
->addTag('twig.component', [
7680
'key' => $mergedConfig['twig_component_name'],
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Symfony\UX\Icons\Twig;
4+
5+
use Symfony\UX\Icons\IconRenderer;
6+
use Symfony\UX\TwigComponent\Event\PreCreateForRenderEvent;
7+
8+
/**
9+
* @author Simon André <[email protected]>
10+
*
11+
* @internal
12+
*/
13+
final class UXIconComponentListener
14+
{
15+
public function __construct(
16+
private IconRenderer $iconRenderer,
17+
private string $componentName,
18+
) {
19+
}
20+
21+
public function onPreCreateForRender(PreCreateForRenderEvent $event): void
22+
{
23+
if ($this->componentName !== $event->getName()) {
24+
return;
25+
}
26+
27+
$attributes = $event->getInputProps();
28+
$name = (string) $attributes['name'];
29+
unset($attributes['name']);
30+
31+
$svg = $this->iconRenderer->renderIcon($name, $attributes);
32+
$event->setRenderedString($svg);
33+
$event->stopPropagation();
34+
}
35+
}

src/Icons/tests/Integration/Twig/UXIconExtensionTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,11 @@ public function testRenderIcons(): void
3939
<li id="second"><svg viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg></li>
4040
<li id="third"><svg viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
4141
<li id="forth"><svg viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
42-
<li id="fifth"><svg viewBox="0 0 24 24" fill="currentColor" class="h-6 w-6"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg>
43-
</li>
44-
<li id="sixth"><svg viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg>
45-
</li>
42+
<li id="fifth"><svg viewBox="0 0 24 24" fill="currentColor" class="h-6 w-6"><path fill-rule="evenodd" d="M7.5 6a4.5 4.5 0 1 1 9 0 4.5 4.5 0 0 1-9 0ZM3.751 20.105a8.25 8.25 0 0 1 16.498 0 .75.75 0 0 1-.437.695A18.683 18.683 0 0 1 12 22.5c-2.786 0-5.433-.608-7.812-1.7a.75.75 0 0 1-.437-.695Z" clip-rule="evenodd"></path></svg></li>
43+
<li id="sixth"><svg viewBox="0 0 24 24" fill="currentColor"><path fill-rule="evenodd" d="M19.916 4.626a.75.75 0 0 1 .208 1.04l-9 13.5a.75.75 0 0 1-1.154.114l-6-6a.75.75 0 0 1 1.06-1.06l5.353 5.353 8.493-12.74a.75.75 0 0 1 1.04-.207Z" clip-rule="evenodd"></path></svg></li>
4644
</ul>
4745
HTML,
48-
$output
46+
preg_replace("#(\s+)</li>#m", "</li>", $output)
4947
);
5048
}
5149
}

0 commit comments

Comments
 (0)