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

DayPilot.Menu hide() with Escape button

Asked by Alex5
21 days 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]
19 days 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
17 days ago.

Works like a charm. Thanks!

New Reply
This reply is
Attachments:
or drop files here
Your name (optional):