Skip to content

Instantly share code, notes, and snippets.

@konstantinmuenster
Created November 8, 2022 12:25
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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.
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