Skip to content

bug(material/select): Material 3 mat-selects use the secondary color for selected item backgrounds (when the component is styled to primary) #29553

Closed
@bdirito

Description

@bdirito

Is this a regression?

  • Yes, this behavior used to work in the previous version

Material 2 uses a static color for this highlight.

The previous version in which this bug was not present was

No response

Description

The styling of a select (mat-select) highlights the background of a selected element. However even when you are using the default 'primary' styling of a select (as documented https://material.angular.io/guide/theming#using-component-color-variants) the background highlighting of the selected option in the select uses the secondary palette.

Screenshot 2024-08-07 at 10 23 41 AM

Reproduction

This is a stackblitz forked from the mat-select documentation. It has been adjusted to use custom styles as produced by the output of ng generate @angular/material:m3-theme with the following colors: primary: ff0000, secondary: 00ff00, tertiary: 0000ff. The generated m3-theme.scss was copied over into that stack blitz and m3-theme.$light-theme was then used for @include mat.all-component-themes(m3-theme.$light-theme) in styles.scss

StackBlitz link: https://stackblitz.com/edit/nngoxu-ggvcsg?file=src%2Fstyles.scss
Steps to reproduce:

  1. click to select element to open up the options list; note the background of the selected element is miscolored

Expected Behavior

I would expect that the selected background color of a given palette to either be static (as in M2's rgba(0, 0, 0, 0.04)) or to be based on the colors in the palette chosen to style that component.

Possible work arounds:

  • Use material provided palettes (ie mat.$red-palette).
  • Use a secondary color / palette that is produced as a variant the primary color / palette.
  • Set the css variable --mat-option-selected-state-layer-color.

For my code I went with option 3; --mat-option-selected-state-layer-color: rgba(0, 0, 0, 0.04);. Note for option 3, as this is styling something in a cdk-overlay-container you have to make sure that your override still applies.

Actual Behavior

The background color of a 'primary' (default) styled select appears to be using the color as specified in the secondary palette at the '90' level. This 'works' when you use the included scss material palettes because the 'secondary' palette is bundled as a subelement of that palette. A trimmed version of the 'rose' palette (rose/red is one of the styling options on the angular material page), as found in @angular/material/core/themeing/_palettes.scss looks like

/// Rose color palette to be used as primary or tertiary palette.
$rose-palette: _apply-patches((
  0: #000000,
  10: #3f001b,
  <... color levels ...>
  secondary: (
    <... color levels ...>
    90: #ffd9e1, // this is what the selected element actually uses on material.angular.io when using the 'Rose and Red' theme
    <... more color levels>
  ),
  neutral: (
    <... color levels...>
  ),
  neutral-variant: (
     <... color levels ...>
  ),
));

So even for the documentation stylings all of the secondary palettes are mutated forms of the primary palettes and so the selected element's background looks good. When using the ng generate @angular/material:m3-theme no such binding between the primary palette & secondary palette is implied or generated. So when it selects a secondary.90 for styling the background of a primary styled mat-select component it looks wrong.

Environment

  • Angular: 18.0.1
  • CDK/Material: 18.1.3
  • Browser(s): chrome
  • Operating System (e.g. Windows, macOS, Ubuntu): macOs

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions