UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

25 lines 871 B
import { useEffect, useRef } from 'react'; export var useEscapeClickaway = function useEscapeClickaway(onEscape, onClickAway) { var ref = useRef(null); useEffect(function () { var handleClickAway = function handleClickAway(event) { var el = ref.current; if (event.target instanceof Element && el && !el.contains(event.target)) { onClickAway(); } }; var handleKeydown = function handleKeydown(event) { if (event.key === 'Escape') { event.preventDefault(); onEscape(); } }; document.addEventListener('mousedown', handleClickAway); document.addEventListener('keydown', handleKeydown); return function () { document.removeEventListener('mousedown', handleClickAway); document.removeEventListener('keydown', handleKeydown); }; }, [onClickAway, onEscape]); return ref; };