### Run Local Development Server Source: https://material.angular.dev/guide/getting-started Start the Angular development server to view your application locally. Access the application in your browser at http://localhost:4200. ```bash ng serve ``` -------------------------------- ### Linear Mode Stepper Example Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper Implements a custom stepper in linear mode, where users must complete previous steps before proceeding. This example does not use forms and includes manual completion logic. ```html ``` -------------------------------- ### Applying Theme Styles with Utility Classes Source: https://material.angular.dev/guide/theming Example of how to use the generated system utility classes to apply surface background and on-surface text colors to the body element. ```html ``` -------------------------------- ### Install Angular Material Source: https://material.angular.dev/guide/getting-started Use the Angular CLI to add Angular Material to your project. This command installs necessary dependencies and configures global styles and fonts. ```bash ng add @angular/material ``` -------------------------------- ### Install Angular CDK Schematics Source: https://material.angular.dev/guide/schematics If you only need to install the Angular Component Dev Kit (CDK), use this command which includes its schematics. ```bash ng add @angular/cdk ``` -------------------------------- ### Basic Theme Setup with Violet Palette Source: https://material.angular.dev/guide/theming Sets up a basic theme using the violet color palette, Roboto font, and standard density. It targets the `html` selector and sets `color-scheme` to `light dark` for automatic mode switching. ```scss @use '@angular/material' as mat; html { color-scheme: light dark; @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: 0 )); } ``` -------------------------------- ### Apply Multiple Themes Contextually Source: https://material.angular.dev/guide/theming Define multiple themes by calling the `mat.theme` mixin more than once. This example applies a default violet theme and a specific cyan theme to elements within '.example-bright-container'. ```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, )); } ``` -------------------------------- ### Include Strong Focus Indicators Source: https://material.angular.dev/guide/material-2-theming Include the `strong-focus-indicators` mixin once per application to emit structural indicator styles. This example also includes component themes. ```scss @use '@angular/material' as mat; @include mat.strong-focus-indicators(); $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); $my-theme: mat.m2-define-light-theme(( color: ( primary: $my-primary, accent: $my-accent, ) )); @include mat.all-component-themes($my-theme); @include mat.strong-focus-indicators-theme($my-theme); ``` -------------------------------- ### Enable Strong Focus Indicators Source: https://material.angular.dev/guide/theming Call `mat.strong-focus-indicators()` to enable highly visible outlines on focused elements, improving accessibility. This example includes it alongside a custom theme setup. ```scss @use "@angular/material" as mat; html { color-scheme: light dark; @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: 0 )); @include mat.strong-focus-indicators(); } ``` -------------------------------- ### Load Google Fonts Source: https://material.angular.dev/guide/theming Include this HTML in your application's `` to load specific font families and weights from Google Fonts. This example loads Roboto with weights 400, 500, and 700, and Open Sans with weights 300 through 800. ```html ``` -------------------------------- ### Apply Typography Map to Theme Source: https://material.angular.dev/guide/theming Use this to set distinct font families for plain and brand text, and specify weights for bold, medium, and regular text. This example uses Roboto for plain text and Open Sans for brand text, with custom weights and the violet color palette. ```scss @use '@angular/material' as mat; html { @include mat.theme(( color: mat.$violet-palette, typography: ( plain-family: Roboto, brand-family: Open Sans, bold-weight: 900, medium-weight: 500, regular-weight: 300, ), density: 0, )); } ``` -------------------------------- ### Test with Component Harnesses Source: https://material.angular.dev/guide/using-component-harnesses This example shows the same test using Angular Material component harnesses. It utilizes `MatSelectHarness` for a more declarative and robust way to interact with the select component, abstracting DOM details and asynchronous behavior. ```typescript describe('issue-report-selector', () => { let fixture: ComponentFixture; let loader: HarnessLoader; beforeEach(async () => { TestBed.configureTestingModule({ imports: [IssueReportSelectorModule], declarations: [IssueReportSelector], }); fixture = TestBed.createComponent(IssueReportSelector); fixture.detectChanges(); loader = TestbedHarnessEnvironment.loader(fixture); }); it('should switch to bug report template', async () => { expect(fixture.debugElement.query('bug-report-form')).toBeNull(); const select = await loader.getHarness(MatSelectHarness); await select.open(); const bugOption = await select.getOption({text: 'Bug'}); await bugOption.click(); expect(fixture.debugElement.query('bug-report-form')).not.toBeNull(); }); }); ``` -------------------------------- ### Apply Density to Theme Source: https://material.angular.dev/guide/theming Adjusts the spacing within components by setting a density value. A value of -2 reduces whitespace, making the layout more compact. This example also applies the violet color palette and Roboto font family. ```scss @use '@angular/material' as mat; html { @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: -2, )); } ``` -------------------------------- ### Test without Component Harnesses Source: https://material.angular.dev/guide/using-component-harnesses This example demonstrates a test for an issue report selector component without using component harnesses. It directly queries the DOM using CSS selectors and Angular's `By.css`, requiring manual handling of asynchronous operations. ```typescript describe('issue-report-selector', () => { let fixture: ComponentFixture; beforeEach(async () => { TestBed.configureTestingModule({ imports: [IssueReportSelectorModule], declarations: [IssueReportSelector], }); fixture = TestBed.createComponent(IssueReportSelector); fixture.detectChanges(); }); it('should switch to bug report template', async () => { expect(fixture.debugElement.query('bug-report-form')).toBeNull(); const selectTrigger = fixture.debugElement.query(By.css('.mat-select-trigger')); selectTrigger.triggerEventHandler('click', {}); fixture.detectChanges(); await fixture.whenStable(); const options = document.querySelectorAll('.mat-select-panel mat-option'); options[1].click(); // Click the second option, "Bug". fixture.detectChanges(); await fixture.whenStable(); expect(fixture.debugElement.query('bug-report-form')).not.toBeNull(); }); }); ``` -------------------------------- ### Customize Strong Focus Indicator Appearance Source: https://material.angular.dev/guide/typography Customize the appearance of strong focus indicators by passing a configuration map to the `strong-focus-indicators` mixin. This example sets custom border color, style, width, and radius. ```scss @use '@angular/material' as mat; @include mat.strong-focus-indicators(( border-color: red, border-style: dotted, border-width: 4px, border-radius: 2px, )); ``` -------------------------------- ### Use Custom Control in Material Form Field Source: https://material.angular.dev/guide/creating-a-custom-form-field-control This example shows how to integrate a custom form field control, `example-tel-input`, within the Angular Material `` component. ```html ``` -------------------------------- ### Use Theme Styles with CSS Variables Source: https://material.angular.dev/guide/theming Apply theme colors and typography to custom components using CSS variables generated by `mat.theme`. This example styles a banner with primary container background, on-primary container text, and body large font. ```css .my-component { 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); } ``` -------------------------------- ### Customize Strong Focus Indicator Appearance Source: https://material.angular.dev/guide/theming Pass a configuration map to `mat.strong-focus-indicators` to customize the border color, style, width, and radius of the focus indicators. This example sets custom dotted red borders. ```scss @use "@angular/material" as mat; @include mat.strong-focus-indicators(( border-color: red, border-style: dotted, border-width: 4px, border-radius: 2px, )); ``` -------------------------------- ### Override System Tokens with mat.theme overrides map Source: https://material.angular.dev/guide/theming Alternatively, provide an optional override map directly within the `mat.theme` mixin to replace values. This example sets the `primary-container` token to orange. ```scss @use "@angular/material" as mat; html { color-scheme: light dark; @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: 0 ), $overrides: ( primary-container: orange, )); } ``` -------------------------------- ### Customize Card Component Tokens Source: https://material.angular.dev/guide/theming Utilize the `mat.card-overrides` mixin to customize specific tokens for the Card component. This example changes the background color, border radius, and title font size. ```scss html { @include mat.card-overrides(( elevated-container-color: red, elevated-container-shape: 32px, title-text-size: 2rem, )); } ``` -------------------------------- ### Load Harnesses from Child Loader Source: https://material.angular.dev/guide/using-component-harnesses Use `getChildLoader` to get a `HarnessLoader` for a specific sub-section of the DOM, identified by a CSS selector. This allows for more targeted harness loading. ```typescript it('should work', async () => { const footerLoader = await loader.getChildLoader('.footer'); const footerButton = await footerLoader.getHarness(MatButtonHarness); }); ``` -------------------------------- ### Handle Container Click in Custom Form Field Source: https://material.angular.dev/guide/creating-a-custom-form-field-control Implement the `onContainerClick` method to define behavior when the user clicks on the form field. This example focuses the input element within the custom control. ```typescript onContainerClick(event: MouseEvent) { if ((event.target as Element).tagName.toLowerCase() != 'input') { this._elementRef.nativeElement.querySelector('input').focus(); } } ``` -------------------------------- ### Override System Tokens with mat.theme-overrides Source: https://material.angular.dev/guide/theming Use the `mat.theme-overrides` mixin to redefine CSS variables for system tokens. This example applies a violet color palette while specifically changing the `primary-container` token to a shade of blue. ```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 )); } } ``` -------------------------------- ### Iterating Over Custom Steps Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper Demonstrates how to dynamically generate steps for the custom stepper by iterating over a data source and using a custom step component. ```html @for (step of mySteps; track step) { } ``` -------------------------------- ### Setting up HarnessLoader for Unit Tests Source: https://material.angular.dev/guide/using-component-harnesses Import `TestbedHarnessEnvironment` and `HarnessLoader` to create a `HarnessLoader` instance for a component fixture in unit tests. This loader is used to find and interact with Angular Material components. ```typescript import {HarnessLoader} from '@angular/cdk/testing'; import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed'; let loader: HarnessLoader; describe('my-component', () => { beforeEach(async () => { TestBed.configureTestingModule({imports: [MyModule], declarations: [UserProfile]}); fixture = TestBed.createComponent(UserProfile); loader = TestbedHarnessEnvironment.loader(fixture); }); } ``` -------------------------------- ### Enabling System Utility Classes Source: https://material.angular.dev/guide/theming Includes system utility classes for applying theme styles directly from component templates. This allows for easy application of background and text colors. ```scss html { ... @include mat.system-classes(); } ``` -------------------------------- ### Generate Navigation Component Source: https://material.angular.dev/guide/schematics Creates a new component featuring a responsive sidenav and a toolbar, suitable for application navigation. ```bash ng generate @angular/material:navigation ``` -------------------------------- ### Conditional Dark Mode with CSS Class Source: https://material.angular.dev/guide/theming Apply a dark theme conditionally by setting `color-scheme` within a CSS selector. This example applies dark mode when the 'dark-mode' class is present on the body. ```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; } ``` -------------------------------- ### Include all theme dimensions for a component Source: https://material.angular.dev/guide/material-2-theming Use the component's theme mixin to include all theme dimensions (base, color, typography, density) at once. This is the recommended approach for laying down base styles. ```scss @include mat.checkbox-theme($theme) ``` -------------------------------- ### Apply Theme Styles using Utility Classes Source: https://material.angular.dev/guide/theming-your-components Applies theme styles in the component template using Material Design utility classes for background, text color, and font. ```html ``` -------------------------------- ### Check Theme Configuration Dimensions Source: https://material.angular.dev/guide/material-2-theming Use `theme-has` to verify if a theme includes configuration for specific dimensions like base, color, typography, or density. ```scss $theme: mat.m2-define-dark-theme(...); $has-base: mat.theme-has($theme, base); $has-color: mat.theme-has($theme, color); $has-typography: mat.theme-has($theme, typography); $has-density: mat.theme-has($theme, density); ``` -------------------------------- ### Define a Light Theme with Color Palettes Source: https://material.angular.dev/guide/material-2-theming Constructs a light theme using defined color palettes, typography configuration, and density. The theme is defined using `m2-define-light-theme`. ```scss @use '@angular/material' as mat; $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); // The "warn" palette is optional and defaults to red if not specified. $my-warn: mat.m2-define-palette(mat.$m2-red-palette); $my-theme: mat.m2-define-light-theme(( color: ( primary: $my-primary, accent: $my-accent, warn: $my-warn, ), typography: mat.m2-define-typography-config(), density: 0, )); ``` -------------------------------- ### Generate Dashboard Component Source: https://material.angular.dev/guide/schematics Creates a component with a dynamic grid list of Material Design cards, useful for dashboard layouts. ```bash ng generate @angular/material:dashboard ``` -------------------------------- ### Scope Component Styles with Sass Mixins Source: https://material.angular.dev/guide/material-2-theming Customize component styles within a specific scope by including Sass mixins within a CSS rule declaration. This example customizes button colors for elements with the `.my-special-section` class. ```scss @use '@angular/material' as mat; .my-special-section { $special-primary: mat.m2-define-palette(mat.$m2-orange-palette); $special-accent: mat.m2-define-palette(mat.$m2-brown-palette); $special-theme: mat.m2-define-dark-theme(( color: (primary: $special-primary, accent: $special-accent), )); @include mat.button-color($special-theme); } ``` -------------------------------- ### Apply typography styles in HTML Source: https://material.angular.dev/guide/material-2-theming Demonstrates how to apply Material Design typography styles using emitted CSS classes, both inside and outside the `.mat-typography` container. ```html

Top header

Introductory text

Inner header

Some inner text

``` -------------------------------- ### Load Button Harnesses Source: https://material.angular.dev/guide/using-component-harnesses Use `getHarness` or `getAllHarnesses` to load component harnesses. `getAllHarnesses` retrieves all instances, while `getHarness` retrieves the first instance. All harness APIs are asynchronous and return Promises. ```typescript import {MatButtonHarness} from '@angular/material/button/testing'; ... it('should work', async () => { const buttons = await loader.getAllHarnesses(MatButtonHarness); // length: 3 const firstButton = await loader.getHarness(MatButtonHarness); // === buttons[0] }); ``` -------------------------------- ### Generate Material 3 Theme Color File Source: https://material.angular.dev/guide/schematics Generates a file with Material 3 palettes and optional high contrast color override mixins for theme customization. ```bash ng generate @angular/material:theme-color ``` -------------------------------- ### Load Harnesses with Predicates Source: https://material.angular.dev/guide/using-component-harnesses Use the static `with` method on component harnesses to create a `HarnessPredicate`. This predicate filters harnesses based on constraints like `selector`, `ancestor`, or harness-specific options like `text` for buttons. ```typescript it('should work', async () => { // Harness for mat-button whose id is 'more-info'. const info = await loader.getHarness(MatButtonHarness.with({selector: '#more-info'})); // Harness for mat-button whose text is 'Cancel'. const cancel = await loader.getHarness(MatButtonHarness.with({text: 'Cancel'})); // Harness for mat-button with class 'confirm' and whose text is either 'Ok' or 'Okay'. const okButton = await loader.getHarness( MatButtonHarness.with({selector: '.confirm', text: /^(Ok|Okay)$/})); }); ``` -------------------------------- ### Apply Surface Background Color Source: https://material.angular.dev/guide/theming-your-components Use this class for general component surfaces like tables, dialogs, and menus. Text should use `on-surface` or `on-surface-variant` for contrast. ```css .mat-bg-surface { background-color: var(--mat-sys-surface); } ``` -------------------------------- ### Using the Custom Stepper Component Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper Integrates the custom stepper component into the application's template, defining steps using the `` tag. ```html

This is any content of "Step 1"

This is any content of "Step 2"

``` -------------------------------- ### Customize Strong Focus Indicators Source: https://material.angular.dev/guide/material-2-theming Customize strong focus indicator styles by passing a configuration map to `strong-focus-indicators` and setting a custom color for `strong-focus-indicators-theme`. ```scss @use '@angular/material' as mat; @include mat.strong-focus-indicators(( border-style: dotted, border-width: 4px, border-radius: 2px, )); $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); $my-theme: mat.m2-define-light-theme(( color: ( primary: $my-primary, accent: $my-accent, ) )); @include mat.all-component-themes($my-theme); @include mat.strong-focus-indicators-theme(purple); ``` -------------------------------- ### Include Roboto Font from Google Fonts Source: https://material.angular.dev/guide/material-2-theming Add these HTML tags to your application's `` to load the Roboto font and its specified weights from Google Fonts. ```html ``` -------------------------------- ### Apply Material Design Border (CSS) Source: https://material.angular.dev/guide/theming-your-components Use this class to apply a standard Material Design border, useful for components needing a visible boundary like outlined buttons. ```css .mat-border { border: 1px solid var(--mat-sys-outline); } ``` -------------------------------- ### Use Theme Styles with Utility Classes Source: https://material.angular.dev/guide/theming Achieve the same theming effects as CSS variables by using Angular Material's pre-defined utility classes for backgrounds, text colors, borders, and typography. ```html
``` -------------------------------- ### Theme with Custom Primary and Tertiary Palettes (Light Type) Source: https://material.angular.dev/guide/theming Defines a theme with a violet primary palette and an orange tertiary palette. The `theme-type` is set to `light`, ensuring only light color values are applied. Roboto typography and standard density are also configured. ```scss @use '@angular/material' as mat; html { @include mat.theme(( color: ( primary: mat.$violet-palette, tertiary: mat.$orange-palette, theme-type: light, ), typography: Roboto, density: 0 )); } ``` -------------------------------- ### Include base styles for a component Source: https://material.angular.dev/guide/material-2-theming Use the component's base mixin to include structural styles like border-radius and border-width. This should be included once per application. ```scss @include mat.checkbox-base($theme) ``` -------------------------------- ### Define Color Palettes for a Theme Source: https://material.angular.dev/guide/material-2-theming Defines primary, accent, and optional warn color palettes using Sass. Requires importing the Angular Material Sass API. ```scss @use '@angular/material' as mat; $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); // The "warn" palette is optional and defaults to red if not specified. $my-warn: mat.m2-define-palette(mat.$m2-red-palette); ``` -------------------------------- ### Apply Core and Button Theme Styles Source: https://material.angular.dev/guide/material-2-theming Applies prerequisite styles for common features and button-specific styles using Sass mixins. Requires the theme definition and importing the Angular Material Sass API. ```scss @use '@angular/material' as mat; $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); $my-theme: mat.m2-define-light-theme(( color: ( primary: $my-primary, accent: $my-accent, ), typography: mat.m2-define-typography-config(), density: 0, )); // Emit theme-dependent styles for common features used across multiple components. @include mat.core-theme($my-theme); // Emit styles for MatButton based on `$my-theme`. Because the configuration // passed to `m2-define-light-theme` omits typography, `button-theme` will not // emit any typography styles. @include mat.button-theme($my-theme); // Include the theme mixins for other components you use here. ``` -------------------------------- ### Configure Light Mode with Dark Mode Override Source: https://material.angular.dev/guide/typography Set the default mode to light and override to dark mode when a specific class (e.g., `dark-mode`) is applied to the body. This allows for conditional theming. ```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; } ``` -------------------------------- ### Define and Include Material Design 2 Theme Source: https://material.angular.dev/guide/theming-your-components Defines a Material Design 2 theme and includes core theme styles, button theme, and system classes. ```scss @use "@angular/material" as mat; $theme: mat.m2-define-light-theme(( color: ( primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500), ), ... )); html { @include mat.core-theme($theme); @include mat.button-theme($theme); ... @include mat.m2-theme($theme); @include mat.system-classes(); } ``` -------------------------------- ### Include density styles for a component Source: https://material.angular.dev/guide/material-2-theming Use the component's density mixin to include styles related to element size and spacing. This may need to be included multiple times for different density modes, such as normal and compact. ```scss @include mat.checkbox-density($theme) ``` -------------------------------- ### Default Application Background and Text Colors Source: https://material.angular.dev/guide/theming Applies default background and on-surface text colors to the body using CSS variables defined by the theme. ```css body { background: var(--mat-sys-surface); color: var(--mat-sys-on-surface); } ``` -------------------------------- ### Custom Stepper Component Template Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper The HTML template for the custom stepper. It displays the current step, projects content from `CdkStep`, and includes navigation buttons and step indicators. ```html

Step {{selectedIndex + 1}}/{{steps.length}}

@for (step of steps; track step; let i = $index) { }
``` -------------------------------- ### Primary Background Color Utility Class Source: https://material.angular.dev/guide/theming-your-components Use this utility class to apply a primary background color to components. Ensure text and icons use the 'on-primary' token for contrast. ```css .mat-bg-primary { background-color: var(--mat-sys-primary); } ``` -------------------------------- ### Apply Full Border Radius (CSS) Source: https://material.angular.dev/guide/theming-your-components Use this class to apply a full border radius, making components circular, such as user avatars, badges, and buttons. ```css .mat-corner-full { border-radius: var(--mat-sys-corner-full); } ``` -------------------------------- ### Custom Stepper Component Implementation Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper Extends `CdkStepper` to create a custom stepper. Provides itself as `CdkStepper` for recognition by other components and includes a method to handle step clicks. ```typescript @Component({ selector: 'app-custom-stepper', templateUrl: './custom-stepper.component.html', styleUrl: './custom-stepper.component.css', // This custom stepper provides itself as CdkStepper so that it can be recognized // by other components. providers: [{ provide: CdkStepper, useExisting: CustomStepperComponent }] }) export class CustomStepperComponent extends CdkStepper { onClick(index: number): void { this.selectedIndex = index; } } ``` -------------------------------- ### Read Density Scale from Theme Source: https://material.angular.dev/guide/material-2-theming Use `get-theme-density` to retrieve the density scale value configured within a theme. ```scss $theme: mat.m2-define-dark-theme(...); $density-scale: mat.get-theme-density($theme); ``` -------------------------------- ### Include color styles for a component Source: https://material.angular.dev/guide/material-2-theming Use the component's color mixin to include styles related to application colors. This may need to be included multiple times for different configurations, such as light and dark themes. ```scss @include mat.checkbox-color($theme) ``` -------------------------------- ### Apply M3 Theme Globally (SCSS) Source: https://material.angular.dev/guide/material-2-theming Applies a Material 3 theme globally by including the `mat.theme` mixin within the `html` selector. This is the recommended approach for v19+. ```scss @use '@angular/material' as mat; html { @include mat.theme(( color: mat.$violet-palette, typography: Roboto, density: 0, )); } ``` -------------------------------- ### Custom Component Theme Mixin (Legacy) Source: https://material.angular.dev/guide/theming-your-components A Sass mixin to define theme styles for custom components using Material Design 2 Sass APIs and color configurations. ```scss @use "@angular/material" as mat; @use "sass:map"; @mixin my-component-theme($theme) { $foreground: map.get(mat.m2-get-color-config($theme), foreground); $background: map.get(mat.m2-get-color-config($theme), background); $primary: map.get(mat.m2-get-color-config($theme), primary); $typography: mat.m2-get-typography-config($config-or-theme); .widget-a { background-color: mat.m2-get-color-from-palette($background, card); color: mat.m2-get-color-from-palette($foreground, text); @include mat.m2-typography-level($config, body-1); } .widget-b { background-color: mat.m2-get-color-from-palette($primary, default); color: mat.m2-get-color-from-palette($primary, default-contrast); } } ``` -------------------------------- ### Apply Surface Container Background Color Source: https://material.angular.dev/guide/theming-your-components Use this class for surfaces that need to stand out from the main surface color, like menus. ```css .mat-bg-surface-container { background-color: var(--mat-sys-surface-container); } ``` -------------------------------- ### Apply Extra Small Border Radius (CSS) Source: https://material.angular.dev/guide/theming-your-components Use this class to apply an extra small border radius, suitable for components like chips, snackbars, and tooltips. ```css .mat-corner-xs { border-radius: var(--mat-sys-corner-extra-small); } ``` -------------------------------- ### Include Material 2 backwards compatibility styles for color variants Source: https://material.angular.dev/guide/material-2-theming Use this mixin to enable backwards compatibility for color variants in M3 themes. This restores the behavior of the `color="primary"`, `color="accent"`, or `color="warn"` options. ```scss @use '@angular/material' as mat; $theme: mat.define-theme(); html { @include mat.all-component-themes($theme); @include mat.color-variants-backwards-compatibility($theme); } ``` -------------------------------- ### Component Theme Compatibility Check (SCSS) Source: https://material.angular.dev/guide/material-2-theming Checks the theme version using `mat.get-theme-version` to conditionally apply Material 3 or Material 2 styles within a component's theme mixin. ```scss @use '@angular/material' as mat; @mixin my-comp-theme($theme) { @if (mat.get-theme-version($theme) == 1) { // Add your new M3 styles here. } @else { // Keep your old M2 styles here. } } ``` -------------------------------- ### Define a custom typography config Source: https://material.angular.dev/guide/material-2-theming Use `m2-define-typography-config` to create a map of all typography levels for your theme. Parameters for each level are optional and default to Material Design's baseline if not specified. ```scss @use '@angular/material' as mat; $my-custom-typography-config: mat.m2-define-typography-config( $headline-1: mat.m2-define-typography-level(112px, 112px, 300, $letter-spacing: -0.05em), $headline-2: mat.m2-define-typography-level(56px, 56px, 400, $letter-spacing: -0.02em), $headline-3: mat.m2-define-typography-level(45px, 48px, 400, $letter-spacing: -0.005em), $headline-4: mat.m2-define-typography-level(34px, 40px, 400), $headline-5: mat.m2-define-typography-level(24px, 32px, 400), // ... ); ``` -------------------------------- ### Implement MatFormFieldControl Interface Source: https://material.angular.dev/guide/creating-a-custom-form-field-control This snippet shows the basic structure for implementing the `MatFormFieldControl` interface, including dependency injection for `MatFormField`. ```typescript export class MyTelInput implements MatFormFieldControl { ... constructor(... @Optional() public parentFormField: MatFormField) { ``` -------------------------------- ### Use a Predefined Angular Material Palette Source: https://material.angular.dev/guide/material-2-theming Imports and assigns a predefined Material Design palette from the Angular Material library. This palette can then be used when defining a custom theme. ```sass @use '@angular/material' as mat; $my-palette: mat.$m2-indigo-palette; ``` -------------------------------- ### Interact with MatButton Harness Source: https://material.angular.dev/guide/using-component-harnesses Interact with component harnesses using their provided methods, such as `click()`, `isDisabled()`, or `getText()`. The CDK harnesses automatically handle change detection and wait for the fixture to be stable after actions. ```typescript it('should mark confirmed when ok button clicked', async () => { const okButton = await loader.getHarness(MatButtonHarness.with({selector: '.confirm'}); expect(fixture.componentInstance.confirmed).toBe(false); expect(await okButton.isDisabled()).toBe(false); await okButton.click(); expect(fixture.componentInstance.confirmed).toBe(true); }); ``` -------------------------------- ### Apply Lowest Surface Container Background Color Source: https://material.angular.dev/guide/theming-your-components Use this class for surfaces needing the least emphasis against the main surface color. ```css .mat-bg-surface-container-lowest { background-color: var(--mat-sys-surface-container-lowest); } ``` -------------------------------- ### Secondary Background Color Utility Class Source: https://material.angular.dev/guide/theming-your-components Use this utility class for less prominent components that require a different color scheme than the primary. Ensure text and icons use the 'on-secondary' token for contrast. ```css .mat-bg-secondary { background-color: var(--mat-sys-secondary); } ``` -------------------------------- ### Read Material 2 Theme Colors and Type with Sass Source: https://material.angular.dev/guide/material-2-theming Use `get-theme-color` to read specific colors from primary, accent, warn, foreground, or background palettes. `get-theme-type` helps determine if the theme is light or dark, which can be used for conditional styling. ```scss $theme: mat.m2-define-dark-theme(...); $primary-default: mat.get-theme-color($theme, primary, default); $accent-a100: mat.get-theme-color($theme, accent, A100); $warn-500-contrast: mat.get-theme-color($theme, warn, 500-contrast); $foreground-text: mat.get-theme-color($theme, foreground, text); $background-card: mat.get-theme-color($theme, background, card); $type: mat.get-theme-type($theme); $custom-background: if($type == dark, #030, #dfd); ``` -------------------------------- ### Style Placeholder Based on Floating State Source: https://material.angular.dev/guide/creating-a-custom-form-field-control Apply CSS to control the opacity of placeholder elements. The placeholder is visible (opacity 1) when the host element has the 'floating' class, and hidden (opacity 0) otherwise. ```css span { opacity: 0; transition: opacity 200ms; } :host.floating span { opacity: 1; } ``` -------------------------------- ### Material Design Typography System Tokens Source: https://material.angular.dev/guide/theming-your-components Lists all available CSS variables for the Material Design typography system, including font, line-height, size, tracking, and weight properties for various categories and sizes. ```css --mat-sys-body-large --mat-sys-body-large-font --mat-sys-body-large-line-height --mat-sys-body-large-size --mat-sys-body-large-tracking --mat-sys-body-large-weight --mat-sys-body-medium --mat-sys-body-medium-font --mat-sys-body-medium-line-height --mat-sys-body-medium-size --mat-sys-body-medium-tracking --mat-sys-body-medium-weight --mat-sys-body-small --mat-sys-body-small-font --mat-sys-body-small-line-height --mat-sys-body-small-size --mat-sys-body-small-tracking --mat-sys-body-small-weight --mat-sys-display-large --mat-sys-display-large-font --mat-sys-display-large-line-height --mat-sys-display-large-size --mat-sys-display-large-tracking --mat-sys-display-large-weight --mat-sys-display-medium --mat-sys-display-medium-font --mat-sys-display-medium-line-height --mat-sys-display-medium-size --mat-sys-display-medium-tracking --mat-sys-display-medium-weight --mat-sys-display-small --mat-sys-display-small-font --mat-sys-display-small-line-height --mat-sys-display-small-size --mat-sys-display-small-tracking --mat-sys-display-small-weight --mat-sys-headline-large --mat-sys-headline-large-font --mat-sys-headline-large-line-height --mat-sys-headline-large-size --mat-sys-headline-large-tracking --mat-sys-headline-large-weight --mat-sys-headline-medium --mat-sys-headline-medium-font --mat-sys-headline-medium-line-height --mat-sys-headline-medium-size --mat-sys-headline-medium-tracking --mat-sys-headline-medium-weight --mat-sys-headline-small --mat-sys-headline-small-font --mat-sys-headline-small-line-height --mat-sys-headline-small-size --mat-sys-headline-small-tracking --mat-sys-headline-small-weight --mat-sys-label-large --mat-sys-label-large-font --mat-sys-label-large-line-height --mat-sys-label-large-size --mat-sys-label-large-tracking --mat-sys-label-large-weight --mat-sys-label-large-weight-prominent --mat-sys-label-medium --mat-sys-label-medium-font --mat-sys-label-medium-line-height --mat-sys-label-medium-size --mat-sys-label-medium-tracking --mat-sys-label-medium-weight --mat-sys-label-medium-weight-prominent --mat-sys-label-small --mat-sys-label-small-font --mat-sys-label-small-line-height --mat-sys-label-small-size --mat-sys-label-small-tracking --mat-sys-label-small-weight --mat-sys-title-large --mat-sys-title-large-font --mat-sys-title-large-line-height --mat-sys-title-large-size --mat-sys-title-large-tracking --mat-sys-title-large-weight --mat-sys-title-medium --mat-sys-title-medium-font --mat-sys-title-medium-line-height --mat-sys-title-medium-size --mat-sys-title-medium-tracking --mat-sys-title-medium-weight --mat-sys-title-small --mat-sys-title-small-font --mat-sys-title-small-line-height --mat-sys-title-small-size --mat-sys-title-small-tracking --mat-sys-title-small-weight ``` -------------------------------- ### Apply Small Border Radius (CSS) Source: https://material.angular.dev/guide/theming-your-components Use this class to apply a small border radius, suitable for components like text fields. ```css .mat-corner-sm { border-radius: var(--mat-sys-corner-small); } ``` -------------------------------- ### Apply Inverse Surface Background Color Source: https://material.angular.dev/guide/theming-your-components Use this class to make elements stand out against the default color scheme, suitable for temporary notifications. Text should use `inverse-on-surface` for contrast. ```css .mat-bg-inverse-surface { background-color: var(--mat-sys-inverse-surface); } ``` -------------------------------- ### Include Material 2 backwards compatibility styles for typography hierarchy Source: https://material.angular.dev/guide/material-2-theming Call the `typography-hierarchy` mixin with `$back-compat: true` to generate CSS classes that match M2 typescale names in addition to M3 names. This aids migration from M2 to M3. ```scss @use '@angular/material' as mat; $theme: mat.define-theme(); @include mat.typography-hierarchy($theme, $back-compat: true); ``` -------------------------------- ### Generate Drag and Drop Component Source: https://material.angular.dev/guide/schematics Generates a component utilizing the Angular CDK drag and drop directives for interactive list manipulation. ```bash ng generate @angular/cdk:drag-drop ``` -------------------------------- ### Material Design System Outline Tokens Source: https://material.angular.dev/guide/theming-your-components These CSS custom properties define the system tokens for outlines in Material Design. ```css --mat-sys-outline --mat-sys-outline-variant ``` -------------------------------- ### Control Label Floating Behavior Source: https://material.angular.dev/guide/creating-a-custom-form-field-control Use the `@HostBinding` decorator to apply the 'floating' class when the label should float. The label floats if the control is focused or not empty. ```typescript @HostBinding('class.floating') get shouldLabelFloat() { return this.focused || !this.empty; } ``` -------------------------------- ### Include typography styles for a component Source: https://material.angular.dev/guide/material-2-theming Use the component's typography mixin to include styles related to fonts, such as font family, size, weight, and line-height. This may need to be included multiple times for different font configurations. ```scss @include mat.checkbox-typography($theme) ``` -------------------------------- ### Define Multiple Themes in One File Source: https://material.angular.dev/guide/material-2-theming Define multiple themes within a single Sass file to support various themes without managing multiple CSS assets. Use `@include` within specific CSS rule declarations to control which theme applies. ```scss @use '@angular/material' as mat; // Define a dark theme $dark-theme: mat.m2-define-dark-theme(( color: ( primary: mat.m2-define-palette(mat.$m2-pink-palette), accent: mat.m2-define-palette(mat.$m2-blue-grey-palette), ), // Only include `typography` and `density` in the default dark theme. typography: mat.m2-define-typography-config(), density: 0, )); // Define a light theme $light-theme: mat.m2-define-light-theme(( color: ( primary: mat.m2-define-palette(mat.$m2-indigo-palette), accent: mat.m2-define-palette(mat.$m2-pink-palette), ), )); // Apply the dark theme by default @include mat.core-theme($dark-theme); @include mat.button-theme($dark-theme); // Apply the light theme only when the user prefers light themes. @media (prefers-color-scheme: light) { // Use the `-color` mixins to only apply color styles without reapplying the same // typography and density styles. @include mat.core-color($light-theme); @include mat.button-color($light-theme); } ``` -------------------------------- ### Small Label Typeface CSS Source: https://material.angular.dev/guide/theming-your-components Use for text in a badge. ```css .mat-font-label-sm { font: var(--mat-sys-label-small); letter-spacing: var(--mat-sys-label-small-tracking); } ``` -------------------------------- ### Generate Address Form Component Source: https://material.angular.dev/guide/schematics Generates a new Angular component for an address form using Material Design form controls and buttons. ```bash ng generate @angular/material:address-form ``` -------------------------------- ### Apply Theme Styles using CSS Variables Source: https://material.angular.dev/guide/theming-your-components Applies theme styles directly in a component's stylesheet using CSS custom properties for surface, on-surface, and typography. ```css .widget-a { background-color: var(--mat-sys-surface); color: var(--mat-sys-on-surface); font: var(--mat-sys-body-medium); } .widget-b { background-color: var(--mat-sys-primary); color: var(--mat-sys-on-primary); } ``` -------------------------------- ### Generate Table Component Source: https://material.angular.dev/guide/schematics Generates a component with a pre-configured Angular Material table, including datasource, sorting, and pagination. ```bash ng generate @angular/material:table ``` -------------------------------- ### Custom Control with Material Form Field Features Source: https://material.angular.dev/guide/creating-a-custom-form-field-control This snippet illustrates using a custom form field control with additional Material Design features like placeholders, required indicators, icons, and hints. ```html phone Include area code ``` -------------------------------- ### Apply Primary Text Color Source: https://material.angular.dev/guide/theming-your-components Use this class for text that needs to stand out, such as text buttons or selected tab labels. ```css .mat-text-primary { color: var(--mat-sys-primary); } ``` -------------------------------- ### Linear Mode Completion Logic Source: https://material.angular.dev/guide/creating-a-custom-stepper-using-the-cdk-stepper TypeScript code for managing the completion state of steps in a linear stepper. The `completeStep` method updates a boolean flag to enable progression. ```typescript export class AppComponent { completed = false; completeStep(): void { this.completed = true; } } ``` -------------------------------- ### Medium Display Typeface CSS Source: https://material.angular.dev/guide/theming-your-components Use for short, impactful text in hero sections or titles on larger screens. ```css .mat-font-display-md { font: var(--mat-sys-display-medium); letter-spacing: var(--mat-sys-display-medium-tracking); } ``` -------------------------------- ### Extract theme-based styles to separate Sass partial Source: https://material.angular.dev/guide/material-2-theming Splits component styles into a main SCSS file and a Sass partial (`_*-theme.scss`) for theme-based styles, by convention. ```scss // carousel.scss .my-carousel { display: flex; } .my-carousel-button { border-radius: 50%; } ``` ```scss // _carousel-theme.scss @mixin color($theme) { .my-carousel-button { color: blue; } } @mixin typography($theme) { .my-carousel { font-family: serif; } } ``` -------------------------------- ### Generate Tree Component Source: https://material.angular.dev/guide/schematics Generates a component that visualizes a nested folder structure using the Angular Material `` component. ```bash ng generate @angular/material:tree ``` -------------------------------- ### Apply All Component Theme Styles Source: https://material.angular.dev/guide/material-2-theming Applies theme styles for all Angular Material components using the `all-component-themes` mixin. This is a convenient way to include all component styles if your application uses most or all components. ```scss @use '@angular/material' as mat; $my-primary: mat.m2-define-palette(mat.$m2-indigo-palette, 500); $my-accent: mat.m2-define-palette(mat.$m2-pink-palette, A200, A100, A400); $my-theme: mat.m2-define-light-theme(( color: ( primary: $my-primary, accent: $my-accent, ), typography: mat.m2-define-typography-config(), density: 0, )); @include mat.all-component-themes($my-theme); ```