From 01a95013c6ae0b7bce31f74ec8e1d4afe3749cda Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Thu, 26 Oct 2023 10:07:17 +0200 Subject: [PATCH] refactor(material/button): handle disabled states through tokens MDC applies disabled styles through the `:disabled` selector which forced us to work around it by using a selector like `.mat-mdc-button[disabled][disabled]` and setting both the disabled and enabled button tokens. This is problematic, because it increases the specificity too much and it introduces space for mistakes, because we have to duplicate the tokens values. These changes resolve the issue by re-applying the token slots to disabled buttons with the correct selector so that they can reuse the same tokens as the `button` nodes. --- src/material/button/_button-base.scss | 13 --- .../button/_button-theme-private.scss | 12 --- src/material/button/_button-theme.scss | 84 +++---------------- src/material/button/button.scss | 56 +++++++++++-- .../core/tokens/m2/mdc/_outlined-button.scss | 3 +- 5 files changed, 61 insertions(+), 107 deletions(-) diff --git a/src/material/button/_button-base.scss b/src/material/button/_button-base.scss index 285370cf0752..9755ae930317 100644 --- a/src/material/button/_button-base.scss +++ b/src/material/button/_button-base.scss @@ -1,4 +1,3 @@ -@use 'sass:map'; @use '@material/touch-target' as mdc-touch-target; @use '../core/style/layout-common'; @@ -89,15 +88,3 @@ $query: mdc-helpers.$mdc-base-styles-query); } } - -// Changes a button token set to exclude the ripple styles. -@function mat-private-button-remove-ripple($tokens) { - @return map.merge($tokens, ( - focus-state-layer-color: null, - focus-state-layer-opacity: null, - hover-state-layer-color: null, - hover-state-layer-opacity: null, - pressed-state-layer-color: null, - pressed-state-layer-opacity: null, - )); -} diff --git a/src/material/button/_button-theme-private.scss b/src/material/button/_button-theme-private.scss index 4889e5b5c41d..2926f533c46b 100644 --- a/src/material/button/_button-theme-private.scss +++ b/src/material/button/_button-theme-private.scss @@ -45,18 +45,6 @@ } } -// Wraps the content style in a selector for the disabled state. -// MDC adds theme color by using :not(:disabled), so just using [disabled] once will not -// override this, neither will it apply to anchor tags. This needs to override the -// previously set theme color, so it must be ordered after the theme styles. -// TODO(andrewseguin): Discuss with the MDC team to see if we can avoid the :not(:disabled) by -// manually styling disabled buttons with a [disabled] selector. -@mixin apply-disabled-style() { - &[disabled][disabled] { - @content; - } -} - // Hides the touch target on lower densities. @mixin touch-target-density($scale) { @include mdc-helpers.if-touch-targets-unsupported($scale) { diff --git a/src/material/button/_button-theme.scss b/src/material/button/_button-theme.scss index b5b28b2c0635..1ccf96e803bf 100644 --- a/src/material/button/_button-theme.scss +++ b/src/material/button/_button-theme.scss @@ -3,7 +3,6 @@ @use '@material/button/button-filled-theme' as mdc-button-filled-theme; @use '@material/button/button-protected-theme' as mdc-button-protected-theme; @use '@material/button/button-outlined-theme' as mdc-button-outlined-theme; -@use '@material/theme/theme-color' as mdc-theme-color; @use '@material/elevation/elevation-theme' as mdc-elevation-theme; @use './button-theme-private'; @@ -21,25 +20,23 @@ @return if(mdc-helpers.variable-safe-contrast-tone($palette, $is-dark) == 'dark', #000, #fff); } -@mixin _outlined-button-variant($color) { - @include mdc-button-outlined-theme.theme(( - label-text-color: $color, - )); -} - @mixin base($theme) { // TODO(mmalerba): Move button base tokens here } @mixin color($theme) { - @include mdc-helpers.using-mdc-theme($theme) { - $is-dark: inspection.get-theme-type($theme) == dark; - $on-surface: mdc-theme-color.prop-value(on-surface); - $disabled-ink-color: rgba($on-surface, if($is-dark, 0.5, 0.38)); - $primary: mdc-theme-color.prop-value(primary); - $secondary: mdc-theme-color.prop-value(secondary); - $error: mdc-theme-color.prop-value(error); + $surface: inspection.get-theme-color($theme, background, card); + $primary: inspection.get-theme-color($theme, primary); + $accent: inspection.get-theme-color($theme, accent); + $error: inspection.get-theme-color($theme, warn); + $on-surface: _on-color($theme, $surface); + $on-primary: _on-color($theme, $primary); + $on-accent: _on-color($theme, $accent); + $on-error: _on-color($theme, $error); + + // TODO: remove these when tokenizing the ripples. + @include mdc-helpers.using-mdc-theme($theme) { // Ripple colors .mat-mdc-button, .mat-mdc-outlined-button { @include button-theme-private.ripple-theme-styles($theme, false); @@ -50,16 +47,6 @@ } } - $surface: inspection.get-theme-color($theme, background, card); - $primary: inspection.get-theme-color($theme, primary); - $accent: inspection.get-theme-color($theme, accent); - $error: inspection.get-theme-color($theme, warn); - - $on-surface: _on-color($theme, $surface); - $on-primary: _on-color($theme, $primary); - $on-accent: _on-color($theme, $accent); - $on-error: _on-color($theme, $error); - .mat-mdc-button { @include mdc-button-text-theme.theme(tokens-mdc-text-button.get-color-tokens($theme)); @@ -176,55 +163,6 @@ @include mdc-button-outlined-theme.theme($warn-color-tokens); } } - - $is-dark: inspection.get-theme-type($theme) == dark; - $disabled-ink-color: rgba($on-surface, if($is-dark, 0.5, 0.38)); - $disabled-container-color: rgba($on-surface, 0.12); - - // TODO: these disabled styles are a bit too specific currently. - // Once the buttons are fully tokenized, we should rework how they're applied. - .mat-mdc-button { - @include button-theme-private.apply-disabled-style() { - @include mdc-button-text-theme.theme(( - disabled-label-text-color: $disabled-ink-color, - label-text-color: $disabled-ink-color, - )); - } - } - - .mat-mdc-raised-button { - @include button-theme-private.apply-disabled-style() { - @include mdc-elevation-theme.elevation(0); - @include mdc-button-protected-theme.theme(( - disabled-container-color: $disabled-container-color, - disabled-label-text-color: $disabled-ink-color, - container-color: $disabled-container-color, - label-text-color: $disabled-ink-color, - )); - } - } - - .mat-mdc-outlined-button { - @include button-theme-private.apply-disabled-style() { - @include mdc-button-outlined-theme.theme(( - label-text-color: $disabled-ink-color, - disabled-label-text-color: $disabled-ink-color, - disabled-outline-color: rgba($on-surface, 0.12), - outline-color: rgba($on-surface, 0.12), - )); - } - } - - .mat-mdc-unelevated-button { - @include button-theme-private.apply-disabled-style() { - @include mdc-button-filled-theme.theme(( - disabled-container-color: $disabled-container-color, - disabled-label-text-color: $disabled-ink-color, - container-color: $disabled-container-color, - label-text-color: $disabled-ink-color, - )); - } - } } @mixin typography($theme) { diff --git a/src/material/button/button.scss b/src/material/button/button.scss index 2c69c33eb51b..21c9c6ab9c0e 100644 --- a/src/material/button/button.scss +++ b/src/material/button/button.scss @@ -10,6 +10,7 @@ @use './button-base'; @use '../core/mdc-helpers/mdc-helpers'; @use '../core/style/private' as style-private; +@use '../core/tokens/token-utils'; @use '../core/focus-indicators/private' as focus-indicators-private; @use '../core/tokens/m2/mdc/filled-button' as tokens-mdc-filled-button; @use '../core/tokens/m2/mdc/outlined-button' as tokens-mdc-outlined-button; @@ -22,12 +23,20 @@ @include mdc-custom-properties.configure($emit-fallback-values: false, $emit-fallback-vars: false) { .mat-mdc-button { - @include mdc-button-text-theme.theme-styles(tokens-mdc-text-button.get-token-slots()); + $mdc-text-button-slots: tokens-mdc-text-button.get-token-slots(); + + @include mdc-button-text-theme.theme-styles($mdc-text-button-slots); @include mdc-button-text-theme.theme(tokens-mdc-text-button.get-unthemable-tokens()); + + @include token-utils.use-tokens(tokens-mdc-text-button.$prefix, $mdc-text-button-slots) { + // We need to re-apply the disabled tokens since MDC uses + // `:disabled` which doesn't apply to anchors. + @include button-base.mat-private-button-disabled { + @include token-utils.create-token-slot(color, disabled-label-text-color); + } + } } - // Note that we don't include a feature query, because this mixins declare - // all the "slots" for CSS variables that will be defined in the theme. .mat-mdc-unelevated-button { $mdc-filled-button-slots: tokens-mdc-filled-button.get-token-slots(); @@ -36,10 +45,17 @@ // Add default values for MDC text button tokens that aren't outputted by the theming API. @include mdc-button-filled-theme.theme(tokens-mdc-filled-button.get-unthemable-tokens()); + + @include token-utils.use-tokens(tokens-mdc-filled-button.$prefix, $mdc-filled-button-slots) { + // We need to re-apply the disabled tokens since MDC uses + // `:disabled` which doesn't apply to anchors. + @include button-base.mat-private-button-disabled { + @include token-utils.create-token-slot(color, disabled-label-text-color); + @include token-utils.create-token-slot(background-color, disabled-container-color); + } + } } - // Note that we don't include a feature query, because this mixins declare - // all the "slots" for CSS variables that will be defined in the theme. .mat-mdc-raised-button { $mdc-button-protected-slots: tokens-mdc-protected-button.get-token-slots(); @@ -48,10 +64,24 @@ // Add default values for MDC text button tokens that aren't outputted by the theming API. @include mdc-button-protected-theme.theme(tokens-mdc-protected-button.get-unthemable-tokens()); + + @include token-utils.use-tokens( + tokens-mdc-protected-button.$prefix, + $mdc-button-protected-slots) { + // We need to re-apply the disabled tokens since MDC uses + // `:disabled` which doesn't apply to anchors. + @include button-base.mat-private-button-disabled { + @include token-utils.create-token-slot(color, disabled-label-text-color); + @include token-utils.create-token-slot(background-color, disabled-container-color); + + // Since we're still doing elevation through the theme, we need additional specificity here. + &[disabled] { + box-shadow: none; + } + } + } } - // Note that we don't include a feature query, because this mixins declare - // all the "slots" for CSS variables that will be defined in the theme. .mat-mdc-outlined-button { $mdc-outlined-button-slots: tokens-mdc-outlined-button.get-token-slots(); @@ -60,6 +90,17 @@ // Add default values for MDC text button tokens that aren't outputted by the theming API. @include mdc-button-outlined-theme.theme(tokens-mdc-outlined-button.get-unthemable-tokens()); + + @include token-utils.use-tokens( + tokens-mdc-outlined-button.$prefix, + $mdc-outlined-button-slots) { + // We need to re-apply the disabled tokens since MDC uses + // `:disabled` which doesn't apply to anchors. + @include button-base.mat-private-button-disabled { + @include token-utils.create-token-slot(color, disabled-label-text-color); + @include token-utils.create-token-slot(border-color, disabled-outline-color); + } + } } } @@ -68,7 +109,6 @@ .mat-mdc-raised-button, .mat-mdc-outlined-button { @include button-base.mat-private-button-interactive(); - @include button-base.mat-private-button-disabled(); @include button-base.mat-private-button-touch-target(false); @include style-private.private-animation-noop(); } diff --git a/src/material/core/tokens/m2/mdc/_outlined-button.scss b/src/material/core/tokens/m2/mdc/_outlined-button.scss index 621f61b08239..746fa27a5547 100644 --- a/src/material/core/tokens/m2/mdc/_outlined-button.scss +++ b/src/material/core/tokens/m2/mdc/_outlined-button.scss @@ -64,12 +64,13 @@ $prefix: (mdc, outlined-button); // Tokens that can be configured through Angular Material's color theming API. @function get-color-tokens($theme, $color: null, $on-color: null) { + $is-dark: inspection.get-theme-type($theme) == dark; $surface: inspection.get-theme-color($theme, background, card); $on-surface: _on-color($theme, $surface); @return ( disabled-outline-color: rgba($on-surface, 0.12), - disabled-label-text-color: rgba($on-surface, 0.38), + disabled-label-text-color: rgba($on-surface, if($is-dark, 0.5, 0.38)), label-text-color: if($color, $color, inherit), outline-color: rgba($on-surface, 0.12) );