bem.styl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // -----------------------------------------------------------------------------
  2. // bem-sugar.styl --- Bem mixins for stylus language
  3. //
  4. // Copyright (c) 2017 Ilya Obuhov
  5. //
  6. // Author: Ilya Obuhov <iobuhov.mail@gmail.com>
  7. // URL: https://github.com/iobuhov/stylus-bem-sugar
  8. e-prefix ?= '__'
  9. m-prefix ?= '--'
  10. m-delimiter ?= '_'
  11. group-store = ()
  12. str()
  13. join('', arguments)
  14. b(name)
  15. .{name}
  16. {block}
  17. group()
  18. caller = called-from[0]
  19. level = length(called-from) + 1
  20. elements = group-store[level]
  21. selector = ()
  22. parent = null
  23. {join(',', elements)}
  24. {block}
  25. group-store[level] = null
  26. m(mod, val=null)
  27. val = val && m-delimiter + val
  28. mod = m-prefix + mod
  29. mod = val ? mod + val : mod
  30. caller = called-from[0]
  31. if caller in ('group')
  32. level = length(called-from)
  33. mod = str('&', mod)
  34. if group-store[level] == null
  35. group-store[level] = mod
  36. else
  37. push(group-store[level], mod)
  38. &{mod}
  39. {block}
  40. e(element)
  41. element = e-prefix + element
  42. caller = called-from[0]
  43. gcaller = called-from[1]
  44. if caller in ('group')
  45. level = length(called-from)
  46. if gcaller in ('e' 'm')
  47. element = str('& ^[0]', element)
  48. else
  49. element = str('^[0]', element)
  50. if group-store[level] == null
  51. group-store[level] = element
  52. else
  53. push(group-store[level], element)
  54. else
  55. if caller in ('e' 'm')
  56. & ^[0]{element}
  57. {block}
  58. else
  59. &{element}
  60. {block}