From 14da9f50105f3bde55cff2daf48b71d9fe170d6f Mon Sep 17 00:00:00 2001 From: Aryan Ramesh Jain <138214350+jainaryan04@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:31:39 +0530 Subject: [PATCH] initial #469: add hook and toggle --- frontend/src/Pages/Herosection.jsx | 4 +++- frontend/src/components/ThemeToggle.jsx | 32 +++++++++++++++++++++++++ frontend/src/hooks/useDarkMode.js | 16 +++++++++++++ 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 frontend/src/components/ThemeToggle.jsx create mode 100644 frontend/src/hooks/useDarkMode.js diff --git a/frontend/src/Pages/Herosection.jsx b/frontend/src/Pages/Herosection.jsx index 750dcf4..3fbc936 100644 --- a/frontend/src/Pages/Herosection.jsx +++ b/frontend/src/Pages/Herosection.jsx @@ -1,5 +1,6 @@ import React from "react"; import "./Herosection.css"; +import ThemeToggle from "../components/ThemeToggle" import logo from "../assets/stationsaarthi.svg"; import navigationsvg from "../assets/svg/navigation.svg"; import bookingsvg from "../assets/svg/bookings.svg"; @@ -56,8 +57,9 @@ const Herosection = () => { return ( <>
-
+
+
diff --git a/frontend/src/components/ThemeToggle.jsx b/frontend/src/components/ThemeToggle.jsx new file mode 100644 index 0000000..d7d0c43 --- /dev/null +++ b/frontend/src/components/ThemeToggle.jsx @@ -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 ( +
+ + + +
+ ); +}; + +export default ThemeSwitcher; \ No newline at end of file diff --git a/frontend/src/hooks/useDarkMode.js b/frontend/src/hooks/useDarkMode.js new file mode 100644 index 0000000..1513686 --- /dev/null +++ b/frontend/src/hooks/useDarkMode.js @@ -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; \ No newline at end of file