-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathangular-material-20.mdc
More file actions
285 lines (220 loc) · 8.26 KB
/
angular-material-20.mdc
File metadata and controls
285 lines (220 loc) · 8.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
---
description: This rule provides comprehensive best practices and coding standards for Angular development with Angular Material, focusing on Material 3 (a.k.a. M3) design, theme configuration and various API usages.
globs: ["**/*.{ts,html,scss,css}"]
---
# Angular Material 20 Best Practices
This project adheres to modern Angular Material best practices, emphasizing maintainability, performance, accessibility, scalability and correct usage of APIs.
## Core Principles
### SCSS vs CSS
- **Prefer SCSS** for styling, especially when theme customization is required. Angular Material has APIs written in SCSS.
- **Use CSS only** when you need to use theme styles generated by `mat.theme` mixin. Modifications or customizations for Angular Material theming are not possible with CSS.
### Component & Directive Imports
Always import components and directives from `@angular/material` package. Avoid importing modules.
**Good:**
```ts
import { MatButton } from '@angular/material/button';
import { MatIcon } from '@angular/material/icon';
import { MatCard, MatCardContent } from '@angular/material/card';
```
**Bad:**
```ts
import { MatButtonModule } from '@angular/material/button';
import { MatIconModule } from '@angular/material/icon';
import { MatCardModule } from '@angular/material/card';
```
## Theming Configuration
Always use `mat.theme` mixin to configure the theme. **Avoid** using `mat.define-theme`, `mat.define-light-theme`, `mat.define-dark-theme` functions and `mat.core` mixin.
### Basic Theme Configuration
The `mat.theme` mixin takes a map with the following properties:
- **`color`**: Single color palette or a map of color palettes
- **`typography`**: Single typography or a map of typography properties
- **`density`**: Integer from 0 to -5 (0 = default spacing, -5 = most dense)
### Simple Theme Example
```scss
@use '@angular/material' as mat;
html {
color-scheme: light dark;
@include mat.theme((
color: mat.$violet-palette,
typography: Roboto,
density: 0
));
}
```
### Advanced Theme with Multiple Color Palettes
```scss
@use '@angular/material' as mat;
html {
color-scheme: light dark;
@include mat.theme((
color: (
primary: mat.$violet-palette,
tertiary: mat.$orange-palette,
),
typography: (
plain-family: Roboto,
brand-family: Open Sans,
bold-weight: 900,
medium-weight: 500,
regular-weight: 300,
),
density: 0
));
}
```
### Supporting Light and Dark Mode with Selectors
```scss
@use '@angular/material' as mat;
html {
color-scheme: light;
@include mat.theme((
color: mat.$violet-palette,
typography: Roboto,
density: 0
));
}
body.dark-mode {
color-scheme: dark;
}
```
### Multiple Themes
```scss
@use '@angular/material' as mat;
html {
@include mat.theme((
color: mat.$violet-palette,
typography: Roboto,
density: 0,
));
}
.example-bright-container {
@include mat.theme((
color: mat.$cyan-palette,
));
}
```
## Button Directives
Always use `matButton`, `matIconButton` & `matFab` selectors for Angular Material buttons' directives. **Avoid** using `mat-button`, `mat-raised-button`, `mat-stroked-button`, `mat-flat-button`, `mat-icon-button`, `mat-fab`, etc. selectors.
### Correct Button Usage
```html
<button matButton>Basic</button>
<button matButton="elevated">Elevated</button>
<button matButton="outlined">Outlined</button>
<button matButton="filled">Filled</button>
<button matButton="tonal">Tonal</button>
<button matIconButton aria-label="Example icon button with a vertical three dot icon">
<mat-icon>more_vert</mat-icon>
</button>
<button matFab aria-label="Example icon button with a delete icon">
<mat-icon>delete</mat-icon>
</button>
```
### Incorrect Button Usage (Avoid)
```html
<button mat-button>Basic</button>
<button mat-raised-button>Elevated</button>
<button mat-stroked-button>Outlined</button>
<button mat-flat-button>Filled</button>
<button mat-icon-button aria-label="Example icon button">
<mat-icon>more_vert</mat-icon>
</button>
<button mat-fab aria-label="Example icon button">
<mat-icon>delete</mat-icon>
</button>
```
## Using Theme Styles & System Variables
The `mat.theme` mixin emits CSS variables known as "System Variables" that can be used to style components.
### Basic Usage Example
```css
:host {
background: var(--mat-sys-primary-container);
color: var(--mat-sys-on-primary-container);
border: 1px solid var(--mat-sys-outline-variant);
font: var(--mat-sys-body-large);
}
```
### System Variables Reference
#### Colors
- `--mat-sys-primary`, `--mat-sys-on-primary`, `--mat-sys-primary-container`, `--mat-sys-on-primary-container`
- `--mat-sys-secondary`, `--mat-sys-tertiary`, and their `on-` and `container` variants
- `--mat-sys-surface`, `--mat-sys-on-surface`, `--mat-sys-surface-container`
- `--mat-sys-error`, `--mat-sys-on-error`, `--mat-sys-error-container`
- `--mat-sys-outline`, `--mat-sys-outline-variant`
#### Typography
- `--mat-sys-display-small`, `--mat-sys-display-medium`, `--mat-sys-display-large`
- `--mat-sys-headline-small`, `--mat-sys-title-medium`, `--mat-sys-body-large`
- `--mat-sys-label-medium`, etc.
#### Shape (Border Radius)
- `--mat-sys-corner-extra-small`, `--mat-sys-corner-medium`, `--mat-sys-corner-large`, etc.
#### Elevation (Shadow)
- `--mat-sys-level0` through `--mat-sys-level5`
See [Angular Material docs](https://material.angular.dev/guide/system-variables) for a full list.
## Customizing Tokens
Angular Material components allow for narrowly targeted customization through the `overrides` mixins.
### System Token Overrides
Use `mat.theme-overrides` mixin to change system-level tokens:
```scss
@use "@angular/material" as mat;
html {
color-scheme: light dark;
@include mat.theme((
color: mat.$violet-palette,
typography: Roboto,
density: 0,
));
.example-orange-primary-container {
@include mat.theme-overrides((
primary-container: #84ffff,
));
}
}
```
### Multiple System Overrides
```scss
@use "@angular/material" as mat;
.example-container {
@include mat.theme-overrides((
primary: #ebdcff,
on-primary: #230f46,
body-medium: 500 1.15rem/1.3rem Arial,
corner-large: 32px,
level3: 0 4px 6px 1px var(--mat-sys-surface-dim),
));
}
```
### Component Token Overrides
Each Angular Material component defines an `overrides` mixin for customizing tokenized styles:
```scss
html {
@include mat.card-overrides((
elevated-container-color: red,
elevated-container-shape: 32px,
title-text-size: 2rem,
));
}
```
### When to Use Which Mixin
| Mixin | Use Case | Ideal Usage Count |
| --------------------------- | --------------------------------------------------------------------------- | ------------------------------------------ |
| `mat.theme` | Theme for application | 1 |
| `mat.theme-overrides` | Change system-level tokens (e.g., success theme with green color) | Equals to contextual themes in application |
| `mat.<COMPONENT>-overrides` | Change component-level tokens (e.g., emphasis button colors for new feature) | As and when needed |
## Typography Binding to HTML Elements
By default, Angular Material doesn't bind typescale levels to HTML elements, but here are recommended bindings:
| Typescale Level | Size | Native Element |
| --------------- | -------- | -------------- |
| `display` | `large` | `<h1>` |
| `display` | `medium` | `<h2>` |
| `display` | `small` | `<h3>` |
| `headline` | `large` | `<h4>` |
| `headline` | `medium` | `<h5>` |
| `headline` | `small` | `<h6>` |
## Best Practices Summary
1. **Always use `mat.theme` mixin** for theme configuration
2. **Import components and directives**, not modules
3. **Use new button directive syntax** (`matButton`, `matIconButton`, `matFab`)
4. **Leverage system variables** for consistent styling
5. **Use override mixins** for targeted customization
6. **Prefer SCSS** for theme-related styling
7. **Include `color-scheme` property** for proper light/dark mode support
8. **Never override styles by targeting internal class names.**