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

What would be the best way to display a label once the zoom level exceeds a certain threshold in Leaflet?

As someone new to the Leaflet library and JavaScript in general, I'm currently facing a challenge with showing/hiding a leaflet label based on the zoom level. The markers are within a 'cluster' layer loaded via AJAX callback, and I bind the ...

When using Webpack, there may be difficulties resolving relative path import of express static files

I am currently developing an Outlook add-in with an Express server running. To ensure compatibility with Outlook Desktop, I need to transpile JavaScript to ES5 using Webpack. Below is the simplified structure of my project: /public /javascripts ssoAu ...

Issues with click events in the navigation menu

How can I make my menu close when clicking on other parts of my website, instead of opening? I know that I should use a click event for this, but when I implemented a click event, my menu encountered 2 unwanted problems: 1- Whenever I clicked on a menu i ...

Javascript Using Promise to Await Ajax Content Loading

As a newcomer to JavaScript, I am exploring ways to traverse a DOM that utilizes Checkboxes for filtering results and displays them through Ajax. The checkboxes are organized in 4 levels: Grand Parent Parent Child Grand Child My goal is ...

Creating a dropdown navigation menu using jQuery

I have been experimenting with creating a customized Drop Down menu using ul li and Jquery, following this helpful Tutorial. Here is the sample HTML Code for testing: <div id="dd" class="wrapper-dropdown-3" tabindex="1"> <span>OS</span> ...

The requested resource lacks the 'Access-Control-Allow-Origin' header in a basic HTML form

Can someone help me understand why I keep encountering this error in my basic HTML form? I am attempting to display XML data on my website to showcase news stories, but unfortunately, I keep getting stuck with this persistent error. Any assistance would ...

Transfer a csv file from a static webpage to an S3 bucket

Looking to create a webpage for uploading csv files to an S3 bucket? I recently followed a tutorial on the AWS website that might be helpful. Check it out here. I made some modifications to accept filename parameters in my method, and everything seems to ...

Guide on integrating the @nuxtjs/axios plugin with Nuxt3

I'm trying to fetch API data from using this code: <template> <div> </div> </template> <script> definePageMeta({ layout: "products" }) export default { data () { return { data: &apo ...

Laravel does not have the capability to provide a genuine json response

I am facing an issue with my Laravel controller. The code is straightforward: class MyController extends Controller { public function list() { return response()->json(['a' => 'hello']); } } When I access the ...

Semi-Transparent Photo Slideshow

Currently, I am in the process of developing a Pokedex-like feature for a project that I am working on. The functionality is working as expected, but there is one particular feature that I would like to implement. My goal is to display only certain element ...

Having trouble sending values via POST request in Node.js using Express

Currently, I am in the process of learning how to use Express (v4) with Node.js. My main goal right now is to create a basic REST API. This API specifically focuses on one endpoint: /orders. The main functionality I am trying to achieve is the ability to r ...

Getting data from an API with authorization in Next.js using axios - a step-by-step guide

click here to view the image user = Cookies.get("user") I'm having trouble accessing this pathway. I stored user data, including the token, using cookies. Please assist me with this issue. ...

Generating a customizable PDF using jSPDF

I am in need of creating a dynamic PDF document with text content that could change along with their positions. The header columns are defined as an object array below: [ { "title": "Occupied Vacancies", "dataK ...

Transforming data in javascript

I am faced with a data transformation challenge involving extracting information from user input in Apache Superset using metrics. The data is assigned to the variable dataTransformation. {country: "Afghanistan", region: "South Asia", y ...

I am experiencing difficulties in accessing the DOM

I have implemented jQuery $.ajax to dynamically load data into table rows as shown below: <table id='row-data'> <tr><td>1001</td></tr> <tr><td>1322</td></tr> <tr><td>15 ...

Comparison of element state prior to and post editing (with contentEditable)

Exploring how elements within a div can be monitored for changes made by the user (thanks to contentEditable), I created a sample page with the following setup: before_html = $("#example_div").children(); $("#differences_button").on("click", ...

What is the best way to configure maxSockets in Node.js while working with Express?

Can the maximum number of sockets in Node.js be adjusted while working with the Express framework? More information can be found here. ...

Building a custom onChange event handler in Formik allows for greater

I want to modify the onChange function in formik input so that it converts the value from a string to a number. However, I'm unable to change the behavior as expected and the console.log doesn't show up on the screen. It seems like Formik's ...

Issues with the functionality of Bootstrap

Upon loading the page, I am encountering an issue with a collapse on the 'main' id. It fails to collapse initially and only functions correctly after being clicked. I have not utilized 'collapse in', so I am unsure why this behavior per ...

Load the page's content gradually, without needing to wait for all the content to be fully loaded

I am facing an issue with a webpage that displays 10 different items, each of which has a slow loading time. Currently, the entire page waits for all 10 items to fully load before displaying anything. I am interested in finding out if it is feasible to sh ...