content logo

React Menus:

HTML Menu on Right Click using React

To save the physical space of your website, you are able to use different kinds of menus. Using menus can help you organize your website's contents beautifully and classify them into different categories and subcategories. This code is the HTML Menu on Right Click using React with a different design. This Right Click Menu Code is simple, and as a result, it fits with most kids of websites quickly. When you choose this HTML Right Click Menu for your website, you can categorize all parts of your website into several sections, and it's changed the design of your website. Plus, users can access different sorts of information easily.

To have a better understanding of this React Click Menu, you can look at its preview down below. In this preview, you can see the blue and green backgrounds of this code. The phrase “Right click anywhere on the screen!” is written in the middle of the page. In this white small page on this React Simple Menu Example, you will face different menus and categories. These items are located on a small white page that you can move to by clicking on each one.

#

Right Click Menu Code

#

HTML Right Click Menu

#

React Click Menu

#

React Simple Menu Example

<!-- This script got from frontendfreecode.com -->
<div id="root"></div><a style="font-size: 8pt; text-decoration: none" target="_blank" href="http://frontendfreecode.com">Free Frontend</a>
                                                                            
#root {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	background: linear-gradient(75deg, #00E39F, #00C4E1);
}
#root::before {
	content: "Right click anywhere on the screen!";
	font-family: sans-serif;
	font-size: 12px;
	color: white;
}
.contextMenu {
	position: fixed;
	background: white;
	box-shadow: 0px 2px 10px #999999;
}
.contextMenu--option {
	padding: 6px 50px 5px 10px;
	min-width: 160px;
	cursor: default;
	font-size: 12px;
}
.contextMenu--option:hover {
	background: linear-gradient(to top, #555, #333);
	color: white;
}
.contextMenu--option:active {
	color: #e9e9e9;
	background: linear-gradient(to top, #555, #444);
}
.contextMenu--option__disabled {
	color: #999999;
	pointer-events: none;
}
.contextMenu--separator {
	width: 100%;
	height: 1px;
	background: #CCCCCC;
	margin: 0 0 0 0;
}
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class ContextMenu extends React.Component {
    constructor(...args) {
        super(...args); _defineProperty(this, "state",
            {
                visible: false
            }); _defineProperty(this, "_handleContextMenu",
                event => {
                    event.preventDefault();
                    this.setState({ visible: true });
                    const clickX = event.clientX;
                    const clickY = event.clientY;
                    const screenW = window.innerWidth;
                    const screenH = window.innerHeight;
                    const rootW = this.root.offsetWidth;
                    const rootH = this.root.offsetHeight;
                    const right = screenW - clickX > rootW;
                    const left = !right;
                    const top = screenH - clickY > rootH;
                    const bottom = !top;
                    if (right) {
                        this.root.style.left = `${clickX + 5}px`;
                    }
                    if (left) {
                        this.root.style.left = `${clickX - rootW - 5}px`;
                    }
                    if (top) {
                        this.root.style.top = `${clickY + 5}px`;
                    }
                    if (bottom) {
                        this.root.style.top = `${clickY - rootH - 5}px`;
                    }
                }); _defineProperty(this, "_handleClick",
                    event => {
                        const { visible } = this.state;
                        const wasOutside = !(event.target.contains === this.root);
                        if (wasOutside && visible) this.setState({ visible: false });
                    }); _defineProperty(this, "_handleScroll",
                        () => {
                            const { visible } = this.state;
                            if (visible) this.setState({ visible: false });
                        });
    } componentDidMount() { document.addEventListener('contextmenu', this._handleContextMenu); document.addEventListener('click', this._handleClick); document.addEventListener('scroll', this._handleScroll); } componentWillUnmount() { document.removeEventListener('contextmenu', this._handleContextMenu); document.removeEventListener('click', this._handleClick); document.removeEventListener('scroll', this._handleScroll); }
    render() {
        const { visible } = this.state;
        return (visible || null) && /*#__PURE__*/
            React.createElement("div", { ref: ref => { this.root = ref; }, className: "contextMenu" }, /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Share this"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "New window"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Visit official site"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option contextMenu--option__disabled" }, "View full version"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Settings"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--separator" }), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "About this app"));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ContextMenu, null), document.getElementById('root'));
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- This script got from frontendfreecode.com -->

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js'></script>
<style>
#root {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100vh;
	background: linear-gradient(75deg, #00E39F, #00C4E1);
}
#root::before {
	content: "Right click anywhere on the screen!";
	font-family: sans-serif;
	font-size: 12px;
	color: white;
}
.contextMenu {
	position: fixed;
	background: white;
	box-shadow: 0px 2px 10px #999999;
}
.contextMenu--option {
	padding: 6px 50px 5px 10px;
	min-width: 160px;
	cursor: default;
	font-size: 12px;
}
.contextMenu--option:hover {
	background: linear-gradient(to top, #555, #333);
	color: white;
}
.contextMenu--option:active {
	color: #e9e9e9;
	background: linear-gradient(to top, #555, #444);
}
.contextMenu--option__disabled {
	color: #999999;
	pointer-events: none;
}
.contextMenu--separator {
	width: 100%;
	height: 1px;
	background: #CCCCCC;
	margin: 0 0 0 0;
}
</style>

</head>
<body>
<div id="root"></div><div id="bcl"><a style="font-size:8pt;text-decoration:none;" href="http://www.devanswer.com">Free Frontend</a></div>
<script>
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class ContextMenu extends React.Component {
    constructor(...args) {
        super(...args); _defineProperty(this, "state",
            {
                visible: false
            }); _defineProperty(this, "_handleContextMenu",
                event => {
                    event.preventDefault();
                    this.setState({ visible: true });
                    const clickX = event.clientX;
                    const clickY = event.clientY;
                    const screenW = window.innerWidth;
                    const screenH = window.innerHeight;
                    const rootW = this.root.offsetWidth;
                    const rootH = this.root.offsetHeight;
                    const right = screenW - clickX > rootW;
                    const left = !right;
                    const top = screenH - clickY > rootH;
                    const bottom = !top;
                    if (right) {
                        this.root.style.left = `${clickX + 5}px`;
                    }
                    if (left) {
                        this.root.style.left = `${clickX - rootW - 5}px`;
                    }
                    if (top) {
                        this.root.style.top = `${clickY + 5}px`;
                    }
                    if (bottom) {
                        this.root.style.top = `${clickY - rootH - 5}px`;
                    }
                }); _defineProperty(this, "_handleClick",
                    event => {
                        const { visible } = this.state;
                        const wasOutside = !(event.target.contains === this.root);
                        if (wasOutside && visible) this.setState({ visible: false });
                    }); _defineProperty(this, "_handleScroll",
                        () => {
                            const { visible } = this.state;
                            if (visible) this.setState({ visible: false });
                        });
    } componentDidMount() { document.addEventListener('contextmenu', this._handleContextMenu); document.addEventListener('click', this._handleClick); document.addEventListener('scroll', this._handleScroll); } componentWillUnmount() { document.removeEventListener('contextmenu', this._handleContextMenu); document.removeEventListener('click', this._handleClick); document.removeEventListener('scroll', this._handleScroll); }
    render() {
        const { visible } = this.state;
        return (visible || null) && /*#__PURE__*/
            React.createElement("div", { ref: ref => { this.root = ref; }, className: "contextMenu" }, /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Share this"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "New window"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Visit official site"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option contextMenu--option__disabled" }, "View full version"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "Settings"), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--separator" }), /*#__PURE__*/
                React.createElement("div", { className: "contextMenu--option" }, "About this app"));
    }
}
ReactDOM.render( /*#__PURE__*/React.createElement(ContextMenu, null), document.getElementById('root'));
</script>

</body>
</html>
Preview