Skip to content

Rename createShadowRoot to attachShadow #129

Closed

Description

@dglazkov

Given that chrome already shipped createShadowRoot and it's incompatible with the syntax we decided on at our April meeting, we need a new, possibly better name for the same. @rniwa, @hober, and @othermaciej and I briefly discussed this and came up with attachShadow.

Activity

  1. annevk commented on Jul 2, 2015

    @annevk
    Collaborator

    Will we get removeShadow this time around as well?

  2. hayatoito commented on Jul 2, 2015

    @hayatoito
    Contributor

    I'm okay to have removeShadow. However, can we explain a use case for that?

  3. hayatoito commented on Jul 2, 2015

    @hayatoito
    Contributor

    It's easy to have removeShadow in the spec (and the implementation, I think).
    The reason we don't have it is that I've never heard a strong request for that.

    Since multiple shadow roots have been removed, I think it's okay to have removeShadow.

    BTW, having attachShadow and removeShadow sound inconsistent.
    attach/detach sounds consistent, however, ShadowRoot can't live without a host element so far.

    Do you have an interest to have a ShadowRoot without a shadow host?

    My rough idea is:

    var shadowHost = document.createElement('div');
    // shadowHost.shadowRoot == null
    var shadowRoot = document.createShadowRoot();  // New API
    // shadowRoot.host == null
    shadowHost.attachShadow(shadowRoot);  // New API
    // shadowHost.shadowRoot == shadowRoot
    // shadowRoot.host == shadowHost
    var sr = shadowHost.detachShadow();  // New API
    // shadowRoot == sr
    // shadowHost.shadowRoot == null
    // sr.host == null

    Is this better?

    Note that this implies that ShadowRoot instance is no longer tightly associated with particular shadow host in it's lifecycle. We can re-use the same ShadowRoot instance as other shadow host's shadow tree. Attaching a ShadowRoot implies detaching it from the former shadow host, if it's already attached.

  4. dglazkov commented on Jul 2, 2015

    @dglazkov
    ContributorAuthor

    Can we move removeShadow to v2?

  5. annevk commented on Jul 2, 2015

    @annevk
    Collaborator

    Yeah, my bad for raising it here. I do like @hayatoito's idea of reusing ShadowRoot instances, and giving them a constructor.

  6. domenic commented on Jul 3, 2015

    @domenic
    Collaborator

    Constructors, hmm. Good idea.

    As far as I can tell all the essential data about a shadow root is:

    • its host element
    • its mode

    If we say that you can attach and detach shadow roots, then essentially we are saying that the host is mutable. Thus:

    const s = new ShadowRoot({ mode });
    // s is not yet associated to a host
    
    s.host = someHost; // now it is
    s.host = otherHost; // detach from someHost, attach to otherHost
    s.host = null; // now it is not attached to any host
    
    // Note: a side effect of `s.host = x` is setting `x@[[shadowRoot]]` to `s`.
    // (`x@[[shadowRoot]] is notation for what the spec calls "x's shadow root")

    We then say that attachShadow is defined as

    Element.prototype.attachShadow = function (shadowInitDict) {
      // Web IDL argument/this validation here
      const s = new ShadowRoot(shadowInitDict);
      s.host = this;
      return s;
    };

    and a putative detachShadow is

    Element.prototype.detachShadow = function (shadowInitDict) {
      // Web IDL this validation here
    
      const s = this@[[shadowRoot]];
      if (!s) {
        throw new DOMException("blahblahblah", "pickatype");
      }
    
      s.host = null;
      return s;
    };
  7. domenic commented on Jul 3, 2015

    @domenic
    Collaborator

    In case it wasn't clear, that can all be v2 stuff, retrofitted on top of a naive attachShadow definition. Although it is simple enough and nice enough that I am not sure why it wouldn't be v1. I am obviously biased :)

  8. annevk commented on Jul 3, 2015

    @annevk
    Collaborator

    Would also be kind of cool if a shadow root could remain attached across multiple hosts. We'd be getting pretty close to decorators then.

  9. added a commit that references this issue on Sep 7, 2015
    633ff5e
  10. added a commit that references this issue on Jul 30, 2025
    fc96d25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions