Skip to content

feat: theme switch #50

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

Closed
wants to merge 1 commit into from
Closed
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
15,776 changes: 15,776 additions & 0 deletions dark-mode/package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dark-mode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"private": true,
"engines": {
"node" : ">= 15.0.0"
"node": ">= 15.0.0"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^1.2.35",
Expand Down
28 changes: 26 additions & 2 deletions dark-mode/src/common/containers/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
import React from 'react';
import React, { useReducer }from 'react';
import AppContext from '../../context/AppContext';
import APPTHEME from '../../constant/appTheme';

const initState = {
theme: APPTHEME.LIGHT
};

const htmlRoot = document.getElementsByTagName('html')[0];

const reducer = (state, action) => {
switch (action.type) {
case 'setLightTheme':
htmlRoot.classList.remove('dark-mode');
return Object.assign({}, state, { theme: APPTHEME.LIGHT });
case 'setDarkTheme':
htmlRoot.classList.add('dark-mode');
return Object.assign({}, state, { theme: APPTHEME.DARK });
default: return state;
}
}

export default function App({ children }) {
return children;
const [state, dispatch] = useReducer(reducer, initState);

return <AppContext.Provider value={{ state, dispatch }}>
{children}
</AppContext.Provider>;
}
4 changes: 4 additions & 0 deletions dark-mode/src/constant/appTheme.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
DARK: 'dark',
LIGHT: 'light'
}
3 changes: 3 additions & 0 deletions dark-mode/src/context/AppContext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import React from 'react';

export default React.createContext(null);
1 change: 1 addition & 0 deletions dark-mode/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from 'react';
import ReactDOM from 'react-dom';
import AppContainer from './common/containers/App';
import './styles/_main.scss';
import './styles/_dark-mode.scss';
import Routes from './routes';

ReactDOM.render(
Expand Down
22 changes: 18 additions & 4 deletions dark-mode/src/routes/App/components/App.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import React from 'react';
import React, { useContext, useMemo } from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faMoon } from '@fortawesome/free-solid-svg-icons';
import { faMoon, faSun } from '@fortawesome/free-solid-svg-icons';
import AppContext from '../../../context/AppContext';
import APPTHEME from '../../../constant/appTheme';
import '../styles/_app.scss';

function App() {
const { state, dispatch } = useContext(AppContext);

const themeIcon = useMemo(() => state.theme === APPTHEME.LIGHT ? faMoon : faSun, [state.theme])

const changeTheme = () => {
dispatch({
type: state.theme === APPTHEME.LIGHT ? 'setDarkTheme' : 'setLightTheme'
})
}

return (
<div className="app">
<div className="level">
Expand All @@ -12,8 +24,10 @@ function App() {
</div>

{/* --The button that should toggle dark mode-- */}
<button className="app__dark-mode-btn icon level-right">
<FontAwesomeIcon icon={faMoon} />
<button
className={['app__theme-btn icon level-right ', state.theme === APPTHEME.DARK ? 'app__theme-btn__dark-mode' : ''].join('')}
onClick={changeTheme}>
<FontAwesomeIcon icon={themeIcon} />
</button>

</div>
Expand Down
6 changes: 5 additions & 1 deletion dark-mode/src/routes/App/styles/_app.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
margin: auto;
box-sizing: content-box;

&__dark-mode-btn {
&__theme-btn {
border: 0;
background: transparent;
font-size: 30px;
Expand All @@ -21,6 +21,10 @@
&:focus {
outline: none;
}

&__dark-mode {
color: #FFA500;
}
}

.buttons > .button {
Expand Down