Created
November 8, 2022 12:25
-
-
Save konstantinmuenster/dd391a78b167972f03e198198fe6b5ae to your computer and use it in GitHub Desktop.
Transform a string that contains CSS into an object that can be used to style React components via the `style` property.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const css2obj = css => { | |
const regex = /(?<=^|;)\s*([^:]+)\s*:\s*([^;]+)\s*/g | |
const styles = {}; | |
css.replace(regex, (_,p,value) => { | |
const property = p.replace(/-(.)/g, (_,p) => p.toUpperCase()) | |
styles[property] = value | |
}); | |
return styles; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment