react-portal
Version:
To make your life with React Portals easier.
159 lines (136 loc) • 5.63 kB
JavaScript
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _PortalCompat = require('./PortalCompat');
var _PortalCompat2 = _interopRequireDefault(_PortalCompat);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var KEYCODES = {
ESCAPE: 27
};
var PortalWithState = function (_React$Component) {
_inherits(PortalWithState, _React$Component);
function PortalWithState(props) {
_classCallCheck(this, PortalWithState);
var _this = _possibleConstructorReturn(this, (PortalWithState.__proto__ || Object.getPrototypeOf(PortalWithState)).call(this, props));
_this.portalNode = null;
_this.state = { active: !!props.defaultOpen };
_this.openPortal = _this.openPortal.bind(_this);
_this.closePortal = _this.closePortal.bind(_this);
_this.wrapWithPortal = _this.wrapWithPortal.bind(_this);
_this.handleOutsideMouseClick = _this.handleOutsideMouseClick.bind(_this);
_this.handleKeydown = _this.handleKeydown.bind(_this);
return _this;
}
_createClass(PortalWithState, [{
key: 'componentDidMount',
value: function componentDidMount() {
if (this.props.closeOnEsc) {
document.addEventListener('keydown', this.handleKeydown);
}
if (this.props.closeOnOutsideClick) {
document.addEventListener('click', this.handleOutsideMouseClick);
}
}
}, {
key: 'componentWillUnmount',
value: function componentWillUnmount() {
if (this.props.closeOnEsc) {
document.removeEventListener('keydown', this.handleKeydown);
}
if (this.props.closeOnOutsideClick) {
document.removeEventListener('click', this.handleOutsideMouseClick);
}
}
}, {
key: 'openPortal',
value: function openPortal(e) {
if (this.state.active) {
return;
}
if (e && e.nativeEvent) {
e.nativeEvent.stopImmediatePropagation();
}
this.setState({ active: true }, this.props.onOpen);
}
}, {
key: 'closePortal',
value: function closePortal() {
if (!this.state.active) {
return;
}
this.setState({ active: false }, this.props.onClose);
}
}, {
key: 'wrapWithPortal',
value: function wrapWithPortal(children) {
var _this2 = this;
if (!this.state.active) {
return null;
}
return _react2.default.createElement(
_PortalCompat2.default,
{
node: this.props.node,
key: 'react-portal',
ref: function ref(portalNode) {
return _this2.portalNode = portalNode;
}
},
children
);
}
}, {
key: 'handleOutsideMouseClick',
value: function handleOutsideMouseClick(e) {
if (!this.state.active) {
return;
}
var root = this.portalNode && (this.portalNode.props.node || this.portalNode.defaultNode);
if (!root || root.contains(e.target) || e.button && e.button !== 0) {
return;
}
this.closePortal();
}
}, {
key: 'handleKeydown',
value: function handleKeydown(e) {
if (e.keyCode === KEYCODES.ESCAPE && this.state.active) {
this.closePortal();
}
}
}, {
key: 'render',
value: function render() {
return this.props.children({
openPortal: this.openPortal,
closePortal: this.closePortal,
portal: this.wrapWithPortal,
isOpen: this.state.active
});
}
}]);
return PortalWithState;
}(_react2.default.Component);
PortalWithState.propTypes = {
children: _propTypes2.default.func.isRequired,
defaultOpen: _propTypes2.default.bool,
node: _propTypes2.default.any,
closeOnEsc: _propTypes2.default.bool,
closeOnOutsideClick: _propTypes2.default.bool,
onOpen: _propTypes2.default.func,
onClose: _propTypes2.default.func
};
PortalWithState.defaultProps = {
onOpen: function onOpen() {},
onClose: function onClose() {}
};
exports.default = PortalWithState;