Skip to content

Commit 8872c16

Browse files
authored
refactor(material/autocomplete): switch to tokens API (#27288)
Reworks the autocomplete to use the new tokens API.
1 parent 5dd6a0d commit 8872c16

File tree

4 files changed

+73
-33
lines changed

4 files changed

+73
-33
lines changed

src/material/autocomplete/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ sass_binary(
3838
deps = [
3939
"//:mdc_sass_lib",
4040
"//src/cdk:sass_lib",
41+
"//src/material/core:core_scss_lib",
4142
],
4243
)
4344

src/material/autocomplete/_autocomplete-theme.scss

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,20 @@
1-
@use '@material/menu-surface/mixins' as mdc-menu-surface;
2-
@use '@material/list/evolution-mixins' as mdc-list;
31
@use '../core/theming/theming';
42
@use '../core/typography/typography';
5-
@use '../core/mdc-helpers/mdc-helpers';
3+
@use '../core/style/sass-utils';
4+
@use '../core/tokens/token-utils';
5+
@use '../core/tokens/m2/mat/autocomplete' as tokens-mat-autocomplete;
66

77
@mixin color($config-or-theme) {
88
$config: theming.get-color-config($config-or-theme);
9-
@include mdc-helpers.using-mdc-theme($config) {
10-
@include mdc-menu-surface.core-styles(mdc-helpers.$mdc-theme-styles-query);
11-
@include mdc-list.without-ripple(mdc-helpers.$mdc-theme-styles-query);
12-
}
13-
}
14-
15-
@mixin typography($config-or-theme) {
16-
$config: typography.private-typography-to-2018-config(
17-
theming.get-typography-config($config-or-theme));
18-
@include mdc-helpers.using-mdc-typography($config) {
19-
@include mdc-menu-surface.core-styles(mdc-helpers.$mdc-typography-styles-query);
209

21-
.mat-mdc-autocomplete-panel {
22-
// Note that we include this private mixin, because the public one adds
23-
// a bunch of styles that we aren't using for the autocomplete panel.
24-
@include mdc-list.list-base(mdc-helpers.$mdc-typography-styles-query);
25-
}
10+
@include sass-utils.current-selector-or-root() {
11+
@include token-utils.create-token-values(tokens-mat-autocomplete.$prefix,
12+
tokens-mat-autocomplete.get-color-tokens($config));
2613
}
2714
}
2815

16+
@mixin typography($config-or-theme) {}
17+
2918
@mixin density($config-or-theme) {}
3019

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

src/material/autocomplete/autocomplete.scss

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
@use '@angular/cdk';
2-
@use '@material/menu-surface/mixins' as mdc-menu-surface;
3-
@use '@material/list/evolution-mixins' as mdc-list;
4-
5-
@include mdc-menu-surface.core-styles($query: structure);
6-
7-
// Note that the `.mdc-menu-surface` is here in order to bump up the specificity
8-
// and avoid interference with `mat-menu` which uses the same mixins from MDC.
9-
.mdc-menu-surface.mat-mdc-autocomplete-panel {
2+
@use '../core/style/elevation';
3+
@use '../core/tokens/token-utils';
4+
@use '../core/tokens/m2/mat/autocomplete' as tokens-mat-autocomplete;
5+
6+
// Even though we don't use the MDC styles, we need to keep the classes in the
7+
// DOM for the Gmat versions to work. We need to bump up the specificity here
8+
// so that it's higher than MDC's styles.
9+
div.mat-mdc-autocomplete-panel {
10+
@include elevation.elevation(8);
1011
width: 100%; // Ensures that the panel matches the overlay width.
1112
max-height: 256px; // Prevents lists with a lot of option from growing too high.
12-
position: static; // MDC uses `absolute` by default which will throw off our positioning.
1313
visibility: hidden;
14-
// MDC sets the transform-origin programmatically based on whether the dropdown is above or
15-
// below the input. We use our own positioning logic, so we need to set this ourselves.
1614
transform-origin: center top;
15+
overflow: auto;
16+
padding: 8px 0;
17+
border-radius: 4px;
18+
box-sizing: border-box;
19+
20+
// Workaround in case other MDC menu surface styles bleed in.
21+
position: static;
22+
23+
@include token-utils.use-tokens(
24+
tokens-mat-autocomplete.$prefix, tokens-mat-autocomplete.get-token-slots()) {
25+
@include token-utils.create-token-slot(background-color, background-color);
26+
}
1727

18-
@include mdc-list.list-base($query: structure);
1928
@include cdk.high-contrast(active, off) {
2029
outline: solid 1px;
2130
}
@@ -28,8 +37,6 @@
2837
.mat-mdc-autocomplete-panel-above & {
2938
border-bottom-left-radius: 0;
3039
border-bottom-right-radius: 0;
31-
// MDC sets the transform-origin programmatically based on whether the dropdown is above or
32-
// below the input. We use our own positioning logic, so we need to set this ourselves.
3340
transform-origin: center bottom;
3441
}
3542

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
@use 'sass:map';
2+
@use '../../token-utils';
3+
@use '../../../theming/theming';
4+
@use '../../../style/sass-utils';
5+
6+
// The prefix used to generate the fully qualified name for tokens in this file.
7+
$prefix: (mat, autocomplete);
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+
@function get-unthemable-tokens() {
12+
@return ();
13+
}
14+
15+
// Tokens that can be configured through Angular Material's color theming API.
16+
@function get-color-tokens($config) {
17+
$background: map.get($config, background);
18+
19+
@return (
20+
background-color: theming.get-color-from-palette($background, card)
21+
);
22+
}
23+
24+
// Tokens that can be configured through Angular Material's typography theming API.
25+
@function get-typography-tokens($config) {
26+
@return ();
27+
}
28+
29+
// Tokens that can be configured through Angular Material's density theming API.
30+
@function get-density-tokens($config) {
31+
@return ();
32+
}
33+
34+
// Combines the tokens generated by the above functions into a single map with placeholder values.
35+
// This is used to create token slots.
36+
@function get-token-slots() {
37+
@return sass-utils.deep-merge-all(
38+
get-unthemable-tokens(),
39+
get-color-tokens(token-utils.$placeholder-color-config),
40+
get-typography-tokens(token-utils.$placeholder-typography-config),
41+
get-density-tokens(token-utils.$placeholder-density-config)
42+
);
43+
}

0 commit comments

Comments
 (0)