A
A
Alexander2021-08-18 20:48:59
React
Alexander, 2021-08-18 20:48:59

How to prevent the modal from closing when the scrollbar is clicked?

Hello, can you please tell me how to prevent the modal window from closing when clicking on the scrollbar?
I have a custom useOnClickOutside hook, how do I add the scrollbar itself to the on click condition?
I will be grateful for the answer

export const useOnClickOutside = (ref: React.RefObject<HTMLElement>, handler: (event: Event) => void) => {
  React.useEffect(() => {
    const listener = (event: Event) => {
      if (!ref.current || ref.current.contains(event.target as Node)) {
        return
      }
      handler(event)
    }
    document.addEventListener('mousedown', listener)
    document.addEventListener('touchstart', listener)
    return () => {
      document.removeEventListener('mousedown', listener)
      document.removeEventListener('touchstart', listener)
    }
  }, [ref, handler])
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
B1dloKoder, 2021-08-20
@B1dloKoder

As far as I can understand, you need to add one more condition event.target != ref.current. Because by clicking on the scrollbar we are clicking on the container.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question