├── src
├── ss.png
├── index.js
├── components
│ ├── Content.js
│ └── Toggle.js
├── styles
│ ├── useDarkMode.js
│ └── globalStyles.js
└── App.js
├── public
├── robots.txt
├── favicon.ico
├── logo192.png
├── logo512.png
├── manifest.json
└── index.html
├── README.md
├── .gitignore
└── package.json
/src/ss.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/candraKriswinarto/react-dark-mode/HEAD/src/ss.png
--------------------------------------------------------------------------------
/public/robots.txt:
--------------------------------------------------------------------------------
1 | # https://www.robotstxt.org/robotstxt.html
2 | User-agent: *
3 | Disallow:
4 |
--------------------------------------------------------------------------------
/public/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/candraKriswinarto/react-dark-mode/HEAD/public/favicon.ico
--------------------------------------------------------------------------------
/public/logo192.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/candraKriswinarto/react-dark-mode/HEAD/public/logo192.png
--------------------------------------------------------------------------------
/public/logo512.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/candraKriswinarto/react-dark-mode/HEAD/public/logo512.png
--------------------------------------------------------------------------------
/src/index.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import ReactDOM from 'react-dom';
3 | import App from './App';
4 |
5 | ReactDOM.render(
6 |
7 |
8 | ,
9 | document.getElementById('root')
10 | );
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # How to Add Dark Mode to Your React App | Styled Components
2 | Implement dark mode in react app with styled components
3 |
4 | ## screenshot
5 | 
6 |
7 | ### Video Tutorial
8 | [youtube](https://youtu.be/Zgvm-mP9_3A)
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2 |
3 | # dependencies
4 | /node_modules
5 | /.pnp
6 | .pnp.js
7 |
8 | # testing
9 | /coverage
10 |
11 | # production
12 | /build
13 |
14 | # misc
15 | .DS_Store
16 | .env.local
17 | .env.development.local
18 | .env.test.local
19 | .env.production.local
20 |
21 | npm-debug.log*
22 | yarn-debug.log*
23 | yarn-error.log*
24 |
--------------------------------------------------------------------------------
/src/components/Content.js:
--------------------------------------------------------------------------------
1 | import React from 'react'
2 |
3 | export const Content = () => {
4 | return (
5 |
6 |
Lorem ipsum dolor sit amet consectetur.
7 |
Lorem ipsum dolor sit, amet consectetur adipisicing elit. Neque id dolore aspernatur consequuntur dignissimos adipisci aut maxime, commodi magni veritatis reiciendis et nulla deleniti placeat voluptatibus iste repellendus voluptate. Voluptatibus?
8 |
Read More
9 |
10 | )
11 | }
12 |
--------------------------------------------------------------------------------
/public/manifest.json:
--------------------------------------------------------------------------------
1 | {
2 | "short_name": "React App",
3 | "name": "Create React App Sample",
4 | "icons": [
5 | {
6 | "src": "favicon.ico",
7 | "sizes": "64x64 32x32 24x24 16x16",
8 | "type": "image/x-icon"
9 | },
10 | {
11 | "src": "logo192.png",
12 | "type": "image/png",
13 | "sizes": "192x192"
14 | },
15 | {
16 | "src": "logo512.png",
17 | "type": "image/png",
18 | "sizes": "512x512"
19 | }
20 | ],
21 | "start_url": ".",
22 | "display": "standalone",
23 | "theme_color": "#000000",
24 | "background_color": "#ffffff"
25 | }
26 |
--------------------------------------------------------------------------------
/src/styles/useDarkMode.js:
--------------------------------------------------------------------------------
1 | import { useState, useEffect } from 'react'
2 |
3 | export const useDarkMode = () => {
4 | const [ theme, setTheme ] = useState('dark');
5 |
6 | const setMode = mode => {
7 | window.localStorage.setItem('theme', mode);
8 | setTheme(mode);
9 | }
10 |
11 | const toggleTheme = () => {
12 | theme === 'dark' ? setMode('light') : setMode('dark');
13 | }
14 |
15 | useEffect(() => {
16 | const localTheme = window.localStorage.getItem('theme');
17 | localTheme ? setTheme(localTheme) : setMode('dark');
18 | }, []);
19 |
20 | return [ theme, toggleTheme ];
21 | }
22 |
--------------------------------------------------------------------------------
/src/App.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import { Content } from './components/Content';
3 | import { Toggle } from './components/Toggle';
4 | import { useDarkMode } from './styles/useDarkMode';
5 | import { GlobalStyles, lightTheme, darkTheme } from './styles/globalStyles';
6 | import styled, { ThemeProvider } from 'styled-components';
7 |
8 | const Container = styled.div`
9 | max-width: 50%;
10 | margin: 8rem auto 0;
11 | `;
12 |
13 | function App() {
14 | const [ theme, toggleTheme ] = useDarkMode();
15 | const themeMode = theme === 'light' ? lightTheme : darkTheme;
16 |
17 | return (
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 | );
26 | }
27 |
28 | export default App;
29 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "react-dark-light-mode",
3 | "version": "0.1.0",
4 | "private": true,
5 | "dependencies": {
6 | "@testing-library/jest-dom": "^4.2.4",
7 | "@testing-library/react": "^9.3.2",
8 | "@testing-library/user-event": "^7.1.2",
9 | "react": "^16.13.1",
10 | "react-dom": "^16.13.1",
11 | "react-scripts": "3.4.3",
12 | "styled-components": "^5.2.0"
13 | },
14 | "scripts": {
15 | "start": "react-scripts start",
16 | "build": "react-scripts build",
17 | "test": "react-scripts test",
18 | "eject": "react-scripts eject"
19 | },
20 | "eslintConfig": {
21 | "extends": "react-app"
22 | },
23 | "browserslist": {
24 | "production": [
25 | ">0.2%",
26 | "not dead",
27 | "not op_mini all"
28 | ],
29 | "development": [
30 | "last 1 chrome version",
31 | "last 1 firefox version",
32 | "last 1 safari version"
33 | ]
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/src/styles/globalStyles.js:
--------------------------------------------------------------------------------
1 | import { createGlobalStyle } from 'styled-components';
2 |
3 | export const GlobalStyles = createGlobalStyle`
4 | body {
5 | background: ${({ theme }) => theme.body};
6 | color: ${({ theme }) => theme.text};
7 | font-family: 'Roboto', sans-serif;
8 | transition: all .5s linear;
9 | }
10 |
11 | p {
12 | line-height: 1.4rem;
13 | }
14 |
15 | .btn-primary {
16 | background: ${({ theme }) => theme.primary};
17 | color: ${({ theme }) => theme.body};
18 | padding: 0.5rem 1.5rem;
19 | font-size: 1rem;
20 | border-radius: 1rem;
21 | cursor: pointer;
22 | outline: none;
23 | border: none;
24 | transition: all .5s linear;
25 |
26 | }
27 | `;
28 |
29 | export const lightTheme = {
30 | body: '#fff',
31 | text: '#121212',
32 | primary: '#6200ee',
33 | };
34 |
35 | export const darkTheme = {
36 | body: '#121212',
37 | text: '#fff',
38 | primary: '#bb86fc',
39 | };
--------------------------------------------------------------------------------
/src/components/Toggle.js:
--------------------------------------------------------------------------------
1 | import React from 'react';
2 | import styled from 'styled-components';
3 |
4 | let Sun, Moon;
5 |
6 | Sun = Moon = styled.svg`
7 | position: absolute;
8 | top: 2rem;
9 | right: 4rem;
10 | transition: all .5s linear;
11 | `;
12 |
13 | export const Toggle = ({ theme, toggleTheme }) => {
14 | console.log(theme);
15 | return (
16 |
17 | { theme === 'light' ?
18 | :
19 |
}
20 |
21 | )
22 | }
23 |
--------------------------------------------------------------------------------
/public/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
12 |
13 |
17 |
18 |
27 |
28 | React App
29 |
30 |
31 | You need to enable JavaScript to run this app.
32 |
33 |
43 |
44 |
45 |
--------------------------------------------------------------------------------