search envelope-o feed check
Home Unanswered Active Tags New Question
user comment-o

DayPilot.Menu hide() with Escape button

Asked by Alex5
1 month ago.

Currently testing a context menu implementation with DayPilot.Menu.

All good so far. Is there a native way to hide the menu on pressing ESC?

document.addEventListener would work also but not sure if we want to attach it globally.

Answer posted by Dan Letecky [DayPilot]
1 month ago.

It is necessary to add the event handler globally (attach it to the document element). Attaching it to the menu element would only work after calling focus(), and you would also have to handle focus loss.

If you don’t want to add a permanent global handler, you can add it in onShow, and remove it in onHide:

const escHandler = (ev) => {
  if (e.key === "Escape") {
    DayPilot.Menu.hide();
  }
};

const menu = new DayPilot.Menu({
  items: [...],
  onShow: args => {
    document.addEventListener("keydown", escHandler);
  },
  onHide: args => {
    document.removeEventListener("keydown", escHandler);
  },
  // ...
});
Answer posted by Alex5
1 month ago.

Works like a charm. Thanks!

This question is more than 1 months old and has been closed. Please create a new question if you have anything to add.