Move useStyle inside TheProvider component
This commit is contained in:
@@ -1,25 +1,45 @@
|
|||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import WbSunnyIcon from '@material-ui/icons/WbSunny'
|
import WbSunnyIcon from '@material-ui/icons/WbSunny'
|
||||||
import Brightness3Icon from '@material-ui/icons/Brightness3'
|
import Brightness3Icon from '@material-ui/icons/Brightness3'
|
||||||
|
import {Box, makeStyles} from '@material-ui/core'
|
||||||
|
|
||||||
export default function SwitchTheme({darkMode, setDarkMode}) {
|
const useStyles = makeStyles({
|
||||||
|
switch: {
|
||||||
|
position: 'absolute',
|
||||||
|
right: '1em',
|
||||||
|
top: '95px',
|
||||||
|
zIndex: 1,
|
||||||
|
cursor: 'pointer'
|
||||||
|
},
|
||||||
|
switchFixed: {
|
||||||
|
position: 'fixed',
|
||||||
|
right: '1em',
|
||||||
|
top: '95px',
|
||||||
|
zIndex: 9990,
|
||||||
|
cursor: 'pointer'
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
export default function SwitchTheme({switchFixed, darkMode, setDarkMode}) {
|
||||||
|
const classes = useStyles()
|
||||||
const handleClick = () => {
|
const handleClick = () => {
|
||||||
localStorage.setItem('oki-dark', !darkMode)
|
localStorage.setItem('oki-dark', !darkMode)
|
||||||
setDarkMode(!darkMode)
|
setDarkMode(!darkMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<Box className={switchFixed ? classes.switchFixed : classes.switch}>
|
||||||
{darkMode ? (
|
{darkMode ? (
|
||||||
<WbSunnyIcon onClick={handleClick} />
|
<WbSunnyIcon onClick={handleClick} />
|
||||||
) : (
|
) : (
|
||||||
<Brightness3Icon onClick={handleClick} />
|
<Brightness3Icon onClick={handleClick} />
|
||||||
)}
|
)}
|
||||||
</div>
|
</Box>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
SwitchTheme.propTypes = {
|
SwitchTheme.propTypes = {
|
||||||
|
switchFixed: PropTypes.bool.isRequired,
|
||||||
darkMode: PropTypes.bool.isRequired,
|
darkMode: PropTypes.bool.isRequired,
|
||||||
setDarkMode: PropTypes.func.isRequired
|
setDarkMode: PropTypes.func.isRequired
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-23
@@ -1,35 +1,16 @@
|
|||||||
import {useEffect, useMemo, useState} from 'react'
|
import {useEffect, useMemo, useState} from 'react'
|
||||||
import {useRouter} from 'next/router'
|
import {useRouter} from 'next/router'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
import {createMuiTheme, ThemeProvider, makeStyles} from '@material-ui/core/styles'
|
import {createMuiTheme, ThemeProvider} from '@material-ui/core/styles'
|
||||||
import CssBaseline from '@material-ui/core/CssBaseline'
|
import CssBaseline from '@material-ui/core/CssBaseline'
|
||||||
import {Provider, useSession} from 'next-auth/client'
|
import {Provider, useSession} from 'next-auth/client'
|
||||||
import {Box} from '@material-ui/core'
|
|
||||||
|
|
||||||
import SwitchTheme from '../components/switch-theme'
|
import SwitchTheme from '../components/switch-theme'
|
||||||
|
|
||||||
const useStyles = makeStyles({
|
|
||||||
switch: {
|
|
||||||
position: 'absolute',
|
|
||||||
right: '1em',
|
|
||||||
top: '95px',
|
|
||||||
zIndex: 1,
|
|
||||||
cursor: 'pointer'
|
|
||||||
},
|
|
||||||
switchFixed: {
|
|
||||||
position: 'fixed',
|
|
||||||
right: '1em',
|
|
||||||
top: '95px',
|
|
||||||
zIndex: 9990,
|
|
||||||
cursor: 'pointer'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
export default function MyApp(props) {
|
export default function MyApp(props) {
|
||||||
const {Component, pageProps} = props
|
const {Component, pageProps} = props
|
||||||
const [darkMode, setDarkMode] = useState(false)
|
const [darkMode, setDarkMode] = useState(false)
|
||||||
const [switchFixed, setSwitchFixed] = useState(false)
|
const [switchFixed, setSwitchFixed] = useState(false)
|
||||||
const classes = useStyles()
|
|
||||||
|
|
||||||
const darkTheme = useMemo(() => createMuiTheme({
|
const darkTheme = useMemo(() => createMuiTheme({
|
||||||
palette: {
|
palette: {
|
||||||
@@ -71,9 +52,7 @@ export default function MyApp(props) {
|
|||||||
<ThemeProvider theme={darkTheme}>
|
<ThemeProvider theme={darkTheme}>
|
||||||
<CssBaseline />
|
<CssBaseline />
|
||||||
<Provider session={pageProps.session}>
|
<Provider session={pageProps.session}>
|
||||||
<Box className={switchFixed ? classes.switchFixed : classes.switch}>
|
<SwitchTheme switchFixed={switchFixed} darkMode={darkMode} setDarkMode={setDarkMode} />
|
||||||
<SwitchTheme darkMode={darkMode} setDarkMode={setDarkMode} />
|
|
||||||
</Box>
|
|
||||||
{Component.auth ? (
|
{Component.auth ? (
|
||||||
<Auth><Component {...pageProps} /></Auth>
|
<Auth><Component {...pageProps} /></Auth>
|
||||||
) : (
|
) : (
|
||||||
|
|||||||
Reference in New Issue
Block a user