liveBook · Manning

Sass

As explained in Pro Angular 16

SCSS is a CSS-compatible stylesheet syntax that supports Sass features such as variables, functions, and nesting.

Sass is a CSS superset for building complex styles without endlessly duplicating CSS properties. SCSS files use the .scss extension, remain compatible with CSS, and compile during an Angular project build into stylesheets browsers understand. section 28.3

Use Sass features to organize component styling and connect custom components to a theme:

  • Import Sass and Angular Material features with @use, then access namespaced variables and functions such as material.$indigo-palette. section 28.3.2
  • Define primary, accent, and warning palettes from Angular Material base palettes, and configure typography for generated styles. section 28.3.2
  • Generate related selectors with @each, nesting, and interpolation, then retrieve palette colors and typography values through theme functions. section 28.3.2
  • Inspect package SCSS and browser developer tools when theme APIs or generated styles are difficult to understand. section 28.3.2

Themed custom button

A customButton.component.scss file imports Angular Material, defines three palettes, configures typography, and styles button.custom-button-primary, button.custom-button-accent, and button.custom-button-warn through an @each loop. Palette functions supply background and contrast colors, while nested font properties use the configured button typography. A disabled selector applies the theme's disabled colors. The Angular component attaches the matching class from its themeColor input.

Listing 28.19. The customButton.component.scss file in the src/app/core folder
@use "@angular/material" as material;
 
$primary: material.define-palette(material.$indigo-palette);
$accent: material.define-palette(material.$pink-palette, A200, A100, A400);
$warn: material.define-palette(material.$red-palette);
 
$typography: material.define-typography-config();
 
button[class*="custom-button-"] {
    padding: 7px 12px;
    border: none;
    border-radius: 4px;
    margin: 2px;
}
 
button.custom-button {
    @each $name, $palette in (primary: $primary, 
            accent: $accent, warn: $warn) {
        &-#{$name} {
            background-color: 
                material.get-color-from-palette($palette, default);
            color: material.get-color-from-palette($palette, 
                default-contrast);
            font: {
                family: material.font-family($typography, button);
                size: material.font-size($typography, button);
                weight: material.font-weight($typography, button);
            } 
        }
    }    
}
 
$bg: material.$light-theme-background-palette;
$fg: material.$light-theme-foreground-palette;
 
:host[disabled] button[class*="custom-button-"], 
         button[class*="custom-button-"]:disabled  {
    background-color: 
        material.get-color-from-palette($bg, disabled-button);
    color: material.get-color-from-palette($fg, disabled-button);
}

Inspect the complete SCSS listing to see imports, palette variables, typography configuration, shared button rules, generated theme classes, and disabled-state styling together. section 28.3.2

How Pro Angular 16 covers it

How deep it goes
Explains compilation, variables, functions, @use, palettes, typography, nesting, and loops, but defers comprehensive Sass language coverage.
Examples
Uses a themed custom button with generated primary, accent, warning, typography, and disabled-state selectors.
What it assumes
Assumes CSS and Angular component context, while introducing SCSS syntax, Sass variables, functions, palettes, and @use usage.
What you can do after
Apply it via Angular build tools to generate themed custom-component CSS from SCSS.
How it connects
Connects CSS, SCSS, Angular build tools, Angular Material themes, CSS selectors, palettes, typography, and browser developer tools.

Also explained in

Compare how 2 books explain Sass →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro Angular 16. The section links point to the parts of the book it draws on.