Skip to content

Commit e2ac195

Browse files
mmalerbajelbourn
andauthored
docs(material/theming): Add docs for the new base theming dimension (#27992)
Co-authored-by: Jeremy Elbourn <[email protected]>
1 parent 17ea8e7 commit e2ac195

File tree

1 file changed

+81
-32
lines changed

1 file changed

+81
-32
lines changed

guides/theming.md

Lines changed: 81 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## What is theming?
44

5-
Angular Material's theming system lets you customize color, typography, and density styles for components
6-
in your application. The theming system is based on Google's
5+
Angular Material's theming system lets you customize base, color, typography, and density styles for
6+
components in your application. The theming system is based on Google's
77
[Material Design][material-design-theming] specification.
88

99
This document describes the concepts and APIs for customizing colors. For typography customization,
@@ -86,8 +86,8 @@ $my-palette: mat.$indigo-palette;
8686

8787
## Themes
8888

89-
A **theme** is a collection of color, typography, and density options. Each theme includes three palettes that
90-
determine component colors:
89+
A **theme** is a collection of color, typography, and density options. Each theme includes three
90+
palettes that determine component colors:
9191

9292
* A **primary** palette for the color that appears most frequently throughout your application
9393
* An **accent**, or _secondary_, palette used to selectively highlight key parts of your UI
@@ -117,7 +117,8 @@ times will result in duplicate CSS in your application.
117117
#### Defining a theme
118118

119119
Angular Material represents a theme as a Sass map that contains your color, typography, and density
120-
choices. See [Angular Material Typography][mat-typography] for an in-depth guide to customizing typography. See
120+
choices, as well as some base design system settings. See
121+
[Angular Material Typography][mat-typography] for an in-depth guide to customizing typography. See
121122
[Customizing density](#customizing-density) below for details on adjusting component density.
122123

123124
Constructing the theme first requires defining your primary and accent palettes, with an optional
@@ -166,13 +167,15 @@ $my-theme: mat.define-light-theme((
166167
The `core-theme` Sass mixin emits prerequisite styles for common features used by multiple
167168
components, such as ripples. This mixin must be included once per theme.
168169

169-
Each Angular Material component has a mixin for each color , typography, and density. For example, `MatButton` declares
170-
`button-color`, `button-typography`, and `button-density`. Each mixin emits only the styles corresponding to that
171-
area of customization.
170+
Each Angular Material component has a mixin for each [theming dimension](#theming-dimensions): base,
171+
color, typography, and density. For example, `MatButton` declares `button-base`, `button-color`,
172+
`button-typography`, and `button-density`. Each mixin emits only the styles corresponding to that
173+
dimension of customization.
172174

173-
Additionally, each component has a "theme" mixin that emits all styles that depend on the theme config.
174-
This theme mixin only emits color, typography, or density styles if you provided a corresponding
175-
configuration to `define-light-theme` or `define-dark-theme`.
175+
Additionally, each component has a "theme" mixin that emits all styles that depend on the theme
176+
config. This theme mixin only emits color, typography, or density styles if you provided a
177+
corresponding configuration to `define-light-theme` or `define-dark-theme`, and it always emits the
178+
base styles.
176179

177180
Apply the styles for each of the components used in your application by including each of their
178181
theme Sass mixins.
@@ -205,10 +208,11 @@ $my-theme: mat.define-light-theme((
205208
```
206209

207210
As an alternative to listing every component that your application uses, Angular Material offers
208-
Sass mixins that includes styles for all components in the library: `all-component-colors`,
209-
`all-component-typographies`, `all-component-densities`, and `all-component-themes`. These mixins behave the same as
210-
individual component mixins, except they emit styles for `core-theme` and _all_ 35+ components in Angular
211-
Material. Unless your application uses every single component, this will produce unnecessary CSS.
211+
Sass mixins that includes styles for all components in the library: `all-component-bases`,
212+
`all-component-colors`, `all-component-typographies`, `all-component-densities`, and
213+
`all-component-themes`. These mixins behave the same as individual component mixins, except they
214+
emit styles for `core-theme` and _all_ 35+ components in Angular Material. Unless your application
215+
uses every single component, this will produce unnecessary CSS.
212216

213217
```scss
214218
@use '@angular/material' as mat;
@@ -235,6 +239,46 @@ your project's `angular.json` file][adding-styles].
235239

236240
[adding-styles]: https://angular.io/guide/workspace-config#styles-and-scripts-configuration
237241

242+
#### Theming dimensions
243+
244+
Angular Material themes are divided along several dimensions:
245+
246+
Base
247+
: Common base styles for the design system. These styles don't change based on your configured
248+
colors, typography, or density, so they only need to be included once per application. These
249+
mixins include structural styles such as border-radius, border-width, etc. All components have a base
250+
mixin that can be used to include its base styles. (For example,
251+
`@include mat.checkbox-base($theme)`)
252+
253+
Color
254+
: Styles related to the colors in your application. These style should be included at least once in
255+
your application. Depending on your needs, you may need to include these styles multiple times
256+
with different configurations. (For example, if your app supports light and dark theme colors.)
257+
All components have a color mixin that can be used to include its color styles. (For example,
258+
`@include mat.checkbox-color($theme)`)
259+
260+
Typography
261+
: Styles related to the fonts used in your application, including the font family, size, weight,
262+
line-height, and letter-spacing. These style should be included at least once in your application.
263+
Depending on your needs, you may need to include these styles multiple times with different
264+
configurations. (For example, if your app supports reading content in either a serif or sans-serif
265+
font.) All components have a typography mixin that can be used to include its typography
266+
styles. (For example, `@include mat.checkbox-typography($theme)`)
267+
268+
Density
269+
: Styles related to the size and spacing of elements in your application. These style should be
270+
included at least once in your application. Depending on your needs, you may need to include these
271+
styles multiple times with different configurations. (For example, if your app supports both a
272+
normal and compact mode). All components have a density mixin that can be used to include its
273+
density styles. (For example, `@include mat.checkbox-density($theme)`)
274+
275+
All components also support a theme mixin that can be used to include the component's styles for all
276+
theme dimensions at once. (For example, `@include mat.checkbox-theme($theme)`).
277+
278+
The recommended approach is to rely on the `theme` mixins to lay down your base styles, and if
279+
needed us the single dimension mixins to override particular aspects for parts of your app (see the
280+
section on [Multiple themes in one file](#multiple-themes-in-one-file).)
281+
238282
### Using a pre-built theme
239283

240284
Angular Material includes four pre-built theme CSS files, each with different palettes selected.
@@ -327,7 +371,8 @@ file. The approach for this loading depends on your application.
327371
By default, Angular Material does not apply any styles to your DOM outside
328372
its own components. If you want to set your application's background color
329373
to match the components' theme, you can either:
330-
1. Put your application's main content inside `mat-sidenav-container`, assuming you're using `MatSidenav`, or
374+
1. Put your application's main content inside `mat-sidenav-container`, assuming you're using
375+
`MatSidenav`, or
331376
2. Apply the `mat-app-background` CSS class to your main content root element (typically `body`).
332377

333378
### Scoping style customizations
@@ -385,20 +430,22 @@ $my-palette: mat.define-palette(mat.$indigo-palette);
385430
## Customizing density
386431

387432
Angular Material's density customization is based on the
388-
[Material Design density guidelines](https://m2.material.io/design/layout/applying-density.html). This system
389-
defines a scale where zero represents the default density. You can decrement the number for _more density_ and increment
390-
the number for _less density_.
433+
[Material Design density guidelines][material-density]. This system defines a scale where zero
434+
represents the default density. You can decrement the number for _more density_ and increment the
435+
number for _less density_.
391436

392437
The density system is based on a *density scale*. The scale starts with the
393438
default density of `0`. Each whole number step down (`-1`, `-2`, etc.) reduces
394-
the affected sizes by `4px`, down to the minimum size necessary for a component to render coherently.
439+
the affected sizes by `4px`, down to the minimum size necessary for a component to render
440+
coherently.
395441

396-
Components that appear in task-based or pop-up contexts, such as `MatDatepicker`, don't change their size via the
397-
density system. The [Material Design density guidance](https://m2.material.io/design/layout/applying-density.html)
398-
explicitly discourages increasing density for such interactions because they don't compete for space in the
442+
Components that appear in task-based or pop-up contexts, such as `MatDatepicker`, don't change their
443+
size via the density system. The [Material Design density guidance][material-density] explicitly
444+
discourages increasing density for such interactions because they don't compete for space in the
399445
application's layout.
400446

401-
You can apply custom density setting to the entire library or to individual components using their density Sass mixins.
447+
You can apply custom density setting to the entire library or to individual components using their
448+
density Sass mixins.
402449

403450
```scss
404451
// You can set a density setting in your theme to apply to all components.
@@ -414,6 +461,8 @@ $dark-theme: mat.define-dark-theme((
414461
}
415462
```
416463

464+
[material-density]: https://m2.material.io/design/layout/applying-density.html
465+
417466
## Strong focus indicators
418467

419468
By default, most components indicate browser focus by changing their background color as described
@@ -505,14 +554,14 @@ the CSS in each shadow root, or by using [Constructable Stylesheets][constructab
505554

506555
## User preference media queries
507556

508-
Angular Material does not apply styles based on user preference media queries, such as `prefers-color-scheme`
509-
or `prefers-contrast`. Instead, Angular Material's Sass mixins give you the flexibility to
510-
apply theme styles to based on the conditions that make the most sense for your users. This may mean using media
511-
queries directly or reading a saved user preference.
557+
Angular Material does not apply styles based on user preference media queries, such as
558+
`prefers-color-scheme` or `prefers-contrast`. Instead, Angular Material's Sass mixins give you the
559+
flexibility to apply theme styles to based on the conditions that make the most sense for your
560+
users. This may mean using media queries directly or reading a saved user preference.
512561

513562
## Style customization outside the theming system
514563

515-
Angular Material supports customizing color, typography, and density as outlined in this document. Angular
516-
strongly discourages, and does not directly support, overriding component CSS outside the theming
517-
APIs described above. Component DOM structure and CSS classes are considered private implementation
518-
details that may change at any time.
564+
Angular Material supports customizing color, typography, and density as outlined in this document.
565+
Angular strongly discourages, and does not directly support, overriding component CSS outside the
566+
theming APIs described above. Component DOM structure and CSS classes are considered private
567+
implementation details that may change at any time.

0 commit comments

Comments
 (0)