Skip to content

Support for Scope Elements #2957

Closed
Closed

Description

@say25

Stencil version:

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:

Activity

  1. johnjenkins commented on Jul 9, 2021

    @johnjenkins
    Contributor

    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>
    )```
    
  2. added
    Feature: Want this? Upvote it!
    Request For Comments
    on Aug 17, 2021
  3. added
    Has Workaround
    on Aug 17, 2021
  4. splitinfinities commented on Aug 17, 2021

    @splitinfinities
    Contributor

    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 👍 !

  5. mayerraphael commented on Feb 27, 2024

    @mayerraphael
  6. raveenita commented on May 29, 2024

    @raveenita

    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

  7. christian-bromann commented on Nov 19, 2024

    @christian-bromann
    Member

    @raveenita yes, we would love any contributions to this. I am happy to support when any technical question arise. Mind providing some insights in how you would like to implement this?

  8. fcano-ut commented on Jan 10, 2025

    @fcano-ut

    I was thinking what it would take for Stencyl to be compatible with scoped custom elements polyfill

    When calling the attachShadow function, we should pass a registry option that might be passed down to that function like this:

    const registry = new CustomElementRegistry();
    
    defineCustomElements(registry);

    I believe it would be as simple as that, but I might be totally wrong. I'm missing in the code the part where we avoid calling defineCustomElement again if the element is already called. If such a code exists, it should also need to be changed somehow 🤔 There's also code related to supporting Shadow DOM polyfills, but the custom element registry polyfill is not supporting those polyfills. So these two features might not work together, so that's something else that might cause complexity 🤔

    Anyway, that was just a little "what if" experiment. From what I can see, the Scoped Custom Element feature is still a WIP proposal, so I understand if Stencyl doesn't want to support that yet. We discussed forking Stencyl (or using patch-packages to override parts of the code), but we decided to wait a while and see if the proposal can move forward soon... Hope it does! 🤞

  9. johnjenkins commented on Jan 10, 2025

    @johnjenkins
    Contributor

    I'm missing in the code the part where we avoid calling defineCustomElement again if the element is already called.

    @fcano-ut it depends on the output being generated...

    For the dist / lazy loader output the proxy components (the skeleton elements that wait for connectedCallback before fetching / wiring in the 'real' class) define themselves here

    For the custom-element output the functions are created (via AST) so they can be bundled with each component module in-turn here

  10. sdvg commented on Mar 25, 2025

    @sdvg

    I spent some time creating a POC using Scoped Custom Elements in a micro-frontend scenario.

    Unfortunately, I ran into two major issues that make Scoped Elements impractical for our use case:

    1. When a React app renders inside a Shadow Root with scoped customElements, the custom element tags don’t upgrade to their corresponding Web Components. I assume this is because React uses document.createElement rather than the scoped version (shadowRoot.createElement) to instantiate elements. Other frameworks and libraries likely have the same limitation.

    2. Scoped Custom Elements require micro-frontends to be rendered inside a Shadow Root (as expected), but there’s no straightforward way to inherit global document styles within the Shadow Root. While this might not be a complete blocker, it would introduce significant breaking changes to existing setups.

    That said, there may still be valid use cases for Scoped Custom Elements in Stencil - it just doesn't seem like the right fit for micro-frontend support.

  11. fcano-ut commented on Apr 3, 2025

    @fcano-ut

    Similar to what @sdvg commented, I also run a PoC and couldn't get this to work on React. There's a React issue that tracks this, but it would be a breaking change and it didn't get any attention from maintainers so it doesn't look like it will land in any release soon: react/react#28938

  12. michaelrgalloway commented on Sep 19, 2025

    @michaelrgalloway
    1. Scoped Custom Elements require micro-frontends to be rendered inside a Shadow Root (as expected), but there’s no straightforward way to inherit global document styles within the Shadow Root. While this might not be a complete blocker, it would introduce significant breaking changes to existing setups.

    I'd argue that this is a feature of shadow roots, not the opposite. One of the best selling points for shadow roots is the encapsulation of css. There are several ways to achieve obtaining css rules defined globally into the shadow root. CSS variables or adopted styled sheets for instance. In our micro frontend setup, we take advantage of both and it works out well.

    Our use case for scoped custom elements is design component library versions. If one micro app team is on design library version 3.45 and another on version 4.32, then two of the same component name can live on the same web app without collision.

    Looking forward to when Stencil supports this feature.

    Scoped Custom Elements are currently being implemented in the major browsers. If React or other frameworks have problems with rendering or resolving when inside shadow roots, seems like this is a limitation of the framework, not the standard.

  13. johnjenkins commented on Dec 18, 2025

    @johnjenkins
    Contributor

    hey all - I think most use cases should be covered via #6211 ?
    If not lemme know and I'll re-open

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Feature: Want this? Upvote it!Has WorkaroundRequest For Comments

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions