Skip to content

Should delegatesFocus use the flat tree? #7207

Closed

Description

@domenic

See discussion in #7079. Including the effects of that PR, focusing and unfocusing (e.g. via focus() and blur()) a delegatesFocus shadow host uses the flat tree: given

<!doctype html>
<div id="host">
  <div tabindex=0>In light tree</div>
</div>
<script>
  let host = document.getElementById("host");
  host.attachShadow({ mode: "open", delegatesFocus: true }).innerHTML = `
    <slot></slot>
    <div tabindex=0>In shadow tree</div>
  `;
  host.focus();
</script>

the <div tabindex=0>In light tree</div> will be focused, and if you insert host.blur() afterward, the div will be blurred.

@emilio states

I find delegating focus to stuff outside the shadow tree a bit weird, because you call focus(), the focus changes, but activeElement is not the host, and the host doesn't match :focus either.

I'm not sure why delegatesFocus was specified to look for focusable elements using the flat tree rather than the shadow-including tree of the shadow root, seems really weird.

which I tend to agree with. Recall that the main use cases for delegatesFocus are things like <input type="date"> where you want to focus things in the shadow DOM, not the light DOM.

The alternate proposal is that delegatesFocus should use the shadow tree. In this case, in the above example, the <div tabindex=0>In shadow tree</div> would get focus delegated to it, and document.activeElement would be the shadow host, and :focus would match the host.

Activity

  1. added
    topic: shadow
    on Oct 12, 2021
  2. added
    topic: focus
    on Oct 15, 2021
  3. added
    agenda+
    on Oct 15, 2021
  4. domenic commented on Oct 15, 2021

    @domenic
    MemberAuthor

    @mfreed7, do you find the arguments here convincing? I think we should probably use the shadow tree instead of the flat tree, for both blur() and focus(); we could try to find some web developers to ask, but based on the known use cases for delegatesFocus I think shadow tree is correct.

  5. mfreed7 commented on Oct 19, 2021

    @mfreed7
    Contributor

    Yeah, I do find them a bit convincing. But as I mentioned, I'd feel better getting some input from developers that use delegatesFocus. Have we heard of any use cases that involve focusing slotted light dom content? I would hate to resolve this in favor of using the shadow root's shadow-including subtree, change browser behavior, and then find out what the critical use case was for instead using the flat tree.

    Absent any such developer input, I suppose I could be convinced by the above arguments. I do think it sounds a bit funny to focus light dom slotted content. So if we're in a hurry to resolve this one way or the other, I'm ok changing Chromium's focus() to not use the flat tree.

  6. emilio commented on Oct 19, 2021

    @emilio
    Contributor

    I'm not on a massive rush other than "the longer we wait to change this, the easier it is for people to depend on it"

  7. domenic commented on Oct 20, 2021

    @domenic
    MemberAuthor

    Trying to get developer feedback seems fair. I think you're more connected to the web components developer-using community than I am these days, so could you take point on that?

    Edit: I also tweeted, to do my part :) https://twitter.com/domenic/status/1450919182850211849

    I don't think we're in a big hurry, but it does stand out as a wart. And I got the impression it was hurting Firefox's progress on shipping some stuff?

  8. nemzes commented on Oct 20, 2021

    @nemzes

    I'm my limited experience using WCs, this change seems to make sense and is in line with other isolation models. If I want to focus/blur some slotted content I should be able to do that regardless of the shadow DOM by keeping a reference to the slotted light DOM. I see delegatesFocus as part of the API into the WC, not the slotted content.

  9. WickyNilliams commented on Oct 21, 2021

    @WickyNilliams

    This changes makes sense to me. I've only dabbled with delegatesFocus, due to lack of broad browser support, but i would have been surprised to discover that it can delegate to slotted content. I specifically want this to delegate to my shadow elements, that's the benefit of the feature imo

  10. bathos commented on Oct 21, 2021

    @bathos

    I'd feel better getting some input from developers that use delegatesFocus

    Just one data point but I have used it a fair amount and was under the impression that the proposed behavior was already how it worked. I either got lucky and just never had slots prior to the first in-shadow focusable or possibly just learned I have some bugs I don't know about!

  11. emilio commented on Oct 21, 2021

    @emilio
    Contributor

    Seems there's no great compelling argument for the current behavior then... I'm happy to update the WPTs and Gecko. @domenic can / do you want to fix the spec? It'd probably take me a bit more than you, but if you're busy I can try :-)

  12. emilio commented on Oct 21, 2021

    @emilio
    Contributor
  13. domenic commented on Oct 21, 2021

    @domenic
    MemberAuthor

    Sure, I'm happy to fix the spec. I will do it by expanding the scope of #7079.

  14. mfreed7 commented on Oct 21, 2021

    @mfreed7
    Contributor

    Thanks for tweeting, @domenic. Based on seemingly universal support for not including slotted content, I also agree. Let's land it.

    Thanks, I appreciate you gathering some additional input!

  15. added a commit that references this issue on Oct 28, 2021
    8615199
  16. added 3 commits that reference this issue on Nov 1, 2021
    9ba7124
    63b3e9d
    6b24119
  17. added 2 commits that reference this issue on Dec 4, 2021
    302559e
    24d0840
  18. added 2 commits that reference this issue on Jun 3, 2022
    5524251
    5180d9c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agenda+topic: focustopic: shadow

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions