I’m encountering a warning in my Angular application that states:
Blocked aria-hidden on an element because its descendant retained focus. The focus must not be hidden from assistive technology users. Avoid using aria-hidden on a focused element or its ancestor. Consider using the inert attribute instead.
I’m using a modal dialog created with the Angular Bootstrap (ng-bootstrap) library. Here’s a simplified version of the relevant code:
<div class="modal-header">
<h4 class="modal-title">Serial Number</h4>
<button type="button" class="btn-close" aria-label="Close" aria-label="Close" (click)="popupClose(false)">
<span aria-hidden="true">×</span>
</button>
</div>
Issue:
The warning indicates that an element with aria-hidden is blocking focus from its descendant elements. I want to ensure that my modal is accessible and does not cause issues for assistive technology users.
I considered using the inert attribute as suggested, but I’m unsure how to implement it in this context.
I’ve looked through the ng-bootstrap
documentation and found no specific guidance on this warning.
2
Answers
This warning occurs when an element or one of its ancestors has the aria-hidden="true" attribute, which hides the element from assistive technologies like screen readers. However, if this element (or any of its descendants) is focusable, assistive technology users may still "see" it, which can create a confusing user experience.
Here’s how you can resolve it:
Here’s an example of how to adjust your button:
By removing aria-hidden="true" and using aria-label to improve accessibility, you avoid the conflict with focusable elements. If you need the button hidden temporarily, consider setting it to display: none instead, as it will remove it from the accessibility tree entirely.
Let’s suppose this is how you’re using the NgbModal to open the modal inside your angular component:
Now you will see expected warning which occurs because of how ng-bootstrap manages modals. When a modal is opened, ng-bootstrap adds aria-hidden="true" to the ‘app-root’ element to make it inaccessible to assistive technologies while the modal is active.
However, at this moment:
retains focus.
aria-hidden="true", this creates an accessibility conflict. Assistive
technologies flag this as an issue because the focused element is
effectively hidden.
To resolve this, remove the focus from the button (or any triggering element) before opening the modal. This prevents the focused element from being inside the aria-hidden="true" ancestor.