Created
December 16, 2021 22:04
-
-
Save markbosky/ef2c152058cb65ac0f5a8ba5960fcf09 to your computer and use it in GitHub Desktop.
Light and Dark Favicon Based on User's Browser Theme
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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