var React = require('react');
var isAncestor = require('./is-ancestor');

var OutsideClick = React.createClass({
  displayName: 'OutsideClick',

  propTypes: {
    children: React.PropTypes.any,
    component: React.PropTypes.string,
    onOutsideClick: React.PropTypes.func,
  },

  getDefaultProps: function () {
    return {
      component: 'div',
    };
  },

  componentDidMount: function () {
    if (document) {
      document.addEventListener('click', this._handleDocumentClick);
    }
  },

  componentWillUnmount: function () {
    if (document) {
      document.removeEventListener('click', this._handleDocumentClick);
    }
  },

  _handleDocumentClick: function (event) {
    if (this._elem) {
      if (!isAncestor(event.target, this._elem)) {
        this._handleOutsideClick(event);
      }
    }
  },

  _handleOutsideClick: function (event) {
    if (this.props.onOutsideClick) this.props.onOutsideClick(event);
  },

  render: function () {
    const {
      children,
      component,
      onOutsideClick,
      ...otherProps
    } = this.props;

    const props = Object.assign(otherProps, {
      ref: function (elem) { this._elem = elem; }.bind(this),
    });

    return React.createElement(component, props, children);
  },

});

module.exports = OutsideClick;
@@ -1,73 +1,225 @@
-var React = require('
+/**%0A * Copyright Felipe Thom%C3%A9.%0A * All rights reserved.%0A *%0A * This source code is licensed under the BSD-style license found in the%0A * LICENSE file in the root di
 re
-a
 ct
-');%0Avar isAncestor = require('./is-ancestor
+ory of this source tree.%0A */%0A%0Avar React = require('react
 ');%0A
@@ -305,16 +305,51 @@
 ypes: %7B%0A
+    capture: React.PropTypes.bool,%0A
     chil
@@ -414,16 +414,51 @@
 string,%0A
+    onClick: React.PropTypes.func,%0A
     onOu
@@ -576,24 +576,95 @@
    %7D;%0A  %7D,%0A%0A
+  componentWillMount: function () %7B%0A    this._wasInside = false;%0A  %7D,%0A%0A
   componentD
@@ -734,32 +734,49 @@
 entListener(
-'click',
+%0A        'click',%0A       
  this._handl
@@ -781,32 +781,67 @@
 dleDocumentClick
+,%0A        this.props.capture%0A      
 );%0A    %7D%0A  %7D,%0A%0A 
@@ -932,24 +932,41 @@
 istener(
-'click',
+%0A        'click',%0A       
  this._h
@@ -987,45 +987,80 @@
 lick
-);%0A    %7D%0A  %7D,%0A%0A  _handleDocumentClick
+,%0A        this.props.capture%0A      );%0A    %7D%0A  %7D,%0A%0A  _checkForBubblePhase
 : fu
@@ -1084,26 +1084,32 @@
     if (
+!
 this._
-elem
+wasInside
 ) %7B%0A    
@@ -1114,48 +1114,181 @@
     
-if (!isAncestor(event.target, this._elem
+this._handleOutsideClick(event);%0A    %7D%0A    this._wasInside = false;%0A  %7D,%0A%0A  _checkForCapturePhase: function (event) %7B%0A    if (this._elem && !this._elem.contains(event.target
 )) %7B
@@ -1290,26 +1290,24 @@
 t)) %7B%0A      
-  
 this._handle
@@ -1331,19 +1331,194 @@
 t);%0A    
+%7D%0A
   %7D
+,%0A%0A  _handleDocumentClick: function (event) %7B%0A    if (this.props.capture) %7B%0A      this._checkForCapturePhase(event);%0A    %7D%0A    else %7B%0A      this._checkForBubblePhase(event);
 %0A    %7D%0A 
@@ -1645,14 +1645,27 @@
 %0A%0A  
-render
+_handleWrapperClick
 : fu
@@ -1672,24 +1672,29 @@
 nction (
+event
 ) %7B%0A    
 const %7B%0A
@@ -1689,98 +1689,153 @@
     
-const %7B%0A      children,%0A      component,%0A      onOutsideClick,%0A      ...otherProps%0A    %7D =
+this._wasInside = true;%0A    if (this.props.onClick) this.props.onClick(event);%0A  %7D,%0A%0A  render: function () %7B%0A    var newProps = Object.assign(%7B%7D,
  thi
@@ -1845,67 +1845,185 @@
 rops
+)
 ;
-%0A
 %0A    
-const props = Object.assign(otherProps, %7B%0A      ref:
+delete newProps.capture;%0A    delete newProps.children;%0A    delete newProps.component;%0A    delete newProps.onClick;%0A    delete newProps.onOutsideClick;%0A%0A    newProps.ref =
  fun
@@ -2036,16 +2036,22 @@
 (elem) %7B
+%0A     
  this._e
@@ -2061,16 +2061,20 @@
  = elem;
+%0A   
  %7D.bind(
@@ -2082,76 +2082,162 @@
 his)
-,
+;%0A
 %0A    
-%7D);%0A%0A    return React.createElement(component, props, 
+newProps.onClick = this._handleWrapperClick;%0A%0A    return React.createElement(%0A      this.props.component,%0A      newProps,%0A      this.props.
 children
 );%0A 
@@ -2232,16 +2232,21 @@
 children
+%0A    
 );%0A  %7D,%0A

Add capture phase support and remove destructure assignment