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

0.0.2 • Public • Published

Use Click Outside

`@react-hooks-hub/use-click-outside` is a custom React hook designed for detecting clicks outside a specified element or set of elements. Easy to integrate and use, this hook is perfect for scenarios like closing dropdown menus, popups, or modals when a user clicks outside these components. It supports multiple referenced elements and provides a straightforward interface for adding custom click outside event handling. Compatible with React and TypeScript, useClickOutside is an essential tool for enhancing user interaction in your web applications.

NPM version CI status   Bundlephobia  

@react-hooks-hub/use-click-outside

Installation

Use your preferred package manager to install @react-hooks-hub/use-click-outside:

npm install @react-hooks-hub/use-click-outside
# or
yarn add @react-hooks-hub/use-click-outside

Usage

Import and use the useClickOutside hook in your component:

import React, { useState, useRef } from 'react';
import { useClickOutside } from '@react-hooks-hub/use-click-outside';

function DropdownMenu() {
    const [isOpen, setIsOpen] = useState(false);
    const menuRef = useRef(null);

    useClickOutside([menuRef], (isOutside) => {
      setIsOpen(isOutside)
    });

    return (
        <div>
            <button onClick={() => setIsOpen(!isOpen)}>Toggle Menu</button>
            {isOpen && (
                <div ref={menuRef}>
                    {/* content */}
                </div>
            )}
        </div>
    );
}

export default DropdownMenu;

Package Sidebar

Install

npm i @react-hooks-hub/use-click-outside

Weekly Downloads

3

Version

0.0.2

License

MIT

Unpacked Size

7.42 kB

Total Files

7

Last publish

Collaborators

  • keized