use-click-outside
Detects click and touch events outside of given element or elements group
Source
Docs
Package
Usage
API
The use-click-outside hook accepts 5 arguments:
handler– function that is called when clicking outsideevents– optional list of events that trigger outside click,['mousedown', 'touchstart']by defaultnodes- optional list of nodes that should not trigger outside click eventenabled- optional boolean to dynamically enable/disable the listener,trueby defaultcapture- optional boolean to register listeners in the capture phase,falseby default
The hook returns a ref object that must be passed to the element
based on which outside clicks should be captured.
Change events
By default, use-click-outside listens to mousedown and touchstart events,
you can change these events by passing an array of events as second argument:
Multiple nodes
Capture phase
By default, use-click-outside listens to events in the bubble phase. If an element
calls event.stopPropagation(), the event never reaches the document and the handler
is not called. Pass true as the fifth argument to listen in the capture phase instead,
so the handler is called before any other element can stop the event: