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

support configurable shadowroot attribute for <template> tags #65

Open
thescientist13 opened this issue Jun 12, 2022 · 0 comments
Open
Labels
feature New feature or request question Further information is requested
Milestone

Comments

@thescientist13
Copy link
Member

thescientist13 commented Jun 12, 2022

Type of Change

  • New Feature Request

Summary

Coming out of #63 (comment), just wanted to track the discussion around how to actually support the shadowroot attribute for <template> tags as I'm not super clear on is how instances of <template> tags (HTMLTemplateElement) and ShadowRoot work together, specifically around the shadowroot="open" attribute and mode.

Specifically, as per that PR, when creating a template, a template returns its contents inside <template> tags, eg.

<wcc-footer>
  <template shadowroot="open">
    <footer class="footer">
      <h4>My Blog &copy; 2022</h4>
    </footer>
  </template>
</wcc-footer>

And when instantiating a Shadow Root in a custom element, we can set the mode that way, e.g.

class Footer extends HTMLElement {
  connectedCallback() {
    if (!this.shadowRoot) {
      this.attachShadow({ mode: 'open' });
      this.shadowRoot.appendChild(template.content.cloneNode(true));
    }
  }
}

So how does a template get access to the mode, which we would want so as to properly set the shadowroot attribute? Presumably some users may want the option of open / closed, and this may also impact how we implement #16. Also, these two are the same things, right?

Details

My understanding is shadowRoot and HTMLTemplateElement are different class hierarchies (sub classes) from what I understand, so not sure what option to pursue here? I can think of a couple options at least just to get the ball rolling

  1. User sets the attribute manually on a <template>
    const template = document.createElement('template');
    
    template.innerHTML = `    
      <footer class="footer">
        <h4>My Blog &copy; ${new Date().getFullYear()}</h4>
      </footer>
    `;
    
    template.setAttribute('shadowroot', 'open');
  2. Have the compiler check for mode when serializing and update it that way
    async function renderComponentRoots(tree) {
      for (const node of tree.childNodes) {
        if (node.tagName && node.tagName.indexOf('-') > 0) {
          ...
          
          const elementHtml = elementInstance.shadowRoot
            ? elementInstance.getInnerHTML({ includeShadowRoots: true })
            : elementInstance.innerHTML;
          const elementTree = parseFragment(elementHtml);
    
          // if we have a shadowRoot, set the `<template>` shadowroot attribute according to shadowRoot.mode
          if(elementInstance.shadowRoot) {
            const mode = elementInstance.shadowRoot.mode;
            //  do something with the tree and set the attribute on the `<template>` accordingly
          }
    
          ...
        }
    
        ...
      }
    
      return tree;
    }

I'm thinking option #2 seems pretty feasible. Also, not sure if this means we need a get innerHTML for HTMLTemplateElement too?

@thescientist13 thescientist13 added feature New feature or request question Further information is requested labels Jun 12, 2022
@thescientist13 thescientist13 added this to the 1.0 milestone Jun 12, 2022
@thescientist13 thescientist13 changed the title support configurable shadow-root attribute on <template> tags support configurable shadowroot attribute for <template> tags Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant