forked from dhairyagothi/StationGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
initial dhairyagothi#469: add hook and toggle
- Loading branch information
1 parent
6c5b560
commit 14da9f5
Showing
3 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
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,32 @@ | ||
import { FaSun, FaMoon } from 'react-icons/fa'; | ||
import useDarkMode from '../hooks/useDarkMode'; | ||
import "../../src/index.css"; | ||
|
||
const ThemeSwitcher = () => { | ||
const [theme, setTheme] = useDarkMode(); | ||
const isDarkMode = theme === 'dark'; | ||
|
||
return ( | ||
<div className="flex flex-row justify-center items-center"> | ||
<label className="relative inline-block w-16 h-9"> | ||
<input | ||
type="checkbox" | ||
className="sr-only peer" | ||
checked={isDarkMode} | ||
onChange={() => setTheme(isDarkMode ? 'light' : 'dark')} | ||
/> | ||
<span className="block w-full h-full bg-blue-500 rounded-3xl border-2 border-blue-400 cursor-pointer peer-checked:bg-slate-800 peer-checked:border-blue-400 transition-colors duration-400"></span> | ||
|
||
{/* Sun Icon for Light Mode */} | ||
<span className="absolute top-1/2 left-[0.3em] w-4 h-4 bg-gradient-to-r from-pink-500 to-orange-400 rounded-full transform -translate-y-1/2 transition-all duration-400 opacity-100 peer-checked:opacity-0"></span> | ||
|
||
{/* Moon Icon for Dark Mode */} | ||
<span className="absolute top-1/2 left-[calc(100%-1.3em)] w-4 h-4 bg-transparent rounded-full transform -translate-y-1/2 transition-all duration-400 opacity-0 peer-checked:opacity-100 peer-checked:shadow-[inset_-3px_-2px_5px_-2px_#8983f7,inset_-8px_-4px_0_0_#a3dafb]"></span> | ||
</label> | ||
|
||
|
||
</div> | ||
); | ||
}; | ||
|
||
export default ThemeSwitcher; |
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,16 @@ | ||
import { useState, useEffect } from 'react'; | ||
|
||
const useDarkMode = () => { | ||
const [theme, setTheme] = useState(() => localStorage.getItem('theme') || 'light'); | ||
|
||
useEffect(() => { | ||
const root = document.documentElement; | ||
const isDark = theme === 'dark'; | ||
root.classList.toggle('dark', isDark); | ||
localStorage.setItem('theme', theme); | ||
}, [theme]); | ||
|
||
return [theme, setTheme]; | ||
}; | ||
|
||
export default useDarkMode; |