Giter Site home page Giter Site logo

react-detect-event-outside's Introduction

react-detect-event-outside

EventOutside component for React.

A component which detects click, focus and touch events outside from it and calls a function to handle the event.

NOTE: We capture events at the document level and wish to support some events which do not bubble, for example to detect when elements outside a component have gained focus. In these situations we: (a) use the useCapture argument of addEventListener, or (b) find a similar event which does bubble (sometimes difficult).

Example

Using the EventOutside component

import React from 'react'
import EventOutside from 'react-detect-event-outside'

const onEvent = (evt, el) => console.log('event happened outside', evt, el)

const SomeComponent = () => (
  <div className="SomeComponent">
    <EventOutside onEventOutside={onEvent}>
      <div className="SomeComponent-inside">
        <h2>Inside</h2>
        <input type="text" />
      </div>
    </EventOutside>
    <div className="SomeComponent-outside">
      <h2>Outside</h2>
      <input type="text" />
    </div>
  </div>
)

export default SomeComponent

Wrapping your own component with the withEventOutside HOC

NOTE: This is useful if you wish to cause some state change within a component, since the handleEventOutside handler is defined within your component.

import React, { Component } from 'react'
import { withEventOutside } from 'react-detect-event-outside'

class Handler extends Component {
  handleEventOutside = (evt, el) =>
    console.log('Handler: event happened outside', evt, el)

  render() {
    const { children } = this.props
    return <div className="SomeComponent-inside">{children}</div>
  }
}

const WrappedHandler = withEventOutside()(Handler)

const SomeComponent = () => (
  <div className="SomeComponent">
    <WrappedHandler>
      <h2>Inside</h2>
      <input type="text" />
    </WrappedHandler>
    <div className="SomeComponent-outside">
      <h2>Outside</h2>
      <input type="text" />
    </div>
  </div>
)

export default SomeComponent

Install

yarn add react-detect-event-outside

react-detect-event-outside's People

Contributors

sebinsua avatar

Watchers

 avatar  avatar

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.