_functions.scss 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // Copyright 2019 Google Inc.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. @use "sass:list";
  23. @use "sass:map";
  24. @use "sass:meta";
  25. @use "./variables";
  26. ///
  27. /// Returns component property value based on given density config and density scale.
  28. ///
  29. /// @param {Map} $density-config - Density configuration for component.
  30. /// @param {Number | String} $density-scale - Density scale value for component. Examples: `-3`, `0` or `minimum`.
  31. /// @param {Map} $property-name - Density scale map for the target component.
  32. ///
  33. /// @example
  34. /// mdc-density-prop-value(
  35. /// $density-config: (
  36. /// height: (
  37. /// default: 36px,
  38. /// maximum: 40px,
  39. /// minimum: 24px,
  40. /// ),
  41. /// ),
  42. /// $density-scale: minimum,
  43. /// $property-name: height,
  44. /// )
  45. /// // 24px
  46. ///
  47. /// @example
  48. /// mdc-density-prop-value(
  49. /// $density-config: (
  50. /// height: (
  51. /// default: 40px,
  52. /// maximum: 60px,
  53. /// minimum: 24px,
  54. /// ),
  55. /// ),
  56. /// $density-scale: -2,
  57. /// $property-name: height,
  58. /// )
  59. /// // 32px
  60. ///
  61. /// @example
  62. /// mdc-density-prop-value(
  63. /// $density-config: (
  64. /// height: (
  65. /// default: 36px,
  66. /// maximum: 40px,
  67. /// minimum: 24px,
  68. /// ),
  69. /// width: (
  70. /// default: 56px,
  71. /// maximum: 64px,
  72. /// minimum: 48px,
  73. /// ),
  74. /// ),
  75. /// $density-scale: minimum,
  76. /// $property-name: width,
  77. /// )
  78. /// // 48px
  79. ///
  80. @function prop-value($density-config, $density-scale, $property-name) {
  81. @if (meta.type-of($density-scale) == "string" and
  82. list.index($list: variables.$supported-scales, $value: $density-scale) == null) {
  83. @error "mdc-density: Supported density scales #{variables.$supported-scales}, but received #{$density-scale}.";
  84. }
  85. @if (list.index($list: variables.$supported-properties, $value: $property-name) == null) {
  86. @error "mdc-density: Supported density properties #{variables.$supported-properties}," +
  87. "but received #{$property-name}.";
  88. }
  89. $value: null;
  90. $property-scale-map: map.get($density-config, $property-name);
  91. @if map.has-key($property-scale-map, $density-scale) {
  92. $value: map.get($property-scale-map, $density-scale);
  93. } @else {
  94. $value: map.get($property-scale-map, default) + $density-scale * variables.$interval;
  95. }
  96. $min-value: map.get($property-scale-map, variables.$minimum-scale);
  97. $max-value: map.get($property-scale-map, variables.$maximum-scale);
  98. @if ($value < $min-value or $value > $max-value) {
  99. @error "mdc-density: #{$property-name} must be between #{$min-value} and " +
  100. "#{$max-value} (inclusive), but received #{$value}.";
  101. }
  102. @return $value;
  103. }