From 8246112ca12f90edb98629ab82e27a792a1fafad Mon Sep 17 00:00:00 2001 From: Liam DeBeasi Date: Tue, 22 Feb 2022 08:56:30 -0500 Subject: [PATCH] fix(toast): toast is now correctly excluded from focus trapping (#24816) resolves #24733 --- core/src/components/toast/toast.tsx | 4 +- core/src/utils/overlays.ts | 30 +++-------- core/src/utils/test/overlays/index.html | 37 +++++++++++++- core/src/utils/test/overlays/overlays.e2e.ts | 53 +++++++++++++++++++- core/src/utils/test/utils.ts | 19 +++++++ 5 files changed, 115 insertions(+), 28 deletions(-) diff --git a/core/src/components/toast/toast.tsx b/core/src/components/toast/toast.tsx index b917422421..6d519a35f4 100644 --- a/core/src/components/toast/toast.tsx +++ b/core/src/components/toast/toast.tsx @@ -140,9 +140,7 @@ export class Toast implements ComponentInterface, OverlayInterface { @Event({ eventName: 'ionToastDidDismiss' }) didDismiss!: EventEmitter; connectedCallback() { - prepareOverlay(this.el, { - trapKeyboardFocus: false - }); + prepareOverlay(this.el); } /** diff --git a/core/src/utils/overlays.ts b/core/src/utils/overlays.ts index 588c5517e6..2bf66d6c6f 100644 --- a/core/src/utils/overlays.ts +++ b/core/src/utils/overlays.ts @@ -31,16 +31,10 @@ export const pickerController = /*@__PURE__*/createController('ion-popover'); export const toastController = /*@__PURE__*/createController('ion-toast'); -export interface OverlayListenerOptions { - trapKeyboardFocus: boolean; -} - -export const prepareOverlay = (el: T, options: OverlayListenerOptions = { - trapKeyboardFocus: true -}) => { +export const prepareOverlay = (el: T) => { /* tslint:disable-next-line */ if (typeof document !== 'undefined') { - connectListeners(document, options); + connectListeners(document); } const overlayIndex = lastId++; el.overlayIndex = overlayIndex; @@ -119,7 +113,7 @@ const focusLastDescendant = (ref: Element, overlay: HTMLIonOverlayElement) => { * Should NOT include: Toast */ const trapKeyboardFocus = (ev: Event, doc: Document) => { - const lastOverlay = getOverlay(doc); + const lastOverlay = getOverlay(doc, 'ion-alert,ion-action-sheet,ion-loading,ion-modal,ion-picker,ion-popover'); const target = ev.target as HTMLElement | null; /** @@ -256,22 +250,12 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => { } }; -const connectListeners = (doc: Document, options: OverlayListenerOptions) => { +const connectListeners = (doc: Document) => { if (lastId === 0) { lastId = 1; - if (options.trapKeyboardFocus) { - doc.addEventListener('focus', (ev: FocusEvent) => { - /** - * ion-menu has its own focus trapping listener - * so we do not want the two listeners to conflict - * with each other. - */ - if (ev.target && (ev.target as HTMLElement).tagName === 'ION-MENU') { - return; - } - trapKeyboardFocus(ev, doc); - }, true); - } + doc.addEventListener('focus', (ev: FocusEvent) => { + trapKeyboardFocus(ev, doc); + }, true); // handle back-button click doc.addEventListener('ionBackButton', ev => { diff --git a/core/src/utils/test/overlays/index.html b/core/src/utils/test/overlays/index.html index 63ece09198..862dece6f4 100644 --- a/core/src/utils/test/overlays/index.html +++ b/core/src/utils/test/overlays/index.html @@ -10,8 +10,9 @@ @@ -30,18 +31,27 @@ + + Text Input + + + Create a Modal Create Nested Overlay Modal Present a Hidden Modal Create and Present a Modal Simulate Hardware Back Button + Create and Present Toast