2024-08-29 13:20:28 +00:00
|
|
|
import styles from '@/styles/Dropdown.module.scss';
|
|
|
|
|
|
|
|
import Link from 'next/link';
|
|
|
|
|
2024-08-29 14:06:07 +00:00
|
|
|
import { MouseEventHandler, useRef } from 'react';
|
2024-08-29 13:20:28 +00:00
|
|
|
|
2024-08-29 14:06:07 +00:00
|
|
|
export default function Dropdown({ children, items, className }: { children: React.ReactNode, items: { divider?: boolean, icon?: any, label?: string, href?: string, onClick?: MouseEventHandler<HTMLAnchorElement> }[], className: string }) {
|
2024-08-29 13:20:28 +00:00
|
|
|
const dropDownRef = useRef<HTMLDivElement>(null);
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
dropDownRef.current?.classList.toggle(styles.open);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={`${styles.dropDown} ${className}`} ref={dropDownRef}>
|
|
|
|
<label onClick={handleClick}>
|
|
|
|
{children}
|
|
|
|
</label>
|
|
|
|
|
|
|
|
<div className={styles.dropDownMenu}>
|
|
|
|
<ul>
|
|
|
|
{items.map((item, i) => (
|
|
|
|
(item.divider &&
|
|
|
|
<li key={i}>
|
|
|
|
<div className={styles.divider}></div>
|
|
|
|
</li>
|
|
|
|
) || (
|
|
|
|
<li key={i}>
|
2024-08-29 14:06:07 +00:00
|
|
|
{item.href ? (
|
|
|
|
<Link href={item.href!}>
|
|
|
|
{item.icon && <item.icon />}
|
|
|
|
|
|
|
|
{item.label}
|
|
|
|
</Link>
|
|
|
|
) : (
|
|
|
|
<a onClick={item.onClick}>
|
|
|
|
{item.icon && <item.icon />}
|
|
|
|
|
|
|
|
{item.label}
|
|
|
|
</a>
|
|
|
|
)}
|
2024-08-29 13:20:28 +00:00
|
|
|
</li>
|
|
|
|
)
|
|
|
|
))}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={styles.mobileOverlay} onClick={handleClick} />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|