Skip to content

Commit b6712f4

Browse files
committed
(nav): blur active element before page transitions
1 parent 3598b91 commit b6712f4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

core/src/components/nav/nav.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,19 @@ export class Nav implements NavOutlet {
146146
this.destroyed = false;
147147
}
148148

149+
/**
150+
* Blur the active element before any transition between pages
151+
* to prevent a11y issues since active element could be kept inside
152+
* an element that will be set with aria-hidden="true" and/or inert
153+
* which makes the element non-interactive.
154+
*/
155+
blurActiveElement() {
156+
const activeElement = document.activeElement as HTMLElement | null;
157+
if (activeElement) {
158+
activeElement.blur();
159+
}
160+
}
161+
149162
disconnectedCallback() {
150163
for (const view of this.views) {
151164
lifecycle(view.element!, LIFECYCLE_WILL_UNLOAD);
@@ -179,6 +192,7 @@ export class Nav implements NavOutlet {
179192
opts?: NavOptions | null,
180193
done?: TransitionDoneFn
181194
): Promise<boolean> {
195+
this.blurActiveElement();
182196
return this.insert(-1, component, componentProps, opts, done);
183197
}
184198

@@ -200,6 +214,7 @@ export class Nav implements NavOutlet {
200214
opts?: NavOptions | null,
201215
done?: TransitionDoneFn
202216
): Promise<boolean> {
217+
this.blurActiveElement();
203218
return this.insertPages(insertIndex, [{ component, componentProps }], opts, done);
204219
}
205220

@@ -239,6 +254,7 @@ export class Nav implements NavOutlet {
239254
*/
240255
@Method()
241256
pop(opts?: NavOptions | null, done?: TransitionDoneFn): Promise<boolean> {
257+
this.blurActiveElement();
242258
return this.removeIndex(-1, 1, opts, done);
243259
}
244260

@@ -273,6 +289,7 @@ export class Nav implements NavOutlet {
273289
*/
274290
@Method()
275291
popToRoot(opts?: NavOptions | null, done?: TransitionDoneFn): Promise<boolean> {
292+
this.blurActiveElement();
276293
return this.removeIndex(1, -1, opts, done);
277294
}
278295

@@ -316,6 +333,7 @@ export class Nav implements NavOutlet {
316333
opts?: NavOptions | null,
317334
done?: TransitionDoneFn
318335
): Promise<boolean> {
336+
this.blurActiveElement();
319337
return this.setPages([{ component, componentProps }], opts, done);
320338
}
321339

@@ -514,6 +532,8 @@ export class Nav implements NavOutlet {
514532
* @returns Whether the transition succeeds.
515533
*/
516534
private async queueTrns(ti: TransitionInstruction, done: TransitionDoneFn | undefined): Promise<boolean> {
535+
this.blurActiveElement();
536+
517537
if (this.isTransitioning && ti.opts?.skipIfBusy) {
518538
return false;
519539
}

0 commit comments

Comments
 (0)