_density.scss 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $interval: 4px !default;
  26. $minimum-scale: minimum !default;
  27. $maximum-scale: maximum !default;
  28. $supported-scales: (default, minimum, maximum) !default;
  29. $supported-properties: (height, size, margin-top, margin-bottom, top) !default;
  30. $default-scale: 0 !default;
  31. ///
  32. /// Returns component property value based on given density config and density scale.
  33. ///
  34. /// @param {Map} $density-config - Density configuration for component.
  35. /// @param {Number | String} $density-scale - Density scale value for component. Examples: `-3`, `0` or `minimum`.
  36. /// @param {Map} $property-name - Density scale map for the target component.
  37. ///
  38. /// @example
  39. /// mdc-density-prop-value(
  40. /// $density-config: (
  41. /// height: (
  42. /// default: 36px,
  43. /// maximum: 40px,
  44. /// minimum: 24px,
  45. /// ),
  46. /// ),
  47. /// $density-scale: minimum,
  48. /// $property-name: height,
  49. /// )
  50. /// // 24px
  51. ///
  52. /// @example
  53. /// mdc-density-prop-value(
  54. /// $density-config: (
  55. /// height: (
  56. /// default: 40px,
  57. /// maximum: 60px,
  58. /// minimum: 24px,
  59. /// ),
  60. /// ),
  61. /// $density-scale: -2,
  62. /// $property-name: height,
  63. /// )
  64. /// // 32px
  65. ///
  66. /// @example
  67. /// mdc-density-prop-value(
  68. /// $density-config: (
  69. /// height: (
  70. /// default: 36px,
  71. /// maximum: 40px,
  72. /// minimum: 24px,
  73. /// ),
  74. /// width: (
  75. /// default: 56px,
  76. /// maximum: 64px,
  77. /// minimum: 48px,
  78. /// ),
  79. /// ),
  80. /// $density-scale: minimum,
  81. /// $property-name: width,
  82. /// )
  83. /// // 48px
  84. ///
  85. @function prop-value($density-config, $density-scale, $property-name) {
  86. @if (
  87. meta.type-of($density-scale) ==
  88. 'string' and
  89. list.index($list: $supported-scales, $value: $density-scale) ==
  90. null
  91. ) {
  92. @error "mdc-density: Supported density scales #{$supported-scales}, but received #{$density-scale}.";
  93. }
  94. @if (
  95. list.index($list: $supported-properties, $value: $property-name) == null
  96. ) {
  97. @error "mdc-density: Supported density properties #{$supported-properties}," +
  98. "but received #{$property-name}.";
  99. }
  100. $value: null;
  101. $property-scale-map: map.get($density-config, $property-name);
  102. @if map.has-key($property-scale-map, $density-scale) {
  103. $value: map.get($property-scale-map, $density-scale);
  104. } @else {
  105. $value: map.get($property-scale-map, default) + $density-scale * $interval;
  106. }
  107. $min-value: map.get($property-scale-map, $minimum-scale);
  108. $max-value: map.get($property-scale-map, $maximum-scale);
  109. @if ($value < $min-value or $value > $max-value) {
  110. @error "mdc-density: #{$property-name} must be between #{$min-value} and " +
  111. "#{$max-value} (inclusive), but received #{$value}.";
  112. }
  113. @return $value;
  114. }