Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContextMenu #17840

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/src/pages/components/menus/ContextMenu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useEffect } from 'react';
import Menu from '@material-ui/core/Menu';
import MenuItem from '@material-ui/core/MenuItem';
import Button from '@material-ui/core/Button';

const ContextMenu = () => {
const [anchorEl, setAnchorEl] = React.useState(null);
const [mouseX, setMouseX] = React.useState(null);
const [mouseY, setMouseY] = React.useState(null);

const handleClick = event => {
event.preventDefault();
setMouseY(event.clientY - 4);
setMouseX(event.clientX + 2);
setAnchorEl(event.currentTarget);
};

useEffect(() => {}, [mouseX, mouseY]);

const handleClose = () => {
setAnchorEl(null);
};

return (
<div
className="context-wrapper"
aria-controls="simple-menu"
aria-haspopup="true"
onContextMenu={handleClick}
>
<Button>Right Click To Open Menu</Button>
<Menu
id="simple-menu"
anchorEl={anchorEl}
keepMounted
open={Boolean(anchorEl)}
onClose={handleClose}
anchorReference="anchorPosition"
anchorPosition={{ top: mouseY, left: mouseX }}
anchorOrigin={{
vertical: 'top',
horizontal: 'left',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
>
<MenuItem onClick={handleClose}>Profile</MenuItem>
<MenuItem onClick={handleClose}>My account</MenuItem>
<MenuItem onClick={handleClose}>Logout</MenuItem>
</Menu>
</div>
);
};

export default ContextMenu;
6 changes: 6 additions & 0 deletions docs/src/pages/components/menus/menus.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ Use a different transition.

{{"demo": "pages/components/menus/FadeMenu.js"}}

## Context Menu

The `ContextMenu` wraps the area in which you'd like right-click functionality. Just add the tag `<ContextMenu></ContextMenu>` around the element and your custom context menu will be available anywhere within that element.

{{"demo": "pages/components/menus/ContextMenu.js"}}

## Complementary projects

For more advanced use cases you might be able to take advantage of:
Expand Down