-
Notifications
You must be signed in to change notification settings - Fork 1
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
Showing
4 changed files
with
52 additions
and
0 deletions.
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
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,20 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import useLocalStorage from "./useLocalStorage"; | ||
|
||
export default function LightDarkMode() { | ||
|
||
const [theme, setTheme] = useLocalStorage('theme', 'dark') | ||
|
||
function handleToggleTheme() { | ||
setTheme(theme === 'light' ? 'dark' : 'light') | ||
} | ||
|
||
return ( | ||
<div className="flex flex-col gap-3 flex-1 justify-center items-start data-[theme='dark']:bg-black data-[theme='light']:bg-white" data-theme={theme}> | ||
<div className="container space-y-8"> | ||
<p className="data-[theme='dark']:text-white data-[theme='light']:text-black text-4xl" data-theme={theme}>Hello World !</p> | ||
<Button className="data-[theme='dark']:bg-white data-[theme='dark']:text-black data-[theme='light']:bg-black data-[theme='light']:text-white ml-2" data-theme={theme} onClick={handleToggleTheme}>Change Theme</Button> | ||
</div> | ||
</div> | ||
); | ||
} |
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,24 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
export default function useLocalStorage(key, defaultValue) { | ||
const [value, setValue] = useState(() => { | ||
let currentValue; | ||
|
||
try { | ||
currentValue = JSON.parse( | ||
localStorage.getItem(key) || String(defaultValue), | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
currentValue = defaultValue; | ||
} | ||
|
||
return currentValue; | ||
}); | ||
|
||
useEffect(() => { | ||
localStorage.setItem(key, JSON.stringify(value)); | ||
}, [key, value]); | ||
|
||
return [value, setValue]; | ||
} |