bitovi / react-to-web-component

Convert react components to native Web Components. Works with Preact too!
https://www.bitovi.com/open-source/react-to-web-component
MIT License
728 stars 43 forks source link

how to add style element to web component #165

Open Lionardo opened 10 months ago

Lionardo commented 10 months ago

I have a use case in which I need to pass a style element to the web component like this: But it doesn't work. If shadow option is set to open or closed it doesn't matter. I need to encapsulate styles during runtime that will be loaded over a request. If this doesn't work what could be the alternative?

const ChatWebComponent = r2wc(Chat, {
  props: {
    Id: "string",
  },
} as any);

customElements.define("my-chat", ChatWebComponent);
const chat = document.createElement("my-chat");
chat.Id = "5896045a";
const style = document.createElement('style');

style.textContent = `
    #my-chat {
      font-family: "Roboto" !important;
      // some more styles...
    }
`;

// Append the style element to the shadow root.
chat.shadowRoot.appendChild(style);`
document.body.appendChild(chat);