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.

354 lines
9.8 KiB

  1. // stylelint-disable property-blacklist, scss/dollar-variable-default
  2. // SCSS RFS mixin
  3. //
  4. // Automated responsive values for font sizes, paddings, margins and much more
  5. //
  6. // Licensed under MIT (https://github.com/twbs/rfs/blob/main/LICENSE)
  7. // Configuration
  8. // Base value
  9. $rfs-base-value: 1.25rem !default;
  10. $rfs-unit: rem !default;
  11. @if $rfs-unit != rem and $rfs-unit != px {
  12. @error "`#{$rfs-unit}` is not a valid unit for $rfs-unit. Use `px` or `rem`.";
  13. }
  14. // Breakpoint at where values start decreasing if screen width is smaller
  15. $rfs-breakpoint: 1200px !default;
  16. $rfs-breakpoint-unit: px !default;
  17. @if $rfs-breakpoint-unit != px and $rfs-breakpoint-unit != em and $rfs-breakpoint-unit != rem {
  18. @error "`#{$rfs-breakpoint-unit}` is not a valid unit for $rfs-breakpoint-unit. Use `px`, `em` or `rem`.";
  19. }
  20. // Resize values based on screen height and width
  21. $rfs-two-dimensional: false !default;
  22. // Factor of decrease
  23. $rfs-factor: 10 !default;
  24. @if type-of($rfs-factor) != number or $rfs-factor <= 1 {
  25. @error "`#{$rfs-factor}` is not a valid $rfs-factor, it must be greater than 1.";
  26. }
  27. // Mode. Possibilities: "min-media-query", "max-media-query"
  28. $rfs-mode: min-media-query !default;
  29. // Generate enable or disable classes. Possibilities: false, "enable" or "disable"
  30. $rfs-class: false !default;
  31. // 1 rem = $rfs-rem-value px
  32. $rfs-rem-value: 16 !default;
  33. // Safari iframe resize bug: https://github.com/twbs/rfs/issues/14
  34. $rfs-safari-iframe-resize-bug-fix: false !default;
  35. // Disable RFS by setting $enable-rfs to false
  36. $enable-rfs: true !default;
  37. // Cache $rfs-base-value unit
  38. $rfs-base-value-unit: unit($rfs-base-value);
  39. @function divide($dividend, $divisor, $precision: 10) {
  40. $sign: if($dividend > 0 and $divisor > 0 or $dividend < 0 and $divisor < 0, 1, -1);
  41. $dividend: abs($dividend);
  42. $divisor: abs($divisor);
  43. @if $dividend == 0 {
  44. @return 0;
  45. }
  46. @if $divisor == 0 {
  47. @error "Cannot divide by 0";
  48. }
  49. $remainder: $dividend;
  50. $result: 0;
  51. $factor: 10;
  52. @while ($remainder > 0 and $precision >= 0) {
  53. $quotient: 0;
  54. @while ($remainder >= $divisor) {
  55. $remainder: $remainder - $divisor;
  56. $quotient: $quotient + 1;
  57. }
  58. $result: $result * 10 + $quotient;
  59. $factor: $factor * .1;
  60. $remainder: $remainder * 10;
  61. $precision: $precision - 1;
  62. @if ($precision < 0 and $remainder >= $divisor * 5) {
  63. $result: $result + 1;
  64. }
  65. }
  66. $result: $result * $factor * $sign;
  67. $dividend-unit: unit($dividend);
  68. $divisor-unit: unit($divisor);
  69. $unit-map: (
  70. "px": 1px,
  71. "rem": 1rem,
  72. "em": 1em,
  73. "%": 1%
  74. );
  75. @if ($dividend-unit != $divisor-unit and map-has-key($unit-map, $dividend-unit)) {
  76. $result: $result * map-get($unit-map, $dividend-unit);
  77. }
  78. @return $result;
  79. }
  80. // Remove px-unit from $rfs-base-value for calculations
  81. @if $rfs-base-value-unit == px {
  82. $rfs-base-value: divide($rfs-base-value, $rfs-base-value * 0 + 1);
  83. }
  84. @else if $rfs-base-value-unit == rem {
  85. $rfs-base-value: divide($rfs-base-value, divide($rfs-base-value * 0 + 1, $rfs-rem-value));
  86. }
  87. // Cache $rfs-breakpoint unit to prevent multiple calls
  88. $rfs-breakpoint-unit-cache: unit($rfs-breakpoint);
  89. // Remove unit from $rfs-breakpoint for calculations
  90. @if $rfs-breakpoint-unit-cache == px {
  91. $rfs-breakpoint: divide($rfs-breakpoint, $rfs-breakpoint * 0 + 1);
  92. }
  93. @else if $rfs-breakpoint-unit-cache == rem or $rfs-breakpoint-unit-cache == "em" {
  94. $rfs-breakpoint: divide($rfs-breakpoint, divide($rfs-breakpoint * 0 + 1, $rfs-rem-value));
  95. }
  96. // Calculate the media query value
  97. $rfs-mq-value: if($rfs-breakpoint-unit == px, #{$rfs-breakpoint}px, #{divide($rfs-breakpoint, $rfs-rem-value)}#{$rfs-breakpoint-unit});
  98. $rfs-mq-property-width: if($rfs-mode == max-media-query, max-width, min-width);
  99. $rfs-mq-property-height: if($rfs-mode == max-media-query, max-height, min-height);
  100. // Internal mixin used to determine which media query needs to be used
  101. @mixin _rfs-media-query {
  102. @if $rfs-two-dimensional {
  103. @if $rfs-mode == max-media-query {
  104. @media (#{$rfs-mq-property-width}: #{$rfs-mq-value}), (#{$rfs-mq-property-height}: #{$rfs-mq-value}) {
  105. @content;
  106. }
  107. }
  108. @else {
  109. @media (#{$rfs-mq-property-width}: #{$rfs-mq-value}) and (#{$rfs-mq-property-height}: #{$rfs-mq-value}) {
  110. @content;
  111. }
  112. }
  113. }
  114. @else {
  115. @media (#{$rfs-mq-property-width}: #{$rfs-mq-value}) {
  116. @content;
  117. }
  118. }
  119. }
  120. // Internal mixin that adds disable classes to the selector if needed.
  121. @mixin _rfs-rule {
  122. @if $rfs-class == disable and $rfs-mode == max-media-query {
  123. // Adding an extra class increases specificity, which prevents the media query to override the property
  124. &,
  125. .disable-rfs &,
  126. &.disable-rfs {
  127. @content;
  128. }
  129. }
  130. @else if $rfs-class == enable and $rfs-mode == min-media-query {
  131. .enable-rfs &,
  132. &.enable-rfs {
  133. @content;
  134. }
  135. }
  136. @else {
  137. @content;
  138. }
  139. }
  140. // Internal mixin that adds enable classes to the selector if needed.
  141. @mixin _rfs-media-query-rule {
  142. @if $rfs-class == enable {
  143. @if $rfs-mode == min-media-query {
  144. @content;
  145. }
  146. @include _rfs-media-query {
  147. .enable-rfs &,
  148. &.enable-rfs {
  149. @content;
  150. }
  151. }
  152. }
  153. @else {
  154. @if $rfs-class == disable and $rfs-mode == min-media-query {
  155. .disable-rfs &,
  156. &.disable-rfs {
  157. @content;
  158. }
  159. }
  160. @include _rfs-media-query {
  161. @content;
  162. }
  163. }
  164. }
  165. // Helper function to get the formatted non-responsive value
  166. @function rfs-value($values) {
  167. // Convert to list
  168. $values: if(type-of($values) != list, ($values,), $values);
  169. $val: '';
  170. // Loop over each value and calculate value
  171. @each $value in $values {
  172. @if $value == 0 {
  173. $val: $val + ' 0';
  174. }
  175. @else {
  176. // Cache $value unit
  177. $unit: if(type-of($value) == "number", unit($value), false);
  178. @if $unit == px {
  179. // Convert to rem if needed
  180. $val: $val + ' ' + if($rfs-unit == rem, #{divide($value, $value * 0 + $rfs-rem-value)}rem, $value);
  181. }
  182. @else if $unit == rem {
  183. // Convert to px if needed
  184. $val: $val + ' ' + if($rfs-unit == px, #{divide($value, $value * 0 + 1) * $rfs-rem-value}px, $value);
  185. }
  186. @else {
  187. // If $value isn't a number (like inherit) or $value has a unit (not px or rem, like 1.5em) or $ is 0, just print the value
  188. $val: $val + ' ' + $value;
  189. }
  190. }
  191. }
  192. // Remove first space
  193. @return unquote(str-slice($val, 2));
  194. }
  195. // Helper function to get the responsive value calculated by RFS
  196. @function rfs-fluid-value($values) {
  197. // Convert to list
  198. $values: if(type-of($values) != list, ($values,), $values);
  199. $val: '';
  200. // Loop over each value and calculate value
  201. @each $value in $values {
  202. @if $value == 0 {
  203. $val: $val + ' 0';
  204. }
  205. @else {
  206. // Cache $value unit
  207. $unit: if(type-of($value) == "number", unit($value), false);
  208. // If $value isn't a number (like inherit) or $value has a unit (not px or rem, like 1.5em) or $ is 0, just print the value
  209. @if not $unit or $unit != px and $unit != rem {
  210. $val: $val + ' ' + $value;
  211. }
  212. @else {
  213. // Remove unit from $value for calculations
  214. $value: divide($value, $value * 0 + if($unit == px, 1, divide(1, $rfs-rem-value)));
  215. // Only add the media query if the value is greater than the minimum value
  216. @if abs($value) <= $rfs-base-value or not $enable-rfs {
  217. $val: $val + ' ' + if($rfs-unit == rem, #{divide($value, $rfs-rem-value)}rem, #{$value}px);
  218. }
  219. @else {
  220. // Calculate the minimum value
  221. $value-min: $rfs-base-value + divide(abs($value) - $rfs-base-value, $rfs-factor);
  222. // Calculate difference between $value and the minimum value
  223. $value-diff: abs($value) - $value-min;
  224. // Base value formatting
  225. $min-width: if($rfs-unit == rem, #{divide($value-min, $rfs-rem-value)}rem, #{$value-min}px);
  226. // Use negative value if needed
  227. $min-width: if($value < 0, -$min-width, $min-width);
  228. // Use `vmin` if two-dimensional is enabled
  229. $variable-unit: if($rfs-two-dimensional, vmin, vw);
  230. // Calculate the variable width between 0 and $rfs-breakpoint
  231. $variable-width: #{divide($value-diff * 100, $rfs-breakpoint)}#{$variable-unit};
  232. // Return the calculated value
  233. $val: $val + ' calc(' + $min-width + if($value < 0, ' - ', ' + ') + $variable-width + ')';
  234. }
  235. }
  236. }
  237. }
  238. // Remove first space
  239. @return unquote(str-slice($val, 2));
  240. }
  241. // RFS mixin
  242. @mixin rfs($values, $property: font-size) {
  243. @if $values != null {
  244. $val: rfs-value($values);
  245. $fluidVal: rfs-fluid-value($values);
  246. // Do not print the media query if responsive & non-responsive values are the same
  247. @if $val == $fluidVal {
  248. #{$property}: $val;
  249. }
  250. @else {
  251. @include _rfs-rule {
  252. #{$property}: if($rfs-mode == max-media-query, $val, $fluidVal);
  253. // Include safari iframe resize fix if needed
  254. min-width: if($rfs-safari-iframe-resize-bug-fix, (0 * 1vw), null);
  255. }
  256. @include _rfs-media-query-rule {
  257. #{$property}: if($rfs-mode == max-media-query, $fluidVal, $val);
  258. }
  259. }
  260. }
  261. }
  262. // Shorthand helper mixins
  263. @mixin font-size($value) {
  264. @include rfs($value);
  265. }
  266. @mixin padding($value) {
  267. @include rfs($value, padding);
  268. }
  269. @mixin padding-top($value) {
  270. @include rfs($value, padding-top);
  271. }
  272. @mixin padding-right($value) {
  273. @include rfs($value, padding-right);
  274. }
  275. @mixin padding-bottom($value) {
  276. @include rfs($value, padding-bottom);
  277. }
  278. @mixin padding-left($value) {
  279. @include rfs($value, padding-left);
  280. }
  281. @mixin margin($value) {
  282. @include rfs($value, margin);
  283. }
  284. @mixin margin-top($value) {
  285. @include rfs($value, margin-top);
  286. }
  287. @mixin margin-right($value) {
  288. @include rfs($value, margin-right);
  289. }
  290. @mixin margin-bottom($value) {
  291. @include rfs($value, margin-bottom);
  292. }
  293. @mixin margin-left($value) {
  294. @include rfs($value, margin-left);
  295. }