question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transform between CSS string and JS object

See original GitHub issue

Are there any functions in emotion (either via api, or in the supporting code) that will accept a css string (or css file of classes and transform into a proper JS object? I see a few other libraries out there that do this, but if it can be done with emotion, this is a win.

For example, convert

foo {
  background-color: black;
  color: orange;
  margin-top: 10px;
} 

Into:

{
  foo: {
    backgroundColor: "black",
    color: "orange",
    marginTop: "10px"
  }
}

I tried passing the string to css``, but it just transforms it into a string under a style field.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Andaristcommented, Oct 24, 2020

U can use serializeStyles from @emotion/serialize:

serializeStyles([cssObj])
1reaction
Andaristcommented, Oct 23, 2020

Not sure what is your exact use case and what do u plan to do with the converted object but this should be a good base for you to start working with: https://codesandbox.io/s/stupefied-tdd-j70vt?file=/src/index.js

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javascript / convert CSS style string into JS object
First split the string with ; as the separator, and then for each result split with : , placing the items in an...
Read more >
Convert css string to object - CodePen
JS (Babel) ; 1. var myDynamicCSS = "color:#ffffff; background-color: #00ffbb; border-radius:20px;"; ; 2. ​ ; 3. var myDynamicElement = document.querySelector('.my- ...
Read more >
Convert css string to object - Banner Animation - GreenSock
Hey guys, I have a dynamic template, where you can input css into the XML feed, and the banner will read that on...
Read more >
Express CSS with JavaScript Objects for CSS-in-JS Notation
We will build a small utility function that will convert the style object we declare to a string that can be applied to...
Read more >
Converting a JavaScript Object to a String | HTML Goodies
Learn how to convert a JavaScript object to a string with three different JSON methods in this JavaScript web development tutorial.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found