This repository has been archived by the owner on Nov 6, 2020. It is now read-only.
/
Tooltip.js
156 lines (139 loc) · 4.25 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React, { Component, createRef } from 'react'
import { createPortal } from 'react-dom'
import propTypes from '@dhis2/prop-types'
import { resolve } from 'styled-jsx/css'
import { Popper } from './Popper.js'
import { colors, layers } from './theme.js'
;('') // TODO: https://github.com/jsdoc/jsdoc/issues/1718
const TOOLTIP_OFFSET = 4
const popperStyle = resolve`
z-index: ${layers.applicationTop};
pointer-events: none;
`
const offsetModifier = {
name: 'offset',
options: {
offset: [0, TOOLTIP_OFFSET],
},
}
const OPEN_DELAY = 200
const CLOSE_DELAY = 400
/**
* @module
* @param {Tooltip.PropTypes} props
* @returns {React.Component}
*
* @example import { Tooltip } from '@dhis2/ui-core'
*
* @see Specification: {@link https://github.com/dhis2/design-system/blob/master/molecules/tooltip.md|Design system}
* @see Live demo: {@link /demo/?path=/story/tooltips--default-fluid|Storybook}
*/
class Tooltip extends Component {
state = { open: false }
ref = createRef()
openTimeout = null
closeTimeout = null
componentWillUnmount() {
clearTimeout(this.openTimeout)
clearTimeout(this.closeTimeout)
}
onMouseOver = () => {
clearTimeout(this.closeTimeout)
this.openTimeout = setTimeout(() => {
this.setState({ open: true })
}, OPEN_DELAY)
}
onMouseOut = () => {
clearTimeout(this.openTimeout)
this.closeTimeout = setTimeout(() => {
this.setState({ open: false })
}, CLOSE_DELAY)
}
render() {
const {
children,
className,
content,
dataTest,
maxWidth,
placement,
} = this.props
const { open } = this.state
return (
<>
{typeof children === 'function' ? (
children({
onMouseOver: this.onMouseOver,
onMouseOut: this.onMouseOut,
ref: this.ref,
})
) : (
<span
onMouseOver={this.onMouseOver}
onMouseOut={this.onMouseOut}
ref={this.ref}
data-test={`${dataTest}-reference`}
>
{children}
</span>
)}
{open &&
createPortal(
<Popper
className={popperStyle.className}
placement={placement}
reference={this.ref}
modifiers={[offsetModifier]}
>
<div
className={className}
data-test={`${dataTest}-content`}
>
{content}
</div>
</Popper>,
document.body
)}
{popperStyle.styles}
<style jsx>{`
div {
max-width: ${maxWidth}px;
}
div {
padding: 6px 8px;
background-color: ${colors.grey800};
border-radius: 3px;
color: ${colors.white};
font-size: 12px;
line-height: 16px;
}
`}</style>
</>
)
}
}
Tooltip.defaultProps = {
dataTest: 'dhis2-uicore-tooltip',
maxWidth: 300,
placement: 'top',
tag: 'span',
}
/**
* @typedef {Object} PropTypes
* @static
* @prop {Node} [children]
* @prop {string} [className]
* @prop {Node} [content]
* @prop {string} [dataTest=dhis2-uicore-tooltip]
* @prop {number} [maxWidth=300]
* @prop {('top'|'bottom'|'right'|'left')} [placement=top]
*/
Tooltip.propTypes = {
children: propTypes.node,
className: propTypes.string,
content: propTypes.node,
dataTest: propTypes.string,
maxWidth: propTypes.number,
placement: propTypes.oneOf(['top', 'right', 'bottom', 'left']),
}
export { Tooltip, TOOLTIP_OFFSET }