Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

atscott
Copy link
Contributor

@atscott atscott commented Dec 26, 2023

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.

Copy link
Member

@crisbeto crisbeto left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

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
Copy link
Contributor Author

atscott commented Dec 26, 2023

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

Yea, definitely a concern I have as well.

@devversion devversion removed their request for review January 2, 2024 11:26
@atscott
Copy link
Contributor Author

atscott commented Jan 2, 2024

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 atscott closed this Jan 2, 2024
@atscott atscott added the blocked This issue is blocked by some external factor, such as a prerequisite PR label Jan 3, 2024
@atscott atscott reopened this Jan 3, 2024
@atscott
Copy link
Contributor Author

atscott commented Jan 3, 2024

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 angular/angular#52455 is merged and rerun TGP

@atscott atscott requested a review from jelbourn as a code owner January 3, 2024 22:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: material/dialog blocked This issue is blocked by some external factor, such as a prerequisite PR
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants