Skip to content

Instantly share code, notes, and snippets.

@jsmanifest
Created April 2, 2020 03:18
Show Gist options
  • Save jsmanifest/f214c4579b165758307867f398d57295 to your computer and use it in GitHub Desktop.
Save jsmanifest/f214c4579b165758307867f398d57295 to your computer and use it in GitHub Desktop.
import React from 'react'
import Button from '@material-ui/core/Button'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import './styles.css'
const items = [
{ to: '/home', label: 'Home' },
{ to: '/blog', label: 'Blog' },
{ to: '/about', label: 'About' },
{ to: '/contact', label: 'Contact' },
{
to: '/help-center',
label: 'Help Center',
items: [
{ to: '/privacy-policy', label: 'Privacy Policy' },
{ to: '/tos', label: 'Terms of Service' },
{ to: '/partners', label: 'Partners' },
{
to: '/faq',
label: 'FAQ',
items: [
{ to: '/faq/newsletter', label: 'Newsletter FAQs' },
{ to: '/faq/career', label: 'Employment/Career FAQs' },
],
},
],
},
]
const MyMenu = React.forwardRef(
({ items, anchorEl: anchorElProp, createOnClick, onClose }, ref) => {
const [anchorEl, setAnchorEl] = React.useState(null)
return (
<Menu
ref={ref}
open={Boolean(anchorElProp)}
onClose={onClose}
anchorEl={anchorElProp}
anchorOrigin={{ vertical: 'top', horizontal: 'right' }}
transformOrigin={{ vertical: 'top', horizontal: 'right' }}
>
{items.map((item) => (
<div key={item.to}>
<MenuItem onMouseEnter={item.items && createOnClick(setAnchorEl)}>
{item.label}
</MenuItem>
{item.items && (
<MyMenu
key={item.to}
items={item.items}
anchorEl={anchorEl}
createOnClick={createOnClick}
onClose={() => setAnchorEl(null)}
/>
)}
</div>
))}
</Menu>
)
},
)
function App() {
const [anchorEl, setAnchorEl] = React.useState(null)
const createOnClick = (callback) => {
return (e) => {
e.persist()
return callback(e.currentTarget)
}
}
return (
<div>
<Button onMouseEnter={createOnClick(setAnchorEl)} variant="outlined">
View More
</Button>
<MyMenu
items={items}
anchorEl={anchorEl}
createOnClick={createOnClick}
onClose={() => setAnchorEl(null)}
/>
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment