notiflix / Notiflix

Notiflix is a pure JavaScript library for client-side non-blocking notifications, popup boxes, loading indicators, and more that makes your web projects much better.
https://notiflix.github.io
MIT License
647 stars 55 forks source link

[FEAT] - add option to enable/disable key prompts | Confirm dialogbox #59

Closed prashil40 closed 2 years ago

prashil40 commented 2 years ago

Is your feature request related to a problem? Please describe.

Yes, whenever I try to use Notiflix.Confirm.Show for delete confirmation, I have to select OK / Cancel button with mouse

Describe the solution you'd like

There can be an option to enable/disable keys like "Enter" (for Ok) and "Esc" (for Cancel)

furcan commented 2 years ago

Hi @prashil40,

Notiflix is not focusing listen to the keyboard events. So, you can please create your custom solution.

An example/simple solution can be as below.

// Listener
var documentKeyupEventListener = function (event) {
  // Enter
  if (event.keyCode === 13) {
    window.document.getElementById('NXConfirmButtonOk').click();
  }

  // ESC
  if (event.keyCode === 27) {
    window.document.getElementById('NXConfirmButtonCancel').click();
  }
};

// Adding Listener
window.document.addEventListener('keyup', documentKeyupEventListener);

// Notiflix
Notiflix.Confirm.show(
  'Notiflix Confirm',
  'Do you agree with me?',
  'Yes',
  'No',
  function okCb() {
    // Removing Listener
    window.document.removeEventListener('keyup', documentKeyupEventListener);
  },
  function cancelCb() {
    // Removing Listener
    window.document.removeEventListener('keyup', documentKeyupEventListener);
  },
);

Thanks, Furkan