Skip to content

fix(cdk-experimental/dialog): focus restoration not working inside shadow dom #23194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/cdk-experimental/dialog/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ ng_module(
"//src/cdk/bidi",
"//src/cdk/keycodes",
"//src/cdk/overlay",
"//src/cdk/platform",
"//src/cdk/portal",
"@npm//@angular/animations",
"@npm//@angular/core",
Expand All @@ -38,6 +39,7 @@ ng_test_library(
"//src/cdk/bidi",
"//src/cdk/keycodes",
"//src/cdk/overlay",
"//src/cdk/platform",
"//src/cdk/testing/private",
"@npm//@angular/common",
"@npm//@angular/platform-browser",
Expand Down
5 changes: 3 additions & 2 deletions src/cdk-experimental/dialog/dialog-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import {animate, AnimationEvent, state, style, transition, trigger} from '@angular/animations';
import {FocusTrapFactory} from '@angular/cdk/a11y';
import {_getFocusedElementPierceShadowDom} from '@angular/cdk/platform';
import {
BasePortalOutlet,
CdkPortalOutlet,
Expand Down Expand Up @@ -228,7 +229,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
/** Saves a reference to the element that was focused before the dialog was opened. */
private _savePreviouslyFocusedElement() {
if (this._document) {
this._elementFocusedBeforeDialogWasOpened = this._document.activeElement as HTMLElement;
this._elementFocusedBeforeDialogWasOpened = _getFocusedElementPierceShadowDom();
}
}

Expand Down Expand Up @@ -259,7 +260,7 @@ export class CdkDialogContainer extends BasePortalOutlet implements OnDestroy {
}
});
} else {
const activeElement = this._document.activeElement;
const activeElement = _getFocusedElementPierceShadowDom();

// Otherwise ensure that focus is on the dialog container. It's possible that a different
// component tried to move focus while the open animation was running. See:
Expand Down
41 changes: 38 additions & 3 deletions src/cdk-experimental/dialog/dialog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
NgModule,
TemplateRef,
ViewChild,
ViewContainerRef
ViewContainerRef,
ViewEncapsulation
} from '@angular/core';
import {By} from '@angular/platform-browser';
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
Expand All @@ -26,11 +27,12 @@ import {Directionality} from '@angular/cdk/bidi';
import {CdkDialogContainer} from './dialog-container';
import {OverlayContainer} from '@angular/cdk/overlay';
import {A, ESCAPE} from '@angular/cdk/keycodes';
import {_supportsShadowDom} from '@angular/cdk/platform';
import {
dispatchKeyboardEvent,
createKeyboardEvent,
dispatchEvent,
} from '../../cdk/testing/private';
} from '@angular/cdk/testing/private';
import {DIALOG_DATA, Dialog, DialogModule, DialogRef} from './index';

describe('Dialog', () => {
Expand Down Expand Up @@ -935,6 +937,32 @@ describe('Dialog', () => {
document.body.removeChild(button);
}));

it('should re-focus trigger element inside the shadow DOM when dialog closes', fakeAsync(() => {
if (!_supportsShadowDom()) {
return;
}

viewContainerFixture.destroy();
const fixture = TestBed.createComponent(ShadowDomComponent);
fixture.detectChanges();
const button = fixture.debugElement.query(By.css('button'))!.nativeElement;

button.focus();

const dialogRef = dialog.openFromComponent(PizzaMsg);
flushMicrotasks();
fixture.detectChanges();
flushMicrotasks();

const spy = spyOn(button, 'focus').and.callThrough();
dialogRef.close();
flushMicrotasks();
fixture.detectChanges();
tick(500);

expect(spy).toHaveBeenCalled();
}));

it('should not move focus if it was moved outside the dialog while animating', fakeAsync(() => {
// Create a element that has focus before the dialog is opened.
const button = document.createElement('button');
Expand Down Expand Up @@ -1220,6 +1248,12 @@ class DialogWithInjectedData {
@Component({template: '<p>Pasta</p>'})
class DialogWithoutFocusableElements {}

@Component({
template: `<button>I'm a button</button>`,
encapsulation: ViewEncapsulation.ShadowDom
})
class ShadowDomComponent {}

// Create a real (non-test) NgModule as a workaround for
// https://github.com/angular/angular/issues/10760
const TEST_DIRECTIVES = [
Expand All @@ -1230,7 +1264,8 @@ const TEST_DIRECTIVES = [
ComponentWithOnPushViewContainer,
ContentElementDialog,
DialogWithInjectedData,
DialogWithoutFocusableElements
DialogWithoutFocusableElements,
ShadowDomComponent,
];

@NgModule({
Expand Down