css-guards.less 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 
  2. .light when (lightness(@a) > 50%) {
  3. color: green;
  4. }
  5. .dark when (lightness(@a) < 50%) {
  6. color: orange;
  7. }
  8. @a: #ddd;
  9. .see-the {
  10. @a: #444; // this mirrors what mixins do - they evaluate the guards at the point of definition
  11. .light();
  12. .dark();
  13. }
  14. .hide-the {
  15. .light();
  16. .dark();
  17. }
  18. .multiple-conditions-1 when (@b = 1), (@c = 2), (@d = 3) {
  19. color: red;
  20. }
  21. .multiple-conditions-2 when (@b = 1), (@c = 2), (@d = 2) {
  22. color: blue;
  23. }
  24. @b: 2;
  25. @c: 3;
  26. @d: 3;
  27. .inheritance when (@b = 2) {
  28. .test {
  29. color: black;
  30. }
  31. &:hover {
  32. color: pink;
  33. }
  34. .hideme when (@b = 1) {
  35. color: green;
  36. }
  37. & when (@b = 1) {
  38. hideme: green;
  39. }
  40. }
  41. .hideme when (@b = 1) {
  42. .test {
  43. color: black;
  44. }
  45. &:hover {
  46. color: pink;
  47. }
  48. .hideme when (@b = 1) {
  49. color: green;
  50. }
  51. }
  52. & when (@b = 1) {
  53. .hideme {
  54. color: red;
  55. }
  56. }
  57. .mixin-with-guard-inside(@colWidth) {
  58. // selector with guard (applies also to & when() ...)
  59. .clsWithGuard when (@colWidth <= 0) {
  60. dispaly: none;
  61. }
  62. }
  63. .mixin-with-guard-inside(0px);
  64. .dont-split-me-up {
  65. width: 1px;
  66. & when (@c = 3) {
  67. color: red;
  68. }
  69. & when (@c = 3) {
  70. height: 1px;
  71. }
  72. + & when (@c = 3) { // creates invalid css but tests that we don't fold it in
  73. sibling: true;
  74. }
  75. }
  76. .scope-check when (@c = 3) {
  77. @k: 1px;
  78. & when (@c = 3) {
  79. @k: 2px;
  80. sub-prop: @k;
  81. }
  82. prop: @k;
  83. }
  84. .scope-check-2 {
  85. .scope-check();
  86. @k:4px;
  87. }
  88. .errors-if-called when (@c = never) {
  89. .mixin-doesnt-exist();
  90. }
  91. a:hover when (2 = true) {5:-}