theme-switch.tsx 1.3 KB

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