0.1.1 • Published 4 years ago

@app-elements/use-document-click v0.1.1

Weekly downloads
118
License
ISC
Repository
github
Last release
4 years ago

useDocumentClick

useDocumentClick takes a ref and a callback. When a click (or tap) occurs, the callback is invoked with a boolean indicating if the click happened outside of the given ref.

Useful for closing dropdowns, modals, dialogs, etc. when the user clicks outside of the component.

Installation

npm install --save @app-elements/use-document-click

Usage

import { useDocumentClick } from '@app-elements/use-document-click'

const MyComponent = (props) => {
  const ref = useRef()
  useDocumentClick(ref, (isOutsideRef) => isOutsideRef && setOpen(false))
  
  return (
    <h1 ref={ref}>Hello</h1>
  )
}

Props

PropTypeDefaultDescription
refRefNoneThe React ref
callbackFunctionNoneFunction to invoke on document click