Skip to content

Commit

Permalink
feat(button): Support state-layer-color theming for buttons and icon-…
Browse files Browse the repository at this point in the history
…buttons

PiperOrigin-RevId: 572985469
  • Loading branch information
material-web-copybara authored and copybara-github committed Oct 12, 2023
1 parent c0d21ec commit 127a44b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 26 deletions.
27 changes: 4 additions & 23 deletions packages/mdc-button/_button-shared-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ $disabled-container-color: rgba(
$icon-size: map.get($theme, with-icon-icon-size);
@include _icon-size($icon-size, $query: $query);

@include _states-colors(
(
focus: map.get($theme, focus-state-layer-color),
@include ripple-theme.states-colors(
$color-map: (
hover: map.get($theme, hover-state-layer-color),
pressed: map.get($theme, pressed-state-layer-color),
press: map.get($theme, pressed-state-layer-color),
),
$ripple-target: button-ripple.$ripple-target,
$query: $query
);

Expand Down Expand Up @@ -647,25 +647,6 @@ $disabled-container-color: rgba(
}
}

@mixin _states-colors($color-map, $query: feature-targeting.all()) {
$hover: map.get($color-map, hover);

$hover-value: if(
custom-properties.is-custom-prop($hover),
custom-properties.get-fallback($hover),
$hover
);
// TODO(b/191298796): support focused & pressed key colors.

@if $hover-value != null {
@include ripple-theme.states-base-color(
$color: $hover,
$ripple-target: button-ripple.$ripple-target,
$query: $query
);
}
}

@mixin _label-text-typography(
$typography-map,
$query: feature-targeting.all()
Expand Down
8 changes: 8 additions & 0 deletions packages/mdc-icon-button/_icon-button-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ $light-theme: (
$ripple-target: $ripple-target
);

@include ripple-theme.states-colors(
(
hover: map.get($theme, hover-state-layer-color),
press: map.get($theme, pressed-state-layer-color),
),
$ripple-target: $ripple-target
);

@include _focus-ring-color(map.get($theme, 'focus-ring-color'));
@include _focus-ring-offset(map.get($theme, 'focus-ring-offset'));
}
Expand Down
49 changes: 46 additions & 3 deletions packages/mdc-ripple/_ripple-theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ $_ripple-theme: (

// TODO(b/191298796): Support states layer color for every interactive states.
// Use only hover state layer color, ignoring focus and pressed color.
// Consider replacing states-base-color with states-colors.
@include internal-theme-styles($theme, $ripple-target);
}

Expand All @@ -137,6 +138,7 @@ $_ripple-theme: (
);
}

/// @deprecated Use states-colors instead.
@mixin states-base-color(
$color,
$query: feature-targeting.all(),
Expand All @@ -161,6 +163,35 @@ $_ripple-theme: (
}
}

///
/// Customizes ripple color in `hover` and `press` states
/// @param {map} $color-map - map specifying custom opacity of zero or more states
/// @param {string} $ripple-target - the optional selector for the ripple element
///
@mixin states-colors(
$color-map: (),
$ripple-target: '&',
$query: feature-targeting.all()
) {
$feat-color: feature-targeting.create-target($query, color);

@if map.get($color-map, hover) {
@include hover-state-layer-selector($ripple-target) {
@include feature-targeting.targets($feat-color) {
@include theme.property(background-color, map.get($color-map, hover));
}
}
}

@if map.get($color-map, press) {
@include press-state-layer-selector($ripple-target) {
@include feature-targeting.targets($feat-color) {
@include theme.property(background-color, map.get($color-map, press));
}
}
}
}

///
/// Customizes ripple opacities in `hover`, `focus`, or `press` states
/// @param {map} $opacity-map - map specifying custom opacity of zero or more states
Expand Down Expand Up @@ -299,15 +330,14 @@ $_ripple-theme: (
}

&:not(.mdc-ripple-upgraded) {
// Apply press additively by using the ::after pseudo-element
#{$ripple-target}::after {
@include press-state-layer-selector($ripple-target) {
@include feature-targeting.targets($feat-animation) {
transition: opacity $fade-out-duration linear;
}
}

&:active {
#{$ripple-target}::after {
@include press-state-layer-selector($ripple-target) {
@include feature-targeting.targets($feat-animation) {
transition-duration: $fade-in-duration;
}
Expand Down Expand Up @@ -543,8 +573,21 @@ $_ripple-theme: (
@return map.get($opacity-map, $state);
}

/// @deprecated Use hover-state-layer-selector instead.
@mixin states-background-selector($ripple-target) {
@include hover-state-layer-selector($ripple-target) {
@content;
}
}

@mixin hover-state-layer-selector($ripple-target) {
#{$ripple-target}::before {
@content;
}
}

@mixin press-state-layer-selector($ripple-target) {
#{$ripple-target}::after {
@content;
}
}

0 comments on commit 127a44b

Please sign in to comment.