hugo-theme-hello-friend/source/js/theme.js

17 lines
513 B
JavaScript
Raw Normal View History

2018-07-20 19:14:22 +02:00
// Toggle theme
const getTheme = window.localStorage && window.localStorage.getItem('theme')
2018-07-20 19:14:22 +02:00
const themeToggle = document.querySelector('.theme-toggle')
const isDark = getTheme === 'dark' || getTheme === null
2018-07-20 19:14:22 +02:00
document.body.classList.toggle('dark-theme', isDark)
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-theme')
window.localStorage &&
window.localStorage.setItem(
'theme',
document.body.classList.contains('dark-theme') ? 'dark' : 'light',
)
})