I have successfully implemented a function to switch between light and dark mode on my webpage using VueJS. However, I am facing an issue with the code:
<a @click="toggleTheme" class="switch-mode" href>
<div class="switch-mode-btn">
</div>
</a>
toggleTheme() {
let currentTheme = localStorage.getItem('theme');
currentTheme == 'darkMode' ? currentTheme = '' : currentTheme = 'darkMode';
document.documentElement.setAttribute('data-theme', currentTheme);
localStorage.setItem('theme', currentTheme);
}
mounted() {
let localTheme = localStorage.getItem('theme');
document.documentElement.setAttribute('data-theme', localTheme);
},
css
:root {
/* LIGHT MODE */
--background-color: #fcfcfc;
--text-color: rgba(17, 24, 39);
/* END LIGHT MODE */
}
[data-theme="darkMode"] {
/* DARK MODE */
--background-color: rgba(17, 24, 39);
--text-color: #fcfcfc;
/* END DARK MODE */
}
Whenever I click on the toggleTheme
function, my page reloads. Is there a way to make this dynamic without refreshing the page? Any help would be greatly appreciated.
Thank you