react-conventions
Version:
An open source set of React components that implement Ambassador's Design and UX patterns.
203 lines (165 loc) • 7.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
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 _bind = require('classnames/bind');
var _bind2 = _interopRequireDefault(_bind);
var _style = require('./style.scss');
var _style2 = _interopRequireDefault(_style);
var _RenderToLayer = require('../internal/RenderToLayer');
var _RenderToLayer2 = _interopRequireDefault(_RenderToLayer);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }
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; }
/**
* The Tooltip component.
*/
var Tooltip = function (_React$Component) {
_inherits(Tooltip, _React$Component);
function Tooltip(props) {
_classCallCheck(this, Tooltip);
var _this = _possibleConstructorReturn(this, (Tooltip.__proto__ || Object.getPrototypeOf(Tooltip)).call(this, props));
_this.state = {
showing: false
};
_this.componentDidMount = function () {
if (_this.props.show) {
setTimeout(function () {
_this.props.show ? _this.showTooltip() : null;
}, 1000);
}
};
_this.componentWillReceiveProps = function (nextProps) {
if (typeof nextProps.show !== 'undefined') {
_this.setState({ showing: nextProps.show });
}
};
_this.showTooltip = function () {
_this.tooltipPlacement();
_this.setState({ showing: true });
};
_this.hideTooltip = function () {
if (!_this.props.show) {
_this.setState({ showing: false });
}
};
_this.handleTooltipEnter = function () {
if (_this.props.mouseOverCallback) {
_this.props.mouseOverCallback();
}
};
_this.handleTooltipOut = function () {
if (_this.props.mouseOutCallback) {
_this.props.mouseOutCallback();
}
};
_this.tooltipPlacement = function () {
var triggerRect = _this._triggerElement.getBoundingClientRect();
_this._tooltipPlacement = {};
_this._tooltipPlacement.translate = triggerRect.width / 2;
switch (_this.props.tooltipPlacement) {
case 'bottom':
_this._tooltipPlacement.left = triggerRect.left + (triggerRect.right - triggerRect.left) / 2;
_this._tooltipPlacement.top = triggerRect.bottom;
break;
case 'right':
_this._tooltipPlacement.left = triggerRect.right;
_this._tooltipPlacement.top = triggerRect.top + (triggerRect.bottom - triggerRect.top) / 2;
break;
case 'left':
_this._tooltipPlacement.left = triggerRect.left;
_this._tooltipPlacement.top = triggerRect.top + (triggerRect.bottom - triggerRect.top) / 2;
break;
default:
_this._tooltipPlacement.left = triggerRect.left + (triggerRect.right - triggerRect.left) / 2;
_this._tooltipPlacement.top = triggerRect.top;
}
};
_this.getTranslate = function () {
return _this._tooltipPlacement.translate + 'px';
};
_this.getStyles = function () {
var style = {};
if (_this.state.showing && !_this.props.show || _this.state.showing && _this.props.show && _this.props.tooltipPlacement !== 'top') {
style.top = _this._tooltipPlacement.top + window.pageYOffset;
style.left = _this._tooltipPlacement.left + window.pageXOffset;
style.opacity = 0.9;
} else if (_this.state.showing && _this.props.show && _this.props.tooltipPlacement === 'top') {
style.top = 'inherit';
style.left = 'inherit';
style.opacity = 0.9;
style.transform = 'translateX(-50%) translateX(-' + _this.getTranslate() + ') translateY(-100%) translateY(-6px)';
}
return style;
};
_this.renderTooltip = function () {
var cx = _bind2.default.bind(_style2.default);
var tooltipShowingClass = _this.state.showing ? _style2.default['tooltip-showing'] : '';
var tooltipClass = cx(_style2.default['tooltip-component'], _this.props.optClass, tooltipShowingClass, _style2.default[_this.props.tooltipPlacement]);
var styles = _this.getStyles();
return _react2.default.createElement(
'span',
{ className: tooltipClass, style: styles, onMouseEnter: _this.handleTooltipEnter, onMouseOut: _this.handleTooltipOut },
_this.props.content
);
};
_this.render = function () {
var _this$props = _this.props,
content = _this$props.content,
optClass = _this$props.optClass,
tooltipPlacement = _this$props.tooltipPlacement,
appendToBody = _this$props.appendToBody,
show = _this$props.show,
other = _objectWithoutProperties(_this$props, ['content', 'optClass', 'tooltipPlacement', 'appendToBody', 'show']);
return _react2.default.createElement(
'span',
_extends({ onMouseOver: _this.showTooltip, onMouseOut: _this.hideTooltip, ref: function ref(c) {
return _this._triggerElement = c;
} }, other),
_this.props.children,
_this.props.appendToBody ? _react2.default.createElement(_RenderToLayer2.default, { render: _this.renderTooltip, open: true }) : _this.renderTooltip()
);
};
return _this;
}
return Tooltip;
}(_react2.default.Component);
Tooltip.defaultProps = {
tooltipPlacement: 'top'
};
Tooltip.propTypes = {
/**
* The content to display inside the `Tooltip`. Could be number, string, element or an array containing these types.
*/
content: _react2.default.PropTypes.node,
/**
* Optional styles to add to the tooltip.
*/
optClass: _react2.default.PropTypes.string,
/**
* The placement of the tooltip.
*/
tooltipPlacement: _react2.default.PropTypes.oneOf(['left', 'right', 'top', 'bottom']),
/**
* Whether to insert the tooltip element after the trigger element or append it to the document body.
*/
appendToBody: _react2.default.PropTypes.bool,
/**
* Whether to show the tooltip element by default.
*/
show: _react2.default.PropTypes.bool,
/**
* Callback to call when mouseover is called
*/
mouseOverCallback: _react2.default.PropTypes.func,
/**
* Callback to call when mouseout is called
*/
mouseOutCallback: _react2.default.PropTypes.func
};
exports.default = Tooltip;