import { useEffect } from "react";
const ESCAPE_CODE = "Escape";
/**
 * To used call the callback onEscape.
 * @param {*} callback - used call on Escape key press.
 * @param {*} dependencies Dependencies must be an array!
 */

export default function useEscapeEffect(callback) {
  let dependencies = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
  useEffect(() => {
    if (!callback) {
      return;
    }

    let onKeyDown = event => {
      if (event.code === ESCAPE_CODE) {
        callback(event);
      }
    };

    document.addEventListener('keydown', onKeyDown);
    return () => {
      document.removeEventListener('keydown', onKeyDown);
    };
  }, dependencies);
}
;