From d7fadb37571702d752f6d486d04d77497d0cdcf7 Mon Sep 17 00:00:00 2001 From: Mastan Sayyad Date: Mon, 22 Jul 2024 05:21:28 +0530 Subject: [PATCH] Update Theme.Togglr.jsx --- src/components/ThemeToggle.jsx | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/ThemeToggle.jsx b/src/components/ThemeToggle.jsx index 051b668..fce1bf4 100644 --- a/src/components/ThemeToggle.jsx +++ b/src/components/ThemeToggle.jsx @@ -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 ( @@ -34,4 +38,3 @@ const ThemeToggle = () => { }; export default ThemeToggle; -