Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit 3aa613e

Browse files
committed
Added dark mode support
1 parent 64fa1f7 commit 3aa613e

File tree

7 files changed

+109
-4
lines changed

7 files changed

+109
-4
lines changed

angular.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"output": "/"
2929
}
3030
],
31-
"styles": ["src/styles.css"],
31+
"styles": ["src/styles.scss"],
3232
"scripts": []
3333
},
3434
"configurations": {
@@ -81,7 +81,7 @@
8181
"polyfills": "src/polyfills.ts",
8282
"tsConfig": "src/tsconfig.spec.json",
8383
"scripts": [],
84-
"styles": ["src/styles.css"],
84+
"styles": ["src/styles.scss"],
8585
"assets": [
8686
{
8787
"glob": "**/*",

src/app/app.component.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<mat-toolbar color="primary" style="display: flex;justify-content: space-between;">
22
<div class="page-title">{{'Angular Update Guide'|i18n}}</div>
33
<div style="flex-grow:1"></div>
4+
<div style="margin:16px;">
5+
<button mat-icon-button (click)="toggleTheme()">
6+
<mat-icon *ngIf="!darkMode">dark_mode</mat-icon>
7+
<mat-icon *ngIf="darkMode">light_mode</mat-icon>
8+
</button>
9+
</div>
410
<div style="margin:16px;">
511
<a href="https://angular.io/guide/updating" style="color: white; display: flex;">
612
<svg style="width:24px;height:24px" viewBox="0 0 24 24" >

src/app/app.component.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { AnalyticsService } from './analytics.service';
66
import { getLocalizedAction, currentLocale } from './localization';
77
import { I18nPipe } from './i18n.pipe';
88
import { Clipboard } from '@angular/cdk/clipboard';
9+
import {ThemeService} from "./theme/theme.service";
910

1011
interface Option {
1112
id: string;
@@ -75,15 +76,19 @@ export class AppComponent {
7576
* Only save the locale in the URL if it was already there, or the user changed it
7677
*/
7778
saveLocale = false;
78-
79+
darkMode: boolean = false;
7980
steps: Step[] = RECOMMENDATIONS;
8081

8182
constructor(
8283
public location: Location,
8384
public track: AnalyticsService,
8485
public i18Service: I18nPipe,
85-
private clipboard: Clipboard
86+
private clipboard: Clipboard,
87+
private readonly themeService: ThemeService
8688
) {
89+
if(this.themeService.getSavedTheme() === this.themeService.darkTheme){
90+
this.toggleTheme(false)
91+
}
8792
this.optionList = [
8893
{ id: 'ngUpgrade', name: 'ngUpgrade', description: i18Service.transform('to combine AngularJS & Angular') },
8994
{ id: 'material', name: 'Angular Material', description: '' },
@@ -298,6 +303,11 @@ export class AppComponent {
298303
this.saveLocale = true;
299304
this.showUpdatePath();
300305
}
306+
307+
toggleTheme(useStorage: boolean = true) {
308+
this.themeService.setTheme(useStorage);
309+
this.darkMode = this.themeService.isDarkMode();
310+
}
301311
}
302312

303313
/** Whether or not the user is running on a Windows OS. */

src/app/app.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { Location, LocationStrategy, PathLocationStrategy } from '@angular/commo
1616

1717
import './locales';
1818
import { I18nPipe } from './i18n.pipe';
19+
import {MatIconModule} from "@angular/material/icon";
1920

2021
@NgModule({
2122
declarations: [AppComponent, I18nPipe],
@@ -31,6 +32,7 @@ import { I18nPipe } from './i18n.pipe';
3132
MatGridListModule,
3233
MatProgressBarModule,
3334
MatButtonToggleModule,
35+
MatIconModule,
3436
MatMenuModule,
3537
],
3638
providers: [Location, { provide: LocationStrategy, useClass: PathLocationStrategy }],

src/app/theme/theme.service.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import {Injectable} from '@angular/core';
2+
3+
@Injectable({
4+
providedIn: 'root'
5+
})
6+
export class ThemeService {
7+
private readonly localStorageKey = "theme-id";
8+
public readonly darkTheme = "dark-theme";
9+
public readonly lightTheme = "light-theme";
10+
11+
setTheme(useStorage: boolean) {
12+
let updatedClasses: string;
13+
let darkMode: boolean = false;
14+
const bodyTag = document.getElementsByTagName("body")
15+
if (bodyTag) {
16+
const bodyClasses: string = bodyTag[0]?.getAttribute("class") || "";
17+
if (bodyClasses.includes(this.darkTheme)) {
18+
updatedClasses = bodyClasses.replace(this.darkTheme, "");
19+
} else {
20+
darkMode = true;
21+
updatedClasses = `${bodyClasses} ${this.darkTheme}`;
22+
}
23+
bodyTag[0]?.setAttribute("class", updatedClasses);
24+
}
25+
if (useStorage) {
26+
this.updateLocalStorage(darkMode)
27+
}
28+
}
29+
30+
updateLocalStorage(darkMode: boolean) {
31+
localStorage.setItem(this.localStorageKey, darkMode ? this.darkTheme : this.lightTheme)
32+
}
33+
34+
getSavedTheme() {
35+
return localStorage.getItem(this.localStorageKey)
36+
}
37+
38+
isDarkMode() {
39+
const bodyTag = document.getElementsByTagName("body");
40+
if (bodyTag) {
41+
return bodyTag[0]?.getAttribute("class")?.includes(this.darkTheme) || false;
42+
}
43+
return false;
44+
}
45+
}

src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
<meta name="viewport" content="width=device-width, initial-scale=1">
1010
<link rel="icon" type="image/x-icon" href="favicon.ico">
11+
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
1112
</head>
1213

1314
<body>

src/styles.css renamed to src/styles.scss

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
1+
@use '@angular/material' as mat;
2+
@import "@angular/material/prebuilt-themes/pink-bluegrey.css";
13
@import "@angular/material/prebuilt-themes/indigo-pink.css";
24
@import url('https://fonts.googleapis.com/css?family=Roboto');
35
@import url('https://fonts.googleapis.com/css?family=Roboto+Mono');
6+
@include mat.core();
7+
8+
$dark-theme: mat.define-dark-theme((
9+
color: (
10+
primary: mat.define-palette(mat.$pink-palette),
11+
secondary: mat.define-palette(mat.$blue-grey-palette),
12+
accent: mat.define-palette(mat.$red-palette)
13+
),
14+
typography: mat.define-typography-config(),
15+
density: 0,
16+
));
17+
18+
.dark-theme {
19+
background: #303030;
20+
@include mat.all-component-themes($dark-theme);
21+
color: #fff;
22+
h1, h2, h3, h4, h5, h6, p, li, ul, ol, td, th, table {
23+
color: #fff;
24+
}
25+
a {
26+
color: #f48fb1;
27+
}
28+
a:hover {
29+
color: #ffb0c7;
30+
}
31+
code {
32+
background: #ffffff0f;
33+
&:hover{
34+
background: #ffffff4a;
35+
}
36+
&:hover:after {
37+
background: #e91e63;
38+
}
39+
40+
&:hover:before {
41+
background: #404040;
42+
}
43+
}
44+
}
445

546
* {
647
margin: 0;

0 commit comments

Comments
 (0)