Skip to content

Commit

Permalink
add favicons with theme support
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Sep 29, 2024
1 parent 9943c5a commit 6d5f4e1
Show file tree
Hide file tree
Showing 8 changed files with 1,734 additions and 25 deletions.
8 changes: 8 additions & 0 deletions assets/src/images/logo-lofi.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions assets/src/images/logo-night.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions assets/src/images/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion assets/src/js/lib/themeSelector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ const storageKey = 'theme';
function getThemes(themeSelector) {
return themeSelector.getAttribute('data-toggle-theme').split(',').map((t) => t.trim());
}
function changeFavicons(current) {
const els = document.querySelectorAll('link[rel~="icon"]')
for (const el of els) {
el.href = el.href.replace(/\w+\/favicon/, `${current}/favicon`);
}
}
export function initTheme(themeSelector) {
const [darkTheme, lightTheme] = getThemes(themeSelector);
let currentTheme = window.localStorage.getItem(storageKey);
Expand All @@ -11,7 +17,10 @@ export function initTheme(themeSelector) {
currentTheme = lightTheme;
}
}
if (currentTheme === lightTheme) themeSelector.checked = true;
if (currentTheme === lightTheme) {
themeSelector.checked = true;
changeFavicons(currentTheme);
}
document.querySelector('html').setAttribute('data-theme', currentTheme);
window.localStorage.setItem(storageKey, currentTheme);
}
Expand All @@ -22,6 +31,7 @@ export function themeSelector(themeSelector) {
themeSelector.addEventListener('change', (e) => {
currentTheme = e.target.checked ? lightTheme : darkTheme;
document.querySelector('html').setAttribute('data-theme', currentTheme);
changeFavicons(currentTheme);
window.localStorage.setItem(storageKey, currentTheme);
});
}
Loading

0 comments on commit 6d5f4e1

Please sign in to comment.