mango-ui-v3/components/NavDropMenu.tsx

98 lines
3.2 KiB
TypeScript
Raw Normal View History

2022-02-17 01:44:28 -08:00
import { Fragment, useRef } from 'react'
import { Popover, Transition } from '@headlessui/react'
import { ChevronDownIcon } from '@heroicons/react/solid'
import Link from 'next/link'
2022-02-09 19:33:43 -08:00
type NavDropMenuProps = {
menuTitle: string | React.ReactNode
2022-02-23 01:51:27 -08:00
linksArray: [string, string, boolean, React.ReactNode][]
2022-02-09 19:33:43 -08:00
}
export default function NavDropMenu({
menuTitle = '',
linksArray = [],
}: NavDropMenuProps) {
2022-03-28 08:42:18 -07:00
const buttonRef = useRef<HTMLButtonElement>(null)
const toggleMenu = () => {
buttonRef?.current?.click()
}
const onHover = (open, action) => {
if (
2022-02-17 01:44:28 -08:00
(!open && action === 'onMouseEnter') ||
(open && action === 'onMouseLeave')
) {
toggleMenu()
}
}
return (
2022-02-17 01:44:28 -08:00
<Popover className="relative">
{({ open }) => (
<div
onMouseEnter={() => onHover(open, 'onMouseEnter')}
onMouseLeave={() => onHover(open, 'onMouseLeave')}
className="flex flex-col"
>
<Popover.Button
className={`-mr-3 rounded-none px-3 focus:bg-th-bkg-3 focus:outline-none ${
2022-02-17 01:44:28 -08:00
open && 'bg-th-bkg-3'
}`}
ref={buttonRef}
>
2022-02-17 01:44:28 -08:00
<div
className={`flex h-14 items-center rounded-none font-bold hover:text-th-primary`}
2022-02-09 19:33:43 -08:00
>
2022-02-17 01:44:28 -08:00
<span className="font-bold">{menuTitle}</span>
<ChevronDownIcon
className={`default-transition ml-0.5 h-5 w-5 ${
open ? 'rotate-180 transform' : 'rotate-360 transform'
2022-02-17 01:44:28 -08:00
}`}
aria-hidden="true"
/>
</div>
</Popover.Button>
<Transition
appear={true}
show={open}
as={Fragment}
enter="transition-all ease-in duration-200"
enterFrom="opacity-0 transform scale-75"
enterTo="opacity-100 transform scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<Popover.Panel className="absolute top-14 z-10">
<div className="relative rounded-b-md bg-th-bkg-3 px-4 py-2.5">
2022-02-23 01:51:27 -08:00
{linksArray.map(([name, href, isExternal, icon]) =>
!isExternal ? (
2021-07-18 07:17:52 -07:00
<Link href={href} key={href}>
2022-03-19 11:06:13 -07:00
<a className="default-transition flex items-center whitespace-nowrap py-1.5 text-th-fgd-1 hover:text-th-primary">
2022-02-23 01:51:27 -08:00
{icon ? <div className="mr-2">{icon}</div> : null}
{name}
</a>
</Link>
) : (
<a
2022-03-19 11:06:13 -07:00
className="default-transition flex items-center whitespace-nowrap py-1.5 text-th-fgd-1 hover:text-th-primary"
href={href}
2021-07-18 07:17:52 -07:00
key={href}
target="_blank"
rel="noopener noreferrer"
>
2022-02-23 01:51:27 -08:00
{icon ? <div className="mr-2">{icon}</div> : null}
{name}
</a>
)
)}
</div>
</Popover.Panel>
2022-02-17 01:44:28 -08:00
</Transition>
</div>
)}
</Popover>
)
}