-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f20b726
commit 0419b52
Showing
11 changed files
with
312 additions
and
207 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { HiOutlineMoon, HiOutlineSun } from "react-icons/hi"; | ||
|
||
const DarkModeToggle = () => { | ||
const [darkMode, setDarkMode] = useState(() => { | ||
const savedMode = localStorage.getItem('dark-mode'); | ||
return savedMode !== null ? JSON.parse(savedMode) : false; | ||
}); | ||
|
||
useEffect(() => { | ||
const root = document.documentElement; | ||
if (darkMode) { | ||
root.classList.add('dark'); | ||
} else { | ||
root.classList.remove('dark'); | ||
} | ||
localStorage.setItem('dark-mode', JSON.stringify(darkMode)); | ||
}, [darkMode]); | ||
|
||
return ( | ||
<button | ||
onClick={() => setDarkMode(!darkMode)} | ||
className="py-2 px-4 text-gray-800 dark:text-gray-200 rounded-full transition-all duration-300 ease-in-out" | ||
> | ||
{darkMode ? ( | ||
<HiOutlineSun className="h-5 w-5 mr-1" /> | ||
) : ( | ||
<HiOutlineMoon className="h-5 w-5 mr-1" /> | ||
)} | ||
</button> | ||
); | ||
}; | ||
|
||
export default DarkModeToggle; |
Oops, something went wrong.