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: add light/dark toggle & persist prefs #33

Open
wants to merge 4 commits into
base: master
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
10 changes: 10 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"react": "^18.1.0",
"react-avatar": "^4.1.0",
"react-confirm-alert": "^2.8.0",
"react-dark-mode-toggle": "^0.2.0",
"react-dom": "^18.1.0",
"react-fast-marquee": "^1.3.2",
"react-files": "^3.0.0-alpha.3",
Expand Down
12 changes: 11 additions & 1 deletion src/components/Layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ import TaskCreateRuns from './TaskCreate';

const Layout = () => {
const [scroll, setScroll] = useState(0);
const [darkMode, setDarkMode] = useState(false);
const [isLoggedIn, setIsLoggedIn] = useState('loading');
const [userData, setUserData] = useState({});
const location = useLocation();
const params = new URLSearchParams(location.hash);
const navigate = useNavigate();

const storedDarkMode = JSON.parse(localStorage.getItem('darkMode'));
const [darkMode, setDarkMode] = useState(storedDarkMode || false);

function arr2obj(arr) {
const obj = {};
arr.forEach((v) => {
Expand Down Expand Up @@ -80,10 +82,18 @@ const Layout = () => {
};
});

useEffect(() => {
if (darkMode) {
document.body.classList.add('dark');
document.body.classList.add('bg-gray-800');
}
}, [darkMode]);

const toggleDarkMode = () => {
document.body.classList.toggle('dark');
document.body.classList.toggle('bg-gray-800');
setDarkMode(!darkMode);
localStorage.setItem('darkMode', JSON.stringify(!darkMode));
};

const showToast = (type, msg) => {
Expand Down
12 changes: 9 additions & 3 deletions src/components/Navbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Avatar from 'react-avatar';
import { Menu, MenuItem, MenuButton } from '@szhsin/react-menu';
import { host_uri } from '../config';
import { confirmAlert } from 'react-confirm-alert';
import DarkModeToggle from 'react-dark-mode-toggle';

const Navbar = ({
scroll,
Expand Down Expand Up @@ -50,7 +51,7 @@ const Navbar = ({
confirmAlert({
customUI: ({ onClose }) => {
return (
<div className="bg-white rounded-lg p-10 shadow-xl font-open ">
<div className="bg-white rounded-lg p-10 shadow-xl font-open">
<h1 className="mb-8">
Are you sure to logout from{' '}
<span className="font-mons font-semibold">KRINI</span>?
Expand Down Expand Up @@ -100,7 +101,7 @@ const Navbar = ({
: 'bg-white z-10 fixed w-full shadow-lg py-5 pt-10 md:px-32 px-10 flex justify-between items-center'
}
>
{/* <div className=""> */}
{/* <div className=""> */}
<Link to="/">
<div className="flex items-center cursor-pointer">
<div className="h-10 mr-4 mb-1">
Expand Down Expand Up @@ -135,7 +136,12 @@ const Navbar = ({
</Link>
</div>
) : (
<div>
<div className="flex items-center">
<DarkModeToggle
onChange={toggleDarkMode}
checked={darkMode}
size={40}
/>
<Menu
menuButton={
<MenuButton>
Expand Down
Loading