2023-10-23 23:11:59 +02:00
|
|
|
"use client"
|
2023-07-30 18:50:14 +02:00
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
import classNames from "clsx"
|
2021-05-08 19:52:04 +02:00
|
|
|
|
2023-10-23 23:11:59 +02:00
|
|
|
import { useTheme } from "@/theme/theme.client"
|
|
|
|
import type { CookiesStore } from "@/utils/constants"
|
2021-05-08 19:52:04 +02:00
|
|
|
|
2023-08-01 14:11:46 +02:00
|
|
|
export interface SwitchThemeProps {
|
|
|
|
cookiesStore: CookiesStore
|
|
|
|
}
|
2021-05-08 19:52:04 +02:00
|
|
|
|
2023-08-01 14:11:46 +02:00
|
|
|
export const SwitchTheme = (props: SwitchThemeProps): JSX.Element => {
|
|
|
|
const { cookiesStore } = props
|
|
|
|
const theme = useTheme(cookiesStore)
|
2021-05-08 19:52:04 +02:00
|
|
|
|
2023-08-01 14:11:46 +02:00
|
|
|
const handleClick = async (): Promise<void> => {
|
2023-10-23 23:11:59 +02:00
|
|
|
const { setTheme } = await import("@/theme/theme.server")
|
|
|
|
const newTheme = theme === "dark" ? "light" : "dark"
|
2023-08-01 14:11:46 +02:00
|
|
|
setTheme(newTheme)
|
2021-08-13 15:48:29 +02:00
|
|
|
}
|
|
|
|
|
2021-05-08 19:52:04 +02:00
|
|
|
return (
|
2022-10-03 20:52:15 +02:00
|
|
|
<div
|
2023-10-23 23:11:59 +02:00
|
|
|
className="flex items-center"
|
|
|
|
data-cy="switch-theme-click"
|
2022-10-03 20:52:15 +02:00
|
|
|
onClick={handleClick}
|
|
|
|
>
|
2023-10-23 23:11:59 +02:00
|
|
|
<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 20:52:15 +02:00
|
|
|
<div
|
2023-10-23 23:11:59 +02:00
|
|
|
data-cy="switch-theme-dark"
|
2022-10-03 20:52:15 +02:00
|
|
|
className={classNames(
|
2024-01-28 03:21:11 +01:00
|
|
|
"absolute inset-y-0 left-[8px] my-auto h-[10px] w-[14px] leading-[0] transition-opacity duration-[250ms] ease-in-out",
|
2022-10-03 20:52:15 +02:00
|
|
|
{
|
2023-10-23 23:11:59 +02:00
|
|
|
"opacity-100": theme === "dark",
|
|
|
|
"opacity-0": theme === "light",
|
|
|
|
},
|
2022-10-03 20:52:15 +02:00
|
|
|
)}
|
|
|
|
>
|
2024-01-28 03:21:11 +01:00
|
|
|
<span className="relative flex size-[10px] items-center justify-center">
|
2022-10-03 20:52:15 +02:00
|
|
|
🌜
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
<div
|
2023-10-23 23:11:59 +02:00
|
|
|
data-cy="switch-theme-light"
|
2022-10-03 20:52:15 +02:00
|
|
|
className={classNames(
|
2024-01-28 03:21:11 +01:00
|
|
|
"absolute inset-y-0 right-[10px] my-auto size-[10px] leading-[0]",
|
2022-10-03 20:52:15 +02:00
|
|
|
{
|
2023-10-23 23:11:59 +02:00
|
|
|
"opacity-100": theme === "light",
|
|
|
|
"opacity-0": theme === "dark",
|
|
|
|
},
|
2022-10-03 20:52:15 +02:00
|
|
|
)}
|
|
|
|
>
|
2024-01-28 03:21:11 +01:00
|
|
|
<span className="relative flex size-[10px] items-center justify-center">
|
2022-10-03 20:52:15 +02:00
|
|
|
🌞
|
|
|
|
</span>
|
2021-05-08 19:52:04 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2022-10-03 20:52:15 +02:00
|
|
|
<div
|
|
|
|
className={classNames(
|
2024-05-21 20:18:05 +02:00
|
|
|
"absolute top-px box-border size-[22px] rounded-[50%] bg-[#fafafa] text-white transition-all duration-[250ms] ease-in-out",
|
2022-10-03 20:52:15 +02:00
|
|
|
{
|
2023-10-23 23:11:59 +02:00
|
|
|
"left-[27px]": theme === "dark",
|
|
|
|
"left-0": theme === "light",
|
|
|
|
},
|
2022-10-03 20:52:15 +02:00
|
|
|
)}
|
2023-10-23 23:11:59 +02:00
|
|
|
style={{ border: "1px solid #4d4d4d" }}
|
2022-10-03 20:52:15 +02:00
|
|
|
/>
|
|
|
|
<input
|
2023-10-23 23:11:59 +02:00
|
|
|
data-cy="switch-theme-input"
|
|
|
|
type="checkbox"
|
|
|
|
aria-label="Dark mode toggle"
|
2024-05-21 20:18:05 +02:00
|
|
|
className="absolute -m-px hidden size-px overflow-hidden border-0 p-0"
|
2022-10-03 20:52:15 +02:00
|
|
|
defaultChecked
|
|
|
|
/>
|
2021-05-08 19:52:04 +02:00
|
|
|
</div>
|
2022-10-03 20:52:15 +02:00
|
|
|
</div>
|
2021-05-08 19:52:04 +02:00
|
|
|
)
|
|
|
|
}
|