Skip to content

Commit

Permalink
Merge pull request #72 from infinum/feature/a11y-icons
Browse files Browse the repository at this point in the history
Feature - Implement a11y for Icon component
  • Loading branch information
piqusy authored Dec 6, 2024
2 parents a29d6ea + 54eaac2 commit f3b2de1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This projects adheres to [Semantic Versioning](https://semver.org/) and [Keep a
- Fix Image component responsive output Co-authored-by: goran.alkovic@infinum.com
- Add "Auto" width to wrapperContent and set is as defaults
- Add a buttonType attribute to Button component
- Implement a11y for Icon component either by setting aria-hidden to true (default) or using the existing iconName label from manifest options key
- Updated dependencies.

## [1.4.6]
Expand Down
19 changes: 18 additions & 1 deletion blocks/init/src/Blocks/components/icon/icon.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
$additionalClass = $attributes['additionalClass'] ?? '';

$iconName = Helpers::checkAttr('iconName', $attributes, $manifest);
$iconAriaHidden = Helpers::checkAttr('iconAriaHidden', $attributes, $manifest);

if (!isset($manifest['icons'][$iconName])) {
return;
Expand All @@ -29,7 +30,23 @@
$className = Helpers::tailwindClasses('base', $attributes, $manifest, $additionalClass);

if (!empty($className)) {
$icon = str_replace('<svg ', '<svg class="' . htmlspecialchars($className) . '" ', $icon);
$icon = str_replace('<svg ', '<svg class="' . esc_attr($className) . '" ', $icon);
}

if ($iconAriaHidden) {
$icon = str_replace('<svg ', '<svg aria-hidden="true" ', $icon);
} else {
$iconTitle = '';
$iconOption = array_filter($manifest['options']['iconName'], fn($option) => $option['value'] === $iconName);

if (!empty($iconOption)) {
$iconTitle = reset($iconOption)['label'];
}

if (!empty($iconTitle)) {
$titleTag = '<title>' . esc_html($iconTitle) . '</title>';
$icon = str_replace('</svg>', $titleTag . '</svg>', $icon);
}
}

// phpcs:ignore Eightshift.Security.HelpersEscape.OutputNotEscaped
Expand Down
4 changes: 4 additions & 0 deletions blocks/init/src/Blocks/components/icon/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"iconUse": {
"type": "boolean",
"default": true
},
"iconAriaHidden": {
"type": "boolean",
"default": true
}
},
"options": {
Expand Down

0 comments on commit f3b2de1

Please sign in to comment.