_mixins.scss 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2017 Google Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. @use 'sass:list';
  21. @use 'sass:map';
  22. @use 'sass:math';
  23. @use './variables';
  24. // returns the lower grid boundary or null if the smallest grid is selected
  25. @function breakpoint-min($size) {
  26. @if not map.has-key(variables.$columns, $size) {
  27. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  28. }
  29. $min: map.get(variables.$breakpoints, $size);
  30. @return if($min > 0, $min, null);
  31. }
  32. // returns the upper grid boundary or null if the largest grid is selected
  33. @function breakpoint-max($size) {
  34. @if not map.has-key(variables.$columns, $size) {
  35. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  36. }
  37. $names: map.keys(variables.$columns);
  38. $n: list.index($names, $size);
  39. $prev: if($n > 1, list.nth($names, $n - 1), null);
  40. @return if($prev, (breakpoint-min($prev) - 1px), null);
  41. }
  42. // Private mixins, meant for internal use.
  43. @mixin media-query_($size) {
  44. @if not map.has-key(variables.$columns, $size) {
  45. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  46. }
  47. $min: breakpoint-min($size);
  48. $max: breakpoint-max($size);
  49. @if $min == null and $max != null {
  50. // Phone
  51. @media (max-width: $max) {
  52. @content;
  53. }
  54. } @else if $min != null and $max != null {
  55. // Tablet
  56. @media (min-width: $min) and (max-width: $max) {
  57. @content;
  58. }
  59. } @else if $min != null and $max == null {
  60. // Desktop
  61. @media (min-width: $min) {
  62. @content;
  63. }
  64. } @else {
  65. // Fallback - no breakpoints defined
  66. @content;
  67. }
  68. }
  69. @mixin cell-span_($size, $span, $gutter) {
  70. @if not map.has-key(variables.$columns, $size) {
  71. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  72. }
  73. $percent: math.percentage(
  74. math.div($span, map.get(variables.$columns, $size))
  75. );
  76. @if $percent > 100% {
  77. $percent: 100%;
  78. }
  79. width: calc(#{$percent} - #{$gutter});
  80. width: calc(#{$percent} - var(--mdc-layout-grid-gutter-#{$size}, #{$gutter}));
  81. @supports (display: grid) {
  82. width: auto;
  83. grid-column-end: span math.min($span, map.get(variables.$columns, $size));
  84. }
  85. }
  86. // Public mixins, meant for developer usage.
  87. @mixin layout-grid($size, $margin, $max-width: null) {
  88. @if not map.has-key(variables.$columns, $size) {
  89. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  90. }
  91. box-sizing: border-box;
  92. margin: 0 auto;
  93. padding: $margin;
  94. padding: var(--mdc-layout-grid-margin-#{$size}, #{$margin});
  95. @if $max-width {
  96. max-width: $max-width;
  97. }
  98. }
  99. @mixin inner($size, $margin, $gutter) {
  100. @if not map.has-key(variables.$columns, $size) {
  101. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  102. }
  103. display: flex;
  104. flex-flow: row wrap;
  105. align-items: stretch;
  106. margin: math.div(-$gutter, 2);
  107. margin: calc(var(--mdc-layout-grid-gutter-#{$size}, #{$gutter}) / 2 * -1);
  108. @supports (display: grid) {
  109. display: grid;
  110. margin: 0;
  111. grid-gap: $gutter;
  112. grid-gap: var(--mdc-layout-grid-gutter-#{$size}, $gutter);
  113. grid-template-columns: repeat(
  114. map.get(variables.$columns, $size),
  115. minmax(0, 1fr)
  116. );
  117. }
  118. }
  119. @mixin cell($size, $default-span, $gutter) {
  120. @if not map.has-key(variables.$columns, $size) {
  121. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  122. }
  123. @include cell-span_($size, $default-span, $gutter);
  124. box-sizing: border-box;
  125. margin: math.div($gutter, 2);
  126. margin: calc(var(--mdc-layout-grid-gutter-#{$size}, #{$gutter}) / 2);
  127. @supports (display: grid) {
  128. margin: 0;
  129. }
  130. }
  131. @mixin cell-order($order) {
  132. order: $order;
  133. }
  134. @mixin cell-align($position) {
  135. @if $position == 'top' {
  136. align-self: flex-start;
  137. @supports (display: grid) {
  138. align-self: start;
  139. }
  140. }
  141. @if $position == 'middle' {
  142. align-self: center;
  143. }
  144. @if $position == 'bottom' {
  145. align-self: flex-end;
  146. @supports (display: grid) {
  147. align-self: end;
  148. }
  149. }
  150. @if $position == 'stretch' {
  151. align-self: stretch;
  152. }
  153. }
  154. @mixin fixed-column-width($size, $margin, $gutter, $column-width) {
  155. @if not map.has-key(variables.$columns, $size) {
  156. @error "Invalid style specified! Choose one of #{map.keys(variables.$columns)}";
  157. }
  158. $columnCount: map.get(variables.$columns, $size);
  159. $gutter-number: $columnCount - 1;
  160. $margin-number: 2;
  161. width: $column-width * $columnCount + $gutter * $gutter-number + $margin *
  162. $margin-number;
  163. width: calc(
  164. var(--mdc-layout-grid-column-width-#{$size}, #{$column-width}) * #{$columnCount} +
  165. var(--mdc-layout-grid-gutter-#{$size}, #{$gutter}) * #{$gutter-number} +
  166. var(--mdc-layout-grid-margin-#{$size}, #{$margin}) * #{$margin-number}
  167. );
  168. }