stencil icon indicating copy to clipboard operation
stencil copied to clipboard

Support for Scope Elements

Open say25 opened this issue 3 years ago • 4 comments

Stencil version:

 @stencil/[email protected]

I'm submitting a:

[ ] bug report [x] feature request [ ] support request => Please do not submit support requests here, use one of these channels: https://stencil-worldwide.herokuapp.com/ or https://forum.ionicframework.com/

Current behavior:

Currently custom elements can conflict when multiple WebComponents have custom elements with the same name on the page.

Expected behavior:

Steps to reproduce:

  • Create a Stencil Component that has a custom element with the name hello-world-component that renders "Hello World"
  • Create another Stencil Component that has a custom element with the name hello-world-component that renders "Hello World 2"
  • Include both on the same page and see a conflict in behavior as only one can be registered.

Other information:

  • An attempt to fix this with a prefix pattern was tried in https://github.com/ionic-team/stencil/pull/843
  • This was brought back up in https://github.com/ionic-team/stencil/pull/2427

say25 avatar Jul 09 '21 21:07 say25

not saying this isn't valid, however if you need to do this now there are ways:

  1. with the dist bundle: import { defineCustomElements } from '@yourcomponent/libraryname/dist/loader'; ... defineCustomElements(window, { transformTagName: tagName => `unique-prefix-${tagName}` })
  2. with with dist-custom-elements bundle, after importing and when defining you can use https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define

The gotcha with these methods is that stencil doesn't replace tag names within a components' internal jsx. For that you can do something like:

const InnerComponent = `inner-component${suffix}`;
return (
    <Host>
        <InnerComponent />
    </Host>
)```

johnjenkins avatar Jul 09 '21 21:07 johnjenkins

Hey there, thank you for the patience getting back to you! This idea is interesting for sure. If there are alternatives, please share the. If you want to see something like this in the Stencil code please 👍 !

splitinfinities avatar Aug 17 '21 16:08 splitinfinities

Wanted to bump this. Since October 2023 the first browser tests are getting green:

https://wpt.fyi/results/custom-elements/scoped-registry?label=experimental&label=master&aligned

mayerraphael avatar Feb 27 '24 06:02 mayerraphael

Hi @splitinfinities Can we work in this via open source? Our project have this necessities and workarounds in my opinion doen'st worth it if we can make a difference

raveenita avatar May 29 '24 12:05 raveenita