Setting up themes in Angular Material involves configuring our application to use custom themes or one of the pre-built themes provided by Angular Material.
Theme Selector
We have followed a series of steps for the Angular Material theme selector, as listed below:
Step: 1 We've implemented a menu dropdown using the mat menu for selecting themes, making it convenient to switch between different themes. For this, we have written HTML code in the "app.component.html" as follows:
Step: 2 For improved project organization, we've created a distinct SCSS partial file named "_palette.scss" This file has been carefully crafted and is situated within the "mat-themes" folder to encompass themes and associated content.
We have included various color palettes in the "_palette.scss" file. For example, as shown below:
$indigo-palette: (50: #e8eaf6,100: #c5cae9,200: #9fa8da,300: #7986cb,// ... continues to 900contrast: (50:rgba(black,0.87),100:rgba(black,0.87),200:rgba(black,0.87),300:white,// ... continues to 900 ));
We will delve further into this section in the upcoming custom theme page.
Step: 3 In the "app-themes" section, we have integrated several themes named "deeppurple-amber," "indigo-pink," "pink-bluegrey," and "purple-green," as depicted below.
// Below codes should only be included ONCE in your application.// Add your desired themes to this map.$app-themes: (deeppurple-amber: (primary-base: mat.$deep-purple-palette,accent-base: mat.$amber-palette,success: mat.$green-palette,is-dark: false,foreground: mat.$light-theme-foreground-palette,background: mat.$light-theme-background-palette,typography: mat.define-typography-config( $font-family: "Arimo", ), ),indigo-pink: (primary-base: mat.$indigo-palette,accent-base: mat.$pink-palette,success: mat.$green-palette,is-dark: false,foreground: mat.$light-theme-foreground-palette,background: mat.$light-theme-background-palette,typography: mat.define-typography-config( $font-family: "Cabin", ), ),pink-bluegrey: (primary-base: mat.$pink-palette,accent-base: $sbmat-lemongreen,success: mat.$red-palette,is-dark: true,foreground: mat.$dark-theme-foreground-palette,background: mat.$dark-theme-background-palette,typography: mat.define-typography-config( $font-family: "Poppins", ), ),purple-green: (primary-base: mat.$purple-palette,accent-base: mat.$green-palette,success: mat.$green-palette,is-dark: true,foreground: mat.$dark-theme-foreground-palette,background: mat.$dark-theme-background-palette,typography: mat.define-typography-config( $font-family: "Quicksand", ), ),);
Step: 4 To achieve this, we have included the following code in the "app.component.ts" file.
Once we've established a mixin named "app-component-theme" within "app.component.scss," we've allocated background and foreground colors from the palette as demonstrated below:
@mixinapp-component-theme($theme) {// Get the color config from the theme. $color-config:mat.get-color-config($theme);// Get the primary color palette from the color-config. $background:map.get($color-config,'background'); $foreground:map.get($color-config,'foreground');}
We should include the previously created mixin in "themes.scss" which is located inside "app-themes".
@mixinapp-themes($theme) {/* all */@includeapp-component-theme($theme); }
After crafting themes for both light and dark modes and incorporating the "app-themes" section within each of them.
After completing all the above steps, the Theme Selector will appear on our website, enabling us to switch between themes effortlessly, as depicted below.
For more in-depth information on theming and customization options, covering aspects like palettes and best practices for theming, please visit the following links: