|
1 | 1 | 'use client' |
| 2 | +import styles from './DropdownMenu.module.css' |
2 | 3 |
|
3 | 4 | import { Link, Menu, MenuItem } from '@mui/material' |
4 | 5 |
|
5 | 6 | import React, { useState } from 'react' |
6 | | -// import { PATHS } from '@utils/Constants' |
7 | 7 |
|
8 | | -export default function DropDownMenu (): React.ReactElement { |
9 | | - const [anchorEl, setAnchorEl] = useState<Element | null>(null) |
10 | | - const open = Boolean(anchorEl); |
| 8 | +export default function DropdownMenu ({ title, items }: { title: string, items: string[] }): React.ReactElement { |
| 9 | + const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null); |
| 10 | + let currentlyHovering = false; |
11 | 11 |
|
12 | | - const handleMouseOver = (event: React.MouseEvent) => { |
13 | | - setAnchorEl(event.currentTarget) |
| 12 | + const handleClick = (event: React.MouseEvent<HTMLElement>) => { |
| 13 | + if (anchorEl !== event.currentTarget) { |
| 14 | + setAnchorEl(event.currentTarget); |
| 15 | + } |
14 | 16 | }; |
15 | 17 |
|
16 | | - const handleMouseOut = () => { |
17 | | - setAnchorEl(null) |
| 18 | + const handleClose = () => { |
| 19 | + setAnchorEl(null); |
18 | 20 | }; |
19 | 21 |
|
| 22 | + const handleHover = () => { |
| 23 | + currentlyHovering = true; |
| 24 | + } |
| 25 | + |
| 26 | + const handleCloseHover = () => { |
| 27 | + currentlyHovering = false; |
| 28 | + setTimeout(() => { |
| 29 | + if (!currentlyHovering) { |
| 30 | + handleClose(); |
| 31 | + } |
| 32 | + }, 50); |
| 33 | + } |
| 34 | + |
20 | 35 | return ( |
21 | | - <> |
22 | | - <div |
23 | | - onMouseEnter={handleMouseOver} |
24 | | - aria-controls={open ? 'basic-menu' : undefined} |
| 36 | + <div> |
| 37 | + <Link |
| 38 | + className='flex-horizontal cursor-pointer' |
| 39 | + aria-owns={anchorEl ? "simple-menu" : undefined} |
25 | 40 | aria-haspopup="true" |
26 | | - aria-expanded={open ? 'true' : undefined} |
27 | | - className={'flex-horizontal'}> |
28 | | - <Link>Robotics</Link> |
29 | | - </div> |
| 41 | + onClick={handleClick} |
| 42 | + onMouseOver={handleClick} |
| 43 | + onMouseLeave={handleCloseHover}> |
| 44 | + {title} |
| 45 | + </Link> |
| 46 | + |
30 | 47 | <Menu |
31 | 48 | id="simple-menu" |
32 | 49 | anchorEl={anchorEl} |
33 | | - open={open} |
34 | | - onClose={handleMouseOut} |
35 | | - transformOrigin={{ |
36 | | - vertical: 'bottom', |
37 | | - horizontal: 'right', |
| 50 | + open={Boolean(anchorEl)} |
| 51 | + onClose={handleClose} |
| 52 | + MenuListProps={{ |
| 53 | + onMouseLeave: handleCloseHover, |
| 54 | + onMouseEnter: handleHover, |
| 55 | + style: { pointerEvents: 'auto' } |
38 | 56 | }} |
39 | | - MenuListProps={{ 'aria-labelledby': 'basic-button' }}> |
40 | | - <MenuItem onClick={handleMouseOut}>Profile</MenuItem> |
41 | | - <MenuItem onClick={handleMouseOut}>My account</MenuItem> |
42 | | - <MenuItem onClick={handleMouseOut}>Logout</MenuItem> |
| 57 | + hideBackdrop |
| 58 | + PopoverClasses={{ |
| 59 | + root: styles.popOverRoot |
| 60 | + }}> |
| 61 | + {items.map((item, i) => <MenuItem key={i} className={styles.menuItem} onClick={handleClose}>{item}</MenuItem>)} |
43 | 62 | </Menu> |
44 | | - </> |
| 63 | + </div> |
45 | 64 | ) |
46 | 65 | } |
0 commit comments