Skip to content

Commit c0eb79e

Browse files
thetaPCShaneK
andauthored
fix(overlays): keep focus inside modal and popover when tabbing in Chrome 157+ (#31755)
Issue number: internal ## What is the current behavior? In Chrome 157 and later, pressing Tab on the last focusable element of a presented `ion-modal` or `ion-popover` moves focus out of the overlay. Chrome now lets Tab move focus off the end of the page without firing a `focus` event, so the document focus listener that traps focus never runs. Alert, action sheet and loading are not affected because they already render focusable sentinels around their content. ## What is the new behavior? - Modal and popover render a focusable boundary before and after their content. A boundary moves focus to the first or last focusable element, so Tab wraps without focus leaving the page. - Alert, action sheet and loading use the same shared boundary in place of their own sentinels, and the scoped focus trap in `overlays.ts` no longer wraps focus itself. - Boundaries are hidden while the focus trap is off (`focusTrap="false"`, or a sheet modal below its `backdropBreakpoint`), so focus can still leave the overlay. - Shift+Tab from the alert wrapper now wraps to the last button. Previously it moved to the first button. ## Does this introduce a breaking change? - [ ] Yes - [x] No ## Other information - Test on localhost, not the Vercel preview. The Vercel toolbar's floating button sits after the overlay and catches the Tab, which hides the bug on `main`. - Unblocks the Playwright 1.65 update in #31714, where the Mobile Chrome focus trap tests fail on this Chrome change. - In Firefox, the first Tab into a popover now lands on the native `textarea` inside `ion-textarea` instead of the `ion-textarea` host, which matches Chrome and Safari. The extra Firefox-only Tab in the popover textarea test was removed. - Checked with VoiceOver in Safari and Chrome: Tab wraps announce only the newly focused element, and the VoiceOver cursor never stops on a boundary, the same as on `main`. --------- Co-authored-by: ShaneK <561207+ShaneK@users.noreply.github.com>
1 parent f8f25bc commit c0eb79e

12 files changed

Lines changed: 378 additions & 54 deletions

File tree

‎core/src/components/action-sheet/action-sheet.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Gesture } from '@utils/gesture';
44
import { createButtonActiveGesture } from '@utils/gesture/button-active';
55
import { raf } from '@utils/helpers';
66
import { createLockController } from '@utils/lock-controller';
7+
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
78
import {
89
BACKDROP,
910
cleanupRootFocusTrapAccessibility,
@@ -657,7 +658,7 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
657658
>
658659
<ion-backdrop tappable={this.backdropDismiss} />
659660

660-
<div tabindex="0" aria-hidden="true"></div>
661+
<OverlayFocusBoundary overlay={this.el} edge="start" />
661662

662663
<div class="action-sheet-wrapper ion-overlay-wrapper" ref={(el) => (this.wrapperEl = el)}>
663664
<div class="action-sheet-container">
@@ -707,7 +708,7 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
707708
</div>
708709
</div>
709710

710-
<div tabindex="0" aria-hidden="true"></div>
711+
<OverlayFocusBoundary overlay={this.el} edge="end" />
711712
</Host>
712713
);
713714
}

‎core/src/components/alert/alert.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { raf } from '@utils/helpers';
77
import { createLockController } from '@utils/lock-controller';
88
import { printIonWarning } from '@utils/logging';
99
import { getOverlayLabelJustify, getOverlayLabelPlacement } from '@utils/overlay-control-label';
10+
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
1011
import {
1112
BACKDROP,
1213
cleanupRootFocusTrapAccessibility,
@@ -913,7 +914,7 @@ export class Alert implements ComponentInterface, OverlayInterface {
913914
>
914915
<ion-backdrop tappable={this.backdropDismiss} />
915916

916-
<div tabindex="0" aria-hidden="true"></div>
917+
<OverlayFocusBoundary overlay={this.el} edge="start" />
917918

918919
<div
919920
class="alert-wrapper ion-overlay-wrapper"
@@ -951,7 +952,7 @@ export class Alert implements ComponentInterface, OverlayInterface {
951952
{this.renderAlertButtons()}
952953
</div>
953954

954-
<div tabindex="0" aria-hidden="true"></div>
955+
<OverlayFocusBoundary overlay={this.el} edge="end" />
955956
</Host>
956957
);
957958
}

‎core/src/components/loading/loading.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Watch, Component, Element, Event, Host, Method, Prop, h } from '@stenci
33
import { ENABLE_HTML_CONTENT_DEFAULT } from '@utils/config';
44
import { raf } from '@utils/helpers';
55
import { createLockController } from '@utils/lock-controller';
6+
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
67
import {
78
BACKDROP,
89
cleanupRootFocusTrapAccessibility,
@@ -367,7 +368,7 @@ export class Loading implements ComponentInterface, OverlayInterface {
367368
>
368369
<ion-backdrop visible={this.showBackdrop} tappable={this.backdropDismiss} />
369370

370-
<div tabindex="0" aria-hidden="true"></div>
371+
<OverlayFocusBoundary overlay={this.el} edge="start" />
371372

372373
<div class="loading-wrapper ion-overlay-wrapper">
373374
{spinner && (
@@ -379,7 +380,7 @@ export class Loading implements ComponentInterface, OverlayInterface {
379380
{message !== undefined && this.renderLoadingMessage(msgId)}
380381
</div>
381382

382-
<div tabindex="0" aria-hidden="true"></div>
383+
<OverlayFocusBoundary overlay={this.el} edge="end" />
383384
</Host>
384385
);
385386
}

‎core/src/components/modal/modal.scss‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ ion-backdrop {
111111
background: transparent;
112112
}
113113

114+
/**
115+
* Removes the focus boundaries from the tab order
116+
* so focus can leave the modal when the trap is off.
117+
*/
118+
:host(.ion-disable-focus-trap) .ion-overlay-focus-boundary {
119+
display: none;
120+
}
121+
114122
@media only screen and (min-width: $modal-inset-min-width) and (min-height: $modal-inset-min-height-small) {
115123
:host {
116124
--width: #{$modal-inset-width};

‎core/src/components/modal/modal.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import type { Attributes } from '@utils/helpers';
77
import { createLockController } from '@utils/lock-controller';
88
import { printIonWarning } from '@utils/logging';
99
import { Style as StatusBarStyle, StatusBar } from '@utils/native/status-bar';
10+
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
1011
import {
1112
BACKDROP,
1213
cleanupRootFocusTrapAccessibility,
@@ -1760,6 +1761,8 @@ export class Modal implements ComponentInterface, OverlayInterface {
17601761

17611762
{mode === 'ios' && <div class="modal-shadow"></div>}
17621763

1764+
<OverlayFocusBoundary overlay={this.el} edge="start" />
1765+
17631766
<div
17641767
/*
17651768
role and aria-modal must be used on the
@@ -1794,6 +1797,8 @@ export class Modal implements ComponentInterface, OverlayInterface {
17941797
)}
17951798
<slot onSlotchange={this.onSlotChange}></slot>
17961799
</div>
1800+
1801+
<OverlayFocusBoundary overlay={this.el} edge="end" />
17971802
</Host>
17981803
);
17991804
}

‎core/src/components/popover/popover.scss‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@
7171
z-index: $z-index-overlay-wrapper;
7272
}
7373

74+
/**
75+
* Removes the focus boundaries from the tab order
76+
* so focus can leave the popover when the trap is off.
77+
*/
78+
:host(.ion-disable-focus-trap) .ion-overlay-focus-boundary {
79+
display: none;
80+
}
81+
7482
.popover-content {
7583
display: flex;
7684
position: absolute;

‎core/src/components/popover/popover.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { CoreDelegate, attachComponent, detachComponent } from '@utils/framework
55
import { addEventListener, raf, hasLazyBuild } from '@utils/helpers';
66
import { createLockController } from '@utils/lock-controller';
77
import { printIonWarning } from '@utils/logging';
8+
import { OverlayFocusBoundary } from '@utils/overlay-focus-boundary';
89
import {
910
BACKDROP,
1011
cleanupRootFocusTrapAccessibility,
@@ -795,12 +796,16 @@ export class Popover implements ComponentInterface, PopoverInterface {
795796
>
796797
{!parentPopover && <ion-backdrop tappable={this.backdropDismiss} visible={this.showBackdrop} part="backdrop" />}
797798

799+
<OverlayFocusBoundary overlay={this.el} edge="start" />
800+
798801
<div class="popover-wrapper ion-overlay-wrapper" onClick={dismissOnSelect ? () => this.dismiss() : undefined}>
799802
{enableArrow && <div class="popover-arrow" part="arrow"></div>}
800803
<div class="popover-content" part="content">
801804
<slot></slot>
802805
</div>
803806
</div>
807+
808+
<OverlayFocusBoundary overlay={this.el} edge="end" />
804809
</Host>
805810
);
806811
}

‎core/src/components/popover/test/basic/popover.e2e.ts‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,6 @@ configs({ modes: ['ios'], directions: ['ltr'] }).forEach(({ title, config }) =>
222222

223223
await page.keyboard.press(tabKey);
224224

225-
// for Firefox, ion-textarea is focused first
226-
// need to tab again to get to native input
227-
if (browserName === 'firefox') {
228-
await page.keyboard.press(tabKey);
229-
}
230-
231225
await expect(innerNativeTextarea).toBeFocused();
232226

233227
await page.keyboard.press('ArrowDown');
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { FunctionalComponent } from '@stencil/core';
2+
import { h } from '@stencil/core';
3+
import { focusFirstDescendant, focusLastDescendant } from '@utils/focus-trap';
4+
5+
interface OverlayFocusBoundaryProps {
6+
/** The overlay host element. */
7+
overlay: HTMLElement;
8+
/** Whether the boundary renders before or after the overlay content. */
9+
edge: 'start' | 'end';
10+
}
11+
12+
/**
13+
* Moves focus off a boundary. Focus arriving from inside the overlay
14+
* wraps to the opposite end. Focus arriving from outside enters at the
15+
* nearest end.
16+
*/
17+
const onBoundaryFocus = (overlay: HTMLElement, ev: FocusEvent, edge: 'start' | 'end') => {
18+
/**
19+
* Shadow overlays search their slotted content. Scoped overlays search
20+
* their wrapper, since the boundaries are siblings of it in the same
21+
* tree and would otherwise be found first.
22+
*/
23+
const container = overlay.shadowRoot ? overlay : overlay.querySelector<HTMLElement>('.ion-overlay-wrapper');
24+
if (!container) {
25+
return;
26+
}
27+
28+
const from = ev.relatedTarget as Node | null;
29+
const fromInside =
30+
from !== null && from !== overlay && (overlay.contains(from) || overlay.shadowRoot?.contains(from) === true);
31+
const focusFirst = edge === 'start' ? !fromInside : fromInside;
32+
33+
if (focusFirst) {
34+
focusFirstDescendant(container, overlay);
35+
} else {
36+
focusLastDescendant(container, overlay);
37+
}
38+
};
39+
40+
/**
41+
* Renders a focusable element before or after an overlay's content that
42+
* keeps Tab inside the overlay. The document focus listener in overlays.ts
43+
* cannot do this alone, since browsers can move focus past the end of the
44+
* page without firing a focus event.
45+
*/
46+
export const OverlayFocusBoundary: FunctionalComponent<OverlayFocusBoundaryProps> = ({ overlay, edge }) => (
47+
<div
48+
class="ion-overlay-focus-boundary"
49+
tabIndex={0}
50+
aria-hidden="true"
51+
onFocus={(ev) => onBoundaryFocus(overlay, ev, edge)}
52+
></div>
53+
);

‎core/src/utils/overlays.ts‎

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,9 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => {
248248

249249
/**
250250
* Otherwise, we must be focusing an element
251-
* inside of the overlay. The two possible options
252-
* here are an input/button/etc or the ion-focus-trap
253-
* element. The focus trap element is used to prevent
254-
* the keyboard focus from leaving the overlay when
255-
* using Tab or screen assistants.
251+
* inside of the overlay.
256252
*/
257253
} else {
258-
/**
259-
* We do not want to focus the traps, so get the overlay
260-
* wrapper element as the traps live outside of the wrapper.
261-
*/
262-
263254
const overlayRoot = getElementRoot(lastOverlay);
264255
if (!overlayRoot.contains(target)) {
265256
return;
@@ -276,41 +267,12 @@ const trapKeyboardFocus = (ev: Event, doc: Document) => {
276267
* Additionally, if the backdrop was tapped we should not
277268
* move focus back inside the wrapper as that could cause
278269
* an interactive elements focus state to activate.
270+
*
271+
* The focus boundaries outside of the wrapper move
272+
* focus back inside on their own.
279273
*/
280274
if (overlayWrapper.contains(target) || target === overlayRoot.querySelector('ion-backdrop')) {
281275
lastOverlay.lastFocus = target;
282-
} else {
283-
/**
284-
* Otherwise, we must have focused one of the focus traps.
285-
* We need to wrap the focus to either the first element
286-
* or the last element.
287-
*/
288-
289-
/**
290-
* Once we call `focusFirstDescendant` and focus the first
291-
* descendant, another focus event will fire which will
292-
* cause `lastOverlay.lastFocus` to be updated before
293-
* we can run the code after that. We will cache the value
294-
* here to avoid that.
295-
*/
296-
const lastFocus = lastOverlay.lastFocus;
297-
298-
// Focus the first element in the overlay wrapper
299-
focusFirstDescendant(overlayWrapper, lastOverlay);
300-
301-
/**
302-
* If the cached last focused element is the
303-
* same as the active element, then we need
304-
* to wrap focus to the last descendant. This happens
305-
* when the first descendant is focused, and the user
306-
* presses Shift + Tab. The previous line will focus
307-
* the same descendant again (the first one), causing
308-
* last focus to equal the active element.
309-
*/
310-
if (lastFocus === doc.activeElement) {
311-
focusLastDescendant(overlayWrapper, lastOverlay);
312-
}
313-
lastOverlay.lastFocus = doc.activeElement as HTMLElement;
314276
}
315277
}
316278
};

0 commit comments

Comments
 (0)