cx.js.flow 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Copyright (c) 2013-present, Facebook, Inc.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. *
  7. * @providesModule cx
  8. */
  9. /**
  10. * This function is used to mark string literals representing CSS class names
  11. * so that they can be transformed statically. This allows for modularization
  12. * and minification of CSS class names.
  13. *
  14. * In static_upstream, this function is actually implemented, but it should
  15. * eventually be replaced with something more descriptive, and the transform
  16. * that is used in the main stack should be ported for use elsewhere.
  17. *
  18. * @param string|object className to modularize, or an object of key/values.
  19. * In the object case, the values are conditions that
  20. * determine if the className keys should be included.
  21. * @param [string ...] Variable list of classNames in the string case.
  22. * @return string Renderable space-separated CSS className.
  23. */
  24. function cx(classNames) {
  25. if (typeof classNames == 'object') {
  26. return Object.keys(classNames).filter(className => classNames[className]).map(replace).join(' ');
  27. }
  28. return Array.prototype.map.call(arguments, replace).join(' ');
  29. }
  30. function replace(str) {
  31. return str.replace(/\//g, '-');
  32. }
  33. module.exports = cx;