Skip to content

Commit 072d53f

Browse files
committed
* Added ability to switch themes
1 parent e109e55 commit 072d53f

File tree

7 files changed

+118
-6
lines changed

7 files changed

+118
-6
lines changed

src/components/Footer/index.jsx

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import BottomNavigation from '@material-ui/core/BottomNavigation';
33
import BottomNavigationAction from '@material-ui/core/BottomNavigationAction';
4+
import Brightness7Icon from '@material-ui/icons/Brightness7';
5+
import Brightness5Icon from '@material-ui/icons/Brightness5';
6+
import { MainContext } from '../../contexts/MainContextProvider';
7+
import { setThemeIndex } from '../../reducers/MainReducer/Actions';
48

59
const Footer = () => {
10+
const [state, dispatch] = useContext(MainContext);
611
const currentYear = new Date().getFullYear();
712

13+
const { themeIndex } = state;
14+
15+
/**
16+
* Change the theme
17+
*/
18+
const changeTheme = () => {
19+
dispatch(setThemeIndex(themeIndex === 1 ? 0 : 1));
20+
};
21+
822
return (
923
<BottomNavigation showLabels style={{ marginTop: 20 }}>
1024
<BottomNavigationAction label={`Copyright © ${currentYear} CodeDead`} />
25+
<BottomNavigationAction
26+
icon={themeIndex === 0 ? <Brightness5Icon /> : <Brightness7Icon />}
27+
onClick={changeTheme}
28+
/>
1129
</BottomNavigation>
1230
);
1331
};

src/components/Layout/index.jsx

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import React from 'react';
1+
import React, { useContext } from 'react';
22
import { graphql, useStaticQuery } from 'gatsby';
33
import { CssBaseline } from '@material-ui/core';
4+
import { createMuiTheme, ThemeProvider } from '@material-ui/core/styles';
45
import DefaultAppBar from '../DefaultAppBar';
56
import Footer from '../Footer';
7+
import { MainContext } from '../../contexts/MainContextProvider';
8+
import ThemeSelector from '../../utils/ThemeSelector';
69

710
const Layout = ({ children }) => {
11+
const [state] = useContext(MainContext);
12+
const { themeIndex, themeColorIndex } = state;
813
const data = useStaticQuery(graphql`query {
914
site {
1015
siteMetadata {
@@ -17,8 +22,22 @@ const Layout = ({ children }) => {
1722
}
1823
}`);
1924

25+
let themeType = 'light';
26+
if (themeIndex === 1) {
27+
themeType = 'dark';
28+
}
29+
30+
const color = ThemeSelector(themeColorIndex, state.themes.defaultColor);
31+
32+
const theme = createMuiTheme({
33+
palette: {
34+
primary: color,
35+
type: themeType,
36+
},
37+
});
38+
2039
return (
21-
<>
40+
<ThemeProvider theme={theme}>
2241
<CssBaseline />
2342
<DefaultAppBar
2443
title={data.site.siteMetadata.title}
@@ -28,7 +47,7 @@ const Layout = ({ children }) => {
2847
/>
2948
{children}
3049
<Footer />
31-
</>
50+
</ThemeProvider>
3251
);
3352
};
3453

src/contexts/MainContextProvider/index.jsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import React, { useReducer, createContext } from 'react';
22
import MainReducer from '../../reducers/MainReducer';
33

4+
const themeIndex = localStorage.themeIndex ? parseFloat(localStorage.themeIndex) : 0;
5+
const themeColorIndex = localStorage.themeColorIndex ? parseFloat(localStorage.themeColorIndex) : 0;
6+
47
const initState = {
58
pageIndex: 0,
9+
themeIndex,
10+
themeColorIndex,
11+
themes: {
12+
defaultColor: '#3f50b5',
13+
},
614
};
715

816
export const MainContext = createContext(initState);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const SET_PAGE_INDEX = 'SET_PAGE_INDEX';
2+
export const SET_THEME_INDEX = 'SET_THEME_INDEX';
3+
export const SET_THEME_COLOR_INDEX = 'SET_THEME_COLOR_INDEX';
Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
import { SET_PAGE_INDEX } from './actionTypes';
1+
import {
2+
SET_PAGE_INDEX,
3+
SET_THEME_COLOR_INDEX,
4+
SET_THEME_INDEX,
5+
} from './actionTypes';
26

37
export const setPageIndex = (index) => ({
48
type: SET_PAGE_INDEX,
59
payload: index,
610
});
11+
12+
export const setThemeIndex = (index) => ({
13+
type: SET_THEME_INDEX,
14+
payload: index,
15+
});
16+
17+
export const setThemeColorIndex = (index) => ({
18+
type: SET_THEME_COLOR_INDEX,
19+
payload: index,
20+
});

src/reducers/MainReducer/index.jsx

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { SET_PAGE_INDEX } from './Actions/actionTypes';
1+
import {
2+
SET_PAGE_INDEX,
3+
SET_THEME_COLOR_INDEX,
4+
SET_THEME_INDEX,
5+
} from './Actions/actionTypes';
26

37
const MainReducer = (state, action) => {
48
switch (action.type) {
@@ -7,6 +11,18 @@ const MainReducer = (state, action) => {
711
...state,
812
pageIndex: action.payload,
913
};
14+
case SET_THEME_INDEX:
15+
localStorage.themeIndex = action.payload;
16+
return {
17+
...state,
18+
themeIndex: action.payload,
19+
};
20+
case SET_THEME_COLOR_INDEX:
21+
localStorage.themeColorIndex = action.payload;
22+
return {
23+
...state,
24+
themeColorIndex: action.payload,
25+
};
1026
default:
1127
return state;
1228
}

src/utils/ThemeSelector/index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import deepPurple from '@material-ui/core/colors/deepPurple';
2+
import purple from '@material-ui/core/colors/purple';
3+
import green from '@material-ui/core/colors/green';
4+
import lightGreen from '@material-ui/core/colors/lightGreen';
5+
import blue from '@material-ui/core/colors/blue';
6+
import lightBlue from '@material-ui/core/colors/lightBlue';
7+
import red from '@material-ui/core/colors/red';
8+
import grey from '@material-ui/core/colors/grey';
9+
10+
const ThemeSelector = (index, defaultColor) => {
11+
switch (index) {
12+
default:
13+
return {
14+
main: defaultColor,
15+
};
16+
case 1:
17+
return purple;
18+
case 2:
19+
return deepPurple;
20+
case 3:
21+
return green;
22+
case 4:
23+
return lightGreen;
24+
case 5:
25+
return red;
26+
case 6:
27+
return blue;
28+
case 7:
29+
return lightBlue;
30+
case 8:
31+
return grey;
32+
}
33+
};
34+
35+
export default ThemeSelector;

0 commit comments

Comments
 (0)