Skip to content

Commit

Permalink
fixed toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
RchtDshr committed Sep 20, 2024
1 parent 4376660 commit 343cd43
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
29 changes: 15 additions & 14 deletions src/Components/ThemeToggle.jsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { useState, useEffect } from 'react';
import { Moon, Sun } from 'lucide-react';
import { initializeTheme, toggleTheme } from '../utils/theme.js';

const ThemeToggleButton = () => {
const [darkMode, setDarkMode] = useState(false);
const [darkMode, setDarkMode] = useState(false);

useEffect(() => {
const isDarkMode = localStorage.theme === 'dark' ||
(!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches);
setDarkMode(isDarkMode);
document.documentElement.classList.toggle('dark', isDarkMode);
}, []);

const toggleTheme = () => {
setDarkMode(!darkMode);
document.documentElement.classList.toggle('dark');
localStorage.theme = darkMode ? 'light' : 'dark';
};
// Initialize theme based on user's preference or system preference on mount
useEffect(() => {
initializeTheme();
const savedTheme = localStorage.getItem('theme') || (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
setDarkMode(savedTheme === 'dark');
}, []);
// Toggle dark mode
const handleToggle = () => {
toggleTheme();
setDarkMode(!darkMode); // Flip state
};

return (
<button
onClick={toggleTheme}
onClick={handleToggle}
className="relative w-16 h-8 rounded-full bg-primary dark:bg-white focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 transition-colors duration-200 ease-in-out"
aria-label="Toggle Dark Mode"
>
Expand Down
12 changes: 6 additions & 6 deletions src/Home/Home.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import ThemeToggleButton from "../Components/ThemeToggle"

const Home = () => {
return (
<div>
Home
</div>
);
};
<div>Home</div>
)
}

export default Home;
export default Home

0 comments on commit 343cd43

Please sign in to comment.