Skip to content

Instantly share code, notes, and snippets.

@kakonbarman
Forked from codersantosh/1. render-css.js
Created June 28, 2022 15:08
Show Gist options
  • Save kakonbarman/297db76bf72a2792b32555ef45f44708 to your computer and use it in GitHub Desktop.
Save kakonbarman/297db76bf72a2792b32555ef45f44708 to your computer and use it in GitHub Desktop.
React Component for Internal CSS
/*STYLIS : https://www.npmjs.com/package/stylis*/
import {compile, serialize, stringify, middleware, prefixer} from 'stylis';
/*Process the Raw CSS*/
const getProcessedCss = (rawCss) =>{
return serialize(compile(rawCss), middleware([prefixer, stringify]))
}
/*Render CSS inside style tag*/
const RenderCss = (props) => {
return (
<style>{getProcessedCss(props.children)}</style>
);
};
export default RenderCss;
/*How to call render CSS*/
/*Import Render CSS*/
import RenderCss from "../style";
/*Component with Internal CSS*/
const ComponentWithInternalCss= (props) => {
return (
<>
<RenderCss>
{`
.parent-class {
min-height:200px;
.child-class{
color:orange;
}
}
`}
</RenderCss>
<div className='parent-class'>
<div className='child-class'>
</div>
</div>
</>
)
}
export default ComponentWithInternalCss
/*Output Style/CSS*/
<style>.parent-class{min-height:200px;}.parent-class .child-class{color:orange;}</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment