-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tooltip.tsx
88 lines (77 loc) · 2.2 KB
/
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
import React, { Children, ReactNode, cloneElement, forwardRef } from 'react';
import {
TooltipProps as DefaultTooltipProps,
TooltipPopup,
useTooltip,
} from '@reach/tooltip';
import { animated, useTransition } from '@react-spring/web';
import type * as Polymorphic from '@reach/utils/polymorphic';
import { Stack } from '../../Stack';
import { TooltipStyle } from './styles';
const AnimatedTooltipContent = animated(TooltipPopup);
interface TooltipProps
extends Omit<DefaultTooltipProps, 'label' | 'ariaLabel'> {
content: ReactNode;
}
const Tooltip = forwardRef(
({ children, id, DEBUG_STYLE, content, ...props }, forwardedRef) => {
const child = Children.only(children) as any;
const [trigger, tooltip, isVisible] = useTooltip({
id,
onMouseEnter: child.props.onMouseEnter,
onMouseMove: child.props.onMouseMove,
onMouseLeave: child.props.onMouseLeave,
onFocus: child.props.onFocus,
onBlur: child.props.onBlur,
onKeyDown: child.props.onKeyDown,
onMouseDown: child.props.onMouseDown,
ref: child.ref,
DEBUG_STYLE,
});
const transitions = useTransition(isVisible ? tooltip : null, {
from: {
opacity: 0,
transform: 'scale(0.9)',
},
enter: {
opacity: 1,
transform: 'scale(1)',
},
leave: {
opacity: 0,
transform: 'scale(1)',
},
config: (item) => ({
mass: 1,
tension: item ? 500 : 1500,
friction: item ? 40 : 1200,
}),
});
return (
<Stack>
{(stackingOrder) => (
<>
<TooltipStyle />
{cloneElement(child, trigger)}
{transitions(
(style, item) =>
item && (
<AnimatedTooltipContent
ref={forwardedRef}
label={content}
{...item}
{...props}
style={{
...style,
zIndex: stackingOrder,
}}
/>
)
)}
</>
)}
</Stack>
);
}
) as Polymorphic.ForwardRefComponent<'div', TooltipProps>;
export { Tooltip };