1
1
mirror of https://github.com/theoludwig/react-component-form.git synced 2024-07-17 07:30:13 +02:00
react-component-form/example/components/Header/SwitchTheme.tsx

79 lines
2.4 KiB
TypeScript
Raw Normal View History

import { useEffect, useState } from "react"
import classNames from "clsx"
import { useTheme } from "next-themes"
2022-08-26 20:19:31 +02:00
export const SwitchTheme: React.FC = () => {
const [mounted, setMounted] = useState(false)
const { theme, setTheme } = useTheme()
useEffect(() => {
setMounted(true)
}, [])
if (!mounted) {
return null
}
const handleClick = (): void => {
setTheme(theme === "dark" ? "light" : "dark")
2022-08-26 20:19:31 +02:00
}
return (
2022-10-03 21:23:17 +02:00
<div
className="flex items-center"
data-cy="switch-theme-click"
2022-10-03 21:23:17 +02:00
onClick={handleClick}
>
<div className="relative inline-block cursor-pointer touch-pan-x select-none border-0 bg-transparent p-0">
<div className="h-[24px] w-[50px] rounded-[30px] bg-[#4d4d4d] p-0 text-white transition-all duration-200 ease-in-out">
2022-10-03 21:23:17 +02:00
<div
data-cy="switch-theme-dark"
2022-10-03 21:23:17 +02:00
className={classNames(
"absolute top-0 bottom-0 left-[8px] mt-auto mb-auto h-[10px] w-[14px] leading-[0] transition-opacity duration-[250ms] ease-in-out",
2022-10-03 21:23:17 +02:00
{
"opacity-100": theme === "dark",
"opacity-0": theme === "light",
},
2022-10-03 21:23:17 +02:00
)}
>
<span className="relative flex h-[10px] w-[10px] items-center justify-center">
2022-10-03 21:23:17 +02:00
🌜
</span>
</div>
<div
data-cy="switch-theme-light"
2022-10-03 21:23:17 +02:00
className={classNames(
"absolute right-[10px] top-0 bottom-0 mt-auto mb-auto h-[10px] w-[10px] leading-[0]",
2022-10-03 21:23:17 +02:00
{
"opacity-100": theme === "light",
"opacity-0": theme === "dark",
},
2022-10-03 21:23:17 +02:00
)}
>
<span className="relative flex h-[10px] w-[10px] items-center justify-center">
2022-10-03 21:23:17 +02:00
🌞
</span>
2022-08-26 20:19:31 +02:00
</div>
</div>
2022-10-03 21:23:17 +02:00
<div
className={classNames(
"absolute top-[1px] box-border h-[22px] w-[22px] rounded-[50%] bg-[#fafafa] text-white transition-all duration-[250ms] ease-in-out",
2022-10-03 21:23:17 +02:00
{
"left-[27px]": theme === "dark",
"left-0": theme === "light",
},
2022-10-03 21:23:17 +02:00
)}
style={{ border: "1px solid #4d4d4d" }}
2022-10-03 21:23:17 +02:00
/>
<input
data-cy="switch-theme-input"
type="checkbox"
aria-label="Dark mode toggle"
className="absolute m-[-1px] h-[1px] w-[1px] overflow-hidden border-0 p-0"
2022-10-03 21:23:17 +02:00
defaultChecked
/>
2022-08-26 20:19:31 +02:00
</div>
2022-10-03 21:23:17 +02:00
</div>
2022-08-26 20:19:31 +02:00
)
}