Skip to content

Commit 4e8da15

Browse files
committed
refactor(material/button): switch outlined button to tokens API
1 parent cf124b3 commit 4e8da15

File tree

4 files changed

+159
-49
lines changed

4 files changed

+159
-49
lines changed

src/material/button/_button-theme.scss

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
@use '../core/theming/inspection';
1414
@use '../core/typography/typography';
1515
@use '../core/tokens/m2/mdc/filled-button' as tokens-mdc-filled-button;
16+
@use '../core/tokens/m2/mdc/outlined-button' as tokens-mdc-outlined-button;
1617
@use '../core/tokens/m2/mdc/protected-button' as tokens-mdc-protected-button;
1718
@use '../core/tokens/m2/mdc/text-button' as tokens-mdc-text-button;
1819

@@ -40,39 +41,6 @@
4041
$secondary: mdc-theme-color.prop-value(secondary);
4142
$error: mdc-theme-color.prop-value(error);
4243

43-
.mat-mdc-outlined-button {
44-
@include mdc-button-outlined-theme.theme((
45-
outline-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12)
46-
));
47-
48-
&.mat-unthemed {
49-
@include _outlined-button-variant($on-surface);
50-
}
51-
52-
&.mat-primary {
53-
@include _outlined-button-variant($primary);
54-
}
55-
56-
&.mat-accent {
57-
@include _outlined-button-variant($secondary);
58-
}
59-
60-
&.mat-warn {
61-
@include _outlined-button-variant($error);
62-
}
63-
64-
@include button-theme-private.apply-disabled-style() {
65-
@include mdc-button-outlined-theme.theme((
66-
// We need to pass both the disabled and enabled values, because the enabled
67-
// ones apply to anchors while the disabled ones are for buttons.
68-
label-text-color: $disabled-ink-color,
69-
disabled-label-text-color: $disabled-ink-color,
70-
outline-color: rgba($on-surface, 0.12),
71-
disabled-outline-color: rgba($on-surface, 0.12),
72-
));
73-
}
74-
}
75-
7644
// Ripple colors
7745
.mat-mdc-button, .mat-mdc-outlined-button {
7846
@include button-theme-private.ripple-theme-styles($theme, false);
@@ -179,6 +147,37 @@
179147
}
180148
}
181149

150+
.mat-mdc-outlined-button {
151+
$default-color-tokens: tokens-mdc-outlined-button.get-color-tokens(
152+
$theme,
153+
$on-surface,
154+
$on-surface
155+
);
156+
$primary-color-tokens: tokens-mdc-outlined-button.get-color-tokens(
157+
$theme,
158+
$primary,
159+
$on-primary
160+
);
161+
$accent-color-tokens: tokens-mdc-outlined-button.get-color-tokens($theme, $accent, $on-accent);
162+
$warn-color-tokens: tokens-mdc-outlined-button.get-color-tokens($theme, $error, $on-error);
163+
164+
&.mat-unthemed {
165+
@include mdc-button-outlined-theme.theme($default-color-tokens);
166+
}
167+
168+
&.mat-primary {
169+
@include mdc-button-outlined-theme.theme($primary-color-tokens);
170+
}
171+
172+
&.mat-accent {
173+
@include mdc-button-outlined-theme.theme($accent-color-tokens);
174+
}
175+
176+
&.mat-warn {
177+
@include mdc-button-outlined-theme.theme($warn-color-tokens);
178+
}
179+
}
180+
182181
$is-dark: inspection.get-theme-type($theme) == dark;
183182
$disabled-ink-color: rgba($on-surface, if($is-dark, 0.5, 0.38));
184183
$disabled-container-color: rgba($on-surface, 0.12);
@@ -206,6 +205,17 @@
206205
}
207206
}
208207

208+
.mat-mdc-outlined-button {
209+
@include button-theme-private.apply-disabled-style() {
210+
@include mdc-button-outlined-theme.theme((
211+
label-text-color: $disabled-ink-color,
212+
disabled-label-text-color: $disabled-ink-color,
213+
disabled-outline-color: rgba($on-surface, 0.12),
214+
outline-color: rgba($on-surface, 0.12),
215+
));
216+
}
217+
}
218+
209219
.mat-mdc-unelevated-button {
210220
@include button-theme-private.apply-disabled-style() {
211221
@include mdc-button-filled-theme.theme((

src/material/button/button.scss

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,12 @@
1313
@use '../core/style/private' as style-private;
1414
@use '../core/focus-indicators/private' as focus-indicators-private;
1515
@use '../core/tokens/m2/mdc/filled-button' as tokens-mdc-filled-button;
16+
@use '../core/tokens/m2/mdc/outlined-button' as tokens-mdc-outlined-button;
1617
@use '../core/tokens/m2/mdc/protected-button' as tokens-mdc-protected-button;
1718
@use '../core/tokens/m2/mdc/text-button' as tokens-mdc-text-button;
1819

1920
@include mdc-helpers.disable-mdc-fallback-declarations {
2021
@include mdc-button.static-styles-without-ripple($query: mdc-helpers.$mdc-base-styles-query);
21-
22-
.mat-mdc-outlined-button {
23-
// Keys to exclude from the MDC theme config, allowing us to drop styles we don't need.
24-
$override-keys: button-base.mat-private-button-remove-ripple((
25-
label-text-font: null,
26-
label-text-size: null,
27-
label-text-tracking: null,
28-
label-text-transform: null,
29-
label-text-weight: null,
30-
with-icon-icon-size: null,
31-
label-text-color: inherit,
32-
));
33-
34-
@include mdc-button-outlined-theme.theme-styles(
35-
map.merge(mdc-button-outlined-theme.$light-theme, $override-keys));
36-
}
3722
}
3823

3924
@include mdc-custom-properties.configure($emit-fallback-values: false, $emit-fallback-vars: false) {
@@ -65,6 +50,18 @@
6550
// Add default values for MDC text button tokens that aren't outputted by the theming API.
6651
@include mdc-button-protected-theme.theme(tokens-mdc-protected-button.get-unthemable-tokens());
6752
}
53+
54+
// Note that we don't include a feature query, because this mixins declare
55+
// all the "slots" for CSS variables that will be defined in the theme.
56+
.mat-mdc-outlined-button {
57+
$mdc-outlined-button-slots: tokens-mdc-outlined-button.get-token-slots();
58+
59+
// Add the slots for MDC text button.
60+
@include mdc-button-outlined-theme.theme-styles($mdc-outlined-button-slots);
61+
62+
// Add default values for MDC text button tokens that aren't outputted by the theming API.
63+
@include mdc-button-outlined-theme.theme(tokens-mdc-outlined-button.get-unthemable-tokens());
64+
}
6865
}
6966

7067
.mat-mdc-button,
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
@use '../../token-utils';
2+
@use '../../../mdc-helpers/mdc-helpers';
3+
@use '../../../style/sass-utils';
4+
@use '../../../theming/inspection';
5+
6+
// The prefix used to generate the fully qualified name for tokens in this file.
7+
$prefix: (mdc, outlined-button);
8+
9+
// Tokens that can't be configured through Angular Material's current theming API,
10+
// but may be in a future version of the theming API.
11+
//
12+
// Tokens that are available in MDC, but not used in Angular Material should be mapped to `null`.
13+
// `null` indicates that we are intentionally choosing not to emit a slot or value for the token in
14+
// our CSS.
15+
@function get-unthemable-tokens() {
16+
@return (
17+
keep-touch-target: false,
18+
19+
outline-width: 1px,
20+
container-shape: 4px,
21+
22+
hover-state-layer-opacity: 0.04,
23+
focus-state-layer-opacity: 0.12,
24+
pressed-state-layer-opacity: 0.12,
25+
26+
hover-label-text-color: null,
27+
focus-label-text-color: null,
28+
pressed-label-text-color: null,
29+
30+
hover-outline-color: null,
31+
focus-outline-color: null,
32+
pressed-outline-color: null,
33+
34+
focus-ring-color: null,
35+
focus-ring-offset: null,
36+
37+
with-icon-icon-size: null,
38+
with-icon-icon-color: null,
39+
with-icon-hover-icon-color: null,
40+
with-icon-focus-icon-color: null,
41+
with-icon-pressed-icon-color: null,
42+
with-icon-disabled-icon-color: null,
43+
44+
label-text-size: null,
45+
label-text-font: null,
46+
label-text-weight: null,
47+
label-text-tracking: null,
48+
label-text-transform: null,
49+
50+
container-height: 36px
51+
);
52+
}
53+
54+
@function _on-color($theme, $palette) {
55+
@if ($palette) {
56+
$is-dark: inspection.get-theme-type($theme) == dark;
57+
@return if(mdc-helpers.variable-safe-contrast-tone($palette, $is-dark) == 'dark', #000, #fff);
58+
}
59+
}
60+
61+
// Tokens that can be configured through Angular Material's color theming API.
62+
@function get-color-tokens($theme, $color: null, $on-color: null) {
63+
$surface: inspection.get-theme-color($theme, background, card);
64+
$on-surface: _on-color($theme, $surface);
65+
66+
@return (
67+
focus-state-layer-color: $color,
68+
hover-state-layer-color: $color,
69+
pressed-state-layer-color: $color,
70+
disabled-outline-color: rgba($on-surface, 0.12),
71+
disabled-label-text-color: rgba($on-surface, 0.38),
72+
label-text-color: if($color, $color, inherit),
73+
outline-color: rgba($on-surface, 0.12)
74+
);
75+
}
76+
77+
// Tokens that can be configured through Angular Material's typography theming API.
78+
@function get-typography-tokens($theme) {
79+
@return ();
80+
}
81+
82+
// Tokens that can be configured through Angular Material's density theming API.
83+
@function get-density-tokens($theme) {
84+
@return ();
85+
}
86+
87+
// Combines the tokens generated by the above functions into a single map with placeholder values.
88+
// This is used to create token slots.
89+
@function get-token-slots() {
90+
@return sass-utils.deep-merge-all(
91+
get-unthemable-tokens(),
92+
get-color-tokens(token-utils.$placeholder-color-config),
93+
get-typography-tokens(token-utils.$placeholder-typography-config),
94+
get-density-tokens(token-utils.$placeholder-density-config)
95+
);
96+
}

src/material/core/tokens/tests/test-validate-tokens.scss

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
@use 'sass:list';
22
@use 'sass:map';
33

4+
@use '@material/button/button-outlined-theme' as mdc-button-outlined-theme;
45
@use '@material/button/button-protected-theme' as mdc-button-protected-theme;
56
@use '@material/button/button-filled-theme' as mdc-button-filled-theme;
67
@use '@material/button/button-text-theme' as mdc-button-text-theme;
@@ -29,6 +30,7 @@
2930
@use '../m2/mdc/protected-button' as tokens-mdc-protected-button;
3031
@use '../m2/mdc/filled-button' as tokens-mdc-filled-button;
3132
@use '../m2/mdc/text-button' as tokens-mdc-text-button;
33+
@use '../m2/mdc/outlined-button' as tokens-mdc-outlined-button;
3234
@use '../m2/mdc/circular-progress' as tokens-mdc-circular-progress;
3335
@use '../m2/mdc/linear-progress' as tokens-mdc-linear-progress;
3436
@use '../m2/mdc/elevated-card' as tokens-mdc-elevated-card;
@@ -177,3 +179,8 @@
177179
$slots: tokens-mdc-text-button.get-token-slots(),
178180
$reference: mdc-button-text-theme.$light-theme
179181
);
182+
@include validate-slots(
183+
$component: 'm2.mdc.outlined-button',
184+
$slots: tokens-mdc-outlined-button.get-token-slots(),
185+
$reference: mdc-button-outlined-theme.$light-theme
186+
);

0 commit comments

Comments
 (0)