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

Based on the clock example I made a quick polyfill (not complete as light-DOM slots are not supported) #16

Open
LionsAd opened this issue May 19, 2022 · 1 comment

Comments

@LionsAd
Copy link

LionsAd commented May 19, 2022

<!DOCTYPE html>

<shadowed-element>
    <template shadowroot=open>
        <!-- This is the SSR content -->
        <style>
          div {
            font-size:24pt;
            color: blue;
          }
        </style>
        <div>
          <span id=hour>12</span> : <span id=min>34</span> : <span id=sec>56</span>
        </div>
    </template>
</shadowed-element>

<script>
  customElements.define('shadowed-element', class extends HTMLElement {
    constructor() {
      super();
      let shadowRoot = null;
    
      try {
        const internals = this.attachInternals();
        if (internals.shadowRoot) {
          shadowRoot = internals.shadowRoot;
        }
      }
      catch (e) {
        
      }

      if (!shadowRoot) {
        var template = this.querySelector('template');
        shadowRoot = this.attachShadow({mode: 'open'});
        shadowRoot.innerHTML = template.innerHTML;
      }
      this.hours = shadowRoot.querySelector('#hour');
      this.minutes = shadowRoot.querySelector('#min');
      this.seconds = shadowRoot.querySelector('#sec');
    }


    connectedCallback() {
      this.update();
      this.interval = setInterval(() => this.update(), 1000);
    }

    disconnectedCallback() {
      clearInterval(this.interval);
    }

    update() {
      const pad = v => {return String(v).padStart(2,'0')}
      const d = new Date();
      this.hours.textContent = pad(d.getHours());
      this.minutes.textContent = pad(d.getMinutes());
      this.seconds.textContent = pad(d.getSeconds());
    }
  });
</script>

That way the data does not need to be duplicated.

Live example here:

https://jsbin.com/joyavociza/edit?html,output

@TechQuery
Copy link

You can try the latest polyfill: https://github.com/EasyWebApp/declarative-shadow-dom-polyfill

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants