Socket
Book a DemoInstallSign in
Socket

use-outside-click-react-hook

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-outside-click-react-hook

React Hook for Handling Clicks and Touches Outside a Specified Element

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
12
33.33%
Maintainers
1
Weekly downloads
 
Created
Source

use-outside-click-react-hook

Build Status

React Hook for Handling Clicks and Touches Outside a Specified Element (Only 3 code steps!).

Storybook:

https://shadiabuhilal.github.io/use-outside-click-react-hook/

Install

npm i use-outside-click-react-hook

Usage

import use-outside-click-react-hook

import useOutsideClick from 'use-outside-click-react-hook';

Using useOutsideClick

useOutsideClick React Hook

Example:

const [currentStepIndexState, setCurrentStepIndex] = useState(0);

...

export default function FooComponent() {
    const [isOpenState, setIsOpenState] = useState(false);

    // STEP1: Use ref to attach this functionality to the element that requires outside click and touch detection.
    const menuPopupDivRef = useRef(null);

    // STEP2: Use useOutsideClick React Hook to apply this functionality to the element that requires outside click and touch detection.
    useOutsideClick(menuPopupDivRef, () => {
        setIsOpenState(false);
    });

    return <div className='page-content'>
        <button data-testid="btnMenu" onClick={()=> setIsOpenState(true)}>Menu</button>
        {/* STEP3: Set ref to the element that requires outside click and touch detection */}
        {isOpenState && <div ref={menuPopupDivRef} className='menu-popup'>
            <ul>
                <li>Item 1</li>
                <li>Item 2</li>
                <li>Item 3</li>
            </ul>
        </div>}
    </div>;
}

For more info, please check storybook

Keywords

react

FAQs

Package last updated on 25 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.