|
| 1 | +This is an experimental theming API based on [design tokens](https://m3.material.io/foundations/design-tokens/how-to-use-tokens). It is currently in the prototype phase, |
| 2 | +and still being evaluated. |
| 3 | + |
| 4 | +## Design tokens |
| 5 | +- Design tokens are a set of variables that determine what components look like. They can affect things like color, typography, desnity, elevation, border radius, and more. |
| 6 | +- Angular Material represents design tokens as CSS variables |
| 7 | + |
| 8 | +## M2 vs M3 tokens |
| 9 | +- Angular Material can use tokens corresponding to either the [Material Design 2](https://m2.material.io/) or [Material Design 3](https://m3.material.io/) spec |
| 10 | + - Token values for M2 can be obtained by: |
| 11 | + 1. Generating them from an Angular Material theme object (e.g. one defined with `mat.define-light-theme`). To generate M2 tokens for a theme, pass it to the `mat.m2-tokens-from-theme` function. |
| 12 | + - Token values for M3 are not yet available |
| 13 | + |
| 14 | +Example: |
| 15 | +```scss |
| 16 | +// Create an Angular Material theme. |
| 17 | +$my-theme: mat.define-light-theme(...); |
| 18 | + |
| 19 | +// Create tokens for M2 from the theme. |
| 20 | +$m2-tokens: mat.m2-tokens-from-theme($my-theme); |
| 21 | +``` |
| 22 | +## Component theme configuration functions |
| 23 | +- These functions are used to specify which tokens should be applied by the theming mixins _and_ to customize the tokens used in that component to something other than the value from the token set |
| 24 | +- So far the following component theme configuration functions have been implements: |
| 25 | + - `matx.checkbox` configures tokens for the mat-checkbox to be applied |
| 26 | + - `matx.card` configures tokens for the mat-card to be applied |
| 27 | +- The returned configurations from these functions are passed to `matx.theme` or `matx.retheme` |
| 28 | +- If no arguments are passed, the configuration instructs the mixin to just output the default value for all of the tokens needed by that component |
| 29 | +- The functions can also accept a map of customizations as an argument. |
| 30 | + - Each function has its own set of supported map keys that can be used to customize the value of the underlying tokens |
| 31 | + - The map keys are a higher level API then the tokens, some of the keys may result in a single token being change, but some may change multiple tokens |
| 32 | + - For supported map keys (TODO: have docs for these): |
| 33 | + - See `$_customization-resolvers` [here](https://github.com/angular/components/blob/main/src/material-experimental/theming/_checkbox.scss) for `matx.checkbox` |
| 34 | + - See `$_customization-resolvers` [here](https://github.com/angular/components/blob/main/src/material-experimental/theming/_card.scss) for `matx.card` |
| 35 | + |
| 36 | +## Theming mixins |
| 37 | +- There are 2 mixins used for theming apps |
| 38 | + - `matx.theme` is intended to apply the full theme for some components, with all tokens they need to function. |
| 39 | + - `matx.retheme` is intended to re-apply specific tokens to change the appearance for some components by overriding the tokens applied by `matx.theme`. |
| 40 | +- Both mixins emit *only* CSS variables representing design tokens |
| 41 | +- Both mixins emit their tokens directly under the user specified selector. This gives the user complete control over the selector specificity. |
| 42 | +- Using `matx.theme` |
| 43 | + - Takes 2 arguments: |
| 44 | + - `$tokens` The set of token defaults that will be used for any tokens not explicitly customized by the component theme config |
| 45 | + - `$components` List of component theme configs indicating which components to emit tokens for, and optionally, customizations for some token values |
| 46 | + - Outputs *all* tokens used by the configured components |
| 47 | +- Using `matx.retheme` |
| 48 | + - Takes 1 argument: |
| 49 | + - `$components` List of component theme configs to emit customized token values for |
| 50 | + - Outputs *only* the explicitly customized tokens, not any of the other tokens used by the component |
| 51 | + |
| 52 | +## Recommended theming structure |
| 53 | +- Apply the base token values using `matx.theme` *once* |
| 54 | +- Choose selectors with minimal specificity when applying tokens |
| 55 | +- Prefer to rely on CSS inheritance to apply token overrides rather than specificity. |
| 56 | + For example if checkbox tokens are set on the root element (`html`) they will be inherited down |
| 57 | + the DOM and affect any `<mat-checkbox>` within the document. If checkboxes in a specific section |
| 58 | + need to appear differently, say within `.dark-sidebar`, set the token overrides on the |
| 59 | + `.dark-sidebar` element and they will be inherited down to the checkboxes within, instead of the |
| 60 | + values from the root element. |
| 61 | +- For a small example, see this [alternate partial theme](https://github.com/angular/components/blob/main/src/dev-app/theme-token-api.scss) for the dev-app |
0 commit comments