You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
4.4 KiB

  1. // Breakpoint viewport sizes and media queries.
  2. //
  3. // Breakpoints are defined as a map of (name: minimum width), order from small to large:
  4. //
  5. // (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px)
  6. //
  7. // The map defined in the `$grid-breakpoints` global variable is used as the `$breakpoints` argument by default.
  8. // Name of the next breakpoint, or null for the last breakpoint.
  9. //
  10. // >> breakpoint-next(sm)
  11. // md
  12. // >> breakpoint-next(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  13. // md
  14. // >> breakpoint-next(sm, $breakpoint-names: (xs sm md lg xl))
  15. // md
  16. @function breakpoint-next($name, $breakpoints: $grid-breakpoints, $breakpoint-names: map-keys($breakpoints)) {
  17. $n: index($breakpoint-names, $name);
  18. @if not $n {
  19. @error "breakpoint `#{$name}` not found in `#{$breakpoints}`";
  20. }
  21. @return if($n < length($breakpoint-names), nth($breakpoint-names, $n + 1), null);
  22. }
  23. // Minimum breakpoint width. Null for the smallest (first) breakpoint.
  24. //
  25. // >> breakpoint-min(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  26. // 576px
  27. @function breakpoint-min($name, $breakpoints: $grid-breakpoints) {
  28. $min: map-get($breakpoints, $name);
  29. @return if($min != 0, $min, null);
  30. }
  31. // Maximum breakpoint width.
  32. // The maximum value is reduced by 0.02px to work around the limitations of
  33. // `min-` and `max-` prefixes and viewports with fractional widths.
  34. // See https://www.w3.org/TR/mediaqueries-4/#mq-min-max
  35. // Uses 0.02px rather than 0.01px to work around a current rounding bug in Safari.
  36. // See https://bugs.webkit.org/show_bug.cgi?id=178261
  37. //
  38. // >> breakpoint-max(md, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  39. // 767.98px
  40. @function breakpoint-max($name, $breakpoints: $grid-breakpoints) {
  41. $max: map-get($breakpoints, $name);
  42. @return if($max and $max > 0, $max - .02, null);
  43. }
  44. // Returns a blank string if smallest breakpoint, otherwise returns the name with a dash in front.
  45. // Useful for making responsive utilities.
  46. //
  47. // >> breakpoint-infix(xs, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  48. // "" (Returns a blank string)
  49. // >> breakpoint-infix(sm, (xs: 0, sm: 576px, md: 768px, lg: 992px, xl: 1200px))
  50. // "-sm"
  51. @function breakpoint-infix($name, $breakpoints: $grid-breakpoints) {
  52. @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}");
  53. }
  54. // Media of at least the minimum breakpoint width. No query for the smallest breakpoint.
  55. // Makes the @content apply to the given breakpoint and wider.
  56. @mixin media-breakpoint-up($name, $breakpoints: $grid-breakpoints) {
  57. $min: breakpoint-min($name, $breakpoints);
  58. @if $min {
  59. @media (min-width: $min) {
  60. @content;
  61. }
  62. } @else {
  63. @content;
  64. }
  65. }
  66. // Media of at most the maximum breakpoint width. No query for the largest breakpoint.
  67. // Makes the @content apply to the given breakpoint and narrower.
  68. @mixin media-breakpoint-down($name, $breakpoints: $grid-breakpoints) {
  69. $max: breakpoint-max($name, $breakpoints);
  70. @if $max {
  71. @media (max-width: $max) {
  72. @content;
  73. }
  74. } @else {
  75. @content;
  76. }
  77. }
  78. // Media that spans multiple breakpoint widths.
  79. // Makes the @content apply between the min and max breakpoints
  80. @mixin media-breakpoint-between($lower, $upper, $breakpoints: $grid-breakpoints) {
  81. $min: breakpoint-min($lower, $breakpoints);
  82. $max: breakpoint-max($upper, $breakpoints);
  83. @if $min != null and $max != null {
  84. @media (min-width: $min) and (max-width: $max) {
  85. @content;
  86. }
  87. } @else if $max == null {
  88. @include media-breakpoint-up($lower, $breakpoints) {
  89. @content;
  90. }
  91. } @else if $min == null {
  92. @include media-breakpoint-down($upper, $breakpoints) {
  93. @content;
  94. }
  95. }
  96. }
  97. // Media between the breakpoint's minimum and maximum widths.
  98. // No minimum for the smallest breakpoint, and no maximum for the largest one.
  99. // Makes the @content apply only to the given breakpoint, not viewports any wider or narrower.
  100. @mixin media-breakpoint-only($name, $breakpoints: $grid-breakpoints) {
  101. $min: breakpoint-min($name, $breakpoints);
  102. $next: breakpoint-next($name, $breakpoints);
  103. $max: breakpoint-max($next);
  104. @if $min != null and $max != null {
  105. @media (min-width: $min) and (max-width: $max) {
  106. @content;
  107. }
  108. } @else if $max == null {
  109. @include media-breakpoint-up($name, $breakpoints) {
  110. @content;
  111. }
  112. } @else if $min == null {
  113. @include media-breakpoint-down($next, $breakpoints) {
  114. @content;
  115. }
  116. }
  117. }