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

feat(dark-mode): preserved dark mode state using local storage #476

Open
wants to merge 3 commits into
base: feat/dark-mode
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
32 changes: 24 additions & 8 deletions client/src/context/ThemeContext.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { useContext, useEffect, useState } from 'react';

import { ThemeProvider } from '@mui/material/styles';

import darkTheme from '../config/themes/dark';
import lightTheme from '../config/themes/light';

import useLocalStorage from '../hooks/useLocalStorage';
//context for theming
export const ThemeContext = React.createContext({
isDarkTheme: undefined,
Expand All @@ -15,12 +13,30 @@ export const useTheme = () => useContext(ThemeContext);

//context provider
const ThemeContextProvider = ({ children }) => {
const [isDarkTheme, setDarkTheme] = useState(false);

const [defaultTheme, setDefaultTheme] = useLocalStorage('DefaultTheme');
const [isDarkTheme, setDarkTheme] = useLocalStorage('IsDarkTheme');
useEffect(() => {}, [isDarkTheme]);
SUNNYKUMAR1232 marked this conversation as resolved.
Show resolved Hide resolved
useEffect(() => {
const currentHour = new Date().getHours();
setDarkTheme(currentHour >= 20 || currentHour <= 8 ? true : false);
}, [setDarkTheme]);
const darkModeMediaQuery = window.matchMedia(
'(prefers-color-scheme: dark)',
);
const setTheme = (event) => {
if (event.matches) {
setDefaultTheme(defaultTheme === true ? defaultTheme : null);
setDarkTheme(true);
} else {
setDefaultTheme(false);
setDarkTheme(defaultTheme === false ? defaultTheme : null);
}
};
setTheme(darkModeMediaQuery);
darkModeMediaQuery.addEventListener('change', setTheme);
setDarkTheme(isDarkTheme);
setDefaultTheme(isDarkTheme ? isDarkTheme : isDarkTheme);
return () => {
darkModeMediaQuery.removeEventListener('change', setTheme);
};
}, [isDarkTheme]);

return (
<ThemeContext.Provider value={{ isDarkTheme, setDarkTheme }}>
Expand Down
26 changes: 26 additions & 0 deletions client/src/hooks/useLocalStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState } from 'react';

const useLocalStorage = (key, initialValue) => {
const [state, setState] = useState(() => {
try {
const value = window.localStorage.getItem(key);
return value != 'null' ? JSON.parse(value) : initialValue;
} catch (error) {
console.log(error);
}
});

const setValue = (value) => {
try {
const valueToStore = value instanceof Function ? value(state) : value;
window.localStorage.setItem(key, JSON.stringify(valueToStore));
setState(value);
} catch (error) {
console.log(error);
}
};

return [state, setValue];
};

export default useLocalStorage;
SUNNYKUMAR1232 marked this conversation as resolved.
Show resolved Hide resolved
Loading