fix(theme): synchronize navbar logo with active color scheme
Replace CSS background-image switching with direct navbar image source updates to avoid Safari and WebKit repaint failures. Select the appropriate logo during initial DOM construction and update it when the system color scheme changes, Safari restores a cached page, or the document returns to the foreground. Preserve CSP compatibility and Gitea subpath support while retaining the existing logo dimensions.
This commit is contained in:
@@ -4,3 +4,34 @@
|
||||
window.plausible=window.plausible||function(){(plausible.q=plausible.q||[]).push(arguments)},plausible.init=plausible.init||function(i){plausible.o=i||{}};
|
||||
plausible.init()
|
||||
</script>
|
||||
|
||||
<script nonce="{{ctx.CspScriptNonce}}">
|
||||
(() => {
|
||||
const darkMode = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
const updateNavbarLogo = () => {
|
||||
const logo = document.querySelector("#navbar-logo img");
|
||||
const theme = document.documentElement.dataset.theme;
|
||||
if (!logo) return false;
|
||||
if (!theme?.startsWith("codecraftsmen-")) return true;
|
||||
|
||||
const dark = theme === "codecraftsmen-dark" || (theme === "codecraftsmen-auto" && darkMode.matches);
|
||||
const src = `${window.config.assetUrlPrefix}/img/logo-${dark ? "dark" : "light"}-no-slogan.svg`;
|
||||
if (logo.getAttribute("src") !== src) logo.setAttribute("src", src);
|
||||
return true;
|
||||
};
|
||||
|
||||
const navbarObserver = new MutationObserver(() => {
|
||||
if (updateNavbarLogo()) navbarObserver.disconnect();
|
||||
});
|
||||
navbarObserver.observe(document.documentElement, {childList: true, subtree: true});
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
updateNavbarLogo();
|
||||
navbarObserver.disconnect();
|
||||
}, {once: true});
|
||||
darkMode.addEventListener("change", updateNavbarLogo);
|
||||
window.addEventListener("pageshow", updateNavbarLogo);
|
||||
document.addEventListener("visibilitychange", () => {
|
||||
if (!document.hidden) updateNavbarLogo();
|
||||
});
|
||||
})();
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user