

Also from Kynth Studios


Also from Kynth Studios


Also from Kynth Studios
1234567# Angular Material 20 Best Practices89This project adheres to modern Angular Material best practices, emphasizing maintainability, performance, accessibility, scalability and correct usage of APIs.1011## Core Principles1213### SCSS vs CSS1415- **Prefer SCSS** for styling, especially when theme customization is required. Angular Material has APIs written in SCSS.16- **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.1718### Component & Directive Imports1920Always import components and directives from `@angular/material` package. Avoid importing modules.2122**Good:**2324```ts25import { MatButton } from '@angular/material/button';26import { MatIcon } from '@angular/material/icon';27import { MatCard, MatCardContent } from '@angular/material/card';28```2930**Bad:**3132```ts33import { MatButtonModule } from '@angular/material/button';34import { MatIconModule } from '@angular/material/icon';35import { MatCardModule } from '@angular/material/card';36```3738## Theming Configuration3940Always 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.4142### Basic Theme Configuration4344The `mat.theme` mixin takes a map with the following properties:4546- **`color`**: Single color palette or a map of color palettes47- **`typography`**: Single typography or a map of typography properties48- **`density`**: Integer from 0 to -5 (0 = default spacing, -5 = most dense)4950### Simple Theme Example5152```scss53@use '@angular/material' as mat;5455html {56 color-scheme: light dark;57 @include mat.theme(58 (59 color: mat.$violet-palette,60 typography: Roboto,61 density: 0,62 )63 );64}65```6667### Advanced Theme with Multiple Color Palettes6869```scss70@use '@angular/material' as mat;7172html {73 color-scheme: light dark;74 @include mat.theme(75 (76 color: (77 primary: mat.$violet-palette,78 tertiary: mat.$orange-palette,79 ),80 typography: (81 plain-family: Roboto,82 brand-family: Open Sans,83 bold-weight: 900,84 medium-weight: 500,85 regular-weight: 300,86 ),87 density: 0,88 )89 );90}91```9293### Supporting Light and Dark Mode with Selectors9495```scss96@use '@angular/material' as mat;9798html {99 color-scheme: light;100 @include mat.theme(101 (102 color: mat.$violet-palette,103 typography: Roboto,104 density: 0,105 )106 );107}108109body.dark-mode {110 color-scheme: dark;111}112```113114### Multiple Themes115116```scss117@use '@angular/material' as mat;118119html {120 @include mat.theme(121 (122 color: mat.$violet-palette,123 typography: Roboto,124 density: 0,125 )126 );127}128129.example-bright-container {130 @include mat.theme(131 (132 color: mat.$cyan-palette,133 )134 );135}136```137138## Button Directives139140Always 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.141142### Correct Button Usage143144```html145<button matButton>Basic</button>146<button matButton="elevated">Elevated</button>147<button matButton="outlined">Outlined</button>148<button matButton="filled">Filled</button>149<button matButton="tonal">Tonal</button>150<button151 matIconButton152 aria-label="Example icon button with a vertical three dot icon">153 <mat-icon>more_vert</mat-icon>154</button>155<button matFab aria-label="Example icon button with a delete icon">156 <mat-icon>delete</mat-icon>157</button>158```159160### Incorrect Button Usage (Avoid)161162```html163<button mat-button>Basic</button>164<button mat-raised-button>Elevated</button>165<button mat-stroked-button>Outlined</button>166<button mat-flat-button>Filled</button>167<button mat-icon-button aria-label="Example icon button">168 <mat-icon>more_vert</mat-icon>169</button>170<button mat-fab aria-label="Example icon button">171 <mat-icon>delete</mat-icon>172</button>173```174175## Using Theme Styles & System Variables176177The `mat.theme` mixin emits CSS variables known as "System Variables" that can be used to style components.178179### Basic Usage Example180181```css182:host {183 background: var(--mat-sys-primary-container);184 color: var(--mat-sys-on-primary-container);185 border: 1px solid var(--mat-sys-outline-variant);186 font: var(--mat-sys-body-large);187}188```189190### System Variables Reference191192#### Colors193194- `--mat-sys-primary`, `--mat-sys-on-primary`, `--mat-sys-primary-container`, `--mat-sys-on-primary-container`195- `--mat-sys-secondary`, `--mat-sys-tertiary`, and their `on-` and `container` variants196- `--mat-sys-surface`, `--mat-sys-on-surface`, `--mat-sys-surface-container`197- `--mat-sys-error`, `--mat-sys-on-error`, `--mat-sys-error-container`198- `--mat-sys-outline`, `--mat-sys-outline-variant`199200#### Typography201202- `--mat-sys-display-small`, `--mat-sys-display-medium`, `--mat-sys-display-large`203- `--mat-sys-headline-small`, `--mat-sys-title-medium`, `--mat-sys-body-large`204- `--mat-sys-label-medium`, etc.205206#### Shape (Border Radius)207208- `--mat-sys-corner-extra-small`, `--mat-sys-corner-medium`, `--mat-sys-corner-large`, etc.209210#### Elevation (Shadow)211212- `--mat-sys-level0` through `--mat-sys-level5`213214See [Angular Material docs](https://material.angular.dev/guide/system-variables) for a full list.215216## Customizing Tokens217218Angular Material components allow for narrowly targeted customization through the `overrides` mixins.219220### System Token Overrides221222Use `mat.theme-overrides` mixin to change system-level tokens:223224```scss225@use '@angular/material' as mat;226227html {228 color-scheme: light dark;229 @include mat.theme(230 (231 color: mat.$violet-palette,232 typography: Roboto,233 density: 0,234 )235 );236237 .example-orange-primary-container {238 @include mat.theme-overrides(239 (240 primary-container: #84ffff,241 )242 );243 }244}245```246247### Multiple System Overrides248249```scss250@use '@angular/material' as mat;251252.example-container {253 @include mat.theme-overrides(254 (255 primary: #ebdcff,256 on-primary: #230f46,257 body-medium: 500 1.15rem/1.3rem Arial,258 corner-large: 32px,259 level3: 0 4px 6px 1px var(--mat-sys-surface-dim),260 )261 );262}263```264265### Component Token Overrides266267Each Angular Material component defines an `overrides` mixin for customizing tokenized styles:268269```scss270html {271 @include mat.card-overrides(272 (273 elevated-container-color: red,274 elevated-container-shape: 32px,275 title-text-size: 2rem,276 )277 );278}279```280281### When to Use Which Mixin282283| Mixin | Use Case | Ideal Usage Count |284| --------------------------- | ---------------------------------------------------------------------------- | ------------------------------------------ |285| `mat.theme` | Theme for application | 1 |286| `mat.theme-overrides` | Change system-level tokens (e.g., success theme with green color) | Equals to contextual themes in application |287| `mat.<COMPONENT>-overrides` | Change component-level tokens (e.g., emphasis button colors for new feature) | As and when needed |288289## Typography Binding to HTML Elements290291By default, Angular Material doesn't bind typescale levels to HTML elements, but here are recommended bindings:292293| Typescale Level | Size | Native Element |294| --------------- | -------- | -------------- |295| `display` | `large` | `<h1>` |296| `display` | `medium` | `<h2>` |297| `display` | `small` | `<h3>` |298| `headline` | `large` | `<h4>` |299| `headline` | `medium` | `<h5>` |300| `headline` | `small` | `<h6>` |301302## CSS Classes303304Avoid applying CSS classes to inner components/elements of Angular Material components. If needed, wrap them in a container and apply classes to container.305306For example, avoid doing this:307308```html309<button matButton class="flex items-center gap-2">310 <mat-icon class="ml-1">add</mat-icon>311 Add312</button>313```314315Instead, do this:316317```html318<button matButton>319 <mat-icon>add</mat-icon>320 Add321</div>322```323324## SVGs vs mat-icons325326Try to use `mat-icon` for icons that are part of the Material Design icon set. For other icons, use `MatIconRegistry` to register the icon and use it in the template using `svgIcon` attribute.327328```angular-ts329import { MatIconRegistry } from '@angular/material/icon';330import { DomSanitizer } from '@angular/platform-browser';331332const ICON = `SVG Icon HTML string`;333334constructor(private matIconRegistry: MatIconRegistry, private sanitizer: DomSanitizer) {335 this.matIconRegistry.addSvgIconLiteral('custom-icon', this.sanitizer.bypassSecurityTrustHtml(ICON));336}337```338339```angular-html340<mat-icon svgIcon="custom-icon"></mat-icon>341```342343## Best Practices Summary3443451. **Always use `mat.theme` mixin** for theme configuration3462. **Import components and directives**, not modules3473. **Use new button directive syntax** (`matButton`, `matIconButton`, `matFab`)3484. **Leverage system variables** for consistent styling3495. **Use override mixins** for targeted customization3506. **Prefer SCSS** for theme-related styling3517. **Include `color-scheme` property** for proper light/dark mode support3528. **Never override styles by targeting internal class names.**353
One repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| Angular-Material-Dev/angular-material-ai-rules.github/copilot-instructions.md · 13 | Copilot instructions | styleuiperformanceagent-behaviour | 58/100 | 13 days ago | |
| Angular-Material-Dev/angular-material-ai-rules.windsurf/rules/angular_material_20_rules.md · 13 | Windsurf rules | styleuiperformancedo-not+1 | 65/100 | 13 days ago |
A badge carrying the measured quality of the strongest agent config file in this repository, out of 100. It reads from this index every time somebody loads your page, so it changes when the measurement changes and there is nothing to keep up to date. Free, no account, and the value is not something you or we can set by hand.
[](https://rulestack.kynth.studio/configs/angular-material-dev-angular-material-ai-rules-cursor-rules-angular-material-20)Would rather not hotlink us? Every badge is also served in shields.io’s endpoint schema, so shields renders the image and your readers never talk to our domain:
Published by Toolproof, the masthead over this index and eight others. The method behind the number is at toolproof.kynth.studio/methodology, and the whole thing is readable as JSON with no key at /api.
Directory