File size: 4,776 Bytes
5081fcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"use client";

import * as React from "react";
import { Moon, Sun } from "lucide-react";
import { useTheme } from "next-themes";
import Link from "next/link";
import { Button } from "@/components/ui/button";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuItem,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";

/**
 * TopNav - A responsive navigation header component with theme switching functionality
 * Features:
 * - Colorful brand logo/text
 * - Navigation links for teachers and students
 * - Theme switcher (light/dark/system) with animated icons
 * - Client-side hydration handling
 */
export function TopNav() {
  const { setTheme } = useTheme();
  // Track component mounting to prevent hydration mismatch
  const [mounted, setMounted] = React.useState(false);

  // Enable theme switching only after client-side hydration
  React.useEffect(() => {
    setMounted(true);
  }, []);

  // Pre-hydration render - shows a simplified version without theme toggle
  if (!mounted) {
    return (
      <header className="fixed w-full bg-background-secondary/80 backdrop-blur-sm border-b border-border z-50">
        <div className="container mx-auto px-4 py-4 flex items-center">
          <Link href="/" className="text-3xl font-bold">
            <span className="text-[#FF6B6B]">P</span>
            <span className="text-[#4ECDC4]">l</span>
            <span className="text-[#45B7D1]">a</span>
            <span className="text-[#FDCB6E]">y</span>
            <span className="text-[#FF6B6B]">G</span>
            <span className="text-[#4ECDC4]">o</span>
            <span className="ml-2 text-[#45B7D1]">A</span>
            <span className="text-[#FDCB6E]">I</span>
          </Link>
          <nav className="flex items-center justify-center flex-1 space-x-8">
            <Link
              href="/for-teachers"
              className="text-text-secondary hover:text-primary"
            >
              我是老師
            </Link>
            <Link
              href="/for-students"
              className="text-text-secondary hover:text-primary"
            >
              我是學生
            </Link>
          </nav>
          <div className="w-9 h-9" />{" "}
          {/* Placeholder maintaining layout consistency during hydration */}
        </div>
      </header>
    );
  }

  // Post-hydration render - includes full functionality with theme toggle
  return (
    <header className="fixed w-full bg-background-secondary/80 backdrop-blur-sm border-b border-border z-50">
      <div className="container mx-auto px-4 py-4 flex items-center">
        {/* Brand logo with multi-colored letters */}
        <Link href="/" className="text-3xl font-bold">
          <span className="text-[#FF6B6B]">P</span>
          <span className="text-[#4ECDC4]">l</span>
          <span className="text-[#45B7D1]">a</span>
          <span className="text-[#FDCB6E]">y</span>
          <span className="text-[#FF6B6B]">G</span>
          <span className="text-[#4ECDC4]">o</span>
          <span className="ml-2 text-[#45B7D1]">A</span>
          <span className="text-[#FDCB6E]">I</span>
        </Link>

        {/* Navigation links with language-specific text (Traditional Chinese) */}
        <nav className="flex items-center justify-center flex-1 space-x-8">
          <Link
            href="/for-teachers"
            className="text-text-secondary hover:text-primary"
          >
            我是老師
          </Link>
          <Link
            href="/for-students"
            className="text-text-secondary hover:text-primary"
          >
            我是學生
          </Link>
        </nav>

        {/* Theme switcher dropdown with animated sun/moon icons */}
        <DropdownMenu>
          <DropdownMenuTrigger asChild>
            <Button>
              {/* Animated sun/moon icons that rotate based on theme */}
              <Sun className="h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0" />
              <Moon className="absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100" />
              <span className="sr-only">Toggle theme</span>
            </Button>
          </DropdownMenuTrigger>
          {/* Theme selection menu */}
          <DropdownMenuContent align="end">
            <DropdownMenuItem onClick={() => setTheme("light")}>
              Light
            </DropdownMenuItem>
            <DropdownMenuItem onClick={() => setTheme("dark")}>
              Dark
            </DropdownMenuItem>
            <DropdownMenuItem onClick={() => setTheme("system")}>
              System
            </DropdownMenuItem>
          </DropdownMenuContent>
        </DropdownMenu>
      </div>
    </header>
  );
}