bem.js 942 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.createBEM = createBEM;
  4. /**
  5. * bem helper
  6. * b() // 'button'
  7. * b('text') // 'button__text'
  8. * b({ disabled }) // 'button button--disabled'
  9. * b('text', { disabled }) // 'button__text button__text--disabled'
  10. * b(['disabled', 'primary']) // 'button button--disabled button--primary'
  11. */
  12. function gen(name, mods) {
  13. if (!mods) {
  14. return '';
  15. }
  16. if (typeof mods === 'string') {
  17. return " " + name + "--" + mods;
  18. }
  19. if (Array.isArray(mods)) {
  20. return mods.reduce(function (ret, item) {
  21. return ret + gen(name, item);
  22. }, '');
  23. }
  24. return Object.keys(mods).reduce(function (ret, key) {
  25. return ret + (mods[key] ? gen(name, key) : '');
  26. }, '');
  27. }
  28. function createBEM(name) {
  29. return function (el, mods) {
  30. if (el && typeof el !== 'string') {
  31. mods = el;
  32. el = '';
  33. }
  34. el = el ? name + "__" + el : name;
  35. return "" + el + gen(el, mods);
  36. };
  37. }