Skip to content
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

How to add modular styles to web component in vite during build? #132

Open
aditya-databrain opened this issue Aug 20, 2023 · 7 comments
Open
Labels
need more info This appears to be a valid issue, but more information is required before it can be addressed.

Comments

@aditya-databrain
Copy link

No description provided.

@CodePirate7
Copy link

+1. can anyone help answer?

@christopherjbaker
Copy link

Can you provide an example of what you're trying to accomplish here?

@christopherjbaker christopherjbaker added the need more info This appears to be a valid issue, but more information is required before it can be addressed. label Oct 4, 2023
@aditya-databrain
Copy link
Author

Can you provide an example of what you're trying to accomplish here?

We want to inject the styles in web components during build time.
We are using cssInjectedByJs library to do that but it adds to the head and inside react component useEffect we need to check to get the styles from the head and add it inside the component like this <style>{headStyles}</style>.
So I am asking if we can do this during build time only instead of runtime?

@christopherjbaker
Copy link

I think you're saying that you want to add the <style> tag inside the web component (which you would need to do if you were using shadow dom). This can't be done at build time, since it doesn't exist yet. import "....css" solutions usually inject directly into the head without a way to customize this, so we've not been able to make much progress here. Most CSS-in-JS solutions automatically inject into the head, but they can usually be customized. To support that, R2WC has a "hidden" prop that it always adds to components: container. This will be the root node (or shadow node, when relevant). You can interact with this like any other DOM element.

@mockey-jockey
Copy link

hi @christopherjbaker below is my code and I need to add styles inside Shadow Dom

import ReactDOM from 'react-dom/client';
import { App } from './app';

import r2wc from 'react-to-webcomponent';

const TestApp = r2wc(App, React, ReactDOM, {
  props: {
    test: 'string',
  },
  shadow: 'open',
});

customElements.define('test-app', TestApp);

In this code i need to add styles here, pls help..

@subramanian-iouring
Copy link

hi @christopherjbaker below is my code and I need to add styles inside Shadow Dom

import ReactDOM from 'react-dom/client';
import { App } from './app';

import r2wc from 'react-to-webcomponent';

const TestApp = r2wc(App, React, ReactDOM, {
  props: {
    test: 'string',
  },
  shadow: 'open',
});

customElements.define('test-app', TestApp);

In this code i need to add styles here, pls help..

@mockey-jockey @christopherjbaker
Hi, Facing same issue in shadow DOM.
What is the solution for this?

@bmomberger-bitovi
Copy link
Contributor

hi @christopherjbaker below is my code and I need to add styles inside Shadow Dom

import ReactDOM from 'react-dom/client';
import { App } from './app';

import r2wc from 'react-to-webcomponent';

const TestApp = r2wc(App, React, ReactDOM, {
  props: {
    test: 'string',
  },
  shadow: 'open',
});

customElements.define('test-app', TestApp);

In this code i need to add styles here, pls help..

Do you specifically have to add your styles there? That will make things quite complicated. See my other answer for a more thorough explanation. #147 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need more info This appears to be a valid issue, but more information is required before it can be addressed.
Projects
None yet
Development

No branches or pull requests

6 participants