Skip to content

Commit

Permalink
Wrap in requestAnimationFrame
Browse files Browse the repository at this point in the history
  • Loading branch information
zer0kool committed Aug 21, 2024
1 parent 3ca91ae commit c11125d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ const themeMap = {
dark: "light"
};

const theme = localStorage.getItem('theme')
|| (tmp = Object.keys(themeMap)[0],
localStorage.setItem('theme', tmp),
tmp);
const theme = localStorage.getItem('theme') || (tmp => {
localStorage.setItem('theme', tmp = Object.keys(themeMap)[0]);
return tmp;
})();
const bodyClass = document.body.classList;
bodyClass.add(theme);

function toggleTheme() {
const current = localStorage.getItem('theme');
const next = themeMap[current];

bodyClass.replace(current, next);
localStorage.setItem('theme', next);
requestAnimationFrame(() => { // Wrap in requestAnimationFrame
bodyClass.replace(current, next);
localStorage.setItem('theme', next);
});
}

document.getElementById('themeButton').onclick = toggleTheme;

0 comments on commit c11125d

Please sign in to comment.