components icon indicating copy to clipboard operation
components copied to clipboard

fix(material/dialog): autofocus should wait for rendering to complete

Open atscott opened this issue 2 years ago • 1 comments

The current implementation of the dialog's finishDialogOpen, which includes autofocus, only waits for a microtask before executing. This only works in the tests because of the synchronous call to detectChanges after opening the dialog, ensuring that change detection runs before the microtask. In an application, this is not guaranteed because NgZone will only run change detection when the microtask queue empties, which includes the microtask created by the dialog container.

This commit updates the implementation to wait for the next render to coplete, then queues a microtask that will finish the open process and perform autofocus.

atscott avatar Dec 26 '23 20:12 atscott

LGTM but I suspect that this will be pretty breaking during the TGP.

Yea, definitely a concern I have as well.

atscott avatar Dec 26 '23 20:12 atscott

Closing - after more investigation, the issue with focus is actually the focus trap, not the dialog promise here. afterNextRender should be added to the focus trap instead of the dialog code here.

atscott avatar Jan 02 '24 18:01 atscott

Reopening - This is somewhat the responsibility of the dialog and I think the focus-trap is a separate issue. The dialog has control over what APIs in the focus trap it's calling, so it can instead call focusInitialElement rather than focusInitialElementWhenReady. Adding blocked label to wait until https://github.com/angular/angular/pull/52455 is merged and rerun TGP

atscott avatar Jan 03 '24 22:01 atscott