1.0.0 • Published 3 years ago
@goo_co/use-on-click-outside v1.0.0
@goo_co/use-on-click-outside
React hook for listening for clicks outside an element.
Installation
npm install --save @goo_co/use-on-click-outside
# or
yarn add @goo_co/use-on-click-outsideSimple example
import React from 'react';
import useOnClickOutside from '@goo_co/use-on-click-outside';
const MyComponent = () => {
const [wrapperRef, setSelectWrapperRef] = React.useState(null);
const clickOutsideCallback = () => {
console.log('There was a click outside the wrapper');
};
useOnClickOutside(wrapperRef, clickOutsideCallback);
return (
<div ref={wrapperRef}>
Inside
</div>
);
}
export default MyComponent;Arguments
| Name | Description | Default | |
|---|---|---|---|
| wrapperRef* | Element ref for which you want to detect outside clicks | ReactRef | - |
| clickOutsideCallback* | Function to call on outside click | (e: MouseEvent) => void | - |
| config.capture | The option which forces to dispatch events to the listener before the target object | boolean | false |
| config.enabled | The option which enables/disables event listening bindings | boolean | true |
License
This project is licensed under the terms of the MIT license.
1.0.0
3 years ago