Last active
May 2, 2022 06:02
-
-
Save tomastrajan/ee29cd8e180b14ce9bc120e2f7435db7 to your computer and use it in GitHub Desktop.
Angular Material Theming - overlay handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { OverlayContainer } from '@angular/cdk/overlay'; | |
export class AppComponent implements OnInit { | |
// use this to set correct theme class on app holder | |
// eg: <div [class]="themeClass">...</div> | |
themeClass: string; | |
constructor( | |
private overlayContainer: OverlayContainer | |
) {} | |
ngOnInit(): void { | |
// subscribe to some source of theme change events, then... | |
this.themeClass = newThemeClass; | |
// remove old theme class and add new theme class | |
// we're removing any css class that contains '-theme' string but your theme classes can follow any pattern | |
const overlayContainerClasses = this.overlayContainer.getContainerElement().classList; | |
const themeClassesToRemove = Array.from(classList).filter((item: string) => item.includes('-theme')); | |
if (themeClassesToRemove.length) { | |
overlayContainerClasses.remove(...themeClassesToRemove); | |
} | |
overlayContainerClasses.add(newThemeClass); | |
} | |
} |
Thanks VladislavBaranov your insight was very helpful
I'm attempting to get this working. I created a demo using a mat-select
and it looks like the theme class is not being added to the overlay container. I asked about this on both StackOverflow and the material repository. This is the issue link:
Anyone have any ideas?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This should help in Angular 6 and Angular Material 6.x