1/** 2 3Doxygen Awesome 4https://github.com/jothepro/doxygen-awesome-css 5 6MIT License 7 8Copyright (c) 2021 jothepro 9 10Permission is hereby granted, free of charge, to any person obtaining a copy 11of this software and associated documentation files (the "Software"), to deal 12in the Software without restriction, including without limitation the rights 13to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 14copies of the Software, and to permit persons to whom the Software is 15furnished to do so, subject to the following conditions: 16 17The above copyright notice and this permission notice shall be included in all 18copies or substantial portions of the Software. 19 20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 25OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 26SOFTWARE. 27 28*/ 29 30class DoxygenAwesomeDarkModeToggle extends HTMLElement { 31 static prefersLightModeInDarkModeKey = "prefers-light-mode-in-dark-mode" 32 static prefersDarkModeInLightModeKey = "prefers-dark-mode-in-light-mode" 33 34 static _staticConstructor = function() { 35 DoxygenAwesomeDarkModeToggle.darkModeEnabled = DoxygenAwesomeDarkModeToggle.userPreference 36 DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.darkModeEnabled) 37 // Update the color scheme when the browsers preference changes 38 // without user interaction on the website. 39 window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', event => { 40 DoxygenAwesomeDarkModeToggle.onSystemPreferenceChanged() 41 }) 42 // Update the color scheme when the tab is made visible again. 43 // It is possible that the appearance was changed in another tab 44 // while this tab was in the background. 45 document.addEventListener("visibilitychange", visibilityState => { 46 if (document.visibilityState === 'visible') { 47 DoxygenAwesomeDarkModeToggle.onSystemPreferenceChanged() 48 } 49 }); 50 }() 51 52 constructor() { 53 super(); 54 this.onclick=this.toggleDarkMode 55 } 56 57 /** 58 * @returns `true` for dark-mode, `false` for light-mode system preference 59 */ 60 static get systemPreference() { 61 return window.matchMedia('(prefers-color-scheme: dark)').matches 62 } 63 64 /** 65 * @returns `true` for dark-mode, `false` for light-mode user preference 66 */ 67 static get userPreference() { 68 return (!DoxygenAwesomeDarkModeToggle.systemPreference && localStorage.getItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey)) || 69 (DoxygenAwesomeDarkModeToggle.systemPreference && !localStorage.getItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey)) 70 } 71 72 static set userPreference(userPreference) { 73 DoxygenAwesomeDarkModeToggle.darkModeEnabled = userPreference 74 if(!userPreference) { 75 if(DoxygenAwesomeDarkModeToggle.systemPreference) { 76 localStorage.setItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey, true) 77 } else { 78 localStorage.removeItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey) 79 } 80 } else { 81 if(!DoxygenAwesomeDarkModeToggle.systemPreference) { 82 localStorage.setItem(DoxygenAwesomeDarkModeToggle.prefersDarkModeInLightModeKey, true) 83 } else { 84 localStorage.removeItem(DoxygenAwesomeDarkModeToggle.prefersLightModeInDarkModeKey) 85 } 86 } 87 DoxygenAwesomeDarkModeToggle.onUserPreferenceChanged() 88 } 89 90 static enableDarkMode(enable) { 91 let head = document.getElementsByTagName('head')[0] 92 if(enable) { 93 document.documentElement.classList.add("dark-mode") 94 document.documentElement.classList.remove("light-mode") 95 } else { 96 document.documentElement.classList.remove("dark-mode") 97 document.documentElement.classList.add("light-mode") 98 } 99 } 100 101 static onSystemPreferenceChanged() { 102 DoxygenAwesomeDarkModeToggle.darkModeEnabled = DoxygenAwesomeDarkModeToggle.userPreference 103 DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.darkModeEnabled) 104 } 105 106 static onUserPreferenceChanged() { 107 DoxygenAwesomeDarkModeToggle.enableDarkMode(DoxygenAwesomeDarkModeToggle.darkModeEnabled) 108 } 109 110 toggleDarkMode() { 111 DoxygenAwesomeDarkModeToggle.userPreference = !DoxygenAwesomeDarkModeToggle.userPreference 112 } 113} 114 115customElements.define("doxygen-awesome-dark-mode-toggle", DoxygenAwesomeDarkModeToggle); 116