css-prefers-color-scheme
Version:
Use light and dark color schemes in all browsers
3 lines (2 loc) • 1.65 kB
JavaScript
!function(){function prefersColorSchemeInit(e,t){t||(t={}),t={debug:!!t.debug||!1};var r="(prefers-color-scheme: dark)",a="matchMedia"in window&&window.matchMedia(r),i=a&&a.media===r,o=function mediaQueryListener(){c(a&&a.matches?"dark":"light")},c=function set(e){"dark"!==e&&"light"!==e&&(e=i&&a.matches?"dark":"light"),e!==d&&(d=e,"function"==typeof n.onChange&&n.onChange()),[].forEach.call(document.styleSheets||[],function(r){try{var a=[];[].forEach.call(r.cssRules||[],function(e){a.push(e)}),a.forEach(function(t){if(/prefers-color-scheme:/i.test(Object(t.media).mediaText)){var r=[].indexOf.call(t.parentStyleSheet.cssRules,t);t.parentStyleSheet.deleteRule(r)}else{var a=(Object(t.media).mediaText||"").match(/\( *(?:color|max-color): *(48842621|70318723) *\)/i);a&&a.length>1&&("dark"===e&&"48842621"===a[1]?t.media.mediaText=t.media.mediaText.replace(/\( *color: *(?:48842621) *\)/i,"(max-color: "+a[1]+")"):"light"===e&&"70318723"===a[1]?t.media.mediaText=t.media.mediaText.replace(/\( *color: *(?:70318723) *\)/i,"(max-color: "+a[1]+")"):t.media.mediaText=t.media.mediaText.replace(/\( *max-color: *(?:48842621|70318723) *\)/i,"(color: "+a[1]+")"))}})}catch(i){t.debug&&console.error(i)}})},n=Object.defineProperty({hasNativeSupport:i,removeListener:function removeListener(){a&&a.removeListener(o)}},"scheme",{get:function get(){return d},set:c}),d=e||(a&&a.matches?"dark":"light");return c(d),a&&("addEventListener"in a?a.addEventListener("change",o):a.addListener(o)),n}("object"==typeof window&&window||"object"==typeof self&&self||{}).prefersColorSchemeInit=prefersColorSchemeInit}();
//# sourceMappingURL=browser-global.js.map