Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used Recoil for toggletheme #378

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"react-router-dom": "^6.27.0",
"react-spinners": "^0.14.1",
"react-top-loading-bar": "^2.3.1",
"react-typed": "^2.0.12"
"react-typed": "^2.0.12",
"recoil": "^0.7.7"
},
"devDependencies": {
"@types/react": "^18.2.43",
Expand Down
12 changes: 11 additions & 1 deletion src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,14 @@ import "./App.css";
import AnimatedCursor from "react-animated-cursor";

import { FaAngleDoubleUp } from "react-icons/fa";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { themeState } from "./atoms/ThemeAtom";
import { toggleThemeSelector } from "./atoms/ThemeAtom";

function App() {
const { progress, isDarkMode } = useContext(Context); // Assuming isDarkMode is provided in your context
const { progress} = useContext(Context);
const isDarkMode = useRecoilValue(themeState); //to use the current theme
const toggleTheme = useSetRecoilState(toggleThemeSelector); //to update the state

const [showScroll, setShowScroll] = useState(false);

Expand Down Expand Up @@ -66,6 +71,11 @@ function App() {
document.body.classList.toggle("dark", isDarkMode);
}, [isDarkMode]);

// useEffect(() => {
// // Add or remove the "dark" class on the body based on isDarkMode
// document.body.classList.toggle("dark", isDarkMode);
// }, [isDarkMode]);

return (
<div className={`d-flex flex-column ${isDarkMode ? "dark" : ""}`}>
<AnimatedCursor
Expand Down
28 changes: 28 additions & 0 deletions src/atoms/ThemeAtom
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { atom, selector } from 'recoil';

const getInitialDarkMode = () => {
const savedMode = localStorage.getItem('darkMode');
return savedMode === 'true'; // Convert saved string back to boolean
};

export const themeState = atom({
key: 'themeState',
default: getInitialDarkMode(), // Use the initial value from localStorage
effects_UNSTABLE: [
({ onSet }) => {
onSet((newMode) => {
localStorage.setItem('darkMode', newMode.toString()); // Store as string
});
},
],
});

// Selector to toggle theme
export const toggleThemeSelector = selector({
key: 'toggleThemeSelector',
get: ({ get }) => get(themeState),
set: ({ get, set }) => {
const currentMode = get(themeState);
set(themeState, !currentMode); // Toggle the theme
},
});
4 changes: 3 additions & 1 deletion src/components/shared/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import Tilt from "react-parallax-tilt";
import "../../css/Footer.css";
import { TbHelpSquareFilled } from "react-icons/tb";
import GoogleTranslate from "../../pages/contributor/GoogleTranslate";
import { themeState } from "../../atoms/ThemeAtom";
import { useRecoilValue } from "recoil";

const Footer = () => {
const { isDarkMode } = useContext(Context);
const isDarkMode = useRecoilValue(themeState); //to use the current theme

return (
<footer
Expand Down
6 changes: 5 additions & 1 deletion src/components/shared/Navbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import { Link } from "react-router-dom";
import { MdOutlineDarkMode, MdOutlineLightMode } from "react-icons/md";
import { Context } from "../../context/Context";
import '../shared/Navbar.css';
import { useRecoilValue, useSetRecoilState } from "recoil";
import { themeState } from "../../atoms/ThemeAtom";
import { toggleThemeSelector } from "../../atoms/ThemeAtom";

const Navbar = () => {
const { isDarkMode, toggleTheme } = useContext(Context);
const isDarkMode = useRecoilValue(themeState); //to use the current theme
const toggleTheme = useSetRecoilState(toggleThemeSelector); //to update the state
const [isSticky, setIsSticky] = useState(false);
const [isCollapsed, setIsCollapsed] = useState(true);

Expand Down
16 changes: 0 additions & 16 deletions src/context/Context.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,11 @@ import React, { createContext, useState, useEffect } from 'react';
export const Context = createContext();

const ContextProvider = ({ children }) => {
const getInitialDarkMode = () => localStorage.getItem('darkMode') === 'true';

const [progress, setProgress] = useState(0);
const [isDarkMode, setDarkMode] = useState(getInitialDarkMode);

useEffect(() => {
localStorage.setItem('darkMode', isDarkMode);
}, [isDarkMode]);

const toggleTheme = () => {
setDarkMode((prevMode) => {
const newMode = !prevMode;
localStorage.setItem('darkMode', newMode ? 'dark' : 'light');
return newMode;
});
};

return (
<Context.Provider value={{
progress, setProgress,
isDarkMode, toggleTheme,
}}>
{children}
</Context.Provider>
Expand Down
5 changes: 4 additions & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import "bootstrap/dist/css/bootstrap.min.css";
import 'bootstrap/dist/js/bootstrap.bundle.min';
import "./index.css"
import ContextProvider from "./context/Context";
import { RecoilRoot } from "recoil";

ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<ContextProvider>
<App />
<RecoilRoot>
<App />
</RecoilRoot>
</ContextProvider>
</React.StrictMode>,
);
4 changes: 3 additions & 1 deletion src/pages/About.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useContext } from 'react';
import { BiCalendar, BiMap, BiUser, BiBook, BiBriefcase, BiWorld, BiCalendarX } from 'react-icons/bi';
import { Context } from '../context/Context'; // Assuming your context file is located here
import { themeState } from '../atoms/ThemeAtom';
import { useRecoilValue } from 'recoil';

const About = () => {
const { isDarkMode } = useContext(Context);
const isDarkMode = useRecoilValue(themeState); //to use the current theme

return (
<div className={`container p-4 ${isDarkMode ? 'text-white' : 'text-dark'}`} style={{ backgroundColor: isDarkMode ? '#333' : 'rgba(228, 193, 129, 0.272);' }}>
Expand Down
4 changes: 3 additions & 1 deletion src/pages/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { ReactTyped } from "react-typed";
import VideoButton from "../components/Home/VideoButton";
import Tilt from 'react-parallax-tilt';
import { Context } from '../context/Context';
import { themeState } from "../atoms/ThemeAtom";
import { useRecoilValue } from "recoil";

export default function Home() {
const { isDarkMode } = useContext(Context);
const isDarkMode = useRecoilValue(themeState); //to use the current theme

return (
<div className={`d-flex flex-column align-items-center ${isDarkMode ? 'dark-mode' : 'light-mode'}`} style={{ color: isDarkMode ? 'white' : 'black' }}>
Expand Down