useOnClickOutside()
The
useOnClickOutside() hook allows you to detect click or touch events that happen outside of a specified DOM element. It is essential for implementing behaviors like closing a modal when clicking the backdrop or hiding a dropdown menu when clicking away.Import
typescript
Usage
Open the card, then click anywhere outside of it to close.
State: CLOSED
Component.tsx
API
Arguments
ref– RefObject<T> The React ref object attached to the element you want to detect outside clicks for.handler– (event) => void The function to execute when a click outside is detected.mouseEvent– mousedown | mouseup (Optional) The mouse event type to listen for. Defaults to "mousedown".touchEvent– touchstart | touchend (Optional) The touch event type to listen for. Defaults to touchstart".
Returns
This hook returns
void.