2023-01-03 10:52:20 +01:00
|
|
|
// stylelint-disable property-disallowed-list, scss/dollar-variable-default
|
2019-02-07 23:32:05 +01:00
|
|
|
|
|
|
|
// SCSS RFS mixin
|
|
|
|
//
|
2019-10-27 11:01:30 +01:00
|
|
|
// Automated responsive values for font sizes, paddings, margins and much more
|
2019-02-07 23:32:05 +01:00
|
|
|
//
|
2021-06-18 07:29:21 +02:00
|
|
|
// Licensed under MIT (https://github.com/twbs/rfs/blob/main/LICENSE)
|
2019-02-07 23:32:05 +01:00
|
|
|
|
|
|
|
// Configuration
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Base value
|
|
|
|
$rfs-base-value: 1.25rem !default;
|
|
|
|
$rfs-unit: rem !default;
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
@if $rfs-unit != rem and $rfs-unit != px {
|
|
|
|
@error "`#{$rfs-unit}` is not a valid unit for $rfs-unit. Use `px` or `rem`.";
|
2019-03-06 21:49:06 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Breakpoint at where values start decreasing if screen width is smaller
|
2019-02-07 23:32:05 +01:00
|
|
|
$rfs-breakpoint: 1200px !default;
|
|
|
|
$rfs-breakpoint-unit: px !default;
|
|
|
|
|
2019-03-06 21:49:06 +01:00
|
|
|
@if $rfs-breakpoint-unit != px and $rfs-breakpoint-unit != em and $rfs-breakpoint-unit != rem {
|
|
|
|
@error "`#{$rfs-breakpoint-unit}` is not a valid unit for $rfs-breakpoint-unit. Use `px`, `em` or `rem`.";
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Resize values based on screen height and width
|
2019-02-07 23:32:05 +01:00
|
|
|
$rfs-two-dimensional: false !default;
|
|
|
|
|
|
|
|
// Factor of decrease
|
|
|
|
$rfs-factor: 10 !default;
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
@if type-of($rfs-factor) != number or $rfs-factor <= 1 {
|
2019-02-07 23:32:05 +01:00
|
|
|
@error "`#{$rfs-factor}` is not a valid $rfs-factor, it must be greater than 1.";
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Mode. Possibilities: "min-media-query", "max-media-query"
|
|
|
|
$rfs-mode: min-media-query !default;
|
|
|
|
|
2019-02-07 23:32:05 +01:00
|
|
|
// Generate enable or disable classes. Possibilities: false, "enable" or "disable"
|
|
|
|
$rfs-class: false !default;
|
|
|
|
|
|
|
|
// 1 rem = $rfs-rem-value px
|
|
|
|
$rfs-rem-value: 16 !default;
|
|
|
|
|
|
|
|
// Safari iframe resize bug: https://github.com/twbs/rfs/issues/14
|
|
|
|
$rfs-safari-iframe-resize-bug-fix: false !default;
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Disable RFS by setting $enable-rfs to false
|
|
|
|
$enable-rfs: true !default;
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Cache $rfs-base-value unit
|
|
|
|
$rfs-base-value-unit: unit($rfs-base-value);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2021-06-18 07:29:21 +02:00
|
|
|
@function divide($dividend, $divisor, $precision: 10) {
|
|
|
|
$sign: if($dividend > 0 and $divisor > 0 or $dividend < 0 and $divisor < 0, 1, -1);
|
|
|
|
$dividend: abs($dividend);
|
|
|
|
$divisor: abs($divisor);
|
|
|
|
@if $dividend == 0 {
|
|
|
|
@return 0;
|
|
|
|
}
|
|
|
|
@if $divisor == 0 {
|
|
|
|
@error "Cannot divide by 0";
|
|
|
|
}
|
|
|
|
$remainder: $dividend;
|
|
|
|
$result: 0;
|
|
|
|
$factor: 10;
|
|
|
|
@while ($remainder > 0 and $precision >= 0) {
|
|
|
|
$quotient: 0;
|
|
|
|
@while ($remainder >= $divisor) {
|
|
|
|
$remainder: $remainder - $divisor;
|
|
|
|
$quotient: $quotient + 1;
|
|
|
|
}
|
|
|
|
$result: $result * 10 + $quotient;
|
|
|
|
$factor: $factor * .1;
|
|
|
|
$remainder: $remainder * 10;
|
|
|
|
$precision: $precision - 1;
|
|
|
|
@if ($precision < 0 and $remainder >= $divisor * 5) {
|
|
|
|
$result: $result + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$result: $result * $factor * $sign;
|
|
|
|
$dividend-unit: unit($dividend);
|
|
|
|
$divisor-unit: unit($divisor);
|
|
|
|
$unit-map: (
|
|
|
|
"px": 1px,
|
|
|
|
"rem": 1rem,
|
|
|
|
"em": 1em,
|
|
|
|
"%": 1%
|
|
|
|
);
|
|
|
|
@if ($dividend-unit != $divisor-unit and map-has-key($unit-map, $dividend-unit)) {
|
|
|
|
$result: $result * map-get($unit-map, $dividend-unit);
|
|
|
|
}
|
|
|
|
@return $result;
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Remove px-unit from $rfs-base-value for calculations
|
|
|
|
@if $rfs-base-value-unit == px {
|
2021-06-18 07:29:21 +02:00
|
|
|
$rfs-base-value: divide($rfs-base-value, $rfs-base-value * 0 + 1);
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
@else if $rfs-base-value-unit == rem {
|
2021-06-18 07:29:21 +02:00
|
|
|
$rfs-base-value: divide($rfs-base-value, divide($rfs-base-value * 0 + 1, $rfs-rem-value));
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Cache $rfs-breakpoint unit to prevent multiple calls
|
|
|
|
$rfs-breakpoint-unit-cache: unit($rfs-breakpoint);
|
|
|
|
|
|
|
|
// Remove unit from $rfs-breakpoint for calculations
|
2019-10-27 11:01:30 +01:00
|
|
|
@if $rfs-breakpoint-unit-cache == px {
|
2021-06-18 07:29:21 +02:00
|
|
|
$rfs-breakpoint: divide($rfs-breakpoint, $rfs-breakpoint * 0 + 1);
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
@else if $rfs-breakpoint-unit-cache == rem or $rfs-breakpoint-unit-cache == "em" {
|
2021-06-18 07:29:21 +02:00
|
|
|
$rfs-breakpoint: divide($rfs-breakpoint, divide($rfs-breakpoint * 0 + 1, $rfs-rem-value));
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Calculate the media query value
|
2021-06-18 07:29:21 +02:00
|
|
|
$rfs-mq-value: if($rfs-breakpoint-unit == px, #{$rfs-breakpoint}px, #{divide($rfs-breakpoint, $rfs-rem-value)}#{$rfs-breakpoint-unit});
|
2019-10-27 11:01:30 +01:00
|
|
|
$rfs-mq-property-width: if($rfs-mode == max-media-query, max-width, min-width);
|
|
|
|
$rfs-mq-property-height: if($rfs-mode == max-media-query, max-height, min-height);
|
|
|
|
|
|
|
|
// Internal mixin used to determine which media query needs to be used
|
|
|
|
@mixin _rfs-media-query {
|
|
|
|
@if $rfs-two-dimensional {
|
2020-04-14 14:04:29 +02:00
|
|
|
@if $rfs-mode == max-media-query {
|
|
|
|
@media (#{$rfs-mq-property-width}: #{$rfs-mq-value}), (#{$rfs-mq-property-height}: #{$rfs-mq-value}) {
|
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@else {
|
|
|
|
@media (#{$rfs-mq-property-width}: #{$rfs-mq-value}) and (#{$rfs-mq-property-height}: #{$rfs-mq-value}) {
|
|
|
|
@content;
|
|
|
|
}
|
2019-03-06 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
@else {
|
2019-10-27 11:01:30 +01:00
|
|
|
@media (#{$rfs-mq-property-width}: #{$rfs-mq-value}) {
|
|
|
|
@content;
|
|
|
|
}
|
2019-03-06 21:49:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Internal mixin that adds disable classes to the selector if needed.
|
|
|
|
@mixin _rfs-rule {
|
|
|
|
@if $rfs-class == disable and $rfs-mode == max-media-query {
|
|
|
|
// Adding an extra class increases specificity, which prevents the media query to override the property
|
|
|
|
&,
|
|
|
|
.disable-rfs &,
|
|
|
|
&.disable-rfs {
|
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@else if $rfs-class == enable and $rfs-mode == min-media-query {
|
|
|
|
.enable-rfs &,
|
|
|
|
&.enable-rfs {
|
2019-03-06 21:49:06 +01:00
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@else {
|
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Internal mixin that adds enable classes to the selector if needed.
|
|
|
|
@mixin _rfs-media-query-rule {
|
|
|
|
|
|
|
|
@if $rfs-class == enable {
|
|
|
|
@if $rfs-mode == min-media-query {
|
2019-03-06 21:49:06 +01:00
|
|
|
@content;
|
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
|
|
|
|
@include _rfs-media-query {
|
|
|
|
.enable-rfs &,
|
|
|
|
&.enable-rfs {
|
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
2019-03-06 21:49:06 +01:00
|
|
|
}
|
|
|
|
@else {
|
2019-10-27 11:01:30 +01:00
|
|
|
@if $rfs-class == disable and $rfs-mode == min-media-query {
|
|
|
|
.disable-rfs &,
|
|
|
|
&.disable-rfs {
|
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@include _rfs-media-query {
|
2019-03-06 21:49:06 +01:00
|
|
|
@content;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Helper function to get the formatted non-responsive value
|
|
|
|
@function rfs-value($values) {
|
|
|
|
// Convert to list
|
|
|
|
$values: if(type-of($values) != list, ($values,), $values);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
$val: '';
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Loop over each value and calculate value
|
|
|
|
@each $value in $values {
|
|
|
|
@if $value == 0 {
|
|
|
|
$val: $val + ' 0';
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
@else {
|
|
|
|
// Cache $value unit
|
|
|
|
$unit: if(type-of($value) == "number", unit($value), false);
|
|
|
|
|
|
|
|
@if $unit == px {
|
|
|
|
// Convert to rem if needed
|
2021-06-18 07:29:21 +02:00
|
|
|
$val: $val + ' ' + if($rfs-unit == rem, #{divide($value, $value * 0 + $rfs-rem-value)}rem, $value);
|
2019-10-27 11:01:30 +01:00
|
|
|
}
|
|
|
|
@else if $unit == rem {
|
|
|
|
// Convert to px if needed
|
2021-06-18 07:29:21 +02:00
|
|
|
$val: $val + ' ' + if($rfs-unit == px, #{divide($value, $value * 0 + 1) * $rfs-rem-value}px, $value);
|
2019-10-27 11:01:30 +01:00
|
|
|
}
|
|
|
|
@else {
|
|
|
|
// 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
|
|
|
|
$val: $val + ' ' + $value;
|
|
|
|
}
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove first space
|
|
|
|
@return unquote(str-slice($val, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helper function to get the responsive value calculated by RFS
|
|
|
|
@function rfs-fluid-value($values) {
|
|
|
|
// Convert to list
|
|
|
|
$values: if(type-of($values) != list, ($values,), $values);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
$val: '';
|
2019-03-06 21:49:06 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Loop over each value and calculate value
|
|
|
|
@each $value in $values {
|
|
|
|
@if $value == 0 {
|
|
|
|
$val: $val + ' 0';
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
|
2019-02-07 23:32:05 +01:00
|
|
|
@else {
|
2019-10-27 11:01:30 +01:00
|
|
|
// Cache $value unit
|
|
|
|
$unit: if(type-of($value) == "number", unit($value), false);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// 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
|
|
|
|
@if not $unit or $unit != px and $unit != rem {
|
|
|
|
$val: $val + ' ' + $value;
|
|
|
|
}
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
@else {
|
|
|
|
// Remove unit from $value for calculations
|
2021-06-18 07:29:21 +02:00
|
|
|
$value: divide($value, $value * 0 + if($unit == px, 1, divide(1, $rfs-rem-value)));
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Only add the media query if the value is greater than the minimum value
|
|
|
|
@if abs($value) <= $rfs-base-value or not $enable-rfs {
|
2021-06-18 07:29:21 +02:00
|
|
|
$val: $val + ' ' + if($rfs-unit == rem, #{divide($value, $rfs-rem-value)}rem, #{$value}px);
|
2019-10-27 11:01:30 +01:00
|
|
|
}
|
|
|
|
@else {
|
|
|
|
// Calculate the minimum value
|
2021-06-18 07:29:21 +02:00
|
|
|
$value-min: $rfs-base-value + divide(abs($value) - $rfs-base-value, $rfs-factor);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Calculate difference between $value and the minimum value
|
|
|
|
$value-diff: abs($value) - $value-min;
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Base value formatting
|
2021-06-18 07:29:21 +02:00
|
|
|
$min-width: if($rfs-unit == rem, #{divide($value-min, $rfs-rem-value)}rem, #{$value-min}px);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Use negative value if needed
|
|
|
|
$min-width: if($value < 0, -$min-width, $min-width);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Use `vmin` if two-dimensional is enabled
|
|
|
|
$variable-unit: if($rfs-two-dimensional, vmin, vw);
|
|
|
|
|
|
|
|
// Calculate the variable width between 0 and $rfs-breakpoint
|
2021-06-18 07:29:21 +02:00
|
|
|
$variable-width: #{divide($value-diff * 100, $rfs-breakpoint)}#{$variable-unit};
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Return the calculated value
|
|
|
|
$val: $val + ' calc(' + $min-width + if($value < 0, ' - ', ' + ') + $variable-width + ')';
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove first space
|
|
|
|
@return unquote(str-slice($val, 2));
|
|
|
|
}
|
|
|
|
|
|
|
|
// RFS mixin
|
|
|
|
@mixin rfs($values, $property: font-size) {
|
|
|
|
@if $values != null {
|
|
|
|
$val: rfs-value($values);
|
|
|
|
$fluidVal: rfs-fluid-value($values);
|
|
|
|
|
|
|
|
// Do not print the media query if responsive & non-responsive values are the same
|
|
|
|
@if $val == $fluidVal {
|
|
|
|
#{$property}: $val;
|
|
|
|
}
|
|
|
|
@else {
|
|
|
|
@include _rfs-rule {
|
|
|
|
#{$property}: if($rfs-mode == max-media-query, $val, $fluidVal);
|
2019-02-07 23:32:05 +01:00
|
|
|
|
2019-03-06 21:49:06 +01:00
|
|
|
// Include safari iframe resize fix if needed
|
|
|
|
min-width: if($rfs-safari-iframe-resize-bug-fix, (0 * 1vw), null);
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
2019-10-27 11:01:30 +01:00
|
|
|
|
|
|
|
@include _rfs-media-query-rule {
|
|
|
|
#{$property}: if($rfs-mode == max-media-query, $fluidVal, $val);
|
|
|
|
}
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
// Shorthand helper mixins
|
|
|
|
@mixin font-size($value) {
|
|
|
|
@include rfs($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin padding($value) {
|
|
|
|
@include rfs($value, padding);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin padding-top($value) {
|
|
|
|
@include rfs($value, padding-top);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin padding-right($value) {
|
|
|
|
@include rfs($value, padding-right);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin padding-bottom($value) {
|
|
|
|
@include rfs($value, padding-bottom);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin padding-left($value) {
|
|
|
|
@include rfs($value, padding-left);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin margin($value) {
|
|
|
|
@include rfs($value, margin);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin margin-top($value) {
|
|
|
|
@include rfs($value, margin-top);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin margin-right($value) {
|
|
|
|
@include rfs($value, margin-right);
|
|
|
|
}
|
|
|
|
|
|
|
|
@mixin margin-bottom($value) {
|
|
|
|
@include rfs($value, margin-bottom);
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|
|
|
|
|
2019-10-27 11:01:30 +01:00
|
|
|
@mixin margin-left($value) {
|
|
|
|
@include rfs($value, margin-left);
|
2019-02-07 23:32:05 +01:00
|
|
|
}
|