DEV Community

Cover image for Make a custom Context Menu using contextMenu.js library
Mohamed Boussaid
Mohamed Boussaid

Posted on • Updated on

Make a custom Context Menu using contextMenu.js library

Sometimes we want to provide a custom contextmenu on our websites and in this post we are going to see how we can do this using a lightweight library that allows you to do this

in our post we are going to add a custom context menu to a textarea field that has two option Copy All and Delete All

Include the library



<head>
<script src="https://unpkg.com/@mboussaid/contextmenu.js@1.0.0" ></script>

</head>


Enter fullscreen mode Exit fullscreen mode

our html


<html>

<head>
<script src="https://unpkg.com/@mboussaid/contextmenu.js@1.0.0" ></script>

</head>
<body>

<textarea style="width:400px;height:400px"  id="myTextArea">Hello World</textarea>
</body>

</html>


Enter fullscreen mode Exit fullscreen mode

Syntax


contextMenu({
//// ARRAY OF ITEMS YOU NEED ON CONTEXT MENU
items:[
{
template: ///  HTML TEMPALTE HERE 
onClick : /// THE FUNCTION THAT WILL BE EXECUTED IF USER CLICKED
}
]
}).init(/* HERE WHERE YOU WANT TO ADD CONTECT MENU TO IT */)


Enter fullscreen mode Exit fullscreen mode

Javacript Code


contextMenu({

items:[
{
template: "<span>Copy All</spa>"
onClick : function(){

  /* Get the text field */
  var copyText = document.getElementById("myTextArea");

  /* Select the text field */
  copyText.select();

  /* Copy the text inside the text field */
  navigator.clipboard.writeText(copyText.value);

  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);
}
},
{
template: "<span>Delete All</spa>"
onClick : function(){
document.getElementById("myTextArea").value = ''
}
}
]
}).init(document.getElementById("myTextArea"))


Enter fullscreen mode Exit fullscreen mode

before we need to know that the html of our context menu will be like this :


<div class="contextMenu">
<ul class="contextMenuList">
<li class="contextMenuItem"><span>Copy All</span></li>
<li class="contextMenuItem"><span>Delete All</span></li>
</ul>
</div>
Enter fullscreen mode Exit fullscreen mode

Style

.contextMenu{

    background-color: #7d5fff;
    font-family: 'Open Sans';

}

.contextMenuItem{
    transition: all 0.4s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: center;
    padding: 10px;
    color:#fff;

}

.contextMenuItem:hover{
    background-color: #5f27cd;
    color:#fff;
}




Enter fullscreen mode Exit fullscreen mode

Final Code


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <script src="https://unpkg.com/@mboussaid/contextmenu.js@1.0.0" ></script>



<style>


.contextMenu{

    background-color: #7d5fff;
    font-family: 'Open Sans';

}

.contextMenuItem{
    transition: all 0.4s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    align-content: center;
    padding: 10px;
    color:#fff;

}

.contextMenuItem:hover{
    background-color: #5f27cd;
    color:#fff;
}


</style>
</head>
<body>


<textarea style="width:400px;height:400px"  id="myTextArea">Hello World</textarea>


    <script>

contextMenu({

items:[
{
template: "<span>Copy All</spa>",
onClick : function(){

  /* Get the text field */
  var copyText = document.getElementById("myTextArea");

  /* Select the text field */
  copyText.select();

  /* Copy the text inside the text field */
  navigator.clipboard.writeText(copyText.value);

  /* Alert the copied text */
  alert("Copied the text: " + copyText.value);
}
},
{
template: "<span>Delete All</spa>",
onClick : function(){
document.getElementById("myTextArea").value = ''
}
}
]
}).init(document.getElementById("myTextArea"))


    </script>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

Result

Demo

Demo
Github Repository

Discussion (0)