All files / src/utils/hooks useClickToggleVisible.ts

72.72% Statements 8/11
0% Branches 0/2
75% Functions 3/4
72.72% Lines 8/11

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 24 25 26 27 28    66x       8x   8x               8x 8x 8x 8x       8x        
import { useState, useEffect } from 'react';
 
const useClickToggleVisible = (): [
  boolean,
  React.Dispatch<React.SetStateAction<boolean>>
] => {
  const [visible, setVisible] = useState(false);
 
  const handleClickOutside = (event: MouseEvent): void => {
    const triggerNode = event.target as HTMLElement;
 
    if (!triggerNode.closest('.ant-popover')) {
      setVisible(false);
    }
  };
 
  useEffect(() => {
    document.addEventListener('mousedown', handleClickOutside);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
    };
  }, []);
 
  return [visible, setVisible];
};
 
export default useClickToggleVisible;