Skip to content

Commit 44b57a5

Browse files
committed
fix(material/tooltip): change tooltip to use MDC's token API
1 parent d10c189 commit 44b57a5

File tree

4 files changed

+124
-39
lines changed

4 files changed

+124
-39
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
@use 'sass:map';
2+
@use '../../../theming/theming';
3+
@use '../../../typography/typography-utils';
4+
@use '../../../mdc-helpers/mdc-helpers';
5+
@use '../../token-utils';
6+
7+
// The prefix used to generate the fully qualified name for tokens in this file.
8+
$prefix: (mdc, plain-tooltip);
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+
// Border radius for the tooltip container.
19+
container-shape: 4px,
20+
// Line height of the tooltip text.
21+
supporting-text-line-height: 16px,
22+
// MDC does not seem to use these token.
23+
supporting-text-type: null,
24+
);
25+
}
26+
27+
// Tokens that can be configured through Angular Material's color theming API.
28+
@function get-color-tokens($config) {
29+
$background: map.get($config, background);
30+
31+
@return (
32+
// Color of the tooltip container.
33+
container-color: theming.get-color-from-palette($background, tooltip),
34+
// Color of the tooltip text.
35+
supporting-text-color: #fff,
36+
);
37+
}
38+
39+
// Tokens that can be configured through Angular Material's typography theming API.
40+
@function get-typography-tokens($config) {
41+
// TODO(amysorto): The earlier implementation of the tooltip used MDC's APIs to create the
42+
// typography tokens. As a result, we unintentionally allowed `null` typography configs to be
43+
// passed in. Since there a lot of apps that now depend on this pattern, we need this temporary
44+
// fallback.
45+
@if ($config == null) {
46+
$config: mdc-helpers.private-fallback-typography-from-mdc();
47+
}
48+
49+
@return (
50+
// Font for the tooltip text.
51+
supporting-text-font: typography-utils.font-family($config, caption)
52+
or typography-utils.font-family($config),
53+
// Font size for the the tooltip text.
54+
supporting-text-size: typography-utils.font-size($config, caption),
55+
// Font weight of the the tooltip text.
56+
supporting-text-weight: typography-utils.font-weight($config, caption),
57+
// Tracking (space between letters) of the tooltip text.
58+
supporting-text-tracking: typography-utils.letter-spacing($config, caption),
59+
);
60+
}
61+
62+
// Tokens that can be configured through Angular Material's density theming API.
63+
@function get-density-tokens($config) {
64+
@return ();
65+
}
66+
67+
// Combines the tokens generated by the above functions into a single map with placeholder values.
68+
// This is used to create token slots.
69+
@function get-token-slots() {
70+
@return map.merge(
71+
get-unthemable-tokens(),
72+
map.merge(
73+
get-color-tokens(token-utils.$placeholder-color-config),
74+
map.merge(
75+
get-typography-tokens(token-utils.$placeholder-typography-config),
76+
get-density-tokens(token-utils.$placeholder-density-config)
77+
)
78+
)
79+
);
80+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
@use '@material/icon-button/icon-button-theme' as mdc-icon-button-theme;
99
@use '@material/linear-progress/linear-progress-theme' as mdc-linear-progress-theme;
1010
@use '@material/list/list-theme' as mdc-list-theme;
11+
@use '@material/tooltip/plain-tooltip-theme' as mdc-plaintip-tooltip-theme;
1112
@use '@material/radio/radio-theme' as mdc-radio-theme;
1213
@use '@material/tab-indicator/tab-indicator-theme' as mdc-tab-indicator-theme;
1314
@use '@material/tab/tab-theme' as mdc-tab-theme;
@@ -21,6 +22,7 @@
2122
@use '../m2/mdc/linear-progress' as tokens-mdc-linear-progress;
2223
@use '../m2/mdc/list' as tokens-mdc-list;
2324
@use '../m2/mdc/outlined-card' as tokens-mdc-outlined-card;
25+
@use '../m2/mdc/plain-tooltip' as tokens-mdc-plain-tooltip;
2426
@use '../m2/mdc/radio' as tokens-mdc-radio;
2527
@use '../m2/mdc/tab-indicator' as tokens-mdc-tab-indicator;
2628
@use '../m2/mdc/tab' as tokens-mdc-tab;
@@ -73,6 +75,11 @@
7375
$slots: tokens-mdc-linear-progress.get-token-slots(),
7476
$reference: mdc-linear-progress-theme.$light-theme
7577
);
78+
@include validate-slots(
79+
$component: 'm2.mdc.plain-tooltip',
80+
$slots: tokens-mdc-plain-tooltip.get-token-slots(),
81+
$reference: mdc-plaintip-tooltip-theme.$light-theme
82+
);
7683
@include validate-slots(
7784
$component: 'm2.mdc.radio',
7885
$slots: tokens-mdc-radio.get-token-slots(),

src/material/tooltip/_tooltip-theme.scss

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,37 @@
1-
@use 'sass:map';
2-
@use '@material/tooltip/plain-tooltip-theme';
3-
@use '@material/theme/theme-color' as mdc-theme-color;
4-
@use '@material/typography/typography' as mdc-typography;
1+
@use '@material/tooltip/plain-tooltip-theme' as mdc-plain-tooltip-theme;
52
@use '../core/theming/theming';
6-
@use '../core/theming/palette';
7-
@use '../core/mdc-helpers/mdc-helpers';
83
@use '../core/typography/typography';
4+
@use '../core/tokens/m2/mdc/plain-tooltip' as m2-mdc-plain-tooltip;
95

106
@mixin color($config-or-theme) {
117
$config: theming.get-color-config($config-or-theme);
12-
@include mdc-helpers.using-mdc-theme($config) {
13-
.mat-mdc-tooltip {
14-
@include plain-tooltip-theme.theme((
15-
container-color: map.get(palette.$light-theme-background-palette, tooltip),
16-
supporting-text-color: mdc-theme-color.prop-value(text-primary-on-dark)
17-
));
18-
}
8+
$mdc-tooltip-color-tokens: m2-mdc-plain-tooltip.get-color-tokens($config);
9+
10+
// Add values for MDC tooltip tokens.
11+
.mat-mdc-tooltip {
12+
@include mdc-plain-tooltip-theme.theme($mdc-tooltip-color-tokens);
1913
}
2014
}
2115

2216
@mixin typography($config-or-theme) {
2317
$config: typography.private-typography-to-2018-config(
2418
theming.get-typography-config($config-or-theme));
25-
@include mdc-helpers.using-mdc-typography($config) {
26-
.mat-mdc-tooltip {
27-
@include plain-tooltip-theme.theme((
28-
supporting-text-font: mdc-typography.get-font(caption),
29-
supporting-text-size: mdc-typography.get-size(caption),
30-
supporting-text-weight: mdc-typography.get-weight(caption),
31-
supporting-text-tracking: mdc-typography.get-tracking(caption),
32-
));
33-
}
19+
$mdc-tooltip-typography-tokens: m2-mdc-plain-tooltip.get-typography-tokens($config);
20+
21+
// Add values for MDC tooltip tokens.
22+
.mat-mdc-tooltip {
23+
@include mdc-plain-tooltip-theme.theme($mdc-tooltip-typography-tokens);
3424
}
3525
}
3626

3727
@mixin density($config-or-theme) {
3828
$density-scale: theming.get-density-config($config-or-theme);
29+
$mdc-tooltip-density-tokens: m2-mdc-plain-tooltip.get-density-tokens($density-scale);
30+
31+
// Add values for MDC tooltip tokens.
32+
.mat-mdc-tooltip {
33+
@include mdc-plain-tooltip-theme.theme($mdc-tooltip-density-tokens);
34+
}
3935
}
4036

4137
@mixin theme($theme-or-color-config) {

src/material/tooltip/tooltip.scss

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
@use '@material/tooltip/tooltip';
2-
@use '@material/tooltip/tooltip-theme';
3-
@use '@material/tooltip/plain-tooltip-theme';
4-
@use '../core/mdc-helpers/mdc-helpers';
5-
6-
@include mdc-helpers.disable-mdc-fallback-declarations {
7-
// Only include the structural styles, because we handle the animation ourselves.
8-
@include tooltip.static-styles($query: structure);
9-
@include plain-tooltip-theme.theme-styles((
10-
container-shape: tooltip-theme.$border-radius,
11-
container-color: #fff,
12-
supporting-text-color: #000,
13-
supporting-text-font: inherit,
14-
supporting-text-size: inherit,
15-
supporting-text-tracking: inherit,
16-
supporting-text-weight: inherit,
17-
));
1+
@use '@material/theme/custom-properties' as mdc-custom-properties;
2+
@use '@material/tooltip/tooltip' as mdc-tooltip;
3+
@use '@material/tooltip/plain-tooltip-theme' as mdc-plain-tooltip-theme;
4+
@use '../core/tokens/m2/mdc/plain-tooltip' as m2-mdc-plain-tooltip;
5+
6+
@include mdc-custom-properties.configure($emit-fallback-values: false,
7+
$emit-fallback-vars: false) {
8+
$mdc-tooltip-token-slots: m2-mdc-plain-tooltip.get-token-slots();
9+
10+
// Add the MDC tooltip static styles.
11+
@include mdc-tooltip.static-styles();
12+
13+
.mat-mdc-tooltip {
14+
// Add the official slots for the MDC tooltip.
15+
@include mdc-plain-tooltip-theme.theme-styles($mdc-tooltip-token-slots);
16+
17+
// Add default values for MDC tooltip tokens that aren't outputted by the theming API.
18+
@include mdc-plain-tooltip-theme.theme(m2-mdc-plain-tooltip.get-unthemable-tokens());
19+
}
1820
}
1921

2022
.mat-mdc-tooltip {

0 commit comments

Comments
 (0)