Skip to content

[Icons] Set aria-hidden="true" when title/label not set #1690

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

Merged
merged 1 commit into from
Apr 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
14 changes: 13 additions & 1 deletion src/Icons/src/IconRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,20 @@ public function __construct(
public function renderIcon(string $name, array $attributes = []): string
{
return $this->registry->get($name)
->withAttributes([...$this->defaultIconAttributes, ...$attributes])
->withAttributes($this->getIconAttributes($name, $attributes))
->toHtml()
;
}

private function getIconAttributes(string $name, array $attributes): array
{
$iconAttributes = $this->defaultIconAttributes;

// Add aria-hidden attribute
if ([] === array_intersect(['aria-hidden', 'aria-label', 'aria-labelledby', 'title'], array_keys($attributes))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

simple curiosity where did you get this list?

Copy link
Member Author

Choose a reason for hiding this comment

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

An image’s alt attribute.
A SVG’s child title element.
Using aria-label or aria-labelledby on an image or SVG.
Using a title attribute on an image or SVG.

https://www.scottohara.me/blog/2019/05/22/contextual-images-svgs-and-a11y.html

$iconAttributes['aria-hidden'] = 'true';
}

return [...$iconAttributes, ...$attributes];
}
}
4 changes: 2 additions & 2 deletions src/Icons/tests/Fixtures/templates/template1.html.twig
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<ul class="svg">
<li id="first">{{ ux_icon('user', {class: 'h-8 w-8'}) }}</li>
<li id="second">{{ ux_icon('user') }}</li>
<li id="second">{{ ux_icon('user', {"aria-label": "AriaLabel"}) }}</li>
<li id="third">{{ ux_icon('sub:check', {'data-action': 'string "with" quotes'}) }}</li>
<li id="fifth"><twig:UX:Icon name="user" class="h-8 w-8" /></li>
<li id="sixth"><twig:ux:icon name="sub:check" /></li>
<li id="sixth"><twig:ux:icon name="sub:check" aria-labelledby="foo" /></li>
<li id="seventh"><twig:Ux:Icon :name="'sub:'~'check'" /></li>
<li id="eighth">{{ ux_icon('iconamoon:3d-duotone') }}</li>
</ul>
14 changes: 7 additions & 7 deletions src/Icons/tests/Integration/RenderIconsInTwigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function testRenderIcons(): void
$this->assertSame(
<<<HTML
<ul class="svg">
<li id="first"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8"><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>
<li id="second"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-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>
<li id="third"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" data-action="string &quot;with&quot; quotes"><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>
<li id="fifth"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8"><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>
<li id="sixth"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6"><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>
<li id="seventh"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6"><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>
<li id="eighth"><svg viewBox="0 0 24 24" fill="currentColor"><g fill="none"><path fill="currentColor" d="m12 3l7.794 4.5v7.845a2 2 0 0 1-1 1.732L13 20.423a2 2 0 0 1-2 0l-5.794-3.346a2 2 0 0 1-1-1.732V7.5z" opacity=".16"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m12 3l7.794 4.5v7.845a2 2 0 0 1-1 1.732L13 20.423a2 2 0 0 1-2 0l-5.794-3.346a2 2 0 0 1-1-1.732V7.5z"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 7v5l-4.33 2.5M12 12l4.33 2.5"/></g></svg></li>
<li id="first"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8" aria-hidden="true"><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>
<li id="second"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-label="AriaLabel"><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>
<li id="third"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-hidden="true" data-action="string &quot;with&quot; quotes"><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>
<li id="fifth"><svg viewBox="0 0 24 24" fill="currentColor" class="h-8 w-8" aria-hidden="true"><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>
<li id="sixth"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-labelledby="foo"><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>
<li id="seventh"><svg viewBox="0 0 24 24" fill="currentColor" class="w-6 h-6" aria-hidden="true"><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>
<li id="eighth"><svg viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><g fill="none"><path fill="currentColor" d="m12 3l7.794 4.5v7.845a2 2 0 0 1-1 1.732L13 20.423a2 2 0 0 1-2 0l-5.794-3.346a2 2 0 0 1-1-1.732V7.5z" opacity=".16"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m12 3l7.794 4.5v7.845a2 2 0 0 1-1 1.732L13 20.423a2 2 0 0 1-2 0l-5.794-3.346a2 2 0 0 1-1-1.732V7.5z"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 7v5l-4.33 2.5M12 12l4.33 2.5"/></g></svg></li>
</ul>
HTML,
trim($output)
Expand Down
52 changes: 50 additions & 2 deletions src/Icons/tests/Unit/IconRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testRenderIconWithAttributes(): void

$svg = $iconRenderer->renderIcon('foo', $attributes);

$this->assertSame('<svg viewBox="0 0 24 24" class="icon" id="FooBar"><path d="M0 0L12 12"/></svg>', $svg);
$this->assertSame('<svg aria-hidden="true" viewBox="0 0 24 24" class="icon" id="FooBar"><path d="M0 0L12 12"/></svg>', $svg);
}

public function testRenderIconWithDefaultAttributes(): void
Expand All @@ -78,7 +78,7 @@ public function testRenderIconWithDefaultAttributes(): void

$svg = $iconRenderer->renderIcon('foo');

$this->assertSame('<svg viewBox="0 0 24 24" class="icon"><path d="M0 0L12 12"/></svg>', $svg);
$this->assertSame('<svg viewBox="0 0 24 24" class="icon" aria-hidden="true"><path d="M0 0L12 12"/></svg>', $svg);
}

/**
Expand All @@ -92,6 +92,7 @@ public function testRenderIconWithAttributesAndDefaultAttributes($iconAttrs, $de
$iconRenderer = new IconRenderer($registry, $defaultAttrs);

$svg = $iconRenderer->renderIcon('foo', $renderAttr);
$svg = str_replace(' aria-hidden="true"', '', $svg);
$this->assertStringStartsWith($expectedTag, $svg);
}

Expand Down Expand Up @@ -159,6 +160,53 @@ public static function provideAttributesWithDefaultAttributesCases()
];
}

/**
* @dataProvider provideAriaHiddenCases
*
* @param array<string, string> $attributes
*/
public function testRenderIconWithAutoAriaHidden(array $attributes, string $expectedSvg): void
{
$registry = $this->createRegistry([
'foo' => '<path d="M0 0L12 12"/>',
]);
$iconRenderer = new IconRenderer($registry);

$svg = $iconRenderer->renderIcon('foo', $attributes);
$this->assertSame($expectedSvg, $svg);
}

/**
* @return iterable<array{array<string, string>, string}>
*/
public static function provideAriaHiddenCases(): iterable
{
yield 'no attributes' => [
[],
'<svg aria-hidden="true"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-hidden attribute' => [
['aria-hidden' => 'true'],
'<svg aria-hidden="true"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-hidden false + aria-label' => [
['aria-hidden' => 'false', 'aria-label' => 'foo'],
'<svg aria-hidden="false" aria-label="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'title attribute' => [
['title' => 'foo'],
'<svg title="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-labelledby attribute' => [
['aria-labelledby' => 'foo'],
'<svg aria-labelledby="foo"><path d="M0 0L12 12"/></svg>',
];
yield 'aria-label attribute' => [
['aria-label' => 'foo'],
'<svg aria-label="foo"><path d="M0 0L12 12"/></svg>',
];
}

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