theme-switch.tsx 1.4 KB

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