Skip to content

bug(COMPONENT): Blocked aria-hidden on an element because its descendant retained focus. #30187

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

Closed
1 task done
tenji73 opened this issue Dec 14, 2024 · 13 comments · Fixed by #31030
Closed
1 task done
Assignees
Labels
area: material/dialog P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Comments

@tenji73
Copy link

tenji73 commented Dec 14, 2024

Is this a regression?

  • Yes, this behavior used to work in the previous version

The previous version in which this bug was not present was

No response

Description

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, which will also prevent focus. For more details, see the aria-hidden section of the WAI-ARIA specification at https://w3c.github.io/aria/#aria-hidden.
Element with focus: button
Ancestor with aria-hidden: <material-docs-app ng-version=​"19.0.4" aria-hidden=​"true">​…​​flex...

also visible when trying the examples in chrome (Version 131.0.6778.109 (Official Build) (arm64))
https://material.angular.io/components/dialog/overview

happens when the dialog has a button...

Reproduction

examples on the material.angular.io:
https://material.angular.io/components/dialog/overview

Expected Behavior

no error

Actual Behavior

works, but shows an error in the console

Environment

  • Angular: 18.2
  • CDK/Material: 18
  • Browser(s): chrome (Version 131.0.6778.109 (Official Build) (arm64))
  • Operating System (e.g. Windows, macOS, Ubuntu): mac os
@tenji73 tenji73 added the needs triage This issue needs to be triaged by the team label Dec 14, 2024
@ymdelgado2332
Copy link

I'm experiencing the same issue when using MatDialog in Angular Material version 16.2.14. The error I see in the browser console is:

"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, which will also prevent focus."

This happens when I open a dialog, and it seems related to how aria-hidden is applied to the background content while the dialog is active. From what I've researched, this issue appears to have been present since version 13 of Angular Material and still persists in the current version.

@ymdelgado2332
Copy link

I was able to resolve this issue using the solution provided in this StackOverflow thread.

Before opening the dialog, I added the following lines of code to ensure that the previously focused element (e.g., a button) loses focus:
const buttonElement = document.activeElement as HTMLElement; // Get the currently focused element
buttonElement.blur(); // Remove focus from the button

This prevents the error by ensuring that no focused element is hidden by the aria-hidden attribute when the dialog is opened. It worked perfectly for me! Let me know if it helps you too.

@shifatul-i
Copy link

+1

@aeslinger0
Copy link

aeslinger0 commented Jan 14, 2025

@ymdelgado2332 wouldn't that fix the error but still break accessibility? The focus should be returned to the original element once the dialog is closed, otherwise a mouse-less user might have to tab down all the way from the top.

I'm trying to play around with the delayFocusTrap or autoFocus properties to see if those help, but I can't seem to reproduce it consistently.

@crisbeto crisbeto added P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent area: material/dialog and removed needs triage This issue needs to be triaged by the team labels Jan 20, 2025
@kkys4bfgp75be9p
Copy link

same error exists on 19 version:

Image

@VOSolyanik
Copy link

We have a similar issue. fixed it by adding MAT_DIALOG_DEFAULT_OPTIONS.

{
  provide: MAT_DIALOG_DEFAULT_OPTIONS,
  useValue: { autoFocus: 'dialog', restoreFocus: true }
}

@tenji73
Copy link
Author

tenji73 commented Feb 11, 2025

nice solution! thanks!

@krisBerserker
Copy link

had the same issue with vueJs3 and primevue.
Changed ''to'' parameter with ''url'' in the definitions of items collection and add in the template:
items: [
{ label: 'Accueil', icon: 'pi pi-fw pi-home inert', to: '/' },
{ label: 'Évènements', icon: 'pi pi-fw pi-calendar', to: '/evenements' },
{ label: 'Statistique', icon: 'pi pi-fw pi-chart-bar', to: '/statistique' },
{ label: 'Login', icon: 'pi pi-fw pi-sign-in', to: '/login' }
]
changed into :
items: [
{ label: 'Accueil', icon: 'pi pi-fw pi-home inert', url: '/' },
{ label: 'Évènements', icon: 'pi pi-fw pi-calendar', url: '/evenements' },
{ label: 'Statistique', icon: 'pi pi-fw pi-chart-bar', url: '/statistique' },
{ label: 'Login', icon: 'pi pi-fw pi-sign-in', url: '/login' }
]

@grepsoft
Copy link

grepsoft commented Mar 2, 2025

This issue is related to accessibility. To resolve this, you can use the inert attribute instead of aria-hidden, which will also prevent focus.

inert should be true with dialog is show otherwise false.

@marc-wilson
Copy link

We have a similar issue. fixed it by adding MAT_DIALOG_DEFAULT_OPTIONS.

{
  provide: MAT_DIALOG_DEFAULT_OPTIONS,
  useValue: { autoFocus: 'dialog', restoreFocus: true }
}

seems like the most "correct" way to do it. not working for me though... tried a few different variations of the config. but nothing.

@ChrisJohnNewton
Copy link

ChrisJohnNewton commented Mar 18, 2025

This issue is related to accessibility. To resolve this, you can use the inert attribute instead of aria-hidden, which will also prevent focus.

inert should be true with dialog is show otherwise false.

Yes this seems like the solution - hopefully the Angular Material team will look into it

@kvandana451
Copy link

{
provide: MAT_DIALOG_DEFAULT_OPTIONS,
useValue: { autoFocus: 'dialog', restoreFocus: true }
} Somehow this works but @ChrisJohnNewton where can we add the inert attribute to the root?

@q100va
Copy link

q100va commented Mar 25, 2025

I was able to resolve this issue using the solution provided in this StackOverflow thread.

Before opening the dialog, I added the following lines of code to ensure that the previously focused element (e.g., a button) loses focus: const buttonElement = document.activeElement as HTMLElement; // Get the currently focused element buttonElement.blur(); // Remove focus from the button

This prevents the error by ensuring that no focused element is hidden by the aria-hidden attribute when the dialog is opened. It worked perfectly for me! Let me know if it helps you too.

Thank you for link! I used @asgarov1's solution with a custom directive.

@crisbeto crisbeto self-assigned this May 5, 2025
crisbeto added a commit to crisbeto/material2 that referenced this issue May 5, 2025
The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes angular#30187.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 5, 2025
The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes angular#30187.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 5, 2025
The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes angular#30187.
crisbeto added a commit to crisbeto/material2 that referenced this issue May 6, 2025
…gular#31030)

The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes angular#30187.
crisbeto added a commit that referenced this issue May 6, 2025
…1030) (#31036)

The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes #30187.
wildcardalice pushed a commit to wildcardalice/components that referenced this issue May 12, 2025
…gular#31030)

The dialog moves focus in an `afterRender`, because it needs to give the content some time to be rendered. This is problematic with some relatively recent behavior in Chrome where the `aria-hidden` gets blocked and a warning is logged if it contains the focused element.

These changes add a way for the container to indicate when it's done moving focus and use the new API to apply the `aria-hidden`.

Fixes angular#30187.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/dialog P3 An issue that is relevant to core functions, but does not impede progress. Important, but not urgent
Projects
None yet
Development

Successfully merging a pull request may close this issue.