Skip to content

Commit 7607bf1

Browse files
committed
refactor(material/button): switch outlined button to tokens API
1 parent 3e31986 commit 7607bf1

File tree

4 files changed

+159
-38
lines changed

4 files changed

+159
-38
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/button-filled' as tokens-mdc-button-filled;
16+
@use '../core/tokens/m2/mdc/button-outlined' as tokens-mdc-button-outlined;
1617
@use '../core/tokens/m2/mdc/button-protected' as tokens-mdc-button-protected;
1718

1819
@function _on-color($theme, $palette) {
@@ -73,39 +74,6 @@
7374
}
7475
}
7576

76-
.mat-mdc-outlined-button {
77-
@include mdc-button-outlined-theme.theme((
78-
outline-color: rgba(mdc-theme-color.prop-value(on-surface), 0.12)
79-
));
80-
81-
&.mat-unthemed {
82-
@include _outlined-button-variant($on-surface);
83-
}
84-
85-
&.mat-primary {
86-
@include _outlined-button-variant($primary);
87-
}
88-
89-
&.mat-accent {
90-
@include _outlined-button-variant($secondary);
91-
}
92-
93-
&.mat-warn {
94-
@include _outlined-button-variant($error);
95-
}
96-
97-
@include button-theme-private.apply-disabled-style() {
98-
@include mdc-button-outlined-theme.theme((
99-
// We need to pass both the disabled and enabled values, because the enabled
100-
// ones apply to anchors while the disabled ones are for buttons.
101-
label-text-color: $disabled-ink-color,
102-
disabled-label-text-color: $disabled-ink-color,
103-
outline-color: rgba($on-surface, 0.12),
104-
disabled-outline-color: rgba($on-surface, 0.12),
105-
));
106-
}
107-
}
108-
10977
// Ripple colors
11078
.mat-mdc-button, .mat-mdc-outlined-button {
11179
@include button-theme-private.ripple-theme-styles($theme, false);
@@ -189,6 +157,37 @@
189157
}
190158
}
191159

160+
.mat-mdc-outlined-button {
161+
$default-color-tokens: tokens-mdc-button-outlined.get-color-tokens(
162+
$theme,
163+
$on-surface,
164+
$on-surface
165+
);
166+
$primary-color-tokens: tokens-mdc-button-outlined.get-color-tokens(
167+
$theme,
168+
$primary,
169+
$on-primary
170+
);
171+
$accent-color-tokens: tokens-mdc-button-outlined.get-color-tokens($theme, $accent, $on-accent);
172+
$warn-color-tokens: tokens-mdc-button-outlined.get-color-tokens($theme, $error, $on-error);
173+
174+
&.mat-unthemed {
175+
@include mdc-button-outlined-theme.theme($default-color-tokens);
176+
}
177+
178+
&.mat-primary {
179+
@include mdc-button-outlined-theme.theme($primary-color-tokens);
180+
}
181+
182+
&.mat-accent {
183+
@include mdc-button-outlined-theme.theme($accent-color-tokens);
184+
}
185+
186+
&.mat-warn {
187+
@include mdc-button-outlined-theme.theme($warn-color-tokens);
188+
}
189+
}
190+
192191
$is-dark: inspection.get-theme-type($theme) == dark;
193192
$disabled-ink-color: rgba($on-surface, if($is-dark, 0.5, 0.38));
194193
$disabled-container-color: rgba($on-surface, 0.12);
@@ -205,6 +204,17 @@
205204
}
206205
}
207206

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

src/material/button/button.scss

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@use '../core/focus-indicators/private' as focus-indicators-private;
1515
@use '../core/tokens/m2/mdc/button-filled' as tokens-mdc-button-filled;
1616
@use '../core/tokens/m2/mdc/button-protected' as tokens-mdc-button-protected;
17+
@use '../core/tokens/m2/mdc/button-outlined' as tokens-mdc-button-outlined;
1718

1819
@include mdc-helpers.disable-mdc-fallback-declarations {
1920
@include mdc-button.static-styles-without-ripple($query: mdc-helpers.$mdc-base-styles-query);
@@ -35,11 +36,6 @@
3536
@include mdc-button-text-theme.theme-styles(
3637
map.merge(mdc-button-text-theme.$light-theme, $override-keys));
3738
}
38-
39-
.mat-mdc-outlined-button {
40-
@include mdc-button-outlined-theme.theme-styles(
41-
map.merge(mdc-button-outlined-theme.$light-theme, $override-keys));
42-
}
4339
}
4440

4541
@include mdc-custom-properties.configure($emit-fallback-values: false, $emit-fallback-vars: false) {
@@ -66,6 +62,18 @@
6662
// Add default values for MDC text button tokens that aren't outputted by the theming API.
6763
@include mdc-button-protected-theme.theme(tokens-mdc-button-protected.get-unthemable-tokens());
6864
}
65+
66+
// Note that we don't include a feature query, because this mixins declare
67+
// all the "slots" for CSS variables that will be defined in the theme.
68+
.mat-mdc-outlined-button {
69+
$mdc-button-outlined-slots: tokens-mdc-button-outlined.get-token-slots();
70+
71+
// Add the slots for MDC text button.
72+
@include mdc-button-outlined-theme.theme-styles($mdc-button-outlined-slots);
73+
74+
// Add default values for MDC text button tokens that aren't outputted by the theming API.
75+
@include mdc-button-outlined-theme.theme(tokens-mdc-button-outlined.get-unthemable-tokens());
76+
}
6977
}
7078

7179
.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, button-protected);
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: null
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/card/elevated-card-theme' as mdc-elevated-card-theme;
67
@use '@material/card/outlined-card-theme' as mdc-outlined-card-theme;
@@ -24,6 +25,7 @@
2425
@use '@material/textfield/outlined-text-field-theme' as mdc-outlined-text-field-theme;
2526
@use '@material/theme/validate' as mdc-validate;
2627

28+
@use '../m2/mdc/button-outlined' as tokens-mdc-button-outlined;
2729
@use '../m2/mdc/button-protected' as tokens-mdc-button-protected;
2830
@use '../m2/mdc/circular-progress' as tokens-mdc-circular-progress;
2931
@use '../m2/mdc/linear-progress' as tokens-mdc-linear-progress;
@@ -168,3 +170,8 @@
168170
$slots: tokens-mdc-button-protected.get-token-slots(),
169171
$reference: mdc-button-protected-theme.$light-theme
170172
);
173+
@include validate-slots(
174+
$component: 'm2.mdc.button-outlined',
175+
$slots: tokens-mdc-button-outlined.get-token-slots(),
176+
$reference: mdc-button-outlined-theme.$light-theme
177+
);

0 commit comments

Comments
 (0)