import React, { useRef } from 'react'; /** * 모달 오버레이(배경) 클릭 시에만 닫히도록 하는 핸들러 쌍. * 모달 내부에서 텍스트를 드래그 선택하다가 마우스가 오버레이 위에서 놓이면 * click 이벤트의 타겟이 오버레이가 되어 의도치 않게 닫히는 문제를 막기 위해, * mousedown과 click(mouseup) 타겟이 모두 오버레이 자신일 때만 닫는다. */ export function useOverlayClose(onClose: () => void) { const mouseDownOnSelfRef = useRef(false); const onMouseDown = (e: React.MouseEvent) => { mouseDownOnSelfRef.current = e.target === e.currentTarget; }; const onClick = (e: React.MouseEvent) => { if (mouseDownOnSelfRef.current && e.target === e.currentTarget) { onClose(); } mouseDownOnSelfRef.current = false; }; return { onMouseDown, onClick }; }