mirror of
https://github.com/imfing/hextra.git
synced 2025-08-23 15:56:36 -04:00
fix(favicon): dynamic favicon switching based on color scheme in js (#735)
* fix(favicon): dynamic favicon switching based on color scheme in js * refactor(favicon): simplify favicon logic and ensure dynamic switching based on color scheme * docs(favicon): enhance favicon setup instructions with dark mode support and adaptive SVG guidance
This commit is contained in:
22
assets/js/favicon.js
Normal file
22
assets/js/favicon.js
Normal file
@@ -0,0 +1,22 @@
|
||||
// {{ $faviconDarkExists := fileExists (path.Join "static" "favicon-dark.svg") }}
|
||||
(function () {
|
||||
const faviconEl = document.getElementById("favicon-svg");
|
||||
const faviconDarkExists = "{{ $faviconDarkExists }}" === "true";
|
||||
|
||||
if (faviconEl && faviconDarkExists) {
|
||||
const lightFavicon = '{{ "favicon.svg" | relURL }}';
|
||||
const darkFavicon = '{{ "favicon-dark.svg" | relURL }}';
|
||||
|
||||
const darkModeQuery = window.matchMedia("(prefers-color-scheme: dark)");
|
||||
|
||||
function updateFavicon(e) {
|
||||
faviconEl.href = e.matches ? darkFavicon : lightFavicon;
|
||||
}
|
||||
|
||||
// Set favicon on load
|
||||
updateFavicon(darkModeQuery);
|
||||
|
||||
// Listen for system preference changes
|
||||
darkModeQuery.addEventListener("change", updateFavicon);
|
||||
}
|
||||
})();
|
Reference in New Issue
Block a user