-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathebay-tooltip.tsx
96 lines (85 loc) · 2.52 KB
/
ebay-tooltip.tsx
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
import React, { CSSProperties, FC, useRef } from 'react'
import { findComponent } from '../common/component-utils'
import {
Tooltip,
TooltipHost,
TooltipContent,
TooltipProps,
PointerDirection,
useTooltip
} from '../common/tooltip-utils'
import EbayTooltipContent from './ebay-tooltip-content'
import EbayTooltipHost from './ebay-tooltip-host'
// @todo: this type is weird, we should improve it
type Props = Omit<TooltipProps, 'ref'> & {
noHover?: boolean
onExpand?: () => void
onCollapse?: () => void
pointer?: PointerDirection
overlayStyle?: CSSProperties
}
const EbayTooltip: FC<Props> = ({
className,
pointer,
overlayStyle,
noHover,
onFocus = () => {},
onBlur = () => {},
onMouseEnter = () => {},
onMouseLeave = () => {},
onExpand,
onCollapse,
children,
...rest
}) => {
const { isExpanded, expandTooltip, collapseTooltip } = useTooltip({ onCollapse, onExpand })
const timeoutRef = useRef<ReturnType<typeof setTimeout>>()
const handleOnMouseEnter = (event) => {
onMouseEnter(event)
if (!noHover) {
clearTimeout(timeoutRef.current)
expandTooltip()
}
}
const handleOnMouseLeave = (event) => {
onMouseLeave(event)
if (!noHover) {
clearTimeout(timeoutRef.current)
timeoutRef.current = setTimeout(() => {
collapseTooltip()
}, 300)
}
}
const handleOnFocus = (event) => {
onFocus(event)
expandTooltip()
}
const handleOnBlur = (event) => {
onBlur(event)
collapseTooltip()
}
const content = findComponent(children, EbayTooltipContent)
const host = findComponent(children, EbayTooltipHost)
if (!host) {
throw new Error(`EbayTooltip: Please use a EbayTooltipHost that defines the host of the tooltip`)
}
if (!content) {
throw new Error(`EbayTooltip: Please use a EbayTooltipContent that defines the content of the tooltip`)
}
return (
<Tooltip
{...rest}
className={className}
type="tooltip"
isExpanded={isExpanded}
onFocus={handleOnFocus}
onBlur={handleOnBlur}
onMouseEnter={handleOnMouseEnter}
onMouseLeave={handleOnMouseLeave}
>
<TooltipHost {...host.props} />
<TooltipContent {...content.props} type="tooltip" style={overlayStyle} pointer={pointer} />
</Tooltip>
)
}
export default EbayTooltip