2022-12-02 11:47:13 +00:00
|
|
|
import React from "react";
|
|
|
|
// next
|
|
|
|
import Link from "next/link";
|
|
|
|
// headless ui
|
|
|
|
import { Menu, Transition } from "@headlessui/react";
|
|
|
|
// icons
|
2022-12-16 16:20:09 +00:00
|
|
|
import { ChevronDownIcon, EllipsisHorizontalIcon } from "@heroicons/react/24/outline";
|
2022-12-02 11:47:13 +00:00
|
|
|
// types
|
|
|
|
import type { MenuItemProps, Props } from "./types";
|
2022-12-16 16:20:09 +00:00
|
|
|
// constants
|
|
|
|
import { classNames } from "constants/common";
|
2022-12-02 11:47:13 +00:00
|
|
|
|
2022-12-16 16:20:09 +00:00
|
|
|
const CustomMenu = ({
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
className = "",
|
|
|
|
ellipsis = false,
|
|
|
|
width,
|
|
|
|
textAlignment,
|
|
|
|
}: Props) => {
|
2022-12-02 11:47:13 +00:00
|
|
|
return (
|
2022-12-16 16:20:09 +00:00
|
|
|
<Menu as="div" className={`relative text-left ${className}`}>
|
2022-12-02 11:47:13 +00:00
|
|
|
<div>
|
2022-12-16 16:20:09 +00:00
|
|
|
{ellipsis ? (
|
|
|
|
<Menu.Button className="grid relative place-items-center hover:bg-gray-100 rounded p-1 focus:outline-none">
|
|
|
|
<EllipsisHorizontalIcon className="h-4 w-4" />
|
|
|
|
</Menu.Button>
|
|
|
|
) : (
|
|
|
|
<Menu.Button
|
|
|
|
className={`flex justify-between items-center gap-1 hover:bg-gray-100 border rounded-md shadow-sm px-2 w-full py-1 cursor-pointer focus:outline-none focus:ring-1 focus:ring-indigo-500 focus:border-indigo-500 text-xs duration-300 ${
|
|
|
|
textAlignment === "right"
|
|
|
|
? "text-right"
|
|
|
|
: textAlignment === "center"
|
|
|
|
? "text-center"
|
|
|
|
: "text-left"
|
|
|
|
}`}
|
|
|
|
>
|
|
|
|
{label}
|
|
|
|
<ChevronDownIcon className="h-3 w-3" aria-hidden="true" />
|
|
|
|
</Menu.Button>
|
|
|
|
)}
|
2022-12-02 11:47:13 +00:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<Transition
|
|
|
|
as={React.Fragment}
|
|
|
|
enter="transition ease-out duration-100"
|
|
|
|
enterFrom="transform opacity-0 scale-95"
|
|
|
|
enterTo="transform opacity-100 scale-100"
|
|
|
|
leave="transition ease-in duration-75"
|
|
|
|
leaveFrom="transform opacity-100 scale-100"
|
|
|
|
leaveTo="transform opacity-0 scale-95"
|
|
|
|
>
|
2022-12-16 16:20:09 +00:00
|
|
|
<Menu.Items
|
|
|
|
className={`absolute right-0 z-10 mt-2 origin-top-right rounded-md bg-white text-xs shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none ${
|
|
|
|
width === "auto" ? "min-w-full whitespace-nowrap" : "w-56"
|
|
|
|
}`}
|
|
|
|
>
|
2022-12-02 11:47:13 +00:00
|
|
|
<div className="py-1">{children}</div>
|
|
|
|
</Menu.Items>
|
|
|
|
</Transition>
|
|
|
|
</Menu>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const MenuItem: React.FC<MenuItemProps> = ({ children, renderAs, href, onClick }) => {
|
|
|
|
return (
|
|
|
|
<Menu.Item>
|
2022-12-16 16:20:09 +00:00
|
|
|
{({ active, close }) =>
|
2022-12-02 11:47:13 +00:00
|
|
|
renderAs === "a" ? (
|
|
|
|
<Link href={href ?? ""}>
|
|
|
|
<a
|
2022-12-16 16:20:09 +00:00
|
|
|
className="block p-2 text-gray-700 hover:bg-indigo-50 hover:text-gray-900"
|
|
|
|
onClick={close}
|
2022-12-02 11:47:13 +00:00
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
onClick={onClick}
|
|
|
|
className={classNames(
|
2022-12-16 16:20:09 +00:00
|
|
|
active ? "bg-indigo-50 text-gray-900" : "text-gray-700",
|
|
|
|
"block w-full p-2 text-left"
|
2022-12-02 11:47:13 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</button>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
</Menu.Item>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
CustomMenu.MenuItem = MenuItem;
|
|
|
|
|
|
|
|
export default CustomMenu;
|