object-to-css icon indicating copy to clipboard operation
object-to-css copied to clipboard

Added support for nested objects

Open sbrichardson opened this issue 7 years ago • 2 comments

Currently, if a css object has nested classes the resulting css will be [object Object]

This PR will process them correctly by recursively calling for any nested objects, building the nested selectors correctly.

Added isPlainObj util function, can modify to something simpler if needed.

sbrichardson avatar Apr 10 '19 22:04 sbrichardson

Example usage

const sample_with_class = {
  div: {
    display: 'inline-flex',
    appearance: 'none',
    border: 'none',
  },
  '.title': {
    fontSize: 32,
    'font-weight': 700,
    border: '0 solid transparent',
    transformOrigin: 'center top',
    'transform-origin': 'center top',
    perspective: '1em',
  },
} 

const output = objToCss(sample_with_class)

/*

// Result

div:{display:inline-flex;appearance:none;border:none;}.title:{font-size:32px;font-weight:700px;border:0 solid transparent;transform-origin:center top;transform-origin:center top;perspective:1em;}'

// Formatted Result

div: {
  display: inline-flex;
  appearance: none;
  border: none;
}

.title: {
  font-size: 32px;
  font-weight: 700px;
  border: 0 solid transparent;
  transform-origin: center top;
  transform-origin: center top;
  perspective: 1em;
}

*/

sbrichardson avatar Apr 10 '19 22:04 sbrichardson

Sorry, took me a while to see this. This can be done at a higher level with a different package, since I want to just keep this simple. People may want different classnames for example, not sure.

natew avatar Nov 19 '19 19:11 natew