1.0.0 • Published 2 years ago

@goo_co/use-on-click-outside v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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-outside

Simple 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

NameDescriptionDefault
wrapperRef*Element ref for which you want to detect outside clicksReactRef-
clickOutsideCallback*Function to call on outside click(e: MouseEvent) => void-
config.captureThe option which forces to dispatch events to the listener before the target objectbooleanfalse
config.enabledThe option which enables/disables event listening bindingsbooleantrue

License

This project is licensed under the terms of the MIT license.