import { useEffect, useRef, useState } from 'react';

export function useClickOutside(fn: any, ref: any): void {
   useEffect(() => {
      // Função
      function handleClickOutside(event): void {
         if (ref.current && !ref.current.contains(event.target)) {
            fn();
         }
      }

      // Adicionar evento
      document.addEventListener('mousedown', handleClickOutside);
      return () => {
         // Remover evento
         document.removeEventListener('mousedown', handleClickOutside);
      };
   }, [ref, fn]);
}
