This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
tooltip.js
131 lines (112 loc) · 3.31 KB
/
tooltip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import {Disposable} from 'event-kit';
import {RefHolderPropType} from '../prop-types';
import {createItem} from '../helpers';
const VERBATIM_OPTION_PROPS = [
'title', 'html', 'placement', 'trigger', 'keyBindingCommand', 'keyBindingTarget',
];
const OPTION_PROPS = [
...VERBATIM_OPTION_PROPS,
'tooltips', 'className', 'showDelay', 'hideDelay',
];
export default class Tooltip extends React.Component {
static propTypes = {
manager: PropTypes.object.isRequired,
target: RefHolderPropType.isRequired,
title: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]),
html: PropTypes.bool,
className: PropTypes.string,
placement: PropTypes.oneOfType([
PropTypes.string,
PropTypes.func,
]),
trigger: PropTypes.oneOf(['hover', 'click', 'focus', 'manual']),
showDelay: PropTypes.number,
hideDelay: PropTypes.number,
keyBindingCommand: PropTypes.string,
keyBindingTarget: PropTypes.element,
children: PropTypes.element,
itemHolder: RefHolderPropType,
}
static defaultProps = {
getItemComponent: () => {},
}
constructor(props, context) {
super(props, context);
this.refSub = new Disposable();
this.tipSub = new Disposable();
this.domNode = null;
if (this.props.children !== undefined) {
this.domNode = document.createElement('div');
this.domNode.className = 'react-atom-tooltip';
}
this.lastTooltipProps = {};
}
componentDidMount() {
this.setupTooltip();
}
render() {
if (this.props.children !== undefined) {
return ReactDOM.createPortal(
this.props.children,
this.domNode,
);
} else {
return null;
}
}
componentDidUpdate() {
if (this.shouldRecreateTooltip()) {
this.refSub.dispose();
this.tipSub.dispose();
this.setupTooltip();
}
}
componentWillUnmount() {
this.refSub.dispose();
this.tipSub.dispose();
}
getTooltipProps() {
const p = {};
for (const key of OPTION_PROPS) {
p[key] = this.props[key];
}
return p;
}
shouldRecreateTooltip() {
return OPTION_PROPS.some(key => this.lastTooltipProps[key] !== this.props[key]);
}
setupTooltip() {
this.lastTooltipProps = this.getTooltipProps();
const options = {};
VERBATIM_OPTION_PROPS.forEach(key => {
if (this.props[key] !== undefined) {
options[key] = this.props[key];
}
});
if (this.props.className !== undefined) {
options.class = this.props.className;
}
if (this.props.showDelay !== undefined || this.props.hideDelay !== undefined) {
const delayDefaults = (this.props.trigger === 'hover' || this.props.trigger === undefined)
&& {show: 1000, hide: 100}
|| {show: 0, hide: 0};
options.delay = {
show: this.props.showDelay !== undefined ? this.props.showDelay : delayDefaults.show,
hide: this.props.hideDelay !== undefined ? this.props.hideDelay : delayDefaults.hide,
};
}
if (this.props.children !== undefined) {
options.item = createItem(this.domNode, this.props.itemHolder);
}
this.refSub = this.props.target.observe(t => {
this.tipSub.dispose();
this.tipSub = this.props.manager.add(t, options);
});
}
}