Modify the attributes of a CSS class according to the chosen theme (selected within the user interface) in Angular 8

I have a custom-built application with Angular 8 where users can switch between two unique themes in the user interface. I have a specific div class that I want to change the background-color for each theme, but unfortunately I am having difficulty achieving this.

Is there anyone out there who has experience with implementing something similar and could offer some guidance?

This is how my app component HTML looks:

<div class="ui-layout">
    <div class="card">
        <div class="wrapper">


        </div>
    </div>
</div>

Here's a snippet from my app component SCSS file:

.card {
  background-color: #ffffff;
}

.black-blue-theme {
    .card { 
    background-color: #424242 !important;
    }
}

And finally, here is my app component TypeScript code:

  ngOnInit() {
        this.componentCssClass = 'light-indigo-theme';
        themes.current('generic.light');
        this.setupOverlayContainer();
        this.subscribeToObservableFooter();

    }

    setupOverlayContainer() {
        const containerElement = this.overlayContainer.getContainerElement();
        if (containerElement) {
            const classList = this.overlayContainer.getContainerElement().classList;
            const toRemove = Array.from(classList).filter((item: string) =>
                item.includes('-theme'),
            );
            classList.remove(...toRemove);
            classList.add(this.componentCssClass);
        }
    }

    changeTheme(type: string) {
        this.componentCssClass = type;
        if (type === 'black-blue-theme') {
            themes.current('generic.dark');
        } else {
            themes.current('generic.light');
        }
        this.setupOverlayContainer();
    }

Answer №1

Two themes are available, with one designated as the default option. Let's set up a system for switching between them.

app.component.ts

currentTheme: string = 'generic.dark';

changeTheme(type: string) {
    this.currentTheme = type;
}

app.component.html

<div class="ui-layout" [ngClass]="currentTheme">
    <div class="card">
        <div class="wrapper">


        </div>
    </div>
</div>

Answer №2

Finally fixed it. The key was making changes in app.component.scss instead of Myapp.component.scss

Here's the snippet from app.component.scss:

.black-blue-theme {
    .card { 
        background-color: #424242 !important;
    }
} 

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

Inheritance of Type Member in TypeScript

My data structure looks like this: export abstract class Person { ... } export class FPerson extends Person { a: A; b: B; } export class JPerson extends Person { c: C; } export class User { person: Person; } When dealing with the s ...

Encountered a TypeError in Angular 2 related to zone.addTask operation

My code suddenly stopped working and I have no clue why. HTML: <a class="nav_link" [routerLink]="['RowList']"> <svg><use xlink:href="#icon-list"></use></svg> List </a> JS: @RouteConfig([ {path: ...

Issue with forRoot method not triggering within Angular library

I have developed an Angular library with a static forRoot method to transfer static data from the application to the library. The purpose of creating a forRoot method is to effectively manage this process. export class DynamicFormBuilderModule { public s ...

"Encountering an issue with Angular2 where @Input is showing as undefined when attempting to bind

Perhaps it's normal to encounter this issue while testing this.myColor. It appears to be undefined, but why is that? Here's the mistake in my code: <h1 myDR [myColor]="red" > Test </h1> import {Component, Directive, Input, ...

Issues with Angular 2 Cli setup

Exploring the world of Angular 2, I decided to dive into the installation process of angular2 cli. However, upon executing 'ng new newapp' after installation, an unexpected error appeared where 'mg' was referenced rather than 'ng&a ...

How can I handle a queue in Angular and rxjs by removing elements efficiently?

I'm facing a challenging issue with my code that I need help explaining. The problem lies in the fact that a function is frequently called, which returns an observable, but this function takes some time to complete. The function send() is what gets c ...

A specialized WordPress template that loads just the header and footer

I have encountered a strange issue while trying to create a custom template for WordPress. The index.php file is working perfectly fine - all elements load without any issues. Here is the code in index.php: <?php get_header(); ?> <?php if ( is_ ...

Customizing the underlineFocusStyle of a material-ui TextField component using regular CSS styling

When working with Material-UI components, you have the option to provide a style object for the component itself within the JSX tag. However, in certain cases like the underlineFocusStyle of a TextField component, you need to use a separate style object. ...

Something is amiss with the PHP email validation functionality

I have been facing issues with the functionality of my form that uses radio buttons to display textboxes. I implemented a PHP script for email validation and redirection upon completion, but it doesn't seem to be functioning correctly. The error messa ...

Tips for including space at the beginning and end of a dynamically created table cell

My table is dynamically generated with contents drawn from a database, creating rows based on the data. Each cell has a rounded border and 2px padding for consistency. I want all cells to appear evenly spaced and padded vertically, but I'm facing an ...

Switch the designation to Hover Class

I am working with nested divs and have assigned a CSS class to one of the inner divs. How can I trigger the hover effect of the class (class.hover) when the user hovers over the outer div, even if they are not directly over the inner div? I believe this m ...

Is there a way to showcase all components on a single page in Angular 2 without needing to bootstrap them in appModule?

Is there a way to showcase all my Angular 2 components on one page without having to bootstrap them individually in AppModule.ts? I'm looking for a solution using router-outlet or parent-child relationships. Any guidance on how to achieve this after s ...

Troubleshooting Angular Build Errors: Integrating Three.js

Upon setting up a new Angular application and integrating three along with @types/three, I proceeded to create a basic component. However, upon executing ng build --prod, the following errors are displayed: ERROR in node_modules/three/src/core/BufferAttri ...

Include previous input as a "phantom" thumbnail to the slider element of type "range"

https://i.stack.imgur.com/JnuCN.png Using a tutorial from w3schools, I have customized a regular range slider. The objective is to send a new value command to an external MQTT-based home automation system and display the previous value as a ghost-thumb in ...

Tips for modifying link height using CSS

I'm currently facing an issue with adjusting the height of "footer a" links that are using Fontface vector icons in the footer. I have added a red border around them to visually identify the problem. Ideally, when navigating through the links, the bor ...

An issue has been detected by Zone.js where the ZoneAwarePromise `(window|global).Promise` has been unexpectedly replaced

I have recently integrated the Angular2 quickstart code into my existing webpack setup, but I seem to be facing an issue where something is interfering with the promise from zone.js, resulting in an error. Based on my research on Stack Overflow, it appears ...

Issue: Unable to resolve all parameters for setupRouter function

I encountered an error stating "Can't resolve all parameters for RouteParams" while setting up a basic app for routing. Here is how my app.module.ts file is structured: import { NgModule } from '@angular/core'; import { BrowserModule ...

Change the background color of mat-select to match the selected mat-option

<mat-form-field> <mat-select [formControlName]="name"> <mat-option [value]="option.id" [ngStyle]="{backgroundColor: option.color}" *ngFor="let option of list; let i = index" ...

Utilizing Angular Services to Share Events and Reusing Components Multiple Times on a Page

My unique custom table is made up of multiple components, each emitting events using a shared service called TableEvent. These events are subscribed to by a class named TableTemplate, which facilitates communication among the different components of the ta ...

Is there a way to determine if a browser is operating in compatibility mode?

Is there a way to determine if the browser is in compatibility mode or not? I need to apply different CSS depending on the mode. ...