diff options
Diffstat (limited to 'layouts/partials/header/theme-button.html')
| -rw-r--r-- | layouts/partials/header/theme-button.html | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/layouts/partials/header/theme-button.html b/layouts/partials/header/theme-button.html new file mode 100644 index 0000000..0a8313b --- /dev/null +++ b/layouts/partials/header/theme-button.html @@ -0,0 +1,47 @@ +<button class="toggle-theme" aria-label="Toggle Theme" title="Toggle Theme" onclick="toggleTheme()"> + <span class="theme-icon light"> {{ partial "utils/icon" "sun" }} </span> + <span class="theme-icon dark"> {{ partial "utils/icon" "moon" }} </span> +</button> + +<script> + document.addEventListener('DOMContentLoaded', function () { + const theme = localStorage.getItem('theme'); + + if (!theme || theme === 'light') { + setTheme('light'); + } else { + setTheme(theme); + } + }); + + function setTheme(theme) { + const html = document.querySelector('html'); + localStorage.setItem('theme', theme); + + if (theme === 'light') { + if (html.classList.contains('dark')) { + document.querySelector('html').classList.remove('dark'); + } + + document.querySelector('.theme-icon.light').style.display = 'none'; + document.querySelector('.theme-icon.dark').style.display = 'block'; + } else { + if (!html.classList.contains('dark')) { + document.querySelector('html').classList.add('dark'); + } + + document.querySelector('.theme-icon.dark').style.display = 'none'; + document.querySelector('.theme-icon.light').style.display = 'block'; + } + } + + function toggleTheme() { + const theme = localStorage.getItem('theme'); + + if (theme === 'light') { + setTheme('dark'); + } else { + setTheme('light'); + } + } +</script>
\ No newline at end of file |
