_functions.sass 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. @use 'sass:list'
  2. @use 'sass:map'
  3. @use 'sass:math'
  4. @use 'sass:meta'
  5. @function map-deep-set($map, $keys, $value)
  6. $maps: ($map,)
  7. $result: null
  8. // If the last key is a map already
  9. // Warn the user we will be overriding it with $value
  10. @if meta.type-of(list.nth($keys, -1)) == "map"
  11. @warn "The last key you specified is a map; it will be overrided with `#{$value}`."
  12. // If $keys is a single key
  13. // Just merge and return
  14. @if list.length($keys) == 1
  15. @return map.merge($map, ( $keys: $value ))
  16. // Loop from the first to the second to last key from $keys
  17. // Store the associated map to this key in the $maps list
  18. // If the key doesn't exist, throw an error
  19. @for $i from 1 through list.length($keys) - 1
  20. $current-key: list.nth($keys, $i)
  21. $current-map: list.nth($maps, -1)
  22. $current-get: map.get($current-map, $current-key)
  23. @if $current-get == null
  24. @error "Key `#{$current-key}` doesn't exist at current level in map."
  25. $maps: list.append($maps, $current-get)
  26. // Loop from the last map to the first one
  27. // Merge it with the previous one
  28. @for $i from list.length($maps) through 1
  29. $current-map: list.nth($maps, $i)
  30. $current-key: list.nth($keys, $i)
  31. $current-val: if($i == list.length($maps), $value, $result)
  32. $result: map.merge($current-map, ($current-key: $current-val))
  33. // Return result
  34. @return $result
  35. @function map-deep-get($map, $keys...)
  36. @each $key in $keys
  37. $map: map.get($map, $key)
  38. @return $map
  39. @function breakpoint-min($name, $breakpoints)
  40. $min: map.get($breakpoints, $name)
  41. @return if($min != 0, $min, null)
  42. @function breakpoint-infix($name, $breakpoints)
  43. @return if(breakpoint-min($name, $breakpoints) == null, "", "-#{$name}")
  44. // Adapted from https://gist.github.com/pentzzsolt/4949bbd7691d43d00616dc4f1451cae9#file-non-destructive-map-merge-4-scss
  45. @function map-deep-merge($parent-map, $child-map)
  46. $result: $parent-map
  47. @each $key, $child in $child-map
  48. $parent-has-key: map.has-key($result, $key)
  49. $parent-value: map.get($result, $key)
  50. $parent-type: meta.type-of($parent-value)
  51. $child-type: meta.type-of($child)
  52. $parent-is-map: $parent-type == map
  53. $child-is-map: $child-type == map
  54. @if (not $parent-has-key) or ($parent-type != $child-type) or (not ($parent-is-map and $child-is-map))
  55. $result: map.merge($result, ( $key: $child ))
  56. @else
  57. $result: map.merge($result, ( $key: map-deep-merge($parent-value, $child) ))
  58. @return $result
  59. @function theme-color($color, $opacity: 1)
  60. $color: rgba(var(--v-theme-#{$color}), $opacity)
  61. @return $color
  62. @function roundEven($val)
  63. @return 2 * math.round($val * .5)