How to hide and show option in Atom Context Menu


#1

Hello I am wondering what is the best method for showing and hide one single option in the context menu.

I see there is a shouldDisplay(event) https://atom.io/docs/api/latest/ContextMenuManager#instance-add
(Optional) A Function that is called to determine whether to display this item on a given context menu deployment.

I am unsure how to approach this problem. Any examples of how to use the shouldDisplay(event)???


#2

The shouldDisplay property should be a function that returns true or false based on some calculation.

For example if you want to only show an item in the context menu in the morning you could write

atom.contextMenu.add({
  "atom-text-editor": [
    {
      label: "Good Morning­čî×",
      shouldDisplay: function(event) {
        return (new Date().getHours() < 12);
      }
    }
  ]
});

The event parameter will have more information about the right click event like the x,y coordinate and whether they were holding down any special keys at the time that they clicked.

MouseEvent