Skip to content

refactor(material/autocomplete): switch to tokens API #27288

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/material/autocomplete/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sass_binary(
deps = [
"//:mdc_sass_lib",
"//src/cdk:sass_lib",
"//src/material/core:core_scss_lib",
],
)

Expand Down
27 changes: 8 additions & 19 deletions src/material/autocomplete/_autocomplete-theme.scss
Original file line number Diff line number Diff line change
@@ -1,31 +1,20 @@
@use '@material/menu-surface/mixins' as mdc-menu-surface;
@use '@material/list/evolution-mixins' as mdc-list;
@use '../core/theming/theming';
@use '../core/typography/typography';
@use '../core/mdc-helpers/mdc-helpers';
@use '../core/style/sass-utils';
@use '../core/tokens/token-utils';
@use '../core/tokens/m2/mat/autocomplete' as tokens-mat-autocomplete;

@mixin color($config-or-theme) {
$config: theming.get-color-config($config-or-theme);
@include mdc-helpers.using-mdc-theme($config) {
@include mdc-menu-surface.core-styles(mdc-helpers.$mdc-theme-styles-query);
@include mdc-list.without-ripple(mdc-helpers.$mdc-theme-styles-query);
}
}

@mixin typography($config-or-theme) {
$config: typography.private-typography-to-2018-config(
theming.get-typography-config($config-or-theme));
@include mdc-helpers.using-mdc-typography($config) {
@include mdc-menu-surface.core-styles(mdc-helpers.$mdc-typography-styles-query);

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

@mixin typography($config-or-theme) {}

@mixin density($config-or-theme) {}

@mixin theme($theme-or-color-config) {
Expand Down
35 changes: 21 additions & 14 deletions src/material/autocomplete/autocomplete.scss
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
@use '@angular/cdk';
@use '@material/menu-surface/mixins' as mdc-menu-surface;
@use '@material/list/evolution-mixins' as mdc-list;

@include mdc-menu-surface.core-styles($query: structure);

// Note that the `.mdc-menu-surface` is here in order to bump up the specificity
// and avoid interference with `mat-menu` which uses the same mixins from MDC.
.mdc-menu-surface.mat-mdc-autocomplete-panel {
@use '../core/style/elevation';
@use '../core/tokens/token-utils';
@use '../core/tokens/m2/mat/autocomplete' as tokens-mat-autocomplete;

// Even though we don't use the MDC styles, we need to keep the classes in the
// DOM for the Gmat versions to work. We need to bump up the specificity here
// so that it's higher than MDC's styles.
div.mat-mdc-autocomplete-panel {
@include elevation.elevation(8);
width: 100%; // Ensures that the panel matches the overlay width.
max-height: 256px; // Prevents lists with a lot of option from growing too high.
position: static; // MDC uses `absolute` by default which will throw off our positioning.
visibility: hidden;
// MDC sets the transform-origin programmatically based on whether the dropdown is above or
// below the input. We use our own positioning logic, so we need to set this ourselves.
transform-origin: center top;
overflow: auto;
padding: 8px 0;
border-radius: 4px;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see the border-radius and padding potentially being themable here, but MDC's menu surface doesn't expose tokens for them so I didn't either. We can always add custom tokens afterwards.

box-sizing: border-box;

// Workaround in case other MDC menu surface styles bleed in.
position: static;

@include token-utils.use-tokens(
tokens-mat-autocomplete.$prefix, tokens-mat-autocomplete.get-token-slots()) {
@include token-utils.create-token-slot(background-color, background-color);
}

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

Expand Down
43 changes: 43 additions & 0 deletions src/material/core/tokens/m2/mat/_autocomplete.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@use 'sass:map';
@use '../../token-utils';
@use '../../../theming/theming';
@use '../../../style/sass-utils';

// The prefix used to generate the fully qualified name for tokens in this file.
$prefix: (mat, autocomplete);

// Tokens that can't be configured through Angular Material's current theming API,
// but may be in a future version of the theming API.
@function get-unthemable-tokens() {
@return ();
}

// Tokens that can be configured through Angular Material's color theming API.
@function get-color-tokens($config) {
$background: map.get($config, background);

@return (
background-color: theming.get-color-from-palette($background, card)
);
}

// Tokens that can be configured through Angular Material's typography theming API.
@function get-typography-tokens($config) {
@return ();
}

// Tokens that can be configured through Angular Material's density theming API.
@function get-density-tokens($config) {
@return ();
}

// Combines the tokens generated by the above functions into a single map with placeholder values.
// This is used to create token slots.
@function get-token-slots() {
@return sass-utils.deep-merge-all(
get-unthemable-tokens(),
get-color-tokens(token-utils.$placeholder-color-config),
get-typography-tokens(token-utils.$placeholder-typography-config),
get-density-tokens(token-utils.$placeholder-density-config)
);
}