Skip to content

Commit fe2eece

Browse files
committed
docs(material/theming): Add docs the new base theming dimension
1 parent d5dd878 commit fe2eece

File tree

1 file changed

+129
-32
lines changed

1 file changed

+129
-32
lines changed

guides/theming.md

Lines changed: 129 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,14 @@ $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`.
176178

177179
Apply the styles for each of the components used in your application by including each of their
178180
theme Sass mixins.
@@ -205,10 +207,11 @@ $my-theme: mat.define-light-theme((
205207
```
206208

207209
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.
210+
Sass mixins that includes styles for all components in the library: `all-component-bases`,
211+
`all-component-colors`, `all-component-typographies`, `all-component-densities`, and
212+
`all-component-themes`. These mixins behave the same as individual component mixins, except they
213+
emit styles for `core-theme` and _all_ 35+ components in Angular Material. Unless your application
214+
uses every single component, this will produce unnecessary CSS.
212215

213216
```scss
214217
@use '@angular/material' as mat;
@@ -235,6 +238,95 @@ your project's `angular.json` file][adding-styles].
235238

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

241+
#### Theming dimensions
242+
243+
Angular Material themes are divided along several dimensions:
244+
245+
Base
246+
: Common base styles for the design system. These styles don't change based on your configured
247+
colors, typography, or density, so they only need to be included once per application. These
248+
styles may include things such as border-radius, border-width, etc. All components have a base
249+
mixin that can be used to include its base styles. (For example,
250+
`@include mat.checkbox-base($theme)`)
251+
252+
Color
253+
: Styles related to the colors in your application. These style should be included at least once in
254+
your application. Depending on your needs, you may need to include these styles multiple times
255+
with different configurations. (For example, if your app supports light and dark theme colors.)
256+
All components have a color mixin that can be used to include its color styles. (For example,
257+
`@include mat.checkbox-color($theme)`)
258+
259+
Typography
260+
: Styles related to the fonts used in your application, including the font family, size, weight,
261+
line-height, and letter-spacing. These style should be included at least once in your application.
262+
Depending on your needs, you may need to include these styles multiple times with different
263+
configurations. (For example, if your app supports reading content in either a serif or sans-serif
264+
font.) All components have a typography mixin that can be used to include its typography
265+
styles. (For example, `@include mat.checkbox-typography($theme)`)
266+
267+
Density
268+
: Styles related to the size and spacing of elements in your application. These style should be
269+
included at least once in your application. Depending on your needs, you may need to include these
270+
styles multiple times with different configurations. (For example, if your app supports both a
271+
normal and compact mode). All components have a density mixin that can be used to include its
272+
density styles. (For example, `@include mat.checkbox-density($theme)`)
273+
274+
All components also support a theme mixin that can be used to include the component's styles for all
275+
theme dimensions at once. (For example, `@include mat.checkbox-theme($theme)`).
276+
277+
If you need to include [multiple themes in a single file](defining-multiple-themes), the recommended
278+
approach to structuring the file is to include the full theme mixin for any components used in your
279+
app at the root, and then have scoped overrides of individual theme dimensions as needed. For
280+
example:
281+
282+
```scss
283+
@use '@angular/material' as mat;
284+
285+
// Default theme.
286+
$base-theme: mat.define-dark-theme((
287+
color: (
288+
primary: mat.define-palette(mat.$indigo-palette),
289+
accent: mat.define-palette(mat.$pink-palette),
290+
),
291+
typography: mat.define-typography-config(),
292+
density: 0,
293+
));
294+
295+
// Light mode colors theme.
296+
$light-theme: mat.define-light-theme((
297+
color: (
298+
primary: mat.define-palette(mat.$pink-palette),
299+
accent: mat.define-palette(mat.$blue-grey-palette),
300+
),
301+
));
302+
303+
// Compact density theme.
304+
$compact-theme: mat.define-dark-theme((
305+
density: -2,
306+
));
307+
308+
@include mat.core();
309+
310+
// Setup the default settings for all theming dimensions.
311+
@include mat.core-theme($theme);
312+
@include mat.button-theme($theme);
313+
@include mat.checkbox-theme($theme);
314+
315+
// Use light theme colors for users that prefer them.
316+
@media (prefers-color-scheme: light) {
317+
@include mat.core-color($theme);
318+
@include mat.button-color($light-theme);
319+
@include mat.checkbox-color($light-theme);
320+
}
321+
322+
// Use more compact density when the app is in compact mode.
323+
.compact-mode {
324+
@include mat.core-density($theme);
325+
@include mat.button-density($compact-theme);
326+
@include mat.checkbox-density($compact-theme);
327+
}
328+
```
329+
238330
### Using a pre-built theme
239331

240332
Angular Material includes four pre-built theme CSS files, each with different palettes selected.
@@ -327,7 +419,8 @@ file. The approach for this loading depends on your application.
327419
By default, Angular Material does not apply any styles to your DOM outside
328420
its own components. If you want to set your application's background color
329421
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
422+
1. Put your application's main content inside `mat-sidenav-container`, assuming you're using
423+
`MatSidenav`, or
331424
2. Apply the `mat-app-background` CSS class to your main content root element (typically `body`).
332425

333426
### Scoping style customizations
@@ -385,20 +478,22 @@ $my-palette: mat.define-palette(mat.$indigo-palette);
385478
## Customizing density
386479

387480
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_.
481+
[Material Design density guidelines][material-density]. This system defines a scale where zero
482+
represents the default density. You can decrement the number for _more density_ and increment the
483+
number for _less density_.
391484

392485
The density system is based on a *density scale*. The scale starts with the
393486
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.
487+
the affected sizes by `4px`, down to the minimum size necessary for a component to render
488+
coherently.
395489

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
490+
Components that appear in task-based or pop-up contexts, such as `MatDatepicker`, don't change their
491+
size via the density system. The [Material Design density guidance][material-density] explicitly
492+
discourages increasing density for such interactions because they don't compete for space in the
399493
application's layout.
400494

401-
You can apply custom density setting to the entire library or to individual components using their density Sass mixins.
495+
You can apply custom density setting to the entire library or to individual components using their
496+
density Sass mixins.
402497

403498
```scss
404499
// You can set a density setting in your theme to apply to all components.
@@ -414,6 +509,8 @@ $dark-theme: mat.define-dark-theme((
414509
}
415510
```
416511

512+
[material-density]: https://m2.material.io/design/layout/applying-density.html
513+
417514
## Strong focus indicators
418515

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

506603
## User preference media queries
507604

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.
605+
Angular Material does not apply styles based on user preference media queries, such as
606+
`prefers-color-scheme` or `prefers-contrast`. Instead, Angular Material's Sass mixins give you the
607+
flexibility to apply theme styles to based on the conditions that make the most sense for your
608+
users. This may mean using media queries directly or reading a saved user preference.
512609

513610
## Style customization outside the theming system
514611

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.
612+
Angular Material supports customizing color, typography, and density as outlined in this document.
613+
Angular strongly discourages, and does not directly support, overriding component CSS outside the
614+
theming APIs described above. Component DOM structure and CSS classes are considered private
615+
implementation details that may change at any time.

0 commit comments

Comments
 (0)