Skip to content
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

Modal dialog: small improvement for elementShouldBeHidden #65829

Closed
2 tasks done
afercia opened this issue Oct 2, 2024 · 4 comments · Fixed by #65941
Closed
2 tasks done

Modal dialog: small improvement for elementShouldBeHidden #65829

afercia opened this issue Oct 2, 2024 · 4 comments · Fixed by #65941
Labels
[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.

Comments

@afercia
Copy link
Contributor

afercia commented Oct 2, 2024

Description

The modal dialog component uses some logic to determine what body children should be aria-hidden when the modal dialog is open.

export function modalize( modalElement?: HTMLDivElement ) {
const elements = Array.from( document.body.children );
const hiddenElements: Element[] = [];
hiddenElementsByDepth.push( hiddenElements );
for ( const element of elements ) {
if ( element === modalElement ) {
continue;
}
if ( elementShouldBeHidden( element ) ) {
element.setAttribute( 'aria-hidden', 'true' );
hiddenElements.push( element );
}
}
}
/**
* Determines if the passed element should not be hidden from screen readers.
*
* @param element The element that should be checked.
*
* @return Whether the element should not be hidden from screen-readers.
*/
export function elementShouldBeHidden( element: Element ) {
const role = element.getAttribute( 'role' );
return ! (
element.tagName === 'SCRIPT' ||
element.hasAttribute( 'aria-hidden' ) ||
element.hasAttribute( 'aria-live' ) ||
( role && LIVE_REGION_ARIA_ROLES.has( role ) )
);
}

Some elements are correctly filtered out as they should not get the aria-hidden attribute. It looks like elements with the hidden HTML attribute could be filtered out as well, as they're already completely hidden from assistive technologies.

Currentlty, aria-hidden is added and then removed also to these elements, which seems unnecessary:

Image

Step-by-step reproduction instructions

  • Go to the post editor.
  • Inspect the DOM.
  • For the purpose of this test, make sure no speak messages have been sent to the aria-live regions.
  • When no messages have been sent yet, observe the element with id a11y-speak-intro-text does have an HTML attribute hidden.
  • Open a modal dialog e.g. the Keyboard Shortcuts one by pressing the keys Control+Option+H.
  • Observe the a11y-speak-intro-text element gets an aria-hidden="true" attribute, which is unnecessary as the element is already hidden.
  • There may be other elements with a hidden HTML attribute e.g. added by plugins that don't need aria-hidden="true".

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

  • Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

  • Yes
@afercia afercia added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components [Type] Enhancement A suggestion for improvement. labels Oct 2, 2024
@vk17-starlord
Copy link
Contributor

vk17-starlord commented Oct 7, 2024

The code could be optimized by checking whether an element already has the hidden attribute, and if so, skip applying aria-hidden="true". The current behavior is redundant, as hidden already takes care of making the element inaccessible to assistive technologies.

This could be achieved by modifying the elementShouldBeHidden() function to include a condition like:

javascript

if (element.hasAttribute('hidden')) {
return false;
}

should i raise PR with this change @afercia ?

@afercia
Copy link
Contributor Author

afercia commented Oct 8, 2024

@vk17-starlord sure please do feel free to submit a PR.
I think the best approach would be to add one more 'OR' here, where some element with sprecific attributes are already checked.

export function elementShouldBeHidden( element: Element ) {
const role = element.getAttribute( 'role' );
return ! (
element.tagName === 'SCRIPT' ||
element.hasAttribute( 'aria-hidden' ) ||
element.hasAttribute( 'aria-live' ) ||
( role && LIVE_REGION_ARIA_ROLES.has( role ) )
);
}

@vk17-starlord
Copy link
Contributor

@afercia sure working on it

@vk17-starlord
Copy link
Contributor

@afercia can you check this ?

afercia added a commit that referenced this issue Oct 14, 2024
…5941)

* Fixed : Modal dialog: small improvement for elementShouldBeHidden #65829

* Refactor `elementShouldBeHidden` to combine all checks into a single return statement

* Added : PR description in changelog.md

* Removed comments from function and fixed changelog

* Removed modal details from bug fixes and moved to enhancements

* Add missing empty line to changelog.

* moved : PR details to enhancement section

* Add missing empty line to changelog.

---------

Co-authored-by: vk17-starlord <[email protected]>
Co-authored-by: afercia <[email protected]>
Co-authored-by: up1512001 <[email protected]>
ciampo pushed a commit that referenced this issue Oct 14, 2024
…5941)

* Fixed : Modal dialog: small improvement for elementShouldBeHidden #65829

* Refactor `elementShouldBeHidden` to combine all checks into a single return statement

* Added : PR description in changelog.md

* Removed comments from function and fixed changelog

* Removed modal details from bug fixes and moved to enhancements

* Add missing empty line to changelog.

* moved : PR details to enhancement section

* Add missing empty line to changelog.

---------

Co-authored-by: vk17-starlord <[email protected]>
Co-authored-by: afercia <[email protected]>
Co-authored-by: up1512001 <[email protected]>
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this issue Nov 13, 2024
…rdPress#65941)

* Fixed : Modal dialog: small improvement for elementShouldBeHidden WordPress#65829

* Refactor `elementShouldBeHidden` to combine all checks into a single return statement

* Added : PR description in changelog.md

* Removed comments from function and fixed changelog

* Removed modal details from bug fixes and moved to enhancements

* Add missing empty line to changelog.

* moved : PR details to enhancement section

* Add missing empty line to changelog.

---------

Co-authored-by: vk17-starlord <[email protected]>
Co-authored-by: afercia <[email protected]>
Co-authored-by: up1512001 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Components /packages/components [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants