Skip to content

Commit c3baad8

Browse files
committed
Implement dark light toggle
1 parent caebc1e commit c3baad8

File tree

9 files changed

+80
-10
lines changed

9 files changed

+80
-10
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
:host {
2+
--background-color: white;
3+
--font-color: black;
4+
}
5+
6+
:host.dark {
7+
--background-color: black;
8+
--font-color: white;
9+
}
10+
11+
:host {
12+
display: block;
13+
background-color: var(--background-color);
14+
color: var(--font-color);
15+
}
Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,27 @@
1-
import { Component } from '@angular/core';
1+
import { Component, HostBinding, OnDestroy } from '@angular/core';
2+
import { Subscription } from 'rxjs';
3+
import { ThemeService } from './theme.service';
24

35
@Component({
46
selector: 'app-root',
57
template: `
68
<app-navbar></app-navbar>
79
<router-outlet></router-outlet>
810
`,
11+
styleUrls: ['./app.component.css'],
912
})
10-
export class AppComponent {}
13+
export class AppComponent implements OnDestroy {
14+
subscription: Subscription;
15+
16+
@HostBinding('class') classes = 'dark';
17+
18+
constructor(private themeService: ThemeService) {
19+
this.subscription = this.themeService.theme$.subscribe((theme) => {
20+
this.classes = theme;
21+
});
22+
}
23+
24+
ngOnDestroy(): void {
25+
this.subscription.unsubscribe();
26+
}
27+
}

projects/train-platform/src/app/navbar/navbar.component.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
nav {
22
height: 40px;
33
width: 100%;
4-
background-color: aqua;
54
display: flex;
65
flex-direction: row;
76
justify-content: space-between;

projects/train-platform/src/app/navbar/navbar.component.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@
55
</li>
66
</ul>
77
<div class="page-options">
8+
<div>
9+
<label>{{ "navbar.theme.text" | translate }}</label>
10+
<select [ngModel]="theme" (ngModelChange)="updateTheme($event)">
11+
<option value="light">{{ "navbar.theme.light" | translate }}</option>
12+
<option value="dark">{{ "navbar.theme.dark" | translate }}</option>
13+
</select>
14+
</div>
815
<div>
916
<label>{{ "navbar.language.text" | translate }}</label>
1017
<select [ngModel]="language" (ngModelChange)="updateLanguage($event)">

projects/train-platform/src/app/navbar/navbar.component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Component } from '@angular/core';
22
import { TranslateService } from '@ngx-translate/core';
3+
import { ThemeService } from '../theme.service';
34

45
@Component({
56
selector: 'app-navbar',
@@ -8,8 +9,17 @@ import { TranslateService } from '@ngx-translate/core';
89
})
910
export class NavbarComponent {
1011
language: 'en' | 'de' = 'en';
12+
theme: 'light' | 'dark' = 'light';
1113

12-
constructor(private translateService: TranslateService) {}
14+
constructor(
15+
private translateService: TranslateService,
16+
private themeService: ThemeService
17+
) {}
18+
19+
updateTheme(theme: 'light' | 'dark'): void {
20+
this.theme = theme;
21+
this.themeService.updateTheme(theme);
22+
}
1323

1424
updateLanguage(lang: 'en' | 'de'): void {
1525
this.language = lang;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject, Observable } from 'rxjs';
3+
4+
type Theme = 'light' | 'dark';
5+
6+
@Injectable({
7+
providedIn: 'root',
8+
})
9+
export class ThemeService {
10+
private themeSubject: BehaviorSubject<Theme> = new BehaviorSubject<Theme>(
11+
'light'
12+
);
13+
public readonly theme$ = this.themeSubject.asObservable();
14+
15+
updateTheme(theme: Theme): void {
16+
this.themeSubject.next(theme);
17+
}
18+
}

projects/train-platform/src/assets/i18n/de.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"text": "Sprache",
66
"en": "Englisch",
77
"de": "Deutsch"
8+
},
9+
"theme": {
10+
"text": "Farbe",
11+
"light": "Hell",
12+
"dark": "Dunkel"
813
}
914
},
1015
"booking": {

projects/train-platform/src/assets/i18n/en.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"text": "Language",
66
"en": "English",
77
"de": "German"
8+
},
9+
"theme": {
10+
"text": "Theme",
11+
"light": "Light",
12+
"dark": "Dark"
813
}
914
},
1015
"booking": {
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +0,0 @@
1-
body {
2-
margin: 0;
3-
max-width: 800px;
4-
margin-left: auto;
5-
margin-right: auto;
6-
}

0 commit comments

Comments
 (0)