Closed Justinidlerz closed 3 years ago
@alexander-akait Thanks for reviewing the PR.
css-loader
can not combine all styles into one container.
Such as a react App has many components and I want to mount that into a shadow-DOM root.
But I don't want to import CSS files from each component.
So the full case see like: /a/component.tsx
import css from './style.css'
// ...
export component;
/b/component.tsx
import css from './style.css'
// ...
export component;
There have many components...
main.tsx
import Main from 'components';
import {render} from 'react-dom'
import getCss from 'style-loader/dist/runtime/injectStyleInScript';
class MyElement extends Element {
constructor() {
super();
const shadowContent = this.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.innerHTML = getCss();
shadowContent.appendChild(style);
const root = document.createElement('div');
shadowContent.appendChild(root);
render(<Main />, root);
}
}
run css.toString()
@alexander-akait So you mean I can use that see like:
import Main from 'components';
import {render} from 'react-dom'
import allCss from 'css-loader/dist/runtime/api';
class MyElement extends Element {
constructor() {
super();
const shadowContent = this.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.innerHTML = allCss.toString(); // This one?
shadowContent.appendChild(style);
const root = document.createElement('div');
shadowContent.appendChild(root);
render(<Main />, root);
}
}
No, just use:
import Main from 'components';
import {render} from 'react-dom'
import styles from './style.css';
class MyElement extends Element {
constructor() {
super();
const shadowContent = this.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.innerHTML = styles.toString();
shadowContent.appendChild(style);
const root = document.createElement('div');
shadowContent.appendChild(root);
render(<Main />, root);
}
}
@alexander-akait Sorry, I don't quite understand the method of use you proposed
Could you please give me an example of how to use the css-loader
to getting the combined styles?
Example above
Just remove style-loader
, you don't need this
No, just use:
import Main from 'components'; import {render} from 'react-dom' import styles from './style.css'; class MyElement extends Element { constructor() { super(); const shadowContent = this.attachShadow({ mode: 'open' }); const style = document.createElement('style'); style.innerHTML = styles.toString(); shadowContent.appendChild(style); const root = document.createElement('div'); shadowContent.appendChild(root); render(<Main />, root); } }
I can import the root CSS file see like your provided case. But how to apply nested component styles?
Sorry, it is not QA services, please use stackoverflow for questions and personal help, we don't need this API, because css-loader
provide your ability to use toString
and return string of CSS, it is enough for your case
/a/component.tsx
import css from './style.css'
const A = () => {
return <div />
}
export default A;
/b/component.tsx
import css from './style.css'
import A from '../a/component'
const B = () => {
return <div>
<A />
</div>
}
export component;
There have many components...
main.tsx
import B from 'b/component';
import {render} from 'react-dom'
import getCss from 'style-loader/dist/runtime/injectStyleInScript';
class MyElement extends Element {
constructor() {
super();
const shadowContent = this.attachShadow({ mode: 'open' });
const style = document.createElement('style');
style.innerHTML = getCss();
shadowContent.appendChild(style);
const root = document.createElement('div');
shadowContent.appendChild(root);
render(<Main />, root);
}
}
You don't need this API, what is not clear from my answer? Remove style-loader
and don't use this, you don't need this for custom components.
You mix multiple react component with the one custom component, this is the wrong approach to what you are trying to achieve
You should not have import css from './style.css'
for react components, styles only inside custom component
This PR contains a:
Motivation / Use-Case
Extract styles and put into a container, then can injected into the shadow-DOM conainer