This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #398 from grantlouisherman/enhancement/227-dark-li…
…ght-switcher enhancement(switcher): implementing dark-light switcher for remote
- Loading branch information
Showing
6 changed files
with
90 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
.dark { | ||
--ion-background-color: black; | ||
--ion-text-color: #ffffff; | ||
--ion-item-background: #1A1B1E; | ||
--ion-item-background-activated: #313131; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
remote/src/app/modals/app-remote-dark-mode-switch/app-remote-dark-mode-switch.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
|
67 changes: 67 additions & 0 deletions
67
remote/src/app/modals/app-remote-dark-mode-switch/app-remote-dark-mode-switch.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Component, h, Listen, State } from '@stencil/core'; | ||
import { set, get } from 'idb-keyval'; | ||
|
||
@Component({ | ||
tag: 'dark-mode-switch', | ||
}) | ||
export class DarkModeSwitch { | ||
|
||
@State() | ||
private darkModePreference: Boolean = false; | ||
private domBodyClassList: DOMTokenList = document.body.classList; | ||
|
||
// error handling if the idb-keyval throws an error | ||
private async idbKeyValPromiseHandler (promise): Promise<any> { | ||
return Promise.resolve(promise) | ||
.then( valueOfPromise => valueOfPromise) | ||
.catch(err => Error(err)); | ||
} | ||
|
||
private async resolveDarkModePreference(): Promise<Boolean> { | ||
let getDarkModePreferenceFromIdb: Promise<any> = await this.idbKeyValPromiseHandler(get('darkModePreferences')); | ||
let getDarkModePreferenceFromMedia = window.matchMedia('(prefers-color-scheme: dark)'); | ||
if( getDarkModePreferenceFromIdb instanceof Error ){ | ||
return false; | ||
} | ||
let isDarkModePreferenceDetermined: Boolean | Promise<Boolean> = | ||
getDarkModePreferenceFromIdb || | ||
getDarkModePreferenceFromMedia.matches; | ||
|
||
return isDarkModePreferenceDetermined ? isDarkModePreferenceDetermined : this.darkModePreference; | ||
} | ||
|
||
|
||
private updateDarkModePreferences(): void { | ||
!this.darkModePreference ? | ||
this.domBodyClassList.add('dark') : | ||
this.domBodyClassList.remove('dark'); | ||
this.darkModePreference = !this.darkModePreference; | ||
} | ||
|
||
async componentDidLoad(){ | ||
this.darkModePreference = await this.resolveDarkModePreference(); | ||
const DARKTOGGLE: HTMLInputElement = document.querySelector('#themeToggle'); | ||
if(this.darkModePreference){ | ||
DARKTOGGLE.checked = true; | ||
this.domBodyClassList.add('dark'); | ||
} | ||
} | ||
|
||
@Listen('click', { capture: true }) | ||
handleOnToggleChange() { | ||
this.updateDarkModePreferences(); | ||
this.idbKeyValPromiseHandler(set('darkModePreferences', this.darkModePreference)); | ||
} | ||
|
||
render() { | ||
return ( | ||
<ion-item> | ||
<ion-icon name="moon"></ion-icon> | ||
<ion-label> | ||
Toggle Dark Theme | ||
</ion-label> | ||
<ion-toggle id="themeToggle"></ion-toggle> | ||
</ion-item> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters