-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.js
174 lines (151 loc) · 4.82 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/** @jsx jsx */
import { jsx } from '@emotion/core';
import { Component, createRef, Fragment } from 'react';
import { createPortal } from 'react-dom';
import flushable from 'flushable';
import styled from '@emotion/styled';
import { Popper } from 'react-popper';
import { TransitionProvider, fade } from '@arch-ui/modal-utils';
import { colors, gridSize } from '@arch-ui/theme';
// ==============================
// Styled Component
// ==============================
const TooltipElement = styled.div({
backgroundColor: colors.N80,
borderRadius: 3,
color: 'white',
fontSize: '0.75rem',
fontWeight: 500,
padding: `${gridSize / 2}px ${gridSize}px`,
pointerEvents: 'none', // tooltips are non-interactive, they shouldn't get in the way of other elements
zIndex: 2,
});
let TooltipPositioner = props => {
return createPortal(
<Popper
referenceElement={props.targetNode}
placement={props.placement}
modifiers={{ hide: { enabled: false }, preventOverflow: { enabled: false } }}
>
{({ ref, style }) => (
<div ref={ref} css={{ zIndex: 2000 }} style={{ ...props.style, ...style }}>
<div css={{ margin: gridSize }}>
<TooltipElement className={props.className}>{props.children}</TooltipElement>
</div>
</div>
)}
</Popper>,
document.body
);
};
// ==============================
// Stateful Component
// ==============================
const LISTENER_OPTIONS = { passive: true };
const NOOP = () => {};
let pendingHide;
const showTooltip = (fn, defaultDelay) => {
const isHidePending = pendingHide && pendingHide.pending();
if (isHidePending) {
pendingHide.flush();
}
const pendingShow = flushable(() => fn(isHidePending), isHidePending ? 0 : defaultDelay);
return pendingShow.cancel;
};
const hideTooltip = (fn, defaultDelay) => {
pendingHide = flushable(flushed => fn(flushed), defaultDelay);
return pendingHide.cancel;
};
export default class Tooltip extends Component {
state = {
immediatelyHide: false,
immediatelyShow: false,
isVisible: false,
};
ref = createRef();
cancelPendingSetState = NOOP;
static defaultProps = {
delay: 300,
placement: 'bottom',
};
componentDidMount() {
const target = this.ref.current;
if (!target) {
throw new Error('You must pass the ref onto your target node.');
}
if (!target.nodeName) {
throw new Error(
"It looks like you've passed the ref onto a component. You must pass the ref onto your target node."
);
}
target.addEventListener('mouseenter', this.handleMouseEnter, LISTENER_OPTIONS);
target.addEventListener('mouseleave', this.handleMouseLeave, LISTENER_OPTIONS);
}
componentWillUnmount() {
this.cancelPendingSetState();
const target = this.ref.current;
if (target) {
target.removeEventListener('mouseenter', this.handleMouseEnter, LISTENER_OPTIONS);
target.removeEventListener('mouseleave', this.handleMouseLeave, LISTENER_OPTIONS);
}
}
cancel = () => {
this.cancelPendingSetState();
this.setState({ isVisible: false, immediatelyHide: true });
};
handleMouseEnter = () => {
this.cancelPendingSetState();
if (this.state.isVisible) {
return;
}
if (this.props.hideOnMouseDown && this.ref.current) {
this.ref.current.addEventListener('mousedown', this.cancel, LISTENER_OPTIONS);
}
if (this.props.hideOnKeyDown) {
document.addEventListener('keydown', this.cancel, LISTENER_OPTIONS);
}
this.cancelPendingSetState = showTooltip(immediatelyShow => {
this.setState({
isVisible: true,
immediatelyShow,
});
}, this.props.delay);
};
handleMouseLeave = () => {
this.cancelPendingSetState();
if (!this.state.isVisible) {
return;
}
if (this.props.hideOnMouseDown && this.ref.current) {
this.ref.current.removeEventListener('mousedown', this.cancel, LISTENER_OPTIONS);
}
if (this.props.hideOnKeyDown) {
document.removeEventListener('keydown', this.cancel, LISTENER_OPTIONS);
}
this.cancelPendingSetState = hideTooltip(immediatelyHide => {
this.setState({ isVisible: false, immediatelyHide });
}, this.props.delay);
};
render() {
const { children, content, onHide, onShow, placement, className } = this.props;
const { isVisible } = this.state;
const ref = this.ref;
return (
<Fragment>
{children(ref)}
<TransitionProvider isOpen={isVisible} onEntered={onShow} onExited={onHide}>
{transitionState => (
<TooltipPositioner
targetNode={this.ref.current}
placement={placement}
className={className}
style={fade(transitionState)}
>
{content}
</TooltipPositioner>
)}
</TransitionProvider>
</Fragment>
);
}
}