All files / components/layout theme-toggle.tsx

92.3% Statements 12/13
100% Branches 14/14
80% Functions 4/5
91.66% Lines 11/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 841x                               6x 6x   6x 3x     6x   3x 6x   6x                                               1x                 1x                                            
"use client";
 
import { useTheme } from "next-themes";
import {
  DropdownMenu,
  DropdownMenuTrigger,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuPortal,
} from "@/components/ui/dropdown-menu";
 
import { Button } from "@/components/ui/button";
import { Moon, Sun, Laptop } from "lucide-react";
import { useEffect, useState } from "react";
 
export function ThemeToggle() {
  const { theme, setTheme, systemTheme } = useTheme();
  const [mounted, setMounted] = useState(false);
 
  useEffect(() => {
    setMounted(true);
  }, []);
 
  if (!mounted) return null;
 
  const currentTheme = theme === "system" ? systemTheme : theme;
  const isDark = currentTheme === "dark";
 
  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button
          variant="outline"
          size="icon"
          aria-label={
            isDark
              ? "Current theme dark. Open theme menu"
              : "Current theme light. Open theme menu"
          }
          title="Toggle theme"
        >
          {isDark ? (
            <Moon className="h-[1.2rem] w-[1.2rem] text-slate-300" />
          ) : (
            <Sun className="h-[1.2rem] w-[1.2rem] text-orange-500" />
          )}
        </Button>
      </DropdownMenuTrigger>
 
      <DropdownMenuPortal>
        <DropdownMenuContent className="z-50 border border-gray-200 dark:border-gray-700 bg-white dark:bg-neutral-900">
          <DropdownMenuItem
            onClick={() => setTheme("light")}
            className={`hover:bg-orange-200 dark:hover:bg-gray-600 ${
              isDark ? "text-orange-400" : "text-slate-700"
            }`}
          >
            <Sun className="mr-2 h-4 w-4" />
            Light
          </DropdownMenuItem>
          <DropdownMenuItem
            onClick={() => setTheme("dark")}
            className={`hover:bg-orange-200 dark:hover:bg-gray-600 ${
              isDark ? "text-orange-400" : "text-slate-700"
            }`}
          >
            <Moon className="mr-2 h-4 w-4" />
            Dark
          </DropdownMenuItem>
          <DropdownMenuItem
            onClick={() => setTheme("system")}
            className={`hover:bg-orange-200 dark:hover:bg-gray-600 ${
              isDark ? "text-orange-400" : "text-slate-700"
            }`}
          >
            <Laptop className="mr-2 h-4 w-4" />
            System
          </DropdownMenuItem>
        </DropdownMenuContent>
      </DropdownMenuPortal>
    </DropdownMenu>
  );
}