-
-
Notifications
You must be signed in to change notification settings - Fork 270
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
[UX-Icons] Allow Reuse #1800
Comments
It is something we have in mind (@kbond even prototyped something as first), but this is much more complex than it seems Webpages are very often not rendered in a linear / complete manner (think cache, ESI, components, duplicate ids, ...) |
Yes, this is the Simon and I are planning some refactoring so I'd like to hold off on this until this is done. We'll keep it in mind though as I think it's required. |
For the record, I was able to implement it on my personal project by adding a decorator on top of the IconRegistry, instead of the IconRenderer. I share it below if anyone need this, but remember it may not work in all cases as mentionned by @smnandre, and we shouldn't decorate View code<?php
declare(strict_types=1);
namespace App\Icon;
use Symfony\Component\DependencyInjection\Attribute\AsDecorator;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\UX\Icons\Icon;
use Symfony\UX\Icons\IconRegistryInterface;
#[AsDecorator('.ux_icons.icon_registry')]
final class IconAlreadyUsedRegistry implements IconRegistryInterface
{
/**
* @var array<string, string>
*/
private array $alreadyUsed = [];
public function __construct(
private RequestStack $requestStack,
private IconRegistryInterface $decorated,
) {}
public function get(string $name): Icon
{
$icon = $this->decorated->get($name);
$id = $icon::nameToId($name);
// Generate an unique Id for each icon by Request
// Needed as static-site does not restart kernel on each request,
// and icon are considered as already used on new pages
$request = $this->requestStack->getCurrentRequest();
if (null === $request) {
return $icon;
}
$id = md5(sprintf('%s-%s', $id, spl_object_id($request)));
if (!\array_key_exists($id, $this->alreadyUsed)) {
$this->alreadyUsed[$id] = $id;
return $icon->withAttributes(['id' => $id]);
}
// Return a reusable icon, take only viewBox attribute to avoid rendering issues
return new Icon('<use xlink:href="#' . $id . '"/>', [$icon->getAttributes()['viewBox']]);
}
} Fully implemented here |
We are working on the configuration of icons (default attributes, prefix aliases). Once it's done we will be able to implement this feature (step by step) :) I think the separation between the symbols (what's gonna be reused) and the rendered icons (using those symbols) should be stronger... Or we may have troubles with default attributes, runtime render ones, etc. So the final HTML should probably be more like <svg style="display: none;" hidden>
<symbol id="icon:lucide:heart" viewBox="0 0 512 512"> ... </symbol>
</svg>
<svg viewBox="0 0 512 512"><use href="#icon:lucide:heart"></use></svg>
<svg viewBox="0 0 512 512" style="color:red"><use href="#icon:lucide:heart"></use></svg> Two comments here:
The other complexity lies in those two questions:
I'm not saying we must find a single silver-bullet answer. |
When an SVG has been added once in a page, it can be reused with the
<use xlink:href=#id>
later in the same page to avoid duplication of svg.It could be great if ux_icon would have a
allowReuse
parameters to transform icon to use a previously added.For example :
Would produce following html
I was able to achieve this by adding following line in IconRenderer
For really complexe svg reused multiple time, it could save the size of the generated page.
WDYT ? If you think such feature would be nice, I could provide a PR.
The text was updated successfully, but these errors were encountered: