blowfish/assets/js/appearance.js

112 lines
3.6 KiB
JavaScript
Raw Permalink Normal View History

2022-09-10 14:05:37 -05:00
const sitePreference = document.documentElement.getAttribute("data-default-appearance");
const userPreference = localStorage.getItem("appearance");
if ((sitePreference === "dark" && userPreference === null) || userPreference === "dark") {
document.documentElement.classList.add("dark");
}
if (document.documentElement.getAttribute("data-auto-appearance") === "true") {
if (
window.matchMedia &&
window.matchMedia("(prefers-color-scheme: dark)").matches &&
userPreference !== "light"
) {
document.documentElement.classList.add("dark");
}
window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", (event) => {
if (event.matches) {
document.documentElement.classList.add("dark");
} else {
document.documentElement.classList.remove("dark");
}
});
}
window.addEventListener("DOMContentLoaded", (event) => {
const switcher = document.getElementById("appearance-switcher");
2022-09-11 11:20:31 -05:00
const switcherMobile = document.getElementById("appearance-switcher-mobile");
updateMeta();
this.updateLogo?.(getTargetAppearance());
2022-09-10 14:05:37 -05:00
if (switcher) {
switcher.addEventListener("click", () => {
document.documentElement.classList.toggle("dark");
var targetAppearance = getTargetAppearance();
2022-09-10 14:05:37 -05:00
localStorage.setItem(
"appearance",
targetAppearance
2022-09-10 14:05:37 -05:00
);
updateMeta();
this.updateLogo?.(targetAppearance);
2022-09-10 14:05:37 -05:00
});
switcher.addEventListener("contextmenu", (event) => {
event.preventDefault();
localStorage.removeItem("appearance");
});
}
2022-09-11 11:20:31 -05:00
if (switcherMobile) {
switcherMobile.addEventListener("click", () => {
document.documentElement.classList.toggle("dark");
var targetAppearance = getTargetAppearance();
2022-09-11 11:20:31 -05:00
localStorage.setItem(
"appearance",
targetAppearance
2022-09-11 11:20:31 -05:00
);
updateMeta();
this.updateLogo?.(targetAppearance);
2022-09-11 11:20:31 -05:00
});
switcherMobile.addEventListener("contextmenu", (event) => {
event.preventDefault();
localStorage.removeItem("appearance");
});
}
});
var updateMeta = () => {
var elem, style;
elem = document.querySelector('body');
style = getComputedStyle(elem);
document.querySelector('meta[name="theme-color"]').setAttribute('content', style.backgroundColor);
}
{{ if and (.Site.Params.Logo) (.Site.Params.SecondaryLogo) }}
{{ $primaryLogo := resources.Get .Site.Params.Logo }}
{{ $secondaryLogo := resources.Get .Site.Params.SecondaryLogo }}
{{ if and ($primaryLogo) ($secondaryLogo) }}
var updateLogo = (targetAppearance) => {
var imgElems = document.querySelectorAll("img.logo");
var logoContainers = document.querySelectorAll("span.logo");
targetLogoPath =
targetAppearance == "{{ .Site.Params.DefaultAppearance }}" ?
"{{ $primaryLogo.RelPermalink }}" : "{{ $secondaryLogo.RelPermalink }}"
for (const elem of imgElems) {
elem.setAttribute("src", targetLogoPath)
}
{{ if eq $primaryLogo.MediaType.SubType "svg" }}
targetContent =
targetAppearance == "{{ .Site.Params.DefaultAppearance }}" ?
`{{ $primaryLogo.Content | safeHTML }}` : `{{ $secondaryLogo.Content | safeHTML }}`
for (const container of logoContainers) {
container.innerHTML = targetContent;
}
{{ end }}
}
{{ end }}
{{- end }}
var getTargetAppearance = () => {
return document.documentElement.classList.contains("dark") ? "dark" : "light"
}
window.addEventListener("DOMContentLoaded", (event) => {
const scroller = document.getElementById("top-scroller");
const footer = document.getElementById("site-footer");
2023-05-23 06:46:12 -05:00
if(scroller && footer && scroller.getBoundingClientRect().top > footer.getBoundingClientRect().top) {
scroller.hidden = true;
}
});