aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/src/components/theme-toggle.tsx
blob: 753277579e8030ed7f122be287431f23f60549a9 (plain) (blame)
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
84
85
86
87
88
89
90
91
92
93
94
"use client"

import * as React from "react"
import { Moon, Sun, Monitor } from "lucide-react"
import { useTheme } from "next-themes"

import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"

export function ThemeToggle() {
  const { setTheme, theme } = useTheme()
  const [mounted, setMounted] = React.useState(false)

  // Ensure we only render the correct icon after mounting to avoid hydration mismatch
  React.useEffect(() => {
    setMounted(true)
  }, [])

  if (!mounted) {
    return (
      <Button 
        variant="ghost" 
        size="icon" 
        className="nav-button-transparent backdrop-blur-sm min-w-[44px] min-h-[44px]"
        disabled
      >
        <Monitor className="h-5 w-5" />
        <span className="sr-only">Toggle theme</span>
      </Button>
    )
  }

  return (
    <DropdownMenu>
      <DropdownMenuTrigger asChild>
        <Button 
          variant="ghost" 
          size="icon" 
          className="nav-button-transparent backdrop-blur-sm min-w-[44px] min-h-[44px]"
        >
          {/* Simple icon transitions with consistent styling */}
          <Sun className="h-5 w-5 rotate-0 scale-100 transition-all duration-500 ease-in-out dark:-rotate-180 dark:scale-0" />
          <Moon className="absolute h-5 w-5 rotate-180 scale-0 transition-all duration-500 ease-in-out dark:rotate-0 dark:scale-100" />
          
          <span className="sr-only">Toggle theme</span>
        </Button>
      </DropdownMenuTrigger>
      
      <DropdownMenuContent 
        align="end" 
        className="nav-dropdown-transparent"
        sideOffset={8}
      >
        <DropdownMenuItem 
          onClick={() => setTheme("light")} 
          className="flex items-center gap-3 nav-dropdown-item cursor-pointer"
        >
          <Sun className="h-4 w-4" />
          <span className="flex-1">Light</span>
          {theme === "light" && (
            <span className="text-xs font-medium"></span>
          )}
        </DropdownMenuItem>
        
        <DropdownMenuItem 
          onClick={() => setTheme("dark")} 
          className="flex items-center gap-3 nav-dropdown-item cursor-pointer"
        >
          <Moon className="h-4 w-4" />
          <span className="flex-1">Dark</span>
          {theme === "dark" && (
            <span className="text-xs font-medium"></span>
          )}
        </DropdownMenuItem>
        
        <DropdownMenuItem 
          onClick={() => setTheme("system")} 
          className="flex items-center gap-3 nav-dropdown-item cursor-pointer"
        >
          <Monitor className="h-4 w-4" />
          <span className="flex-1">System</span>
          {theme === "system" && (
            <span className="text-xs font-medium"></span>
          )}
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}