_touch-target.scss 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // stylelint-disable selector-class-pattern --
  23. // Selector '.mdc-*' should only be used in this project.
  24. @use 'sass:math';
  25. @use '@material/base/mixins' as base-mixins;
  26. @use '@material/feature-targeting/feature-targeting';
  27. @use '@material/rtl/rtl';
  28. @use '@material/theme/theme';
  29. @use '@material/theme/keys';
  30. @use '@material/theme/custom-properties';
  31. $height: 48px !default;
  32. $width: $height !default;
  33. /// Styles applied to the component's touch target wrapper element.
  34. @mixin wrapper($query: feature-targeting.all()) {
  35. $feat-structure: feature-targeting.create-target($query, structure);
  36. .mdc-touch-target-wrapper {
  37. @include feature-targeting.targets($feat-structure) {
  38. // Ensure that styles are only emitted once across all components that
  39. // have increased touch targets.
  40. @include base-mixins.emit-once('mdc-touch-target/wrapper') {
  41. // NOTE: Will change to `inline-block` in the future, but keeping as is
  42. // temporarily for backwards-compatibility.
  43. display: inline;
  44. }
  45. }
  46. }
  47. }
  48. /// Styles applied to the component's inner touch target element.
  49. /// By default, only sets the inner element height to the minimum touch target
  50. /// height ($mdc-touch-target-height).
  51. /// @param {Boolean} $set-width [false] - Sets the inner element width to the
  52. /// minimum touch target width ($mdc-touch-target-width).
  53. /// @param $height [$mdc-touch-target-height] - Touch target height.
  54. /// @param $width [$mdc-touch-target-width] - Touch target width.
  55. @mixin touch-target(
  56. $set-width: false,
  57. $query: feature-targeting.all(),
  58. $height: $height,
  59. $width: $width
  60. ) {
  61. $feat-structure: feature-targeting.create-target($query, structure);
  62. @include feature-targeting.targets($feat-structure) {
  63. position: absolute;
  64. top: 50%;
  65. height: $height;
  66. }
  67. @if $set-width {
  68. @include feature-targeting.targets($feat-structure) {
  69. @include rtl.ignore-next-line();
  70. left: 50%;
  71. width: $width;
  72. @include rtl.ignore-next-line();
  73. transform: translate(-50%, -50%);
  74. }
  75. } @else {
  76. @include feature-targeting.targets($feat-structure) {
  77. left: 0;
  78. right: 0;
  79. transform: translateY(-50%);
  80. }
  81. }
  82. }
  83. /// Applies margin to the component with the increased touch target,
  84. /// to compensate for the touch target.
  85. @mixin margin(
  86. $component-height,
  87. $component-width: null,
  88. $touch-target-height: $height,
  89. $touch-target-width: $width,
  90. $query: feature-targeting.all()
  91. ) {
  92. $feat-structure: feature-targeting.create-target($query, structure);
  93. @include feature-targeting.targets($feat-structure) {
  94. @if keys.is-key($touch-target-height) or
  95. keys.is-key($component-height) or
  96. custom-properties.is-custom-prop($touch-target-height) or
  97. custom-properties.is-custom-prop($component-height) or
  98. custom-properties.is-custom-prop-string($touch-target-height) or
  99. custom-properties.is-custom-prop-string($component-height)
  100. {
  101. // Custom properties
  102. @include theme.property(
  103. margin-top,
  104. 'max((touch-target-height - component-height) / 2, 0px)',
  105. $replace: (
  106. component-height: $component-height,
  107. touch-target-height: $touch-target-height
  108. )
  109. );
  110. @include theme.property(
  111. margin-bottom,
  112. 'max((touch-target-height - component-height) / 2, 0px)',
  113. $replace: (
  114. component-height: $component-height,
  115. touch-target-height: $touch-target-height
  116. )
  117. );
  118. } @else {
  119. // Static values
  120. $vertical-margin-value: math.div(
  121. $touch-target-height - $component-height,
  122. 2
  123. );
  124. margin-top: $vertical-margin-value;
  125. margin-bottom: $vertical-margin-value;
  126. }
  127. }
  128. @if $component-width {
  129. @include feature-targeting.targets($feat-structure) {
  130. @if keys.is-key($touch-target-width) or
  131. keys.is-key($component-width) or
  132. custom-properties.is-custom-prop($touch-target-width) or
  133. custom-properties.is-custom-prop($component-width) or
  134. custom-properties.is-custom-prop-string($touch-target-width) or
  135. custom-properties.is-custom-prop-string($component-width)
  136. {
  137. // Custom properties
  138. @include theme.property(
  139. margin-right,
  140. 'max((touch-target-width - component-width) / 2, 0px)',
  141. $replace: (
  142. component-width: $component-width,
  143. touch-target-width: $touch-target-width
  144. )
  145. );
  146. @include theme.property(
  147. margin-left,
  148. 'max((touch-target-width - component-width) / 2), 0px',
  149. $replace: (
  150. component-width: $component-width,
  151. touch-target-width: $touch-target-width
  152. )
  153. );
  154. } @else {
  155. // Static values
  156. $horizontal-margin-value: math.div(
  157. $touch-target-width - $component-width,
  158. 2
  159. );
  160. margin-right: $horizontal-margin-value;
  161. margin-left: $horizontal-margin-value;
  162. }
  163. }
  164. }
  165. }