76 lines
		
	
	
		
			No EOL
		
	
	
		
			2.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			No EOL
		
	
	
		
			2.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
| {{ $style := "light-without-switcher" }}
 | |
| {{ if and (isset site.Params "style") (ne site.Params.style "") }}
 | |
|     {{ $style = site.Params.style | lower }}
 | |
| {{ end }}
 | |
| 
 | |
| {{ if not (in (slice "light-without-switcher" "dark-without-switcher" "auto-without-switcher") $style) }}
 | |
|     <button class="theme-switcher">
 | |
|         Dark theme
 | |
|     </button>
 | |
| 
 | |
|     <script>
 | |
|     const STORAGE_KEY = 'user-color-scheme'
 | |
|     const defaultTheme = {{ $style }}
 | |
| 
 | |
|     let currentTheme
 | |
|     let switchButton
 | |
|     let autoDefinedScheme = window.matchMedia('(prefers-color-scheme: dark)')
 | |
| 
 | |
|     const autoChangeScheme = e => {
 | |
|         currentTheme = e.matches ? 'dark' : 'light'
 | |
|         document.documentElement.setAttribute('data-theme', currentTheme)
 | |
|         changeButtonText()
 | |
|     }
 | |
| 
 | |
|     document.addEventListener('DOMContentLoaded', function() {
 | |
|         switchButton = document.querySelector('.theme-switcher')
 | |
|         currentTheme = detectCurrentScheme()
 | |
|         if (currentTheme == 'dark') {
 | |
|             document.documentElement.setAttribute('data-theme', 'dark')
 | |
|         }
 | |
|         if (currentTheme == 'auto') {
 | |
|             autoChangeScheme(autoDefinedScheme);
 | |
|             autoDefinedScheme.addListener(autoChangeScheme);
 | |
|         }
 | |
|         changeButtonText()
 | |
|         switchButton.addEventListener('click', switchTheme, false)
 | |
|     })
 | |
| 
 | |
| 
 | |
| 
 | |
|     function detectCurrentScheme() {
 | |
|         if (localStorage.getItem(STORAGE_KEY)) {
 | |
|             return localStorage.getItem(STORAGE_KEY)
 | |
|         } 
 | |
|         if (defaultTheme) {
 | |
|             return defaultTheme
 | |
|         } 
 | |
|         if (!window.matchMedia) {
 | |
|             return 'light'
 | |
|         } 
 | |
|         if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
 | |
|             return 'dark'
 | |
|         }
 | |
|         return 'light'
 | |
|     }
 | |
| 
 | |
|     function changeButtonText()
 | |
|     {
 | |
|         console.log(currentTheme)
 | |
|         switchButton.textContent = currentTheme == 'dark' ? 'Light theme' : 'Dark theme'
 | |
|     }
 | |
| 
 | |
|     function switchTheme(e) {
 | |
|         if (currentTheme == 'dark') {
 | |
|             localStorage.setItem(STORAGE_KEY, 'light')
 | |
|             document.documentElement.setAttribute('data-theme', 'light')
 | |
|             currentTheme = 'light'
 | |
|         } else {
 | |
|             localStorage.setItem(STORAGE_KEY, 'dark')
 | |
|             document.documentElement.setAttribute('data-theme', 'dark')
 | |
|             currentTheme = 'dark'
 | |
|         }
 | |
|         changeButtonText()
 | |
|     }
 | |
|     </script>
 | |
| {{ end }} |