Skip to content

Commit

Permalink
fix(cc-icon): improve SVG aria attributes behavior
Browse files Browse the repository at this point in the history
* only try to update SVG aria attributes if SVG is in the DOM
* don't create several `<title>` tags when `accessibleName` changes
* apply aria attributes on SVG when `icon` changes

Fixes #770
  • Loading branch information
HeleneAmouzou committed Oct 11, 2023
1 parent 6584f8b commit e8645f7
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/components/cc-icon/cc-icon.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,25 @@ export class CcIcon extends LitElement {
}

updated (changedProperties) {
if (!this.skeleton && changedProperties.has('accessibleName')) {
const svg = this.shadowRoot.querySelector('svg');
const shouldPatchSvg = changedProperties.has('accessibleName') || changedProperties.has('icon');
const svg = this.shadowRoot.querySelector('svg');
if (shouldPatchSvg && svg != null) {
if (this.accessibleName == null || this.accessibleName === '') {
svg.setAttribute('aria-hidden', 'true');
svg.removeAttribute('aria-label');
svg.removeAttribute('role');

svg.querySelector('title')?.remove();
}
else {
svg.removeAttribute('aria-hidden');
svg.setAttribute('aria-label', this.accessibleName);
svg.setAttribute('role', 'img');

const title = document.createElement('title');
title.innerHTML = this.accessibleName;
svg.prepend(title);
let title = svg.querySelector('title');
if (title == null) {
title = document.createElement('title');
svg.prepend(title);
}
title.textContent = this.accessibleName;
}
}
}
Expand Down

0 comments on commit e8645f7

Please sign in to comment.