diff --git a/src/App.jsx b/src/App.jsx index eda2925..44b6180 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -14,6 +14,7 @@ import ImageSlider from "@/pages/ImageSlider"; import LoadMoreData from "./pages/LoadMoreData"; import TreeView from "./pages/tree-view/TreeView"; import QRCodeGenerator from "./pages/QRCodeGenerator"; +import LightDarkMode from "./pages/light-dark-mode/light-dark-mode"; function App() { return ( @@ -41,6 +42,8 @@ function App() { } /> {/* generate QR code */} } /> + {/* Light Dark Mode */} + } /> {/* Error Page */} } /> diff --git a/src/data/projects.js b/src/data/projects.js index c1ec553..68115cf 100644 --- a/src/data/projects.js +++ b/src/data/projects.js @@ -33,6 +33,11 @@ export const projects = [ id: 7, name: "QR Code Generator", path: 'qr-code-generator' + }, + { + id: 8, + name: 'Light Dark Mode', + path: 'light-dark-mode', } ]; diff --git a/src/pages/light-dark-mode/light-dark-mode.jsx b/src/pages/light-dark-mode/light-dark-mode.jsx new file mode 100644 index 0000000..6957346 --- /dev/null +++ b/src/pages/light-dark-mode/light-dark-mode.jsx @@ -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 ( +
+
+

Hello World !

+ +
+
+ ); +} diff --git a/src/pages/light-dark-mode/useLocalStorage.jsx b/src/pages/light-dark-mode/useLocalStorage.jsx new file mode 100644 index 0000000..c09df41 --- /dev/null +++ b/src/pages/light-dark-mode/useLocalStorage.jsx @@ -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]; +}