All files / src/adapters react-fela.js

100% Statements 6/6
100% Branches 6/6
100% Functions 2/2
100% Lines 6/6
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47  4x   4x 1x         3x           2x   2x                                                          
function stringifyDeclaration(declaration) {
  const prop = '"' + declaration.property + '":'
 
  if (typeof declaration.value === 'object') {
    return (
      prop + '{' + declaration.value.map(stringifyDeclaration).join(',\n') + '}'
    )
  }
 
  return prop + 'props.' + declaration.value
}
 
export default {
  name: 'react-fela',
  stringify: ({ style, moduleName, classNameMap, className }) => {
    const hasVariations = Object.keys(classNameMap).length > 1
 
    return (
      "import './" +
      moduleName +
      ".elo.css'\n" +
      "import { createComponent } from 'react-fela'\n" +
      (hasVariations
        ? "import { getClassNameFromVariantMap } from '@elodin/runtime'\n\n" +
          'const variantMap = ' +
          JSON.stringify(classNameMap, null, 2) +
          '\n\n'
        : '\n') +
      'function ' +
      moduleName +
      '(props  = {})' +
      ' {\n  ' +
      'return {\n    ' +
      '_className: ' +
      (hasVariations
        ? "getClassNameFromVariantMap('" + className + "', variantMap, props)"
        : className) +
      ',\n    ' +
      style.map(stringifyDeclaration).join(',\n    ') +
      '\n  }\n}\n\n' +
      'export default createComponent(' +
      moduleName +
      ')'
    )
  },
}