-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Move the focus back to the previous focused element for dialog.close() #5678
Comments
What is "the previously focused element"? |
I mean the element that was focused before the dialog showed up. |
What if that element has been made un-focusable (removed, made inert, etc.)? What about if it has moved to another location or reused for a different purpose entirely (e.g. via a virtual DOM framework)? In general, this would be the first time that the specification focuses an element without explicit web developer request or user interaction, so it raises all sorts of unprecedented questions. |
Using https://software.hixie.ch/utilities/js/live-dom-viewer/ I played with variants on And is it really that different from showing the dialog running focusing steps? (Note that I'm not sure this should be limited to |
Yes, it is true that this re-focusing of the element that triggered the dialog is true for any kind of closing the dialog. See also the WAI-ARIA design pattern for role "dialog". It could be argued that dialog is a complex enough compound widget, with modality, anyway, so this is the first of its kind in the HTML specification, and it would also be the first of its kind to request that focus be set back to the triggering element if still possible. The alternative would be the body element, which would require keyboard users to tab a cillion times to get back to the element that triggered the dialog, and completely lose context in the meantime. It would eliminate a very common accessibility mistake if browsers would do this automatically if the triggering element is still focusable. |
As for implementer interest, Mozilla is interested. @hober @cookiecrook there was some talk about WebKit eventually adopting @mfreed7 what about Chrome? |
It's always been recommended that authors move focus back to the "trigger" element (the element whose action caused the dialog or menu to be displayed). This is is consistent with platform conventions, too. For example, on popup buttons like However, perhaps it's too simple to spec the "trigger" as the "previously focused element"? I think most of the time, the previously focused element is the same as the trigger, but there may be some scenarios where it's not. For example, if something is clicked and doesn't received focus from the click, then the "previously focused element" would be whatever was focused before the click. I don't know how common it would be. I'm just trying to consider all scenarios. |
As for WebKit's opinion, there has been support expressed for the |
(Updated: Re-posting this musing as a standalone comment to make it clear it's not an official WebKit position.) In line with that, perhaps this "focus the previously focused element" idea should not be tied to the |
#4937 (comment) is what I was referring to above with regards to interest from WebKit. |
IIRC, there were some outstanding issues with dialog positioning yet to be addressed by CSS WG. @smfr may recall if there are any remaining blockers. |
You're probably thinking of w3c/csswg-drafts#4645, @cookiecrook. |
For what it's worth, this is how the jQuery UI Dialog works.
- https://api.jqueryui.com/dialog/ The jQuery UI Dialog also keeps focus within itself, so you can't tab out of it to something in the background (and if it's a modal dialog you also can't click out of it). |
I remain weakly opposed to this for the reasons stated above, but @mfreed7 would be the ultimate implementer. I think it would be ideal if Firefox produced a proposed processing model and tests to answer about how the various edge cases I mentioned are handled. |
I generally agree, but I would tie it to the "modal top layer" idea rather than inertness. There was some discussion of this in this thread from 2016: #897 (later in that thread) That also points to a since-deleted "focus fixup rule 3" - does anyone know what happened to that? |
@cookiecrook @alice does either of you expect to make progress on a new focus concept around inert/top layer? Otherwise, it seems like a simple and effective way forward here would be to record the previously focused element on the dialog element itself and restore focus there (if still focusable) once the dialog is closed. A broader version of focus scoping can maybe build on top of that in the future? |
I don't have any short terms plans to work on that.
I don't see a problem with this approach being the default behavior for dialog. Seems reasonably forward-compatible to me, even if it's reopened for improvement later via #897 or otherwise. I'm speculating that there may be a timing issue though… If the "trigger" is inert (or otherwise disabled/inactive) while dialog is displayed, how long should the UA give the author to re-enable it (timeout for attempted re-focus?) or should the autofocus behavior be limited to UAs only? |
Just wanted to +1 on general implementer interest from another Chromium contributor (MS Edge). Providing focus rewind as a default—with simple author control if a different experience is required—seems likes a win for both authors and users assuming we can work around the identified issues. |
#4633 began a discussion around top layer APIs, I'm not sure whether it was continued elsewhere. I would love to eventually figure out the interactions between inert, top layer, dialog, blocking elements, and focus fixup/replaying. In the meantime I agree with @annevk and @cookiecrook that we could specify something sensible for dialog and generalise it as we go. I like the idea of restoring focus to the previously focused element, although it's worth noting that in browsers which don't always focus elements on click (i.e. browsers not based on chromium), the previously focused element may not be the element that triggered the dialog. Perhaps we should also specify that we should not scroll to the element that gets focused as a result of a dialog closing, to avoid a confusing experience in that case. I'm not sure I understand the timing issue you refer to though, James. If we're talking about steps for hiding a dialog, won't the UA be doing the focusing? And if we're not talking about that, what are we talking about? |
Gecko's work will be tracked at https://bugzilla.mozilla.org/show_bug.cgi?id=1660271. We'll start with I think what James' concern was, how do the authors move the focus upon modal dialog closing because the element that they want to move could be in So the order of steps is If I am missing something, please let me know! |
Alice wrote:
That's what I was trying to clarify with the comment "or should the autofocus behavior be limited to UAs only?" I don't think there will be an issue if inertness and auto-re-focus behavior is limited to UAs only. I was thinking of some hypothetical scenario where the author also modified the DOM at the time that the dialog was presented (a DOM change that might break the auto-re-focus behavior). Should the UA provide some non-blocking way to do the focusing after the author has had a chance to do any necessary DOM fix-up at the time of the dialog close? It's probably a non-issue that can be addressed when/if a more concrete example arises. In the meantime, authors should probably achieve this by performing any necessary DOM fix-up prior to |
That order may be enough... but the duplicate focus events in quick succession would potentially be noticed by AT users. The hypothetical scenario I was thinking of might be more like this:
|
Yeah, fair point, we don't want to move the focus twice.
It introduces a new event which we hope that we can avoid.
The issue is the Can we dispatch the @alice @mfreed7 What do you think the above changing for |
cc @smfr |
@alice @mfreed7 @cookiecrook @smfr ping. Would be nice to make progress here! |
It's very strange that |
It doesn’t sound like anyone is opposing making the close event sync instead of async, so I suppose I could implement it in chrome along with the other focus change proposed here. I don’t know why it is async and I don’t know if changing it will break any websites, but I could say the same thing about the proposed focus change and I won’t be sure until I try. Are there any WPTs for this behavior yet? |
The usual danger with sync events is outlined in https://w3ctag.github.io/design-principles/#guard-against-recursion . I'm not sure whether any of that reasoning applies here; I think it would require code like |
(There is a small risk with regards to eventually removing support for mutation events (or changing when they fire, at least), but if we manage that we could certainly move this to CEReactions timing then too, I'd think.) |
Add a new `previously focused` element to dialog as a pointer to the current focused element when `dialog.showModal()` and `dialog.show()` are called, such that `dialog.close()` can use it to restore the focus. See whatwg#5678 for more details.
Add a new `previously focused` element to dialog as a pointer to the current focused element when `dialog.showModal()` and `dialog.show()` are called, such that `dialog.close()` can use it to restore the focus. See whatwg#5678 for more details.
While implementing this in chrome, it was pointed out that changing focus synchronously, which fires an event, will make us have to write the code somewhat carefully (making sure that changing focus is the very last thing we do). Was it ever considered to change focus asynchronously, or would that break anything? Am I missing anything on this topic? |
Is it materially different from calling |
The intent to manage focus of a closing dialog is a good one. However, if A common example: a dialog created for the "confirm delete" action with Possible solutions:
|
@AutoSponge thank you! Could you file that as a new issue please? Comments in closed issues tend to get lost. |
From an accessibility pointer of view,
dialog.close()
should move the focus back to the previously focused element to providegood accessibility.
The text was updated successfully, but these errors were encountered: