From 78a7ac018df0adadfae6b19e72ee2da012641e46 Mon Sep 17 00:00:00 2001 From: srgooglo Date: Tue, 31 May 2022 01:09:24 +0200 Subject: [PATCH] handle auto prefered color scheme --- packages/app/src/cores/style/index.jsx | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/packages/app/src/cores/style/index.jsx b/packages/app/src/cores/style/index.jsx index 42b9daa9..68dd22cd 100644 --- a/packages/app/src/cores/style/index.jsx +++ b/packages/app/src/cores/style/index.jsx @@ -12,6 +12,11 @@ export default class StyleCore extends Core { currentVariant = null events = { + "app.autoDarkMode": (value) => { + if (value === true) { + this.handleAutoColorScheme() + } + }, "theme.applyVariant": (value) => { this.applyVariant(value) this.setVariant(value) @@ -28,11 +33,21 @@ export default class StyleCore extends Core { publicMethods = { style: this } - + static get currentVariant() { return document.documentElement.style.getPropertyValue("--themeVariant") } + handleAutoColorScheme() { + const prefered = window.matchMedia("(prefers-color-scheme: light)") + + if (window.app.settings.get("app.auto_darkMode") && !prefered.matches) { + this.applyVariant("dark") + }else { + this.applyVariant("false") + } + } + initialize = async () => { let theme = this.getStoragedTheme() @@ -61,6 +76,11 @@ export default class StyleCore extends Core { // apply variation this.applyVariant(variantKey) + + // handle auto prefered color scheme + if (window.app.settings.get("app.auto_darkMode")) { + window.matchMedia("(prefers-color-scheme: light)").addListener(this.handleAutoColorScheme) + } } getRootVariables = () => {