theme-switch.tsx 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use client";
  2. import { FC, useEffect, useState } from "react";
  3. import { Switch } from "@heroui/switch";
  4. import { useTheme } from "next-themes";
  5. import clsx from "clsx";
  6. import { SunIcon, MoonIcon } from "@/components/icons";
  7. export interface ThemeSwitchProps {
  8. className?: string;
  9. }
  10. export const ThemeSwitch: FC<ThemeSwitchProps> = ({ className }) => {
  11. const { theme, setTheme } = useTheme();
  12. const [mounted, setMounted] = useState(false);
  13. useEffect(() => {
  14. setMounted(true);
  15. }, []);
  16. if (!mounted) return null;
  17. const isLight = theme === "light";
  18. return (
  19. <div className="border-2 border-transparent dark:border-knight rounded-full">
  20. <Switch
  21. className={clsx(
  22. "transition-opacity hover:opacity-80 cursor-pointer",
  23. className,
  24. )}
  25. classNames={{
  26. wrapper:
  27. "w-[4.5rem] h-10 group-data-[selected=true]:bg-midnight bg-[#1e2228]",
  28. thumb: "w-8 h-8 group-data-[selected=true]:ms-8",
  29. base: "w-[4.5rem] h-10",
  30. thumbIcon: "w-5 h-5",
  31. }}
  32. isSelected={isLight}
  33. thumbIcon={({ isSelected, className: iconClassName }) =>
  34. isSelected ? (
  35. <SunIcon className={iconClassName} />
  36. ) : (
  37. <MoonIcon className={iconClassName} />
  38. )
  39. }
  40. onValueChange={(selected) => setTheme(selected ? "light" : "dark")}
  41. />
  42. </div>
  43. );
  44. };