Is it possible to implement mat-color in Angular's theme?

Incorporating an Angular 9 theme into my app, I encountered the need to utilize mat-color lighter and darker functions (for color and background respectively). While I have successfully implemented this in the past with a custom theme, doing so with an Angular theme has proven challenging.

For reference, I have included the following code snippet:

background-color: mat-color($color-error, lighter);
color: mat-color($color-error, darker);

This snippet is included in my CSS file, where $color-error represents a variable I defined. Unfortunately, the desired colors are not appearing in the class as intended.

Is there a way to make use of lighter or darker with mat-color while working within an Angular theme?

Thank you

Answer №1

Check out this example I put together on stackblitz to see how you can create a theme in angular with lighter and darker color variations based on the palette color.

Below is the key code snippet:

theme.scss:

@import '~@angular/material/theming';

@include mat-core();

$custom-primary: mat-palette($mat-teal);
$custom-accent:  mat-palette($mat-pink, A200, A100, A400);
$custom-warn:    mat-palette($mat-red);
$custom-theme:   mat-light-theme($custom-primary, $custom-accent, $custom-warn);

@include angular-material-theme($custom-theme);

This file creates a custom theme and sets a light theme.

variables.scss:

@import './theme.scss';

$primary: mat-color($custom-primary);
$primary-lighter: mat-color($custom-primary, lighter);
$primary-darker: mat-color($custom-primary, darker);

In the above code, three variables are defined containing the primary color, a lighter shade, and a darker shade based on the primary color specified in the theme.

Note that when defining lighter and darker colors, they must be derived from a color within your palette. You cannot simply select any random color (even if it's an Angular color). For instance, for a red color, you would need to use $custom-warn. If you still require to lighten or darken a color outside of the palette, you can utilize SCSS mixins like lighten and darken:

lighten($myColor, 30%), same goes for darken.

To implement this theme and variables, make sure to import theme.scss in

styles.scss</code, and wherever you want to use your custom variables, include <code>variables.scss
- that's all you need to do.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating an interactive element in Angular: A step-by-step guide

Hey there, I'm facing a problem with creating a specific payload structure as shown in the example code snippet below. Everything was going well until I got stuck at the dynamic form part, and despite several attempts, I can't seem to figure it o ...

Angular 10 - Compilation errors caused by the element's location

When running 'ng serve' or 'ng build' in Angular 10, I encountered a build error that stated: ERROR in projects/project-navigator/src/app/modals/building-permissions.component.html:89:61 - error NG8002: Can't bind to 'ngClass& ...

Problem encountered with ESLint when utilizing babel module-resolver in conjunction with TypeScript

Typescript is not making type inferences with resolved imports, but it does with relative imports. Any assistance would be greatly appreciated. https://i.sstatic.net/2pgHX.png When using the 'useTheme' function, an error is displayed: "U ...

Trouble with storing data in Angular Reactive Form

During my work on a project involving reactive forms, I encountered an issue with form submission. I had implemented a 'Submit' button that should submit the form upon clicking. Additionally, there was an anchor tag that, when clicked, added new ...

Verifying the existence of an optional property on my component using Jest

One of the props in my component is called jsonpayload, and it is optional. Here's how it looks in the interface: export interface props { jsonpayload?: payload[] onclick: () => void; } My Jest file: const test_prop: dummy_props ...

Dealing with Promise Wrapper Already Declared in TypeScript Return TypeMismatch

Here is a snippet of TypeScript code I am working with: type Chain = 'eth' export type ApiMethods = { getToken: { path: 'tokens/' (args: { chain: Chain tokenAddress: EthereumAddress }): string } getRank: ...

Ways to utilize gridster by accessing the nativeElement of ElementRef

This issue is specific to the Firefox browser and does not occur in Chrome. My goal is to access the nativeElement for the gridster element. The version of angular-gridster2 being used is v17.0.0. In the HTML code: <gridster [options]="opt ...

Is it possible to transform the original object while converting between different types in Typescript?

Consider having two distinct interfaces: interface InterfaceOne { myString: string, myNum: number } interface interfaceTwo extends InterfaceOne { myBool: boolean } Utilizing the TypeScript code below: let somedata: interfaceTwo = { my ...

Ensure that any changes to the FormControl are detected and trigger an Observable change without requiring user interaction, using .distinctUntilChanged

In my application, I have implemented a FormControl/service that updates when the user types input into a field. Here's how it works: term = new FormControl(); page = new BehaviorSubject(1); .. .. // Calling the search function search(th ...

Utilizing UseRef in Typescript for an Idle Timer

Feeling frustrated with Typescript, everything seems unnecessarily complicated. Trying to follow a tutorial on react-idle-timer and encountering TypeScript compilation issues within minutes. The guides online suggest using when calling useRef with TypeS ...

Leveraging components from external modules within sub-modules

So, I've created a module to export a component for use in different modules. However, I'm facing an issue with how to properly utilize this component within child modules of another parent module. While I have imported the first module into the ...

What impact does introducing a constraint to a generic type have on the inference process?

Let's take a look at this scenario: function identity<T>(arr: T[]) { return arr } identity(["a", "b"]) In the above code snippet, the generic type T is inferred as string, which seems logical. However, when we introduce a ...

Guide to building a nested dropdown navigation in Angular 12 with the power of Bootstrap 5

I am looking to implement a multilevel dropdown feature in my Angular 12 project with Bootstrap 5. Can someone please guide me on how to achieve this? For reference, you can view an example here. Thank you in advance! ...

What are the appropriate scenarios for extending and utilizing an abstract class in Angular?

@Component({ selector: 'my-component', template: `<ng-content></ng-content>`, providers: [ { provide: AbstractClass, useExisting: forwardRef(() => TargetComponent) } ] }) export class TargetComponent extends AbstractCla ...

Troubles encountered with fetching pre-filled values in Angular form automation

In my project, there is a template driven angular form for the Edit profile section in the app. Data is populated from the API for the form, with fields being empty if no data is available. When a user types new data into the fields, it is successfully pos ...

Removing a value from an array of objects in Angular 2

There is a single array that holds objects: one = [ {name: 'Name', key: '4868466'}, {name: 'Name', key: '4868466'}, {name: 'Name', key: '4868466'}, {name: 'Name', key: & ...

Extracting information from console output and displaying it in a table with angular2

https://i.stack.imgur.com/BMt6J.pngI am facing an issue with retrieving data from the console output and populating it into an HTML table. Can someone please assist me with this? Here is the HTML Code: <table class="table"> <tr> <t ...

Exploring the Method of Utilizing JSON Attribute in typeScript

How to access JSON attributes in TypeScript while working on an Angular Project? I'm currently in the process of building an Angular project and I need to know how to access JSON attributes within TypeScript. test:string; response:any; w ...

Incorporating types and optional arguments in Typescript programming

Develop a program using JavaScript to display all the properties of a given object Example object: var student = { name : "David Rayy", sclass : "VI", rollno : 12 }; Your task is to enhance this program by incorporating typing, optional arguments, an ...

Issue with obtaining access token in Angular 8 authentication flow with Code Flow

As I work on implementing SSO login in my code, I encounter a recurring issue. Within my app.module.ts, there is an auth.service provided inside an app initializer. Upon hitting the necessary service and capturing the code from the URL, I proceed to send a ...