-
Notifications
You must be signed in to change notification settings - Fork 446
Return focus when dialog is closed to element focused before open #4095
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
base: dev
Are you sure you want to change the base?
Conversation
Just because I'm curious: what happens if I open the dialog via code somehow? Which element will be focused then? For example in my case I open a modal directly in |
I would say nothing happens then. I think this is done when the code from @dvoituron 's last review comment gets added |
Whatever element that is currently focused at the time that |
|
||
return await Task.Run(async () => | ||
{ | ||
var previouslyFocusedElement = await _module.InvokeAsync<IJSObjectReference>("getActiveElement"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You will find in the documentation: "If the focused element is within a document tree that's not descended from the current document (for example, the focused element is in the main document, and the invoking document is an embedded iframe), then this will be null."
https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
So, previouslyFocusedElement can be null and you need to manage this case in DialogInstance
and all methods called.
@@ -22,6 +25,8 @@ public DialogInstance(Type? type, DialogParameters parameters, object content) | |||
|
|||
public DialogParameters Parameters { get; internal set; } | |||
|
|||
internal IJSObjectReference PreviouslyFocusedElement { get; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This property must be nullable. See my other comment
This new feature should be added to the “dev-v5” branch for the new Dialog component. |
Pull Request
📖 Description
Exposes two functions to get the focused element and set focus in
dialog-utils.js
. Uses that in the dialog provider to get the focused element before the dialog is created (which messes with focus), and then to reset focus after close. Dialog doesn't always close immediately, so I added a small delay.Before:
Grabacion.de.pantalla.2025-08-29.a.la.s.1.05.00.a.m.mov
After:
Grabacion.de.pantalla.2025-08-29.a.la.s.1.06.55.a.m.mov
🎫 Issues
Fixes #4094
👩💻 Reviewer Notes
You can test this using the Dialog samples.
📑 Test Plan
✅ Checklist
General
Component-specific
⏭ Next Steps