File size: 2,455 Bytes
1067b6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import { useState } from "react";
import { Menu } from "lucide-react";
import { routes } from "@/lib/routes";
import Link from "next/link";
import {
  Sheet,
  SheetContent,
  SheetHeader,
  SheetTitle,
  SheetTrigger,
} from "@/components/ui/sheet";
import { ProductSearch } from "./storefront/product-search";
import { ProductImage } from "./product-image";
import { images } from "@/lib/assets";
import { Button } from "./ui/button";
import { useRouter } from "next/navigation";

export const MobileNavigation = () => {
  const [isMobileNavOpen, setIsMobileNavOpen] = useState(false);
  return (
    <>
      <Sheet
        open={isMobileNavOpen}
        onOpenChange={() => setIsMobileNavOpen((prev) => !prev)}
      >
        <SheetTrigger>
          <div className="p-2 rounded-md border border-border">
            <Menu />
          </div>
        </SheetTrigger>
        <SheetContent
          size="full"
          className="overflow-auto lg:max-w-[600px] sm:max-w-[400px] xl:max-w-[650px]"
        >
          <SheetHeader>
            <SheetTitle>Menu</SheetTitle>
          </SheetHeader>
          <div className="my-6">
            <ProductSearch />
          </div>
          <div className="flex items-start justify-center gap-2 flex-col">
            <NavBarLink
              image={images[0]}
              href={routes.products}
              name="Products"
              setIsMobileNavOpen={setIsMobileNavOpen}
            />
            <NavBarLink
              image={images[1]}
              href={routes.account}
              name="Account"
              setIsMobileNavOpen={setIsMobileNavOpen}
            />
          </div>
        </SheetContent>
      </Sheet>
    </>
  );
};

const NavBarLink = (props: {
  href: string;
  name: string;
  image: string;
  setIsMobileNavOpen: React.Dispatch<React.SetStateAction<boolean>>;
}) => {
  const router = useRouter();
  return (
    <div className="flex gap-2 items-center justify-between w-full rounded-md border border-border">
      <ProductImage
        alt="products"
        src={props.image}
        sizes="50px"
        height="h-14"
        width="w-14"
      />
      <div className="w-full text-left">
        <Button
          variant="link"
          onClick={() => {
            router.push(props.href);
            props.setIsMobileNavOpen(false);
          }}
        >
          {props.name}
        </Button>
      </div>
    </div>
  );
};