|
1 | | -import { FiSearch } from "react-icons/fi"; |
2 | | -import { IoAddCircleOutline } from "react-icons/io5"; |
3 | | -import { FiTrendingUp } from "react-icons/fi"; |
| 1 | +import { Link, useNavigate } from "react-router-dom"; |
| 2 | +import { layoutData } from "../../data/layout"; |
| 3 | +import { FaRegUser } from "react-icons/fa6"; |
| 4 | +import { useAuth } from "../../hooks/AuthContext"; |
| 5 | +import { useRef, useState } from "react"; |
| 6 | +import { toast } from "react-toastify"; |
| 7 | +import { BiHome } from "react-icons/bi"; |
4 | 8 |
|
5 | 9 | const MenuMobile = () => { |
| 10 | + //state |
| 11 | + const [showUserMenu, setShowUserMenu] = useState(false); |
| 12 | + |
| 13 | + //hooks |
| 14 | + const navigate = useNavigate(); |
| 15 | + const { user } = useAuth(); |
| 16 | + const menuRef = useRef<HTMLDivElement | null>(null); |
| 17 | + |
| 18 | + //handler |
| 19 | + const handleCloseMenu = () => { |
| 20 | + setShowUserMenu(false); |
| 21 | + }; |
| 22 | + |
| 23 | + const handleUserIconClick = () => { |
| 24 | + if (user) { |
| 25 | + navigate("/"); |
| 26 | + } else { |
| 27 | + setShowUserMenu((prev) => !prev); |
| 28 | + } |
| 29 | + }; |
| 30 | + |
| 31 | + const handleCreateClick = () => { |
| 32 | + if (!user) { |
| 33 | + toast.error("You must create an account to create a blog!"); |
| 34 | + } else { |
| 35 | + navigate("/create"); |
| 36 | + } |
| 37 | + }; |
| 38 | + |
6 | 39 | return ( |
7 | | - <div className="border border-[#6EEB83] flex md:hidden items-center p-[1em]"> |
8 | | - <div className="flex items-center justify-between w-full"> |
9 | | - <ul className="flex flex-col items-center justify-center"> |
10 | | - <li className="w-9 h-9 md:w-16 md:h-16 flex items-center justify-center text-[#212121] text-2xl rounded-full bg-[#6EEB83]"> |
11 | | - S |
12 | | - </li> |
13 | | - </ul> |
14 | | - <ul className="flex flex-col items-center justify-center"> |
15 | | - <li className="text-[#6EEB83] text-4xl cursor-pointer"> |
16 | | - <FiSearch /> |
17 | | - </li> |
18 | | - </ul> |
19 | | - <ul className="flex flex-col items-center justify-center"> |
20 | | - <li className="text-[#6EEB83] text-4xl cursor-pointer"> |
21 | | - <FiTrendingUp /> |
22 | | - </li> |
23 | | - </ul> |
24 | | - <ul className="flex flex-col items-center justify-center"> |
25 | | - <li className="text-[#6EEB83] text-4xl cursor-pointer"> |
26 | | - <IoAddCircleOutline /> |
27 | | - </li> |
28 | | - </ul> |
| 40 | + <div className="fixed bottom-0 w-full left-0 border border-[#6EEB83] flex md:hidden items-center"> |
| 41 | + <div className="w-full flex items-center justify-between p-3 z-20 bc-black"> |
| 42 | + <div className="flex items-center justify-between h-full w-full"> |
| 43 | + <Link to={layoutData.sidebar.link}> |
| 44 | + <h1 className="text-2xl font-semibold c-green"> |
| 45 | + <BiHome /> |
| 46 | + </h1> |
| 47 | + </Link> |
| 48 | + {layoutData.sidebar.navbar.map((item, index) => ( |
| 49 | + <ul |
| 50 | + key={index} |
| 51 | + className="flex flex-col items-center justify-center" |
| 52 | + > |
| 53 | + {item.title === "Create" ? ( |
| 54 | + <div |
| 55 | + onClick={handleCreateClick} |
| 56 | + className="flex flex-col items-center" |
| 57 | + > |
| 58 | + <li className="c-green text-2xl cursor-pointer"> |
| 59 | + {item.icon} |
| 60 | + </li> |
| 61 | + </div> |
| 62 | + ) : ( |
| 63 | + <Link to={item.link} className="flex flex-col items-center"> |
| 64 | + <li className="c-green text-2xl cursor-pointer"> |
| 65 | + {item.icon} |
| 66 | + </li> |
| 67 | + </Link> |
| 68 | + )} |
| 69 | + </ul> |
| 70 | + ))} |
| 71 | + |
| 72 | + <ul className="flex flex-col items-center justify-center"> |
| 73 | + <li |
| 74 | + onClick={handleUserIconClick} |
| 75 | + className="w-8 h-8 flex items-center justify-center c-black text-base rounded-full bc-green cursor-pointer relative" |
| 76 | + > |
| 77 | + <FaRegUser /> |
| 78 | + </li> |
| 79 | + </ul> |
| 80 | + </div> |
29 | 81 | </div> |
| 82 | + |
| 83 | + {!user && ( |
| 84 | + <div |
| 85 | + ref={menuRef} |
| 86 | + className={`absolute bottom-0 -right-3 rounded-e-md p-3 |
| 87 | + transform transition-transform duration-500 ease-in-out -z-10 ${ |
| 88 | + showUserMenu |
| 89 | + ? "-translate-y-[3.2em] opacity-100" |
| 90 | + : "translate-y-52 opacity-100" |
| 91 | + }`} |
| 92 | + > |
| 93 | + {layoutData.sidebar.auth.map((item, index) => ( |
| 94 | + <Link |
| 95 | + key={index} |
| 96 | + to={item.link} |
| 97 | + onClick={handleCloseMenu} |
| 98 | + className={`block w-full py-2 px-4 text-center mb-2 rounded-md ${ |
| 99 | + item.title === "Register" |
| 100 | + ? "c-black bc-green" |
| 101 | + : "c-green border border-green bc-black" |
| 102 | + }`} |
| 103 | + > |
| 104 | + {item.title} |
| 105 | + </Link> |
| 106 | + ))} |
| 107 | + </div> |
| 108 | + )} |
30 | 109 | </div> |
31 | 110 | ); |
32 | 111 | }; |
|
0 commit comments