Skip to content

Commit df9e776

Browse files
committed
fix(material/slider): change slider to use MDC's token API
1 parent 0d499a3 commit df9e776

File tree

4 files changed

+291
-156
lines changed

4 files changed

+291
-156
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
@use 'sass:map';
2+
@use '../../../theming/theming';
3+
@use '../../token-utils';
4+
@use '../../../style/sass-utils';
5+
@use '../../../typography/typography-utils';
6+
7+
// The prefix used to generate the fully qualified name for tokens in this file.
8+
$prefix: (mdc, slider);
9+
10+
// Tokens that can't be configured through Angular Material's current theming API,
11+
// but may be in a future version of the theming API.
12+
//
13+
// Tokens that are available in MDC, but not used in Angular Material should be mapped to `null`.
14+
// `null` indicates that we are intentionally choosing not to emit a slot or value for the token in
15+
// our CSS.
16+
@function get-unthemable-tokens() {
17+
@return (
18+
active-track-height: 6px,
19+
active-track-shape: 9999px,
20+
handle-height: 20px,
21+
handle-shape: 50%,
22+
handle-width: 20px,
23+
inactive-track-height: 4px,
24+
inactive-track-shape: 9999px,
25+
with-overlap-handle-outline-width: 1px,
26+
with-tick-marks-active-container-opacity: 0.6,
27+
with-tick-marks-container-shape: 50%,
28+
with-tick-marks-container-size: 2px,
29+
with-tick-marks-inactive-container-opacity: 0.6,
30+
// =============================================================================================
31+
// = TOKENS NOT USED IN ANGULAR MATERIAL =
32+
// =============================================================================================
33+
disabled-handle-elevation: null,
34+
label-container-elevation: null,
35+
label-container-height: null,
36+
track-elevation: null,
37+
focus-state-layer-opacity: null,
38+
hover-state-layer-opacity: null,
39+
pressed-state-layer-opacity: null,
40+
);
41+
}
42+
43+
// Tokens that can be configured through Angular Material's color theming API.
44+
@function get-color-tokens($config) {
45+
$foreground: map.get($config, foreground);
46+
$elevation: theming.get-color-from-palette($foreground, elevation);
47+
$accent: map.get($config, accent);
48+
$accent-default: theming.get-color-from-palette($accent, default);
49+
$is-dark: map.get($config, is-dark);
50+
$on-surface: if($is-dark, #fff, #000);
51+
52+
// The default for tokens that rely on the theme will use the primary palette
53+
$theme-color-tokens: private-get-color-palette-color-tokens(
54+
map.get($config, primary)
55+
);
56+
57+
@return map.merge(
58+
$theme-color-tokens,
59+
(
60+
disabled-active-track-color: $on-surface,
61+
disabled-handle-color: $on-surface,
62+
disabled-inactive-track-color: $accent-default,
63+
label-container-color: if($is-dark, #fff, #000),
64+
label-label-text-color: if($is-dark, #000, #fff),
65+
pressed-handle-color: null,
66+
with-overlap-handle-outline-color: #fff,
67+
with-tick-marks-disabled-container-color: $on-surface,
68+
hover-state-layer-color: null,
69+
with-tick-marks-active-container-color: #fff,
70+
// (Part of the color tokens because it needs to be combined with the
71+
// shadow color to generate the final box-shadow).
72+
handle-elevation: 1,
73+
handle-shadow-color: if($elevation != null, $elevation, elevation.$color),
74+
)
75+
);
76+
}
77+
78+
@function private-get-color-palette-color-tokens($color-palette) {
79+
$color: theming.get-color-from-palette($color-palette);
80+
81+
@return (
82+
handle-color: $color,
83+
focus-handle-color: $color,
84+
hover-handle-color: $color,
85+
active-track-color: $color,
86+
inactive-track-color: $color,
87+
with-tick-marks-inactive-container-color: $color,
88+
);
89+
}
90+
91+
// Tokens that can be configured through Angular Material's typography theming API.
92+
@function get-typography-tokens($config) {
93+
@if ($config == null) {
94+
$config: mdc-helpers.private-fallback-typography-from-mdc();
95+
}
96+
97+
@return (
98+
label-label-text-font: typography-utils.font-family($config, subtitle-2)
99+
or typography-utils.font-family($config),
100+
label-label-text-size: typography-utils.font-size($config, subtitle-2),
101+
label-label-text-line-height: typography-utils.line-height($config, subtitle-2),
102+
label-label-text-tracking: typography-utils.letter-spacing($config, subtitle-2),
103+
label-label-text-weight: typography-utils.font-weight($config, subtitle-2),
104+
);
105+
}
106+
107+
// Tokens that can be configured through Angular Material's density theming API.
108+
@function get-density-tokens($config) {
109+
@return ();
110+
}
111+
112+
// Combines the tokens generated by the above functions into a single map with placeholder values.
113+
// This is used to create token slots.
114+
@function get-token-slots() {
115+
@return sass-utils.deep-merge-all(
116+
get-unthemable-tokens(),
117+
get-color-tokens(token-utils.$placeholder-color-config),
118+
get-typography-tokens(token-utils.$placeholder-typography-config),
119+
get-density-tokens(token-utils.$placeholder-density-config)
120+
);
121+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
@use '@material/tab-indicator/tab-indicator-theme' as mdc-tab-indicator-theme;
1515
@use '@material/tab/tab-theme' as mdc-tab-theme;
1616
@use '@material/snackbar/snackbar-theme' as mdc-snackbar-theme;
17+
@use '@material/slider/slider-theme' as mdc-slider-theme;
1718
@use '@material/chips/chip-theme' as mdc-chips-theme;
1819
@use '@material/dialog/dialog-theme' as mdc-dialog-theme;
1920
@use '@material/theme/validate' as mdc-validate;
@@ -31,6 +32,7 @@
3132
@use '../m2/mdc/tab-indicator' as tokens-mdc-tab-indicator;
3233
@use '../m2/mdc/tab' as tokens-mdc-tab;
3334
@use '../m2/mdc/snack-bar' as tokens-mdc-snack-bar;
35+
@use '../m2/mdc/slider' as tokens-mdc-slider;
3436
@use '../m2/mdc/chip' as tokens-mdc-chip;
3537
@use '../m2/mdc/dialog' as tokens-mdc-dialog;
3638

@@ -111,6 +113,11 @@
111113
$slots: tokens-mdc-snack-bar.get-token-slots(),
112114
$reference: mdc-snackbar-theme.$light-theme
113115
);
116+
@include validate-slots(
117+
$component: 'm2.mdc.slider',
118+
$slots: tokens-mdc-slider.get-token-slots(),
119+
$reference: mdc-slider-theme.$light-theme
120+
);
114121
@include validate-slots(
115122
$component: 'm2.mdc.chips',
116123
$slots: tokens-mdc-chip.get-token-slots(),
Lines changed: 54 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,71 @@
11
@use 'sass:map';
22

33
@use '@material/slider/slider-theme' as mdc-slider-theme;
4-
@use '@material/theme/theme-color' as mdc-theme-color;
5-
@use '@material/typography/typography' as mdc-typography;
6-
@use '@material/theme/variables' as mdc-theme-variables;
7-
@use '../core/mdc-helpers/mdc-helpers';
84
@use '../core/theming/theming';
95
@use '../core/typography/typography';
6+
@use '../core/tokens/token-utils';
7+
@use '../core/tokens/m2/mdc/slider' as m2-mdc-slider;
108

119

1210
@mixin color($config-or-theme) {
1311
$config: theming.get-color-config($config-or-theme);
12+
$is-dark: map.get($config, is-dark);
13+
14+
$color-tokens: token-utils.resolve-elevation(
15+
m2-mdc-slider.get-color-tokens($config),
16+
handle-elevation,
17+
handle-shadow-color
18+
);
19+
20+
// Add values for MDC slider tokens.
21+
.mat-mdc-slider {
22+
@include mdc-slider-theme.theme($color-tokens);
23+
@include _slider-ripple-color(map.get($config, primary));
24+
25+
// TODO: try to remove this once handle shadow gets added
26+
// Note that technically we can control this using an `rgba` color in `label-container-color`.
27+
// We don't do it, because the shapes MDC uses to construct the indicator overlap which causes
28+
// their color opacities to stack with an `rgba` color.
29+
--mat-mdc-slider-value-indicator-opacity: #{if($is-dark, 0.9, 0.6)};
30+
31+
&.mat-accent {
32+
$accent: map.get($config, accent);
33+
@include mdc-slider-theme.theme(
34+
m2-mdc-slider.private-get-color-palette-color-tokens($accent)
35+
);
36+
@include _slider-ripple-color($accent);
37+
}
1438

15-
@include mdc-helpers.using-mdc-theme($config) {
16-
.mat-mdc-slider {
17-
$is-dark: map.get($config, is-dark);
18-
$on-surface: mdc-theme-color.prop-value(on-surface);
19-
20-
@include mdc-slider-theme.theme((
21-
label-container-color: if($is-dark, white, black),
22-
label-label-text-color: if($is-dark, black, white),
23-
disabled-handle-color: $on-surface,
24-
disabled-active-track-color: $on-surface,
25-
disabled-inactive-track-color: $on-surface,
26-
with-tick-marks-disabled-container-color: $on-surface,
27-
));
28-
29-
// Note that technically we can control this using an `rgba` color in `label-container-color`.
30-
// We don't do it, because the shapes MDC uses to construct the indicator overlap which causes
31-
// their color opacities to stack with an `rgba` color.
32-
--mat-mdc-slider-value-indicator-opacity: #{if($is-dark, 0.9, 0.6)};
33-
34-
&.mat-primary {
35-
@include _slider-color(primary, on-primary);
36-
}
37-
38-
&.mat-accent {
39-
@include _slider-color(secondary, on-secondary);
40-
}
41-
42-
&.mat-warn {
43-
@include _slider-color(error, on-error);
44-
}
39+
&.mat-warn {
40+
$warn: map.get($config, warn);
41+
@include mdc-slider-theme.theme(
42+
m2-mdc-slider.private-get-color-palette-color-tokens($warn)
43+
);
44+
@include _slider-ripple-color($warn);
4545
}
4646
}
4747
}
4848

4949
@mixin typography($config-or-theme) {
5050
$config: typography.private-typography-to-2018-config(
5151
theming.get-typography-config($config-or-theme));
52-
@include mdc-helpers.using-mdc-typography($config) {
53-
.mat-mdc-slider {
54-
@include mdc-slider-theme.theme((
55-
label-label-text-font: mdc-typography.get-font(subtitle2),
56-
label-label-text-size: mdc-typography.get-size(subtitle2),
57-
label-label-text-line-height: mdc-typography.get-line-height(subtitle2),
58-
label-label-text-tracking: mdc-typography.get-tracking(subtitle2),
59-
label-label-text-weight: mdc-typography.get-weight(subtitle2),
60-
));
61-
}
52+
$typography-tokens: m2-mdc-slider.get-typography-tokens($config);
53+
54+
// Add values for MDC slider tokens.
55+
.mat-mdc-slider {
56+
@include mdc-slider-theme.theme($typography-tokens);
6257
}
6358
}
6459

65-
@mixin density($config-or-theme) {}
60+
@mixin density($config-or-theme) {
61+
$density-scale: theming.get-density-config($config-or-theme);
62+
$density-tokens: m2-mdc-slider.get-density-tokens($density-scale);
63+
64+
// Add values for MDC slider tokens.
65+
.mat-mdc-slider {
66+
@include mdc-slider-theme.theme($density-tokens);
67+
}
68+
}
6669

6770
@mixin theme($theme-or-color-config) {
6871
$theme: theming.private-legacy-get-theme($theme-or-color-config);
@@ -83,22 +86,10 @@
8386
}
8487
}
8588

86-
@mixin _slider-color($color, $on-color) {
87-
$ripple-color: map.get(mdc-theme-variables.$property-values, $color);
88-
$resolved-color: mdc-theme-color.prop-value($color);
89-
$resolved-on-color: mdc-theme-color.prop-value($on-color);
90-
91-
@include mdc-slider-theme.theme((
92-
handle-color: $resolved-color,
93-
focus-handle-color: $resolved-color,
94-
hover-handle-color: $resolved-color,
95-
active-track-color: $resolved-color,
96-
inactive-track-color: $resolved-color,
97-
with-tick-marks-active-container-color: $resolved-on-color,
98-
with-tick-marks-inactive-container-color: $resolved-color,
99-
));
100-
101-
--mat-mdc-slider-ripple-color: #{$ripple-color};
102-
--mat-mdc-slider-hover-ripple-color: #{rgba($ripple-color, 0.05)};
103-
--mat-mdc-slider-focus-ripple-color: #{rgba($ripple-color, 0.2)};
89+
// TODO: add comment why this isn't in _slider.scss file and why this is needed
90+
@mixin _slider-ripple-color($color-palette) {
91+
$color: theming.get-color-from-palette($color-palette);
92+
--mat-mdc-slider-ripple-color: #{$color};
93+
--mat-mdc-slider-hover-ripple-color: #{rgba($color, 0.05)};
94+
--mat-mdc-slider-focus-ripple-color: #{rgba($color, 0.2)};
10495
}

0 commit comments

Comments
 (0)