Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

use-close-on-click-outside

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

use-close-on-click-outside

This versatile component helps you easily detect and handle clicks outside a specified element. By utilizing this hook, you can effortlessly implement functionality such as closing modals or dropdown menus when users interact with the external area.

  • 1.5.5
  • unpublished
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

use close on click outside

The perfect external click handler

The useCloseOnClickOutside hook leverages the power of React's useEffect and useState hooks to provide a seamless integration into your project. Simply pass a ref object representing the target element to monitor, and the hook will manage the active state for you. When a click occurs outside the element, the modalActive state is automatically set to false, enabling you to respond and update your UI accordingly.

Installation

$ npm install use-close-on-click-outside

Typescript Example

import { useRef } from 'react'
import useCloseOnClickOutside from "use-close-on-click-outside";

const App = () => {
  const modalRef = useRef<HTMLDivElement>(null);
  const [ modalActive, setModalActive ] = useCloseOnClickOutside(modalRef)

  return (
    <div>
      <button onClick={() => setModalActive(true)}>toggle modal active</button>
        {
          modalActive
            &&
            <>
              <div ref={ref} className="modal">
                <h2>This is a modal</h2>
                <h4>Click outside this modal to close</h4>
              </div>
            </>
        }
    </div>
  )
}

Javascript Example

import { useRef } from 'react'
import useCloseOnClickOutside from "use-close-on-click-outside";

const App = () => {
  const modalRef = useRef(null);
  const [ modalActive, setModalActive ] = useCloseOnClickOutside(modalRef)

  return (
    <div>
      <button onClick={() => setModalActive(true)}>toggle modal active</button>
        {
          modalActive
            &&
            <>
              <div ref={ref} className="modal">
                <h2>This is a modal</h2>
                <h4>Click outside this modal to close</h4>
              </div>
            </>
        }
    </div>
  )
}

export default App




Keywords

FAQs

Package last updated on 02 Dec 2023

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

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc