Skip to content

Use this component to wrap other components to detect if it is clicked outside of the wrapped container.

Notifications You must be signed in to change notification settings

zhenyanghua/react-click-away

Repository files navigation

react-click-away

Use this component to wrap other components to detect if it is clicked outside of the wrapped container.

NPM JavaScript Style Guide

Demo

https://zhenyanghua.github.io/react-click-away/

Install

npm install --save @leafyjava/react-click-away

Usage

import React from 'react'

import ClickOutside from '@leafyjava/react-click-away'

const App = () => {
  const [clicked, setClicked] = React.useState(false);

  const handleClickOutside = () => {
    setClicked(clicked => !clicked);
  }

  return (
    <div>
      <ClickOutside onClickOutside={handleClickOutside}>
        <h1>Click Outside of this Box to Toggle</h1>
      </ClickOutside>
      {clicked && <h1>Clicked Outside Toggled</h1>}
    </div>

  )
}

export default App

License

MIT © zhenyanghua

About

Use this component to wrap other components to detect if it is clicked outside of the wrapped container.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published