Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- `SearchControl`: polish metrics for `compact` size variant ([#54663](https://github.com/WordPress/gutenberg/pull/54663)).
- `Button`: deprecating `isPressed` prop in favour of `aria-pressed` ([#54740](https://github.com/WordPress/gutenberg/pull/54740)).
- `DuotonePicker/ColorListPicker`: Adds appropriate label and description to 'Duotone Filter' picker ([#54473](https://github.com/WordPress/gutenberg/pull/54473)).
- `Modal`: Accessibly hide/show outer modal when nested ([#54743](https://github.com/WordPress/gutenberg/pull/54743)).

### Bug Fix

Expand Down
42 changes: 17 additions & 25 deletions packages/components/src/modal/aria-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ const LIVE_REGION_ARIA_ROLES = new Set( [
'timer',
] );

let hiddenElements: Element[] = [],
isHidden = false;
const hiddenElementsByDepth: Element[][] = [];

/**
* Hides all elements in the body element from screen-readers except
Expand All @@ -19,31 +18,28 @@ let hiddenElements: Element[] = [],
* we should consider removing these helper functions in favor of
* `aria-modal="true"`.
*
* @param {HTMLDivElement} unhiddenElement The element that should not be hidden.
* @param modalElement The element that should not be hidden.
*/
export function hideApp( unhiddenElement?: HTMLDivElement ) {
if ( isHidden ) {
return;
}
export function modalize( modalElement?: HTMLDivElement ) {
const elements = Array.from( document.body.children );
elements.forEach( ( element ) => {
if ( element === unhiddenElement ) {
return;
}
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 );
}
} );
isHidden = true;
}
}

/**
* Determines if the passed element should not be hidden from screen readers.
*
* @param {HTMLElement} element The element that should be checked.
* @param element The element that should be checked.
*
* @return {boolean} Whether the element should not be hidden from screen-readers.
* @return Whether the element should not be hidden from screen-readers.
*/
export function elementShouldBeHidden( element: Element ) {
const role = element.getAttribute( 'role' );
Expand All @@ -56,16 +52,12 @@ export function elementShouldBeHidden( element: Element ) {
}

/**
* Makes all elements in the body that have been hidden by `hideApp`
* visible again to screen-readers.
* Accessibly reveals the elements hidden by the latest modal.
*/
export function showApp() {
if ( ! isHidden ) {
return;
}
hiddenElements.forEach( ( element ) => {
export function unmodalize() {
const hiddenElements = hiddenElementsByDepth.pop();
if ( ! hiddenElements ) return;

for ( const element of hiddenElements )
element.removeAttribute( 'aria-hidden' );
} );
hiddenElements = [];
isHidden = false;
}
7 changes: 5 additions & 2 deletions packages/components/src/modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,15 @@ function UnforwardedModal(
}
}, [ contentRef ] );

useEffect( () => {
ariaHelper.modalize( ref.current );
return () => ariaHelper.unmodalize();
}, [] );

useEffect( () => {
openModalCount++;

if ( openModalCount === 1 ) {
ariaHelper.hideApp( ref.current );
document.body.classList.add( bodyOpenClassName );
}

Expand All @@ -125,7 +129,6 @@ function UnforwardedModal(

if ( openModalCount === 0 ) {
document.body.classList.remove( bodyOpenClassName );
ariaHelper.showApp();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the separation of concerns here between modalizing/unmodalizing and adding/removing the body classname

};
}, [ bodyOpenClassName ] );
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/modal/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,7 @@ describe( 'Modal', () => {
expect( onRequestClose ).not.toHaveBeenCalled();
} );

// TODO enable once nested modals hide outer modals.
it.skip( 'should accessibly hide and show siblings including outer modals', async () => {
it( 'should accessibly hide and show siblings including outer modals', async () => {
const user = userEvent.setup();

const AriaDemo = () => {
Expand Down