Skip to content

Instantly share code, notes, and snippets.

@markbosky
Created December 16, 2021 22:04
Show Gist options
  • Select an option

  • Save markbosky/ef2c152058cb65ac0f5a8ba5960fcf09 to your computer and use it in GitHub Desktop.

Select an option

Save markbosky/ef2c152058cb65ac0f5a8ba5960fcf09 to your computer and use it in GitHub Desktop.
Light and Dark Favicon Based on User's Browser Theme
jQuery(document).ready(function($) {
'use strict';
function setupIcons() {
const lightSchemeIcon = document.querySelector('link#light-scheme-icon');
const darkSchemeIcon = document.querySelector('link#dark-scheme-icon');
function setLight() {
document.head.append(lightSchemeIcon);
darkSchemeIcon.remove();
}
function setDark() {
lightSchemeIcon.remove();
document.head.append(darkSchemeIcon);
}
const matcher = window.matchMedia('(prefers-color-scheme:dark)');
function onUpdate() {
if (matcher.matches) {
setDark();
} else {
setLight();
}
}
matcher.addListener(onUpdate);
onUpdate();
}
setupIcons();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment