From ae5257b2b3b4a1ef910eb3a69e77d3d2984f4d70 Mon Sep 17 00:00:00 2001 From: Fabian Date: Tue, 2 Jul 2019 19:10:16 +0200 Subject: [PATCH] Adding automatic theme change --- src/components/routes/settings/Settings.tsx | 16 ++++++- src/components/routes/vaults/vaults.scss | 2 +- src/index.tsx | 12 ++++- src/theme.ts | 51 +++++++++++++++------ 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/src/components/routes/settings/Settings.tsx b/src/components/routes/settings/Settings.tsx index 9479208..f0d41b6 100644 --- a/src/components/routes/settings/Settings.tsx +++ b/src/components/routes/settings/Settings.tsx @@ -1,6 +1,6 @@ import { h } from "preact"; import { Page } from "../../../page"; -import Theme from "../../../theme"; +import Theme, { ThemeStates } from "../../../theme"; import Navigation from "../../../navigation"; import ArrowLeft from "feather-icons/dist/icons/arrow-left.svg"; @@ -10,6 +10,7 @@ export default class SettingsPage extends Page<{ state: any }, { vault: string } } render() { + let active = Theme.active(); return
history.back()}> @@ -17,7 +18,18 @@ export default class SettingsPage extends Page<{ state: any }, { vault: string }
- +
+ + +
+ {/* */}
; diff --git a/src/components/routes/vaults/vaults.scss b/src/components/routes/vaults/vaults.scss index ac51b98..c6cad76 100755 --- a/src/components/routes/vaults/vaults.scss +++ b/src/components/routes/vaults/vaults.scss @@ -3,7 +3,7 @@ padding: 0.5rem; margin-top: 0 !important; align-items: flex-end; - justify-content: end; + justify-content: start; >span { font-size: 1.5rem; diff --git a/src/index.tsx b/src/index.tsx index 8d7958c..28e7228 100755 --- a/src/index.tsx +++ b/src/index.tsx @@ -99,17 +99,25 @@ console.log("Dark mode:", Theme.active); let err = await Notes.getToken(code) if (err) { Notifications.sendError("Login failed: " + err) - Notes.login() + return Notes.login() } else { window.history.replaceState(null, document.title, "/" + window.location.hash); } } else { - Notes.login() + return Notes.login() } } await Notes.start(); + if (window.navigator.storage && navigator.storage.persist) { + navigator.storage.persisted() + .then(has => has ? true : navigator.storage.persist()) + .then(is => { + console.log("Persistant Storage:", is); + }) + } + Navigation.default = VaultsPage as typeof Page; Navigation.addPage("/vault", VaultPage as typeof Page) Navigation.addPage("/demo", DemoPage as typeof Page) diff --git a/src/theme.ts b/src/theme.ts index 11b090f..ab63b53 100644 --- a/src/theme.ts +++ b/src/theme.ts @@ -1,27 +1,52 @@ const light = require("!!raw-loader!@hibas123/theme/out/light.css").default; const dark = require("!!raw-loader!@hibas123/theme/out/dark.css").default; -let isDark = localStorage.getItem("theme") === "dark"; +export enum ThemeStates { + AUTO, + LIGHT, + DARK +} + +let themeConfig: ThemeStates = Number(localStorage.getItem("theme")); +if (Number.isNaN(themeConfig)) + themeConfig = ThemeStates.AUTO; + +let isDark = false; +let mediaIsDark = false; + +if (window.matchMedia) { + const mediaq = matchMedia("(prefers-color-scheme: dark)"); + mediaIsDark = mediaq.matches; + mediaq.onchange = ev => { + mediaIsDark = ev.matches; + apply(); + } + console.log(mediaq); +} let styleElm: HTMLStyleElement; - -function apply() { - if (styleElm) styleElm.remove(); - styleElm = document.createElement("style"); - document.head.appendChild(styleElm); - styleElm.innerHTML = isDark ? dark : light; +function apply(force?: boolean) { + let shouldDark = themeConfig === ThemeStates.AUTO ? mediaIsDark : themeConfig === ThemeStates.DARK; + if (force || shouldDark !== isDark) { + if (styleElm) styleElm.remove(); + styleElm = document.createElement("style"); + document.head.appendChild(styleElm); + styleElm.innerHTML = shouldDark ? dark : light; + isDark = shouldDark; + } } -apply(); +apply(true); -function toggle() { - isDark = !isDark; - localStorage.setItem("theme", isDark ? "dark" : "light"); + +function change(state: ThemeStates) { + themeConfig = state; + localStorage.setItem("theme", String(themeConfig)); apply(); } export default { - active: () => isDark, - toggle: () => toggle() + active: () => themeConfig, + change: (state: ThemeStates) => change(state) }