From 47ad7f36595ba5061310b0584b16e43d0b2d253c Mon Sep 17 00:00:00 2001 From: Arne Vlaeminck Date: Fri, 1 Nov 2024 12:19:01 +0100 Subject: [PATCH] Added theme switching --- src/app/app.component.html | 58 ++++++++++++++++++++++++++++++-- src/app/app.component.ts | 25 ++++++++++++++ src/app/hero/hero.component.html | 1 + src/app/hero/hero.component.ts | 10 +++++- src/styles.scss | 43 ++++++++++++----------- 5 files changed, 112 insertions(+), 25 deletions(-) diff --git a/src/app/app.component.html b/src/app/app.component.html index 15a4220..489194b 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,7 +1,61 @@ + +
- - + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 047c40f..857beda 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -17,4 +17,29 @@ import { ToggleComponent } from "./toggle/toggle.component"; }) export class AppComponent { title = 'ontvlambaar'; + theme = "light" + + private detectColorScheme(): void { + //local storage is used to override OS theme settings + if(localStorage.getItem("theme")){ + if(localStorage.getItem("theme") == "dark"){ + this.theme = "dark"; + } + } else if(window.matchMedia("(prefers-color-scheme: dark)").matches) { + //OS theme setting detected as dark + this.theme = "dark"; + } + + document.documentElement.setAttribute("data-theme", this.theme); + } + + ngOnInit(): void { + this.detectColorScheme(); + } + + themeChange(theme: string) { + localStorage.setItem("theme", theme); + this.theme = theme; + document.documentElement.setAttribute("data-theme", this.theme); + } } diff --git a/src/app/hero/hero.component.html b/src/app/hero/hero.component.html index 4ca9ddb..43f8d58 100644 --- a/src/app/hero/hero.component.html +++ b/src/app/hero/hero.component.html @@ -2,3 +2,4 @@ Logo light

Ontvlambaar

Jullie het vuur, wij de branding + diff --git a/src/app/hero/hero.component.ts b/src/app/hero/hero.component.ts index 41c875c..32e6472 100644 --- a/src/app/hero/hero.component.ts +++ b/src/app/hero/hero.component.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, computed, input, output } from '@angular/core'; @Component({ selector: 'app-hero', @@ -8,5 +8,13 @@ import { Component } from '@angular/core'; styleUrl: './hero.component.scss' }) export class HeroComponent { + theme= input(); + isDarkMode = computed(() => { + return this.theme() === 'dark'; + }); + themeChange = output(); + public themeClicked(event: any) { + this.themeChange.emit(event.target.checked ? 'dark' : 'light'); + } } diff --git a/src/styles.scss b/src/styles.scss index 8484464..bfefae0 100644 --- a/src/styles.scss +++ b/src/styles.scss @@ -1,25 +1,4 @@ -body { - .only-on-dark { - display: none; - } - - // dark mode - @media (prefers-color-scheme: dark) { - --text-color: rgb(222, 112, 108); - --background-color: rgb(41, 23, 44); - --accent-color: #E94541; - --logo-color: #E94541; - - .only-on-dark { - display: inline-block; - } - - .only-on-light { - display: none; - } - } - - +html { // light mode --text-color: rgb(29, 29, 29); --background-color: rgb(255, 255, 255); @@ -31,4 +10,24 @@ body { color: var(--text-color); background-color: var(--background-color); + + .only-on-dark { + display: none; + } +} + +[data-theme="dark"] { + // dark mode + --text-color: rgb(222, 112, 108); + --background-color: rgb(41, 23, 44); + --accent-color: #E94541; + --logo-color: #E94541; + + .only-on-dark { + display: inline-block; + } + + .only-on-light { + display: none; + } }