From 6c311683d9df5292cd3ec591476665f667d1a69b Mon Sep 17 00:00:00 2001 From: BPower0036 <80090789+BPower0036@users.noreply.github.com> Date: Thu, 5 May 2022 12:58:59 +0000 Subject: [PATCH] https://github.com/mozilla/multi-account-containers/pull/1964 --- src/js/utils.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/js/utils.js b/src/js/utils.js index 6b0c2e3..4cde379 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -177,6 +177,26 @@ const Utils = { false ); }, + /* Theme helper + * + * First, we look if there's a theme already set in the local storage. If + * there isn't one, we set the theme based on `prefers-color-scheme`. + * */ + getTheme(currentTheme, window) { + if (typeof currentTheme !== "undefined" && currentTheme !== "auto") { + return currentTheme; + } + if (window.matchMedia("(prefers-color-scheme: dark)").matches) { + return "dark"; + } + return "light"; + }, + async applyTheme() { + const { currentTheme } = await browser.storage.local.get("currentTheme"); + const popup = document.getElementsByTagName("html")[0]; + const theme = Utils.getTheme(currentTheme, window); + popup.setAttribute("data-theme", theme); + } }; window.Utils = Utils;