forked from coreui/coreui-react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTooltip.js
83 lines (71 loc) · 1.75 KB
/
CTooltip.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
import React, {useState, useEffect, useRef, useCallback} from 'react'
import PropTypes from 'prop-types'
import tippy from 'tippy.js'
import 'tippy.js/dist/tippy.css'
import { renderToString } from 'react-dom/server'
//component - CoreUI / CTooltip
const CTooltip = props=>{
let {
//
children,
content,
interactive,
placement,
trigger,
advancedOptions
} = props
const computedContent = useCallback(
() => typeof content === 'string' ? content : renderToString(content),
[content]
)
const config = {
allowHTML: true,
content: computedContent,
interactive,
placement,
trigger,
...advancedOptions
}
const key = useState(Math.random().toString(36).substr(2))[0]
const instance = useRef()
useEffect(() => {
if (instance.current) {
instance.current.setProps(config)
}
})
useEffect(() => {
const node = document.querySelector(`[data-tooltip="${key}"]`)
instance.current = tippy(node, config)
return () => instance.current.destroy()
}, [key])
return (
<React.Fragment>
{
React.cloneElement(children, {
'data-tooltip': key
})
}
</React.Fragment>
)
}
CTooltip.propTypes = {
children: PropTypes.node,
content: PropTypes.oneOfType([PropTypes.string, PropTypes.node]),
interactive: PropTypes.bool,
placement: PropTypes.oneOf([
'', 'top-end', 'top', 'top-start',
'bottom-end', 'bottom', 'bottom-start',
'right-start', 'right', 'right-end',
'left-start', 'left', 'left-end'
]),
trigger: PropTypes.string,
advancedOptions: PropTypes.object
}
CTooltip.defaultProps = {
content: '',
interactive: false,
placement: 'top',
trigger: 'mouseenter focus',
advancedOptions: {}
}
export default CTooltip