Bug 236322 - Dialog's "previously focused element" and focus-visible
Summary: Dialog's "previously focused element" and focus-visible
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: Safari Technology Preview
Hardware: Mac (Intel) macOS 12
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks: dialog-element
  Show dependency treegraph
 
Reported: 2022-02-08 12:48 PST by Darin Senneff
Modified: 2022-02-15 09:30 PST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Darin Senneff 2022-02-08 12:48:20 PST
When closing a <dialog> element, focus is only moved back to the "previously focused element" if a keyboard was used to initially show the dialog. I'm assuming this has to do with the focus-visible algorithm, as a click doesn't normally put visible focus on a button. 

But because the <dialog> autofocuses a button upon being opened, it's possible users will then use the keyboard to close the modal rather than click – by pressing the ENTER, SPACE, or ESC keys. In these cases where the user has changed their input method from click/tap to keyboard, should the previous element now receive focus? 

I think regardless of the input method used to close a modal, focus should be placed on the previous element to help re-orient the user. But at minimum I think it should in cases where a keyboard was used to close the <dialog>.

Looking at the spec's dialog focusing steps, this edge case seems beyond that. But think there's value in it.
Comment 1 Manuel Rego Casasnovas 2022-02-09 07:51:37 PST
Hi, thanks for the bug report.

If you're testing this on Mac, <button> are not mouse focusable on Mac (see https://bugs.webkit.org/show_bug.cgi?id=229895 for more details).

So I guess that when you close the dialog, if it was opened by a button, it's not going to be focused when you close it.

I've tested on WebKitGTK (Linux), and the button that opens the dialog gets focused when I close it.

If the problem is unrelated to the buttons not being click focusable on Mac, could you please create a test case reproducing the issue?
Comment 2 Darin Senneff 2022-02-09 08:01:50 PST
Yup, I get the non-focusable button if the <dialog> is opened and closed with a mouse. And that if its opened and closed with a keyboard that the button does get focus. 

But what if the <dialog> is opened with a mouse but closed with a keyboard? Since the element autofocuses a button upon opening (and can be closed with the ESC key), it's likely that some users will switch to a keyboard after originally opening the <dialog> with their mouse/finger. In those cases, doesn't it make sense to put keyboard focus to the button?
Comment 3 Radar WebKit Bug Importer 2022-02-15 09:30:38 PST
<rdar://problem/88969727>