Skip to content

Commit 37ea30f

Browse files
committed
test(ac): improve focus trapping
1 parent f2984ab commit 37ea30f

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,27 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
253253
return eventMethod(this.el, 'ionActionSheetWillDismiss');
254254
}
255255

256+
private focusFirstElement() {
257+
if (!this.wrapperEl) return; // Check if wrapperEl is defined
258+
const firstFocusable = this.wrapperEl?.querySelector(
259+
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
260+
);
261+
if (firstFocusable) {
262+
(firstFocusable as HTMLElement).focus();
263+
}
264+
}
265+
266+
// Method to focus the last focusable element
267+
private focusLastElement() {
268+
if (!this.wrapperEl) return; // Check if wrapperEl is defined
269+
const focusableElements = this.wrapperEl.querySelectorAll(
270+
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
271+
);
272+
if (focusableElements.length > 0) {
273+
(focusableElements[focusableElements.length - 1] as HTMLElement).focus();
274+
}
275+
}
276+
256277
private async buttonClick(button: ActionSheetButton) {
257278
const role = button.role;
258279
if (isCancel(role)) {
@@ -385,7 +406,7 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
385406
>
386407
<ion-backdrop tappable={this.backdropDismiss} />
387408

388-
<div tabindex="0"></div>
409+
<div tabindex="0" aria-hidden="true" onFocus={() => this.focusLastElement()}></div>
389410

390411
<div class="action-sheet-wrapper ion-overlay-wrapper" ref={(el) => (this.wrapperEl = el)}>
391412
<div class="action-sheet-container">
@@ -446,7 +467,7 @@ export class ActionSheet implements ComponentInterface, OverlayInterface {
446467
</div>
447468
</div>
448469

449-
<div tabindex="0"></div>
470+
<div tabindex="0" aria-hidden="true" onFocus={() => this.focusFirstElement()}></div>
450471
</Host>
451472
);
452473
}

core/src/components/backdrop/backdrop.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export class Backdrop implements ComponentInterface {
5151
const mode = getIonMode(this);
5252
return (
5353
<Host
54-
tabindex="-1"
5554
aria-hidden="true"
5655
class={{
5756
[mode]: true,

0 commit comments

Comments
 (0)