question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[scoped-elements] demos/docs code does not support extending elements

See original GitHub issue

we have the following code in

page-a./indexjs

const html = createScopedHtml({
  'feature-a': FeatureA,
  'feature-b': FeatureB,
});

export class PageA extends HTMLElement {
  // ...
  connectedCallback() {
    render(html`
      <feature-a></feature-a>
      <feature-b></feature-b>
    `, this.shadowRoot);
  }
}

this is basically a side effect and registeres feature-a

now it is not possible to extend without registering feature-a 🙈

ExtendedPage.js

import { PageA } from 'page-a'; // triggers side effect already
import { FeatureExtended } from '...';

const html = createScopedHtml({
  'feature-extended': FeatureExtended,
  'feature-b': FeatureB, // do I need this?
});

export class ExtendedPage extends PageA {
  // ...
  connectedCallback() {
    render(html`
      <feature-extended></feature-extended>
    `, this.shadowRoot);
  }
}

Strawman proposal Mixin

Ideally, you could use a static get

export class ExtendedPage extends ScopedElementsMixin(PageA) {
  static get scopedElements() {
    const parentEls = super.scopedElements;
    delete parentEls['feature-b'];
    return {
       ...parentEls, 
       'feature-extended': FeatureExtended,
    }
  }
}

Strawman proposal convention

alternatively, you could have a convention for how to get the scoped html

let html;
export class ExtendedPage extends PageA {
  constructor() {
    // ...
    this._registerSubElements();
  }

  _registerSubElements() {
    html = createScopedHtml({
      'feature-a': FeatureA,
      'feature-b': FeatureB,
    });
  }

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
LarsDenBakkercommented, Jan 23, 2020

I’ve put together a demo of using a mixin, and hooking into lit-html rendering directly using a custom templateFactory which returns the scoped template. Basically it’s similar to how shadyRender does CSS scoping.

https://webcomponents.dev/edit/DQRqzOW9uMj0HUDLXysH

The great thing about this is that we don’t need to create our own html tag, we can apply the transformation to regular lit-html templates and we can scope templates per LitElement shadow root including sub templates. We could even apply this on an existing codebase without requiring code changes. Interesting possibilities 😃

Some caveats:

  • This doesn’t work with shady render at the moment, we may need to replicate some code from shady render or ask lit-html for extra hooks.
  • We are “misusing” the eventContext option to get a reference to the element. We need to think about the proper way to do this.
  • Sub templates are processed, but because we cache on the template level it doesn’t scope the elements again when it’s used in a different scope. We would need to change our caching strategy.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Development: Scoped Elements - Open Web Components
Two possible solutions come to mind: Temporarily (!) allow shipping similar source code (most breaking releases are not a total rewrite) and scope...
Read more >
@open-wc/scoped-elements - npm
Two possible solutions come to mind: Temporarily (!) allow shipping similar source code (most breaking releases are not a total rewrite) and ...
Read more >
The evolution of Open-wc scoped-elements
In this blog post I'm going to talk about the evolution of @open-wc/scoped-elements package. If you want to know the context behind it...
Read more >
Principles: Scoped Elements - Lion
Scoped Custom Element Registries is a proposal that will solve the problem. Since this functionality won't be available (especially not cross browser) anytime ......
Read more >
Extending Native DOM Elements with Web Components
Note: Please don't confuse this Custom Element as an example of a good Custom Element, I've missed out plenty best practices to keep...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found