_icon-button-theme.scss 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. @use 'sass:map';
  2. @use 'sass:math';
  3. @use '../core/tokens/m2/mdc/icon-button' as tokens-mdc-icon-button;
  4. @use '../core/tokens/m2/mat/icon-button' as tokens-mat-icon-button;
  5. @use '../core/style/sass-utils';
  6. @use '../core/tokens/token-utils';
  7. @use '../core/theming/theming';
  8. @use '../core/theming/inspection';
  9. @use '../core/theming/validation';
  10. @mixin base($theme) {
  11. @if inspection.get-theme-version($theme) == 1 {
  12. @include _theme-from-tokens(inspection.get-theme-tokens($theme, base));
  13. } @else {
  14. // Add default values for tokens not related to color, typography, or density.
  15. @include sass-utils.current-selector-or-root() {
  16. @include token-utils.create-token-values(
  17. tokens-mdc-icon-button.$prefix,
  18. tokens-mdc-icon-button.get-unthemable-tokens()
  19. );
  20. }
  21. }
  22. }
  23. @mixin _icon-button-variant($theme, $palette) {
  24. $mdc-tokens: if(
  25. $palette,
  26. tokens-mdc-icon-button.private-get-color-palette-color-tokens($theme, $palette),
  27. tokens-mdc-icon-button.get-color-tokens($theme)
  28. );
  29. $mat-tokens: if(
  30. $palette,
  31. tokens-mat-icon-button.private-get-color-palette-color-tokens($theme, $palette),
  32. tokens-mat-icon-button.get-color-tokens($theme)
  33. );
  34. @include token-utils.create-token-values(tokens-mdc-icon-button.$prefix, $mdc-tokens);
  35. @include token-utils.create-token-values(tokens-mat-icon-button.$prefix, $mat-tokens);
  36. }
  37. /// Outputs color theme styles for the mat-icon-button.
  38. /// @param {Map} $theme The theme to generate color styles for.
  39. /// @param {ArgList} Additional optional arguments (only supported for M3 themes):
  40. /// $color-variant: The color variant to use for the button: primary, secondary, tertiary, or error.
  41. @mixin color($theme, $options...) {
  42. @if inspection.get-theme-version($theme) == 1 {
  43. @include _theme-from-tokens(inspection.get-theme-tokens($theme, color), $options...);
  44. } @else {
  45. @include sass-utils.current-selector-or-root() {
  46. @include _icon-button-variant($theme, null);
  47. .mat-mdc-icon-button {
  48. &.mat-primary {
  49. @include _icon-button-variant($theme, primary);
  50. }
  51. &.mat-accent {
  52. @include _icon-button-variant($theme, accent);
  53. }
  54. &.mat-warn {
  55. @include _icon-button-variant($theme, warn);
  56. }
  57. }
  58. }
  59. }
  60. }
  61. @mixin typography($theme) {
  62. @if inspection.get-theme-version($theme) == 1 {
  63. @include _theme-from-tokens(inspection.get-theme-tokens($theme, typography));
  64. } @else {
  65. @include sass-utils.current-selector-or-root() {
  66. @include token-utils.create-token-values(
  67. tokens-mat-icon-button.$prefix,
  68. tokens-mat-icon-button.get-typography-tokens($theme)
  69. );
  70. }
  71. }
  72. }
  73. @mixin density($theme) {
  74. @if inspection.get-theme-version($theme) == 1 {
  75. @include _theme-from-tokens(inspection.get-theme-tokens($theme, density));
  76. } @else {
  77. $icon-size: 24px;
  78. $density-scale: inspection.get-theme-density($theme);
  79. $size-map: (
  80. 0: 48px,
  81. -1: 44px,
  82. -2: 40px,
  83. -3: 36px,
  84. -4: 32px,
  85. -5: 28px,
  86. );
  87. $calculated-size: map.get($size-map, $density-scale);
  88. @include sass-utils.current-selector-or-root() {
  89. @include token-utils.create-token-values(
  90. tokens-mat-icon-button.$prefix,
  91. tokens-mat-icon-button.get-density-tokens($theme)
  92. );
  93. }
  94. // Use `mat-mdc-button-base` to increase the specificity over the button's structural styles.
  95. .mat-mdc-icon-button.mat-mdc-button-base {
  96. // Match the styles that used to be present. This is necessary for backwards
  97. // compat to match the previous implementations selector count (two classes).
  98. --mdc-icon-button-state-layer-size: #{$calculated-size};
  99. // TODO: Switch calculated-size to "var(--mdc-icon-button-state-layer-size)"
  100. // Currently fails validation because the variable is "undefined"
  101. // in the sass stack.
  102. // TODO: Switch icon-size to "var(--mdc-icon-button-icon-size)". Currently
  103. // fails validation because the variable is "undefined" in the sass stack.
  104. width: var(--mdc-icon-button-state-layer-size);
  105. height: var(--mdc-icon-button-state-layer-size);
  106. padding: math.div($calculated-size - $icon-size, 2);
  107. }
  108. }
  109. }
  110. /// Defines the tokens that will be available in the `overrides` mixin and for docs extraction.
  111. @function _define-overrides() {
  112. @return (
  113. (
  114. namespace: tokens-mdc-icon-button.$prefix,
  115. tokens: tokens-mdc-icon-button.get-token-slots(),
  116. ),
  117. (
  118. namespace: tokens-mat-icon-button.$prefix,
  119. tokens: tokens-mat-icon-button.get-token-slots(),
  120. ),
  121. );
  122. }
  123. @mixin overrides($tokens: ()) {
  124. @include token-utils.batch-create-token-values($tokens, _define-overrides()...);
  125. }
  126. /// Outputs all (base, color, typography, and density) theme styles for the mat-icon-button.
  127. /// @param {Map} $theme The theme to generate styles for.
  128. /// @param {ArgList} Additional optional arguments (only supported for M3 themes):
  129. /// $color-variant: The color variant to use for the button: primary, secondary, tertiary, or error.
  130. @mixin theme($theme, $options...) {
  131. @include theming.private-check-duplicate-theme-styles($theme, 'mat-icon-button') {
  132. @if inspection.get-theme-version($theme) == 1 {
  133. @include _theme-from-tokens(inspection.get-theme-tokens($theme), $options...);
  134. } @else {
  135. @include base($theme);
  136. @if inspection.theme-has($theme, color) {
  137. @include color($theme);
  138. }
  139. @if inspection.theme-has($theme, density) {
  140. @include density($theme);
  141. }
  142. @if inspection.theme-has($theme, typography) {
  143. @include typography($theme);
  144. }
  145. }
  146. }
  147. }
  148. @mixin _theme-from-tokens($tokens, $options...) {
  149. @include validation.selector-defined(
  150. 'Calls to Angular Material theme mixins with an M3 theme must be wrapped in a selector'
  151. );
  152. @if ($tokens != ()) {
  153. $mdc-tokens: token-utils.get-tokens-for($tokens, tokens-mdc-icon-button.$prefix, $options...);
  154. $mat-tokens: token-utils.get-tokens-for($tokens, tokens-mat-icon-button.$prefix, $options...);
  155. @include token-utils.create-token-values(tokens-mdc-icon-button.$prefix, $mdc-tokens);
  156. @include token-utils.create-token-values(tokens-mat-icon-button.$prefix, $mat-tokens);
  157. }
  158. }