2021-10-24 05:48:06 +02:00
|
|
|
import { useEffect, useState } from 'react'
|
|
|
|
import { useTheme } from 'next-themes'
|
2021-10-24 05:19:39 +02:00
|
|
|
|
|
|
|
export const SwitchTheme: React.FC = () => {
|
2021-10-24 05:48:06 +02:00
|
|
|
const [mounted, setMounted] = useState(false)
|
|
|
|
const { theme, setTheme } = useTheme()
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setMounted(true)
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
if (!mounted) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleClick = (): void => {
|
|
|
|
setTheme(theme === 'dark' ? 'light' : 'dark')
|
|
|
|
}
|
2021-10-24 05:19:39 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2021-10-24 05:48:06 +02:00
|
|
|
<div
|
|
|
|
className='flex items-center'
|
|
|
|
data-cy='switch-theme-click'
|
|
|
|
onClick={handleClick}
|
|
|
|
>
|
2022-02-19 23:20:33 +01:00
|
|
|
<div className='toggle-theme-button relative inline-block cursor-pointer bg-transparent'>
|
2021-10-24 05:19:39 +02:00
|
|
|
<div className='toggle-track'>
|
2021-10-24 05:48:06 +02:00
|
|
|
<div
|
|
|
|
data-cy='switch-theme-dark'
|
|
|
|
className='toggle-track-check absolute'
|
|
|
|
>
|
2022-02-19 23:20:33 +01:00
|
|
|
<span className='toggle_Dark relative flex items-center justify-center'>
|
2021-10-24 05:48:06 +02:00
|
|
|
🌜
|
|
|
|
</span>
|
2021-10-24 05:19:39 +02:00
|
|
|
</div>
|
2021-10-24 05:48:06 +02:00
|
|
|
<div
|
|
|
|
data-cy='switch-theme-light'
|
|
|
|
className='toggle-track-x absolute'
|
|
|
|
>
|
2022-02-19 23:20:33 +01:00
|
|
|
<span className='toggle_Light relative flex items-center justify-center'>
|
2021-10-24 05:48:06 +02:00
|
|
|
🌞
|
|
|
|
</span>
|
2021-10-24 05:19:39 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-24 05:48:06 +02:00
|
|
|
<div className='toggle-thumb absolute' />
|
2021-10-24 05:19:39 +02:00
|
|
|
<input
|
2021-10-24 05:48:06 +02:00
|
|
|
data-cy='switch-theme-input'
|
2021-10-24 05:19:39 +02:00
|
|
|
type='checkbox'
|
|
|
|
aria-label='Dark mode toggle'
|
2021-10-24 05:48:06 +02:00
|
|
|
className='toggle-screenreader-only absolute overflow-hidden'
|
2021-10-24 05:19:39 +02:00
|
|
|
defaultChecked
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<style jsx>
|
|
|
|
{`
|
|
|
|
.toggle-theme-button {
|
|
|
|
touch-action: pan-x;
|
|
|
|
border: 0;
|
|
|
|
padding: 0;
|
|
|
|
user-select: none;
|
|
|
|
}
|
|
|
|
.toggle-track {
|
|
|
|
width: 50px;
|
|
|
|
height: 24px;
|
|
|
|
padding: 0;
|
|
|
|
border-radius: 30px;
|
|
|
|
background-color: #4d4d4d;
|
|
|
|
transition: all 0.2s ease;
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
.toggle-track-check {
|
|
|
|
width: 14px;
|
|
|
|
height: 10px;
|
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
margin-top: auto;
|
|
|
|
margin-bottom: auto;
|
|
|
|
line-height: 0;
|
|
|
|
left: 8px;
|
|
|
|
opacity: ${theme === 'dark' ? 1 : 0};
|
|
|
|
transition: opacity 0.25s ease;
|
|
|
|
}
|
|
|
|
.toggle-track-x {
|
|
|
|
width: 10px;
|
|
|
|
height: 10px;
|
|
|
|
top: 0;
|
|
|
|
bottom: 0;
|
|
|
|
margin-top: auto;
|
|
|
|
margin-bottom: auto;
|
|
|
|
line-height: 0;
|
|
|
|
right: 10px;
|
|
|
|
opacity: ${theme === 'dark' ? 0 : 1};
|
|
|
|
}
|
|
|
|
.toggle_Dark,
|
|
|
|
.toggle_Light {
|
|
|
|
height: 10px;
|
|
|
|
width: 10px;
|
|
|
|
}
|
|
|
|
.toggle-thumb {
|
|
|
|
left: ${theme === 'dark' ? '27px' : '0px'};
|
|
|
|
width: 22px;
|
|
|
|
height: 22px;
|
|
|
|
border: 1px solid #4d4d4d;
|
|
|
|
border-radius: 50%;
|
|
|
|
background-color: #fafafa;
|
|
|
|
box-sizing: border-box;
|
|
|
|
transition: all 0.25s ease;
|
|
|
|
top: 1px;
|
|
|
|
color: #fff;
|
|
|
|
}
|
|
|
|
.toggle-screenreader-only {
|
|
|
|
border: 0;
|
|
|
|
clip: rect(0 0 0 0);
|
|
|
|
height: 1px;
|
|
|
|
margin: -1px;
|
|
|
|
padding: 0;
|
|
|
|
width: 1px;
|
|
|
|
}
|
|
|
|
`}
|
|
|
|
</style>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|