import React, { useCallback, useRef } from 'react' export const useModalClose = ( callback?: () => void ): { ref: React.RefObject handleClick: (event: React.MouseEvent) => void } => { const ref = useRef(null) const handleClick = useCallback( (event: React.MouseEvent) => { const contentElement = ref.current if (!contentElement) return if (!(event.target instanceof Node)) return if (contentElement.contains(event.target)) return callback?.() }, [callback] ) return { ref, handleClick, } }