|
| 1 | +// Foundation for Sites by ZURB |
| 2 | +// foundation.zurb.com |
| 3 | +// Licensed under MIT Open Source |
| 4 | + |
| 5 | +//// |
| 6 | +/// @group functions |
| 7 | +//// |
| 8 | + |
| 9 | +// scss-lint:disable ZeroUnit |
| 10 | + |
| 11 | +/// Defines the base font size of the page, which is the value `1rem` is equal to. |
| 12 | +/// @type Number |
| 13 | +/// @group global |
| 14 | +$rem-base: 16px !default; |
| 15 | + |
| 16 | +/// Removes the unit (e.g. px, em, rem) from a value, returning the number only. |
| 17 | +/// |
| 18 | +/// @param {Number} $num - Number to strip unit from. |
| 19 | +/// |
| 20 | +/// @returns {Number} The same number, sans unit. |
| 21 | +@function strip-unit($num) { |
| 22 | + @return $num / ($num * 0 + 1); |
| 23 | +} |
| 24 | + |
| 25 | +/// Converts one or more pixel values into matching rem values. |
| 26 | +/// |
| 27 | +/// @param {Number|List} $values - One or more values to convert. Be sure to separate them with spaces and not commas. If you need to convert a comma-separated list, wrap the list in parentheses. |
| 28 | +/// @param {Number} $base [$rem-base] - The base value to use when calculating the `rem`. If you're using Foundation out of the box, this is 16px. |
| 29 | +/// |
| 30 | +/// @returns {List} A list of converted values. |
| 31 | +@function rem-calc($values, $base: $rem-base) { |
| 32 | + $rem-values: (); |
| 33 | + $count: length($values); |
| 34 | + |
| 35 | + @if $base == null { |
| 36 | + $base: $rem-base; |
| 37 | + } |
| 38 | + |
| 39 | + @if $count == 1 { |
| 40 | + @return -zf-to-rem($values, $base); |
| 41 | + } |
| 42 | + |
| 43 | + @for $i from 1 through $count { |
| 44 | + $rem-values: append($rem-values, -zf-to-rem(nth($values, $i), $base)); |
| 45 | + } |
| 46 | + |
| 47 | + @return $rem-values; |
| 48 | +} |
| 49 | + |
| 50 | +// Converts a unitless, pixel, or rem value to em, for use in breakpoints. |
| 51 | +@function -zf-bp-to-em($value) { |
| 52 | + // Pixel and unitless values are converted to rems |
| 53 | + @if unit($value) == 'px' or unitless($value) { |
| 54 | + $value: rem-calc($value); |
| 55 | + } |
| 56 | + |
| 57 | + // Then the value is converted to ems |
| 58 | + @return strip-unit($value) * 1em; |
| 59 | +} |
| 60 | + |
| 61 | +/// Converts a pixel value to matching rem value. *Any* value passed, regardless of unit, is assumed to be a pixel value. By default, the base pixel value used to calculate the rem value is taken from the `$rem-base` variable. |
| 62 | +/// @access private |
| 63 | +/// |
| 64 | +/// @param {Number} $value - Pixel value to convert. |
| 65 | +/// @param {Number} $base [$rem-base] - Base for pixel conversion. |
| 66 | +/// |
| 67 | +/// @returns {Number} A number in rems, calculated based on the given value and the base pixel value. rem values are passed through as is. |
| 68 | +@function -zf-to-rem($value, $base: $rem-base) { |
| 69 | + // Calculate rem if units for $value is not rem |
| 70 | + @if (unit($value) != 'rem') { |
| 71 | + $value: strip-unit($value) / strip-unit($base) * 1rem; |
| 72 | + } |
| 73 | + // Turn 0rem into 0 |
| 74 | + @if ($value == 0rem) { $value: 0; } |
| 75 | + @return $value; |
| 76 | +} |
0 commit comments