Skip to content

Dialog focus trap not working when clicking on backdrop #18799

Closed
@kurushimi0

Description

@kurushimi0

Bug, feature request, or proposal:

Bug

What is the expected behavior?

Focus should stay in the dialog when disableClose is true

What is the current behavior?

Clicking on the backdrop of a modal dialog and then hitting Shift-Tab allows a user to escape the focus trap of a Material Dialog whose disableClose property is set to true.

What are the steps to reproduce?

  1. Open a modal dialog, where the "disableClose" property of the "MatDialogConfig" is set to true.
  2. Click on the backdrop. (The dialog won't close, which is proper behavior.)
  3. Press "Shift-Tab" on the keyboard several times, and you'll notice that the focus is hitting elements behind the dialog.

What is the use-case or motivation for changing an existing behavior?

Need to be able to open a truly modal dialog that does not allow a user to escape the focus trap in an uncontrolled manner.

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

@angular/material 9.1.2

Is there anything else we should know?

As a less-than-ideal workaround, I'm able to subscribe to the "backdropClick" observable via "MatDialogRef". If I've injected a "ViewContainerRef" instance into the component, then I can grab the "ElementRef.nativeElement" for the component, run a "querySelector" to find the first focusable element, and then call the "focus()" function on any such element that I find.

That code looks like this:
if (matDialogConfig.disableClose)
{
matDialogRef.backdropClick().subscribe((event: MouseEvent) => {
//console.log("Called backdropClick subscription", event, matDialogRef);
const element: any = matDialogRef.componentInstance.viewContainerRef.element.nativeElement;
//console.log("Element for dialog component", element);
const querySelector: string = button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"]);
const elements_selectable: any = element.querySelector(querySelector);
//console.log("Selectable elements", elements_selectable);
if (elements_selectable)
{
const element_target: any = Array.isArray(elements_selectable) ? elements_selectable[0] : elements_selectable;
//console.log("Target element", element_target);
element_target.focus();
}
});
}

Metadata

Metadata

Assignees

Labels

P3An issue that is relevant to core functions, but does not impede progress. Important, but not urgent

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions