/
use-tooltip.ts
242 lines (220 loc) · 5.67 KB
/
use-tooltip.ts
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import { useDisclosure, useEventListener, useId } from "@chakra-ui/hooks"
import { popperCSSVars, usePopper, UsePopperProps } from "@chakra-ui/popper"
import { mergeRefs, PropGetter } from "@chakra-ui/react-utils"
import { callAllHandlers, px } from "@chakra-ui/utils"
import * as React from "react"
export interface UseTooltipProps
extends Pick<
UsePopperProps,
| "modifiers"
| "gutter"
| "offset"
| "arrowPadding"
| "direction"
| "placement"
> {
/**
* Delay (in ms) before showing the tooltip
* @default 0ms
*/
openDelay?: number
/**
* Delay (in ms) before hiding the tooltip
* @default 0ms
*/
closeDelay?: number
/**
* If `true`, the tooltip will hide on click
*/
closeOnClick?: boolean
/**
* If `true`, the tooltip will hide while the mouse
* is down
*/
closeOnMouseDown?: boolean
/**
* If `true`, the tooltip will hide on pressing Esc key
*/
closeOnEsc?: boolean
/**
* Callback to run when the tooltip shows
*/
onOpen?(): void
/**
* Callback to run when the tooltip hides
*/
onClose?(): void
/**
* Custom `id` to use in place of `uuid`
*/
id?: string
/**
* If `true`, the tooltip will be shown (in controlled mode)
*/
isOpen?: boolean
/**
* If `true`, the tooltip will be initially shown
*/
defaultIsOpen?: boolean
isDisabled?: boolean
arrowSize?: number
arrowShadowColor?: string
}
export function useTooltip(props: UseTooltipProps = {}) {
const {
openDelay = 0,
closeDelay = 0,
closeOnClick = true,
closeOnMouseDown,
closeOnEsc = true,
onOpen: onOpenProp,
onClose: onCloseProp,
placement,
id,
isOpen: isOpenProp,
defaultIsOpen,
arrowSize = 10,
arrowShadowColor,
arrowPadding,
modifiers,
isDisabled,
gutter,
offset,
direction,
...htmlProps
} = props
const { isOpen, onOpen, onClose } = useDisclosure({
isOpen: isOpenProp,
defaultIsOpen,
onOpen: onOpenProp,
onClose: onCloseProp,
})
const { referenceRef, getPopperProps, getArrowInnerProps, getArrowProps } =
usePopper({
enabled: isOpen,
placement,
arrowPadding,
modifiers,
gutter,
offset,
direction,
})
const tooltipId = useId(id, "tooltip")
const ref = React.useRef<any>(null)
const enterTimeout = React.useRef<number>()
const exitTimeout = React.useRef<number>()
const openWithDelay = React.useCallback(() => {
if (!isDisabled) {
enterTimeout.current = window.setTimeout(onOpen, openDelay)
}
}, [isDisabled, onOpen, openDelay])
const closeWithDelay = React.useCallback(() => {
if (enterTimeout.current) {
clearTimeout(enterTimeout.current)
}
exitTimeout.current = window.setTimeout(onClose, closeDelay)
}, [closeDelay, onClose])
const onClick = React.useCallback(() => {
if (closeOnClick) {
closeWithDelay()
}
}, [closeOnClick, closeWithDelay])
const onMouseDown = React.useCallback(() => {
if (closeOnMouseDown) {
closeWithDelay()
}
}, [closeOnMouseDown, closeWithDelay])
const onKeyDown = React.useCallback(
(event: KeyboardEvent) => {
if (isOpen && event.key === "Escape") {
closeWithDelay()
}
},
[isOpen, closeWithDelay],
)
useEventListener("keydown", closeOnEsc ? onKeyDown : undefined)
React.useEffect(
() => () => {
clearTimeout(enterTimeout.current)
clearTimeout(exitTimeout.current)
},
[],
)
/**
* This allows for catching mouseleave events when the tooltip
* trigger is disabled. There's currently a known issue in
* React regarding the onMouseLeave polyfill.
* @see https://github.com/facebook/react/issues/11972
*/
useEventListener("mouseleave", closeWithDelay, () => ref.current)
const getTriggerProps: PropGetter = React.useCallback(
(props = {}, _ref = null) => {
const triggerProps = {
...props,
ref: mergeRefs(ref, _ref, referenceRef),
onMouseEnter: callAllHandlers(props.onMouseEnter, openWithDelay),
onClick: callAllHandlers(props.onClick, onClick),
onMouseDown: callAllHandlers(props.onMouseDown, onMouseDown),
onFocus: callAllHandlers(props.onFocus, openWithDelay),
onBlur: callAllHandlers(props.onBlur, closeWithDelay),
"aria-describedby": isOpen ? tooltipId : undefined,
}
return triggerProps
},
[
openWithDelay,
closeWithDelay,
onMouseDown,
isOpen,
tooltipId,
onClick,
referenceRef,
],
)
const getTooltipPositionerProps: PropGetter = React.useCallback(
(props = {}, forwardedRef = null) =>
getPopperProps(
{
...props,
style: {
...props.style,
[popperCSSVars.arrowSize.var]: arrowSize
? px(arrowSize)
: undefined,
[popperCSSVars.arrowShadowColor.var]: arrowShadowColor,
},
},
forwardedRef,
),
[getPopperProps, arrowSize, arrowShadowColor],
)
const getTooltipProps = React.useCallback(
(props = {}, _ref = null) => {
const tooltipProps = {
ref: _ref,
...htmlProps,
...props,
id: tooltipId,
role: "tooltip",
style: {
...props.style,
position: "relative",
transformOrigin: popperCSSVars.transformOrigin.varRef,
},
}
return tooltipProps
},
[htmlProps, tooltipId],
)
return {
isOpen,
show: openWithDelay,
hide: closeWithDelay,
getTriggerProps,
getTooltipProps,
getTooltipPositionerProps,
getArrowProps,
getArrowInnerProps,
}
}
export type UseTooltipReturn = ReturnType<typeof useTooltip>