Super Sale Limited Time 50% OFF for All-Access Plans
Save 50% Now

Switch between light and dark mode without reloading page with Angular template


Hi there, I need help:
I need to switch between light and dark mode on metronic angular template without refreshing the page, is there a way on doing that?

I've noticed that there is an injectable service that sets the mode on localStorage but afterwards it reloads the page. I need it to toggle without the dropdown also, only clicking on it.

I couldn't find any topics in the KeenThemes docs or forum.

Angular version: 14.0.0
Metronic template: demo1

Code snippet from the service:

public switchMode(_mode: ThemeModeType) {
if (localStorage) {
const updatedMode = _mode === "system" ? systemMode : _mode;
localStorage.setItem(themeModeLSKey, updatedMode);
localStorage.setItem(themeMenuModeLSKey, _mode);
}
document.location.reload();
}


Component:

import { Component, Input, OnInit } from "@angular/core";
import { Observable } from "rxjs";
import { ThemeModeService, ThemeModeType } from "./theme-mode.service";

@Component({
selector: "app-theme-mode-switcher",
templateUrl: "./theme-mode-switcher.component.html",
})
export class ThemeModeSwitcherComponent implements OnInit {
@Input() toggleBtnClass: string = "";
@Input() toggleBtnIconClass: string = "svg-icon-2";
@Input() menuPlacement: string = "bottom-end";
@Input() menuTrigger: string = "{default: "click", lg: "click"}";
mode$: Observable<ThemeModeType>;
menuMode$: Observable<ThemeModeType>;

constructor(private modeService: ThemeModeService) {}

ngOnInit(): void {
this.mode$ = this.modeService.mode.asObservable();
this.menuMode$ = this.modeService.menuMode.asObservable();
}

switchMode(_mode: ThemeModeType): void {
this.modeService.switchMode(_mode);
}
}


Kind regards.


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (2)


It's great that you found a solution! Thank you for sharing your code snippet. We'll consider adding it in future updates to improve the light/dark mode switching in the Metronic Angular template. If you have any more feedback or need further assistance, feel free to let us know!



UPDATE: I've managed to achieve the request by adding another const property inside the switchMode function inside the service that makes the calls to switch between modes:

Code snippet:

public switchMode(_mode: ThemeModeType) {
if (localStorage) {
const updatedMode = _mode === "system" ? systemMode : _mode;
localStorage.setItem(themeModeLSKey, updatedMode);
localStorage.setItem(themeMenuModeLSKey, updatedMode);
localStorage.setItem(themeModeValueKey, updatedMode); <--- THIS LINE ADDED
this.mode.next(updatedMode); <--- THIS LINE ADDED
document.documentElement.setAttribute("data-theme", updatedMode); <--- THIS LINE ADDED
ThemeModeComponent.init(); <--- THIS LINE ADDED
}
}


Support may close the thread, thanks in advance.


Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Click any option to insert into your comment. Select text first to wrap it.
  • **text** to make things bold
  • *text* to emphasize
  • ### Heading to make headings
  • [link text](url) for links
  • ![alt text](image-url) to paste in an image
  • - item to make a list
  • 1. item to make an ordered list
  • > quote to quote somebody
  • `code` for single line of code
  • ```js ... ``` for JS code block
  • ```html ... ``` for HTML code block
  • ```scss ... ``` for SCSS code block
  • ```php ... ``` for PHP code block
  • --- for a horizontal rule
  • happy  :)
  • shocked  :|
  • sad  :(