_ll-functions.scss 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Custom Limitless functions
  4. *
  5. * Utility mixins and functions for evalutating source code across our variables, maps, and mixins.
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. //
  9. // Removes the unit (e.g. px, em, rem) from a value, returning the number only.
  10. //
  11. // @param {Number} $num - Number to strip unit from.
  12. //
  13. // @returns {Number} The same number, sans unit.
  14. @function strip-unit($num) {
  15. @return $num / ($num * 0 + 1);
  16. }
  17. //
  18. // Converts one or more pixel values into matching rem values.
  19. //
  20. // @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.
  21. // @param {Number} $base [null] - The base value to use when calculating the `rem`. If you're using Foundation out of the box, this is 16px. If this parameter is `null`, the function will reference the `$base-font-size` variable as the base.
  22. //
  23. // @returns {List} A list of converted values.
  24. $global-font-size: 100% !default;
  25. @function rem-calc($values, $base: null) {
  26. $rem-values: ();
  27. $count: length($values);
  28. // If no base is defined, defer to the global font size
  29. @if $base == null {
  30. $base: $global-font-size;
  31. }
  32. // If the base font size is a %, then multiply it by 16px
  33. // This is because 100% font size = 16px in most all browsers
  34. @if unit($base) == '%' {
  35. $base: ($base / 100%) * 16px;
  36. }
  37. // Using rem as base allows correct scaling
  38. @if unit($base) == 'rem' {
  39. $base: strip-unit($base) * 16px;
  40. }
  41. @if $count == 1 {
  42. @return -zf-to-rem($values, $base);
  43. }
  44. @for $i from 1 through $count {
  45. $rem-values: append($rem-values, -zf-to-rem(nth($values, $i), $base));
  46. }
  47. @return $rem-values;
  48. }
  49. //
  50. // 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 `$global-font-size` variable.
  51. // @access private
  52. //
  53. // @param {Number} $value - Pixel value to convert.
  54. // @param {Number} $base [null] - Base for pixel conversion.
  55. //
  56. // @returns {Number} A number in rems, calculated based on the given value and the base pixel value. rem values are passed through as is.
  57. @function -zf-to-rem($value, $base: null) {
  58. // Check if the value is a number
  59. @if type-of($value) != 'number' {
  60. @warn inspect($value) + ' was passed to rem-calc(), which is not a number.';
  61. @return $value;
  62. }
  63. // Transform em into rem if someone hands over 'em's
  64. @if unit($value) == 'em' {
  65. $value: strip-unit($value) * 1rem;
  66. }
  67. // Calculate rem if units for $value is not rem or em
  68. @if unit($value) != 'rem' {
  69. $value: strip-unit($value) / strip-unit($base) * 1rem;
  70. }
  71. // Turn 0rem into 0
  72. @if $value == 0rem {
  73. $value: 0;
  74. }
  75. @return $value;
  76. }