diff options
Diffstat (limited to 'js/theme.ts')
-rw-r--r-- | js/theme.ts | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/js/theme.ts b/js/theme.ts index 36959bc..45101da 100644 --- a/js/theme.ts +++ b/js/theme.ts @@ -1,51 +1,55 @@ -const DEFAULT_THEME = "dark"; +namespace Ach { + export function toggleTheme() { + let body = Ach.getFirstElement(document, "body"); -function toggleTheme() { - let bodies = document.getElementsByTagName("body"); + let theme = "light"; - if (!bodies) { - throw new Error("unable to find body"); - } + if (body.classList.contains("light")) { + theme = "dark"; + } - let theme = "light"; + console.log("setting theme to `" + theme + "`"); - if (bodies[0x0].classList.contains("light")) { - theme = "dark"; + body.classList.toggle("light"); + sessionStorage.setItem("theme", theme); } - console.log("setting theme to `" + theme + "`"); + export function loadTheme() { + let theme = sessionStorage.getItem("theme"); - bodies[0x0].classList.toggle("light"); - localStorage.setItem("theme", theme); -} + if (!theme) { + console.log("theme not set, using default"); + theme = defaultTheme(); + } -function loadTheme() { - let theme = localStorage.getItem("theme"); + switch (theme) { + case "dark": + // We assume this theme in our stylesheets. + break; - if (!theme) { - console.log("theme not set, using default"); - theme = DEFAULT_THEME; - } + case "light": + let body = Ach.getFirstElement(document, "body"); + body.classList.add("light"); - switch (theme) { - case "dark": - // We assume this theme in our stylesheets. - break; + break; - case "light": - let bodies = document.getElementsByTagName("body"); + default: + console.log(`invalid theme \"${theme}\", using default`); + //theme = DEFAULT_THEME; // Redundant now. - if (!bodies) { - throw new Error("unable to find body"); + break; } - bodies[0x0].classList.add("light"); - break; + console.log(`note: theme is now \`${theme}\``); + } - default: - console.log(`invalid theme \"${theme}\", using default`); - //theme = DEFAULT_THEME; // Redundant now. + function defaultTheme(): string { + let hour = new Date().getHours(); - break; + if (hour >= 0x6 && hour <= 0x12) { + return "light"; + } else { + return "dark"; + } } } |