skip to content

When moving an Angular Material app from an M2 theme built with `mat.m2-define-light-theme` to Material 3 in v22, what must change?

level: seniorimportance: should knowfreq 30%

answer

  1. no automated M2-to-M3 migration
  2. the m2- prefix came in v18
  3. one mat.theme replaces component mixins
  4. color variants and typography classes
  5. get-theme-version during the transition

basics

~20 s

No automated migration exists: replace the m2-define-* config and component theme mixins with one mat.theme under html, then handle what M3 drops, chiefly the color input's primary, accent and warn variants, M2 typography class names and custom mixins reading the M2 config.

solid answer

~40 s

`ng update` to v18 only renamed the M2 API (`define-light-theme` to `m2-define-light-theme`, `$indigo-palette` to `$m2-indigo-palette`); Angular Material offers no M2-to-M3 migration because the design result is subjective. The move is manual. Replace the `m2-define-palette` and `m2-define-light-theme` config and the `core-theme` / `all-component-themes` includes with one `mat.theme` inside `html`, choosing M3 tonal palettes or generating one. Then fix what M3 does not carry over: the `color` input's `primary` / `accent` / `warn` variants have no effect (`color-variants-backwards-compatibility` with a `mat.define-theme` object restores them at a CSS cost), `typography-hierarchy` now emits M3 class names such as `.mat-headline-large` (`$back-compat: true` adds the old ones), and your own theme mixins can branch on `mat.get-theme-version` (0 for M2, 1 for M3) until the switch is complete.

code

scss · 10 lines
scss
@use '@angular/material' as mat;

// A shared mixin that works for both theme versions during the migration.
@mixin route-badge-theme($theme) {
  @if (mat.get-theme-version($theme) == 1) {
    .route-badge { background: mat.get-theme-color($theme, primary-container); }
  } @else {
    .route-badge { background: mat.get-theme-color($theme, primary, 100); }
  }
}

go deeper

for a junior

Recall that M2 and M3 are two designs, that m2- names mark the older one, and that mat.theme is the current API.

for a middle

Explain how M2 hue palettes and accent/warn roles differ from M3 tonal palettes and the primary, secondary, tertiary and error roles.

for a senior

Show a staged plan: bilingual custom mixins with get-theme-version, compatibility helpers chosen deliberately, and a visual review nothing automates.

for a principal

Set the policy for many apps: a deadline for compatibility helpers, a shared M3 theme package and acceptance that the redesign is product work, not a refactor.

## Where the names came from | Release | What changed | |---|---| | 18.0 | M2 Sass API renamed with an `m2-` prefix (`m2-define-light-theme`, `m2-define-palette`, `$m2-indigo-palette` ...); `ng update` rewrote existing code automatically | | 19.0 | the `mat.theme` mixin became the recommended M3 API; `ng add` and the prebuilt M3 themes switched to it | | 20.2 | `mat.m2-theme` added: it emits `--mat-sys-*` variables from an M2 config so custom components can read them | | 21.0 | the M3 utility classes were renamed to `mat.system-classes()` | So code with `m2-` names is already *on* v18 or later; it simply still uses Material 2 design. The rename was mechanical. The redesign is not, and the M2 guide says plainly that **no automated M2-to-M3 migration** is offered. ## Before and after An M2 theme builds a **config object** and feeds it to component mixins: ```scss $primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); $theme: mat.m2-define-light-theme(( color: (primary: $primary, accent: $accent), typography: mat.m2-define-typography-config(), density: 0, )); @include mat.core-theme($theme); @include mat.all-component-themes($theme); ``` The M3 replacement is one include that emits CSS variables: ```scss html { @include mat.theme((color: mat.$violet-palette, typography: Roboto, density: 0)); } ``` ## What M3 does differently - **Palettes.** M2 palettes are hue maps (50 to 900, A100 to A700) with contrast colours. M3 palettes are **tonal** (tones 0 to 100) with nested secondary, neutral and error palettes. M2 palettes cannot be passed to `mat.theme`; pick one of the twelve M3 palettes or generate one with `ng generate @angular/material:theme-color`. - **Roles.** M2 had primary, accent and warn. M3 has primary, secondary, tertiary and error, and `accent` maps roughly to tertiary. - **The `color` input.** On components such as buttons, `color="primary" | "accent" | "warn"` is supported in M2 themes only and has no effect in M3. - **Selectors.** M3 expects every `-theme`, `-color`, `-typography`, `-density` or `-base` include to sit inside a selector; wrap root-level includes in `html { }`. - **Typography classes.** `all-component-typographies` no longer emits `typography-hierarchy` styles for M3, and when you call `typography-hierarchy` it emits M3 names (`.mat-headline-large`) instead of M2 ones (`.mat-headline-1`). - **Dead calls.** `@include mat.core()` is a deprecated no-op; delete it. - **Unsupported options.** The `backgroundColor` input of `mat-tab-group` and the `appearance="legacy"` variant of `mat-button-toggle` are not supported with M3 themes. - **Light and dark.** An M2 app usually built two configs, `m2-define-light-theme` and `m2-define-dark-theme`, and re-included the colour mixins under a class or media query. An M3 `mat.theme` writes `light-dark()` pairs by default, so one include plus the CSS `color-scheme` property replaces both; `theme-type: light` or `dark` remains for a single fixed mode. ## Mapping the old pieces to the new | M2 piece | M3 replacement | |---|---| | `m2-define-palette(mat.$m2-indigo-palette, 500)` | a prebuilt tonal palette (`mat.$azure-palette`) or one generated by the theme-color schematic | | `primary` / `accent` / `warn` | primary / tertiary (roughly) / error, with secondary derived from the primary palette | | `m2-define-typography-config()` | `typography: Roboto` or a map with `plain-family` and `brand-family` | | `density: 0` in the config | `density: 0` in `mat.theme`, same 0 to -5 range | | `core-theme` + `all-component-themes` | the single `mat.theme` include | | `indigo-pink.css` and the other M2 prebuilt files | one of the four M3 prebuilt files, or your own `mat.theme` | ## A staged migration 1. **Inventory** every `m2-*` call, every component `-theme` include, every `color=` binding and every M2 typography class. 2. **Make custom theme mixins bilingual**: branch on `mat.get-theme-version($theme)` (0 for M2, 1 for M3) so they work under both during the transition. 3. **Swap the global theme** for `mat.theme` inside `html`, with palettes generated from the brand colour if no prebuilt one fits. 4. **Restore or remove compatibility**: either bind colours the M3 way and drop `color=`, or include `color-variants-backwards-compatibility($theme)` with a theme from `mat.define-theme`, accepting the extra CSS; likewise `typography-hierarchy($theme, $back-compat: true)` for old class names. 5. **Review visually**: M3 changes shapes, elevation and component appearance, and that is the part no tool decides for you. ## Bridges and their cost - `mat.m2-theme($config)` lets you style **your own** components with `--mat-sys-*` variables while still on M2. It does not replace the component theme mixins. - The four M2 prebuilt CSS files (`indigo-pink.css` ...) remain for backwards compatibility; the guide says they will be removed in a future version. - Every compatibility helper adds CSS and keeps M2 thinking alive. Treat each as a dated step, not a destination.

  • After the switch, filled buttons that used color="warn" are no longer red. What are the two ways out?
    Preferably, drop the `color` binding and style the variant the M3 way, through the button's overrides API or a scoped palette. As a stopgap, include `mat.color-variants-backwards-compatibility($theme)` with a theme object from `mat.define-theme`, which regenerates `.mat-primary`, `.mat-accent` and `.mat-warn` styles at the cost of extra CSS.
  • What does mat.m2-theme give a team that cannot move to M3 this quarter?
    It emits `--mat-sys-*` variables from the existing M2 config, so new custom components can be written against system variables now. The Material components themselves still need the M2 component theme mixins until the real switch.
  • Why is there no automated M2-to-M3 migration when the v18 rename had one?
    The rename was mechanical: the same API under new names. Moving to M3 changes palettes, colour roles, shapes and component appearance, which are design decisions the guide says only the app team can make.

  • ng update converts an M2 theme to mat.theme automatically
  • m2- prefixed functions mean the app is on an old Angular Material major
  • M2 palettes such as mat.$m2-indigo-palette can be passed to mat.theme
  • color="accent" keeps working unchanged under an M3 theme
  • mat.m2-theme replaces the component theme mixins for Material components

Assumes Angular Material 22; the m2- names date from v18, mat.theme from v19 and mat.m2-theme from v20.2.