Custom themes

As we previously discussed, Material ships with some default themes like deeppurple-amber, indigo-pink, pink-bluegrey, and purple-green. However, your company or product may have its own color scheme. For this, you can create a custom theme that change the look of your application.

In order to create a new theme, you must implement a new scss file:

  1. Create a new file under src called localcast-theme.scss
  2. Material theme guide, located at https://material.angular.io/guide/theming, includes an up-to-date starting file. I'll break down the contents of the file further
  3. Start by including the base theming library:
src/localcast-theme.scss
@import '~@angular/material/theming';
  1. Import the mat-core() mixin, which includes all common styles used by various Material components:
src/localcast-theme.scss
@include mat-core();
mat-core() should only be included once in your application; otherwise, you'll introduce unnecessary and duplicated css payload in your application.

mat-core() contains the necessary scss functions to be able to inject your custom colors into Material, such as mat-palette, mat-light-theme, and mat-dark-theme.

At a minimum, we must define a new primary and an accent color. Defining new colors, however, is not a straightforward process. Material requires a palette to be defined, mat-palette, which needs to be seeded by a complicated color object that can't just be overridden by a simple hex value such as #BFB900.

To pick your colors, you may use the Material Design Color Tool, located at https://material.io/color. Here's a screenshot of the tool:

Material.io Color Tool
  1. Using Material Palette, select a Primary and a Secondary color:
    • My primary selection is red with a hue value of 500
    • My secondary selection is indigo with a hue value of A400
  2. Observe how your selections would apply to a material design app by going through the 6 prebuilt screen on the left
  3. Evaluate the accessibility implications of your selections, as shown:
Material.io Color Tool Accessibility tab
The tool is warning us that our selections result in ineligible text, when white text is used over the primary color. You should either take care to avoid displaying white text over your primary color or change your selection.

The interface for mat-palette looks like this:

mat-palette($base-palette, $default: 500, $lighter: 100, $darker: 700)
  1. Define the primary and secondary mat-palette objects using the default hue from the tool:
src/localcast-theme.scss
$localcast-primary: mat-palette($mat-red, 500);
$localcast-accent: mat-palette($mat-indigo, A400);
  1. Create a new theme and apply it:
src/localcast-theme.scss
$localcast-app-theme: mat-light-theme($localcast-primary, $localcast-accent);

@include angular-material-theme($localcast-app-theme);
  1. In angular.json, locate the apps.styles attribute
  2. Prepend the list with localcast-theme.scss while removing the styles.input attribute
angular.json
...
"styles": [
"src/localcast-theme.scss",
"src/styles.css"
],
...
Even though your theme is in scss, you may continue using css in the rest of your application. Angular CLI supports compiling both scss and css. If you would like to change the default behavior, you may switch to scss altogether by changing the defaults.styleExt property in the angular.json file from css to scss.

You may also choose to eliminate styles.css and merge its contents with localcast-theme.scss or convert styles.css to a sass file by simply renaming it to styles.scss. If you do this, don't forget to update angular.json.

Your application should now look like this:

LocalCast Weather with custom theme

We can now move the UX task to the done column:

Waffle.io Kanban Board Status
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.72.15