Code Monkey home page Code Monkey logo

react-click-outside's Introduction

ClickOutside

React click outside component.

Installation

$ npm install tj/react-click-outside

Example

<ClickOutside onClickOutside={::this.close}>
  <p>Im a menu or something that you want to hide when clicking outside.</p>
</ClickOutside>

Badges


tjholowaychuk.com  ·  GitHub @tj  ·  Twitter @tjholowaychuk

react-click-outside's People

Contributors

agtlucas avatar andarist avatar bernardodiasc avatar haargeeel avatar inkdpixels avatar jonathanong avatar lourd avatar m19c avatar naoufal avatar oieduardorabelo avatar tj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-click-outside's Issues

Support "names"

So instead of a simple outside-click to an element, if we click outside of both element A and element B (e.g., click(a) /* $b's callback doesn't get called /; click(b) / $a's callback doesn't get called /; click(c) / the callback gets called */). Not necessarily 2 elements, but any element with the same "name".

Best example is an autocomplete input.

Initial Idea

<ClickOutside name="x" onClickOutside={this.handle} />
<ClickOutside name="x" onClickOutside={this.handle} />

Another Idea

<ClickOutsideOf of="x" onClickOutside={this.handle}>
  <ClickOutside name="x"><button /></ClickOutside>
  <ClickOutside name="x"><button /></ClickOutside>
</ClickOutside
  • I don't have a better API yet. Posting this up maybe someone could come up with one.
  • If it's outside the scope of this project (yea it does complicate things a bit), umm feel free to close haha 👌 . Thanks.

Latest update is breaking builds

ERROR in ./~/react-click-outside/build/index.js
Module not found: Error: Can't resolve 'react-transform-hmr' in '/Users/mmmeff/code/reactui/node_modules/react-click-outside/build'
resolve 'react-transform-hmr' in '/Users/mmmeff/code/reactui/node_modules/react-click-outside/build'
  Parsed request is a module
  using description file: /Users/mmmeff/code/reactui/node_modules/react-click-outside/package.json (relative path: ./build)
    Field 'browser' doesn't contain a valid alias configuration
  after using description file: /Users/mmmeff/code/reactui/node_modules/react-click-outside/package.json (relative path: ./build)
    resolve as module

Happened after updating to 1.1.0

Supporting any wrapping element

I'm wishing to wrap the target of this library in something other than a div. I went ahead and made some changes locally to get this type signature:

<ClickOutside onClickOutside={handleClickOff} wrappingElement={'span'}>

Is this something that you would want back upstream?

Release what's on master

Hi TJ,

Could you release what's on master? It makes the module compatible with react 16.

Many thanks!

Doesn't work if you click outside, over an iFrame

Use case:

  • You have a menu A.
  • You have a callback X, which is to be triggered if a click is detected outside A (using this library).
  • Somewhere on the page, you have an iFrame B.
  • When A is visible, and you click anywhere on the page, X gets invoked (working correct).
  • When A is visible, and you click anywhere over B, X doesn't get invoked (bug).

version: 3.0.0

support touch start, etc?

maybe as an option.

idea is if you're on mobile and you click outside an open dropdown, the dropdown should close ASAP.

Maintainence

Hello @tj I notice that you don't maintain this module anymore, I would love to maintain it :). Would you mind handing over control?

use refs instead of react-dom

i'll make a PR eventually. i just can't do it in the github editor:

import React, { Component, PropTypes } from 'react'

export default class ClickOutside extends Component {
  static propTypes = {
    onClickOutside: PropTypes.func.isRequired
  };

  render() {
    const { children } = this.props
    return <div ref='container'>{children}</div>
  }

  componentDidMount() {
    document.addEventListener('click', this.handle, true)
  }

  componentWillUnmount() {
    document.removeEventListener('click', this.handle, true)
  }

  handle = e => {
    const { onClickOutside } = this.props
    const el = this.refs.container
    if (!el.contains(e.target)) onClickOutside(e)
  }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.