Files
pawol.nu/components/switch-theme.js
T

71 lines
1.6 KiB
JavaScript
Raw Normal View History

2021-06-05 21:25:07 +02:00
import PropTypes from 'prop-types'
2022-01-22 12:01:03 +04:00
import Image from 'next/image'
2022-01-19 06:35:04 +04:00
import {styled} from '@mui/material/styles'
2022-01-19 07:06:26 +04:00
import {Box} from '@mui/material'
2021-06-05 21:25:07 +02:00
2022-01-22 12:01:03 +04:00
import lightSwitch from '../public/theme-switch/light.png'
import darkSwitch from '../public/theme-switch/dark.png'
2022-01-19 06:35:04 +04:00
const PREFIX = 'switch-theme'
const classes = {
switch: `${PREFIX}-switch`,
switchFixed: `${PREFIX}-switchFixed`
}
const StyledBox = styled(Box)({
[`& .${classes.switch}`]: {
2022-01-19 06:00:14 +04:00
position: 'absolute',
right: '1em',
2022-01-21 07:54:50 +04:00
top: '100px',
2022-01-19 06:00:14 +04:00
zIndex: 1,
cursor: 'pointer'
},
2022-01-19 06:35:04 +04:00
[`& .${classes.switchFixed}`]: {
2022-01-19 06:00:14 +04:00
position: 'fixed',
right: '1em',
2022-01-21 07:54:50 +04:00
top: '90px',
2022-01-19 06:00:14 +04:00
zIndex: 9990,
cursor: 'pointer'
}
})
2022-01-21 07:54:50 +04:00
export default function SwitchTheme({switchFixed, mode, setMode}) {
const handleClick = seletedMode => {
localStorage.setItem('oki-theme', seletedMode)
setMode(seletedMode)
2021-06-05 21:25:07 +02:00
}
return (
2022-01-21 07:54:50 +04:00
<StyledBox>
<div className={switchFixed ? classes.switchFixed : classes.switch} >
{mode === 'dark' ? (
2022-01-22 12:01:03 +04:00
<Image
alt='Logo #OKi'
width={24}
height={24}
src={darkSwitch}
quality={10}
onClick={() => handleClick('light')}
/>
2022-01-21 07:54:50 +04:00
) : (
2022-01-22 12:01:03 +04:00
<Image
alt='Logo #OKi'
width={24}
height={24}
src={lightSwitch}
quality={10}
onClick={() => handleClick('dark')}
/>
2022-01-21 07:54:50 +04:00
)}
</div>
2022-01-19 06:35:04 +04:00
</StyledBox>
2021-06-05 21:25:07 +02:00
)
}
SwitchTheme.propTypes = {
2022-01-19 06:00:14 +04:00
switchFixed: PropTypes.bool.isRequired,
2022-01-21 07:54:50 +04:00
mode: PropTypes.oneOf(['light', 'dark']),
setMode: PropTypes.func.isRequired
2021-06-05 21:25:07 +02:00
}