sc-react-ions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
162 lines (131 loc) • 5.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.Popover = undefined;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _react = require('react');
var _react2 = _interopRequireDefault(_react);
var _propTypes = require('prop-types');
var _propTypes2 = _interopRequireDefault(_propTypes);
var _reactClickOutside = require('react-click-outside');
var _reactClickOutside2 = _interopRequireDefault(_reactClickOutside);
var _StyledDiv = require('../StyledDiv');
var _StyledDiv2 = _interopRequireDefault(_StyledDiv);
var _styles = require('./styles.css');
var _styles2 = _interopRequireDefault(_styles);
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 Popover = exports.Popover = function (_Component) {
_inherits(Popover, _Component);
function Popover(props) {
_classCallCheck(this, Popover);
var _this = _possibleConstructorReturn(this, (Popover.__proto__ || Object.getPrototypeOf(Popover)).call(this, props));
_this.state = {
position: _this.props.defaultPosition
};
_this.componentDidMount = function () {
_this.updateRect();
_this.forceUpdate();
};
_this.componentDidUpdate = function (prevProps, prevState) {
_this.updateRect();
var updatedPosition = _this.getPosition(_this.state.position);
if (updatedPosition !== prevState.position) _this.setState({ position: updatedPosition });
};
_this.updateRect = function () {
_this._parentRect = _this._popoverWrapper && _this._popoverWrapper.getBoundingClientRect();
_this._boundingRect = _this._popoverWrapper && _this._popoverElement.getBoundingClientRect();
};
_this.getPosition = function (defaultPosition) {
switch (defaultPosition) {
case 'top':
if (_this._boundingRect.top < 0) return 'bottom';
break;
case 'bottom':
if (_this._boundingRect.bottom > window.innerHeight) return 'top';
break;
case 'left':
if (_this._boundingRect.left < 0) return 'right';
break;
case 'right':
if (_this._boundingRect.right > window.innerWidth) return 'left';
break;
}
return defaultPosition;
};
_this.handleClickOutside = function () {
if (_this.props.showing && _this.props.onRequestClose) _this.props.onRequestClose();
};
_this.render = function () {
return _react2.default.createElement(
_StyledDiv2.default,
{
css: (0, _styles2.default)(_extends({}, _this.props, _this.state, { parent: _this._parentRect })),
className: [_this.props.optClass, _this.props.className].join(' ').trim(),
innerRef: function innerRef(p) {
_this._popoverWrapper = p;
}
},
_react2.default.createElement(
'div',
{
className: 'popoverInner',
ref: function ref(c) {
return _this._popoverElement = c;
}
},
_react2.default.createElement(
'div',
{ className: 'popoverContent' },
_this.props.content
)
),
_this.props.children
);
};
if (props.optClass && process.env.NODE_ENV !== 'production') {
console.warn('Popover: Use of optClass will be deprecated as of react-ions 6.0.0, please use `className` instead');
}
return _this;
}
return Popover;
}(_react.Component);
Popover.propTypes = {
/**
* Whether to show the popover.
*/
showing: _propTypes2.default.bool,
/**
* The default position of the popover.
*/
defaultPosition: _propTypes2.default.oneOf(['top', 'bottom', 'right', 'left']),
/**
* The content to display inside the popover.
*/
content: _propTypes2.default.node,
/**
* The width of the popover, in any unit supported in css
*/
width: _propTypes2.default.string,
/**
* Optional class to add to the popover. (DEPRECATED, use className instead)
*/
optClass: _propTypes2.default.string,
/**
* Optional class to add to the popover.
*/
className: _propTypes2.default.string,
/**
* The method to be triggered on close.
*/
onRequestClose: _propTypes2.default.func
};
Popover.defaultProps = {
defaultPosition: 'bottom',
width: '300px',
showing: false
};
exports.default = (0, _reactClickOutside2.default)(Popover);