@phenomenon-hooks/use-outside-click
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

useOutsideClick


Installation

yarn add @phenomenon-hooks/use-outside-click

or

npm i @phenomenon-hooks/use-outside-click

Usage

import React from 'react';
import { useOutsideClick } from '@phenomenon-hooks/use-outside-click';

const Component: React.FC = () => {
    const [isOutsideClick, setIsOutsideClick] = React.useState(false);
    const ref = useOutsideClick(() => {
        setIsOutsideClick(true);
    });

    return (
        <div>
            {/* other markup */}
            <div ref={ref}>
                <p>Click outside of this element to see the result</p>
            </div>
        </div>
    );
};

Anatomy

const ref = useOutsideClick(() => {
    setIsOutsideClick(true);
});

Arguments

Argument Type Description
onOutside function Callback function that will be called when user clicks outside of the element

Returns

Return value Type Description
ref React.RefObject<HTMLElement> React ref object that should be passed to the element that should be tracked

Readme

Keywords

none

Package Sidebar

Install

npm i @phenomenon-hooks/use-outside-click

Weekly Downloads

3

Version

0.0.2

License

MIT

Unpacked Size

7.26 kB

Total Files

8

Last publish

Collaborators

  • mykola.kolomoyets