fix(material/dialog): autofocus should wait for rendering to complete
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.
LGTM but I suspect that this will be pretty breaking during the TGP.
Yea, definitely a concern I have as well.
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.
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