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

Imperative Shadow DOM Distribution API #409

Closed
yuzhe-han opened this issue Jul 28, 2020 · 2 comments · Fixed by #422
Closed

Imperative Shadow DOM Distribution API #409

yuzhe-han opened this issue Jul 28, 2020 · 2 comments · Fixed by #422
Labels
position: positive venue: WHATWG Specifications in a WHATWG Workstream

Comments

@yuzhe-han
Copy link

Request for Mozilla Position on an Emerging Web Specification

Other information

The imperative Shadow DOM distribution API allows developers to explicitly set the assigned nodes for a slot element. With this API, web developers can create web components without needing to add specific markup, slot="" attribute, to children of host component. In addition, it enables conditional slotting based on either environmental state or an attribute passed in.

More details, including more motivating uses cases, can be found in the explainer.

The behavior of this API has been discussed in the last TPAC F2F back in Sept, 2019. The proposal has been implemented in Chrome and we would like to get an official stance from Mozzila.

Example syntax:

<custom-tab show-tab="2">
   <tab-panel></tab-panel>
   <tab-panel></tab-panel>
   <tab-panel></tab-panel>
</custom-tab>
class CustomTab extends HTMLElement {
    static get observedAttributes() {
      return ['show-tab'];
    }
    constructor() {
        super();
        const shadowRoot = this.attachShadow({mode: 'open', slotAssignment: 'manual'});
        shadowRoot.innerHTML = `
            <div class="custom-tab">
                <slot></slot>
            </div>`;
    }
    attributeChangedCallback(name, oldValue, newValue) {
        UpdateDisplayTab(this, newValue);
    }
    connectedCallback() {
        if (!this._observed) {
           const target = this;
           const showTab = this.getAttribute('show-tab');
           const observer = new MutationObserver(function(mutations) {
                UpdateDisplayTab(target, showTab);
            });
            observer.observe(this, {childList: true});
            this._observed = true;
        }
    }
}

function  UpdateDisplayTab(elem, tabIdx) {
    const shadow = elem.shadowRoot;
    const slot = shadow.querySelector("slot");
    const panels = elem.querySelectorAll('tab-panel');
    if (panels.length && tabIdx && tabIdx <= panels.length ) {
      slot.assign([panels[tabIdx-1]]);
    } else {
      slot.assign([]);
    }
}
@annevk annevk added the venue: WHATWG Specifications in a WHATWG Workstream label Aug 5, 2020
@smaug----
Copy link
Collaborator

smaug---- commented Aug 6, 2020

I think the proposal looks reasonable and should be relatively easy to implement.
And when looking at w3ctag/design-reviews#486 (comment), I think it is even good that the proposal doesn't include too many new features at once. If some default slot mechanism is needed, it could be added later.
So I think this is worth prototyping.

@yuzhe-han
Copy link
Author

@smaug---- ,
Thanks for reviewing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
position: positive venue: WHATWG Specifications in a WHATWG Workstream
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants