Skip to content

Instantly share code, notes, and snippets.

@jodyheavener
Last active March 31, 2020 19:43
Show Gist options
  • Select an option

  • Save jodyheavener/e2234d8d9c269c613944bb177e3bf9fb to your computer and use it in GitHub Desktop.

Select an option

Save jodyheavener/e2234d8d9c269c613944bb177e3bf9fb to your computer and use it in GitHub Desktop.
function setFavicon(scheme) {
const svgFavicon = document.querySelector('.js-site-favicon[type="image/svg+xml"]'),
pngFavicon = document.querySelector('.js-site-favicon[type="image/png"]');
scheme || (scheme = "light");
if (svgFavicon && pngFavicon) {
svgFavicon.href = `favicon-${scheme}.svg`;
pngFavicon.href = `favicon-${scheme}.png`;
}
}
function prefersDark() {
return window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches;
}
prefersDark() && setFavicon("dark");
window.matchMedia("(prefers-color-scheme: dark)").addListener(() => {
setFavicon(prefersDark() ? "dark" : "light");
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment