Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(material/button): handle disabled states through tokens #28007

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/material/button/_button-base.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@use 'sass:map';
@use '@material/touch-target' as mdc-touch-target;

@use '../core/style/layout-common';
Expand Down Expand Up @@ -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,
));
}
12 changes: 0 additions & 12 deletions src/material/button/_button-theme-private.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
120 changes: 34 additions & 86 deletions src/material/button/_button-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,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';
Expand All @@ -13,6 +12,7 @@
@use '../core/theming/inspection';
@use '../core/typography/typography';
@use '../core/tokens/m2/mdc/filled-button' as tokens-mdc-filled-button;
@use '../core/tokens/m2/mdc/outlined-button' as tokens-mdc-outlined-button;
@use '../core/tokens/m2/mdc/protected-button' as tokens-mdc-protected-button;
@use '../core/tokens/m2/mdc/text-button' as tokens-mdc-text-button;

Expand All @@ -21,58 +21,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);

.mat-mdc-outlined-button {
@include mdc-button-outlined-theme.theme((
outline-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12)
));

&.mat-unthemed {
@include _outlined-button-variant($on-surface);
}

&.mat-primary {
@include _outlined-button-variant($primary);
}

&.mat-accent {
@include _outlined-button-variant($secondary);
}

&.mat-warn {
@include _outlined-button-variant($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);

@include button-theme-private.apply-disabled-style() {
@include mdc-button-outlined-theme.theme((
// We need to pass both the disabled and enabled values, because the enabled
// ones apply to anchors while the disabled ones are for buttons.
label-text-color: $disabled-ink-color,
disabled-label-text-color: $disabled-ink-color,
outline-color: rgba($on-surface, 0.12),
disabled-outline-color: rgba($on-surface, 0.12),
));
}
}
$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);
Expand All @@ -83,16 +48,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));

Expand Down Expand Up @@ -179,41 +134,34 @@
}
}

$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);
.mat-mdc-outlined-button {
$default-color-tokens: tokens-mdc-outlined-button.get-color-tokens(
$theme,
$on-surface,
$on-surface
);
$primary-color-tokens: tokens-mdc-outlined-button.get-color-tokens(
$theme,
$primary,
$on-primary
);
$accent-color-tokens: tokens-mdc-outlined-button.get-color-tokens($theme, $accent, $on-accent);
$warn-color-tokens: tokens-mdc-outlined-button.get-color-tokens($theme, $error, $on-error);

// 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-unthemed {
@include mdc-button-outlined-theme.theme($default-color-tokens);
}
}

.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-primary {
@include mdc-button-outlined-theme.theme($primary-color-tokens);
}
}

.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,
));
&.mat-accent {
@include mdc-button-outlined-theme.theme($accent-color-tokens);
}

&.mat-warn {
@include mdc-button-outlined-theme.theme($warn-color-tokens);
}
}
}
Expand Down
82 changes: 59 additions & 23 deletions src/material/button/button.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
@use 'sass:map';
@use '@material/button/button' as mdc-button;
@use '@material/button/button-base' as mdc-button-base;
@use '@material/button/variables' as mdc-button-variables;
Expand All @@ -11,39 +10,33 @@
@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;
@use '../core/tokens/m2/mdc/protected-button' as tokens-mdc-protected-button;
@use '../core/tokens/m2/mdc/text-button' as tokens-mdc-text-button;

@include mdc-helpers.disable-mdc-fallback-declarations {
@include mdc-button.static-styles-without-ripple($query: mdc-helpers.$mdc-base-styles-query);

.mat-mdc-outlined-button {
// Keys to exclude from the MDC theme config, allowing us to drop styles we don't need.
$override-keys: button-base.mat-private-button-remove-ripple((
label-text-font: null,
label-text-size: null,
label-text-tracking: null,
label-text-transform: null,
label-text-weight: null,
with-icon-icon-size: null,
label-text-color: inherit,
));

@include mdc-button-outlined-theme.theme-styles(
map.merge(mdc-button-outlined-theme.$light-theme, $override-keys));
}
}

@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();

Expand All @@ -52,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();

Expand All @@ -64,6 +64,43 @@

// 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;
}
}
}
}

.mat-mdc-outlined-button {
$mdc-outlined-button-slots: tokens-mdc-outlined-button.get-token-slots();

// Add the slots for MDC text button.
@include mdc-button-outlined-theme.theme-styles($mdc-outlined-button-slots);

// 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);
}
}
}
}

Expand All @@ -72,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();
}
Expand Down
Loading