All files / src/helpers/hooks useGetOuterRef.ts

80% Statements 8/10
0% Branches 0/4
75% Functions 3/4
80% Lines 8/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23    2x 12x   12x 12x           12x   12x 12x       12x      
import React, { useEffect } from 'react';
 
const useGetOuterRef = (callback: () => void) => {
    const nodeRef = React.useRef<HTMLDivElement>(null);
 
    useEffect(() => {
        const handler = (e: Event) => {
            if (nodeRef?.current && !nodeRef.current.contains(e.target as HTMLDivElement)) {
                callback();
            }
        };
 
        document.addEventListener("mousedown", handler);
 
        return () => {
            document.removeEventListener("mousedown", handler);
        };
    }, [callback]);
 
    return nodeRef;
}
 
export default useGetOuterRef;