use-click-outside
Detect click and touch events outside of specified element
Import
Source
Docs
Package
Usage
API
Hook accepts 3 arguments:
handler
– function that will be called on outside clickevents
– optional list of events that indicate outside clicknodes
- optional list of nodes that should not trigger outside click event
Hook returns React ref
object that should be passed to element 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
In some cases you may want to trigger outside click
event for multiple nodes, for example, when dropdown renders in portal.
To do so provide third argument with an array of nodes which should not trigger outside click event:
TypeScript
Definition
Ref type
By default, use-click-outside returns ref
object with React.MutableRefObject<any>
type
as ref type does not matter in almost all cases. You can specify ref type by passing a type: