Skip to content

Commit c0f5e5e

Browse files
fix(overlay): do not hide overlay if toast is presented (#29140)
Issue number: resolves #29139 --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> When implementing #28997 we did not consider the case where a Toast could be presented. When presenting a Toast after presenting a Modal the linked change causes the Modal to be hidden from screen readers. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> - If the top-most overlay is a Toast then the closest non-Toast overlay is also not hidden from screen readers. ## Does this introduce a breaking change? - [ ] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> Dev build: `7.7.5-dev.11710260658.1fc29a6c` --------- Co-authored-by: Amanda Johnston <[email protected]>
1 parent 85b9d5c commit c0f5e5e

File tree

2 files changed

+131
-17
lines changed

2 files changed

+131
-17
lines changed

core/src/utils/overlays.ts

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -541,16 +541,7 @@ export const present = async <OverlayPresentOptions>(
541541
}
542542

543543
setRootAriaHidden(true);
544-
545-
/**
546-
* Hide all other overlays from screen readers so only this one
547-
* can be read. Note that presenting an overlay always makes
548-
* it the topmost one.
549-
*/
550-
if (doc !== undefined) {
551-
const presentedOverlays = getPresentedOverlays(doc);
552-
presentedOverlays.forEach((o) => o.setAttribute('aria-hidden', 'true'));
553-
}
544+
hideOverlaysFromScreenReaders(overlay.el);
554545

555546
overlay.presented = true;
556547
overlay.willPresent.emit();
@@ -723,13 +714,7 @@ export const dismiss = async <OverlayDismissOptions>(
723714

724715
overlay.el.remove();
725716

726-
/**
727-
* If there are other overlays presented, unhide the new
728-
* topmost one from screen readers.
729-
*/
730-
if (doc !== undefined) {
731-
getPresentedOverlay(doc)?.removeAttribute('aria-hidden');
732-
}
717+
revealOverlaysToScreenReaders();
733718

734719
return true;
735720
};
@@ -966,3 +951,65 @@ export const createTriggerController = () => {
966951
removeClickListener,
967952
};
968953
};
954+
955+
/**
956+
* Ensure that underlying overlays have aria-hidden if necessary so that screen readers
957+
* cannot move focus to these elements. Note that we cannot rely on focus/focusin/focusout
958+
* events here because those events do not fire when the screen readers moves to a non-focusable
959+
* element such as text.
960+
* Without this logic screen readers would be able to move focus outside of the top focus-trapped overlay.
961+
*
962+
* @param newTopMostOverlay - The overlay that is being presented. Since the overlay has not been
963+
* fully presented yet at the time this function is called it will not be included in the getPresentedOverlays result.
964+
*/
965+
const hideOverlaysFromScreenReaders = (newTopMostOverlay: HTMLIonOverlayElement) => {
966+
if (doc === undefined) return;
967+
968+
const overlays = getPresentedOverlays(doc);
969+
970+
for (let i = overlays.length - 1; i >= 0; i--) {
971+
const presentedOverlay = overlays[i];
972+
const nextPresentedOverlay = overlays[i + 1] ?? newTopMostOverlay;
973+
974+
/**
975+
* If next overlay has aria-hidden then all remaining overlays will have it too.
976+
* Or, if the next overlay is a Toast that does not have aria-hidden then current overlay
977+
* should not have aria-hidden either so focus can remain in the current overlay.
978+
*/
979+
if (nextPresentedOverlay.hasAttribute('aria-hidden') || nextPresentedOverlay.tagName !== 'ION-TOAST') {
980+
presentedOverlay.setAttribute('aria-hidden', 'true');
981+
}
982+
}
983+
};
984+
985+
/**
986+
* When dismissing an overlay we need to reveal the new top-most overlay to screen readers.
987+
* If the top-most overlay is a Toast we potentially need to reveal more overlays since
988+
* focus is never automatically moved to the Toast.
989+
*/
990+
const revealOverlaysToScreenReaders = () => {
991+
if (doc === undefined) return;
992+
993+
const overlays = getPresentedOverlays(doc);
994+
995+
for (let i = overlays.length - 1; i >= 0; i--) {
996+
const currentOverlay = overlays[i];
997+
998+
/**
999+
* If the current we are looking at is a Toast then we can remove aria-hidden.
1000+
* However, we potentially need to keep looking at the overlay stack because there
1001+
* could be more Toasts underneath. Additionally, we need to unhide the closest non-Toast
1002+
* overlay too so focus can move there since focus is never automatically moved to the Toast.
1003+
*/
1004+
currentOverlay.removeAttribute('aria-hidden');
1005+
1006+
/**
1007+
* If we found a non-Toast element then we can just remove aria-hidden and stop searching entirely
1008+
* since this overlay should always receive focus. As a result, all underlying overlays should still
1009+
* be hidden from screen readers.
1010+
*/
1011+
if (currentOverlay.tagName !== 'ION-TOAST') {
1012+
break;
1013+
}
1014+
}
1015+
};

core/src/utils/test/overlays/overlays.spec.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { newSpecPage } from '@stencil/core/testing';
22

33
import { Modal } from '../../../components/modal/modal';
4+
import { Toast } from '../../../components/toast/toast';
45
import { Nav } from '../../../components/nav/nav';
56
import { RouterOutlet } from '../../../components/router-outlet/router-outlet';
67
import { setRootAriaHidden } from '../../overlays';
@@ -193,4 +194,70 @@ describe('aria-hidden on individual overlays', () => {
193194
await modalOne.present();
194195
expect(modalOne.hasAttribute('aria-hidden')).toEqual(false);
195196
});
197+
198+
it('should not hide previous overlay if top-most overlay is toast', async () => {
199+
const page = await newSpecPage({
200+
components: [Modal, Toast],
201+
html: `
202+
<ion-modal id="m-one"></ion-modal>
203+
<ion-modal id="m-two"></ion-modal>
204+
<ion-toast id="t-one"></ion-toast>
205+
<ion-toast id="t-two"></ion-toast>
206+
`,
207+
});
208+
209+
const modalOne = page.body.querySelector<HTMLIonModalElement>('ion-modal#m-one')!;
210+
const modalTwo = page.body.querySelector<HTMLIonModalElement>('ion-modal#m-two')!;
211+
const toastOne = page.body.querySelector<HTMLIonModalElement>('ion-toast#t-one')!;
212+
const toastTwo = page.body.querySelector<HTMLIonModalElement>('ion-toast#t-two')!;
213+
214+
await modalOne.present();
215+
await modalTwo.present();
216+
await toastOne.present();
217+
await toastTwo.present();
218+
219+
expect(modalOne.hasAttribute('aria-hidden')).toEqual(true);
220+
expect(modalTwo.hasAttribute('aria-hidden')).toEqual(false);
221+
expect(toastOne.hasAttribute('aria-hidden')).toEqual(false);
222+
expect(toastTwo.hasAttribute('aria-hidden')).toEqual(false);
223+
224+
await toastTwo.dismiss();
225+
226+
expect(modalOne.hasAttribute('aria-hidden')).toEqual(true);
227+
expect(modalTwo.hasAttribute('aria-hidden')).toEqual(false);
228+
expect(toastOne.hasAttribute('aria-hidden')).toEqual(false);
229+
230+
await toastOne.dismiss();
231+
232+
expect(modalOne.hasAttribute('aria-hidden')).toEqual(true);
233+
expect(modalTwo.hasAttribute('aria-hidden')).toEqual(false);
234+
});
235+
236+
it('should hide previous overlay even with a toast that is not the top-most overlay', async () => {
237+
const page = await newSpecPage({
238+
components: [Modal, Toast],
239+
html: `
240+
<ion-modal id="m-one"></ion-modal>
241+
<ion-toast id="t-one"></ion-toast>
242+
<ion-modal id="m-two"></ion-modal>
243+
`,
244+
});
245+
246+
const modalOne = page.body.querySelector<HTMLIonModalElement>('ion-modal#m-one')!;
247+
const modalTwo = page.body.querySelector<HTMLIonModalElement>('ion-modal#m-two')!;
248+
const toastOne = page.body.querySelector<HTMLIonModalElement>('ion-toast#t-one')!;
249+
250+
await modalOne.present();
251+
await toastOne.present();
252+
await modalTwo.present();
253+
254+
expect(modalOne.hasAttribute('aria-hidden')).toEqual(true);
255+
expect(toastOne.hasAttribute('aria-hidden')).toEqual(true);
256+
expect(modalTwo.hasAttribute('aria-hidden')).toEqual(false);
257+
258+
await modalTwo.dismiss();
259+
260+
expect(modalOne.hasAttribute('aria-hidden')).toEqual(false);
261+
expect(toastOne.hasAttribute('aria-hidden')).toEqual(false);
262+
});
196263
});

0 commit comments

Comments
 (0)