UNPKG

11.6 kBJavaScriptView Raw
1import { __extends, __makeTemplateObject } from "tslib";
2import merge from 'lodash/merge';
3import noop from 'lodash/noop';
4import pick from 'lodash/pick';
5import * as React from 'react';
6import * as ReactDOM from 'react-dom';
7import styled from 'styled-components';
8var TARGET_OFFSET = 3;
9var CLICK_TIMEOUT = 1000;
10var createOnRemoveObserver = function (element, onDetachCallback) {
11 var observer = new MutationObserver(function () {
12 if (!document.contains(element)) {
13 observer.disconnect();
14 onDetachCallback();
15 }
16 });
17 observer.observe(document, {
18 childList: true,
19 subtree: true,
20 });
21 return observer;
22};
23var arrowStyle = "\n\tposition: absolute;\n\twidth: 0;\n\theight: 0;\n\tborder-color: transparent;\n\tborder-style: solid;\n";
24var TooltipTopArrow = styled.div(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n\t", " bottom: 0;\n\tleft: 50%;\n\tmargin-left: -5px;\n\tborder-width: 5px 5px 0;\n\tborder-top-color: #000;\n"], ["\n\t", " bottom: 0;\n\tleft: 50%;\n\tmargin-left: -5px;\n\tborder-width: 5px 5px 0;\n\tborder-top-color: #000;\n"])), arrowStyle);
25var TooltipRightArrow = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n\t", " top: 50%;\n\tleft: 0;\n\tmargin-top: -5px;\n\tborder-width: 5px 5px 5px 0;\n\tborder-right-color: #000;\n"], ["\n\t", " top: 50%;\n\tleft: 0;\n\tmargin-top: -5px;\n\tborder-width: 5px 5px 5px 0;\n\tborder-right-color: #000;\n"])), arrowStyle);
26var TooltipLeftArrow = styled.div(templateObject_3 || (templateObject_3 = __makeTemplateObject(["\n\t", " top: 50%;\n\tright: 0;\n\tmargin-top: -5px;\n\tborder-width: 5px 0 5px 5px;\n\tborder-left-color: #000;\n"], ["\n\t", " top: 50%;\n\tright: 0;\n\tmargin-top: -5px;\n\tborder-width: 5px 0 5px 5px;\n\tborder-left-color: #000;\n"])), arrowStyle);
27var TooltipBottomArrow = styled.div(templateObject_4 || (templateObject_4 = __makeTemplateObject(["\n\t", " top: 0;\n\tleft: 50%;\n\tmargin-left: -5px;\n\tborder-width: 0 5px 5px;\n\tborder-bottom-color: #000;\n"], ["\n\t", " top: 0;\n\tleft: 50%;\n\tmargin-left: -5px;\n\tborder-width: 0 5px 5px;\n\tborder-bottom-color: #000;\n"])), arrowStyle);
28var getArrowElement = function (placement) {
29 switch (placement) {
30 case 'right':
31 return TooltipRightArrow;
32 case 'bottom':
33 return TooltipBottomArrow;
34 case 'left':
35 return TooltipLeftArrow;
36 default:
37 return TooltipTopArrow;
38 }
39};
40var TooltipElement = styled.div(templateObject_5 || (templateObject_5 = __makeTemplateObject(["\n\tdisplay: block;\n\tfont-size: 12px;\n\tfont-style: normal;\n\tfont-weight: 400;\n\tletter-spacing: normal;\n\tline-break: auto;\n\tline-height: 1.42857143;\n\tpadding: 5px;\n\tposition: absolute;\n\tmax-width: 300px;\n\ttext-align: left;\n\ttext-align: start;\n\ttext-decoration: none;\n\ttext-shadow: none;\n\ttext-transform: none;\n\tvisibility: hidden;\n\twhite-space: normal;\n\tword-break: normal;\n\tword-spacing: normal;\n\tword-wrap: normal;\n\tz-index: 10000;\n\tcolor: white;\n"], ["\n\tdisplay: block;\n\tfont-size: 12px;\n\tfont-style: normal;\n\tfont-weight: 400;\n\tletter-spacing: normal;\n\tline-break: auto;\n\tline-height: 1.42857143;\n\tpadding: 5px;\n\tposition: absolute;\n\tmax-width: 300px;\n\ttext-align: left;\n\ttext-align: start;\n\ttext-decoration: none;\n\ttext-shadow: none;\n\ttext-transform: none;\n\tvisibility: hidden;\n\twhite-space: normal;\n\tword-break: normal;\n\tword-spacing: normal;\n\tword-wrap: normal;\n\tz-index: 10000;\n\tcolor: white;\n"])));
41var TooltipElementInner = styled.div(templateObject_6 || (templateObject_6 = __makeTemplateObject(["\n\tbackground: black;\n\tborder-radius: 4px;\n\tpadding: 3px 8px;\n\ttext-align: center;\n"], ["\n\tbackground: black;\n\tborder-radius: 4px;\n\tpadding: 3px 8px;\n\ttext-align: center;\n"])));
42var TooltipComponent = (function (_super) {
43 __extends(TooltipComponent, _super);
44 function TooltipComponent(props) {
45 var _this = _super.call(this, props) || this;
46 _this.state = {
47 show: false,
48 placement: 'top',
49 coordinates: {
50 top: 0,
51 left: 0,
52 },
53 };
54 return _this;
55 }
56 TooltipComponent.prototype.observe = function (target) {
57 var _this = this;
58 if (this.observer) {
59 this.observer.disconnect();
60 }
61 this.observer = createOnRemoveObserver(target, function () {
62 _this.hide();
63 });
64 };
65 TooltipComponent.prototype.show = function (e, tooltipText, options) {
66 if (options === void 0) { options = {}; }
67 var top = 0;
68 var left = 0;
69 var target = e.target;
70 this.observe(target);
71 var boundingClientRect = target.getBoundingClientRect();
72 var boundingRect = pick(boundingClientRect, ['top', 'left', 'width', 'height']);
73 boundingRect.top += window.scrollY;
74 boundingRect.left += window.scrollX;
75 var placement = options.placement, containerStyle = options.containerStyle, innerStyle = options.innerStyle, arrowStyle = options.arrowStyle;
76 if (this.tooltipElementInner) {
77 this.tooltipElementInner.innerText = tooltipText;
78 }
79 if (!placement || placement === 'top') {
80 top = boundingRect.top - this.tooltipElement.clientHeight - TARGET_OFFSET;
81 left =
82 boundingRect.left +
83 boundingRect.width / 2 -
84 this.tooltipElement.clientWidth / 2;
85 }
86 if (placement === 'right') {
87 top =
88 boundingRect.top +
89 boundingRect.height / 2 -
90 this.tooltipElement.clientHeight / 2;
91 left = boundingRect.left + boundingRect.width + TARGET_OFFSET;
92 }
93 if (placement === 'bottom') {
94 top = boundingRect.top + boundingRect.height + TARGET_OFFSET;
95 left =
96 boundingRect.left +
97 boundingRect.width / 2 -
98 this.tooltipElement.clientWidth / 2;
99 }
100 if (placement === 'left') {
101 top =
102 boundingRect.top +
103 boundingRect.height / 2 -
104 this.tooltipElement.clientHeight / 2;
105 left =
106 boundingRect.left - this.tooltipElement.clientWidth - TARGET_OFFSET;
107 }
108 this.setState({
109 coordinates: { top: top, left: left },
110 show: true,
111 placement: placement,
112 containerStyle: containerStyle,
113 innerStyle: innerStyle,
114 arrowStyle: arrowStyle,
115 });
116 };
117 TooltipComponent.prototype.hide = function () {
118 if (this.observer) {
119 this.observer.disconnect();
120 this.observer = undefined;
121 }
122 this.setState({
123 show: false,
124 coordinates: {
125 top: 0,
126 left: 0,
127 },
128 });
129 };
130 TooltipComponent.prototype.render = function () {
131 var _this = this;
132 var _a = this.state, placement = _a.placement, containerStyle = _a.containerStyle, innerStyle = _a.innerStyle, arrowStyle = _a.arrowStyle;
133 var Arrow = getArrowElement(placement);
134 var tooltipStyle = merge({
135 top: this.state.coordinates.top,
136 left: this.state.coordinates.left,
137 visibility: this.state.show ? 'visible' : 'hidden',
138 }, containerStyle);
139 return (React.createElement(TooltipElement, { style: tooltipStyle, ref: function (el) { return (_this.tooltipElement = el); } },
140 React.createElement(TooltipElementInner, { ref: function (el) { return (_this.tooltipElementInner = el); }, style: innerStyle }),
141 React.createElement(Arrow, { style: arrowStyle })));
142 };
143 return TooltipComponent;
144}(React.Component));
145var Tooltips = (function () {
146 function Tooltips() {
147 this.initialised = false;
148 this.hideOnMouseOut = null;
149 }
150 Tooltips.prototype.initialiseElements = function () {
151 var _this = this;
152 if (this.initialised || !document || !document.body) {
153 return;
154 }
155 var tooltipRoot = document.createElement('div');
156 tooltipRoot.id = 'rendition-tooltip-root';
157 document.body.appendChild(tooltipRoot);
158 document.addEventListener('mouseover', function (e) {
159 var target = e.target;
160 if (!_this.hideOnMouseOut ||
161 _this.hideOnMouseOut === target) {
162 return;
163 }
164 if (_this.hideOnMouseOut.firstElementChild &&
165 _this.hideOnMouseOut.contains &&
166 _this.hideOnMouseOut.contains(target)) {
167 return;
168 }
169 _this.hide();
170 _this.hideOnMouseOut = null;
171 });
172 this.component = ReactDOM.render(React.createElement(TooltipComponent, null), tooltipRoot);
173 this.initialised = true;
174 };
175 Tooltips.prototype.bindProps = function (props) {
176 var _this = this;
177 if (props.tooltip) {
178 var options_1 = {};
179 var trigger = 'hover';
180 var tooltipText_1;
181 if (typeof props.tooltip === 'string') {
182 tooltipText_1 = props.tooltip;
183 }
184 else {
185 tooltipText_1 = props.tooltip.text;
186 options_1.placement = props.tooltip.placement;
187 options_1.containerStyle = props.tooltip.containerStyle;
188 options_1.innerStyle = props.tooltip.innerStyle;
189 options_1.arrowStyle = props.tooltip.arrowStyle;
190 if (props.tooltip.trigger) {
191 trigger = props.tooltip.trigger;
192 }
193 }
194 if (tooltipText_1) {
195 var showFn_1 = function (e) { return _this.show(e, tooltipText_1, options_1); };
196 if (trigger === 'click') {
197 var oldFn_1 = props.onClick || noop;
198 var hideFn_1 = function () {
199 clearTimeout(_this.hideTimeout);
200 _this.hideTimeout = window.setTimeout(function () { return _this.hide(); }, CLICK_TIMEOUT);
201 };
202 props.onClick = function (e) {
203 showFn_1(e);
204 hideFn_1();
205 oldFn_1(e);
206 };
207 }
208 else {
209 var oldMEFn_1 = props.onMouseEnter || noop;
210 props.onMouseEnter = function (e) {
211 showFn_1(e);
212 oldMEFn_1(e);
213 if (e.target.disabled) {
214 _this.hideOnMouseOut = e.target;
215 }
216 };
217 var oldMLFn_1 = props.onMouseLeave || noop;
218 props.onMouseLeave = function (e) {
219 _this.hide();
220 oldMLFn_1(e);
221 };
222 }
223 }
224 }
225 return props;
226 };
227 Tooltips.prototype.show = function (e, tooltipText, options) {
228 this.initialiseElements();
229 this.component.show(e, tooltipText, options);
230 };
231 Tooltips.prototype.hide = function () {
232 this.initialiseElements();
233 this.component.hide();
234 };
235 return Tooltips;
236}());
237export { Tooltips };
238var templateObject_1, templateObject_2, templateObject_3, templateObject_4, templateObject_5, templateObject_6;