Skip to content

applyTheme sets the wrong CSS vars, doesn't have any effect in Angular Material 3 #187

@tdcolvin

Description

@tdcolvin

I am using Angular Material 3 and I want to set the theme dynamically at runtime, based on user config. I am using applyTheme to do this:

  applyCustomM3Theme() {
    const primaryArgb = argbFromHex(this.primaryColour);
    const theme = themeFromSourceColor(primaryArgb, []);

    applyTheme(theme, { 
      target: document.documentElement, // Apply to the root element
      dark: this.darkTheme
    });

    document.documentElement.style.setProperty('color-scheme', this.darkTheme ? 'dark' : 'light');
  }

But this seems to have no effect on my primary-coloured Angular Material 3 button:

<button mat-flat-button color="primary" (click)="joinRoom()">
Join
</button>

On investigation, it seems to be caused by applyTheme setting CSS variables that have nothing to do with the Material 3 theme. It sets the following:

--md-sys-color-primary: #edb1ff; --md-sys-color-on-primary: #53006f; --md-sys-color-primary-container: #75039c; --md-sys-color-on-primary-container: #f9d8ff; --md-sys-color-secondary: #d5c0d7; --md-sys-color-on-secondary: #3a2c3d; --md-sys-color-secondary-container: #514254; --md-sys-color-on-secondary-container: #f2dcf3; --md-sys-color-tertiary: #f5b7b3; --md-sys-color-on-tertiary: #4c2523; --md-sys-color-tertiary-container: #663b38; --md-sys-color-on-tertiary-container: #ffdad7; --md-sys-color-error: #ffb4ab; --md-sys-color-on-error: #690005; --md-sys-color-error-container: #93000a; --md-sys-color-on-error-container: #ffb4ab; --md-sys-color-background: #1e1b1e; --md-sys-color-on-background: #e8e0e5; --md-sys-color-surface: #1e1b1e; --md-sys-color-on-surface: #e8e0e5; --md-sys-color-surface-variant: #4c444d; --md-sys-color-on-surface-variant: #cfc3cd; --md-sys-color-outline: #988e97; --md-sys-color-outline-variant: #4c444d; --md-sys-color-shadow: #000000; --md-sys-color-scrim: #000000; --md-sys-color-inverse-surface: #e8e0e5; --md-sys-color-inverse-on-surface: #332f33; --md-sys-color-inverse-primary: #902eb6; color-scheme: dark;

But the button's colour is defined as:

.mat-mdc-unelevated-button:not(:disabled) {
  color: var(--mat-button-filled-label-text-color, var(--mat-sys-on-primary));
  background-color: var(--mat-button-filled-container-color, var(--mat-sys-primary));
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions