import React, { MouseEventHandler, ReactNode } from "react"; import { Menu, Transition } from "@headlessui/react"; import clsx from "clsx"; import { usePopper } from "./use-popper"; import Link from "next/link"; interface Props { button: ReactNode; options: { title?: string; routePath?: string; onClick?: () => void | undefined; disabled?: boolean; className?: string; render?: (active: boolean, close: () => void) => ReactNode; }[]; size?: "sm" | "md" | "lg" | "xl"; onClick?: MouseEventHandler; className?: string; } export default function MenuWithPopper({ button, options, size = "sm", onClick, className }: Props) { let [trigger, container] = usePopper({ strategy: "fixed", }); return ( {button}
{options.map((item, idx) => { return ( {({ active, close }) => ( <> {item.render && item.render(active, close)} {item.onClick && ( )} {item.routePath && ( { e.stopPropagation(); close(); }} className={clsx( item.className, "w-full text-left hover:bg-gray-100", item.disabled ? "cursor-not-allowed opacity-50" : "cursor-pointer", active ? "bg-gray-100 text-gray-900" : "text-gray-700", "block px-4 py-2 text-sm" )} > {item.title} )} )} ); })}
); }