RotNot / src /components /bottom-tab-nav.tsx
deploy
Deploy RotNot
9580dce
Raw History Blame Contribute Delete
8.5 kB
"use client";
import Link from "next/link";
import { usePathname } from "next/navigation";
import {
LayoutDashboard,
BrainCircuit,
User,
MoreHorizontal,
Castle,
BookOpen,
Lightbulb,
Puzzle,
Swords,
Wand2,
Code2,
BarChart3,
Settings,
X,
MessageSquare,
} from "lucide-react";
import { useState, useEffect } from "react";
import { motion, AnimatePresence } from "framer-motion";
type TabItem = {
href: string;
label: string;
icon: React.ReactNode;
color: string;
};
// Primary tabs shown in the bottom bar (most used)
const PRIMARY_TABS: TabItem[] = [
{
href: "/",
label: "Home",
icon: <LayoutDashboard size={22} />,
color: "rgba(255,255,255,0.87)",
},
{
href: "/feynman",
label: "Feynman",
icon: <Lightbulb size={22} />,
color: "var(--color-feynman)",
},
{
href: "/coach",
label: "Coach",
icon: <BrainCircuit size={22} />,
color: "#6366f1",
},
{
href: "/profile",
label: "Profile",
icon: <User size={22} />,
color: "rgba(255,255,255,0.87)",
},
{
href: "__more__",
label: "More",
icon: <MoreHorizontal size={22} />,
color: "rgba(255,255,255,0.7)",
},
];
// All nav items for the "More" sheet
const ALL_ITEMS: TabItem[] = [
{
href: "/chess",
label: "Chess",
icon: <Castle size={20} />,
color: "var(--color-chess)",
},
{
href: "/reading",
label: "Deep Reading",
icon: <BookOpen size={20} />,
color: "var(--color-reading)",
},
{
href: "/logic",
label: "Logic Puzzles",
icon: <Puzzle size={20} />,
color: "var(--color-logic)",
},
{
href: "/critical-thinking",
label: "Critical Thinking",
icon: <Swords size={20} />,
color: "var(--color-critical-thinking)",
},
{
href: "/quests",
label: "Skill Quests",
icon: <Wand2 size={20} />,
color: "var(--color-quests)",
},
{
href: "/coding",
label: "Coding Training",
icon: <Code2 size={20} />,
color: "var(--color-coding)",
},
{
href: "/debate",
label: "Debate Arena",
icon: <MessageSquare size={20} />,
color: "var(--color-critical-thinking)",
},
{
href: "/stats",
label: "Stats",
icon: <BarChart3 size={20} />,
color: "rgba(255,255,255,0.7)",
},
{
href: "/settings",
label: "Settings",
icon: <Settings size={20} />,
color: "rgba(255,255,255,0.7)",
},
];
export function BottomTabNav() {
const pathname = usePathname();
const [moreOpen, setMoreOpen] = useState(false);
// Close sheet on route change — setState here is intentional:
// dismiss the "More" drawer whenever the user navigates to a new route.
useEffect(() => {
setMoreOpen(false); // eslint-disable-line react-hooks/set-state-in-effect
}, [pathname]);
// Prevent body scroll when sheet is open
useEffect(() => {
if (moreOpen) {
document.body.style.overflow = "hidden";
} else {
document.body.style.overflow = "";
}
return () => {
document.body.style.overflow = "";
};
}, [moreOpen]);
const isMoreActive = ALL_ITEMS.some((item) =>
pathname.startsWith(item.href)
);
return (
<>
{/* Bottom tab bar */}
<nav
className="md:hidden fixed bottom-0 left-0 right-0 z-40 flex items-stretch border-t"
style={{
background: "var(--surface-1)",
borderColor: "rgba(255,255,255,0.08)",
paddingBottom: "env(safe-area-inset-bottom)",
height: "calc(4rem + env(safe-area-inset-bottom))",
}}
aria-label="Main navigation"
>
{PRIMARY_TABS.map((tab) => {
if (tab.href === "__more__") {
const active = isMoreActive || moreOpen;
return (
<button
key="more"
onClick={() => setMoreOpen((prev) => !prev)}
className="flex-1 flex flex-col items-center justify-center gap-0.5 pt-2 pb-1 transition-colors"
style={{ color: active ? tab.color : "var(--text-muted)" }}
aria-label="More pages"
aria-expanded={moreOpen}
>
<span style={{ color: active ? tab.color : "var(--text-muted)" }}>
{moreOpen ? <X size={22} /> : tab.icon}
</span>
<span className="text-[10px] font-medium tracking-wide">
{moreOpen ? "Close" : tab.label}
</span>
</button>
);
}
const isActive =
tab.href === "/"
? pathname === "/"
: pathname.startsWith(tab.href);
return (
<Link
key={tab.href}
href={tab.href}
className="flex-1 flex flex-col items-center justify-center gap-0.5 pt-2 pb-1 transition-colors"
style={{ color: isActive ? tab.color : "var(--text-muted)" }}
aria-current={isActive ? "page" : undefined}
>
<span style={{ color: isActive ? tab.color : "var(--text-muted)" }}>
{tab.icon}
</span>
<span className="text-[10px] font-medium tracking-wide">
{tab.label}
</span>
{isActive && (
<motion.span
layoutId="bottom-tab-indicator"
className="absolute top-0 left-1/2 -translate-x-1/2 w-8 h-0.5 rounded-full"
style={{ background: tab.color }}
/>
)}
</Link>
);
})}
</nav>
{/* "More" bottom sheet — slides up from bottom */}
<AnimatePresence>
{moreOpen && (
<>
{/* Backdrop */}
<motion.div
key="backdrop"
initial={{ opacity: 0 }}
animate={{ opacity: 1 }}
exit={{ opacity: 0 }}
transition={{ duration: 0.15 }}
className="md:hidden fixed inset-0 z-30 bg-black/60 backdrop-blur-sm"
onClick={() => setMoreOpen(false)}
aria-hidden
/>
{/* Sheet */}
<motion.div
key="sheet"
initial={{ y: "100%" }}
animate={{ y: 0 }}
exit={{ y: "100%" }}
transition={{ type: "spring", damping: 28, stiffness: 300 }}
className="md:hidden fixed bottom-0 left-0 right-0 z-40 rounded-t-2xl border-t"
style={{
background: "var(--surface-1)",
borderColor: "rgba(255,255,255,0.08)",
paddingBottom: "calc(4.5rem + env(safe-area-inset-bottom))",
}}
>
{/* Handle */}
<div className="flex justify-center pt-3 pb-1">
<div
className="w-10 h-1 rounded-full"
style={{ background: "rgba(255,255,255,0.15)" }}
/>
</div>
<div className="px-4 pt-2 pb-1">
<p
className="text-xs font-semibold uppercase tracking-widest mb-3"
style={{ color: "var(--text-muted)" }}
>
Exercises &amp; More
</p>
<div className="grid grid-cols-2 gap-2">
{ALL_ITEMS.map((item) => {
const isActive = pathname.startsWith(item.href);
return (
<Link
key={item.href}
href={item.href}
className="flex items-center gap-3 px-3 py-3 rounded-xl text-sm font-medium transition-all"
style={{
color: isActive ? item.color : "var(--text-secondary)",
background: isActive
? `color-mix(in srgb, ${item.color} 14%, transparent)`
: "rgba(255,255,255,0.04)",
}}
>
<span style={{ color: isActive ? item.color : "var(--text-muted)" }}>
{item.icon}
</span>
<span className="truncate">{item.label}</span>
</Link>
);
})}
</div>
</div>
</motion.div>
</>
)}
</AnimatePresence>
</>
);
}