I have an object in following format:
let style:React.CSSProperties|object = {
marginLeft: '0',
borderRadius: '20px',
display: 'flex',
justifyContent: 'center',
}
I need to convert the React CSSProperties into corresponding string format for instance:
let string_styles:string = `{
margin-left: 0;
border-radius: 20px;
display: flex;
justify-content: center;
}`
since I can't find a tangible syntax to write css such as :hover, ::before, ::before:hover inline, the purpose use it as a styled component for now, meanwhile leave the CSSProperties object alone for future import.
inelegantly, I hard coded the transforming method.
deQuote(dict:object):string{
return JSON.stringify(dict).replace(/['"]+/g, '');
}
// passin: {marginLeft: '0', borderRadius: '20px'}
// return: `{marginLeft: 0, borderRadius: 20px}`
deBraces(str:string):string{
return str.substring(1, str.length-1);
}
// passin: `{marginLeft: 0, borderRadius: 20px}`
// return: `marginLeft: 0, borderRadius: 20px`
deComma(str:string):string{
return str.split(',').join(';');
}
// passin: `marginLeft: 0, borderRadius: 20px`
// return: `marginLeft: 0; borderRadius: 20px`
deBase(dict:object):string{
return deComma(deBraces(deQuote(dict)));
}
// passin: {marginLeft: '0', borderRadius: '20px'}
// return: `marginLeft: 0; borderRadius: 20px`
But here comes the part that is tricky, it takes lot of effort to convert
marginLeft -> margin-left
Solved by customised function,
CSSPropertiesToComponent(dict:React.CSSProperties){
let str = '';
for(const [key, value] of Object.entries(dict)){
let clo = '';
key.split('').forEach(lt=>{
if(lt.toUpperCase() === lt){
clo += '-' + lt.toLowerCase();
}else{
clo += lt;
}
});
str += clo + ':' + value + ';';
}
return str;
}
However, if there is a better way to convert React.CSSProperties into Styled Component string, please let me know.