UNPKG

@create-figma-plugin/ui

Version:

Production-grade Preact components that replicate the Figma UI design

25 lines 959 B
import { useEffect } from 'preact/hooks'; import { getCurrentFromRef } from '../utilities/get-current-from-ref.js'; export function useMouseDownOutside(options) { const { ref, onMouseDownOutside } = options; useEffect(function () { function handleBlur() { onMouseDownOutside(); } function handleMouseDown(event) { const element = getCurrentFromRef(ref); if (element === event.target || element.contains(event.target)) { return; } onMouseDownOutside(); } window.addEventListener('blur', handleBlur); window.addEventListener('mousedown', handleMouseDown); return function () { window.removeEventListener('blur', handleBlur); window.removeEventListener('mousedown', handleMouseDown); }; }, [ref, onMouseDownOutside]); } //# sourceMappingURL=use-mouse-down-outside.js.map