Angular Material style service

Recently, I've been tinkering with a service that allows me to alter the color scheme of my project in Angular Material. Following the official documentation, I managed to change the color successfully. However, I hit a roadblock when trying to revert back to the original color scheme. I understand that toggling between true and false on a div element can affect the color, but is there a way to incorporate this logic directly into the service?

As a beginner in coding, any guidance would be greatly appreciated. Thank you.

custom-css

.defaultTheme {
 --background: red;
}

.green {
 --background: green;
}

service

currentTheme: string = 'defaultTheme';

constructor() {
this.switchColors(this.currentTheme);
}

switchColors(newColor:string) {
 document.body.classList.remove(this.currentTheme);
 this.currentTheme = newColor;
 document.body.classList.add(this.currentTheme);
}

typescript-file

themeSwitch() {
 this.service.switchColors('green');
}

index.html

<div (click)="themeSwitch()">
 <p> test color </p>
</div>

Answer №1

My suggestion would be to consider utilizing a toggle button or radio button for managing this task. However, you can experiment with the following code:

switchColors(newColor:string) {
 let temp = this.currentTheme == 'defaultTheme' | newColor : 'defaultTheme';
 document.body.classList.remove(this.currentTheme);
 this.currentTheme = temp;
 document.body.classList.add(this.currentTheme);
}

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

Setting a pre-selected value in a Vue.js dropdown list involves a few simple steps. This

Currently, I am developing two Vue components. I am sending array data from the parent component to the child component using props. Now, I have a requirement to pre-select a value in the dropdown list of the child component. Below is a snippet of my code ...

Encountering issues when utilizing the 'got' package

I encountered an issue while using 'got' version '11.8.3', resulting in the following error: /app/node_modules/got/dist/source/core/index.js:696 throw new TypeError('The payload has been already provided'); ^ TypeError: The p ...

The expected input should be either an HTMLElement or an SVGElement, but the received input is currently null

Below is the code for a component: function SignUpPage() { return ( <> <h1>Sign Up</h1> <input name="userName" /> </> ); } export default SignUpPage; Testing the component: it("should c ...

Using Javascript to make a call to a RESTful endpoint

My current challenge involves attempting to make a call to the Spotify API from JavaScript: function callAPI() { var xhttp = new XMLHttpRequest(); xhttp.open('GET', 'https://api.spotify.com/v1/search?q=Muse&type=track'); ...

Another inquiry regarding a city autocomplete field in a global setting

While I understand that this question may have been previously asked, I have spent several days searching without finding a satisfactory answer. Some websites, such as eventful.com, etc., have an autosuggest city field with cities from all over the world ...

Displaying Bootstrap 4 dropdown menu on hover

I've noticed that the dropdown menus in Bootstrap 4 are triggered by click by default, but for some reason, my menu is appearing on hover without any additional CSS or JS. This poses a problem for mobile development. I could create a jQuery solution, ...

What is the best way to differentiate between the legend and chart when working with three separate

I have encountered an issue with my pie charts. The first chart has 10 legends displayed below it, while the second chart only has 4 legends. When I adjust the padding to accommodate the 10 legends in the first chart, the names of the 4 legends in the se ...

Leverage Express.js to make a nested request and retrieve JSON data from a secondary backup URL

There are two URLs that contain a .json.gz file - var primaryURL = "http://primary.s3bucket.com/posts.json.gz"; var secondaryURL = "https://secondary.s3bucket.com/posts.json.gz"; By utilizing the request module, I am able to successfully retrieve the jso ...

What could be causing the Angular input [value] to execute the function() repeatedly?

While working on a simple code snippet to display input values from an array in a Typescript HTML template, I encountered an issue where the data wasn't loading quickly enough, resulting in errors until it was fully loaded. To solve this problem, I ad ...

Organizing a series of objects into groups of four for processing

I have a task of organizing an array of objects that represent game players by assigning each player to a group number based on their current group value. The challenge is to ensure that each group has as close to four players as possible, while also acco ...

Show blank value if there are no search results, along with an additional menu option

I am currently working on a Typeahead feature with a customized menu using the renderMenu method. In this setup, I have added 2 custom menu items at the bottom - one divider and a button. An issue arises when there are no search results. If I do not inclu ...

Adjust the text within the treeview node for proper alignment

My treeview has nodes that display text, but when the length of the text increases, it moves to the next line and starts one place before the upper text, causing alignment issues. Is there a way to use CSS or JavaScript to properly align the text? Regards ...

Understanding NestJS Mixins and Their Distinction from Inheritance

After researching, I found that the Nestjs documentation does not include any information about mixins. Here is what I have gathered from my findings on Google and Stack Overflow: A mixin serves as a means of code sharing between classes within Nest. Esse ...

Encountered an issue while setting up ng-circle-progress in Angular: Unable to find an exported member in

I am currently working on incorporating the ng-circle-progress functionality by referring to the documentation at https://www.npmjs.com/package/ng-circle-progress. Here is a snippet from the .ts file: import { Component } from '@angular/core'; i ...

Guide on implementing a text display switch using CSS

I am working on two radio type menus to allow users to select different payment methods which will reveal corresponding information. However, I am facing a challenge in implementing the Precautions content below. Can someone guide me on how to achieve this ...

Is there a way to ensure that both new Date() and new Date("yyyy-mm-dd hh:mm:ss") are initialized with the same timezone?

When utilizing both constructors, I noticed that they generate with different timezones. Ideally, they should be in the same timezone to ensure accurate calculations between them. I attempted to manually parse today's date and time, but this feels li ...

Utilizing the subclass type as a parameter in inheritance

Looking for a way to restrict a function in C# to only accept classes of a specific base class type? In my current implementation, I have a base class (which can also be an interface) and n-classes that extend it. Here is what I am currently doing: abstr ...

What is the best way to incorporate various styles into one :style attribute?

Within my vuetify project, I encountered a challenge of adding multiple styles to the same style attribute. Specifically, I have successfully implemented a vuetify breakpoint and now wish to include {backgroundColor:'Color'} within the existing a ...

How can I integrate keydown.control with a unique click function in Angular?

Is there a way to choose multiple number elements in random order and save them to an array by holding down the control key (CTRL) and clicking on the element? For example, selecting 2 and 4 out of 5. I tried different methods but couldn't figure out ...

Is there a way to make a button on a single div only affect that specific div

I have a PHP query that echoes a div for each row in the table. I want the div to slide up and then, when the user clicks the "read more" button, the div slides down. However, since it is echoed in a loop, all the divs have the same IDs and classes. I wo ...