diff --git a/src/app/components/dark-light-mode/dark-light-mode.component.css b/src/app/components/dark-light-mode/dark-light-mode.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/components/dark-light-mode/dark-light-mode.component.html b/src/app/components/dark-light-mode/dark-light-mode.component.html new file mode 100644 index 0000000..5ec0080 --- /dev/null +++ b/src/app/components/dark-light-mode/dark-light-mode.component.html @@ -0,0 +1,5 @@ + diff --git a/src/app/components/dark-light-mode/dark-light-mode.component.ts b/src/app/components/dark-light-mode/dark-light-mode.component.ts new file mode 100644 index 0000000..c30f350 --- /dev/null +++ b/src/app/components/dark-light-mode/dark-light-mode.component.ts @@ -0,0 +1,18 @@ +import { ChangeDetectionStrategy, Component, inject } from '@angular/core'; +import { FaIconComponent } from '@fortawesome/angular-fontawesome'; +import { DarkLightModeStore } from '../../stores/dark-light-mode.store'; +import { faMoon, faSun } from '@fortawesome/free-solid-svg-icons'; + +@Component({ + selector: 'pap-dark-light-mode', + standalone: true, + imports: [FaIconComponent], + templateUrl: './dark-light-mode.component.html', + styleUrl: './dark-light-mode.component.css', + changeDetection: ChangeDetectionStrategy.OnPush, +}) +export class DarkLightModeComponent { + protected readonly darkLightModeStore = inject(DarkLightModeStore); + protected readonly faSun = faSun; + protected readonly faMoon = faMoon; +} diff --git a/src/app/components/header/header.component.html b/src/app/components/header/header.component.html index 2cf7627..31d4a5e 100644 --- a/src/app/components/header/header.component.html +++ b/src/app/components/header/header.component.html @@ -14,5 +14,8 @@ Tipis P&P Tools +
+ +
diff --git a/src/app/components/header/header.component.ts b/src/app/components/header/header.component.ts index c8d0a28..541242a 100644 --- a/src/app/components/header/header.component.ts +++ b/src/app/components/header/header.component.ts @@ -1,11 +1,12 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; import { FaIconComponent } from '@fortawesome/angular-fontawesome'; import { faBars, faDiceD20 } from '@fortawesome/free-solid-svg-icons'; +import { DarkLightModeComponent } from '../dark-light-mode/dark-light-mode.component'; @Component({ selector: 'pap-header', standalone: true, - imports: [FaIconComponent], + imports: [FaIconComponent, DarkLightModeComponent], templateUrl: './header.component.html', styleUrl: './header.component.css', changeDetection: ChangeDetectionStrategy.OnPush, diff --git a/src/app/services/dark-light-mode.service.ts b/src/app/services/dark-light-mode.service.ts new file mode 100644 index 0000000..c111073 --- /dev/null +++ b/src/app/services/dark-light-mode.service.ts @@ -0,0 +1,19 @@ +import { inject, Injectable } from '@angular/core'; +import { DOCUMENT } from '@angular/common'; + +@Injectable({ + providedIn: 'root', +}) +export class DarkLightModeService { + private readonly document = inject(DOCUMENT); + + enableDarkMode(): void { + this.document.body.parentElement!.classList.add('dark'); + this.document.body.parentElement!.classList.remove('light'); + } + + enableLightMode(): void { + this.document.body.parentElement!.classList.add('light'); + this.document.body.parentElement!.classList.remove('dark'); + } +} diff --git a/src/app/stores/dark-light-mode.store.ts b/src/app/stores/dark-light-mode.store.ts new file mode 100644 index 0000000..1b85ffb --- /dev/null +++ b/src/app/stores/dark-light-mode.store.ts @@ -0,0 +1,40 @@ +import { patchState, signalStore, withHooks, withMethods, withState } from '@ngrx/signals'; +import { DarkLightModeService } from '../services/dark-light-mode.service'; +import { inject } from '@angular/core'; + +interface DarkLightModeState { + mode: 'dark' | 'light'; +} + +const storageKey = 'preferred-mode'; + +export const DarkLightModeStore = signalStore( + { providedIn: 'root' }, + withState({ mode: 'light' }), + withMethods(store => { + const darkLightModeService = inject(DarkLightModeService); + + return { + enableDarkMode: () => { + darkLightModeService.enableDarkMode(); + patchState(store, { mode: 'dark' }); + localStorage.setItem(storageKey, 'dark'); + }, + enableLightMode: () => { + darkLightModeService.enableLightMode(); + patchState(store, { mode: 'light' }); + localStorage.setItem(storageKey, 'light'); + }, + }; + }), + withHooks({ + onInit: store => { + const mode = localStorage.getItem(storageKey) ?? 'light'; + if (mode === 'light') { + return store.enableLightMode(); + } + + return store.enableDarkMode(); + }, + }), +);