Skip to content

Commit 0cb16cf

Browse files
react-9
1 parent 0eba8b0 commit 0cb16cf

File tree

8 files changed

+59
-18
lines changed

8 files changed

+59
-18
lines changed

src/App.css

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
.App{
3-
background-color: #333;
3+
44
width:100%;
55
height:100vh;
6-
}
6+
}
7+
.darkMode{
8+
background-color: #333;
9+
}
10+
11+
.lightMode{
12+
background-color: #ccc;
13+
}

src/App.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { useReducer, useState } from 'react';
1+
import { useContext, useReducer, useState } from 'react';
22
import './App.css';
33
import AddVideo from './components/AddVideo';
44
import videoDB from './data/data';
55
import VideoList from './components/VideoList';
6+
import ThemeContext from './context/ThemeContext';
7+
8+
69
function App() {
710
console.log('render App')
811
const [editableVideo,setEditableVideo] = useState(null);
9-
12+
const [mode,setMode] = useState('darkMode')
1013
function videoReducer(videos,action){
1114
switch(action.type){
1215
case 'ADD':
@@ -27,21 +30,25 @@ function App() {
2730
}
2831

2932
}
30-
3133
const [videos,dispatch] = useReducer(videoReducer,videoDB)
3234

3335

36+
37+
3438
function editVideo(id){
3539
setEditableVideo(videos.find(video=>video.id===id))
3640
}
3741

3842
return (
39-
<div className="App" onClick={()=>console.log('App')}>
43+
<ThemeContext.Provider value={mode}>
44+
<div className={`App ${mode}`} onClick={()=>console.log('App')}>
45+
<button onClick={()=>setMode(mode==='darkMode'? 'lightMode':'darkMode')}>Mode</button>
4046
<AddVideo dispatch={dispatch} editableVideo={editableVideo}></AddVideo>
4147
<VideoList dispatch={dispatch} editVideo={editVideo} videos={videos}></VideoList>
4248

43-
4449
</div>
50+
</ThemeContext.Provider>
51+
4552
);
4653
}
4754

src/components/PlayButton.css

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
button{
2-
background-color: black;
3-
border: 1px solid yellow;
42
padding: 1em;
53
border-radius: 1em;
6-
color: white;
74
cursor: pointer;
85
}
96
button:hover{
107
background-color: grey;
118
color: white;
129
}
10+
button.darkMode, .darkMode button{
11+
background-color: black;
12+
border: 1px solid yellow;
13+
color:white
14+
}
15+
button.lightMode, .lightMode button{
16+
background-color: #ccc;
17+
border: 1px solid black;
18+
color:black
19+
}
1320

src/components/PlayButton.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import './PlayButton.css';
2-
import { useState } from 'react';
2+
import { useContext, useState } from 'react';
3+
import ThemeContext from '../context/ThemeContext';
34

45
function PlayButton({message,children,onPlay,onPause}){
56
console.log('render PlayButton')
7+
const theme = useContext(ThemeContext)
68

79
const [playing, setPlaying] = useState(false);
810
function handleClick(e){
@@ -16,7 +18,7 @@ function PlayButton({message,children,onPlay,onPause}){
1618
}
1719

1820
return (
19-
<button onClick={handleClick}>{children} : {playing?'⏸️':'⏯️'}</button>
21+
<button className={theme} onClick={handleClick}>{children} : {playing?'⏸️':'⏯️'}</button>
2022
)
2123

2224
}

src/components/Video.css

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,31 @@
88
margin: 0 0.2em;
99
float: left;
1010
position: relative;
11+
}
12+
.container.darkMode{
13+
color: white
14+
}
15+
.container.lightMode{
16+
color: black
17+
1118
}
1219
.pic img{
1320
width: 100%;
1421
}
1522
.title{
1623
font-weight: bold;
1724
margin-bottom: 1em;
18-
color:white
1925
}
20-
.channel, .views{
26+
.darkMode .channel, .darkMode .views{
2127
font-size: small;
2228
color:#ccc;
2329
margin-bottom: 0.2em;
2430
}
31+
.lightMode .channel, .lightMode .views{
32+
font-size: small;
33+
color:black;
34+
margin-bottom: 0.2em;
35+
}
2536

2637
.views span{
2738
padding: 0 0.5em;

src/components/Video.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { useContext } from 'react';
2+
import ThemeContext from '../context/ThemeContext';
13
import './Video.css';
24

35
function Video({title,id,channel="Coder Dost",views,time,verified,children,dispatch,editVideo}) {
46
console.log('render Video')
5-
7+
const theme = useContext(ThemeContext)
68

79
return (
810
<>
9-
<div className='container'>
11+
<div className={`container ${theme}`}>
1012
<button className='close' onClick={()=>dispatch({type:'DELETE',payload:id})}>X</button>
1113
<button className='edit' onClick={()=>editVideo(id)}>Edit</button>
1214
<div className="pic">

src/context/ThemeContext.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createContext } from "react";
2+
3+
const ThemeContext = createContext('lightMode')
4+
5+
export default ThemeContext;

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import React from 'react';
22
import ReactDOM from 'react-dom/client';
33
import './index.css';
44
import App from './App';
5-
5+
import ThemeContext from './context/ThemeContext';
66
const root = ReactDOM.createRoot(document.getElementById('root'));
77
root.render(
88
<React.StrictMode>
9-
<App />
9+
<App />
1010
</React.StrictMode>
1111
);
1212

0 commit comments

Comments
 (0)