forked from AbdulBasit313/React-Quiz-App-Template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
41 lines (35 loc) · 1.11 KB
/
App.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { useState } from 'react'
import { ThemeProvider } from 'styled-components'
import Main from './components/Main'
import ToggleTheme from './components/ui/ToggleTheme'
import QuizProvider from './context/QuizProvider'
import { GlobalStyles } from './styles/Global'
import { themes } from './styles/Theme'
function App() {
const [currentTheme, setCurrentTheme] = useState(() => {
const savedTheme = localStorage.getItem('theme')
return savedTheme || 'light'
})
const toggleTheme = (e: React.ChangeEvent<HTMLInputElement>) => {
const { checked } = e.target
setCurrentTheme(checked ? 'dark' : 'light')
localStorage.setItem('theme', checked ? 'dark' : 'light')
}
const theme = currentTheme === 'light' ? themes.light : themes.dark
return (
<ThemeProvider theme={theme}>
<GlobalStyles />
<QuizProvider>
<ToggleTheme
onChange={toggleTheme}
currentTheme={currentTheme}
checked={currentTheme === 'dark'}
id="toggleTheme"
value="theme"
/>
<Main />
</QuizProvider>
</ThemeProvider>
)
}
export default App