Files
xrlms/src/app/NavBar.tsx

133 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-11-18 06:19:26 +09:00
'use client';
import Link from "next/link";
import { useEffect, useRef, useState } from "react";
import { usePathname } from "next/navigation";
import MainLogoSvg from "./svgs/mainlogosvg";
2025-11-18 07:53:09 +09:00
import ChevronDownSvg from "./svgs/chevrondownsvg";
2025-11-18 06:19:26 +09:00
const NAV_ITEMS = [
2025-11-18 09:09:09 +09:00
{ label: "교육 과정 목록", href: "/course-list" },
2025-11-18 06:19:26 +09:00
{ label: "학습 자료실", href: "/resources" },
{ label: "공지사항", href: "/notices" },
];
export default function NavBar() {
const pathname = usePathname();
const [isUserMenuOpen, setIsUserMenuOpen] = useState(false);
const userMenuRef = useRef<HTMLDivElement | null>(null);
const userButtonRef = useRef<HTMLButtonElement | null>(null);
2025-11-18 10:42:48 +09:00
const hideCenterNav = /^\/[^/]+\/review$/.test(pathname);
2025-11-18 23:42:41 +09:00
const isAdminPage = pathname.startsWith('/admin-id');
2025-11-18 06:19:26 +09:00
useEffect(() => {
if (!isUserMenuOpen) return;
const onDown = (e: MouseEvent) => {
const t = e.target as Node;
if (
userMenuRef.current &&
!userMenuRef.current.contains(t) &&
userButtonRef.current &&
!userButtonRef.current.contains(t)
) {
setIsUserMenuOpen(false);
}
};
const onKey = (e: KeyboardEvent) => {
if (e.key === "Escape") setIsUserMenuOpen(false);
};
document.addEventListener("mousedown", onDown);
document.addEventListener("keydown", onKey);
return () => {
document.removeEventListener("mousedown", onDown);
document.removeEventListener("keydown", onKey);
};
}, [isUserMenuOpen]);
return (
<header className="bg-[#060958] h-20">
<div className="mx-auto flex h-full w-full max-w-[1440px] items-center justify-between px-8">
<div className="flex flex-1 items-center gap-9">
<Link href="/" aria-label="XR LMS 홈" className="flex items-center gap-2">
<MainLogoSvg width={46.703} height={36} />
<span className="text-2xl font-extrabold leading-[1.45] text-white">XR LMS</span>
</Link>
2025-11-18 23:42:41 +09:00
{!hideCenterNav && !isAdminPage && (
2025-11-18 10:42:48 +09:00
<nav className="flex h-full items-center">
{NAV_ITEMS.map((item) => {
return (
<Link
key={item.href}
href={item.href}
className={["px-4 py-2 text-[16px] font-semibold text-white"].join(" ")}
>
{item.label}
</Link>
);
})}
</nav>
)}
2025-11-18 06:19:26 +09:00
</div>
<div className="relative flex items-center gap-2">
2025-11-18 23:42:41 +09:00
{isAdminPage ? (
<>
<Link href="/menu/account" className="px-4 py-2 text-[16px] font-semibold text-white">
</Link>
<Link href="/login" className="px-4 py-2 text-[16px] font-semibold text-white">
</Link>
</>
) : (
<>
<Link href="/menu/courses" className="px-4 py-2 text-[16px] font-semibold text-white">
2025-11-18 07:53:09 +09:00
</Link>
2025-11-18 06:19:26 +09:00
<button
2025-11-18 23:42:41 +09:00
ref={userButtonRef}
type="button"
onClick={() => setIsUserMenuOpen((v) => !v)}
aria-haspopup="menu"
aria-expanded={isUserMenuOpen}
className="flex items-center gap-1 px-4 py-2 text-[16px] font-semibold text-white cursor-pointer"
2025-11-18 06:19:26 +09:00
>
2025-11-18 23:42:41 +09:00
<ChevronDownSvg
width={16}
height={16}
className={["transition-transform", isUserMenuOpen ? "rotate-180" : "rotate-0"].join(" ")}
/>
2025-11-18 06:19:26 +09:00
</button>
2025-11-18 23:42:41 +09:00
{isUserMenuOpen && (
<div
ref={userMenuRef}
role="menu"
aria-label="사용자 메뉴"
className="absolute right-0 top-full mt-2 bg-white rounded-lg shadow-[0_0_8px_0_rgba(0,0,0,0.25)] p-3 z-50"
>
<Link
role="menuitem"
href="/menu/account"
className="block w-full h-10 px-2 rounded-lg text-left text-[#333C47] text-[16px] font-medium leading-normal hover:bg-[rgba(236,240,255,0.5)] focus:bg-[rgba(236,240,255,0.5)] outline-none"
onClick={() => setIsUserMenuOpen(false)}
>
</Link>
<button
role="menuitem"
className="w-full h-10 px-2 rounded-lg text-left text-[#333C47] text-[16px] font-medium leading-normal hover:bg-[rgba(236,240,255,0.5)] focus:bg-[rgba(236,240,255,0.5)] outline-none"
>
</button>
</div>
)}
</>
2025-11-18 06:19:26 +09:00
)}
</div>
</div>
</header>
);
}