Skip to content

Commit

Permalink
Update Theme.Togglr.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
MastanSayyad committed Jul 21, 2024
1 parent 1ad4b53 commit d7fadb3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/components/ThemeToggle.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ const ThemeToggle = () => {
useEffect(() => {
const savedTheme = localStorage.getItem('theme') || 'light';
setTheme(savedTheme);
document.documentElement.classList.add(`theme-${savedTheme}`);
applyTheme(savedTheme);
}, []);

const applyTheme = (selectedTheme) => {
document.documentElement.classList.remove(`theme-${theme}`);
document.documentElement.classList.add(`theme-${selectedTheme}`);
localStorage.setItem('theme', selectedTheme);
};

const toggleTheme = () => {
const newTheme = theme === 'light' ? 'dark' : 'light';
setTheme(newTheme);
document.documentElement.classList.remove(`theme-${theme}`);
document.documentElement.classList.add(`theme-${newTheme}`);
localStorage.setItem('theme', newTheme);
applyTheme(newTheme);
};

return (
Expand All @@ -34,4 +38,3 @@ const ThemeToggle = () => {
};

export default ThemeToggle;

0 comments on commit d7fadb3

Please sign in to comment.