DEV Community

Shahid Ullah Khan
Shahid Ullah Khan

Posted on

Disabling the Context Menu (Right Click Menu) in Your Web Application: A Simple Guide (React, Vue, Angular, Javascript)

Introduction:

In the world of web development, there may be times when you'd like to limit certain browser functionality for the end-user. One such function is the context menu, better known as the right-click or inspect element feature. While this function is critical during the development and debugging phase, you might want to disable it in your production environment to protect your source code from being easily viewed or copied.

Today, I'm thrilled to introduce a simple and efficient package that can help you achieve this – disable-context-menu.

What is 'disable-context-menu'?

disable-context-menu is a lightweight and easy-to-integrate package that effectively disables the context menu in your web applications. You can use it across various JavaScript frameworks such as React, Vue, Angular, or even vanilla JS applications.

Usage:

To start, import the package into your application's root:

import { disableContextMenu } from 'disable-context-menu';
Enter fullscreen mode Exit fullscreen mode

Next, call the disableContextMenu() function in your applications, preferably when the application is in a production environment. Here's how you can do this:

if (process.env.NODE_ENV === 'production') {
  disableContextMenu();
}
Enter fullscreen mode Exit fullscreen mode

Examples:

Let's explore how to use this library in different JavaScript frameworks:

React:

For a React application, you can add the function to the main entry of your application like this:

import React from 'react';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import { disableContextMenu } from 'disable-context-menu';
import App from './components/App';

const root = ReactDOM.createRoot(document.getElementById('react-app'));

if (process.env.NODE_ENV !== 'development') {
  disableContextMenu();
}

root.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);
Enter fullscreen mode Exit fullscreen mode

Vue:

In Vue, you can call this function in your root entrypoint file:

import { createApp } from 'vue'
import { disableContextMenu } from 'disable-context-menu';

if (process.env.NODE_ENV !== 'development') {
  disableContextMenu();
}

const app = createApp({
  el: '#app'
})
Enter fullscreen mode Exit fullscreen mode

Angular:

In an Angular application, you can add the function to the main bootstrap component:

import { Component, OnInit } from '@angular/core';
import { disableContextMenu } from 'disable-context-menu';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class App implements OnInit {
  constructor() {}

  ngOnInit() {
    disableContextMenu();
  }
}
Enter fullscreen mode Exit fullscreen mode

Conclusion:

The disable-context-menu package is a powerful yet straightforward tool to help you maintain a layer of privacy over your production application's code. By disabling the context menu, you can deter non-technical users from casually inspecting your application's source code. Remember to use this tool judiciously, as some users rely on the context menu for accessibility and other functions.

Thank you for reading! If you have any questions, feel free to leave a comment below.

Top comments (0)