-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Embedding styles in shadow DOM #172
Comments
I'm wondering the same, no styles seems to be included in the shadow dom |
First of all, this would entirely depend on how your bundler/build tool deals with CSS imports, and the constraints you have in your project. With that said, you can make it work, but at the moment it looks like you must roll your own solution. In my situation, I am using Without modifications, the
After modifying the plugin so that it worked with class MyCustomElement extends r2wc(MyComponent) {
constructor() {
super();
this.container.append(document.getElementById('bun_lightningcss').cloneNode(true));
}
} It works like a charm and fits my use case perfectly. But you might have to find a way to do this with the tools you use if the solution doesn't exist yet. |
Almost same as #172 (comment), My usecase:
Example
I hope this helps. |
Similar to ^but scoped example: import r2wc from "@r2wc/react-to-web-component";
import App from "./App";
class StyledHelloWC extends r2wc(App, {
props: { name: "string" },
shadow: "open",
}) {
connectedCallback() {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
super.connectedCallback();
// window.__styles is injected by vite-plugin-css-injected-by-js
if (window.__styles) {
const template = document.createElement("template");
template.innerHTML = `<style id="vite-plugin-css-injected-by-js">${window.__styles}</style>`;
this.shadowRoot?.appendChild(template.content.cloneNode(true));
}
}
}
customElements.define("hello-tailwind-wc", StyledHelloWC); And in plugins: [
...defaultConfig.plugins,
cssInjectedByJsPlugin({
injectCode: (cssCode: string) => {
return `window.__styles = ${cssCode}`;
},
}),
], |
When we pass
shadow : 'open'
orshadow : 'closed'
we would probably expect the styles to be included in the shadow root.Is there any way to accomplish this?
React component
To create the web component
Usage
The text was updated successfully, but these errors were encountered: