el.xwx.moe/components/Sidebar/SidebarItem.tsx

41 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-04-23 08:26:39 -05:00
// Copyright (C) 2022-present Daniel31x13 <daniel31x13@gmail.com>
// This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3.
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-03-05 15:03:20 -06:00
import Link from "next/link";
2023-05-01 05:07:01 -05:00
import React, { ReactElement, useEffect, useState } from "react";
import { useRouter } from "next/router";
interface SidebarItemProps {
2023-03-05 15:03:20 -06:00
text: string;
2023-03-22 22:06:15 -05:00
icon: ReactElement;
2023-03-05 15:03:20 -06:00
path: string;
2023-05-25 09:17:20 -05:00
className?: string;
}
2023-05-25 09:17:20 -05:00
export default function ({ text, icon, path, className }: SidebarItemProps) {
2023-05-01 05:07:01 -05:00
const router = useRouter();
const [active, setActive] = useState(false);
useEffect(() => {
if (router.asPath === path) setActive(true);
else setActive(false);
}, [router]);
return (
2023-03-05 15:03:20 -06:00
<Link href={path}>
2023-05-01 05:07:01 -05:00
<div
className={`${
active ? "bg-sky-500" : "hover:bg-gray-50 hover:outline bg-gray-100"
2023-05-25 09:17:20 -05:00
} outline-sky-100 outline-1 duration-100 rounded-md my-1 p-2 cursor-pointer flex items-center gap-2 ${className}`}
2023-05-01 05:07:01 -05:00
>
2023-03-22 22:06:15 -05:00
{React.cloneElement(icon, {
2023-05-01 05:07:01 -05:00
className: `w-4 ${active ? "text-white" : "text-sky-300"}`,
2023-03-22 22:06:15 -05:00
})}
2023-05-01 05:07:01 -05:00
<p className={`${active ? "text-white" : "text-sky-900"}`}>{text}</p>
2023-03-05 15:03:20 -06:00
</div>
</Link>
);
}