[scoped-elements] demos/docs code does not support extending elements
See original GitHub issuewe 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:
- Created 4 years ago
- Comments:14 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve put together a demo of using a mixin, and hooking into
lit-html
rendering directly using a customtemplateFactory
which returns the scoped template. Basically it’s similar to howshadyRender
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 perLitElement
shadow root including sub templates. We could even apply this on an existing codebase without requiring code changes. Interesting possibilities 😃Some caveats:
eventContext
option to get a reference to the element. We need to think about the proper way to do this.Fixed by https://github.com/open-wc/open-wc/commit/9868bc7a1bc94d4e54651c92d458f3b413a1ebda