Skip to content

Commit 664ad4f

Browse files
committed
Dropdown menu is working
1 parent 644e917 commit 664ad4f

File tree

4 files changed

+62
-32
lines changed

4 files changed

+62
-32
lines changed

templerobotics.github.io/app/globals.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ body {
2323
color: var(--foreground);
2424
background: var(--background);
2525
font-family: Arial, Helvetica, sans-serif;
26-
27-
& * {
28-
height: 100%;
29-
}
3026
}
3127

3228
.flex-vertical {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1+
.menuItem {
2+
height: 25%;
3+
color: black;
4+
text-align: center;
5+
text-decoration: none;
6+
display: block;
7+
border-bottom: 1px solid black;
8+
}
19

10+
.title:hover {
11+
cursor: pointer;
12+
}
13+
14+
.popOverRoot {
15+
pointer-events: none;
16+
}
Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,65 @@
11
'use client'
2+
import styles from './DropdownMenu.module.css'
23

34
import { Link, Menu, MenuItem } from '@mui/material'
45

56
import React, { useState } from 'react'
6-
// import { PATHS } from '@utils/Constants'
77

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;
1111

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+
}
1416
};
1517

16-
const handleMouseOut = () => {
17-
setAnchorEl(null)
18+
const handleClose = () => {
19+
setAnchorEl(null);
1820
};
1921

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+
2035
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}
2540
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+
3047
<Menu
3148
id="simple-menu"
3249
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' }
3856
}}
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>)}
4362
</Menu>
44-
</>
63+
</div>
4564
)
4665
}

templerobotics.github.io/components/navigation/NavBar.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import React from 'react'
66
import { Link } from '@mui/material'
77
import Image from 'next/image'
88
import { PATHS } from '@utils/Constants'
9-
import DropDownMenu from './DropDownMenu'
9+
import DropDownMenu from './DropdownMenu'
1010

1111
export default function NavBar (): React.ReactElement {
1212
return (
@@ -20,8 +20,8 @@ export default function NavBar (): React.ReactElement {
2020

2121
<div className={'flex-horizontal'}>
2222
<Link href={PATHS.HOME}>Home</Link>
23+
<DropDownMenu title={'Robotics'} items={['Option 1', 'Option 2']}/>
2324
<Link href={PATHS.ROBOTICS}>Robotics</Link>
24-
<DropDownMenu/>
2525
</div>
2626
</div>
2727
)

0 commit comments

Comments
 (0)