import { useEffect } from 'react';
/**
 * хук клика по кнопке клавиатуры `esc`
 */
export const useEscapeClickEffect = ({ onEscape, isActive, preventBubbling, }) => {
    useEffect(() => {
        if (!isActive) {
            return;
        }
        const onKeydown = (e) => {
            if (e.code === 'Escape') {
                if (preventBubbling) {
                    e.stopPropagation();
                    e.stopImmediatePropagation();
                }
                onEscape?.(e, 'escape');
            }
        };
        window.addEventListener('keydown', onKeydown, true);
        return () => {
            window.removeEventListener('keydown', onKeydown, true);
        };
    }, [isActive]);
};
