Skip to content

Instantly share code, notes, and snippets.

@jgphilpott
Last active September 25, 2023 11:36
Show Gist options
  • Save jgphilpott/efdec642fc8bbaaae8115bd4fc49bd05 to your computer and use it in GitHub Desktop.
Save jgphilpott/efdec642fc8bbaaae8115bd4fc49bd05 to your computer and use it in GitHub Desktop.
Code to change favicons depending on a users scheme preference.
# Credit: https://stackoverflow.com/a/55252008/1544937
lightSchemeIcon = document.querySelector "link#light-scheme-icon"
darkSchemeIcon = document.querySelector "link#dark-scheme-icon"
schemeMatcher = window.matchMedia "(prefers-color-scheme: light)"
schemeUpdate = ->
if schemeMatcher.matches
document.head.append lightSchemeIcon
darkSchemeIcon.remove()
window.scheme = "light"
else
document.head.append darkSchemeIcon
lightSchemeIcon.remove()
window.scheme = "dark"
schemeMatcher.addListener schemeUpdate
schemeUpdate()
// Credit: https://stackoverflow.com/a/55252008/1544937
var darkSchemeIcon, lightSchemeIcon, schemeMatcher, schemeUpdate;
lightSchemeIcon = document.querySelector("link#light-scheme-icon");
darkSchemeIcon = document.querySelector("link#dark-scheme-icon");
schemeMatcher = window.matchMedia("(prefers-color-scheme: light)");
schemeUpdate = function() {
if (schemeMatcher.matches) {
document.head.append(lightSchemeIcon);
darkSchemeIcon.remove();
return window.scheme = "light";
} else {
document.head.append(darkSchemeIcon);
lightSchemeIcon.remove();
return window.scheme = "dark";
}
};
schemeMatcher.addListener(schemeUpdate);
schemeUpdate();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment