/
Tippy.js
60 lines (51 loc) · 1.29 KB
/
Tippy.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
import React from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
import tippy from 'tippy.js'
const getNativeTippyProps = props => {
const { children, onCreate, ...tippyProps } = props
return tippyProps
}
class Tippy extends React.Component {
content = React.createRef()
static propTypes = {
content: PropTypes.oneOfType([PropTypes.string, PropTypes.element])
.isRequired,
children: PropTypes.element.isRequired
}
getContent() {
return React.isValidElement(this.props.content)
? this.content.current
: this.props.content
}
componentDidMount() {
this.tip = tippy.one(ReactDOM.findDOMNode(this), {
...getNativeTippyProps(this.props),
content: this.getContent()
})
this.props.onCreate && this.props.onCreate(this.tip)
}
componentDidUpdate() {
this.tip.set({
...getNativeTippyProps(this.props),
content: this.getContent()
})
}
componentWillUnmount() {
if (this.tip) {
this.tip.destroy()
this.tip = null
}
}
render() {
return (
<React.Fragment>
{this.props.children}
{React.isValidElement(this.props.content) && (
<div ref={this.content}>{this.props.content}</div>
)}
</React.Fragment>
)
}
}
export default Tippy