Add Theme Service
parent
706a955023
commit
1ec065cbae
@ -1,10 +1,2 @@
|
||||
<mat-toolbar color="primary">
|
||||
<span>Flotte</span>
|
||||
</mat-toolbar>
|
||||
<p>Das ist ein Paragraph.</p>
|
||||
|
||||
<br />
|
||||
<button mat-raised-button color="primary">Primary</button>
|
||||
<button mat-raised-button color="accent">Accent</button>
|
||||
|
||||
<app-navbar></app-navbar>
|
||||
<router-outlet></router-outlet>
|
||||
|
@ -1,16 +1,16 @@
|
||||
import { Component, Renderer2 } from '@angular/core';
|
||||
import { ColorThemeService } from './services/colorTheme.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-root',
|
||||
templateUrl: './app.component.html',
|
||||
styleUrls: ['./app.component.scss']
|
||||
styleUrls: ['./app.component.scss'],
|
||||
})
|
||||
export class AppComponent {
|
||||
title = 'flotte-frontend';
|
||||
|
||||
constructor(private renderer: Renderer2) {
|
||||
this.renderer.addClass(document.body, 'mat-app-background');
|
||||
this.renderer.addClass(document.body, 'dark-theme');
|
||||
constructor(private renderer: Renderer2, private themeService: ColorThemeService) {
|
||||
this.renderer.addClass(document.body, 'mat-app-background'); //so the background color changes dependent on current theme
|
||||
this.themeService.load();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,62 @@
|
||||
import {Injectable, Renderer2, RendererFactory2} from '@angular/core';
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class ColorThemeService {
|
||||
|
||||
private renderer: Renderer2;
|
||||
private colorTheme: string;
|
||||
// Define prefix for more clear and readable styling classes in scss files
|
||||
|
||||
constructor(rendererFactory: RendererFactory2) {
|
||||
// Create new renderer from renderFactory, to make it possible to use renderer2 in a service
|
||||
this.renderer = rendererFactory.createRenderer(null, null);
|
||||
}
|
||||
|
||||
_detectPrefersColorTheme() {
|
||||
// Detect if prefers-color-theme is supported
|
||||
if (window.matchMedia('(prefers-color-theme)').media !== 'not all') {
|
||||
// Set colorTheme to Dark if prefers-color-theme is dark. Otherwise set to light.
|
||||
this.colorTheme = window.matchMedia('(prefers-color-theme: dark)').matches ? 'dark-theme' : 'light-theme';
|
||||
} else {
|
||||
// If browser dont support prefers-color-theme, set it as default to dark
|
||||
this.colorTheme = 'light-theme';
|
||||
}
|
||||
}
|
||||
|
||||
_setColorTheme(theme) {
|
||||
this.colorTheme = theme;
|
||||
// Save prefers-color-theme to localStorage
|
||||
localStorage.setItem('prefers-color', theme);
|
||||
}
|
||||
|
||||
_getColorTheme() {
|
||||
// Check if any prefers-color-theme is stored in localStorage
|
||||
if (localStorage.getItem('prefers-color')) {
|
||||
// Save prefers-color-theme from localStorage
|
||||
this.colorTheme = localStorage.getItem('prefers-color');
|
||||
} else {
|
||||
// If no prefers-color-theme is stored in localStorage, Try to detect OS default prefers-color-theme
|
||||
this._detectPrefersColorTheme();
|
||||
}
|
||||
}
|
||||
|
||||
load() {
|
||||
this._getColorTheme();
|
||||
this.renderer.addClass(document.body, this.colorTheme);
|
||||
}
|
||||
|
||||
update(theme) {
|
||||
this._setColorTheme(theme);
|
||||
// Remove the old color-theme class
|
||||
this.renderer.removeClass( document.body, (this.colorTheme === 'dark-theme' ? 'light-theme' : 'dark-theme') );
|
||||
// Add the new / current color-theme class
|
||||
this.renderer.addClass(document.body, theme);
|
||||
}
|
||||
|
||||
currentActive() {
|
||||
return this.colorTheme;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue