Ways to update the color of the mat-dialog-title using the material theme

Currently, I am utilizing the Angular Material Dialog and have been attempting to dynamically change the title color of the dialog based on the material theme.

<h1 mat-dialog-title color="primary">{{ title }}</h1>

Even though setting the color using CSS works fine:

[mat-dialog-title] {
  background: #369;
}

I really need to set it as primary, accent, etc., just like I can apply to the mat-buttons. I attempted to adjust some settings in the scss file but couldn't figure out how to change the color using variables such as $primary. Is there a way to set the color as the primary color of the current theme through variables?

Answer №1

Enhance the appearance of dialogs and other overlays by introducing a new theme class to the overlay. Here are two themes to choose from, with the second theme activated by adding the 'alternate-theme' class to the root node.

@import '~@angular/material/theming';
@include mat-core();

$primary-palette: mat-palette($mat-pink, A200, 900, 50);
$accent-palette: mat-palette($mat-light-blue, 500, 900, A100);
$warn-palette: mat-palette($mat-red, 700, 900, A100);
$basic-theme: mat-dark-theme($primary-palette, $accent-palette, 
$warn-palette);
@include angular-material-theme($basic-theme);

$alt-primary-palette: mat-palette($mat-teal);
$alt-accent-palette: mat-palette($mat-pink, 600);
$alt-warn-palette: mat-palette($mat-amber, 700, 900, A100);
.alternate-theme {
  $alternate-primary: $alt-primary-palette;
  $alternate-accent: $alt-accent-palette;
  $alternate-warn: $alt-warn-palette;
  $alternate-theme: mat-light-theme($alternate-primary, $alternate-accent, $alternate-warn);
  @include angular-material-theme($alternate-theme);
}

However, some Angular components like dialogs are treated differently. To apply the 'alternate-theme' class to such overlays, you can do this:

constructor(private overlay: OverlayContainer) {}

Then, when switching themes:

 switchTheme(theme: string) {
   if (theme === 'light') {
     this.overlay.getContainerElement().classList.add('alternate-theme');
   } else {
     this.overlay.getContainerElement().classList.remove('alternate-theme');
   }
 }

Answer №2

If you need to make changes to multiple elements, you can utilize ngStyle. However, if your sole purpose is to change the color, you can achieve it like this:

<h2 mat-dialog-title [style.color]="myNewColor">{{ heading }}</h2>

For a more in-depth explanation, check out this resource:

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

Interact with HTML style attribute using JavaScript

Is there a way to retrieve a specific CSS property from the style attribute of an HTML element, without considering the stylesheet or computed properties? For example: <div style="float:right"></div> function fetchStyleValue(element, propert ...

Tips for implementing a controlled RadioGroup in React: Mapping through an array of values to dynamically populate radio buttons

I have a scenario where I am utilizing a pre-defined set of arrays to populate multiple RadioGroups. The component hierarchy in the codesandbox is structured to resemble that of my actual project. Whenever I select a radio button, I receive an error messa ...

Utilizing HttpClient in a static method service with Angular

One of my services contains a static method that I use for some initialization treatment. Within this method, I need to retrieve data from a web service @Injectable() export class FeaturesInitializationService { static allowedFeaturesModul ...

Iterating through an array with a .forEach loop and referencing the variable name

Currently, I am working on a project using JS/React and dealing with nested arrays in an array. The structure of my data looks like this: const ezra = ["Patriots", "Bears", "Vikings", "Titans", "Jets", "Bengals"] const adam = ["Chiefs", "Cowboys", "Packer ...

Are there any restrictions on the amount of data that can be included in a Sankey diagram created from an Excel sheet? I would

[please provide a description of the image][1]I am encountering an issue with data limitation in plotting a Sankey diagram from an Excel sheet. I have imported an Excel sheet with 1300 rows of data, but I am only able to plot 12 rows of data. Can anyone pl ...

What strategies can I use to optimize the code for the function provided?

This function allows for the dynamic display of select tags for location selection based on employee designation. The current code functions correctly, but I believe it can be optimized further. // Function to activate different location options based on e ...

Creating a simple bootstrap code for developing plugins in Wordpress

After successfully coding in Brackets with the Theseus plugin on my local machine, I encountered a problem when attempting to transfer my code to a Wordpress installation. The transition from Brackets to Wordpress seems far more complicated than expected. ...

Simple Way to Modify Color of Active Page Link - HTML, CSS, and JavaScript

I found a helpful example on how to change link colors for the current page here. Here is the script I added: <script> // current page highlight $(document).ready(function() { $("[href]").each(function() { if (this.href == window.l ...

Issue with Next.js: Setting the width to 100vh prevents the height from being responsive on mobile devices

While I understand that using height: 100vh on mobile is generally discouraged for various reasons, I'm curious as to why height: 100vh paired with width: 100vh in Next.js doesn't produce the expected result. Instead of a full-height square, I en ...

the iframe is not properly displaying the embedded responsive page

The primary webpage incorporates the responsive mobile page created with the JQUERY SUPERSLIDES plugin. The mobile page appears correctly when viewed directly on an iPhone using Safari. However, when it is embedded in an iframe, it does not display as expe ...

Issue: The Karma plugin in Angular CLI >6.0 is now exported from "@angular-devkit/build-angular" instead

Issue: I'm encountering an error in Angular CLI version 6.0 or higher where the Karma plugin is now exported by "@angular-devkit/build-angular". // Below is the Karma configuration file, you can find more information at // module.exports = function ...

"Why is it that the keypress event doesn't function properly when using the on() method

My goal is to capture the enter event for an input field $("input[name='search']").on("keypress", function(e){ if (e.which == '13') { alert('code'); } }); This is the HTML code snippet: <input name="searc ...

HTML and CSS styled accordion with nested checkboxes: handling long items

I am currently working on creating a multi-level accordion for an aside navigation section on a webpage. However, I have encountered an issue where the background color extends too far when hovering over a specific element. This occurs on a folder-type ite ...

The assets path is the directory within the installed package that houses the main application files following the completion of a

I have a Vue.js UI component that is internally built using webpack. This reusable UI component library references its images as shown below: <img src="./assets/logo.png"/> <img src="./assets/edit-icon.svg"/>   <i ...

Utilizing Multiple Select Options and CSS Float Styling in Mozilla Firefox

I have been attempting to customize the CSS of a multiple select element. While I am satisfied with how it looks on Chrome, I am facing issues getting it to work correctly on Mozilla Firefox. $('option').mousedown(function(e) { ...

creating a randomized location within an HTML element using JavaScript

Trying to figure out how to randomly generate a position within an HTML div. I've come up with a JavaScript function that looks like this: function randomInRange(min, max) { return(Math.floor((Math.random() * (max - min) + 1) + min)); } My ...

Using CSS3 to target the final <li> element within the main list in a nested <ul> structure

I have a complex list structure: <ul> <li>Item</li> <li>Item</li> <li>Item</li> <li> <ul> <li>Nested Item</li> <li>Nested Item</li> <li>L ...

Why doesn't package.json typically utilize the preset values stored in the .npmrc file?

Windows 10 x64 Based on the information found here, I created a file called C:\Users\bushm\.npmrc with the following content: author = "Andrey Bushman" However, when I run the command npm init -y in a new directory, I noticed that the pac ...

Display the input text line by line

How can I achieve the desired output for this input parameter? displayText("you and me"); expected output: ["you and me", "you and", "and me", "you", "and", "me"] I have attempted ...

On my website, two elements are stacked on top of each other

The CSS framework I am currently utilizing is Materialize Whenever I try to add a new element, it inexplicably appears below the ones above it. I did not specifically instruct it to be positioned underneath or below the cover container. Adding a z-index c ...