From 9532cb828522a9f63f20a84777b911be17b6f6ac Mon Sep 17 00:00:00 2001 From: panr Date: Fri, 21 Dec 2018 19:07:37 +0100 Subject: Set default theme in config toml --- exampleSite/config.toml | 2 ++ layouts/_default/baseof.html | 2 +- source/js/theme.js | 22 ++++++++++++---------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/exampleSite/config.toml b/exampleSite/config.toml index 244813d..f138adb 100644 --- a/exampleSite/config.toml +++ b/exampleSite/config.toml @@ -7,6 +7,8 @@ paginate = 5 [params] subtitle = "A simple theme for Hugo" + # "light" or "dark" + defaultTheme = "dark" [params.logo] logoText = "hello friend" diff --git a/layouts/_default/baseof.html b/layouts/_default/baseof.html index 173295d..1e91b7d 100644 --- a/layouts/_default/baseof.html +++ b/layouts/_default/baseof.html @@ -6,7 +6,7 @@ {{ end }} {{ partial "head.html" . }} - +
{{ partial "header.html" . }} diff --git a/source/js/theme.js b/source/js/theme.js index b30171f..d5a324b 100644 --- a/source/js/theme.js +++ b/source/js/theme.js @@ -1,16 +1,18 @@ // Toggle theme -const getTheme = window.localStorage && window.localStorage.getItem('theme') -const themeToggle = document.querySelector('.theme-toggle') -const isDark = getTheme === 'dark' || getTheme === null +const getTheme = window.localStorage && window.localStorage.getItem("theme"); +const themeToggle = document.querySelector(".theme-toggle"); +const isDark = getTheme === "dark"; -document.body.classList.toggle('dark-theme', isDark) +if (getTheme !== null) { + document.body.classList.toggle("dark-theme", isDark); +} -themeToggle.addEventListener('click', () => { - document.body.classList.toggle('dark-theme') +themeToggle.addEventListener("click", () => { + document.body.classList.toggle("dark-theme"); window.localStorage && window.localStorage.setItem( - 'theme', - document.body.classList.contains('dark-theme') ? 'dark' : 'light', - ) -}) + "theme", + document.body.classList.contains("dark-theme") ? "dark" : "light", + ); +}); -- cgit v1.2.3