"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
exports.usePressEscapeHandler = usePressEscapeHandler;
var _compat = require("preact/compat");
/**
 * Calls a handler whenever the Escape key is pressed within an element. To capture
 * on the entire document, pass null or undefined for the ref.
 *
 * @param ref - Ref of the element to capture keystrokes in. Defaults to the document
 * @param handler - Function called if the escape key is pressed
 */
function usePressEscapeHandler(ref, handler) {
  function handleEscapeKey(event) {
    const ESCAPE_KEY = 27;
    if (event.keyCode === ESCAPE_KEY || event.key === 'Escape') {
      handler(event);
    }
  }
  (0, _compat.useEffect)(() => {
    const node = ref ? ref.current : document;
    if (!node) return;
    node.addEventListener('keydown', handleEscapeKey);
    return () => {
      node.removeEventListener('keydown', handleEscapeKey);
    };
  }, [handleEscapeKey]);
}
var _default = exports.default = usePressEscapeHandler;