2
0

🐛 (editor) Fix variable dropdown overflow

Closes #209
This commit is contained in:
Baptiste Arnaud
2023-01-04 15:35:11 +01:00
parent e1af6af9c8
commit c1a32ce26b
10 changed files with 125 additions and 94 deletions

View File

@ -0,0 +1,26 @@
import { useEventListener } from '@chakra-ui/react'
import { RefObject } from 'react'
type Handler = (event: MouseEvent) => void
type Props<T> = {
ref: RefObject<T>
handler: Handler
mouseEvent?: 'mousedown' | 'mouseup'
}
export const useOutsideClick = <T extends HTMLElement = HTMLElement>({
ref,
handler,
mouseEvent = 'mousedown',
}: Props<T>): void => {
const triggerHandlerIfOutside = (event: MouseEvent) => {
const el = ref?.current
if (!el || el.contains(event.target as Node)) {
return
}
handler(event)
}
useEventListener(mouseEvent, triggerHandlerIfOutside)
}