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.

348 lines
9.8 KiB

9 months ago
  1. // stylelint-disable scss/dimension-no-non-numeric-values
  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. } @else {
  136. @content;
  137. }
  138. }
  139. // Internal mixin that adds enable classes to the selector if needed.
  140. @mixin _rfs-media-query-rule {
  141. @if $rfs-class == enable {
  142. @if $rfs-mode == min-media-query {
  143. @content;
  144. }
  145. @include _rfs-media-query () {
  146. .enable-rfs &,
  147. &.enable-rfs {
  148. @content;
  149. }
  150. }
  151. }
  152. @else {
  153. @if $rfs-class == disable and $rfs-mode == min-media-query {
  154. .disable-rfs &,
  155. &.disable-rfs {
  156. @content;
  157. }
  158. }
  159. @include _rfs-media-query () {
  160. @content;
  161. }
  162. }
  163. }
  164. // Helper function to get the formatted non-responsive value
  165. @function rfs-value($values) {
  166. // Convert to list
  167. $values: if(type-of($values) != list, ($values,), $values);
  168. $val: "";
  169. // Loop over each value and calculate value
  170. @each $value in $values {
  171. @if $value == 0 {
  172. $val: $val + " 0";
  173. }
  174. @else {
  175. // Cache $value unit
  176. $unit: if(type-of($value) == "number", unit($value), false);
  177. @if $unit == px {
  178. // Convert to rem if needed
  179. $val: $val + " " + if($rfs-unit == rem, #{divide($value, $value * 0 + $rfs-rem-value)}rem, $value);
  180. }
  181. @else if $unit == rem {
  182. // Convert to px if needed
  183. $val: $val + " " + if($rfs-unit == px, #{divide($value, $value * 0 + 1) * $rfs-rem-value}px, $value);
  184. } @else {
  185. // 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
  186. $val: $val + " " + $value;
  187. }
  188. }
  189. }
  190. // Remove first space
  191. @return unquote(str-slice($val, 2));
  192. }
  193. // Helper function to get the responsive value calculated by RFS
  194. @function rfs-fluid-value($values) {
  195. // Convert to list
  196. $values: if(type-of($values) != list, ($values,), $values);
  197. $val: "";
  198. // Loop over each value and calculate value
  199. @each $value in $values {
  200. @if $value == 0 {
  201. $val: $val + " 0";
  202. } @else {
  203. // Cache $value unit
  204. $unit: if(type-of($value) == "number", unit($value), false);
  205. // 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
  206. @if not $unit or $unit != px and $unit != rem {
  207. $val: $val + " " + $value;
  208. } @else {
  209. // Remove unit from $value for calculations
  210. $value: divide($value, $value * 0 + if($unit == px, 1, divide(1, $rfs-rem-value)));
  211. // Only add the media query if the value is greater than the minimum value
  212. @if abs($value) <= $rfs-base-value or not $enable-rfs {
  213. $val: $val + " " + if($rfs-unit == rem, #{divide($value, $rfs-rem-value)}rem, #{$value}px);
  214. }
  215. @else {
  216. // Calculate the minimum value
  217. $value-min: $rfs-base-value + divide(abs($value) - $rfs-base-value, $rfs-factor);
  218. // Calculate difference between $value and the minimum value
  219. $value-diff: abs($value) - $value-min;
  220. // Base value formatting
  221. $min-width: if($rfs-unit == rem, #{divide($value-min, $rfs-rem-value)}rem, #{$value-min}px);
  222. // Use negative value if needed
  223. $min-width: if($value < 0, -$min-width, $min-width);
  224. // Use `vmin` if two-dimensional is enabled
  225. $variable-unit: if($rfs-two-dimensional, vmin, vw);
  226. // Calculate the variable width between 0 and $rfs-breakpoint
  227. $variable-width: #{divide($value-diff * 100, $rfs-breakpoint)}#{$variable-unit};
  228. // Return the calculated value
  229. $val: $val + " calc(" + $min-width + if($value < 0, " - ", " + ") + $variable-width + ")";
  230. }
  231. }
  232. }
  233. }
  234. // Remove first space
  235. @return unquote(str-slice($val, 2));
  236. }
  237. // RFS mixin
  238. @mixin rfs($values, $property: font-size) {
  239. @if $values != null {
  240. $val: rfs-value($values);
  241. $fluid-val: rfs-fluid-value($values);
  242. // Do not print the media query if responsive & non-responsive values are the same
  243. @if $val == $fluid-val {
  244. #{$property}: $val;
  245. }
  246. @else {
  247. @include _rfs-rule () {
  248. #{$property}: if($rfs-mode == max-media-query, $val, $fluid-val);
  249. // Include safari iframe resize fix if needed
  250. min-width: if($rfs-safari-iframe-resize-bug-fix, (0 * 1vw), null);
  251. }
  252. @include _rfs-media-query-rule () {
  253. #{$property}: if($rfs-mode == max-media-query, $fluid-val, $val);
  254. }
  255. }
  256. }
  257. }
  258. // Shorthand helper mixins
  259. @mixin font-size($value) {
  260. @include rfs($value);
  261. }
  262. @mixin padding($value) {
  263. @include rfs($value, padding);
  264. }
  265. @mixin padding-top($value) {
  266. @include rfs($value, padding-top);
  267. }
  268. @mixin padding-right($value) {
  269. @include rfs($value, padding-right);
  270. }
  271. @mixin padding-bottom($value) {
  272. @include rfs($value, padding-bottom);
  273. }
  274. @mixin padding-left($value) {
  275. @include rfs($value, padding-left);
  276. }
  277. @mixin margin($value) {
  278. @include rfs($value, margin);
  279. }
  280. @mixin margin-top($value) {
  281. @include rfs($value, margin-top);
  282. }
  283. @mixin margin-right($value) {
  284. @include rfs($value, margin-right);
  285. }
  286. @mixin margin-bottom($value) {
  287. @include rfs($value, margin-bottom);
  288. }
  289. @mixin margin-left($value) {
  290. @include rfs($value, margin-left);
  291. }