From be5649deef97c626b399471073396e4334a57ae2 Mon Sep 17 00:00:00 2001 From: Ayushmaanagarwal1121 Date: Sat, 1 Jun 2024 19:49:42 +0530 Subject: [PATCH] added dark mode --- frontend/src/App.js | 8 +++-- .../src/components/LoginComponents/Input.js | 9 +++--- .../src/components/LoginComponents/Main.js | 22 ++++++++++---- .../src/components/SignupComponents/Main.js | 20 ++++++++----- .../components/ToggleMode/DarkModeContext.js | 30 +++++++++++++++++++ frontend/src/pages/Home.js | 9 +++--- frontend/src/pages/Service.js | 8 +++-- 7 files changed, 80 insertions(+), 26 deletions(-) create mode 100644 frontend/src/components/ToggleMode/DarkModeContext.js diff --git a/frontend/src/App.js b/frontend/src/App.js index eb5b42d..df1d680 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -13,6 +13,7 @@ import Settings from './pages/Settings'; import Root,{loader as loadingAction} from './pages/Root'; import Search from './pages/Search'; +import { DarkModeProvider } from './components/ToggleMode/DarkModeContext'; const router = createBrowserRouter([ @@ -60,8 +61,11 @@ const router = createBrowserRouter([ } ]); function App() { - return (); -} + return ( + + + +);} export default App; diff --git a/frontend/src/components/LoginComponents/Input.js b/frontend/src/components/LoginComponents/Input.js index 862de15..2c9e4d7 100644 --- a/frontend/src/components/LoginComponents/Input.js +++ b/frontend/src/components/LoginComponents/Input.js @@ -1,6 +1,7 @@ -import React from 'react' +import React, { useContext, useState } from 'react' +import { DarkModeContext } from '../ToggleMode/DarkModeContext'; export default function Input({text,type,placeholder,onSetData,name}) { - + const {isDarkMode}=useContext(DarkModeContext) function setInput(e) { onSetData((data)=>{ @@ -13,8 +14,8 @@ export default function Input({text,type,placeholder,onSetData,name}) { return (
-
{text}
- +
{text}
+
) } diff --git a/frontend/src/components/LoginComponents/Main.js b/frontend/src/components/LoginComponents/Main.js index 2c99de4..4a280af 100644 --- a/frontend/src/components/LoginComponents/Main.js +++ b/frontend/src/components/LoginComponents/Main.js @@ -1,4 +1,4 @@ -import React,{useState} from 'react' +import React,{useContext, useState} from 'react' import { Link } from 'react-router-dom' import Input from './Input' import Square from './Square' @@ -14,10 +14,14 @@ import LockOpenIcon from '@mui/icons-material/LockOpen'; import { ToastContainer, toast } from "react-toastify"; import Visibility from '@mui/icons-material/Visibility'; import VisibilityOff from '@mui/icons-material/VisibilityOff'; +import { DarkModeContext } from '../ToggleMode/DarkModeContext' +import {FaSun,FaMoon} from 'react-icons/fa6' + export default function Main() { const submit=useSubmit(); + const {isDarkMode,toggleMode}=useContext(DarkModeContext) const [loginData,setloginData]=useState({email:'',password:''}); const [submitting,setSubmiting]=useState(false) const [showPassword, setShowPassword] = useState(false); @@ -73,18 +77,23 @@ export default function Main() { const handleMouseDownPassword = (event) => event.preventDefault(); // Prevent default action on mouse down return ( -
+ <> + +
+ +
+ - + -
+
Log In


-
+
}
- Already have and account ? SignUp + Already have and account ? SignUp
@@ -123,5 +132,6 @@ export default function Main() {
+ ) } diff --git a/frontend/src/components/SignupComponents/Main.js b/frontend/src/components/SignupComponents/Main.js index 910b306..3b0367c 100644 --- a/frontend/src/components/SignupComponents/Main.js +++ b/frontend/src/components/SignupComponents/Main.js @@ -1,4 +1,4 @@ -import React,{useState} from 'react' +import React,{useContext, useState} from 'react' import Input from '../LoginComponents/Input' import Square from '../LoginComponents/Square' import { GoogleLogin } from '@react-oauth/google' @@ -13,12 +13,15 @@ import HowToRegIcon from '@mui/icons-material/HowToReg'; import { ToastContainer, toast } from "react-toastify"; import Visibility from '@mui/icons-material/Visibility'; import VisibilityOff from '@mui/icons-material/VisibilityOff'; +import { DarkModeContext } from '../ToggleMode/DarkModeContext'; +import {FaSun,FaMoon} from 'react-icons/fa6' export default function Main() { const navigation=useNavigation(); const submit=useSubmit(); + const {isDarkMode,toggleMode}=useContext(DarkModeContext) const [SignUpData,setSignUpData]=useState({name:'',email:'',password:''}); const[submiting,setSubmiting]=useState(false); const [showPassword, setShowPassword] = useState(false); @@ -143,18 +146,21 @@ export default function Main() { const handleClickShowPassword = () => setShowPassword(!showPassword); // Toggle password visibility const handleMouseDownPassword = (event) => event.preventDefault(); // Prevent default action on mouse down return ( -
+ <> +
+ +
- -
+ +
Sign Up


- +
}
- Already have and account ? LogIn + Already have and account ? LogIn
{/* SIGN UP FOR SPOTIFY */} @@ -190,6 +196,6 @@ export default function Main() {
-
+
) } diff --git a/frontend/src/components/ToggleMode/DarkModeContext.js b/frontend/src/components/ToggleMode/DarkModeContext.js new file mode 100644 index 0000000..7ff181d --- /dev/null +++ b/frontend/src/components/ToggleMode/DarkModeContext.js @@ -0,0 +1,30 @@ +import React, { createContext, useEffect, useState } from 'react'; +import {FaSun,FaMoon} from 'react-icons/fa6' +export const DarkModeContext = createContext(); + +export function DarkModeProvider({ children }) { + const [isDarkMode, setIsDarkMode] = useState(false); + + useEffect(() => { + const savedMode = localStorage.getItem('mode') || 'light'; + setIsDarkMode(savedMode === 'dark'); + }, []); + + const toggleMode = () => { + if (isDarkMode) { + setIsDarkMode(false); + localStorage.setItem('mode', 'light'); + } else { + setIsDarkMode(true); + localStorage.setItem('mode', 'dark'); + } + }; + + return ( + +
+ {children} + +
+ ); +} diff --git a/frontend/src/pages/Home.js b/frontend/src/pages/Home.js index 2b7a65a..2c535b3 100644 --- a/frontend/src/pages/Home.js +++ b/frontend/src/pages/Home.js @@ -1,4 +1,4 @@ -import React, { useEffect } from 'react' +import React, { useContext, useEffect } from 'react' import NavBar from '../components/HomeComponents/NavBar' import Description from '../components/HomeComponents/Description' import Service from './Service' @@ -6,8 +6,9 @@ import { useState } from 'react' import LoadingPage from './LoadingPage' import { useNavigate } from 'react-router-dom' +import { DarkModeContext } from '../components/ToggleMode/DarkModeContext' export default function Home() { - + const {isDarkMode}=useContext(DarkModeContext) const navigate=useNavigate(); const [isLoading,setIsLoading]=useState(true); @@ -15,7 +16,7 @@ export default function Home() { const checkIfLoggedIn=async()=>{ const cookie=localStorage.getItem('jwt'); - const response=await fetch(`${process.env.REACT_APP_API_URL}/api/v1/users/protect`,{ + const response=await fetch(`${process.env.REACT_APP_API_URL}/api/v1/users/protect`,{ method:'post', headers:{ 'Content-type':'application/json', @@ -39,7 +40,7 @@ export default function Home() { return (
{isLoading&&} - {!isLoading&&(<>
+ {!isLoading&&(<>
diff --git a/frontend/src/pages/Service.js b/frontend/src/pages/Service.js index 22f5b92..db065d7 100644 --- a/frontend/src/pages/Service.js +++ b/frontend/src/pages/Service.js @@ -1,12 +1,14 @@ -import React from 'react' +import React, { useContext } from 'react' import Card from '../components/HomeComponents/Card' +import { DarkModeContext } from '../components/ToggleMode/DarkModeContext' export default function Service() { + const {isDarkMode}=useContext(DarkModeContext) return ( -
+
-
Get the best of all the features
+
Get the best of all the features