BeforeIDieCode / BeforeIDieAchievements

๐ŸŒŸAn open source project that helps developers make their first pull request and contribute to open source projects. ๐Ÿš€๐ŸŽฏ Developers can contribute by sharing what they want to do before they die. ๐Ÿ’ก๐Ÿ”—
https://before-i-die-achievements.vercel.app
MIT License
120 stars 187 forks source link

[Feature Request] Light and dark theme #203

Open Angeltheesoto opened 1 year ago

Angeltheesoto commented 1 year ago

Description

Light and dark theme

Feature:

We could create a simple light and dark mode for the user to toggle between.

Possible Solution:

import React, { useContext, useState } from "react";

const ThemeContext = React.createContext();

export const ThemeProvider = ({ children }) => {
  const [theme, setTheme] = useState(true);

  const toggleTheme = (isLight) => {
    setTheme(isLight);
    setLocalTheme(isLight)
  };

const setLocalTheme = async (value) => {
      try {
        await localStorage.setItem("theme", value.toString());
      } catch (err) {
        console.log("Error setting local theme: ", err);
      }
    };

  return (
    <ThemeContext.Provider value={{ theme, setTheme, toggleTheme }}>
      {children}
    </ThemeContext.Provider>
  );
};

export const useTheme = () => {
  return useContext(ThemeContext);
};
import { ThemeProvider } from "./context/ThemeContext"; // new line

... morecode

<ThemeProvider> // new line
  <React.StrictMode>
     <RouterProvider router={router} />
  </React.StrictMode>
</ThemeProvider> // new line

... more code
import { useTheme } from "./context/ThemeContext";

function ExampleComponent() {
     const { theme, setTheme, toggleTheme } = useTheme();

   ... more code
}
  const initializeLocalStorage = async () => {
    try {
      const localTheme = await localStorage.getItem("theme");
      if (!localTheme) {
        localStorage.setItem("theme", JSON.stringify(theme));
      } else {
        const parsedTheme = JSON.parse(localTheme);
        setTheme(parsedTheme);
      }
    } catch (error) {
      console.log("Error initializing LocalStorage:", error);
    }
  };

  useEffect(() => {
    initializeLocalStorage();
  }, []);
div[data-theme="light"] {
   color: "black",
   background-color: "white"
}

div[data-theme="dark"] {
   color: "white",
   background-color: "black"
}
import { useTheme } from "../../context/ThemeContext";

function ExampleComponent() {
const { theme, setTheme, toggleTheme } = useTheme();

... more code

<div data-theme={theme ? "light" : "dark"}> 
... more code
</div>
}

... more code

Screenshots

No response

Checklist

joyboy-reincarnated commented 1 year ago

Hi I can try to do this

XanderRubio commented 1 year ago

Hi @Angeltheesoto !

Excellent angel! I'm currently traveling and haven't looked to closely at the code you provided. I love the idea of a light and dark mode. Go for it and I will go ahead and assign. Have a nice day and happy developing!

XanderRubio commented 1 year ago

Nice and thank you for your thourugh explaination in your open issue. I appreciate your documentation and the issue is assigned to you @Angeltheesoto. Have a great Sunday and thank you for your initiative๐Ÿ‘๐Ÿป

XanderRubio commented 1 year ago

Hi I can try to do this

Thanks @joyboy-reincarnated for your enthusiasm! The issue has already been assigned. Take a look at the ReadMe of the repo and you can contribute right away to the project with sharing what you want to do before you die. Have a nice day!

Angeltheesoto commented 1 year ago

@XanderRubio, Thank you I will start working on it! And hope you have a great weekend as well ๐Ÿ˜Š

XanderRubio commented 1 year ago

@XanderRubio, Thank you I will start working on it! And hope you have a great weekend as well ๐Ÿ˜Š

Great and thank you @Angeltheesoto!

benjamindotdev commented 8 months ago

Hey everyone. If this hasn't been completed, I can jump on it