Sub topic 12
Using Sass in component styles
This chapter assumes you can declare component styles with styleUrl and styles, and that you know how Emulated encapsulation scopes CSS to a component. It shows how Sass fits into that model.
Sass runs before encapsulation
Sass is a CSS preprocessor. It adds variables, nesting, mixins and modules, and it compiles to plain CSS at build time. The Angular CLI picks the compiler from the file extension. A component that points styleUrl at an .scss file gets that file compiled first. Angular then applies view encapsulation to the resulting CSS.
Encapsulation therefore behaves the same with Sass as with CSS. The browser never sees Sass, and Sass has no effect on how Angular scopes styles.
Enabling Sass
New projects created with ng new --style=scss are already set up. In an existing project, rename the file and update styleUrl:
import { Component } from '@angular/core';
@Component({
selector: 'app-alert',
template: `<div class="alert error"><ng-content /></div>`,
styleUrl: './alert.component.scss',
})
export class AlertComponent {}Inline styles are treated as CSS by default. To write SCSS there, set inlineStyleLanguage in the build options of angular.json:
{
"projects": {
"my-app": {
"architect": {
"build": {
"options": {
"inlineStyleLanguage": "scss"
}
}
}
}
}
}The my-app key must match your project name.
Sharing tokens with `@use`
Put values that several components need into a partial. A partial is a Sass file whose name starts with an underscore. Load it with @use, the current module system. @import is deprecated.
// src/styles/_tokens.scss
$radius: 4px;
$error-bg: #fee2e2;
@mixin padded($size: 8px) {
padding: $size ($size * 2);
}// src/app/alert/alert.component.scss
@use '../../styles/tokens' as t;
:host {
display: block;
}
.alert {
@include t.padded;
border-radius: t.$radius;
&.error {
background: t.$error-bg;
}
}Nesting works with :host too. Writing :host { .alert { ... } } compiles to :host .alert, and Angular scopes it as usual.
If you would rather write @use 'tokens' than a relative path, add stylePreprocessorOptions.includePaths to the build options in angular.json:
{
"stylePreprocessorOptions": {
"includePaths": ["src/styles"]
}
}Common errors
- Emitting rules from a shared partial. Every component that loads a partial gets its own copy of any CSS rules in it. Keep partials to variables, functions and mixins, which produce no output until you use them.
- Expecting Sass variables to change at runtime.
$error-bgis replaced with a value during compilation. For themes that change in the browser, use CSS custom properties. You can set a custom property from Sass if you like, as in--card-background: #{t.$error-bg};. - Sass in inline
styleswithoutinlineStyleLanguage. Nesting and variables then reach the browser as invalid CSS, and the build usually fails. - A wrong
@usepath. Paths are relative to the file that contains the@use, not to the project root, unless you configuredincludePaths. - Duplicated global styles. If a component file loads the same partial that
styles.scssloads, only the variables are shared. The generated rules are not.
Exercise
Exercise
A component's SCSS file contains @use '../../styles/tokens' as t;, and _tokens.scss includes a .button { color: red; } rule. Two components load this partial. What is wrong with this setup, and how do you fix it?
Show answer
Each component's compiled CSS includes a copy of the .button rule, so the rule is duplicated and then scoped to each component. Partials shared through @use should only hold variables, functions and mixins. Move the .button rule into a component's own stylesheet or into the global styles.scss, and keep only reusable definitions in the partial.