mirror of
https://github.com/davegallant/davegallant.github.io.git
synced 2025-08-05 08:13:40 +00:00
Add theme toggle back and default to 'auto'
This commit is contained in:
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024 Dave Gallant
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
@@ -7,8 +7,9 @@
|
|||||||
<script>
|
<script>
|
||||||
// load comments
|
// load comments
|
||||||
var getTheme =
|
var getTheme =
|
||||||
window.matchMedia &&
|
(localStorage && localStorage.getItem("theme")) ||
|
||||||
window.matchMedia("(prefers-color-scheme: light)").matches
|
(window.matchMedia &&
|
||||||
|
window.matchMedia("(prefers-color-scheme: light)").matches)
|
||||||
? "light"
|
? "light"
|
||||||
: "dark";
|
: "dark";
|
||||||
getTheme = getTheme == null ? "dark" : getTheme;
|
getTheme = getTheme == null ? "dark" : getTheme;
|
||||||
|
@@ -6,8 +6,7 @@
|
|||||||
{{ $backgroundColor = "bg0_h" }}
|
{{ $backgroundColor = "bg0_h" }}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
|
|
||||||
@media (prefers-color-scheme: light) {
|
:root[data-theme="light"] {
|
||||||
:root {
|
|
||||||
--bg: var(--{{ $backgroundColor }});
|
--bg: var(--{{ $backgroundColor }});
|
||||||
--bg0: #fbf1c7;
|
--bg0: #fbf1c7;
|
||||||
--bg0_h: #f9f5d7;
|
--bg0_h: #f9f5d7;
|
||||||
@@ -42,11 +41,9 @@
|
|||||||
& .light--hidden {
|
& .light--hidden {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
:root[data-theme="dark"] {
|
||||||
:root {
|
|
||||||
--bg: var(--{{ $backgroundColor }});
|
--bg: var(--{{ $backgroundColor }});
|
||||||
--bg0: #282828;
|
--bg0: #282828;
|
||||||
--bg0_h: #1d2021;
|
--bg0_h: #1d2021;
|
||||||
@@ -83,11 +80,8 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
:root {
|
:root {
|
||||||
{{ $themeColor := .Param "themeColor" | default "blue" }}
|
{{ $themeColor := .Param "themeColor" | default "blue" }}
|
||||||
--primary: var(--{{ $themeColor }}1);
|
--primary: var(--{{ $themeColor }}1);
|
||||||
--primary-alt: var(--{{ $themeColor }}2);
|
--primary-alt: var(--{{ $themeColor }}2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,11 +1,12 @@
|
|||||||
function getTheme() {
|
function getTheme() {
|
||||||
if (window.matchMedia) {
|
if (localStorage && localStorage.getItem("theme")) {
|
||||||
// OS preference
|
return localStorage.getItem("theme");
|
||||||
return window.matchMedia("(prefers-color-scheme: light)").matches
|
|
||||||
? "light"
|
|
||||||
: "dark";
|
|
||||||
}
|
}
|
||||||
// Undefined
|
return "auto";
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveTheme(theme) {
|
||||||
|
localStorage.setItem("theme", theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setPrismTheme(theme) {
|
function setPrismTheme(theme) {
|
||||||
@@ -26,16 +27,39 @@ function setCommentsTheme(theme) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initial load
|
function setTheme(theme) {
|
||||||
const theme = getTheme();
|
if (theme == "auto") {
|
||||||
if (theme) {
|
theme = window.matchMedia("(prefers-color-scheme: light)").matches
|
||||||
|
? "light"
|
||||||
|
: "dark";
|
||||||
|
}
|
||||||
|
document.documentElement.setAttribute("data-theme", theme);
|
||||||
setPrismTheme(theme);
|
setPrismTheme(theme);
|
||||||
|
setCommentsTheme(theme);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function toggleTheme(e) {
|
||||||
|
const theme = e.currentTarget.classList.contains("light--hidden")
|
||||||
|
? "light"
|
||||||
|
: "dark";
|
||||||
|
setTheme(theme);
|
||||||
|
saveTheme(theme);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Initial load
|
||||||
|
setTheme(getTheme());
|
||||||
|
|
||||||
window
|
window
|
||||||
.matchMedia("(prefers-color-scheme: dark)")
|
.matchMedia("(prefers-color-scheme: dark)")
|
||||||
.addEventListener("change", (event) => {
|
.addEventListener("change", (event) => {
|
||||||
const theme = event.matches ? "dark" : "light";
|
setTheme(getTheme());
|
||||||
setPrismTheme(theme);
|
|
||||||
setCommentsTheme(theme);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// This script is inlined in the <head> of the document, so we have to wait
|
||||||
|
// for the DOM content before can add event listeners to the toggle buttons
|
||||||
|
document.addEventListener("DOMContentLoaded", function () {
|
||||||
|
const toggleButtons = document.querySelectorAll(".theme__toggle");
|
||||||
|
toggleButtons.forEach((btn) => {
|
||||||
|
btn.addEventListener("click", toggleTheme);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
@@ -52,4 +52,11 @@
|
|||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
|
<button class="theme__toggle light--hidden" aria-label="Toggle light mode">
|
||||||
|
{{ partial "icons/tabler-icon.html" "sun" }}
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<button class="theme__toggle dark--hidden" aria-label="Toggle dark mode">
|
||||||
|
{{ partial "icons/tabler-icon.html" "moon" }}
|
||||||
|
</button>
|
||||||
</header>
|
</header>
|
||||||
|
Reference in New Issue
Block a user