All files / src/components/nav/NavBar Navbar.tsx

0% Statements 0/15
0% Branches 0/14
0% Functions 0/8
0% Lines 0/15

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 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 129 130 131 132 133 134 135 136 137 138 139                                                                                                                                                                                                                                                                                     
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from "@/components/ui/popover";
import { ThemeButtons } from "chainr-frontend-shared";
import { ChevronLeft, ChevronRight, Menu, Palette, X } from "lucide-react";
import React, { useEffect, useState } from "react";
import { useLocation } from "react-router-dom";
 
export interface NavbarSection {
  elements: React.ReactNode[];
  hr: boolean;
}
 
export interface NavbarProps {
  sections: NavbarSection[];
  footerElements?: React.ReactNode[];
  desktopOpen: boolean;
  onDesktopToggle: () => void;
}
 
const Navbar: React.FC<NavbarProps> = (props) => {
  const { desktopOpen, onDesktopToggle } = props;
  const [mobileOpen, setMobileOpen] = useState(false);
  const location = useLocation();
 
  useEffect(() => {
    setMobileOpen(false);
  }, [location.pathname]);
 
  const sectionItems = props.sections.map((section, si) => (
    <React.Fragment key={si}>
      {section.elements.map((item, i) => (
        <li key={i} className="list-none">
          {item}
        </li>
      ))}
      {section.hr && <hr className="my-1 border-current/20" />}
    </React.Fragment>
  ));
 
  const renderContent = () => (
    <div className="flex flex-col h-full overflow-x-hidden">
      <nav className="flex-1 overflow-y-auto overflow-x-hidden scrollbar">
        <ul className="flex flex-col gap-0.5 p-1 pr-4">{sectionItems}</ul>
      </nav>
      <div className="pt-3 border-t border-current/20 flex items-center gap-1 flex-wrap">
        {props.footerElements}
        <Popover>
          <PopoverTrigger asChild>
            <button className="p-1.5 rounded opacity-50 hover:opacity-100 transition-opacity cursor-pointer">
              <Palette className="size-4" />
            </button>
          </PopoverTrigger>
          <PopoverContent side="right" className="w-auto">
            <ThemeButtons />
          </PopoverContent>
        </Popover>
      </div>
    </div>
  );
 
  return (
    <>
      {/* Mobile: hamburger — md:hidden works now that button is in @layer base */}
      <button
        className="md:hidden fixed top-4 left-4 z-20 p-2 rounded-md cursor-pointer opacity-70 hover:opacity-100 transition-opacity"
        onClick={() => setMobileOpen(true)}
        aria-label="Open navigation"
      >
        <Menu className="size-5" />
      </button>
 
      {/* Mobile: backdrop */}
      {mobileOpen && (
        <div
          className="md:hidden fixed inset-0 z-30 bg-black/40 backdrop-blur-sm"
          onClick={() => setMobileOpen(false)}
        />
      )}
 
      {/* Desktop: sidebar */}
      <aside
        className={`
          hidden md:flex fixed top-0 left-0 bottom-0 w-64 p-5 flex-col z-10
          overflow-x-hidden
          transition-transform duration-200 ease-in-out
          ${desktopOpen ? "translate-x-0" : "-translate-x-full"}
        `}
      >
        {renderContent()}
      </aside>
 
      {/* Desktop: right-edge toggle tab */}
      <button
        className={`
          hidden md:flex fixed top-1/2 -translate-y-1/2 z-20
          h-12 w-4 p-0 items-center justify-center
          rounded-r opacity-30 hover:opacity-70
          transition-all duration-200 ease-in-out cursor-pointer
          ${desktopOpen ? "left-56" : "left-0"}
        `}
        onClick={onDesktopToggle}
        aria-label={desktopOpen ? "Collapse sidebar" : "Expand sidebar"}
      >
        {desktopOpen ? (
          <ChevronLeft className="size-3" />
        ) : (
          <ChevronRight className="size-3" />
        )}
      </button>
 
      {/* Mobile: slide-in sidebar */}
      <aside
        className={`
          md:hidden fixed top-0 left-0 bottom-0 w-56 p-5 z-40
          bg-background/95 backdrop-blur-md
          overflow-x-hidden
          transition-transform duration-200 ease-in-out
          flex flex-col
          ${mobileOpen ? "translate-x-0" : "-translate-x-full"}
        `}
      >
        <button
          className="absolute top-4 right-4 p-1 opacity-60 hover:opacity-100 transition-opacity cursor-pointer"
          onClick={() => setMobileOpen(false)}
          aria-label="Close navigation"
        >
          <X className="size-4" />
        </button>
        {renderContent()}
      </aside>
    </>
  );
};
 
export default Navbar;