Skip to content
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

[Icons] Icon aliases #2127

Merged
merged 1 commit into from
Sep 9, 2024
Merged
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
4 changes: 4 additions & 0 deletions src/Icons/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG

## 2.20.0

- Add `aliases` configuration option to define icon alternative names.

## 2.19.0

- Add `ignore_not_found` option to silence error during rendering if the
Expand Down
2 changes: 2 additions & 0 deletions src/Icons/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@
->set('.ux_icons.icon_renderer', IconRenderer::class)
->args([
service('.ux_icons.icon_registry'),
abstract_arg('default_icon_attributes'),
abstract_arg('icon_aliases'),
])

->alias('Symfony\UX\Icons\IconRendererInterface', '.ux_icons.icon_renderer')
Expand Down
29 changes: 29 additions & 0 deletions src/Icons/doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,30 @@ Now, all icons will have the ``fill`` attribute set to ``currentColor`` by defau
# renders "user-profile.svg" with fill="red"
{{ ux_icon('user-profile', {fill: 'red'}) }}

Icon Aliases
~~~~~~~~~~~~

You can define aliases for icons in your configuration, which is helpful if
you want to use a different or shorter name for an icon in your templates:

.. code-block:: yaml

# config/packages/ux_icons.yaml
ux_icons:

aliases:
dots: 'clarity:ellipsis-horizontal-line'

Now, you can use the ``dots`` alias in your templates:

.. code-block:: twig

{{ ux_icon('dots') }}

{# same as #}

{{ ux_icon('clarity:ellipsis-horizontal-line') }}

Errors
------

Expand Down Expand Up @@ -517,6 +541,11 @@ Full Configuration
default_icon_attributes:
# Default:
fill: currentColor

# Icon aliases (alias => icon name).
aliases:
# Exemple:
dots: 'clarity:ellipsis-horizontal-line'

# Configuration for the "on demand" icons powered by Iconify.design.
iconify:
Expand Down
9 changes: 9 additions & 0 deletions src/Icons/src/DependencyInjection/UXIconsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public function getConfigTreeBuilder(): TreeBuilder
->info('Default attributes to add to all icons.')
->defaultValue(['fill' => 'currentColor'])
->end()
->arrayNode('aliases')
->info('Icon aliases (alias => icon name).')
->example(['dots' => 'clarity:ellipsis-horizontal-line'])
->normalizeKeys(false)
->scalarPrototype()
->cannotBeEmpty()
->end()
->end()
->arrayNode('iconify')
->info('Configuration for the "on demand" icons powered by Iconify.design.')
->{interface_exists(HttpClientInterface::class) ? 'canBeDisabled' : 'canBeEnabled'}()
Expand Down Expand Up @@ -98,6 +106,7 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container

$container->getDefinition('.ux_icons.icon_renderer')
->setArgument(1, $mergedConfig['default_icon_attributes'])
->setArgument(2, $mergedConfig['aliases'])
;

$container->getDefinition('.ux_icons.twig_icon_runtime')
Expand Down
3 changes: 3 additions & 0 deletions src/Icons/src/IconRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ final class IconRenderer implements IconRendererInterface
public function __construct(
private readonly IconRegistryInterface $registry,
private readonly array $defaultIconAttributes = [],
private readonly ?array $iconAliases = [],
) {
}

Expand All @@ -35,6 +36,8 @@ public function __construct(
*/
public function renderIcon(string $name, array $attributes = []): string
{
$name = $this->iconAliases[$name] ?? $name;

$icon = $this->registry->get($name)
->withAttributes($this->defaultIconAttributes)
->withAttributes($attributes);
Expand Down
19 changes: 19 additions & 0 deletions src/Icons/tests/Unit/IconRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ public static function provideAriaHiddenCases(): iterable
];
}

public function testRenderIconWithAliases(): void
{
$registry = $this->createRegistry([
'foo' => '<path d="M0 FOO"/>',
'bar' => '<path d="M0 BAR"/>',
'baz' => '<path d="M0 BAZ"/>',
]);
$iconRenderer = new IconRenderer($registry, [], ['foo' => 'bar']);

$svg = $iconRenderer->renderIcon('bar');
$this->assertSame('<svg aria-hidden="true"><path d="M0 BAR"/></svg>', $svg);

$svg = $iconRenderer->renderIcon('foo');
$this->assertSame('<svg aria-hidden="true"><path d="M0 BAR"/></svg>', $svg);

$svg = $iconRenderer->renderIcon('baz');
$this->assertSame('<svg aria-hidden="true"><path d="M0 BAZ"/></svg>', $svg);
}

private function createRegistry(array $icons): IconRegistryInterface
{
$registryIcons = [];
Expand Down
Loading