skip to content

In Angular Material 22, how do you give one region, such as a dense control-room console, its own density or palette, and what does it cost?

level: seniorimportance: should knowfreq 32%

answer

  1. call the mixin again, scoped
  2. custom properties inherit downward
  3. pass only the category that changes
  4. density 0 emits nothing
  5. DOM ancestry decides, not component tree

basics

~20 s

Include mat.theme again under the region's selector, passing only what changes, such as density: -2; the redeclared CSS variables inherit to the components inside. Each call re-emits that category's variable set, and a dialog attached outside the region misses it.

solid answer

~50 s

`mat.theme` only declares CSS variables on the selector it is included in, so a second call under `.console` with `(density: -2)` or `(color: mat.$cyan-palette)` overrides those variables for that subtree, and the colours and typography from `html` still inherit. Pass only the category that differs: each call re-emits the full variable set for every category you give it, plus the shape and state variables, though never component selectors. Density has sharp edges. A nested `density: 0` emits no variables, so it cannot undo an enclosing `-2`. From `-2` buttons drop their touch-target element and filled form fields hide their label, and each component clamps at its own minimum: buttons at `-3`, tables at `-4`, form fields and lists at `-5`. Anything rendered outside the region, such as a dialog the CDK attaches to its overlay container, does not inherit the scoped values.

code

scss · 21 lines
scss
@use '@angular/material' as mat;
@use './theme-colors' as transit;

html {
  color-scheme: light dark;
  @include mat.theme((
    color: (primary: transit.$primary-palette),
    typography: Roboto,
    density: 0,
  ));
}

// Control-room console: denser controls, same colours and fonts.
.control-room-console {
  @include mat.theme((density: -2));
}

// A dispatch alert strip with its own palette, inheriting density.
.dispatch-alert {
  @include mat.theme((color: mat.$orange-palette));
}

go deeper

for a junior

Recall that mat.theme can be included again under a class to re-theme only that part of the page.

for a middle

Explain that scoping works through inherited CSS variables, so a scoped call can pass just density or just color and inherit the rest.

for a senior

Show the failure modes: density 0 cannot reset, anything rendered outside the region in the DOM escapes it, and compact density removes touch targets and filled labels.

for a principal

Decide when a variant earns its own scope versus a separate theme build, weighing CSS size, accessibility on touch hardware and who maintains each variant.

## Scoping with a second call Every value `mat.theme` produces is a **CSS custom property** declared on the selector that includes it. Custom properties **inherit**, so components read the nearest declaration up their DOM ancestry. A second call under a narrower selector therefore re-themes only that subtree: ```scss html { @include mat.theme((color: transit.$primary-palette, typography: Roboto, density: 0)); } .control-room-console { @include mat.theme((density: -2)); } ``` The transit operator's control-room console gets compact buttons, form fields, tables and lists. The journey planner around it keeps standard spacing, and the console still inherits the brand colours and fonts from `html` because the second call passed no `color` or `typography`. The theming guide shows the same pattern for colour: a warning panel scoped with `(color: mat.$cyan-palette)`. ## What each call emits | Category passed | Variables emitted on the selector | |---|---| | `color` | every system colour (`--mat-sys-primary`, `--mat-sys-surface` ...) plus the elevation variables | | `typography` | the typescale variables (fonts, sizes, weights, line heights) | | `density` below 0 | component-level density variables (heights, paddings, touch-target display) | | `density: 0` | nothing | | (any call) | the shape and state variables | So the cost of a scoped theme is **one more block of declarations per selector**, sized by the categories you pass. That is far cheaper than an M2 theme, where each scoped `all-component-themes` repeated styles for every component. But it is not free: ten scoped colour themes mean ten full copies of the colour set. Keep scopes few and pass only what changes. ## Colour and typography scopes A scoped **colour** call works the same way. `.dispatch-alert { @include mat.theme((color: mat.$orange-palette)); }` redeclares every system colour inside the alert strip, so its buttons, chips and progress bars take orange tones while density and fonts still come from the ancestors. Because a single-palette value is written as `light-dark()` pairs, the strip follows the same inherited `color-scheme` as the rest of the app and needs no mode handling of its own. A scoped **typography** call is rarer, for example a signage-style board that uses a different brand family for headings, and it redeclares the whole typescale for that subtree. To check what a scope really changed, inspect an element inside it in the browser's dev tools: the styles panel shows each custom property together with the rule that declared it, which shows at once whether the value came from `html` or from the scoped selector. ## Density's sharp edges - **No reset by zero.** `density: 0` emits no variables. A nested `.normal { @include mat.theme((density: 0)); }` inside the console leaves the inherited `-2` values in place. Keep standard-density areas outside the dense scope instead of trying to undo it. - **Per-component floors.** Each component clamps to its own minimum: the button's sizes stop at `-3` (container height 40px down to 28px), the table's at `-4`, the form field's and list's at `-5`. - **Accessibility trade-offs.** From `-2` the buttons stop rendering their touch-target element, and filled form fields no longer show a floating label. The guide warns that density below 0 can make navigation harder. A mouse-and-keyboard control room may accept that; a touch kiosk should not. - **Pop-ups are excluded.** Density does not change surfaces such as the datepicker, following the Material Design guidance for task-based pop-ups. ## What escapes the scope 1. **Globally positioned overlays.** A dialog is attached by the CDK to its overlay container at the end of the document, not inside `.control-room-console`, so it shows the app's density and palette. Include the same scoped mixin for a class you put on the dialog's panel (the dialog configuration's job) if it must match. 2. **Content projected elsewhere.** Anything that ends up outside the region in the DOM, whatever its place in the component tree, follows its DOM ancestors. 3. **Hard-coded values.** Custom styles that wrote colours or sizes directly instead of reading the system variables do not follow any theme. ## Keeping it lean 1. Pass one category per scoped call whenever you can. 2. Prefer one scope per real variant (the console) over per-component calls. 3. Check the compiled CSS size after adding a scope that includes `color`. A scope is the right tool when the variant lives **inside** the same page as the default, as the console does inside the operator's web app. When a whole deployment differs, for example a wall-display build that is always dense and always dark, a separate theme file compiled into that build is simpler than a scope that wraps the entire app.

  • The console will also run on a touch-screen wall display. Which effects of density -2 do you check first?
    From `-2` Material's buttons stop rendering their touch-target element and shrink to a 32px container, and filled form fields drop their floating label, so taps get harder and fields lose a visible label. On touch hardware try `-1`, or keep the dense scope for the mouse-driven console only.
  • Why not reach compact spacing by overriding padding on each Material component in the console?
    Component DOM and classes are private and may change; the density variables are the supported contract, and one scoped call covers every component that supports density with tested per-component floors.
  • Would a scoped palette that sets only color need its own color-scheme declaration?
    No. Its variables are `light-dark()` pairs like the app's, and `color-scheme` inherits from the ancestors, so the region switches modes together with the rest of the app.

  • A nested density: 0 call restores standard spacing inside a dense region
  • A scoped mat.theme call must repeat color and typography
  • Scoped calls emit component selectors for every Material component
  • Scoped density also shrinks the datepicker popup
  • Density keeps shrinking every component down to -5

Assumes Angular Material 22's mat.theme API (v19 onward), where a scoped call emits CSS variables only and a density of 0 emits none.