Skip to content
This repository has been archived by the owner on Nov 2, 2021. It is now read-only.

Feat: Add theme chooser #2180

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f2aa7d4
Feat: overhaul dark theme
balrajsingh9 Jun 21, 2020
5f27ba8
Feat: add primary color in light theme
balrajsingh9 Jun 21, 2020
c0d1ac3
Chore: Add dark background and primary colors
balrajsingh9 Jun 21, 2020
5fa52a0
Fix: color of state borders on dark theme
balrajsingh9 Jun 21, 2020
42f1750
Feat: add react-modal for theme-chooser
balrajsingh9 Jun 21, 2020
91595f3
Refactor: update background color properties
balrajsingh9 Jun 21, 2020
5340767
Feat: Add theme chooser modal
balrajsingh9 Jun 21, 2020
6e8798c
Improv: Add line beneath the moto text
balrajsingh9 Jun 21, 2020
f45f3cd
Feat: update theme selector and remember chosen styles
balrajsingh9 Jun 21, 2020
2deb76f
Fix: dark on dark color issue
balrajsingh9 Jun 21, 2020
d320573
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jun 21, 2020
8ec7f65
Fix: table cell styles
balrajsingh9 Jun 21, 2020
0d9ef8f
Chore: Remove log
balrajsingh9 Jun 22, 2020
6bb356d
fix: Fix a typo
balrajsingh9 Jun 30, 2020
212dd73
chore: Cleanup not required libs
balrajsingh9 Jun 30, 2020
6880c28
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jul 3, 2020
9d76a72
style: Update classes after last merge into master
balrajsingh9 Jul 3, 2020
d5bfc6d
chore: Remove commented out styles
balrajsingh9 Jul 3, 2020
09b75f0
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jul 4, 2020
226ae45
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jul 6, 2020
2a42f3b
Fix: colors in table on:hover
balrajsingh9 Jul 6, 2020
054851b
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jul 15, 2020
98351c2
Fix: Github icon hover color
balrajsingh9 Jul 15, 2020
62526c3
fix: Fix accessibility issue and remove debug statement
balrajsingh9 Jul 15, 2020
9939368
Merge branch 'master' of https://github.com/covid19india/covid19india…
balrajsingh9 Jul 18, 2020
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
npm i && npm start
```


## Contribution

If you're new to contributing to Open Source on Github, [this guide](https://guides.github.com/activities/contributing-to-open-source/) can help you get started. Please check out the [contribution guide](CONTRIBUTING.md) for more details on how issues and pull requests work.
Expand Down
70 changes: 48 additions & 22 deletions 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 @@ -34,6 +34,8 @@
"react-content-loader": "^5.1.0",
"react-dom": "^16.13.1",
"react-feather": "^2.0.8",
"react-is-visible": "^1.1.0",
"react-modal": "^3.11.2",
"react-helmet": "^6.1.0",
"react-i18next": "^11.7.0",
"react-router-dom": "^5.2.0",
Expand All @@ -46,7 +48,6 @@
"source-map-explorer": "^2.4.2",
"swr": "^0.2.3",
"topojson": "^3.0.2",
"use-dark-mode": "^2.3.1",
"workerize-loader": "^1.3.0"
},
"scripts": {
Expand Down
134 changes: 104 additions & 30 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,84 @@
import './App.scss';
import './dark-theme.scss';

import Blog from './components/Blog';
import Navbar from './components/Navbar';
import ThemeChooser from './components/themechooser';
import {PRIMARY_COLORS, BACKGROUND_COLORS} from './constants';

import React, {lazy, useState, Suspense} from 'react';
import React, {lazy, useState, Suspense, useEffect, useCallback} from 'react';
import {Route, Redirect, Switch, useLocation} from 'react-router-dom';
import useDarkMode from 'use-dark-mode';

const Home = lazy(() => import('./components/Home'));
const About = lazy(() => import('./components/About'));
const State = lazy(() => import('./components/State'));
const LanguageSwitcher = lazy(() => import('./components/LanguageSwitcher'));

const colorKeys = Object.freeze({
P_COLOR: 'p-color',
BG_COLOR: 'bg-color',
});

const App = () => {
const darkMode = useDarkMode(false);
const [showLanguageSwitcher, setShowLanguageSwitcher] = useState(false);
const location = useLocation();

const [showThemeChooser, setShowThemeChooser] = useState(false);
const [pColor, setPColor] = useState('');
const [bgColor, setBgColor] = useState('');

useEffect(() => {
const cachedPColor =
localStorage?.getItem(colorKeys.P_COLOR) ?? PRIMARY_COLORS[0];
const cachedBgColor =
localStorage?.getItem(colorKeys.BG_COLOR) ??
BACKGROUND_COLORS.DEFAULT.color;

setPColor(cachedPColor);
setBgColor(cachedBgColor);
}, []);

useEffect(() => {
const root = document.documentElement;
root.style.setProperty('--primary-color', pColor);
}, [pColor]);

useEffect(() => {
const root = document.documentElement;
root.style.setProperty('--background-color', bgColor);
let className;

if (bgColor === BACKGROUND_COLORS.LIGHTS_OUT.color) {
className = 'dark-mode dim';
} else if (bgColor === BACKGROUND_COLORS.DIM.color) {
className = 'dark-mode lights-out';
} else {
className = 'light-theme';
}

document.getElementsByTagName('body')[0].className = className;
}, [bgColor]);

const cacheColor = (key, color) => {
localStorage.setItem(key, color);
};

const handleOnPColorChange = useCallback(
(newColor) => {
setPColor(newColor);
cacheColor(colorKeys.P_COLOR, newColor);
},
[setPColor]
);

const handleOnBgColorChange = useCallback(
(newColor) => {
setBgColor(newColor);
cacheColor(colorKeys.BG_COLOR, newColor);
},
[setBgColor]
);

const pages = [
{
pageLink: '/',
Expand Down Expand Up @@ -44,35 +107,46 @@ const App = () => {
];

return (
<div className="App">
<Suspense fallback={<div />}>
<LanguageSwitcher
<>
{showThemeChooser && (
<ThemeChooser
isOpen={showThemeChooser}
onClose={() => setShowThemeChooser(false)}
onPColorChange={handleOnPColorChange}
onBgColorChange={handleOnBgColorChange}
{...{pColor, bgColor}}
/>
)}
<div className="App" id="root-app">
<Suspense fallback={<div />}>
<LanguageSwitcher
{...{showLanguageSwitcher, setShowLanguageSwitcher}}
/>
</Suspense>

<Navbar
pages={pages}
setShowThemeChooser={setShowThemeChooser}
{...{showLanguageSwitcher, setShowLanguageSwitcher}}
/>
</Suspense>

<Navbar
pages={pages}
{...{darkMode}}
{...{showLanguageSwitcher, setShowLanguageSwitcher}}
/>

<Suspense fallback={<div />}>
<Switch location={location}>
{pages.map((page, index) => {
return (
<Route
exact
path={page.pageLink}
render={({match}) => <page.view />}
key={index}
/>
);
})}
<Redirect to="/" />
</Switch>
</Suspense>
</div>

<Suspense fallback={<div />}>
<Switch location={location}>
{pages.map((page, index) => {
return (
<Route
exact
path={page.pageLink}
render={({match}) => <page.view />}
key={index}
/>
);
})}
<Redirect to="/" />
</Switch>
</Suspense>
</div>
</>
);
};

Expand Down
Loading