_breakpoints.scss 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // breakpoints.scss v1.0 | @ajlkn | MIT licensed */
  2. // Vars.
  3. /// Breakpoints.
  4. /// @var {list}
  5. $breakpoints: () !global;
  6. // Mixins.
  7. /// Sets breakpoints.
  8. /// @param {map} $x Breakpoints.
  9. @mixin breakpoints($x: ()) {
  10. $breakpoints: $x !global;
  11. }
  12. /// Wraps @content in a @media block targeting a specific orientation.
  13. /// @param {string} $orientation Orientation.
  14. @mixin orientation($orientation) {
  15. @media screen and (orientation: #{$orientation}) {
  16. @content;
  17. }
  18. }
  19. /// Wraps @content in a @media block using a given query.
  20. /// @param {string} $query Query.
  21. @mixin breakpoint($query: null) {
  22. $breakpoint: null;
  23. $op: null;
  24. $media: null;
  25. // Determine operator, breakpoint.
  26. // Greater than or equal.
  27. @if (str-slice($query, 0, 2) == '>=') {
  28. $op: 'gte';
  29. $breakpoint: str-slice($query, 3);
  30. }
  31. // Less than or equal.
  32. @elseif (str-slice($query, 0, 2) == '<=') {
  33. $op: 'lte';
  34. $breakpoint: str-slice($query, 3);
  35. }
  36. // Greater than.
  37. @elseif (str-slice($query, 0, 1) == '>') {
  38. $op: 'gt';
  39. $breakpoint: str-slice($query, 2);
  40. }
  41. // Less than.
  42. @elseif (str-slice($query, 0, 1) == '<') {
  43. $op: 'lt';
  44. $breakpoint: str-slice($query, 2);
  45. }
  46. // Not.
  47. @elseif (str-slice($query, 0, 1) == '!') {
  48. $op: 'not';
  49. $breakpoint: str-slice($query, 2);
  50. }
  51. // Equal.
  52. @else {
  53. $op: 'eq';
  54. $breakpoint: $query;
  55. }
  56. // Build media.
  57. @if ($breakpoint and map-has-key($breakpoints, $breakpoint)) {
  58. $a: map-get($breakpoints, $breakpoint);
  59. // Range.
  60. @if (type-of($a) == 'list') {
  61. $x: nth($a, 1);
  62. $y: nth($a, 2);
  63. // Max only.
  64. @if ($x == null) {
  65. // Greater than or equal (>= 0 / anything)
  66. @if ($op == 'gte') {
  67. $media: 'screen';
  68. }
  69. // Less than or equal (<= y)
  70. @elseif ($op == 'lte') {
  71. $media: 'screen and (max-width: ' + $y + ')';
  72. }
  73. // Greater than (> y)
  74. @elseif ($op == 'gt') {
  75. $media: 'screen and (min-width: ' + ($y + 1) + ')';
  76. }
  77. // Less than (< 0 / invalid)
  78. @elseif ($op == 'lt') {
  79. $media: 'screen and (max-width: -1px)';
  80. }
  81. // Not (> y)
  82. @elseif ($op == 'not') {
  83. $media: 'screen and (min-width: ' + ($y + 1) + ')';
  84. }
  85. // Equal (<= y)
  86. @else {
  87. $media: 'screen and (max-width: ' + $y + ')';
  88. }
  89. }
  90. // Min only.
  91. @else if ($y == null) {
  92. // Greater than or equal (>= x)
  93. @if ($op == 'gte') {
  94. $media: 'screen and (min-width: ' + $x + ')';
  95. }
  96. // Less than or equal (<= inf / anything)
  97. @elseif ($op == 'lte') {
  98. $media: 'screen';
  99. }
  100. // Greater than (> inf / invalid)
  101. @elseif ($op == 'gt') {
  102. $media: 'screen and (max-width: -1px)';
  103. }
  104. // Less than (< x)
  105. @elseif ($op == 'lt') {
  106. $media: 'screen and (max-width: ' + ($x - 1) + ')';
  107. }
  108. // Not (< x)
  109. @elseif ($op == 'not') {
  110. $media: 'screen and (max-width: ' + ($x - 1) + ')';
  111. }
  112. // Equal (>= x)
  113. @else {
  114. $media: 'screen and (min-width: ' + $x + ')';
  115. }
  116. }
  117. // Min and max.
  118. @else {
  119. // Greater than or equal (>= x)
  120. @if ($op == 'gte') {
  121. $media: 'screen and (min-width: ' + $x + ')';
  122. }
  123. // Less than or equal (<= y)
  124. @elseif ($op == 'lte') {
  125. $media: 'screen and (max-width: ' + $y + ')';
  126. }
  127. // Greater than (> y)
  128. @elseif ($op == 'gt') {
  129. $media: 'screen and (min-width: ' + ($y + 1) + ')';
  130. }
  131. // Less than (< x)
  132. @elseif ($op == 'lt') {
  133. $media: 'screen and (max-width: ' + ($x - 1) + ')';
  134. }
  135. // Not (< x and > y)
  136. @elseif ($op == 'not') {
  137. $media: 'screen and (max-width: ' + ($x - 1) + '), screen and (min-width: ' + ($y + 1) + ')';
  138. }
  139. // Equal (>= x and <= y)
  140. @else {
  141. $media: 'screen and (min-width: ' + $x + ') and (max-width: ' + $y + ')';
  142. }
  143. }
  144. }
  145. // String.
  146. @else {
  147. // Missing a media type? Prefix with "screen".
  148. @if (str-slice($a, 0, 1) == '(') {
  149. $media: 'screen and ' + $a;
  150. }
  151. // Otherwise, use as-is.
  152. @else {
  153. $media: $a;
  154. }
  155. }
  156. }
  157. // Output.
  158. @media #{$media} {
  159. @content;
  160. }
  161. }