_button-outlined-theme.scss 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // Copyright 2021 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:map';
  25. @use 'sass:math';
  26. @use '@material/feature-targeting/feature-targeting';
  27. @use '@material/theme/custom-properties';
  28. @use '@material/theme/keys';
  29. @use '@material/theme/state';
  30. @use '@material/theme/theme';
  31. @use '@material/theme/theme-color';
  32. @use '@material/tokens/resolvers';
  33. @use '@material/ripple/ripple-theme';
  34. @use './button-base';
  35. @use './button-shared-theme';
  36. @use './button-ripple';
  37. $outlined-border-width: 1px !default;
  38. $outline-color: rgba(theme-color.prop-value(on-surface), 0.12) !default;
  39. $custom-property-prefix: 'outlined-button';
  40. $light-theme: (
  41. container-height: button-shared-theme.$height,
  42. container-shape: button-shared-theme.$shape-radius,
  43. disabled-label-text-color: button-shared-theme.$disabled-ink-color,
  44. disabled-outline-color: button-shared-theme.$disabled-container-color,
  45. focus-label-text-color: null,
  46. focus-outline-color: null,
  47. focus-state-layer-color: primary,
  48. focus-state-layer-opacity: 0.12,
  49. hover-label-text-color: null,
  50. hover-outline-color: null,
  51. hover-state-layer-color: primary,
  52. hover-state-layer-opacity: 0.04,
  53. /// Prevent the increased touch target from being reseted if the
  54. /// container-height differs from the default (36px)
  55. keep-touch-target: false,
  56. label-text-color: primary,
  57. label-text-font: button-font-family,
  58. label-text-size: button-font-size,
  59. label-text-tracking: button-letter-spacing,
  60. label-text-transform: button-text-transform,
  61. label-text-weight: button-font-weight,
  62. outline-color: $outline-color,
  63. outline-width: $outlined-border-width,
  64. pressed-label-text-color: null,
  65. pressed-outline-color: null,
  66. pressed-state-layer-color: primary,
  67. pressed-state-layer-opacity: 0.12,
  68. with-icon-icon-color: null,
  69. with-icon-icon-size: 18px,
  70. with-icon-hover-icon-color: null,
  71. with-icon-focus-icon-color: null,
  72. with-icon-pressed-icon-color: null,
  73. with-icon-disabled-icon-color: null,
  74. );
  75. /// Sets theme based on provided theme configuration.
  76. /// Only emits theme related styles.
  77. /// @param {Map} $theme - Theme configuration to use.
  78. @mixin theme($theme, $resolver: resolvers.$material) {
  79. @include theme.validate-theme($light-theme, $theme);
  80. $theme: button-shared-theme.resolve-theme-elevation-keys(
  81. $theme,
  82. $resolver: $resolver
  83. );
  84. @include keys.declare-custom-properties($theme, $custom-property-prefix);
  85. }
  86. @mixin theme-styles(
  87. $theme,
  88. $resolver: resolvers.$material,
  89. $query: feature-targeting.all()
  90. ) {
  91. @include theme.validate-theme-styles($light-theme, $theme);
  92. $theme: keys.create-theme-properties(
  93. $theme,
  94. $prefix: $custom-property-prefix
  95. );
  96. @include _theme($theme, $resolver, $query: $query);
  97. }
  98. @mixin _theme($theme, $resolver, $query) {
  99. @include button-shared-theme.theme-styles($theme, $resolver, $query: $query);
  100. @include outline-color(
  101. (
  102. default: map.get($theme, outline-color),
  103. disabled: map.get($theme, disabled-outline-color),
  104. focus: map.get($theme, focus-outline-color),
  105. hover: map.get($theme, hover-outline-color),
  106. pressed: map.get($theme, pressed-outline-color),
  107. ),
  108. $query: $query
  109. );
  110. @include outline-width(map.get($theme, outline-width), $query: $query);
  111. }
  112. @mixin deprecated-theme-styles($query: feature-targeting.all()) {
  113. .mdc-button--outlined {
  114. $theme: map.merge(
  115. $light-theme,
  116. (
  117. focus-state-layer-color: null,
  118. focus-state-layer-opacity: null,
  119. hover-state-layer-color: null,
  120. hover-state-layer-opacity: null,
  121. pressed-state-layer-color: null,
  122. pressed-state-layer-opacity: null,
  123. label-text-font: null,
  124. label-text-size: null,
  125. label-text-tracking: null,
  126. label-text-transform: null,
  127. label-text-weight: null,
  128. )
  129. );
  130. @include _theme($theme, resolvers.$material, $query: $query);
  131. }
  132. }
  133. ///
  134. /// Sets the outline color to the given color for an enabled button.
  135. /// @param {Color} $color-or-map - The desired outline color, specified either
  136. /// as a flat value or a map of colors with states
  137. /// {default, hover, focused, pressed, disabled} as keys.
  138. ///
  139. @mixin outline-color($color-or-map, $query: feature-targeting.all()) {
  140. &:not(:disabled) {
  141. @include _outline-color(
  142. state.get-default-state($color-or-map),
  143. $query: $query
  144. );
  145. &:hover {
  146. @include _outline-color(
  147. state.get-hover-state($color-or-map),
  148. $query: $query
  149. );
  150. }
  151. @include ripple-theme.focus() {
  152. @include _outline-color(
  153. state.get-focus-state($color-or-map),
  154. $query: $query
  155. );
  156. }
  157. // Increase active state specificity due to ripple-theme.focus().
  158. &:active,
  159. &:focus:active {
  160. @include _outline-color(
  161. state.get-pressed-state($color-or-map),
  162. $query: $query
  163. );
  164. }
  165. }
  166. &:disabled {
  167. @include _outline-color(
  168. state.get-disabled-state($color-or-map),
  169. $query: $query
  170. );
  171. }
  172. }
  173. ///
  174. /// Sets the outline color to the given color for a disabled button.
  175. /// @param {Color} $color - The desired outline color.
  176. /// @deprecated - call `outline-color` instead with `disabled` as a map key.
  177. ///
  178. @mixin disabled-outline-color($color, $query: feature-targeting.all()) {
  179. @include outline-color(
  180. (
  181. disabled: $color,
  182. ),
  183. $query: $query
  184. );
  185. }
  186. @mixin outline-width(
  187. $outline-width,
  188. $padding: button-shared-theme.$contained-horizontal-padding,
  189. // For a button with an icon, the padding on the side of the button with the
  190. // icon.
  191. $padding-icon: button-shared-theme.$contained-horizontal-padding-icon,
  192. $query: feature-targeting.all()
  193. ) {
  194. $feat-structure: feature-targeting.create-target($query, structure);
  195. @if $outline-width != null {
  196. $outline-width-value: if(
  197. custom-properties.is-custom-prop($outline-width),
  198. custom-properties.get-fallback($outline-width),
  199. $outline-width
  200. );
  201. // TODO(b/194792044): uncouple padding from outline-width
  202. // Note: Adjust padding to maintain consistent width with non-outlined buttons
  203. $padding-value: math.max($padding - $outline-width-value, 0);
  204. $padding-icon-value: math.max($padding-icon - $outline-width-value, 0);
  205. @include button-shared-theme.horizontal-padding(
  206. $padding: $padding-value,
  207. $padding-icon: $padding-icon-value,
  208. $query: $query
  209. );
  210. @include feature-targeting.targets($feat-structure) {
  211. @include theme.property(border-width, $outline-width);
  212. }
  213. #{button-ripple.$ripple-target} {
  214. @include feature-targeting.targets($feat-structure) {
  215. @include theme.property(top, -1 * $outline-width-value);
  216. @include theme.property(left, -1 * $outline-width-value);
  217. @include theme.property(bottom, -1 * $outline-width-value);
  218. @include theme.property(right, -1 * $outline-width-value);
  219. @include theme.property(border-width, $outline-width);
  220. }
  221. }
  222. .mdc-button__touch {
  223. @include feature-targeting.targets($feat-structure) {
  224. @include theme.property(
  225. left,
  226. 'calc(-1 * outline-width)',
  227. $replace: (outline-width: $outline-width)
  228. );
  229. @include theme.property(
  230. width,
  231. 'calc(100% + 2 * outline-width)',
  232. $replace: (outline-width: $outline-width)
  233. );
  234. }
  235. }
  236. }
  237. }
  238. ///
  239. /// Sets the outline color to the given color. This mixin should be
  240. /// wrapped in a selector that qualifies button state.
  241. /// @access private
  242. ///
  243. @mixin _outline-color($color, $query: feature-targeting.all()) {
  244. $feat-color: feature-targeting.create-target($query, color);
  245. @if $color {
  246. @include feature-targeting.targets($feat-color) {
  247. @include theme.property(border-color, $color);
  248. }
  249. }
  250. }