Bauke/tildes-issue-log
Bauke
/
tildes-issue-log
Archived
1
Fork 0
This repository has been archived on 2022-10-04. You can view files and clone it, but cannot push or open issues or pull requests.
tildes-issue-log/source/pages/js/theme-switcher.js

25 lines
927 B
JavaScript

window.addEventListener('load', () => {
// On page load, change the theme to light if they have that selected.
// Dark is selected by default.
const themeToggleButton = document.querySelector('button#theme-toggle');
if (window.localStorage.getItem('theme') === 'light-theme') {
document.body.setAttribute('id', 'light-theme');
themeToggleButton.classList.add('light');
}
// When the theme button is clicked, switch the theme to the other one.
themeToggleButton.addEventListener('click', (event) => {
event.preventDefault();
if (document.body.getAttribute('id') === 'dark-theme') {
document.body.setAttribute('id', 'light-theme');
themeToggleButton.classList.add('light');
} else {
document.body.setAttribute('id', 'dark-theme');
themeToggleButton.classList.remove('light');
}
window.localStorage.setItem('theme', document.body.getAttribute('id'));
});
});