Colors in M3 Theme
In Angular Material, the colors are defined using the Material Design 3 (M3) theme. The M3 theme is mainly generated using a single color, called the primary color. So, ideally you get only one color (and it’s tones) from the theme.
Tailwind Like Colors
Tailwind provides default color palettes, and you can use them easily like bg-blue-500, border-blue-900 or text-blue-200.
Let’s see how we can achieve this in Angular Material. Our goal is to generate classes from blue-50 to blue-950. Each single class will take care of background and foreground colors.
1. Blue color palette
Let’s take below palette as an example:
$blue-palette: ( 50: #eff6ff, 100: #dbeafe, 200: #bfdbfe, 300: #93c5fd, 400: #60a5fa, 500: #3b82f6, 600: #2563eb, 700: #1d4ed8, 800: #1e40af, 900: #1e3a8a, 950: #172554,);2. Add contrast palette
$blue-palette: ( 50: #eff6ff, 100: #dbeafe, 200: #bfdbfe, 300: #93c5fd, 400: #60a5fa, 500: #3b82f6, 600: #2563eb, 700: #1d4ed8, 800: #1e40af, 900: #1e3a8a, 950: #172554, contrast: ( 50: #000000, 100: #000000, 200: #000000, 300: #000000, 400: #000000, 500: #ffffff, 600: #ffffff, 700: #ffffff, 800: #ffffff, 900: #ffffff, 950: #ffffff, ),);3. Create palette barrel file
Create a file at src/styles/palettes/_index.scss and export each palette as theme:
@use "sass:map";@use "@angular/material" as mat;
$blue-palette: (/* created in previous step */);
@each $color in (50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950) { .blue-#{$color} { @include mat.theme-overrides( ( primary: map.get($blue-palette, $color), on-primary: map.get($blue-palette, "contrast", $color), ) ); }}Note that as we are only targeting buttons, we are only overriding primary and on-primary colors here. You can override other system variables as needed and refer to below table for default variants for components:
| Component | Default color variant |
|---|---|
| Badge | error |
| Button | primary |
| Button-toggle | secondary |
| Checkbox | primary |
| Chips | secondary |
| Datepicker | primary |
| Fab | primary |
| Form-field | primary |
| Icon | surface |
| Option | secondary |
| Progress-bar | primary |
| Progress-spinner | primary |
| Pseudo-checkbox | primary |
| Radio | primary |
| Select | primary |
| Slide-toggle | primary |
| Slider | primary |
| Stepper | primary |
| Tabs | primary |