Skip to content

Shadow: Specify when slotchange fires #447

Closed

Description

@surma

Safari, Chrome and ShadyDOM currently exhibit different behavior when slotchange does and does not fire. As far as I understand the spec, it is not precisely spec’d, meaning all these current behaviors are technically spec compliant, but can be very frustrating for developers.

Demo 1: Slots are created in the constructor
Demo 2: Slots are creatred in connectedCallback

Which browser fires slotchange under what circumstances?

Slots in constructor, parser creates element Slots in constructor, element gets upgraded Slots in connectedCallback, parser creates element Slots in connectedCallback, element gets upgraded
Chrome Fire No fire Fire No Fire
Safari Fire No fire No Fire No Fire
ShadyDOM Fire Fire Fire Fire

(ShadyDOM was tested using Firefox).

While my main concern is consistency across browsers, I like ShadyDOM’s behavior best in terms of DX as it is consistent with how attributeChangeCallback in that it fires upon initialization. This would allow developers to have their update logic in the slotchange handler and they wouldn’t need to manually detect silently slotted elements in connectedCallback().

/cc @hayatoito

Activity

  1. annevk commented on Apr 26, 2017

    @annevk
    Member

    Why is it not precise?

  2. surma commented on Apr 26, 2017

    @surma
    Author

    @annevk Maybe I am not understanding the spec, that is entirely possible. If it is specced, do you mind telling me which browser is behaving correctly so I can file bugs against the others?

  3. surma commented on Apr 26, 2017

    @surma
    Author

    However, maybe we can have a discussion if it is possible/feasible to change the spec to the behavior I describe below or help me understand why that is not a good idea™️

  4. annevk commented on Apr 26, 2017

    @annevk
    Member

    As far as I can tell the specification consistently fires slotchange however the tree was created. Meaning only "ShadyDOM" is correct (though I haven't looked at the timing). If your understanding is different you'll have to tell me how you arrived at that conclusion.

  5. hayatoito commented on Apr 26, 2017

    @hayatoito
    Member

    I think the following chromium bug is related:
    https://bugs.chromium.org/p/chromium/issues/detail?id=696659

    I believe that the current Blink's behavior satisfies spec-conformance.
    As I mentioned in the bug, I guess what is being proposed here is:

    From #229 (comment)

    When inserting a slot, we set a suppress signaling flag.

    If we change it to:

    When inserting a slot, we do not set a suppress signaling flag.

    Then, I think the spec's behavior matches that of Shady DOM.

  6. annevk commented on Apr 26, 2017

    @annevk
    Member

    See WICG/webcomponents#288 (comment) by @rniwa for why we suppress in that case. I'm okay with changing this if you're all agreed (including @rniwa) we should change this.

    Anyone interested in writing tests?

  7. surma commented on Apr 26, 2017

    @surma
    Author

    Just want to reemphasize that I am arguing from a developer experience perspective here, and that the current state is unexpected (and inconvenient).

    I’d actually be happy to take this opportunity learn to write tests and help out that way. I’m sure one of my colleagues could help me get started here. (@domenic?)

  8. annevk commented on Apr 26, 2017

    @annevk
    Member

    I appreciate you filing the issue. I just wasn't sure at first what the problem was, but @hayatoito stepping through the algorithms in the Chrome bug made it more clear.

  9. annevk commented on Apr 26, 2017

    @annevk
    Member

    http://web-platform-tests.org/ has documentation on writing tests. It's probably worth looking at existing tests once you've got the framework running locally as the easiest way to figure out how to write them.

  10. domenic commented on Apr 26, 2017

    @domenic
    Member

    The other thing to do is check through the existing tests to make sure none of them test the currently-specced behavior; if they do they need to change.

    Happy to help more over IRC or Hangouts or whatever once I'm in the office (~an hour or so). Although those docs should help you get started.

  11. surma commented on Apr 26, 2017

    @surma
    Author

    I opened a PR for a web-platform-tests that tests for the ShadyDOM behavior. Pending consensus in this issue :)

  12. hayatoito commented on Apr 27, 2017

    @hayatoito
    Member

    See WICG/webcomponents#288 (comment) by @rniwa for why we suppress in that case.

    Thanks. I need some time on investigating how Blink can fire a event in such a case. I thought that is doable, but I am not 100% sure. I need to do an experiment.

    I think the interoperability is the most important here. I am open to any opinions.

  13. hayatoito commented on May 10, 2017

    @hayatoito
    Member

    I have roughly implemented the behavior which I think is being proposed; when inserting a slot, and one or more slotables are assigned to the slot at this timing, we fire slotchange on the slot.
    As far as I experimented, Blink doesn't have any significant difficulties to implement this behavior. I can say Blink is okay to change the spec and implement it so that Blink can fire slotchange for Demo2 as well as for Demo1 in #447 (comment).

    If we can agree this change, I am happy to send a PR to DOM Standard, write a WPT, and update Blink's behavior. I think backward compatibility risk is very low.

  14. rniwa commented on May 10, 2017

    @rniwa

    WebKit's behavior is not to fire slotchange on a newly created slot when the slot got assigned of nodes as it got inserted into the tree as I mentioned in WICG/webcomponents#288 (comment). I don't know Chrome fires only in demo1 but not in demo2.

    Here's my thought.

    1. Other DOM events such as change don't fire when the initial value is set.
    2. A well written component probably have an efficient code path for when it's constructing shadow tree instead of relying on slotchange event

    Firing slotchange event when slot is inserted would make (2) harder because now we have to remember to ignore the very first slotchange being fired. So I'm still slightly inclined towards not firing for the initial insertion.

  15. surma commented on May 10, 2017

    @surma
    Author

    Note: While preparing a demo for this reply, I found out that things become even more inconsistent depending on whether you create your slots in the constructor or connectedCallback. I augmented the original table!

    Slots in constructor, parser creates element Slots in constructor, element gets upgraded Slots in connectedCallback, parser creates element Slots in connectedCallback, element gets upgraded
    Chrome Fire No fire Fire No Fire
    Safari Fire No fire No Fire No Fire
    ShadyDOM Fire Fire Fire Fire

    @hayatoito Thanks for investigating. Very glad to hear that the behavior is technically possible and feasible.

    @rniwa As I said, my main goal is consistency, with a preference for changing the behavior to always firing slotchange. Let me address these two things separately:

    Consistency

    The behavior of slotchange depends on:

    • Whether you define your element before or after using the element (upgrade vs parser creation)
    • Whether you define your slots in the constructor or connectedCallback

    Unless you are intimately familiar with the intricacies of Custom Elements and ShadowDOM, this is going to trip up and annoy developers. When developing generic components, you might write code that works fine during development, but because users include your CE script at a different point in the page, things suddenly stop working.

    A similar argument can be made for slotchange behaving differently depending on whether you create the slot in the constructor or in connectedCallback.

    Always firing

    Take a look at this demo, which is a stripped down version of a tab panel component I am working on. I’m using ShadowDOM to “reshuffle” the children as a technique for graceful degradation. If JS is disabled, the tabs function as headings for the different sections. So in the markup tabs and panels are interleaved. I use ShadowDOM do group tabs and panels.

    I need to be notified about new elements because I need to generate IDs and semantically link tabs and panels using aria-controls and aria-labelledby. Right now, I’d have to manually call into the linking code in the connectedCallback() in addition to linking it to the slotchange event.

    I am aware that CE lifecycle callbacks are something different from ShadowDOM events, but the fact that attributeChangedCallback does get called for initialization purposes and slotchange does not, is confusing and somewhat unexpected. A MutationObserver would help, but ShadowDOM already has an internal MO so I think it’d be nice to be able to make use of that (apart from clearing up the aforementioned inconsistency with attributeChangeCallback).

  16. 45 remaining items

  17. hayatoito commented on Jun 5, 2017

    @hayatoito
    Member

    FYI: PR for web platform tests is web-platform-tests/wpt#5954

  18. added 2 commits that reference this issue on Jun 29, 2017
    32e6669
    e1db083
  19. added a commit that references this issue on Jul 4, 2017
    8f8c1c3
  20. trusktr commented on Oct 10, 2017

    @trusktr

    @hayatoito

    Is slotchange for that really a useful event for web developers?

    It's more useful for library authors.

    In real worlds, does removing slots (or its one of ancestors) from the tree happen frequently?

    It's easy to imagine scenarios. Using our imagination or example, imagine a game. When the user's monster character gets to a higher skill level, it earns an extra hand. Objects like guns or tools could be placed into this new hand (i.e. slotted into this new slot). If the character is injured, maybe it's hand gets severed from battle, then the hand is removed (the slot is removed but the perhaps the gun remains in inventory).

    <monster-character position="20 40 50" rotation="0 40 0">
      <weapon-rifle slot="hand-1">
      </weapon-rifle>
      <weapon-pistol slot="hand-2">
      </weapon-pistol>
      <weapon-sword slot="hand-3">
      </weapon-sword>
    </monster-character>

    It would be great if we can see a concrete example of the current usage of a slotchange event listener. Do you know that?

    In infamous, I use slotchange to detect the composed tree. Then I can traverse the composed tree to render a WebGL scene. Traversing the light tree is (obviously) naive, and will not work when users decide to compose my custom elements using shadow dom.

    A-Frame mentioned they don't intend to support Shadow DOM... But, if they did... they would need to understand the composed tree in order to render that with Three.js. slotchange will be very handy for them if they want to add this feature now or in the future.

  21. trusktr commented on Oct 10, 2017

    @trusktr

    I forgot to add, slotchange events could be used, for example, to properly connect or disconnect Three.js THREE.Object3D instances from each other if those, for example, were used to render <monster-character>. Simply relying on parentNode or watching children with MutationObserver won't work, the scene won't be rendered correctly unless we consider the composed tree.

  22. trusktr commented on Oct 10, 2017

    @trusktr

    At the moment, in infamous, I'm observing addition/removal of slots with MO, and using slotchange for while the slots are in DOM (my impl is here and here and some cases still need to be handled).

    If slotchanged fired when slots were removed and the removed nodes were observable, I might just move to using only slotchange and no MO.

    This is my perspective as someone using Web Components. If I could easily detect distributed and undistributed nodes with only a slotchange event, that might be more helpful, but I'm not completely sure yet.

    It seems to me that it might be more helpful in some cases to have a distributedchildren event on shadow roots, and in the handler we could detect which slots elements went to and which slots elements were undistributed from (including being able to observe which parent a slot was removed from).

    A Custom Element (a reusable component) owns its whole shadow root, so it would be okay to have shadow root events like this, because the whole logic is still encapsulated inside the Custom Element.

    The event of "elements being distributed into a shadow root" seems great for more generic whole-shadow-root logic, and slotchange could still be useful for fine tuning specific logic to specific areas of the tree.

  23. annevk commented on Oct 10, 2017

    @annevk
    Member

    @trusktr if you want a change in behavior it would be much better to file a new issue. We're not tracking closed issues.

  24. trusktr commented on Oct 10, 2017

    @trusktr

    @annevk I don't have a new issue per se, I'm just pointing out use cases that might be useful to consider (because @hayatoito had asked, but not many were provided).

    After the changes in #459, how does the above table change? Is everything Fire now?

  25. hayatoito commented on Oct 11, 2017

    @hayatoito
    Member

    @trusktr Blink has already implemented the spec change. You can confirm the new behavior in Blink. If you find a bug, I would appreciate if you can file a bug for Blink.

  26. dejan9393 commented on Nov 14, 2017

    @dejan9393

    If anyone else is looking for a workaround for this issue on Safari, try checking for slot.assignedNodes().length in the connectedCallback

  27. trusktr commented on Nov 14, 2017

    @trusktr

    I will test soon on my end when I circle back to ShadowDOM support for my lib. What should I expect from the changes? Is everything in the above table Fire, or are some of them still expected to be No Fire?

  28. added a commit that references this issue on Sep 3, 2025
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions