14

Is there a tool, that converts React inline style descriptions to CSS rules?

Example input:

{
    minHeight: 200,
    position: 'relative',
    flexGrow: 1,
    flexShrink: 1,
    display: 'flex',
    flexDirection: 'column',
}

Example output:

{
    min-height: 200px;
    position: relative;
    flex-grow: 1;
    flex-shrink: 1;
    display: flex;
    flex-flow: column;
}

To put it differently, I want a tool that does the exact opposite of this: https://github.com/raphamorim/native-css

I would like to use it manually, so command line or web-based is preferable.

4 Answers 4

16

Yes there's a tool that does just that!

https://glitch.com/~convert-css-js

If you paste your example input into the JS side (has to be in parentheses), you get the CSS output you specified (less the final semi-colon).

glitch convert css to js screenshot

Sign up to request clarification or add additional context in comments.

1 Comment

Absurdly simple and effective! Takes a little while to load. But man-oh-man, just what I was looking for! Thanks a ton!
2

Have you tried this "A command line tool for JSS" https://github.com/cssinjs/cli

1 Comment

I tried to use it from command line, but couldn't make it work in 20 minuntes, so I had to step back. Thanks for pointing anyway!
2

Is there a tool, that converts React inline style descriptions to CSS rules?

There is a tool, yes. It's Regular Expressions (or RegEx).

If you want to transform the following:

  • nH => n-h (minHeight => min-height)
  • xG => x-g (flexGrow => flex-grow)
  • xS => x-s (flexShrink => flex-shrink)
  • xD => x-d (flexDirection => flex-direction)

This is the same Regex operation each time.

The pattern match is:

/([a-z])([A-Z])/

ie. one lowercase letter followed by one uppercase letter, capturing each.

The replacement is:

$1-\L$2

ie.

  • keep the captured lowercase letter
  • follow the captured lowercase letter with a hyphen
  • follow the hyphen with a lowercase version of the captured uppercase letter

4 Comments

Hi Rounin! Thanks for the answer. I like your approach for simplicity, but it won't fit all the cases. For example, flexDirection should be converted to flex-flow, instead of flex-direction. I guess one needs to take the list of all such cases from React source code, to make a complete solution.
@sesm - flex-flow and flex-direction are different CSS rules. flex-flow is a shorthand to set both flex-direction and flex-wrap at once (since they go hand-in-hand). React supports all three of these via flexFlow, flexDirection, and flexWrap. developer.mozilla.org/en-US/docs/Web/CSS/flex-flow
@cchamberlain thanks for you comment, I didn't know this. This actually makes a regexp approach viable.
NP! I was trying to find a tool that did this the other day. Agree regex might be best bet.
1

you can use this tool for converting : https://casbin.org/CssToAndFromReact/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.