_functions.scss 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Remove the unit of a length
  2. // @param {Number} $number - Number to remove unit from
  3. // @return {Number} - Unitless number
  4. @function strip-unit($number) {
  5. @if type-of($number) == 'number' and not unitless($number) {
  6. @return $number / ($number * 0 + 1);
  7. }
  8. @return $number;
  9. }
  10. // ----
  11. // Sass (v3.3.14)
  12. // Compass (v1.0.0.rc.1)
  13. // ----
  14. @function pow($x, $y) {
  15. $ret: 1;
  16. @if $y > 0 {
  17. @for $i from 1 through $y {
  18. $ret: $ret * $x;
  19. }
  20. }
  21. @else {
  22. @for $i from $y to 0 {
  23. $ret: $ret / $x;
  24. }
  25. }
  26. @return $ret;
  27. }
  28. // Map deep get
  29. // @author Hugo Giraudel
  30. // @access public
  31. // @param {Map} $map - Map
  32. // @param {Arglist} $keys - Key chain
  33. // @return {*} - Desired value
  34. //
  35. // Example:
  36. // $m-breakpoint: map-deep-get($__prefix-default-config, "layouts", "M");
  37. @function map-deep-get($map, $keys...) {
  38. @each $key in $keys {
  39. $map: map-get($map, $key);
  40. }
  41. @return $map;
  42. }
  43. // ep set function to set a value in nested maps
  44. // uthor Hugo Giraudel
  45. // ccess public
  46. // aram {Map} $map - Map
  47. // aram {List} $keys - Key chaine
  48. // aram {*} $value - Value to assign
  49. // eturn {Map}
  50. //
  51. // ample:
  52. // _prefix-default-config: map-deep-set($__prefix-default-config, "layouts" "M", 650px);
  53. @function map-deep-set($map, $keys, $value) {
  54. $maps: ($map);
  55. $result: null;
  56. // If the last key is a map already
  57. // Warn the user we will be overriding it with $value
  58. @if type-of(nth($keys, -1)) == "map" {
  59. @warn "The last key you specified is a map; it will be overrided with `#{$value}`.";
  60. }
  61. // If $keys is a single key
  62. // Just merge and return
  63. @if length($keys) == 1 {
  64. @return map-merge($map, ($keys: $value));
  65. }
  66. // Loop from the first to the second to last key from $keys
  67. // Store the associated map to this key in the $maps list
  68. // If the key doesn't exist, throw an error
  69. @for $i from 1 through length($keys) - 1 {
  70. $current-key: nth($keys, $i);
  71. $current-map: nth($maps, -1);
  72. $current-get: map-get($current-map, $current-key);
  73. @if $current-get == null {
  74. @error "Key `#{$key}` doesn't exist at current level in map.";
  75. }
  76. $maps: append($maps, $current-get);
  77. }
  78. // Loop from the last map to the first one
  79. // Merge it with the previous one
  80. @for $i from length($maps) through 1 {
  81. $current-map: nth($maps, $i);
  82. $current-key: nth($keys, $i);
  83. $current-val: if($i == length($maps), $value, $result);
  84. $result: map-merge($current-map, ($current-key: $current-val));
  85. }
  86. // Return result
  87. @return $result;
  88. }
  89. // jQuery-style extend function
  90. // - Child themes can use this function to `reset` the values in
  91. // config maps without editing the `master` Sass files.
  92. // - src: https://www.sitepoint.com/extra-map-functions-sass/
  93. // - About `map-merge()`:
  94. // - - only takes 2 arguments
  95. // - - is not recursive
  96. // @param {Map} $map - first map
  97. // @param {ArgList} $maps - other maps
  98. // @param {Bool} $deep - recursive mode
  99. // @return {Map}
  100. // Examples:
  101. // $grid-configuration-default: (
  102. // 'columns': 12,
  103. // 'layouts': (
  104. // 'small': 800px,
  105. // 'medium': 1000px,
  106. // 'large': 1200px,
  107. // ),
  108. // );
  109. // $grid-configuration-custom: (
  110. // 'layouts': (
  111. // 'large': 1300px,
  112. // 'huge': 1500px
  113. // ),
  114. // );
  115. // $grid-configuration-user: (
  116. // 'direction': 'ltr',
  117. // 'columns': 16,
  118. // 'layouts': (
  119. // 'large': 1300px,
  120. // 'huge': 1500px
  121. // ),
  122. // );
  123. // $deep: false
  124. // $grid-configuration: map-extend($grid-configuration-default, $grid-configuration-custom, $grid-configuration-user);
  125. // --> ("columns": 16, "layouts": (("large": 1300px, "huge": 1500px)), "direction": "ltr")
  126. // $deep: true
  127. // $grid-configuration: map-extend($grid-configuration-default, $grid-configuration-custom, $grid-configuration-user, true);
  128. // --> ("columns": 16, "layouts": (("small": 800px, "medium": 1000px, "large": 1300px, "huge": 1500px)), "direction": "ltr")
  129. @function map-extend($map, $maps.../*, $deep */) {
  130. $last: nth($maps, -1);
  131. $deep: $last == true;
  132. $max: if($deep, length($maps) - 1, length($maps));
  133. // Loop through all maps in $maps...
  134. @for $i from 1 through $max {
  135. // Store current map
  136. $current: nth($maps, $i);
  137. // If not in deep mode, simply merge current map with map
  138. @if not $deep {
  139. $map: map-merge($map, $current);
  140. } @else {
  141. // If in deep mode, loop through all tuples in current map
  142. @each $key, $value in $current {
  143. // If value is a nested map and same key from map is a nested map as well
  144. @if type-of($value) == "map" and type-of(map-get($map, $key)) == "map" {
  145. // Recursive extend
  146. $value: map-extend(map-get($map, $key), $value, true);
  147. }
  148. // Merge current tuple with map
  149. $map: map-merge($map, ($key: $value));
  150. }
  151. }
  152. }
  153. @return $map;
  154. }