@@ -19,6 +19,7 @@ const Tooltip = ({
19
19
place = 'top' ,
20
20
offset = 10 ,
21
21
events = [ 'hover' ] ,
22
+ openOnClick = false ,
22
23
positionStrategy = 'absolute' ,
23
24
middlewares,
24
25
wrapper : WrapperElement ,
@@ -60,6 +61,8 @@ const Tooltip = ({
60
61
const [ anchorsBySelect , setAnchorsBySelect ] = useState < HTMLElement [ ] > ( [ ] )
61
62
const mounted = useRef ( false )
62
63
64
+ const shouldOpenOnClick = openOnClick || events . includes ( 'click' )
65
+
63
66
/**
64
67
* useLayoutEffect runs before useEffect,
65
68
* but should be used carefully because of caveats
@@ -255,10 +258,11 @@ const Tooltip = ({
255
258
256
259
const handleClickOutsideAnchors = ( event : MouseEvent ) => {
257
260
const anchorById = document . querySelector < HTMLElement > ( `[id='${ anchorId } ']` )
258
- if ( anchorById ?. contains ( event . target as HTMLElement ) ) {
261
+ const anchors = [ anchorById , ...anchorsBySelect ]
262
+ if ( anchors . some ( ( anchor ) => anchor ?. contains ( event . target as HTMLElement ) ) ) {
259
263
return
260
264
}
261
- if ( anchorsBySelect . some ( ( anchor ) => anchor . contains ( event . target as HTMLElement ) ) ) {
265
+ if ( tooltipRef . current ? .contains ( event . target as HTMLElement ) ) {
262
266
return
263
267
}
264
268
handleShow ( false )
@@ -294,12 +298,10 @@ const Tooltip = ({
294
298
295
299
const enabledEvents : { event : string ; listener : ( event ?: Event ) => void } [ ] = [ ]
296
300
297
- if ( events . find ( ( event : string ) => event === 'click' ) ) {
301
+ if ( shouldOpenOnClick ) {
298
302
window . addEventListener ( 'click' , handleClickOutsideAnchors )
299
303
enabledEvents . push ( { event : 'click' , listener : handleClickTooltipAnchor } )
300
- }
301
-
302
- if ( events . find ( ( event : string ) => event === 'hover' ) ) {
304
+ } else {
303
305
enabledEvents . push (
304
306
{ event : 'mouseenter' , listener : debouncedHandleShowTooltip } ,
305
307
{ event : 'mouseleave' , listener : debouncedHandleHideTooltip } ,
@@ -322,7 +324,7 @@ const Tooltip = ({
322
324
handleHideTooltip ( )
323
325
}
324
326
325
- if ( clickable ) {
327
+ if ( clickable && ! shouldOpenOnClick ) {
326
328
tooltipRef . current ?. addEventListener ( 'mouseenter' , handleMouseEnterTooltip )
327
329
tooltipRef . current ?. addEventListener ( 'mouseleave' , handleMouseLeaveTooltip )
328
330
}
@@ -334,13 +336,13 @@ const Tooltip = ({
334
336
} )
335
337
336
338
return ( ) => {
337
- if ( events . find ( ( event : string ) => event === 'click' ) ) {
339
+ if ( shouldOpenOnClick ) {
338
340
window . removeEventListener ( 'click' , handleClickOutsideAnchors )
339
341
}
340
342
if ( closeOnEsc ) {
341
343
window . removeEventListener ( 'keydown' , handleEsc )
342
344
}
343
- if ( clickable ) {
345
+ if ( clickable && ! shouldOpenOnClick ) {
344
346
tooltipRef . current ?. removeEventListener ( 'mouseenter' , handleMouseEnterTooltip )
345
347
tooltipRef . current ?. removeEventListener ( 'mouseleave' , handleMouseLeaveTooltip )
346
348
}
0 commit comments