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

mousedown on a light DOM element should be able to focus on a shadow container #773

Closed
JanMiksovsky opened this issue Nov 14, 2018 · 11 comments

Comments

@JanMiksovsky
Copy link

JanMiksovsky commented Nov 14, 2018

We've run into some inconsistent and surprising behavior in the way implicit focus-on-mousedown behavior works across the shadow boundary.

Consider a custom element whose shadow contains a focusable element that contains a slot:

<test-element>
  #shadow-root
    <div tabindex="0">
      <slot></slot>
    </div>
</test-element>

And suppose this custom element receives some light DOM children:

<test-element>
  <span>Click me</span>
</test-element>

Repro: https://jsbin.com/qivoheg/edit

What happens if the user clicks inside the "Click me" span in the light DOM?

As far as we can determine, the standard default behavior of a mousedown event is to focus on the nearest focusable ancestor of the event target — but should that nearest ancestor take into account the composed tree?

  • In Firefox, the focus appears to travel up the composed tree. Even when the user clicks a light DOM child of the custom element, Firefox finds the focusable container of the slot inside the custom element's shadow and puts the focus there. This feels correct.
  • In Chrome and Safari, the focus only travels up the light DOM tree. Hence, clicks on a light DOM child do not give focus to the custom element, and the focus is lost (the document body gets the focus). This feels surprising.

We find ourselves needing to normalize the behavior of mousedown to achieve Firefox's default behavior in all browsers so that our components can properly support the keyboard and ARIA.

Here we're focusing on the question of how mousedown should implicitly set keyboard focus. As far as we can tell, this issue is distinct from earlier discussions of tab navigation through focusable shadow elements, and from whether focusing on a shadow host should be able to delegate focus to an inner focusable element.

We had originally thought the issue was limited to Chrome, and filed a related Chromium bug. Now that we've hit the same issue in Safari, we're wondering if this could be addressed more formally.

@rniwa
Copy link
Collaborator

rniwa commented Nov 14, 2018

That definitely sounds like a bug in Safari & Chrome. Focus in general isn't well spec'ed in HTML (I think the person who was working on that at Google has since left), but Gecko's behavior makes a lot more sense to me.

@JanMiksovsky
Copy link
Author

@rniwa Thanks. Shall I file a WebKit bug, or would you prefer to do that?
@hayatoito From your comments on the linked Chromium bug, it sounds like you support fixing this as well. Can you confirm?

@rniwa
Copy link
Collaborator

rniwa commented Nov 14, 2018

Yeah, it would be great if you can file a bug. That way, you can follow along the progress we make.

@hayatoito
Copy link
Contributor

@JanMiksovsky Yeah, I think we should clarify the behavior in the spec sooner or later, however, I agree that we should align the behavior with Firefox in this case.

cc: @rakina

@JanMiksovsky
Copy link
Author

@rniwa Filed as https://bugs.webkit.org/show_bug.cgi?id=191694.

@JanMiksovsky
Copy link
Author

@hayatoito The Chromium and WebKit bugs are now tracking the actual fixes, which is what I'm interested in. I'll let you decide whether you want to leave this issue here open (to track the need to update the spec) or, since we're all in agreement, would prefer to close this issue (and track the spec update somewhere else).

@hayatoito
Copy link
Contributor

@JanMiksovsky Yeah, I think the motivation you filed an issue here would be to let Chromium and Blink fix the implementations. :) I think you succeeded well. Let me close the issue, though we might want to track the spec update somewhere, as you mentioned.

@rniwa
Copy link
Collaborator

rniwa commented Nov 16, 2018

I think we should keep this issue open we have a relevant spec fixed.

@rniwa rniwa reopened this Nov 16, 2018
@hayatoito
Copy link
Contributor

hayatoito commented Nov 16, 2018

I think this should be tracked on whatwg/html, rather than w3c/webcomponents, as part of whatwg/html#2013, or other issues about focus there.

@hayatoito
Copy link
Contributor

@JanMiksovsky

Just FYI.

As README.md file in this repository says:

Please file issues in the most specific repository possible,

It would be better to file an issue for whatwg/html. Basically, please don't use this webcomponents repository for any new issue. The most spec work should be done in whatwg/dom or whatwg/html.

@rniwa
Copy link
Collaborator

rniwa commented Nov 16, 2018

That's fine. I think we just need to file an issue probably in https://github.com/whatwg/html/issues/, and then reference that issue, and close this one.

aarongable pushed a commit to chromium/chromium that referenced this issue Feb 6, 2019
Previously mouse-triggered focusing on an element (e.g. clicking) uses
repeated calls of ParentOrShadowElement() instead of using flat-tree
traversal methods, causing it to go up its shadow-including ancestors
instead of flat-tree. This behavior is not specified, but other
browser vendors had implemented flat-tree traversal usage for focus
traversal.

See: WICG/webcomponents#773

Bug: 894931
Change-Id: I5666c9e973480648e8e9a1774e5d6abe026aac5c
Reviewed-on: https://chromium-review.googlesource.com/c/1455839
Reviewed-by: Hayato Ito <hayato@chromium.org>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629515}
aarongable pushed a commit to chromium/chromium that referenced this issue Mar 11, 2019
This reverts commit 169c2ef.

Reason for revert: Causes crbug.com/939546, and fix for selection seems to be non-trivial.

Original change's description:
> Make mouse-triggered focus use flat tree traversal
> 
> Previously mouse-triggered focusing on an element (e.g. clicking) uses
> repeated calls of ParentOrShadowElement() instead of using flat-tree
> traversal methods, causing it to go up its shadow-including ancestors
> instead of flat-tree. This behavior is not specified, but other
> browser vendors had implemented flat-tree traversal usage for focus
> traversal.
> 
> See: WICG/webcomponents#773
> 
> Bug: 894931
> Change-Id: I5666c9e973480648e8e9a1774e5d6abe026aac5c
> Reviewed-on: https://chromium-review.googlesource.com/c/1455839
> Reviewed-by: Hayato Ito <hayato@chromium.org>
> Reviewed-by: Kent Tamura <tkent@chromium.org>
> Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#629515}

TBR=hayato@chromium.org,tkent@chromium.org,rakina@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 894931
Change-Id: I215fe3f98c6117cd2bff8bd0930f3b8e380646f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1514433
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Commit-Position: refs/heads/master@{#639420}
imran1008 pushed a commit to bloomberg/chromium.bb that referenced this issue May 21, 2019
This reverts commit 91faf4f [formerly 169c2efa93aa145fde63d95c7f3788fe7625f9e7].

Reason for revert: Causes crbug.com/939546, and fix for selection seems to be non-trivial.

Original change's description:
> Make mouse-triggered focus use flat tree traversal
>
> Previously mouse-triggered focusing on an element (e.g. clicking) uses
> repeated calls of ParentOrShadowElement() instead of using flat-tree
> traversal methods, causing it to go up its shadow-including ancestors
> instead of flat-tree. This behavior is not specified, but other
> browser vendors had implemented flat-tree traversal usage for focus
> traversal.
>
> See: WICG/webcomponents#773
>
> Bug: 894931
> Change-Id: I5666c9e973480648e8e9a1774e5d6abe026aac5c
> Reviewed-on: https://chromium-review.googlesource.com/c/1455839
> Reviewed-by: Hayato Ito <hayato@chromium.org>
> Reviewed-by: Kent Tamura <tkent@chromium.org>
> Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#629515}

TBR=hayato@chromium.org,tkent@chromium.org,rakina@chromium.org

# Not skipping CQ checks because original CL landed > 1 day ago.

Bug: 894931, 939546
Change-Id: I215fe3f98c6117cd2bff8bd0930f3b8e380646f9
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1514433
Reviewed-by: Rakina Zata Amni <rakina@chromium.org>
Reviewed-by: Kent Tamura <tkent@chromium.org>
Commit-Queue: Rakina Zata Amni <rakina@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#639420}(cherry picked from commit 5e1e1aa [formerly 5310256007c2d3a77fdf520d2eb2e1d06a04a8d3])
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1519508
Cr-Commit-Position: refs/branch-heads/3729@{#74}
Cr-Branched-From: 1926073 [formerly d4a8972e30b604f090aeda5dfff68386ae656267]-refs/heads/master@{#638880}
Former-commit-id: 7d66bdef48057ade68b30b85d2ebd7d6b2b8d2d6
georgimkv added a commit to SAP/ui5-webcomponents that referenced this issue Jul 26, 2021
WICG/webcomponents#773

On Chrome, clicking on slotted elements (light DOM) in the
dialog (shadow DOM) causes the focus to travel up the light DOM tree to
reach the closes element that is focusable.
Which ends to be the body element as document.activeElement.

This is not the case for FF & Safari, where the focus travels up the
slot, reaching the dialog root which is actually the closest focusable element.

This change introduces a workaround to align the focus behaviour in Chrome
with the other browsers.

Fixes #3466
ilhan007 pushed a commit to SAP/ui5-webcomponents that referenced this issue Aug 3, 2021
On Chrome, clicking on slotted elements (light DOM) in the
dialog (shadow DOM) causes the focus to travel up the light DOM tree to
reach the closes element that is focusable.
Which ends to be the body element as document.activeElement.

This is not the case for FF & Safari, where the focus travels up the
slot, reaching the dialog root which is actually the closest focusable element.

This change introduces a workaround to align the focus behaviour in Chrome
with the other browsers.

Related issue: WICG/webcomponents#773
Fixes #3466
ilhan007 pushed a commit to SAP/ui5-webcomponents that referenced this issue Aug 3, 2021
On Chrome, clicking on slotted elements (light DOM) in the
dialog (shadow DOM) causes the focus to travel up the light DOM tree to
reach the closes element that is focusable.
Which ends to be the body element as document.activeElement.

This is not the case for FF & Safari, where the focus travels up the
slot, reaching the dialog root which is actually the closest focusable element.

This change introduces a workaround to align the focus behaviour in Chrome
with the other browsers.

Related issue: WICG/webcomponents#773
Fixes #3466
@rniwa rniwa closed this as completed Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants