I am trying to toggle the dark mode when a user toggles the switch, which is in the header component. See image below:
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNavAltMarkup" aria-controls="navbarNavAltMarkup" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarNavAltMarkup">
<div class="navbar-nav">
<a class="nav-item nav-link active" href="/">Home <span class="sr-only">(current)</span></a>
<a class="nav-item nav-link" href="/about">About</a>
<a class="nav-item nav-link" href="/contact">Contact</a>
<a class="nav-item nav-link" href="/resume">Resume</a>
</div>
</div>
<div class="custom-control custom-switch">
<input (change)="onChangeToggle()" type="checkbox" class="custom-control-input" id="toggleTheme">
<label class="custom-control-label" for="toggleTheme">Dark Mode</label>
</div>
</nav>
My header component ts file holds a boolean value called setDark which is triggered in an event emitter through the Output decorator :
// header.component.ts
import { Component, OnInit , Output, EventEmitter} from '@angular/core';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
@Output() mode = new EventEmitter<boolean>();
setDark = false;
constructor() { }
ngOnInit(): void {
}
onChangeToggle() {
this.setDark = !this.setDark;
this.mode.emit(this.setDark);
console.log(this.setDark);
}
}
Then in the app.comoonent.html receive boolean
<app-header (mode)="receiveMode($event)"></app-header>
<router-outlet></router-outlet>
// app.component.ts
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
setMode = false;
receiveMode($event) {
this.setMode = $event;
console.log("MODEEEE", this.setMode);
}
title = 'about-me';
}
I have tried to wrap the div around the app-header and router-outlet like so:
<app-header (mode)="receiveMode($event)"></app-header>
<div class="darkTheme">
<router-outlet></router-outlet>
and using ngClass:
<div [ngClass]="{
darkTheme: setMode
}">
</div>
But it doesn't work. I did check to see whether or not the boolean is being logged into the console when toggling the switch. It is.
Have a look at this stackblitz: https://stackblitz.com/edit/angular-rubaka?file=src/app/toolbar-multirow-example.html
it toggles the class "dark-theme" on the #container:ElementRef:
<div #container style="padding: 4rem;" class="mat-typography dark-theme">
The styles.scss defines two different theme's on line 28 (default theme)...
// Include the default theme styles.
@include angular-material-theme($candy-app-theme);
and on line 39 (dark theme)...
.dark-theme {
@include angular-material-theme($dark-theme)
};
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With